xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 2ef79ecb5e906d87475d3e0c49b22425499a89f3)
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 */
183969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1848bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
18560b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
186dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
187bba681cbSJaegeuk Kim 
18875ab4cb8SJaegeuk Kim struct cp_control {
18975ab4cb8SJaegeuk Kim 	int reason;
1904b2fecc8SJaegeuk Kim 	__u64 trim_start;
1914b2fecc8SJaegeuk Kim 	__u64 trim_end;
1924b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
19375ab4cb8SJaegeuk Kim };
19475ab4cb8SJaegeuk Kim 
195662befdaSChao Yu /*
19681c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
197662befdaSChao Yu  */
198662befdaSChao Yu enum {
199662befdaSChao Yu 	META_CP,
200662befdaSChao Yu 	META_NAT,
20181c1a0f1SChao Yu 	META_SIT,
2024c521f49SJaegeuk Kim 	META_SSA,
2034c521f49SJaegeuk Kim 	META_POR,
204662befdaSChao Yu };
205662befdaSChao Yu 
2066451e041SJaegeuk Kim /* for the list of ino */
2076451e041SJaegeuk Kim enum {
2086451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
209fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
210fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2110a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
21239d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2136451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2146451e041SJaegeuk Kim };
2156451e041SJaegeuk Kim 
2166451e041SJaegeuk Kim struct ino_entry {
21739a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
21839a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
21939d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
22039a53e0cSJaegeuk Kim };
22139a53e0cSJaegeuk Kim 
2222710fd7eSChao Yu /* for the list of inodes to be GCed */
22306292073SChao Yu struct inode_entry {
22439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
22539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
22639a53e0cSJaegeuk Kim };
22739a53e0cSJaegeuk Kim 
228a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2297fd9e544SJaegeuk Kim struct discard_entry {
2307fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
231a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
232a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2337fd9e544SJaegeuk Kim };
2347fd9e544SJaegeuk Kim 
235969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
236969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
237969d1b18SChao Yu 
238ba48a33eSChao Yu /* max discard pend list number */
239ba48a33eSChao Yu #define MAX_PLIST_NUM		512
240ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
241ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
242ba48a33eSChao Yu 
24315469963SJaegeuk Kim enum {
24415469963SJaegeuk Kim 	D_PREP,
24515469963SJaegeuk Kim 	D_SUBMIT,
24615469963SJaegeuk Kim 	D_DONE,
24715469963SJaegeuk Kim };
24815469963SJaegeuk Kim 
249004b6862SChao Yu struct discard_info {
250b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
251b01a9201SJaegeuk Kim 	block_t len;			/* length */
252004b6862SChao Yu 	block_t start;			/* actual start address in dev */
253004b6862SChao Yu };
254004b6862SChao Yu 
255b01a9201SJaegeuk Kim struct discard_cmd {
256004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
257004b6862SChao Yu 	union {
258004b6862SChao Yu 		struct {
259004b6862SChao Yu 			block_t lstart;	/* logical start address */
260004b6862SChao Yu 			block_t len;	/* length */
261004b6862SChao Yu 			block_t start;	/* actual start address in dev */
262004b6862SChao Yu 		};
263004b6862SChao Yu 		struct discard_info di;	/* discard info */
264004b6862SChao Yu 
265004b6862SChao Yu 	};
266b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
267b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
268c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
269ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2709a744b92SChao Yu 	unsigned char state;		/* state */
271c81abe34SJaegeuk Kim 	int error;			/* bio error */
272275b66b0SChao Yu };
273275b66b0SChao Yu 
27478997b56SChao Yu enum {
27578997b56SChao Yu 	DPOLICY_BG,
27678997b56SChao Yu 	DPOLICY_FORCE,
27778997b56SChao Yu 	DPOLICY_FSTRIM,
27878997b56SChao Yu 	DPOLICY_UMOUNT,
27978997b56SChao Yu 	MAX_DPOLICY,
28078997b56SChao Yu };
28178997b56SChao Yu 
282ecc9aa00SChao Yu struct discard_policy {
28378997b56SChao Yu 	int type;			/* type of discard */
284ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
285ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
286ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
287ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
288ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
289ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
29078997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
291ecc9aa00SChao Yu };
292ecc9aa00SChao Yu 
2930b54fb84SJaegeuk Kim struct discard_cmd_control {
29415469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
29546f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
296ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
29746f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
2988412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
29915469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
300969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
30115469963SJaegeuk Kim 	struct mutex cmd_lock;
302d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
303d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
304969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
305d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
3068b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
3078b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
3085f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
309004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
31039a53e0cSJaegeuk Kim };
31139a53e0cSJaegeuk Kim 
31239a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
31339a53e0cSJaegeuk Kim struct fsync_inode_entry {
31439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
31539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
316c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
317c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
31839a53e0cSJaegeuk Kim };
31939a53e0cSJaegeuk Kim 
32068afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
32168afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
32239a53e0cSJaegeuk Kim 
32368afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
32468afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
32568afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
32668afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
32739a53e0cSJaegeuk Kim 
328dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
329dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
330309cc2b6SJaegeuk Kim 
331dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
33239a53e0cSJaegeuk Kim {
333dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
334cac5a3d8SDongOh Shin 
335dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
33639a53e0cSJaegeuk Kim 	return before;
33739a53e0cSJaegeuk Kim }
33839a53e0cSJaegeuk Kim 
339dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
34039a53e0cSJaegeuk Kim {
341dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
342cac5a3d8SDongOh Shin 
343dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
34439a53e0cSJaegeuk Kim 	return before;
34539a53e0cSJaegeuk Kim }
34639a53e0cSJaegeuk Kim 
347dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
348dfc08a12SChao Yu 							int size, int type)
349184a5cd2SChao Yu {
350184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
351dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
352dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
353184a5cd2SChao Yu }
354184a5cd2SChao Yu 
35539a53e0cSJaegeuk Kim /*
356e9750824SNamjae Jeon  * ioctl commands
357e9750824SNamjae Jeon  */
358e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
359e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
360d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
361e9750824SNamjae Jeon 
36288b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
36388b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
36488b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
36502a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3661e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3671e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
368d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
369456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
370d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
371d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3724dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3734dd6f977SJaegeuk Kim 						struct f2fs_move_range)
374e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
375e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
37634dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
37734dc77adSJaegeuk Kim 						struct f2fs_gc_range)
378e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3791ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3801ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
381c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
38288b88a66SJaegeuk Kim 
3830b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3840b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3850b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
386f424f664SJaegeuk Kim 
3871abff93dSJaegeuk Kim /*
3881abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3891abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3901abff93dSJaegeuk Kim  */
3911abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3921abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3931abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3941abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
395c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3961abff93dSJaegeuk Kim 
397e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
398e9750824SNamjae Jeon /*
399e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
400e9750824SNamjae Jeon  */
401e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
402e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
40304ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
404e9750824SNamjae Jeon #endif
405e9750824SNamjae Jeon 
4062c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4072c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4082c1d0305SChao Yu 
40934dc77adSJaegeuk Kim struct f2fs_gc_range {
41034dc77adSJaegeuk Kim 	u32 sync;
41134dc77adSJaegeuk Kim 	u64 start;
41234dc77adSJaegeuk Kim 	u64 len;
41334dc77adSJaegeuk Kim };
41434dc77adSJaegeuk Kim 
415d323d005SChao Yu struct f2fs_defragment {
416d323d005SChao Yu 	u64 start;
417d323d005SChao Yu 	u64 len;
418d323d005SChao Yu };
419d323d005SChao Yu 
4204dd6f977SJaegeuk Kim struct f2fs_move_range {
4214dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4224dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4234dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4244dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4254dd6f977SJaegeuk Kim };
4264dd6f977SJaegeuk Kim 
427e066b83cSJaegeuk Kim struct f2fs_flush_device {
428e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
429e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
430e066b83cSJaegeuk Kim };
431e066b83cSJaegeuk Kim 
432f2470371SChao Yu /* for inline stuff */
433f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4346afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4357a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4366afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4377a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4387a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
439b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4406afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
441f2470371SChao Yu 
442f2470371SChao Yu /* for inline dir */
443f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
444f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
445f2470371SChao Yu 				BITS_PER_BYTE + 1))
446f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
447f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
448f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
449f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
450f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
451f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
452f2470371SChao Yu 
453e9750824SNamjae Jeon /*
45439a53e0cSJaegeuk Kim  * For INODE and NODE manager
45539a53e0cSJaegeuk Kim  */
4567b3cd7d6SJaegeuk Kim /* for directory operations */
4577b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
458d8c6822aSJaegeuk Kim 	struct inode *inode;
45976a9dd85SChao Yu 	void *bitmap;
4607b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4617b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4627b3cd7d6SJaegeuk Kim 	int max;
46376a9dd85SChao Yu 	int nr_bitmap;
4647b3cd7d6SJaegeuk Kim };
4657b3cd7d6SJaegeuk Kim 
46664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
46764c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4687b3cd7d6SJaegeuk Kim {
469d8c6822aSJaegeuk Kim 	d->inode = inode;
4707b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
47176a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
472c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
4737b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4747b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
47564c24ecbSTomohiro Kusumi }
476cac5a3d8SDongOh Shin 
47764c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
478f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
47964c24ecbSTomohiro Kusumi {
480f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
481f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
482f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
483f2470371SChao Yu 
48464c24ecbSTomohiro Kusumi 	d->inode = inode;
485f2470371SChao Yu 	d->max = entry_cnt;
486f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
487f2470371SChao Yu 	d->bitmap = t;
488f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
489f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
490f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
4917b3cd7d6SJaegeuk Kim }
4927b3cd7d6SJaegeuk Kim 
493dbe6a5ffSJaegeuk Kim /*
494dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
495dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
496dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
49739a53e0cSJaegeuk Kim  */
498dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
499dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
500266e97a8SJaegeuk Kim enum {
501266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
502266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
503266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
504266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5054f4124d0SChao Yu 					 * by get_data_block.
50639a53e0cSJaegeuk Kim 					 */
507266e97a8SJaegeuk Kim };
508266e97a8SJaegeuk Kim 
509a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
51039a53e0cSJaegeuk Kim 
511817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
512817202d9SChao Yu 
51313054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
51413054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
51513054c54SChao Yu 
51639a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
51713054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
51813054c54SChao Yu 
51913054c54SChao Yu /* number of extent info in extent cache we try to shrink */
52013054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
521c11abd1aSJaegeuk Kim 
52254c2258cSChao Yu struct rb_entry {
52354c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
52454c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
52554c2258cSChao Yu 	unsigned int len;		/* length of the entry */
52654c2258cSChao Yu };
52754c2258cSChao Yu 
52839a53e0cSJaegeuk Kim struct extent_info {
52939a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
530111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
53154c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
53239a53e0cSJaegeuk Kim };
53339a53e0cSJaegeuk Kim 
53413054c54SChao Yu struct extent_node {
53554c2258cSChao Yu 	struct rb_node rb_node;
53654c2258cSChao Yu 	union {
53754c2258cSChao Yu 		struct {
53854c2258cSChao Yu 			unsigned int fofs;
53954c2258cSChao Yu 			unsigned int len;
54054c2258cSChao Yu 			u32 blk;
54154c2258cSChao Yu 		};
54213054c54SChao Yu 		struct extent_info ei;	/* extent info */
54354c2258cSChao Yu 
54454c2258cSChao Yu 	};
54554c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
546201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
54713054c54SChao Yu };
54813054c54SChao Yu 
54913054c54SChao Yu struct extent_tree {
55013054c54SChao Yu 	nid_t ino;			/* inode number */
55113054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
55262c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5533e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
554137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
55513054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
55668e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
55713054c54SChao Yu };
55813054c54SChao Yu 
55939a53e0cSJaegeuk Kim /*
560003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
561003a3e1dSJaegeuk Kim  *
562003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
563003a3e1dSJaegeuk Kim  */
564003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
565003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5667f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5677f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5687f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
569003a3e1dSJaegeuk Kim 
570003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
571003a3e1dSJaegeuk Kim 	block_t m_pblk;
572003a3e1dSJaegeuk Kim 	block_t m_lblk;
573003a3e1dSJaegeuk Kim 	unsigned int m_len;
574003a3e1dSJaegeuk Kim 	unsigned int m_flags;
575da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
576c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
577d5097be5SHyunchul Lee 	int m_seg_type;
578003a3e1dSJaegeuk Kim };
579003a3e1dSJaegeuk Kim 
580e2b4e2bcSChao Yu /* for flag in get_data_block */
581f2220c7fSQiuyang Sun enum {
582f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
583f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
584f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
585f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
586f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
587c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
588f2220c7fSQiuyang Sun };
589e2b4e2bcSChao Yu 
590003a3e1dSJaegeuk Kim /*
59139a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
59239a53e0cSJaegeuk Kim  */
59339a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
594354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
595cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
596e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
59726787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
598b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
59953fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
60039a53e0cSJaegeuk Kim 
601b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
602b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
603b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
604b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
605b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
606b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
607cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
608cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
609cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
610e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
611e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
61226787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
61326787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
614b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
615b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
616b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
617cde4de12SJaegeuk Kim 
618ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
619ab9fa662SJaegeuk Kim 
620*2ef79ecbSChao Yu enum {
621*2ef79ecbSChao Yu 	GC_FAILURE_PIN,
622*2ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
623*2ef79ecbSChao Yu 	MAX_GC_FAILURE
624*2ef79ecbSChao Yu };
625*2ef79ecbSChao Yu 
62639a53e0cSJaegeuk Kim struct f2fs_inode_info {
62739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
62839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
62939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
63038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6311ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
632*2ef79ecbSChao Yu 	/* for gc failure statistic */
633*2ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6346666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
63539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
63639a53e0cSJaegeuk Kim 
63739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
63839a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
639d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
640204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
64139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
64239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
64388c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
644b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
64539a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
64626de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
64788b88a66SJaegeuk Kim 
6480abd675eSChao Yu #ifdef CONFIG_QUOTA
6490abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6500abd675eSChao Yu 
6510abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6520abd675eSChao Yu 	qsize_t i_reserved_quota;
6530abd675eSChao Yu #endif
6540f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6550f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
65657864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
65788b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6587a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
65988b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6603e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
661b2532c69SChao Yu 
662b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
663b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
6645a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
66527161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
666f2470371SChao Yu 
6677a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6685c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6696afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
6701c1d35dfSChao Yu 	struct timespec i_crtime;	/* inode creation time */
671214c2461SJaegeuk Kim 	struct timespec i_disk_time[4];	/* inode disk times */
67239a53e0cSJaegeuk Kim };
67339a53e0cSJaegeuk Kim 
67439a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
675bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
67639a53e0cSJaegeuk Kim {
677bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
678bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
679bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
68039a53e0cSJaegeuk Kim }
68139a53e0cSJaegeuk Kim 
68239a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
68339a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
68439a53e0cSJaegeuk Kim {
68539a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6864d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
68739a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
68839a53e0cSJaegeuk Kim }
68939a53e0cSJaegeuk Kim 
690429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
691429511cdSChao Yu 						u32 blk, unsigned int len)
692429511cdSChao Yu {
693429511cdSChao Yu 	ei->fofs = fofs;
694429511cdSChao Yu 	ei->blk = blk;
695429511cdSChao Yu 	ei->len = len;
696429511cdSChao Yu }
697429511cdSChao Yu 
698004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
699004b6862SChao Yu 						struct discard_info *front)
700004b6862SChao Yu {
7019a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
7029a997188SJaegeuk Kim 		(back->len + front->len < DEF_MAX_DISCARD_LEN);
703004b6862SChao Yu }
704004b6862SChao Yu 
705004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
706004b6862SChao Yu 						struct discard_info *back)
707004b6862SChao Yu {
708004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
709004b6862SChao Yu }
710004b6862SChao Yu 
711004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
712004b6862SChao Yu 						struct discard_info *front)
713004b6862SChao Yu {
714004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
715004b6862SChao Yu }
716004b6862SChao Yu 
717429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
718429511cdSChao Yu 						struct extent_info *front)
719429511cdSChao Yu {
720429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
721429511cdSChao Yu 			back->blk + back->len == front->blk);
722429511cdSChao Yu }
723429511cdSChao Yu 
724429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
725429511cdSChao Yu 						struct extent_info *back)
726429511cdSChao Yu {
727429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
728429511cdSChao Yu }
729429511cdSChao Yu 
730429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
731429511cdSChao Yu 						struct extent_info *front)
732429511cdSChao Yu {
733429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
734429511cdSChao Yu }
735429511cdSChao Yu 
736cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
737205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
738205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
7394abd3f5aSChao Yu {
740205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7414abd3f5aSChao Yu 		et->largest = en->ei;
7427c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
743205b9822SJaegeuk Kim 	}
7444abd3f5aSChao Yu }
7454abd3f5aSChao Yu 
7469a4ffdf5SChao Yu /*
7479a4ffdf5SChao Yu  * For free nid management
7489a4ffdf5SChao Yu  */
7499a4ffdf5SChao Yu enum nid_state {
7509a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7519a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7529a4ffdf5SChao Yu 	MAX_NID_STATE,
753b8559dc2SChao Yu };
754b8559dc2SChao Yu 
75539a53e0cSJaegeuk Kim struct f2fs_nm_info {
75639a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
75739a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
75804d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
75939a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
760cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
761ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7622304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
76339a53e0cSJaegeuk Kim 
76439a53e0cSJaegeuk Kim 	/* NAT cache management */
76539a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
766309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
767b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
76839a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
769309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
770aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
77122ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
77239a53e0cSJaegeuk Kim 
77339a53e0cSJaegeuk Kim 	/* free node ids management */
7748a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7759a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7769a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
777b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
77839a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
779bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
7804ac91242SChao Yu 	unsigned char *nat_block_bitmap;
781586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
78239a53e0cSJaegeuk Kim 
78339a53e0cSJaegeuk Kim 	/* for checkpoint */
78439a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
78522ad0b6aSJaegeuk Kim 
78622ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
78722ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
78822ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
78922ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
790599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
791599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
792599a09b2SChao Yu #endif
79339a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
79439a53e0cSJaegeuk Kim };
79539a53e0cSJaegeuk Kim 
79639a53e0cSJaegeuk Kim /*
79739a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
79839a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
79939a53e0cSJaegeuk Kim  * by the data offset in a file.
80039a53e0cSJaegeuk Kim  */
80139a53e0cSJaegeuk Kim struct dnode_of_data {
80239a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
80339a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
80439a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
80539a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
80639a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
80739a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
80893bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8093cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8103cf45747SChao Yu 	char max_level;			/* level of current page located */
81139a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
81239a53e0cSJaegeuk Kim };
81339a53e0cSJaegeuk Kim 
81439a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
81539a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
81639a53e0cSJaegeuk Kim {
817d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
81839a53e0cSJaegeuk Kim 	dn->inode = inode;
81939a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
82039a53e0cSJaegeuk Kim 	dn->node_page = npage;
82139a53e0cSJaegeuk Kim 	dn->nid = nid;
82239a53e0cSJaegeuk Kim }
82339a53e0cSJaegeuk Kim 
82439a53e0cSJaegeuk Kim /*
82539a53e0cSJaegeuk Kim  * For SIT manager
82639a53e0cSJaegeuk Kim  *
82739a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
82839a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
82939a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
83039a53e0cSJaegeuk Kim  * respectively.
83139a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
83239a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
83339a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
83439a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
83539a53e0cSJaegeuk Kim  * data and 8 for node logs.
83639a53e0cSJaegeuk Kim  */
83739a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
83839a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
83939a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
84039a53e0cSJaegeuk Kim 
84139a53e0cSJaegeuk Kim enum {
84239a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
84339a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
84439a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
84539a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
84639a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
84739a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
84838aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
84939a53e0cSJaegeuk Kim };
85039a53e0cSJaegeuk Kim 
8516b4afdd7SJaegeuk Kim struct flush_cmd {
8526b4afdd7SJaegeuk Kim 	struct completion wait;
853721bd4d5SGu Zheng 	struct llist_node llnode;
85439d787beSChao Yu 	nid_t ino;
8556b4afdd7SJaegeuk Kim 	int ret;
8566b4afdd7SJaegeuk Kim };
8576b4afdd7SJaegeuk Kim 
858a688b9d9SGu Zheng struct flush_cmd_control {
859a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
860a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8618b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8628b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
863721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
864721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
865a688b9d9SGu Zheng };
866a688b9d9SGu Zheng 
86739a53e0cSJaegeuk Kim struct f2fs_sm_info {
86839a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
86939a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
87039a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
87139a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
87239a53e0cSJaegeuk Kim 
8732b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8742b60311dSChao Yu 
87539a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
87639a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
87739a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
87839a53e0cSJaegeuk Kim 
87939a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
88039a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
88139a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
88239a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
88381eb8d6eSJaegeuk Kim 
88481eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
88581eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8867fd9e544SJaegeuk Kim 
887bba681cbSJaegeuk Kim 	/* for batched trimming */
888bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
889bba681cbSJaegeuk Kim 
890184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
891184a5cd2SChao Yu 
892216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
893216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
894c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
895ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
896a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
8976b4afdd7SJaegeuk Kim 
8986b4afdd7SJaegeuk Kim 	/* for flush command control */
899b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
900a688b9d9SGu Zheng 
9010b54fb84SJaegeuk Kim 	/* for discard command control */
9020b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
90339a53e0cSJaegeuk Kim };
90439a53e0cSJaegeuk Kim 
90539a53e0cSJaegeuk Kim /*
90639a53e0cSJaegeuk Kim  * For superblock
90739a53e0cSJaegeuk Kim  */
90839a53e0cSJaegeuk Kim /*
90939a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
91039a53e0cSJaegeuk Kim  *
91139a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
91239a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
91339a53e0cSJaegeuk Kim  */
91436951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
91539a53e0cSJaegeuk Kim enum count_type {
91639a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
917c227f912SChao Yu 	F2FS_DIRTY_DATA,
9182c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
91939a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
92039a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9218dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9220f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
92336951b38SChao Yu 	F2FS_WB_CP_DATA,
92436951b38SChao Yu 	F2FS_WB_DATA,
92539a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
92639a53e0cSJaegeuk Kim };
92739a53e0cSJaegeuk Kim 
92839a53e0cSJaegeuk Kim /*
929e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
93039a53e0cSJaegeuk Kim  * The available types are:
93139a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
93239a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
93339a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
93439a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
93539a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
93639a53e0cSJaegeuk Kim  *			with waiting the bio's completion
93739a53e0cSJaegeuk Kim  * ...			Only can be used with META.
93839a53e0cSJaegeuk Kim  */
9397d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
94039a53e0cSJaegeuk Kim enum page_type {
94139a53e0cSJaegeuk Kim 	DATA,
94239a53e0cSJaegeuk Kim 	NODE,
94339a53e0cSJaegeuk Kim 	META,
94439a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
94539a53e0cSJaegeuk Kim 	META_FLUSH,
9468ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9478ce67cb0SJaegeuk Kim 	INMEM_DROP,
9488c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
94928bc106bSChao Yu 	INMEM_REVOKE,
9508ce67cb0SJaegeuk Kim 	IPU,
9518ce67cb0SJaegeuk Kim 	OPU,
95239a53e0cSJaegeuk Kim };
95339a53e0cSJaegeuk Kim 
954a912b54dSJaegeuk Kim enum temp_type {
955a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
956a912b54dSJaegeuk Kim 	WARM,
957a912b54dSJaegeuk Kim 	COLD,
958a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
959a912b54dSJaegeuk Kim };
960a912b54dSJaegeuk Kim 
961cc15620bSJaegeuk Kim enum need_lock_type {
962cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
963cc15620bSJaegeuk Kim 	LOCK_DONE,
964cc15620bSJaegeuk Kim 	LOCK_RETRY,
965cc15620bSJaegeuk Kim };
966cc15620bSJaegeuk Kim 
967a5fd5050SChao Yu enum cp_reason_type {
968a5fd5050SChao Yu 	CP_NO_NEEDED,
969a5fd5050SChao Yu 	CP_NON_REGULAR,
970a5fd5050SChao Yu 	CP_HARDLINK,
971a5fd5050SChao Yu 	CP_SB_NEED_CP,
972a5fd5050SChao Yu 	CP_WRONG_PINO,
973a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
974a5fd5050SChao Yu 	CP_NODE_NEED_CP,
975a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
976a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
9770a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
978a5fd5050SChao Yu };
979a5fd5050SChao Yu 
980b0af6d49SChao Yu enum iostat_type {
981b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
982b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
983b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
984b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
985b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
986b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
987b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
988b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
989b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
990b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
991b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
992b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
993b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
994b0af6d49SChao Yu 	NR_IO_TYPE,
995b0af6d49SChao Yu };
996b0af6d49SChao Yu 
997458e6197SJaegeuk Kim struct f2fs_io_info {
99805ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
99939d787beSChao Yu 	nid_t ino;		/* inode number */
1000458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1001a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
100204d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1003ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10047a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
100528bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
100605ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10074375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1008fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1009d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1010cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1011fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10120833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1013b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1014578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
1015458e6197SJaegeuk Kim };
1016458e6197SJaegeuk Kim 
101768afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10181ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1019458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10201ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10211ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1022458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1023df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1024fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1025fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10261ff7bd3bSJaegeuk Kim };
10271ff7bd3bSJaegeuk Kim 
10283c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10293c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10303c62be17SJaegeuk Kim struct f2fs_dev_info {
10313c62be17SJaegeuk Kim 	struct block_device *bdev;
10323c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10333c62be17SJaegeuk Kim 	unsigned int total_segments;
10343c62be17SJaegeuk Kim 	block_t start_blk;
10353c62be17SJaegeuk Kim 	block_t end_blk;
10363c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10373c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10383c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10393c62be17SJaegeuk Kim #endif
10403c62be17SJaegeuk Kim };
10413c62be17SJaegeuk Kim 
1042c227f912SChao Yu enum inode_type {
1043c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1044c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10450f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
104657864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1047c227f912SChao Yu 	NR_INODE_TYPE,
1048c227f912SChao Yu };
1049c227f912SChao Yu 
105067298804SChao Yu /* for inner inode cache management */
105167298804SChao Yu struct inode_management {
105267298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
105367298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
105467298804SChao Yu 	struct list_head ino_list;		/* inode list head */
105567298804SChao Yu 	unsigned long ino_num;			/* number of entries */
105667298804SChao Yu };
105767298804SChao Yu 
1058caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1059caf0047eSChao Yu enum {
1060caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1061caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1062caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1063caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1064df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1065bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
1066caf0047eSChao Yu };
1067caf0047eSChao Yu 
10686beceb54SJaegeuk Kim enum {
10696beceb54SJaegeuk Kim 	CP_TIME,
1070d0239e1bSJaegeuk Kim 	REQ_TIME,
10716beceb54SJaegeuk Kim 	MAX_TIME,
10726beceb54SJaegeuk Kim };
10736beceb54SJaegeuk Kim 
10740cdd3195SHyunchul Lee enum {
10755b0e9539SJaegeuk Kim 	GC_NORMAL,
10765b0e9539SJaegeuk Kim 	GC_IDLE_CB,
10775b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
10785b0e9539SJaegeuk Kim 	GC_URGENT,
10795b0e9539SJaegeuk Kim };
10805b0e9539SJaegeuk Kim 
10815b0e9539SJaegeuk Kim enum {
10820cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
10830cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1084f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
10850cdd3195SHyunchul Lee };
10860cdd3195SHyunchul Lee 
108707939627SJaegeuk Kim enum {
108807939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
108907939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
109007939627SJaegeuk Kim };
109107939627SJaegeuk Kim 
109293cf93f1SJunling Zheng enum fsync_mode {
109393cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
109493cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1095d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
109693cf93f1SJunling Zheng };
109793cf93f1SJunling Zheng 
1098ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1099ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1100ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1101ff62af20SSheng Yong #else
1102ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1103ff62af20SSheng Yong #endif
1104ff62af20SSheng Yong 
110539a53e0cSJaegeuk Kim struct f2fs_sb_info {
110639a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11075e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
110839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1109846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1110e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1111fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
111239a53e0cSJaegeuk Kim 
1113178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1114178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1115178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1116178053e2SDamien Le Moal #endif
1117178053e2SDamien Le Moal 
111839a53e0cSJaegeuk Kim 	/* for node-related operations */
111939a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
112039a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
112139a53e0cSJaegeuk Kim 
112239a53e0cSJaegeuk Kim 	/* for segment-related operations */
112339a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11241ff7bd3bSJaegeuk Kim 
11251ff7bd3bSJaegeuk Kim 	/* for bio operations */
1126a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1127e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1128e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
1129107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1130107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11310a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
113239a53e0cSJaegeuk Kim 
113339a53e0cSJaegeuk Kim 	/* for checkpoint */
113439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11358508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1136aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
113739a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
113839936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1139b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1140b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
114159c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1142fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11436beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11446beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
114539a53e0cSJaegeuk Kim 
114667298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11476451e041SJaegeuk Kim 
11486451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
11490d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
115039a53e0cSJaegeuk Kim 
1151c227f912SChao Yu 	/* for inode management */
1152c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1153c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
115439a53e0cSJaegeuk Kim 
115513054c54SChao Yu 	/* for extent tree cache */
115613054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11575e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
115813054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
115913054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11607441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1161137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
116274fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
116313054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
116413054c54SChao Yu 
116539a53e0cSJaegeuk Kim 	/* basic filesystem units */
116639a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
116739a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
116839a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
116939a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
117039a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
117139a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
117239a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
117339a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
117439a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
117539a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
117639a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
117739a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
117839a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1179e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1180ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1181a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1182f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
118339a53e0cSJaegeuk Kim 
118439a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
118539a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1186a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
118739a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1188daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
118980d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1190daeb433eSChao Yu 
1191292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1192292c196aSChao Yu 
119339a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1194523be8a6SJaegeuk Kim 
1195523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
119635782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
119741382ec4SJaegeuk Kim 	/* # of allocated blocks */
119841382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
119939a53e0cSJaegeuk Kim 
1200687de7f1SJaegeuk Kim 	/* writeback control */
1201687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1202687de7f1SJaegeuk Kim 
1203513c5f37SJaegeuk Kim 	/* valid inode count */
1204513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1205513c5f37SJaegeuk Kim 
120639a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
120739a53e0cSJaegeuk Kim 
120839a53e0cSJaegeuk Kim 	/* for cleaning operations */
120939a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
121039a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12115ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12125b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1213*2ef79ecbSChao Yu 	/* for skip statistic */
1214*2ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
121539a53e0cSJaegeuk Kim 
12161ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12171ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12181ad71a27SJaegeuk Kim 
1219b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1220b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1221b1c57c1cSJaegeuk Kim 
122239a53e0cSJaegeuk Kim 	/*
122339a53e0cSJaegeuk Kim 	 * for stat information.
122439a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
122539a53e0cSJaegeuk Kim 	 */
122635b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
122739a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
122839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
122939a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1230b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12315b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12325b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12335b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12345b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1235d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
123603e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
123703e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
123826a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1239648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
124026a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1241648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
124239a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
124333fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
124435b09d82SNamjae Jeon #endif
124539a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1246b59d0baeSNamjae Jeon 
1247b0af6d49SChao Yu 	/* For app/fs IO statistics */
1248b0af6d49SChao Yu 	spinlock_t iostat_lock;
1249b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1250b0af6d49SChao Yu 	bool iostat_enable;
1251b0af6d49SChao Yu 
1252b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1253b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1254b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12552658e50dSJaegeuk Kim 
12562658e50dSJaegeuk Kim 	/* For shrinker support */
12572658e50dSJaegeuk Kim 	struct list_head s_list;
12583c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12593c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
12601228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
12611228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
12622658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12632658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12648f1dbbbbSShuoran Liu 
12658f1dbbbbSShuoran Liu 	/* For write statistics */
12668f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12678f1dbbbbSShuoran Liu 	u64 kbytes_written;
126843b6573bSKeith Mok 
126943b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
127043b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12711ecc0c5cSChao Yu 
1272704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1273704956ecSChao Yu 	__u32 s_chksum_seed;
127439a53e0cSJaegeuk Kim };
127539a53e0cSJaegeuk Kim 
12761ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
127755523519SChao Yu #define f2fs_show_injection_info(type)				\
127855523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
127955523519SChao Yu 		KERN_INFO, fault_name[type],			\
128055523519SChao Yu 		__func__, __builtin_return_address(0))
12811ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12821ecc0c5cSChao Yu {
128363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
12841ecc0c5cSChao Yu 
12851ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12861ecc0c5cSChao Yu 		return false;
12871ecc0c5cSChao Yu 
12881ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12891ecc0c5cSChao Yu 		return false;
12901ecc0c5cSChao Yu 
12911ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12921ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12931ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12941ecc0c5cSChao Yu 		return true;
12951ecc0c5cSChao Yu 	}
12961ecc0c5cSChao Yu 	return false;
12971ecc0c5cSChao Yu }
12981ecc0c5cSChao Yu #endif
12991ecc0c5cSChao Yu 
13008f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13018f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13028f1dbbbbSShuoran Liu  */
13038f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
130468afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
130568afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13068f1dbbbbSShuoran Liu 
13076beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13086beceb54SJaegeuk Kim {
13096beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
13106beceb54SJaegeuk Kim }
13116beceb54SJaegeuk Kim 
13126beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13136beceb54SJaegeuk Kim {
13146bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13156beceb54SJaegeuk Kim 
13166beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13176beceb54SJaegeuk Kim }
13186beceb54SJaegeuk Kim 
1319d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1320d0239e1bSJaegeuk Kim {
1321d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1322d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1323d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1324d0239e1bSJaegeuk Kim 
1325d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1326d0239e1bSJaegeuk Kim 		return 0;
1327d0239e1bSJaegeuk Kim 
1328d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1329d0239e1bSJaegeuk Kim }
1330d0239e1bSJaegeuk Kim 
133139a53e0cSJaegeuk Kim /*
133239a53e0cSJaegeuk Kim  * Inline functions
133339a53e0cSJaegeuk Kim  */
1334416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1335704956ecSChao Yu 			      const void *address, unsigned int length)
1336704956ecSChao Yu {
1337704956ecSChao Yu 	struct {
1338704956ecSChao Yu 		struct shash_desc shash;
1339704956ecSChao Yu 		char ctx[4];
1340704956ecSChao Yu 	} desc;
1341704956ecSChao Yu 	int err;
1342704956ecSChao Yu 
1343704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1344704956ecSChao Yu 
1345704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1346704956ecSChao Yu 	desc.shash.flags = 0;
1347704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1348704956ecSChao Yu 
1349704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1350704956ecSChao Yu 	BUG_ON(err);
1351704956ecSChao Yu 
1352704956ecSChao Yu 	return *(u32 *)desc.ctx;
1353704956ecSChao Yu }
1354704956ecSChao Yu 
1355416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1356416d2dbbSChao Yu 			   unsigned int length)
1357416d2dbbSChao Yu {
1358416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1359416d2dbbSChao Yu }
1360416d2dbbSChao Yu 
1361416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1362416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1363416d2dbbSChao Yu {
1364416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1365416d2dbbSChao Yu }
1366416d2dbbSChao Yu 
1367416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1368416d2dbbSChao Yu 			      const void *address, unsigned int length)
1369416d2dbbSChao Yu {
1370416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1371416d2dbbSChao Yu }
1372416d2dbbSChao Yu 
137339a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
137439a53e0cSJaegeuk Kim {
137539a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
137639a53e0cSJaegeuk Kim }
137739a53e0cSJaegeuk Kim 
137839a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
137939a53e0cSJaegeuk Kim {
138039a53e0cSJaegeuk Kim 	return sb->s_fs_info;
138139a53e0cSJaegeuk Kim }
138239a53e0cSJaegeuk Kim 
13834081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13844081363fSJaegeuk Kim {
13854081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13864081363fSJaegeuk Kim }
13874081363fSJaegeuk Kim 
13884081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13894081363fSJaegeuk Kim {
13904081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13914081363fSJaegeuk Kim }
13924081363fSJaegeuk Kim 
13934081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13944081363fSJaegeuk Kim {
13954081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
13964081363fSJaegeuk Kim }
13974081363fSJaegeuk Kim 
139839a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
139939a53e0cSJaegeuk Kim {
140039a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
140139a53e0cSJaegeuk Kim }
140239a53e0cSJaegeuk Kim 
140339a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
140439a53e0cSJaegeuk Kim {
140539a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
140639a53e0cSJaegeuk Kim }
140739a53e0cSJaegeuk Kim 
140845590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
140945590710SGu Zheng {
141045590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
141145590710SGu Zheng }
141245590710SGu Zheng 
141358bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
141458bfaf44SJaegeuk Kim {
141558bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
141658bfaf44SJaegeuk Kim }
141758bfaf44SJaegeuk Kim 
141839a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
141939a53e0cSJaegeuk Kim {
142039a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
142139a53e0cSJaegeuk Kim }
142239a53e0cSJaegeuk Kim 
142339a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
142439a53e0cSJaegeuk Kim {
142539a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
142639a53e0cSJaegeuk Kim }
142739a53e0cSJaegeuk Kim 
142839a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
142939a53e0cSJaegeuk Kim {
143039a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
143139a53e0cSJaegeuk Kim }
143239a53e0cSJaegeuk Kim 
143339a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
143439a53e0cSJaegeuk Kim {
143539a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
143639a53e0cSJaegeuk Kim }
143739a53e0cSJaegeuk Kim 
143839a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
143939a53e0cSJaegeuk Kim {
144039a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
144139a53e0cSJaegeuk Kim }
144239a53e0cSJaegeuk Kim 
14439df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
14449df27d98SGu Zheng {
14459df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
14469df27d98SGu Zheng }
14479df27d98SGu Zheng 
14484ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
14494ef51a8fSJaegeuk Kim {
14504ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
14514ef51a8fSJaegeuk Kim }
14524ef51a8fSJaegeuk Kim 
1453caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
145439a53e0cSJaegeuk Kim {
1455fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
145639a53e0cSJaegeuk Kim }
145739a53e0cSJaegeuk Kim 
1458caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
145939a53e0cSJaegeuk Kim {
1460fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1461caf0047eSChao Yu }
1462caf0047eSChao Yu 
1463caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1464caf0047eSChao Yu {
1465fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
146639a53e0cSJaegeuk Kim }
146739a53e0cSJaegeuk Kim 
1468d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1469d71b5564SJaegeuk Kim {
1470d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1471d71b5564SJaegeuk Kim }
1472d71b5564SJaegeuk Kim 
1473ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1474ea676733SJaegeuk Kim {
1475ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1476ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1477ea676733SJaegeuk Kim 	return 0;
1478ea676733SJaegeuk Kim }
1479ea676733SJaegeuk Kim 
1480ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1481ced2c7eaSKinglong Mee {
1482ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1483ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1484ced2c7eaSKinglong Mee }
1485ced2c7eaSKinglong Mee 
1486aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
148725ca923bSJaegeuk Kim {
148825ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1489aaec2b1dSChao Yu 
149025ca923bSJaegeuk Kim 	return ckpt_flags & f;
149125ca923bSJaegeuk Kim }
149225ca923bSJaegeuk Kim 
1493aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
149425ca923bSJaegeuk Kim {
1495aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1496aaec2b1dSChao Yu }
1497aaec2b1dSChao Yu 
1498aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1499aaec2b1dSChao Yu {
1500aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1501aaec2b1dSChao Yu 
1502aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
150325ca923bSJaegeuk Kim 	ckpt_flags |= f;
150425ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
150525ca923bSJaegeuk Kim }
150625ca923bSJaegeuk Kim 
1507aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
150825ca923bSJaegeuk Kim {
1509d1aa2453SChao Yu 	unsigned long flags;
1510d1aa2453SChao Yu 
1511d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1512aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1513d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1514aaec2b1dSChao Yu }
1515aaec2b1dSChao Yu 
1516aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1517aaec2b1dSChao Yu {
1518aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1519aaec2b1dSChao Yu 
1520aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
152125ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
152225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
152325ca923bSJaegeuk Kim }
152425ca923bSJaegeuk Kim 
1525aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1526aaec2b1dSChao Yu {
1527d1aa2453SChao Yu 	unsigned long flags;
1528d1aa2453SChao Yu 
1529d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1530aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1531d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1532aaec2b1dSChao Yu }
1533aaec2b1dSChao Yu 
153422ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
153522ad0b6aSJaegeuk Kim {
1536d1aa2453SChao Yu 	unsigned long flags;
1537d1aa2453SChao Yu 
153822ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
153922ad0b6aSJaegeuk Kim 
154022ad0b6aSJaegeuk Kim 	if (lock)
1541d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
154222ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
154322ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
154422ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
154522ad0b6aSJaegeuk Kim 	if (lock)
1546d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
154722ad0b6aSJaegeuk Kim }
154822ad0b6aSJaegeuk Kim 
154922ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
155022ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
155122ad0b6aSJaegeuk Kim {
155222ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
155322ad0b6aSJaegeuk Kim 
1554c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
155522ad0b6aSJaegeuk Kim }
155622ad0b6aSJaegeuk Kim 
1557e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
155839a53e0cSJaegeuk Kim {
1559b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
156039a53e0cSJaegeuk Kim }
156139a53e0cSJaegeuk Kim 
1562cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1563cc15620bSJaegeuk Kim {
1564cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1565cc15620bSJaegeuk Kim }
1566cc15620bSJaegeuk Kim 
1567e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
156839a53e0cSJaegeuk Kim {
1569b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
157039936837SJaegeuk Kim }
157139936837SJaegeuk Kim 
1572e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
157339936837SJaegeuk Kim {
1574b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
157539936837SJaegeuk Kim }
157639936837SJaegeuk Kim 
1577e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
157839936837SJaegeuk Kim {
1579b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
158039a53e0cSJaegeuk Kim }
158139a53e0cSJaegeuk Kim 
1582119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1583119ee914SJaegeuk Kim {
1584119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1585119ee914SJaegeuk Kim 
1586119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1587119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1588119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1589119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1590119ee914SJaegeuk Kim 	return reason;
1591119ee914SJaegeuk Kim }
1592119ee914SJaegeuk Kim 
1593119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1594119ee914SJaegeuk Kim {
1595c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1596119ee914SJaegeuk Kim }
1597119ee914SJaegeuk Kim 
1598119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1599119ee914SJaegeuk Kim {
1600aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1601aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1602119ee914SJaegeuk Kim }
1603119ee914SJaegeuk Kim 
160439a53e0cSJaegeuk Kim /*
160539a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
160639a53e0cSJaegeuk Kim  */
160739a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
160839a53e0cSJaegeuk Kim {
16090eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16100eb0adadSChao Yu 
1611000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
161239a53e0cSJaegeuk Kim }
161339a53e0cSJaegeuk Kim 
16144bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16154bc8e9bcSChao Yu {
16164bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16174bc8e9bcSChao Yu }
16184bc8e9bcSChao Yu 
1619d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1620a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
16217c2e5963SJaegeuk Kim {
1622d8a9a229SJaegeuk Kim 	if (!inode)
1623d8a9a229SJaegeuk Kim 		return true;
16247c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16257c2e5963SJaegeuk Kim 		return false;
1626d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1627d8a9a229SJaegeuk Kim 		return true;
162863189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
16297c2e5963SJaegeuk Kim 		return true;
163063189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
163163189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
16327c2e5963SJaegeuk Kim 		return true;
1633a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1634162b27aeSJaegeuk Kim 		return true;
16357c2e5963SJaegeuk Kim 	return false;
16367c2e5963SJaegeuk Kim }
16377c2e5963SJaegeuk Kim 
16380abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
16390abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
164046008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
164139a53e0cSJaegeuk Kim {
16420abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1643daeb433eSChao Yu 	block_t avail_user_block_count;
16440abd675eSChao Yu 	int ret;
16450abd675eSChao Yu 
16460abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16470abd675eSChao Yu 	if (ret)
16480abd675eSChao Yu 		return ret;
1649dd11a5dfSJaegeuk Kim 
1650cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
165155523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
165255523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16530abd675eSChao Yu 		release = *count;
16540abd675eSChao Yu 		goto enospc;
165555523519SChao Yu 	}
1656cb78942bSJaegeuk Kim #endif
1657dd11a5dfSJaegeuk Kim 	/*
1658dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1659dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1660dd11a5dfSJaegeuk Kim 	 */
1661dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1662cfb271d4SChao Yu 
166339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16642555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
166580d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
166680d42145SYunlong Song 					sbi->current_reserved_blocks;
16677e65be49SJaegeuk Kim 
1668a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
166963189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
16707e65be49SJaegeuk Kim 
1671daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1672daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16737e65be49SJaegeuk Kim 		if (diff > *count)
16747e65be49SJaegeuk Kim 			diff = *count;
1675dd11a5dfSJaegeuk Kim 		*count -= diff;
16760abd675eSChao Yu 		release = diff;
16777e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
167846008c6dSChao Yu 		if (!*count) {
167939a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1680dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16810abd675eSChao Yu 			goto enospc;
168239a53e0cSJaegeuk Kim 		}
168346008c6dSChao Yu 	}
168439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
168541382ec4SJaegeuk Kim 
1686fab2adeeSLiFan 	if (unlikely(release))
16870abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16880abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16890abd675eSChao Yu 	return 0;
16900abd675eSChao Yu 
16910abd675eSChao Yu enospc:
16920abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16930abd675eSChao Yu 	return -ENOSPC;
169439a53e0cSJaegeuk Kim }
169539a53e0cSJaegeuk Kim 
1696da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
169739a53e0cSJaegeuk Kim 						struct inode *inode,
16980eb0adadSChao Yu 						block_t count)
169939a53e0cSJaegeuk Kim {
17000eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17010eb0adadSChao Yu 
170239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17039850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17040eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
170539a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
170680d42145SYunlong Song 	if (sbi->reserved_blocks &&
170780d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
170880d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
170980d42145SYunlong Song 					sbi->current_reserved_blocks + count);
171039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17110abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
171239a53e0cSJaegeuk Kim }
171339a53e0cSJaegeuk Kim 
171439a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
171539a53e0cSJaegeuk Kim {
171635782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17177c4abcbeSChao Yu 
171836951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
171936951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17207c4abcbeSChao Yu 		return;
17217c4abcbeSChao Yu 
1722caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
172339a53e0cSJaegeuk Kim }
172439a53e0cSJaegeuk Kim 
1725a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
172639a53e0cSJaegeuk Kim {
1727204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1728c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1729c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17302c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17312c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
173239a53e0cSJaegeuk Kim }
173339a53e0cSJaegeuk Kim 
173439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
173539a53e0cSJaegeuk Kim {
173635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
173739a53e0cSJaegeuk Kim }
173839a53e0cSJaegeuk Kim 
1739a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
174039a53e0cSJaegeuk Kim {
17415ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17425ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17431fe54f9dSJaegeuk Kim 		return;
17441fe54f9dSJaegeuk Kim 
1745204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1746c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1747c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17482c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17492c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
175039a53e0cSJaegeuk Kim }
175139a53e0cSJaegeuk Kim 
1752523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
175339a53e0cSJaegeuk Kim {
175435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
175539a53e0cSJaegeuk Kim }
175639a53e0cSJaegeuk Kim 
1757204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1758f8b2c1f9SJaegeuk Kim {
1759204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1760f8b2c1f9SJaegeuk Kim }
1761f8b2c1f9SJaegeuk Kim 
17625ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17635ac206cfSNamjae Jeon {
17643519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1765523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1766523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1767523be8a6SJaegeuk Kim 
1768523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17695ac206cfSNamjae Jeon }
17705ac206cfSNamjae Jeon 
177139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
177239a53e0cSJaegeuk Kim {
17738b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
177439a53e0cSJaegeuk Kim }
177539a53e0cSJaegeuk Kim 
1776f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1777f83a2584SYunlei He {
1778f83a2584SYunlei He 	return sbi->discard_blks;
1779f83a2584SYunlei He }
1780f83a2584SYunlei He 
178139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
178239a53e0cSJaegeuk Kim {
178339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
178439a53e0cSJaegeuk Kim 
178539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
178639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
178739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
178839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
178939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
179039a53e0cSJaegeuk Kim 
179139a53e0cSJaegeuk Kim 	return 0;
179239a53e0cSJaegeuk Kim }
179339a53e0cSJaegeuk Kim 
179455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
179555141486SWanpeng Li {
179655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
179755141486SWanpeng Li }
179855141486SWanpeng Li 
179939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
180039a53e0cSJaegeuk Kim {
180139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18021dbe4152SChangman Lee 	int offset;
18031dbe4152SChangman Lee 
1804199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1805199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1806199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1807199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1808199bc3feSChao Yu 	}
1809199bc3feSChao Yu 
181055141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18111dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18121dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18131dbe4152SChangman Lee 		else
181465b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18151dbe4152SChangman Lee 	} else {
18161dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
181725ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
181839a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
181939a53e0cSJaegeuk Kim 	}
18201dbe4152SChangman Lee }
182139a53e0cSJaegeuk Kim 
182239a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
182339a53e0cSJaegeuk Kim {
18248508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
182539a53e0cSJaegeuk Kim 
18268508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
182739a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
182839a53e0cSJaegeuk Kim 	return start_addr;
182939a53e0cSJaegeuk Kim }
183039a53e0cSJaegeuk Kim 
18318508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
18328508e44aSJaegeuk Kim {
18338508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
18348508e44aSJaegeuk Kim 
18358508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
18368508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
18378508e44aSJaegeuk Kim 	return start_addr;
18388508e44aSJaegeuk Kim }
18398508e44aSJaegeuk Kim 
18408508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
18418508e44aSJaegeuk Kim {
18428508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
18438508e44aSJaegeuk Kim }
18448508e44aSJaegeuk Kim 
184539a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
184639a53e0cSJaegeuk Kim {
184739a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
184839a53e0cSJaegeuk Kim }
184939a53e0cSJaegeuk Kim 
18500abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1851000519f2SChao Yu 					struct inode *inode, bool is_inode)
185239a53e0cSJaegeuk Kim {
185339a53e0cSJaegeuk Kim 	block_t	valid_block_count;
185439a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18550abd675eSChao Yu 	bool quota = inode && !is_inode;
18560abd675eSChao Yu 
18570abd675eSChao Yu 	if (quota) {
18580abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18590abd675eSChao Yu 		if (ret)
18600abd675eSChao Yu 			return ret;
18610abd675eSChao Yu 	}
186239a53e0cSJaegeuk Kim 
1863812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1864812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1865812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1866812c6056SChao Yu 		goto enospc;
1867812c6056SChao Yu 	}
1868812c6056SChao Yu #endif
1869812c6056SChao Yu 
187039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
187139a53e0cSJaegeuk Kim 
18727e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18737e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18747e65be49SJaegeuk Kim 
1875a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
187663189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
18777e65be49SJaegeuk Kim 
18787e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
187939a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18800abd675eSChao Yu 		goto enospc;
188139a53e0cSJaegeuk Kim 	}
188239a53e0cSJaegeuk Kim 
1883ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1884cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
188539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18860abd675eSChao Yu 		goto enospc;
188739a53e0cSJaegeuk Kim 	}
188839a53e0cSJaegeuk Kim 
1889ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1890ef86d709SGu Zheng 	sbi->total_valid_block_count++;
189139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
189239a53e0cSJaegeuk Kim 
18930abd675eSChao Yu 	if (inode) {
18940abd675eSChao Yu 		if (is_inode)
18950abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
18960abd675eSChao Yu 		else
18970abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
18980abd675eSChao Yu 	}
18990abd675eSChao Yu 
190041382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19010abd675eSChao Yu 	return 0;
19020abd675eSChao Yu 
19030abd675eSChao Yu enospc:
19040abd675eSChao Yu 	if (quota)
19050abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
19060abd675eSChao Yu 	return -ENOSPC;
190739a53e0cSJaegeuk Kim }
190839a53e0cSJaegeuk Kim 
190939a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1910000519f2SChao Yu 					struct inode *inode, bool is_inode)
191139a53e0cSJaegeuk Kim {
191239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
191339a53e0cSJaegeuk Kim 
19149850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
19159850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1916000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
191739a53e0cSJaegeuk Kim 
1918ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1919ef86d709SGu Zheng 	sbi->total_valid_block_count--;
192080d42145SYunlong Song 	if (sbi->reserved_blocks &&
192180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
192280d42145SYunlong Song 		sbi->current_reserved_blocks++;
192339a53e0cSJaegeuk Kim 
192439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19250abd675eSChao Yu 
19260abd675eSChao Yu 	if (!is_inode)
19270abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
192839a53e0cSJaegeuk Kim }
192939a53e0cSJaegeuk Kim 
193039a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
193139a53e0cSJaegeuk Kim {
19328b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
193339a53e0cSJaegeuk Kim }
193439a53e0cSJaegeuk Kim 
193539a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
193639a53e0cSJaegeuk Kim {
1937513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
193839a53e0cSJaegeuk Kim }
193939a53e0cSJaegeuk Kim 
19400e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
194139a53e0cSJaegeuk Kim {
1942513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
194339a53e0cSJaegeuk Kim }
194439a53e0cSJaegeuk Kim 
1945513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
194639a53e0cSJaegeuk Kim {
1947513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
194839a53e0cSJaegeuk Kim }
194939a53e0cSJaegeuk Kim 
1950a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1951a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1952a56c7c6fSJaegeuk Kim {
1953c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1954c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1955cac5a3d8SDongOh Shin 
1956c41f3cc3SJaegeuk Kim 	if (page)
1957c41f3cc3SJaegeuk Kim 		return page;
1958c41f3cc3SJaegeuk Kim 
195955523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
196055523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1961c41f3cc3SJaegeuk Kim 		return NULL;
196255523519SChao Yu 	}
1963c41f3cc3SJaegeuk Kim #endif
1964a56c7c6fSJaegeuk Kim 	if (!for_write)
1965a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1966a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1967a56c7c6fSJaegeuk Kim }
1968a56c7c6fSJaegeuk Kim 
196901eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
197001eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
197101eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
197201eccef7SChao Yu {
197301eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
197401eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
197501eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
197601eccef7SChao Yu 		return NULL;
197701eccef7SChao Yu 	}
197801eccef7SChao Yu #endif
197901eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
198001eccef7SChao Yu }
198101eccef7SChao Yu 
19826e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19836e2c64adSJaegeuk Kim {
19846e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19856e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19866e2c64adSJaegeuk Kim 
19876e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19886e2c64adSJaegeuk Kim 	kunmap(dst);
19896e2c64adSJaegeuk Kim 	kunmap(src);
19906e2c64adSJaegeuk Kim }
19916e2c64adSJaegeuk Kim 
199239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
199339a53e0cSJaegeuk Kim {
1994031fa8ccSJaegeuk Kim 	if (!page)
199539a53e0cSJaegeuk Kim 		return;
199639a53e0cSJaegeuk Kim 
199739a53e0cSJaegeuk Kim 	if (unlock) {
19989850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
199939a53e0cSJaegeuk Kim 		unlock_page(page);
200039a53e0cSJaegeuk Kim 	}
200109cbfeafSKirill A. Shutemov 	put_page(page);
200239a53e0cSJaegeuk Kim }
200339a53e0cSJaegeuk Kim 
200439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
200539a53e0cSJaegeuk Kim {
200639a53e0cSJaegeuk Kim 	if (dn->node_page)
200739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
200839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
200939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
201039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
201139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
201239a53e0cSJaegeuk Kim }
201339a53e0cSJaegeuk Kim 
201439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2015e8512d2eSGu Zheng 					size_t size)
201639a53e0cSJaegeuk Kim {
2017e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
201839a53e0cSJaegeuk Kim }
201939a53e0cSJaegeuk Kim 
20207bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20217bd59381SGu Zheng 						gfp_t flags)
20227bd59381SGu Zheng {
20237bd59381SGu Zheng 	void *entry;
20247bd59381SGu Zheng 
202580c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
202680c54505SJaegeuk Kim 	if (!entry)
202780c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
20287bd59381SGu Zheng 	return entry;
20297bd59381SGu Zheng }
20307bd59381SGu Zheng 
2031d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2032d62fe971SChao Yu 						int npages, bool no_fail)
2033740432f8SJaegeuk Kim {
2034740432f8SJaegeuk Kim 	struct bio *bio;
2035740432f8SJaegeuk Kim 
2036d62fe971SChao Yu 	if (no_fail) {
2037740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2038740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
203980c54505SJaegeuk Kim 		if (!bio)
204080c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2041740432f8SJaegeuk Kim 		return bio;
2042740432f8SJaegeuk Kim 	}
2043d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2044d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2045d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2046d62fe971SChao Yu 		return NULL;
2047d62fe971SChao Yu 	}
2048d62fe971SChao Yu #endif
2049d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2050d62fe971SChao Yu }
2051740432f8SJaegeuk Kim 
20529be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20539be32d72SJaegeuk Kim 				unsigned long index, void *item)
20549be32d72SJaegeuk Kim {
20559be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20569be32d72SJaegeuk Kim 		cond_resched();
20579be32d72SJaegeuk Kim }
20589be32d72SJaegeuk Kim 
205939a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
206039a53e0cSJaegeuk Kim 
206139a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
206239a53e0cSJaegeuk Kim {
206345590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2064cac5a3d8SDongOh Shin 
206539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
206639a53e0cSJaegeuk Kim }
206739a53e0cSJaegeuk Kim 
20687a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20697a2af766SChao Yu {
20707a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20717a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20727a2af766SChao Yu }
20737a2af766SChao Yu 
207439a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
207539a53e0cSJaegeuk Kim {
207639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
207739a53e0cSJaegeuk Kim }
207839a53e0cSJaegeuk Kim 
20797a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20807a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20817a2af766SChao Yu 			struct page *node_page, unsigned int offset)
208239a53e0cSJaegeuk Kim {
208339a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
208439a53e0cSJaegeuk Kim 	__le32 *addr_array;
20857a2af766SChao Yu 	int base = 0;
20867a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2087cac5a3d8SDongOh Shin 
208845590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20897a2af766SChao Yu 
20907a2af766SChao Yu 	/* from GC path only */
2091979f492fSLiFan 	if (is_inode) {
2092979f492fSLiFan 		if (!inode)
20937a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2094979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20957a2af766SChao Yu 			base = get_extra_isize(inode);
20967a2af766SChao Yu 	}
20977a2af766SChao Yu 
209839a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
20997a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
210039a53e0cSJaegeuk Kim }
210139a53e0cSJaegeuk Kim 
210239a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
210339a53e0cSJaegeuk Kim {
210439a53e0cSJaegeuk Kim 	int mask;
210539a53e0cSJaegeuk Kim 
210639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
210739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
210839a53e0cSJaegeuk Kim 	return mask & *addr;
210939a53e0cSJaegeuk Kim }
211039a53e0cSJaegeuk Kim 
2111a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2112a66cdd98SJaegeuk Kim {
2113a66cdd98SJaegeuk Kim 	int mask;
2114a66cdd98SJaegeuk Kim 
2115a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2116a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2117a66cdd98SJaegeuk Kim 	*addr |= mask;
2118a66cdd98SJaegeuk Kim }
2119a66cdd98SJaegeuk Kim 
2120a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2121a66cdd98SJaegeuk Kim {
2122a66cdd98SJaegeuk Kim 	int mask;
2123a66cdd98SJaegeuk Kim 
2124a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2125a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2126a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2127a66cdd98SJaegeuk Kim }
2128a66cdd98SJaegeuk Kim 
212952aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
213039a53e0cSJaegeuk Kim {
213139a53e0cSJaegeuk Kim 	int mask;
213239a53e0cSJaegeuk Kim 	int ret;
213339a53e0cSJaegeuk Kim 
213439a53e0cSJaegeuk Kim 	addr += (nr >> 3);
213539a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
213639a53e0cSJaegeuk Kim 	ret = mask & *addr;
213739a53e0cSJaegeuk Kim 	*addr |= mask;
213839a53e0cSJaegeuk Kim 	return ret;
213939a53e0cSJaegeuk Kim }
214039a53e0cSJaegeuk Kim 
214152aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
214239a53e0cSJaegeuk Kim {
214339a53e0cSJaegeuk Kim 	int mask;
214439a53e0cSJaegeuk Kim 	int ret;
214539a53e0cSJaegeuk Kim 
214639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
214739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
214839a53e0cSJaegeuk Kim 	ret = mask & *addr;
214939a53e0cSJaegeuk Kim 	*addr &= ~mask;
215039a53e0cSJaegeuk Kim 	return ret;
215139a53e0cSJaegeuk Kim }
215239a53e0cSJaegeuk Kim 
2153c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2154c6ac4c0eSGu Zheng {
2155c6ac4c0eSGu Zheng 	int mask;
2156c6ac4c0eSGu Zheng 
2157c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2158c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2159c6ac4c0eSGu Zheng 	*addr ^= mask;
2160c6ac4c0eSGu Zheng }
2161c6ac4c0eSGu Zheng 
216259c84408SChao Yu /*
216359c84408SChao Yu  * Inode flags
216459c84408SChao Yu  */
216559c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
216659c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
216759c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
216859c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
216959c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
217059c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
217159c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
217259c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
217359c84408SChao Yu /* Reserved for compression usage... */
217459c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
217559c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
217659c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
217759c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
217859c84408SChao Yu /* End compression flags --- maybe not all used */
217959c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
218059c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
218159c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
218259c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
218359c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
218459c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
218559c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
218659c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
218759c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
218859c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
218959c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
219059c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
219159c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
219259c84408SChao Yu 
219359c84408SChao Yu #define F2FS_FL_USER_VISIBLE		0x304BDFFF /* User visible flags */
219459c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
219559c84408SChao Yu 
219659c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
219759c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
219859c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
219959c84408SChao Yu 					 F2FS_APPEND_FL | \
220059c84408SChao Yu 					 F2FS_NODUMP_FL | \
220159c84408SChao Yu 					 F2FS_NOATIME_FL | \
220259c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
220359c84408SChao Yu 
220459c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
220559c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
220659c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
220759c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
220859c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
220959c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
221059c84408SChao Yu 
221159c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
221259c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
221359c84408SChao Yu 
221459c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
221559c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
22165c57132eSChao Yu 
22175c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
22185c57132eSChao Yu {
22195c57132eSChao Yu 	if (S_ISDIR(mode))
22205c57132eSChao Yu 		return flags;
22215c57132eSChao Yu 	else if (S_ISREG(mode))
22225c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
22235c57132eSChao Yu 	else
22245c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
22255c57132eSChao Yu }
22265c57132eSChao Yu 
222739a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
222839a53e0cSJaegeuk Kim enum {
222939a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2230b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
223126de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2232ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
223339a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
223439a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
223539a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2236c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2237c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2238444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
22391001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
224034d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2241fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2242fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
224388b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
224488b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
22455fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
224602a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
22473c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
22481e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2249b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2250510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2251d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2252c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2253dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2254ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
22557a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
22565c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
22571ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
2258*2ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
225939a53e0cSJaegeuk Kim };
226039a53e0cSJaegeuk Kim 
2261205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2262205b9822SJaegeuk Kim 						int flag, bool set)
226339a53e0cSJaegeuk Kim {
2264205b9822SJaegeuk Kim 	switch (flag) {
2265205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2266205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2267205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
22689ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2269205b9822SJaegeuk Kim 		if (set)
2270205b9822SJaegeuk Kim 			return;
2271205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2272205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
22731ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
22747c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2275205b9822SJaegeuk Kim 	}
227639a53e0cSJaegeuk Kim }
227739a53e0cSJaegeuk Kim 
227891942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
227939a53e0cSJaegeuk Kim {
228091942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
228191942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2282205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
228339a53e0cSJaegeuk Kim }
228439a53e0cSJaegeuk Kim 
228591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
228639a53e0cSJaegeuk Kim {
228791942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
228839a53e0cSJaegeuk Kim }
228939a53e0cSJaegeuk Kim 
229091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
229139a53e0cSJaegeuk Kim {
229291942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
229391942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2294205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
229539a53e0cSJaegeuk Kim }
229639a53e0cSJaegeuk Kim 
229791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2298444c580fSJaegeuk Kim {
229991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
230091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
23017c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
230239a53e0cSJaegeuk Kim }
230339a53e0cSJaegeuk Kim 
2304a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2305a1961246SJaegeuk Kim {
2306a1961246SJaegeuk Kim 	if (inc)
2307a1961246SJaegeuk Kim 		inc_nlink(inode);
2308a1961246SJaegeuk Kim 	else
2309a1961246SJaegeuk Kim 		drop_nlink(inode);
23107c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2311a1961246SJaegeuk Kim }
2312a1961246SJaegeuk Kim 
23138edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
23140abd675eSChao Yu 					block_t diff, bool add, bool claim)
23158edd03c8SJaegeuk Kim {
231626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
231726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
231826de9b11SJaegeuk Kim 
23190abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
23200abd675eSChao Yu 	if (add) {
23210abd675eSChao Yu 		if (claim)
23220abd675eSChao Yu 			dquot_claim_block(inode, diff);
23230abd675eSChao Yu 		else
23240abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
23250abd675eSChao Yu 	} else {
23260abd675eSChao Yu 		dquot_free_block(inode, diff);
23270abd675eSChao Yu 	}
23280abd675eSChao Yu 
23297c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
233026de9b11SJaegeuk Kim 	if (clean || recover)
233126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
23328edd03c8SJaegeuk Kim }
23338edd03c8SJaegeuk Kim 
2334fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2335fc9581c8SJaegeuk Kim {
233626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
233726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
233826de9b11SJaegeuk Kim 
2339fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2340fc9581c8SJaegeuk Kim 		return;
2341fc9581c8SJaegeuk Kim 
2342fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
23437c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
234426de9b11SJaegeuk Kim 	if (clean || recover)
234526de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
234626de9b11SJaegeuk Kim }
234726de9b11SJaegeuk Kim 
2348205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2349205b9822SJaegeuk Kim {
2350205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
23517c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2352205b9822SJaegeuk Kim }
2353205b9822SJaegeuk Kim 
23541ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
23551ad71a27SJaegeuk Kim 					unsigned int count)
23561ad71a27SJaegeuk Kim {
2357*2ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
23581ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
23591ad71a27SJaegeuk Kim }
23601ad71a27SJaegeuk Kim 
2361205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2362205b9822SJaegeuk Kim {
2363205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
23647c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2365205b9822SJaegeuk Kim }
2366205b9822SJaegeuk Kim 
2367205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2368205b9822SJaegeuk Kim {
2369205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
23707c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2371205b9822SJaegeuk Kim }
2372205b9822SJaegeuk Kim 
237391942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2374444c580fSJaegeuk Kim {
2375205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2376205b9822SJaegeuk Kim 
2377444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2378205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
23791001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2380205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
238134d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2382205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2383b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2384205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2385510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2386205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
23877a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
23887a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
23891ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
23901ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2391444c580fSJaegeuk Kim }
2392444c580fSJaegeuk Kim 
239391942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2394444c580fSJaegeuk Kim {
2395444c580fSJaegeuk Kim 	ri->i_inline = 0;
2396444c580fSJaegeuk Kim 
239791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2398444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
239991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
24001001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
240191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
240234d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
240391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2404b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
240591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2406510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
24077a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
24087a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
24091ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
24101ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
24117a2af766SChao Yu }
24127a2af766SChao Yu 
24137a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
24147a2af766SChao Yu {
24157a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2416444c580fSJaegeuk Kim }
2417444c580fSJaegeuk Kim 
2418987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2419987c7c31SChao Yu {
242091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2421987c7c31SChao Yu }
2422987c7c31SChao Yu 
242381ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2424de93653fSJaegeuk Kim {
2425b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2426de93653fSJaegeuk Kim }
2427de93653fSJaegeuk Kim 
24286afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
242965985d93SJaegeuk Kim {
2430695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2431cac5a3d8SDongOh Shin 
243265985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2433b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
243465985d93SJaegeuk Kim }
243565985d93SJaegeuk Kim 
243665985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
243765985d93SJaegeuk Kim {
24386afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
243965985d93SJaegeuk Kim }
244065985d93SJaegeuk Kim 
24410dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
24420dbdc2aeSJaegeuk Kim {
244391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
24440dbdc2aeSJaegeuk Kim }
24450dbdc2aeSJaegeuk Kim 
2446b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2447b3d208f9SJaegeuk Kim {
244891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2449b3d208f9SJaegeuk Kim }
2450b3d208f9SJaegeuk Kim 
2451510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2452510022a8SJaegeuk Kim {
245391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2454510022a8SJaegeuk Kim }
2455510022a8SJaegeuk Kim 
24561ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
24571ad71a27SJaegeuk Kim {
24581ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
24591ad71a27SJaegeuk Kim }
24601ad71a27SJaegeuk Kim 
246188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
246288b88a66SJaegeuk Kim {
246391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
246488b88a66SJaegeuk Kim }
246588b88a66SJaegeuk Kim 
24665fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
24675fe45743SChao Yu {
24685fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
24695fe45743SChao Yu }
24705fe45743SChao Yu 
247102a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
247202a1335fSJaegeuk Kim {
247391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
247402a1335fSJaegeuk Kim }
247502a1335fSJaegeuk Kim 
24763c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
24773c6c2bebSJaegeuk Kim {
247891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
24793c6c2bebSJaegeuk Kim }
24803c6c2bebSJaegeuk Kim 
24811e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
24821e84371fSJaegeuk Kim {
248391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
24841e84371fSJaegeuk Kim }
24851e84371fSJaegeuk Kim 
2486f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
24871001b347SHuajun Li {
2488695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
24897a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2490cac5a3d8SDongOh Shin 
24917a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
24921001b347SHuajun Li }
24931001b347SHuajun Li 
249434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
249534d67debSChao Yu {
249691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
249734d67debSChao Yu }
249834d67debSChao Yu 
2499b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2500b5492af7SJaegeuk Kim {
2501b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2502b5492af7SJaegeuk Kim }
2503b5492af7SJaegeuk Kim 
2504b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2505b5492af7SJaegeuk Kim {
2506b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
25077c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2508b5492af7SJaegeuk Kim }
2509b5492af7SJaegeuk Kim 
2510b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2511b5492af7SJaegeuk Kim {
2512b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
25137c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2514b5492af7SJaegeuk Kim }
2515b5492af7SJaegeuk Kim 
251626787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
251726787236SJaegeuk Kim {
2518a0d00fadSChao Yu 	bool ret;
2519a0d00fadSChao Yu 
252026787236SJaegeuk Kim 	if (dsync) {
252126787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
252226787236SJaegeuk Kim 
252326787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
252426787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
252526787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
252626787236SJaegeuk Kim 		return ret;
252726787236SJaegeuk Kim 	}
252826787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
252926787236SJaegeuk Kim 			file_keep_isize(inode) ||
2530235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
253126787236SJaegeuk Kim 		return false;
2532a0d00fadSChao Yu 
2533214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2534214c2461SJaegeuk Kim 		return false;
2535214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2536214c2461SJaegeuk Kim 		return false;
2537214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2538214c2461SJaegeuk Kim 		return false;
2539214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2540214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2541214c2461SJaegeuk Kim 		return false;
2542214c2461SJaegeuk Kim 
2543a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2544a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2545a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2546a0d00fadSChao Yu 
2547a0d00fadSChao Yu 	return ret;
2548267378d4SChao Yu }
2549267378d4SChao Yu 
2550deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
255177888c1eSJaegeuk Kim {
2552deeedd71SChao Yu 	return sb_rdonly(sb);
255377888c1eSJaegeuk Kim }
255477888c1eSJaegeuk Kim 
2555744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2556744602cfSJaegeuk Kim {
2557aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2558744602cfSJaegeuk Kim }
2559744602cfSJaegeuk Kim 
2560eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2561eaa693f4SJaegeuk Kim {
2562eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2563eaa693f4SJaegeuk Kim 		return true;
2564eaa693f4SJaegeuk Kim 
2565eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2566eaa693f4SJaegeuk Kim 		return true;
2567eaa693f4SJaegeuk Kim 
2568eaa693f4SJaegeuk Kim 	return false;
2569eaa693f4SJaegeuk Kim }
2570eaa693f4SJaegeuk Kim 
25713e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
25723e72f721SJaegeuk Kim {
25733e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
257491942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
25753e72f721SJaegeuk Kim 		return false;
25763e72f721SJaegeuk Kim 
2577886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
25783e72f721SJaegeuk Kim }
25793e72f721SJaegeuk Kim 
25801ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
25811ecc0c5cSChao Yu 					size_t size, gfp_t flags)
25820414b004SJaegeuk Kim {
25832c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
258455523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
258555523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
25862c63feadSJaegeuk Kim 		return NULL;
258755523519SChao Yu 	}
25882c63feadSJaegeuk Kim #endif
25890414b004SJaegeuk Kim 	return kmalloc(size, flags);
25900414b004SJaegeuk Kim }
25910414b004SJaegeuk Kim 
2592acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2593acbf054dSChao Yu 					size_t size, gfp_t flags)
2594acbf054dSChao Yu {
2595acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2596acbf054dSChao Yu }
2597acbf054dSChao Yu 
2598628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2599628b3d14SChao Yu 					size_t size, gfp_t flags)
2600628b3d14SChao Yu {
2601628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2602628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2603628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2604628b3d14SChao Yu 		return NULL;
2605628b3d14SChao Yu 	}
2606628b3d14SChao Yu #endif
2607628b3d14SChao Yu 	return kvmalloc(size, flags);
2608628b3d14SChao Yu }
2609628b3d14SChao Yu 
2610628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2611628b3d14SChao Yu 					size_t size, gfp_t flags)
2612628b3d14SChao Yu {
2613628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2614628b3d14SChao Yu }
2615628b3d14SChao Yu 
26167a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2617f2470371SChao Yu {
26187a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2619f2470371SChao Yu }
2620f2470371SChao Yu 
26216afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
26226afc662eSChao Yu {
26236afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
26246afc662eSChao Yu }
26256afc662eSChao Yu 
2626a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
262791942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2628a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2629a6dda0e6SChristoph Hellwig 
26307a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
26317a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
26327a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
26337a2af766SChao Yu 
26345c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
26355c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
26365c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
26375c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
26385c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
26395c57132eSChao Yu 
2640b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2641b0af6d49SChao Yu {
2642b0af6d49SChao Yu 	int i;
2643b0af6d49SChao Yu 
2644b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2645b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2646b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2647b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2648b0af6d49SChao Yu }
2649b0af6d49SChao Yu 
2650b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2651b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2652b0af6d49SChao Yu {
2653b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2654b0af6d49SChao Yu 		return;
2655b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2656b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2657b0af6d49SChao Yu 
2658b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2659b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2660b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2661b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2662b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2663b0af6d49SChao Yu }
2664b0af6d49SChao Yu 
26657b525dd0SChao Yu static inline bool is_valid_blkaddr(block_t blkaddr)
26667b525dd0SChao Yu {
26677b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
26687b525dd0SChao Yu 		return false;
26697b525dd0SChao Yu 	return true;
26707b525dd0SChao Yu }
26717b525dd0SChao Yu 
267239a53e0cSJaegeuk Kim /*
267339a53e0cSJaegeuk Kim  * file.c
267439a53e0cSJaegeuk Kim  */
2675cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2676cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2677cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2678cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2679a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2680a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2681cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2682cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2683f652e9d9SChao Yu void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2684c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2685cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2686cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
26871ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
268839a53e0cSJaegeuk Kim 
268939a53e0cSJaegeuk Kim /*
269039a53e0cSJaegeuk Kim  * inode.c
269139a53e0cSJaegeuk Kim  */
2692cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2693704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2694704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2695cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2696cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2697cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2698211a6fa0SYunlei He void update_inode(struct inode *inode, struct page *node_page);
2699211a6fa0SYunlei He void update_inode_page(struct inode *inode);
2700cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2701cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2702cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
270339a53e0cSJaegeuk Kim 
270439a53e0cSJaegeuk Kim /*
270539a53e0cSJaegeuk Kim  * namei.c
270639a53e0cSJaegeuk Kim  */
2707846ae671SChao Yu int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2708b6a06cbbSChao Yu 							bool hot, bool set);
270939a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
271039a53e0cSJaegeuk Kim 
271139a53e0cSJaegeuk Kim /*
271239a53e0cSJaegeuk Kim  * dir.c
271339a53e0cSJaegeuk Kim  */
2714cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2715cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2716cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2717cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2718cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2719cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2720cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2721cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2722cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2723cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2724cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2725cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2726cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2727cac5a3d8SDongOh Shin 			unsigned int current_depth);
2728cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2729cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2730cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2731cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2732cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2733cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2734cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2735cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2736cac5a3d8SDongOh Shin 			struct page **page);
2737cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2738cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2739cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2740cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2741cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2742cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2743cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2744cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2745cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2746cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2747cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2748cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2749cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2750cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2751cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2752cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
275339a53e0cSJaegeuk Kim 
2754b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2755b7f7a5e0SAl Viro {
27562b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2757510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2758b7f7a5e0SAl Viro }
2759b7f7a5e0SAl Viro 
276039a53e0cSJaegeuk Kim /*
276139a53e0cSJaegeuk Kim  * super.c
276239a53e0cSJaegeuk Kim  */
2763cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2764cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2765ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
27664b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2767cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2768cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2769a07ef784SNamjae Jeon extern __printf(3, 4)
2770cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2771984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
277239a53e0cSJaegeuk Kim 
277339a53e0cSJaegeuk Kim /*
277439a53e0cSJaegeuk Kim  * hash.c
277539a53e0cSJaegeuk Kim  */
27766332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
27776332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
277839a53e0cSJaegeuk Kim 
277939a53e0cSJaegeuk Kim /*
278039a53e0cSJaegeuk Kim  * node.c
278139a53e0cSJaegeuk Kim  */
278239a53e0cSJaegeuk Kim struct dnode_of_data;
278339a53e0cSJaegeuk Kim struct node_info;
278439a53e0cSJaegeuk Kim 
2785a4f843bdSJaegeuk Kim int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
2786cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2787cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2788cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2789cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2790cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2791cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2792cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2793cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
27949c77f754SJaegeuk Kim int truncate_xattr_node(struct inode *inode);
2795cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2796cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2797cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
27985f4ce6abSYunlei He struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2799cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2800cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2801cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2802cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2803cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2804cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2805401db79fSYunlong Song int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2806b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
280722ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2808cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2809cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2810cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2811cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2812cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2813e17d488bSSheng Yong int recover_xattr_data(struct inode *inode, struct page *page);
2814cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2815c376fc0fSYunlei He void restore_node_summary(struct f2fs_sb_info *sbi,
2816cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
281722ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2818cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2819cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
28206e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
282139a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
282239a53e0cSJaegeuk Kim 
282339a53e0cSJaegeuk Kim /*
282439a53e0cSJaegeuk Kim  * segment.c
282539a53e0cSJaegeuk Kim  */
2826b3a97a2aSJaegeuk Kim bool need_SSR(struct f2fs_sb_info *sbi);
2827cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
2828*2ef79ecbSChao Yu void drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
2829cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
28308c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2831cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2832cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2833cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
283439d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2835cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
28361228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2837cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2838cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2839cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
28407950e9acSChao Yu void drop_discard_cmd(struct f2fs_sb_info *sbi);
2841cce13252SChao Yu void stop_discard_thread(struct f2fs_sb_info *sbi);
2842cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2843cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2844cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2845cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2846cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2847cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2848cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2849cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2850cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2851b0af6d49SChao Yu void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2852b0af6d49SChao Yu 						enum iostat_type io_type);
2853cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2854cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2855d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2856cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2857cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2858cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2859cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2860cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2861cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2862cac5a3d8SDongOh Shin 			bool recover_newaddr);
2863cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2864cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2865fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2866fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2867cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2868cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2869d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2870cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2871cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2872cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2873cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2874cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2875cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2876cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
28777fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
28787fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
2879d5097be5SHyunchul Lee int rw_hint_to_seg_type(enum rw_hint hint);
28800cdd3195SHyunchul Lee enum rw_hint io_type_to_rw_hint(struct f2fs_sb_info *sbi, enum page_type type,
28810cdd3195SHyunchul Lee 				enum temp_type temp);
288239a53e0cSJaegeuk Kim 
288339a53e0cSJaegeuk Kim /*
288439a53e0cSJaegeuk Kim  * checkpoint.c
288539a53e0cSJaegeuk Kim  */
2886cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2887cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2888cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2889cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
28907b525dd0SChao Yu bool is_valid_meta_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2891cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2892cac5a3d8SDongOh Shin 			int type, bool sync);
2893cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2894cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2895b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
2896cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2897cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2898cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2899cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
290039d787beSChao Yu void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
290139d787beSChao Yu 					unsigned int devidx, int type);
290239d787beSChao Yu bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
290339d787beSChao Yu 					unsigned int devidx, int type);
2904cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2905cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2906cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2907cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2908cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2909cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2910cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2911cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2912cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2913cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2914cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2915cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
29166e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
291739a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
291839a53e0cSJaegeuk Kim 
291939a53e0cSJaegeuk Kim /*
292039a53e0cSJaegeuk Kim  * data.c
292139a53e0cSJaegeuk Kim  */
29226dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
29236dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
2924b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2925b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2926942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2927b9109b0eSJaegeuk Kim 				enum page_type type);
2928b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2929cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2930b9109b0eSJaegeuk Kim int f2fs_submit_page_write(struct f2fs_io_info *fio);
2931cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2932cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2933cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2934cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2935cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2936cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2937cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2938cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2939cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2940cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2941cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2942cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2943cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2944cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2945cac5a3d8SDongOh Shin 			bool for_write);
2946cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2947cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2948cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2949cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2950cac5a3d8SDongOh Shin 			int create, int flag);
2951cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2952cac5a3d8SDongOh Shin 			u64 start, u64 len);
2953bb9e3bb8SChao Yu bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2954bb9e3bb8SChao Yu bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2955b0af6d49SChao Yu int __f2fs_write_data_pages(struct address_space *mapping,
2956b0af6d49SChao Yu 						struct writeback_control *wbc,
2957b0af6d49SChao Yu 						enum iostat_type io_type);
2958cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2959cac5a3d8SDongOh Shin 			unsigned int length);
2960cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
29615b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2962cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2963cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
29645b7a487cSWeichao Guo #endif
2965b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
296639a53e0cSJaegeuk Kim 
296739a53e0cSJaegeuk Kim /*
296839a53e0cSJaegeuk Kim  * gc.c
296939a53e0cSJaegeuk Kim  */
2970cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2971cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2972cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2973e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2974e066b83cSJaegeuk Kim 			unsigned int segno);
2975cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
297639a53e0cSJaegeuk Kim 
297739a53e0cSJaegeuk Kim /*
297839a53e0cSJaegeuk Kim  * recovery.c
297939a53e0cSJaegeuk Kim  */
2980cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2981cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
298239a53e0cSJaegeuk Kim 
298339a53e0cSJaegeuk Kim /*
298439a53e0cSJaegeuk Kim  * debug.c
298539a53e0cSJaegeuk Kim  */
298639a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
298739a53e0cSJaegeuk Kim struct f2fs_stat_info {
298839a53e0cSJaegeuk Kim 	struct list_head stat_list;
298939a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
299039a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
299139a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
29925b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
29935b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2994c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
29952c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
29962c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
299735782b23SJaegeuk Kim 	int inmem_pages;
29982c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
29995b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
30005b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
300139a53e0cSJaegeuk Kim 	int total_count, utilization;
30028b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
300314d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
300414d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
30055f32366aSChao Yu 	int nr_discard_cmd;
3006d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3007a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3008648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3009f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
301039a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
301139a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
301239a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
301339a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
301442190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
301539a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3016e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
301739a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3018e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
3019*2ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
302039a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
302139a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
302239a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
302339a53e0cSJaegeuk Kim 
302439a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
302539a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3026b9a2c252SChangman Lee 	unsigned int inplace_count;
30279edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
302839a53e0cSJaegeuk Kim };
302939a53e0cSJaegeuk Kim 
3030963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3031963d4f7dSGu Zheng {
3032963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3033963d4f7dSGu Zheng }
3034963d4f7dSGu Zheng 
3035942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
303642190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
303739a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3038dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
303933fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
304033fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
30415b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
30425b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
30435b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
30445b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3045d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3046d5e8f6c9SChao Yu 	do {								\
3047d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3048d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3049d5e8f6c9SChao Yu 	} while (0)
3050d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3051d5e8f6c9SChao Yu 	do {								\
3052d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3053d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3054d5e8f6c9SChao Yu 	} while (0)
30550dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
30560dbdc2aeSJaegeuk Kim 	do {								\
30570dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
305803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
30590dbdc2aeSJaegeuk Kim 	} while (0)
30600dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
30610dbdc2aeSJaegeuk Kim 	do {								\
30620dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
306303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
30640dbdc2aeSJaegeuk Kim 	} while (0)
30653289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
30663289c061SJaegeuk Kim 	do {								\
30673289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
306803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
30693289c061SJaegeuk Kim 	} while (0)
30703289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
30713289c061SJaegeuk Kim 	do {								\
30723289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
307303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
30743289c061SJaegeuk Kim 	} while (0)
3075dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3076dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3077dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3078dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3079b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3080b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
308126a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3082cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
308326a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3084cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
308526a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
308626a28a0cSJaegeuk Kim 	do {								\
308726a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
308826a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
308926a28a0cSJaegeuk Kim 		if (cur > max)						\
309026a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
309126a28a0cSJaegeuk Kim 	} while (0)
3092648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3093648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3094648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3095648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3096648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3097648d50baSChao Yu 	do {								\
3098648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3099648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3100648d50baSChao Yu 		if (cur > max)						\
3101648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3102648d50baSChao Yu 	} while (0)
3103e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
310439a53e0cSJaegeuk Kim 	do {								\
3105963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
310668afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
310768afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
310839a53e0cSJaegeuk Kim 			si->data_segs++;				\
3109e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3110e1235983SChangman Lee 		} else {						\
311139a53e0cSJaegeuk Kim 			si->node_segs++;				\
3112e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3113e1235983SChangman Lee 		}							\
311439a53e0cSJaegeuk Kim 	} while (0)
311539a53e0cSJaegeuk Kim 
311639a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
311768afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
311839a53e0cSJaegeuk Kim 
3119e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
312039a53e0cSJaegeuk Kim 	do {								\
3121963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
312239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
312339a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
312468afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
312539a53e0cSJaegeuk Kim 	} while (0)
312639a53e0cSJaegeuk Kim 
3127e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
312839a53e0cSJaegeuk Kim 	do {								\
3129963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
313039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
313139a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
313268afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
313339a53e0cSJaegeuk Kim 	} while (0)
313439a53e0cSJaegeuk Kim 
3135cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3136cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3137787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
31384589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
313939a53e0cSJaegeuk Kim #else
3140d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3141d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3142d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3143d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3144d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3145d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3146d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3147d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3148d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3149d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3150d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3151d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3152d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3153d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3154d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3155d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3156d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3157d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3158d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3159d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3160d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3161d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3162d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3163d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3164d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3165d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3166d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3167d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3168d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
316939a53e0cSJaegeuk Kim 
317039a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
317139a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3172787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
31734589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
317439a53e0cSJaegeuk Kim #endif
317539a53e0cSJaegeuk Kim 
317639a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
317739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
317839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
317939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
318039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
318139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
318239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
318339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3184cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
318539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
318629e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
31871001b347SHuajun Li 
3188e18c65b2SHuajun Li /*
3189e18c65b2SHuajun Li  * inline.c
3190e18c65b2SHuajun Li  */
3191cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3192cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
3193cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
3194bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3195cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3196cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3197cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3198cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
3199cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
3200cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3201cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
3202cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3203cac5a3d8SDongOh Shin 			struct page *ipage);
3204cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3205cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3206cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3207cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3208cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3209cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3210cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3211cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3212cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3213cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3214cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3215cde4de12SJaegeuk Kim 
3216cde4de12SJaegeuk Kim /*
32172658e50dSJaegeuk Kim  * shrinker.c
32182658e50dSJaegeuk Kim  */
3219cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3220cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3221cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3222cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3223cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3224cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
32252658e50dSJaegeuk Kim 
32262658e50dSJaegeuk Kim /*
3227a28ef1f5SChao Yu  * extent_cache.c
3228a28ef1f5SChao Yu  */
3229004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3230004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
3231004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3232004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3233004b6862SChao Yu 				unsigned int ofs);
3234004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3235004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3236004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3237004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3238004b6862SChao Yu 		bool force);
3239df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3240df0f6b44SChao Yu 						struct rb_root *root);
3241cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3242cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3243cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3244cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3245cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3246cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3247cac5a3d8SDongOh Shin 			struct extent_info *ei);
3248cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
324919b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3250cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3251cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
3252a28ef1f5SChao Yu int __init create_extent_cache(void);
3253a28ef1f5SChao Yu void destroy_extent_cache(void);
3254a28ef1f5SChao Yu 
3255a28ef1f5SChao Yu /*
32568ceffcb2SChao Yu  * sysfs.c
32578ceffcb2SChao Yu  */
3258dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3259dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3260dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3261dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
32628ceffcb2SChao Yu 
32638ceffcb2SChao Yu /*
3264cde4de12SJaegeuk Kim  * crypto support
3265cde4de12SJaegeuk Kim  */
32660b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3267cde4de12SJaegeuk Kim {
3268cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3269cde4de12SJaegeuk Kim }
3270cde4de12SJaegeuk Kim 
32711958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
32721958593eSJaegeuk Kim {
32731958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
32741958593eSJaegeuk Kim }
32751958593eSJaegeuk Kim 
3276cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3277cde4de12SJaegeuk Kim {
3278cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3279cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
32802ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3281cde4de12SJaegeuk Kim #endif
3282cde4de12SJaegeuk Kim }
3283cde4de12SJaegeuk Kim 
32846dbb1796SEric Biggers /*
32856dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
32866dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
32876dbb1796SEric Biggers  */
32886dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3289cde4de12SJaegeuk Kim {
32906dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3291cde4de12SJaegeuk Kim }
3292cde4de12SJaegeuk Kim 
3293ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3294ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3295ccd31cb2SSheng Yong { \
3296ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3297cde4de12SJaegeuk Kim }
3298f424f664SJaegeuk Kim 
3299ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3300ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3301ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3302ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3303ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3304ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3305ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3306ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3307b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
33081c1d35dfSChao Yu 
3309178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3310178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
33113c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3312178053e2SDamien Le Moal {
3313178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
33143c62be17SJaegeuk Kim 	int i;
3315178053e2SDamien Le Moal 
33163c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
33173c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
33183c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
33193c62be17SJaegeuk Kim 	return -EINVAL;
3320178053e2SDamien Le Moal }
3321178053e2SDamien Le Moal #endif
3322178053e2SDamien Le Moal 
332396ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
332496ba2decSDamien Le Moal {
332596ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
332696ba2decSDamien Le Moal 
3327ccd31cb2SSheng Yong 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
332852763a4bSJaegeuk Kim }
332952763a4bSJaegeuk Kim 
333052763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
333152763a4bSJaegeuk Kim {
333252763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
333352763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
333452763a4bSJaegeuk Kim 
333552763a4bSJaegeuk Kim 	switch (mt) {
333652763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
333752763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
333852763a4bSJaegeuk Kim 		break;
333952763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
334052763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
334152763a4bSJaegeuk Kim 		break;
334252763a4bSJaegeuk Kim 	}
334352763a4bSJaegeuk Kim }
334452763a4bSJaegeuk Kim 
3345fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3346fcc85a4dSJaegeuk Kim {
3347fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3348886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3349fcc85a4dSJaegeuk Kim 
3350fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3351fcc85a4dSJaegeuk Kim #else
3352fcc85a4dSJaegeuk Kim 	return 0;
3353fcc85a4dSJaegeuk Kim #endif
3354fcc85a4dSJaegeuk Kim }
3355fcc85a4dSJaegeuk Kim 
3356b91050a8SHyunchul Lee static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3357b91050a8SHyunchul Lee {
33586dbb1796SEric Biggers 	return (f2fs_post_read_required(inode) ||
3359b91050a8SHyunchul Lee 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3360b91050a8SHyunchul Lee 			F2FS_I_SB(inode)->s_ndevs);
3361b91050a8SHyunchul Lee }
3362b91050a8SHyunchul Lee 
336339a53e0cSJaegeuk Kim #endif
3364