xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 004b68621897f06aa2817e7438469d23f4a3a284)
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>
2239307a8eSJaegeuk Kim #include <linux/vmalloc.h>
23740432f8SJaegeuk Kim #include <linux/bio.h>
24d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
2546f47e48SEric Biggers #ifdef CONFIG_F2FS_FS_ENCRYPTION
2646f47e48SEric Biggers #include <linux/fscrypt_supp.h>
2746f47e48SEric Biggers #else
2846f47e48SEric Biggers #include <linux/fscrypt_notsupp.h>
2946f47e48SEric Biggers #endif
3043b6573bSKeith Mok #include <crypto/hash.h>
3139a53e0cSJaegeuk Kim 
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,
47c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
48cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
49cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
50cb78942bSJaegeuk Kim 	FAULT_BLOCK,
51cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5253aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5314b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
548b038c70SChao Yu 	FAULT_IO,
550f348028SChao Yu 	FAULT_CHECKPOINT,
562c63feadSJaegeuk Kim 	FAULT_MAX,
572c63feadSJaegeuk Kim };
582c63feadSJaegeuk Kim 
5908796897SSheng Yong struct f2fs_fault_info {
6008796897SSheng Yong 	atomic_t inject_ops;
6108796897SSheng Yong 	unsigned int inject_rate;
6208796897SSheng Yong 	unsigned int inject_type;
6308796897SSheng Yong };
6408796897SSheng Yong 
652c63feadSJaegeuk Kim extern char *fault_name[FAULT_MAX];
6668afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
672c63feadSJaegeuk Kim #endif
682c63feadSJaegeuk Kim 
6939a53e0cSJaegeuk Kim /*
7039a53e0cSJaegeuk Kim  * For mount options
7139a53e0cSJaegeuk Kim  */
7239a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7339a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7439a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7539a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
79444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
801001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8134d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8234d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8334d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
84d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8589672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
866aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
87343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
8873faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
8936abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9036abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
9139a53e0cSJaegeuk Kim 
9268afcf2dSTomohiro Kusumi #define clear_opt(sbi, option)	((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
9368afcf2dSTomohiro Kusumi #define set_opt(sbi, option)	((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
9468afcf2dSTomohiro Kusumi #define test_opt(sbi, option)	((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
9539a53e0cSJaegeuk Kim 
9639a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
9739a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
9839a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
9939a53e0cSJaegeuk Kim 
100a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
101a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
102a9841c4dSJaegeuk Kim 			 * address format, __le32.
103a9841c4dSJaegeuk Kim 			 */
10439a53e0cSJaegeuk Kim typedef u32 nid_t;
10539a53e0cSJaegeuk Kim 
10639a53e0cSJaegeuk Kim struct f2fs_mount_info {
10739a53e0cSJaegeuk Kim 	unsigned int	opt;
10839a53e0cSJaegeuk Kim };
10939a53e0cSJaegeuk Kim 
110cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT	0x0001
1110bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED	0x0002
112cde4de12SJaegeuk Kim 
11376f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
11476f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
11576f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
116c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
11776f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
118c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
11976f105a2SJaegeuk Kim 
12039a53e0cSJaegeuk Kim /*
12139a53e0cSJaegeuk Kim  * For checkpoint manager
12239a53e0cSJaegeuk Kim  */
12339a53e0cSJaegeuk Kim enum {
12439a53e0cSJaegeuk Kim 	NAT_BITMAP,
12539a53e0cSJaegeuk Kim 	SIT_BITMAP
12639a53e0cSJaegeuk Kim };
12739a53e0cSJaegeuk Kim 
12875ab4cb8SJaegeuk Kim enum {
12975ab4cb8SJaegeuk Kim 	CP_UMOUNT,
130119ee914SJaegeuk Kim 	CP_FASTBOOT,
13175ab4cb8SJaegeuk Kim 	CP_SYNC,
13210027551SJaegeuk Kim 	CP_RECOVERY,
1334b2fecc8SJaegeuk Kim 	CP_DISCARD,
13475ab4cb8SJaegeuk Kim };
13575ab4cb8SJaegeuk Kim 
13647b89808SJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	2048
137bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
1384ddb1a4dSJaegeuk Kim 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
139a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
140a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
1414ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
14215469963SJaegeuk Kim #define DISCARD_ISSUE_RATE		8
14360b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
144dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
145bba681cbSJaegeuk Kim 
14675ab4cb8SJaegeuk Kim struct cp_control {
14775ab4cb8SJaegeuk Kim 	int reason;
1484b2fecc8SJaegeuk Kim 	__u64 trim_start;
1494b2fecc8SJaegeuk Kim 	__u64 trim_end;
1504b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
1514b2fecc8SJaegeuk Kim 	__u64 trimmed;
15275ab4cb8SJaegeuk Kim };
15375ab4cb8SJaegeuk Kim 
154662befdaSChao Yu /*
15581c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
156662befdaSChao Yu  */
157662befdaSChao Yu enum {
158662befdaSChao Yu 	META_CP,
159662befdaSChao Yu 	META_NAT,
16081c1a0f1SChao Yu 	META_SIT,
1614c521f49SJaegeuk Kim 	META_SSA,
1624c521f49SJaegeuk Kim 	META_POR,
163662befdaSChao Yu };
164662befdaSChao Yu 
1656451e041SJaegeuk Kim /* for the list of ino */
1666451e041SJaegeuk Kim enum {
1676451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
168fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
169fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1706451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1716451e041SJaegeuk Kim };
1726451e041SJaegeuk Kim 
1736451e041SJaegeuk Kim struct ino_entry {
17439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
17539a53e0cSJaegeuk Kim 	nid_t ino;		/* inode number */
17639a53e0cSJaegeuk Kim };
17739a53e0cSJaegeuk Kim 
1782710fd7eSChao Yu /* for the list of inodes to be GCed */
17906292073SChao Yu struct inode_entry {
18039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
18139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
18239a53e0cSJaegeuk Kim };
18339a53e0cSJaegeuk Kim 
184a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
1857fd9e544SJaegeuk Kim struct discard_entry {
1867fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
187a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
188a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
1897fd9e544SJaegeuk Kim };
1907fd9e544SJaegeuk Kim 
19115469963SJaegeuk Kim enum {
19215469963SJaegeuk Kim 	D_PREP,
19315469963SJaegeuk Kim 	D_SUBMIT,
19415469963SJaegeuk Kim 	D_DONE,
19515469963SJaegeuk Kim };
19615469963SJaegeuk Kim 
197*004b6862SChao Yu struct discard_info {
198*004b6862SChao Yu 	block_t lstart;			/* logical start address */
199*004b6862SChao Yu 	block_t len;			/* length */
200*004b6862SChao Yu 	block_t start;			/* actual start address in dev */
201*004b6862SChao Yu };
202*004b6862SChao Yu 
203b01a9201SJaegeuk Kim struct discard_cmd {
204*004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
205*004b6862SChao Yu 	union {
206*004b6862SChao Yu 		struct {
207*004b6862SChao Yu 			block_t lstart;	/* logical start address */
208*004b6862SChao Yu 			block_t len;	/* length */
209*004b6862SChao Yu 			block_t start;	/* actual start address in dev */
210*004b6862SChao Yu 		};
211*004b6862SChao Yu 		struct discard_info di;	/* discard info */
212*004b6862SChao Yu 
213*004b6862SChao Yu 	};
214b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
215b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
216c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
21715469963SJaegeuk Kim 	int state;			/* state */
218c81abe34SJaegeuk Kim 	int error;			/* bio error */
219275b66b0SChao Yu };
220275b66b0SChao Yu 
2210b54fb84SJaegeuk Kim struct discard_cmd_control {
22215469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
2230b54fb84SJaegeuk Kim 	struct list_head discard_entry_list;	/* 4KB discard entry list */
2240b54fb84SJaegeuk Kim 	int nr_discards;			/* # of discards in the list */
22522d375ddSChao Yu 	struct list_head discard_pend_list;	/* store pending entries */
22622d375ddSChao Yu 	struct list_head discard_wait_list;	/* store on-flushing entries */
22715469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
22815469963SJaegeuk Kim 	struct mutex cmd_lock;
2290b54fb84SJaegeuk Kim 	int max_discards;			/* max. discards to be issued */
2308b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
2318b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
2325f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
233*004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
23439a53e0cSJaegeuk Kim };
23539a53e0cSJaegeuk Kim 
23639a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
23739a53e0cSJaegeuk Kim struct fsync_inode_entry {
23839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
23939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
240c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
241c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
24239a53e0cSJaegeuk Kim };
24339a53e0cSJaegeuk Kim 
24468afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
24568afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
24639a53e0cSJaegeuk Kim 
24768afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
24868afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
24968afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
25068afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
25139a53e0cSJaegeuk Kim 
252dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
253dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
254309cc2b6SJaegeuk Kim 
255dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
25639a53e0cSJaegeuk Kim {
257dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
258cac5a3d8SDongOh Shin 
259dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
26039a53e0cSJaegeuk Kim 	return before;
26139a53e0cSJaegeuk Kim }
26239a53e0cSJaegeuk Kim 
263dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
26439a53e0cSJaegeuk Kim {
265dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
266cac5a3d8SDongOh Shin 
267dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
26839a53e0cSJaegeuk Kim 	return before;
26939a53e0cSJaegeuk Kim }
27039a53e0cSJaegeuk Kim 
271dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
272dfc08a12SChao Yu 							int size, int type)
273184a5cd2SChao Yu {
274184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
275dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
276dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
277184a5cd2SChao Yu }
278184a5cd2SChao Yu 
27939a53e0cSJaegeuk Kim /*
280e9750824SNamjae Jeon  * ioctl commands
281e9750824SNamjae Jeon  */
282e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
283e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
284d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
285e9750824SNamjae Jeon 
28688b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
28788b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
28888b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
28902a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
2901e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
2911e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
292c1c1b583SChao Yu #define F2FS_IOC_GARBAGE_COLLECT	_IO(F2FS_IOCTL_MAGIC, 6)
293456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
294d323d005SChao Yu #define F2FS_IOC_DEFRAGMENT		_IO(F2FS_IOCTL_MAGIC, 8)
2954dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
2964dd6f977SJaegeuk Kim 						struct f2fs_move_range)
29788b88a66SJaegeuk Kim 
2980b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
2990b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3000b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
301f424f664SJaegeuk Kim 
3021abff93dSJaegeuk Kim /*
3031abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3041abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3051abff93dSJaegeuk Kim  */
3061abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3071abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3081abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3091abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
310c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3111abff93dSJaegeuk Kim 
312e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
313e9750824SNamjae Jeon /*
314e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
315e9750824SNamjae Jeon  */
316e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
317e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
31804ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
319e9750824SNamjae Jeon #endif
320e9750824SNamjae Jeon 
321d323d005SChao Yu struct f2fs_defragment {
322d323d005SChao Yu 	u64 start;
323d323d005SChao Yu 	u64 len;
324d323d005SChao Yu };
325d323d005SChao Yu 
3264dd6f977SJaegeuk Kim struct f2fs_move_range {
3274dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
3284dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
3294dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
3304dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
3314dd6f977SJaegeuk Kim };
3324dd6f977SJaegeuk Kim 
333e9750824SNamjae Jeon /*
33439a53e0cSJaegeuk Kim  * For INODE and NODE manager
33539a53e0cSJaegeuk Kim  */
3367b3cd7d6SJaegeuk Kim /* for directory operations */
3377b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
338d8c6822aSJaegeuk Kim 	struct inode *inode;
3397b3cd7d6SJaegeuk Kim 	const void *bitmap;
3407b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
3417b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
3427b3cd7d6SJaegeuk Kim 	int max;
3437b3cd7d6SJaegeuk Kim };
3447b3cd7d6SJaegeuk Kim 
34564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
34664c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
3477b3cd7d6SJaegeuk Kim {
348d8c6822aSJaegeuk Kim 	d->inode = inode;
3497b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
3507b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
3517b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
3527b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
35364c24ecbSTomohiro Kusumi }
354cac5a3d8SDongOh Shin 
35564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
35664c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_inline_dentry *t)
35764c24ecbSTomohiro Kusumi {
35864c24ecbSTomohiro Kusumi 	d->inode = inode;
3597b3cd7d6SJaegeuk Kim 	d->max = NR_INLINE_DENTRY;
3607b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
3617b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
3627b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
3637b3cd7d6SJaegeuk Kim }
3647b3cd7d6SJaegeuk Kim 
365dbe6a5ffSJaegeuk Kim /*
366dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
367dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
368dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
36939a53e0cSJaegeuk Kim  */
370dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
371dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
372266e97a8SJaegeuk Kim enum {
373266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
374266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
375266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
376266e97a8SJaegeuk Kim 					 * look up a node with readahead called
3774f4124d0SChao Yu 					 * by get_data_block.
37839a53e0cSJaegeuk Kim 					 */
379266e97a8SJaegeuk Kim };
380266e97a8SJaegeuk Kim 
381a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
38239a53e0cSJaegeuk Kim 
383817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
384817202d9SChao Yu 
38513054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
38613054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
38713054c54SChao Yu 
38839a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
38913054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
39013054c54SChao Yu 
39113054c54SChao Yu /* number of extent info in extent cache we try to shrink */
39213054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
393c11abd1aSJaegeuk Kim 
39454c2258cSChao Yu struct rb_entry {
39554c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
39654c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
39754c2258cSChao Yu 	unsigned int len;		/* length of the entry */
39854c2258cSChao Yu };
39954c2258cSChao Yu 
40039a53e0cSJaegeuk Kim struct extent_info {
40139a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
402111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
40354c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
40439a53e0cSJaegeuk Kim };
40539a53e0cSJaegeuk Kim 
40613054c54SChao Yu struct extent_node {
40754c2258cSChao Yu 	struct rb_node rb_node;
40854c2258cSChao Yu 	union {
40954c2258cSChao Yu 		struct {
41054c2258cSChao Yu 			unsigned int fofs;
41154c2258cSChao Yu 			unsigned int len;
41254c2258cSChao Yu 			u32 blk;
41354c2258cSChao Yu 		};
41413054c54SChao Yu 		struct extent_info ei;	/* extent info */
41554c2258cSChao Yu 
41654c2258cSChao Yu 	};
41754c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
418201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
41913054c54SChao Yu };
42013054c54SChao Yu 
42113054c54SChao Yu struct extent_tree {
42213054c54SChao Yu 	nid_t ino;			/* inode number */
42313054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
42462c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
4253e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
426137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
42713054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
42868e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
42913054c54SChao Yu };
43013054c54SChao Yu 
43139a53e0cSJaegeuk Kim /*
432003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
433003a3e1dSJaegeuk Kim  *
434003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
435003a3e1dSJaegeuk Kim  */
436003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
437003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
4387f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
4397f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
4407f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
441003a3e1dSJaegeuk Kim 
442003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
443003a3e1dSJaegeuk Kim 	block_t m_pblk;
444003a3e1dSJaegeuk Kim 	block_t m_lblk;
445003a3e1dSJaegeuk Kim 	unsigned int m_len;
446003a3e1dSJaegeuk Kim 	unsigned int m_flags;
447da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
448003a3e1dSJaegeuk Kim };
449003a3e1dSJaegeuk Kim 
450e2b4e2bcSChao Yu /* for flag in get_data_block */
451e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_READ		0
452e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_DIO		1
453e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_FIEMAP		2
454e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_BMAP		3
455b439b103SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_DIO		4
45624b84912SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_AIO		5
457e2b4e2bcSChao Yu 
458003a3e1dSJaegeuk Kim /*
45939a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
46039a53e0cSJaegeuk Kim  */
46139a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
462354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
463cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
464e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
46526787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
46639a53e0cSJaegeuk Kim 
467b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
468b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
469b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
470b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
471b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
472b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
473cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
474cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
475cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
476e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
477e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
47826787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
47926787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
480cde4de12SJaegeuk Kim 
481ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
482ab9fa662SJaegeuk Kim 
48339a53e0cSJaegeuk Kim struct f2fs_inode_info {
48439a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
48539a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
48639a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
48738431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
48839a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
4896666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
49039a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
49139a53e0cSJaegeuk Kim 
49239a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
49339a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
494d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
495204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
49639a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
49739a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
49888c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
49939a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
50026de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
50188b88a66SJaegeuk Kim 
5020f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
5030f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
50488b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
50588b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
5063e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
50782e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
50839a53e0cSJaegeuk Kim };
50939a53e0cSJaegeuk Kim 
51039a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
511bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
51239a53e0cSJaegeuk Kim {
513bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
514bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
515bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
51639a53e0cSJaegeuk Kim }
51739a53e0cSJaegeuk Kim 
51839a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
51939a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
52039a53e0cSJaegeuk Kim {
52139a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
5224d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
52339a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
52439a53e0cSJaegeuk Kim }
52539a53e0cSJaegeuk Kim 
526429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
527429511cdSChao Yu 						u32 blk, unsigned int len)
528429511cdSChao Yu {
529429511cdSChao Yu 	ei->fofs = fofs;
530429511cdSChao Yu 	ei->blk = blk;
531429511cdSChao Yu 	ei->len = len;
532429511cdSChao Yu }
533429511cdSChao Yu 
534*004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
535*004b6862SChao Yu 						struct discard_info *front)
536*004b6862SChao Yu {
537*004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
538*004b6862SChao Yu }
539*004b6862SChao Yu 
540*004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
541*004b6862SChao Yu 						struct discard_info *back)
542*004b6862SChao Yu {
543*004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
544*004b6862SChao Yu }
545*004b6862SChao Yu 
546*004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
547*004b6862SChao Yu 						struct discard_info *front)
548*004b6862SChao Yu {
549*004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
550*004b6862SChao Yu }
551*004b6862SChao Yu 
552429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
553429511cdSChao Yu 						struct extent_info *front)
554429511cdSChao Yu {
555429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
556429511cdSChao Yu 			back->blk + back->len == front->blk);
557429511cdSChao Yu }
558429511cdSChao Yu 
559429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
560429511cdSChao Yu 						struct extent_info *back)
561429511cdSChao Yu {
562429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
563429511cdSChao Yu }
564429511cdSChao Yu 
565429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
566429511cdSChao Yu 						struct extent_info *front)
567429511cdSChao Yu {
568429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
569429511cdSChao Yu }
570429511cdSChao Yu 
571cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
572205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
573205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
5744abd3f5aSChao Yu {
575205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
5764abd3f5aSChao Yu 		et->largest = en->ei;
5777c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
578205b9822SJaegeuk Kim 	}
5794abd3f5aSChao Yu }
5804abd3f5aSChao Yu 
581b8559dc2SChao Yu enum nid_list {
582b8559dc2SChao Yu 	FREE_NID_LIST,
583b8559dc2SChao Yu 	ALLOC_NID_LIST,
584b8559dc2SChao Yu 	MAX_NID_LIST,
585b8559dc2SChao Yu };
586b8559dc2SChao Yu 
58739a53e0cSJaegeuk Kim struct f2fs_nm_info {
58839a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
58939a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
59004d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
59139a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
592cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
593ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
5942304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
59539a53e0cSJaegeuk Kim 
59639a53e0cSJaegeuk Kim 	/* NAT cache management */
59739a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
598309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
599b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
60039a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
601309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
602aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
60322ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
60439a53e0cSJaegeuk Kim 
60539a53e0cSJaegeuk Kim 	/* free node ids management */
6068a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
607b8559dc2SChao Yu 	struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
608b8559dc2SChao Yu 	unsigned int nid_cnt[MAX_NID_LIST];	/* the number of free node id */
609b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
61039a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
6114ac91242SChao Yu 	unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
6124ac91242SChao Yu 	unsigned char *nat_block_bitmap;
613586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
61439a53e0cSJaegeuk Kim 
61539a53e0cSJaegeuk Kim 	/* for checkpoint */
61639a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
61722ad0b6aSJaegeuk Kim 
61822ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
61922ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
62022ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
62122ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
622599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
623599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
624599a09b2SChao Yu #endif
62539a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
62639a53e0cSJaegeuk Kim };
62739a53e0cSJaegeuk Kim 
62839a53e0cSJaegeuk Kim /*
62939a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
63039a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
63139a53e0cSJaegeuk Kim  * by the data offset in a file.
63239a53e0cSJaegeuk Kim  */
63339a53e0cSJaegeuk Kim struct dnode_of_data {
63439a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
63539a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
63639a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
63739a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
63839a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
63939a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
64093bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
6413cf45747SChao Yu 	char cur_level;			/* level of hole node page */
6423cf45747SChao Yu 	char max_level;			/* level of current page located */
64339a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
64439a53e0cSJaegeuk Kim };
64539a53e0cSJaegeuk Kim 
64639a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
64739a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
64839a53e0cSJaegeuk Kim {
649d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
65039a53e0cSJaegeuk Kim 	dn->inode = inode;
65139a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
65239a53e0cSJaegeuk Kim 	dn->node_page = npage;
65339a53e0cSJaegeuk Kim 	dn->nid = nid;
65439a53e0cSJaegeuk Kim }
65539a53e0cSJaegeuk Kim 
65639a53e0cSJaegeuk Kim /*
65739a53e0cSJaegeuk Kim  * For SIT manager
65839a53e0cSJaegeuk Kim  *
65939a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
66039a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
66139a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
66239a53e0cSJaegeuk Kim  * respectively.
66339a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
66439a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
66539a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
66639a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
66739a53e0cSJaegeuk Kim  * data and 8 for node logs.
66839a53e0cSJaegeuk Kim  */
66939a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
67039a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
67139a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
67239a53e0cSJaegeuk Kim 
67339a53e0cSJaegeuk Kim enum {
67439a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
67539a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
67639a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
67739a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
67839a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
67939a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
68038aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
68139a53e0cSJaegeuk Kim };
68239a53e0cSJaegeuk Kim 
6836b4afdd7SJaegeuk Kim struct flush_cmd {
6846b4afdd7SJaegeuk Kim 	struct completion wait;
685721bd4d5SGu Zheng 	struct llist_node llnode;
6866b4afdd7SJaegeuk Kim 	int ret;
6876b4afdd7SJaegeuk Kim };
6886b4afdd7SJaegeuk Kim 
689a688b9d9SGu Zheng struct flush_cmd_control {
690a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
691a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
6928b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
6938b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
694721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
695721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
696a688b9d9SGu Zheng };
697a688b9d9SGu Zheng 
69839a53e0cSJaegeuk Kim struct f2fs_sm_info {
69939a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
70039a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
70139a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
70239a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
70339a53e0cSJaegeuk Kim 
70439a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
70539a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
70639a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
70739a53e0cSJaegeuk Kim 
70839a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
70939a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
71039a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
71139a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
71281eb8d6eSJaegeuk Kim 
71381eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
71481eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
7157fd9e544SJaegeuk Kim 
716bba681cbSJaegeuk Kim 	/* for batched trimming */
717bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
718bba681cbSJaegeuk Kim 
719184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
720184a5cd2SChao Yu 
721216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
722216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
723c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
724ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
7256b4afdd7SJaegeuk Kim 
7266b4afdd7SJaegeuk Kim 	/* for flush command control */
727b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
728a688b9d9SGu Zheng 
7290b54fb84SJaegeuk Kim 	/* for discard command control */
7300b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
73139a53e0cSJaegeuk Kim };
73239a53e0cSJaegeuk Kim 
73339a53e0cSJaegeuk Kim /*
73439a53e0cSJaegeuk Kim  * For superblock
73539a53e0cSJaegeuk Kim  */
73639a53e0cSJaegeuk Kim /*
73739a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
73839a53e0cSJaegeuk Kim  *
73939a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
74039a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
74139a53e0cSJaegeuk Kim  */
74236951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
74339a53e0cSJaegeuk Kim enum count_type {
74439a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
745c227f912SChao Yu 	F2FS_DIRTY_DATA,
74639a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
74739a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
7488dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
7490f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
75036951b38SChao Yu 	F2FS_WB_CP_DATA,
75136951b38SChao Yu 	F2FS_WB_DATA,
75239a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
75339a53e0cSJaegeuk Kim };
75439a53e0cSJaegeuk Kim 
75539a53e0cSJaegeuk Kim /*
756e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
75739a53e0cSJaegeuk Kim  * The available types are:
75839a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
75939a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
76039a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
76139a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
76239a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
76339a53e0cSJaegeuk Kim  *			with waiting the bio's completion
76439a53e0cSJaegeuk Kim  * ...			Only can be used with META.
76539a53e0cSJaegeuk Kim  */
7667d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
76739a53e0cSJaegeuk Kim enum page_type {
76839a53e0cSJaegeuk Kim 	DATA,
76939a53e0cSJaegeuk Kim 	NODE,
77039a53e0cSJaegeuk Kim 	META,
77139a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
77239a53e0cSJaegeuk Kim 	META_FLUSH,
7738ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
7748ce67cb0SJaegeuk Kim 	INMEM_DROP,
7758c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
77628bc106bSChao Yu 	INMEM_REVOKE,
7778ce67cb0SJaegeuk Kim 	IPU,
7788ce67cb0SJaegeuk Kim 	OPU,
77939a53e0cSJaegeuk Kim };
78039a53e0cSJaegeuk Kim 
781458e6197SJaegeuk Kim struct f2fs_io_info {
78205ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
783458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
78404d328deSMike Christie 	int op;			/* contains REQ_OP_ */
785ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
7867a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
78728bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
78805ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
7894375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
790d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
791458e6197SJaegeuk Kim };
792458e6197SJaegeuk Kim 
79368afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
7941ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
795458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
7961ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
7971ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
798458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
799df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
8001ff7bd3bSJaegeuk Kim };
8011ff7bd3bSJaegeuk Kim 
8023c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
8033c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
8043c62be17SJaegeuk Kim struct f2fs_dev_info {
8053c62be17SJaegeuk Kim 	struct block_device *bdev;
8063c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
8073c62be17SJaegeuk Kim 	unsigned int total_segments;
8083c62be17SJaegeuk Kim 	block_t start_blk;
8093c62be17SJaegeuk Kim 	block_t end_blk;
8103c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
8113c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
8123c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
8133c62be17SJaegeuk Kim #endif
8143c62be17SJaegeuk Kim };
8153c62be17SJaegeuk Kim 
816c227f912SChao Yu enum inode_type {
817c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
818c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
8190f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
820c227f912SChao Yu 	NR_INODE_TYPE,
821c227f912SChao Yu };
822c227f912SChao Yu 
82367298804SChao Yu /* for inner inode cache management */
82467298804SChao Yu struct inode_management {
82567298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
82667298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
82767298804SChao Yu 	struct list_head ino_list;		/* inode list head */
82867298804SChao Yu 	unsigned long ino_num;			/* number of entries */
82967298804SChao Yu };
83067298804SChao Yu 
831caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
832caf0047eSChao Yu enum {
833caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
834caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
835caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
836caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
837df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
838bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
839caf0047eSChao Yu };
840caf0047eSChao Yu 
8416beceb54SJaegeuk Kim enum {
8426beceb54SJaegeuk Kim 	CP_TIME,
843d0239e1bSJaegeuk Kim 	REQ_TIME,
8446beceb54SJaegeuk Kim 	MAX_TIME,
8456beceb54SJaegeuk Kim };
8466beceb54SJaegeuk Kim 
84739a53e0cSJaegeuk Kim struct f2fs_sb_info {
84839a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
8495e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
85039a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
851e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
852fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
85339a53e0cSJaegeuk Kim 
854178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
855178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
856178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
857178053e2SDamien Le Moal #endif
858178053e2SDamien Le Moal 
85939a53e0cSJaegeuk Kim 	/* for node-related operations */
86039a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
86139a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
86239a53e0cSJaegeuk Kim 
86339a53e0cSJaegeuk Kim 	/* for segment-related operations */
86439a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
8651ff7bd3bSJaegeuk Kim 
8661ff7bd3bSJaegeuk Kim 	/* for bio operations */
867924b720bSChao Yu 	struct f2fs_bio_info read_io;			/* for read bios */
8681ff7bd3bSJaegeuk Kim 	struct f2fs_bio_info write_io[NR_PAGE_TYPE];	/* for write bios */
8697dfeaa32SJaegeuk Kim 	struct mutex wio_mutex[NODE + 1];	/* bio ordering for NODE/DATA */
8700a595ebaSJaegeuk Kim 	int write_io_size_bits;			/* Write IO size bits */
8710a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
87239a53e0cSJaegeuk Kim 
87339a53e0cSJaegeuk Kim 	/* for checkpoint */
87439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
8758508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
876aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
87739a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
87839936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
879b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
880b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
88159c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
882fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
8836beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
8846beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
88539a53e0cSJaegeuk Kim 
88667298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
8876451e041SJaegeuk Kim 
8886451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
8890d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
89039a53e0cSJaegeuk Kim 
891c227f912SChao Yu 	/* for inode management */
892c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
893c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
89439a53e0cSJaegeuk Kim 
89513054c54SChao Yu 	/* for extent tree cache */
89613054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
8975e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
89813054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
89913054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
9007441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
901137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
90274fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
90313054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
90413054c54SChao Yu 
90539a53e0cSJaegeuk Kim 	/* basic filesystem units */
90639a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
90739a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
90839a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
90939a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
91039a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
91139a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
91239a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
91339a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
91439a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
91539a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
91639a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
91739a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
91839a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
919e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
92039a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
921ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
92239a53e0cSJaegeuk Kim 
92339a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
92439a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
925a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
92639a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
92739a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
928523be8a6SJaegeuk Kim 
929523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
93035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
93141382ec4SJaegeuk Kim 	/* # of allocated blocks */
93241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
93339a53e0cSJaegeuk Kim 
934687de7f1SJaegeuk Kim 	/* writeback control */
935687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
936687de7f1SJaegeuk Kim 
937513c5f37SJaegeuk Kim 	/* valid inode count */
938513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
939513c5f37SJaegeuk Kim 
94039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
94139a53e0cSJaegeuk Kim 
94239a53e0cSJaegeuk Kim 	/* for cleaning operations */
94339a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
94439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
9455ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
94639a53e0cSJaegeuk Kim 
947e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
948e93b9865SHou Pengyang 	u64 fggc_threshold;
949e93b9865SHou Pengyang 
950b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
951b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
952b1c57c1cSJaegeuk Kim 
95339a53e0cSJaegeuk Kim 	/*
95439a53e0cSJaegeuk Kim 	 * for stat information.
95539a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
95639a53e0cSJaegeuk Kim 	 */
95735b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
95839a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
95939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
96039a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
961b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
9625b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
9635b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
9645b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
9655b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
966d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
96703e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
96803e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
96926a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
970648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
97126a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
972648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
97339a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
97433fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
97535b09d82SNamjae Jeon #endif
97635b09d82SNamjae Jeon 	unsigned int last_victim[2];		/* last victim segment # */
97739a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
978b59d0baeSNamjae Jeon 
979b59d0baeSNamjae Jeon 	/* For sysfs suppport */
980b59d0baeSNamjae Jeon 	struct kobject s_kobj;
981b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
9822658e50dSJaegeuk Kim 
9832658e50dSJaegeuk Kim 	/* For shrinker support */
9842658e50dSJaegeuk Kim 	struct list_head s_list;
9853c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
9863c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
9872658e50dSJaegeuk Kim 	struct mutex umount_mutex;
9882658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
9898f1dbbbbSShuoran Liu 
9908f1dbbbbSShuoran Liu 	/* For write statistics */
9918f1dbbbbSShuoran Liu 	u64 sectors_written_start;
9928f1dbbbbSShuoran Liu 	u64 kbytes_written;
99343b6573bSKeith Mok 
99443b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
99543b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
9961ecc0c5cSChao Yu 
9971ecc0c5cSChao Yu 	/* For fault injection */
9981ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
9991ecc0c5cSChao Yu 	struct f2fs_fault_info fault_info;
10001ecc0c5cSChao Yu #endif
100139a53e0cSJaegeuk Kim };
100239a53e0cSJaegeuk Kim 
10031ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
100455523519SChao Yu #define f2fs_show_injection_info(type)				\
100555523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
100655523519SChao Yu 		KERN_INFO, fault_name[type],			\
100755523519SChao Yu 		__func__, __builtin_return_address(0))
10081ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
10091ecc0c5cSChao Yu {
10101ecc0c5cSChao Yu 	struct f2fs_fault_info *ffi = &sbi->fault_info;
10111ecc0c5cSChao Yu 
10121ecc0c5cSChao Yu 	if (!ffi->inject_rate)
10131ecc0c5cSChao Yu 		return false;
10141ecc0c5cSChao Yu 
10151ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
10161ecc0c5cSChao Yu 		return false;
10171ecc0c5cSChao Yu 
10181ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
10191ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
10201ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
10211ecc0c5cSChao Yu 		return true;
10221ecc0c5cSChao Yu 	}
10231ecc0c5cSChao Yu 	return false;
10241ecc0c5cSChao Yu }
10251ecc0c5cSChao Yu #endif
10261ecc0c5cSChao Yu 
10278f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
10288f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
10298f1dbbbbSShuoran Liu  */
10308f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
103168afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
103268afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
10338f1dbbbbSShuoran Liu 
10346beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
10356beceb54SJaegeuk Kim {
10366beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
10376beceb54SJaegeuk Kim }
10386beceb54SJaegeuk Kim 
10396beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
10406beceb54SJaegeuk Kim {
10416beceb54SJaegeuk Kim 	struct timespec ts = {sbi->interval_time[type], 0};
10426beceb54SJaegeuk Kim 	unsigned long interval = timespec_to_jiffies(&ts);
10436beceb54SJaegeuk Kim 
10446beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
10456beceb54SJaegeuk Kim }
10466beceb54SJaegeuk Kim 
1047d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1048d0239e1bSJaegeuk Kim {
1049d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1050d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1051d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1052d0239e1bSJaegeuk Kim 
1053d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1054d0239e1bSJaegeuk Kim 		return 0;
1055d0239e1bSJaegeuk Kim 
1056d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1057d0239e1bSJaegeuk Kim }
1058d0239e1bSJaegeuk Kim 
105939a53e0cSJaegeuk Kim /*
106039a53e0cSJaegeuk Kim  * Inline functions
106139a53e0cSJaegeuk Kim  */
106243b6573bSKeith Mok static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
106343b6573bSKeith Mok 			   unsigned int length)
106443b6573bSKeith Mok {
106543b6573bSKeith Mok 	SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
106643b6573bSKeith Mok 	u32 *ctx = (u32 *)shash_desc_ctx(shash);
106743b6573bSKeith Mok 	int err;
106843b6573bSKeith Mok 
106943b6573bSKeith Mok 	shash->tfm = sbi->s_chksum_driver;
107043b6573bSKeith Mok 	shash->flags = 0;
107143b6573bSKeith Mok 	*ctx = F2FS_SUPER_MAGIC;
107243b6573bSKeith Mok 
107343b6573bSKeith Mok 	err = crypto_shash_update(shash, address, length);
107443b6573bSKeith Mok 	BUG_ON(err);
107543b6573bSKeith Mok 
107643b6573bSKeith Mok 	return *ctx;
107743b6573bSKeith Mok }
107843b6573bSKeith Mok 
107943b6573bSKeith Mok static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
108043b6573bSKeith Mok 				  void *buf, size_t buf_size)
108143b6573bSKeith Mok {
108243b6573bSKeith Mok 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
108343b6573bSKeith Mok }
108443b6573bSKeith Mok 
108539a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
108639a53e0cSJaegeuk Kim {
108739a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
108839a53e0cSJaegeuk Kim }
108939a53e0cSJaegeuk Kim 
109039a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
109139a53e0cSJaegeuk Kim {
109239a53e0cSJaegeuk Kim 	return sb->s_fs_info;
109339a53e0cSJaegeuk Kim }
109439a53e0cSJaegeuk Kim 
10954081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
10964081363fSJaegeuk Kim {
10974081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
10984081363fSJaegeuk Kim }
10994081363fSJaegeuk Kim 
11004081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
11014081363fSJaegeuk Kim {
11024081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
11034081363fSJaegeuk Kim }
11044081363fSJaegeuk Kim 
11054081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
11064081363fSJaegeuk Kim {
11074081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
11084081363fSJaegeuk Kim }
11094081363fSJaegeuk Kim 
111039a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
111139a53e0cSJaegeuk Kim {
111239a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
111339a53e0cSJaegeuk Kim }
111439a53e0cSJaegeuk Kim 
111539a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
111639a53e0cSJaegeuk Kim {
111739a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
111839a53e0cSJaegeuk Kim }
111939a53e0cSJaegeuk Kim 
112045590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
112145590710SGu Zheng {
112245590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
112345590710SGu Zheng }
112445590710SGu Zheng 
112558bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
112658bfaf44SJaegeuk Kim {
112758bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
112858bfaf44SJaegeuk Kim }
112958bfaf44SJaegeuk Kim 
113039a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
113139a53e0cSJaegeuk Kim {
113239a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
113339a53e0cSJaegeuk Kim }
113439a53e0cSJaegeuk Kim 
113539a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
113639a53e0cSJaegeuk Kim {
113739a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
113839a53e0cSJaegeuk Kim }
113939a53e0cSJaegeuk Kim 
114039a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
114139a53e0cSJaegeuk Kim {
114239a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
114339a53e0cSJaegeuk Kim }
114439a53e0cSJaegeuk Kim 
114539a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
114639a53e0cSJaegeuk Kim {
114739a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
114839a53e0cSJaegeuk Kim }
114939a53e0cSJaegeuk Kim 
115039a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
115139a53e0cSJaegeuk Kim {
115239a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
115339a53e0cSJaegeuk Kim }
115439a53e0cSJaegeuk Kim 
11559df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
11569df27d98SGu Zheng {
11579df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
11589df27d98SGu Zheng }
11599df27d98SGu Zheng 
11604ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
11614ef51a8fSJaegeuk Kim {
11624ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
11634ef51a8fSJaegeuk Kim }
11644ef51a8fSJaegeuk Kim 
1165caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
116639a53e0cSJaegeuk Kim {
1167fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
116839a53e0cSJaegeuk Kim }
116939a53e0cSJaegeuk Kim 
1170caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
117139a53e0cSJaegeuk Kim {
1172fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1173caf0047eSChao Yu }
1174caf0047eSChao Yu 
1175caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1176caf0047eSChao Yu {
1177fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
117839a53e0cSJaegeuk Kim }
117939a53e0cSJaegeuk Kim 
1180d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1181d71b5564SJaegeuk Kim {
1182d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1183d71b5564SJaegeuk Kim }
1184d71b5564SJaegeuk Kim 
1185ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1186ced2c7eaSKinglong Mee {
1187ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1188ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1189ced2c7eaSKinglong Mee }
1190ced2c7eaSKinglong Mee 
1191aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
119225ca923bSJaegeuk Kim {
119325ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1194aaec2b1dSChao Yu 
119525ca923bSJaegeuk Kim 	return ckpt_flags & f;
119625ca923bSJaegeuk Kim }
119725ca923bSJaegeuk Kim 
1198aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
119925ca923bSJaegeuk Kim {
1200aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1201aaec2b1dSChao Yu }
1202aaec2b1dSChao Yu 
1203aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1204aaec2b1dSChao Yu {
1205aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1206aaec2b1dSChao Yu 
1207aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
120825ca923bSJaegeuk Kim 	ckpt_flags |= f;
120925ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
121025ca923bSJaegeuk Kim }
121125ca923bSJaegeuk Kim 
1212aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
121325ca923bSJaegeuk Kim {
1214aaec2b1dSChao Yu 	spin_lock(&sbi->cp_lock);
1215aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1216aaec2b1dSChao Yu 	spin_unlock(&sbi->cp_lock);
1217aaec2b1dSChao Yu }
1218aaec2b1dSChao Yu 
1219aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1220aaec2b1dSChao Yu {
1221aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1222aaec2b1dSChao Yu 
1223aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
122425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
122525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
122625ca923bSJaegeuk Kim }
122725ca923bSJaegeuk Kim 
1228aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1229aaec2b1dSChao Yu {
1230aaec2b1dSChao Yu 	spin_lock(&sbi->cp_lock);
1231aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1232aaec2b1dSChao Yu 	spin_unlock(&sbi->cp_lock);
1233aaec2b1dSChao Yu }
1234aaec2b1dSChao Yu 
123522ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
123622ad0b6aSJaegeuk Kim {
123722ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
123822ad0b6aSJaegeuk Kim 
123922ad0b6aSJaegeuk Kim 	if (lock)
124022ad0b6aSJaegeuk Kim 		spin_lock(&sbi->cp_lock);
124122ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
124222ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
124322ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
124422ad0b6aSJaegeuk Kim 	if (lock)
124522ad0b6aSJaegeuk Kim 		spin_unlock(&sbi->cp_lock);
124622ad0b6aSJaegeuk Kim }
124722ad0b6aSJaegeuk Kim 
124822ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
124922ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
125022ad0b6aSJaegeuk Kim {
125122ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
125222ad0b6aSJaegeuk Kim 
125322ad0b6aSJaegeuk Kim 	return (cpc) ? (cpc->reason == CP_UMOUNT) && set : set;
125422ad0b6aSJaegeuk Kim }
125522ad0b6aSJaegeuk Kim 
1256e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
125739a53e0cSJaegeuk Kim {
1258b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
125939a53e0cSJaegeuk Kim }
126039a53e0cSJaegeuk Kim 
1261e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
126239a53e0cSJaegeuk Kim {
1263b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
126439936837SJaegeuk Kim }
126539936837SJaegeuk Kim 
1266e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
126739936837SJaegeuk Kim {
1268b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
126939936837SJaegeuk Kim }
127039936837SJaegeuk Kim 
1271e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
127239936837SJaegeuk Kim {
1273b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
127439a53e0cSJaegeuk Kim }
127539a53e0cSJaegeuk Kim 
1276119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1277119ee914SJaegeuk Kim {
1278119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1279119ee914SJaegeuk Kim 
1280119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1281119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1282119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1283119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1284119ee914SJaegeuk Kim 	return reason;
1285119ee914SJaegeuk Kim }
1286119ee914SJaegeuk Kim 
1287119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1288119ee914SJaegeuk Kim {
1289119ee914SJaegeuk Kim 	return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
1290119ee914SJaegeuk Kim }
1291119ee914SJaegeuk Kim 
1292119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1293119ee914SJaegeuk Kim {
1294aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1295aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1296119ee914SJaegeuk Kim }
1297119ee914SJaegeuk Kim 
129839a53e0cSJaegeuk Kim /*
129939a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
130039a53e0cSJaegeuk Kim  */
1301064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
130239a53e0cSJaegeuk Kim {
1303d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1304d6b7d4b3SChao Yu 		return -EINVAL;
1305cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1306064e0823SNamjae Jeon 		return -EINVAL;
1307064e0823SNamjae Jeon 	return 0;
130839a53e0cSJaegeuk Kim }
130939a53e0cSJaegeuk Kim 
131039a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
131139a53e0cSJaegeuk Kim 
131239a53e0cSJaegeuk Kim /*
131339a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
131439a53e0cSJaegeuk Kim  */
131539a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
131639a53e0cSJaegeuk Kim {
131739a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
13186c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
131939a53e0cSJaegeuk Kim 	else
13206c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
132139a53e0cSJaegeuk Kim }
132239a53e0cSJaegeuk Kim 
13234bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
13244bc8e9bcSChao Yu {
13254bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
13264bc8e9bcSChao Yu }
13274bc8e9bcSChao Yu 
13288edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *, blkcnt_t, bool);
132939a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
133046008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
133139a53e0cSJaegeuk Kim {
1332dd11a5dfSJaegeuk Kim 	blkcnt_t diff;
1333dd11a5dfSJaegeuk Kim 
1334cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
133555523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
133655523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1337cb78942bSJaegeuk Kim 		return false;
133855523519SChao Yu 	}
1339cb78942bSJaegeuk Kim #endif
1340dd11a5dfSJaegeuk Kim 	/*
1341dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1342dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1343dd11a5dfSJaegeuk Kim 	 */
1344dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1345cfb271d4SChao Yu 
134639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
13472555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
13482555a2d5SJaegeuk Kim 	if (unlikely(sbi->total_valid_block_count > sbi->user_block_count)) {
1349dd11a5dfSJaegeuk Kim 		diff = sbi->total_valid_block_count - sbi->user_block_count;
1350dd11a5dfSJaegeuk Kim 		*count -= diff;
13512555a2d5SJaegeuk Kim 		sbi->total_valid_block_count = sbi->user_block_count;
135246008c6dSChao Yu 		if (!*count) {
135339a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1354dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
135539a53e0cSJaegeuk Kim 			return false;
135639a53e0cSJaegeuk Kim 		}
135746008c6dSChao Yu 	}
135839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
135941382ec4SJaegeuk Kim 
13602555a2d5SJaegeuk Kim 	f2fs_i_blocks_write(inode, *count, true);
136139a53e0cSJaegeuk Kim 	return true;
136239a53e0cSJaegeuk Kim }
136339a53e0cSJaegeuk Kim 
1364da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
136539a53e0cSJaegeuk Kim 						struct inode *inode,
136639a53e0cSJaegeuk Kim 						blkcnt_t count)
136739a53e0cSJaegeuk Kim {
136839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
13699850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
13709850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, inode->i_blocks < count);
137139a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
137239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
13732555a2d5SJaegeuk Kim 	f2fs_i_blocks_write(inode, count, false);
137439a53e0cSJaegeuk Kim }
137539a53e0cSJaegeuk Kim 
137639a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
137739a53e0cSJaegeuk Kim {
137835782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
13797c4abcbeSChao Yu 
138036951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
138136951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
13827c4abcbeSChao Yu 		return;
13837c4abcbeSChao Yu 
1384caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
138539a53e0cSJaegeuk Kim }
138639a53e0cSJaegeuk Kim 
1387a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
138839a53e0cSJaegeuk Kim {
1389204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1390c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1391c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
139239a53e0cSJaegeuk Kim }
139339a53e0cSJaegeuk Kim 
139439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
139539a53e0cSJaegeuk Kim {
139635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
139739a53e0cSJaegeuk Kim }
139839a53e0cSJaegeuk Kim 
1399a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
140039a53e0cSJaegeuk Kim {
14015ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
14025ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
14031fe54f9dSJaegeuk Kim 		return;
14041fe54f9dSJaegeuk Kim 
1405204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1406c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1407c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
140839a53e0cSJaegeuk Kim }
140939a53e0cSJaegeuk Kim 
1410523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
141139a53e0cSJaegeuk Kim {
141235782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
141339a53e0cSJaegeuk Kim }
141439a53e0cSJaegeuk Kim 
1415204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1416f8b2c1f9SJaegeuk Kim {
1417204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1418f8b2c1f9SJaegeuk Kim }
1419f8b2c1f9SJaegeuk Kim 
14205ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
14215ac206cfSNamjae Jeon {
14223519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1423523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1424523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1425523be8a6SJaegeuk Kim 
1426523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
14275ac206cfSNamjae Jeon }
14285ac206cfSNamjae Jeon 
142939a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
143039a53e0cSJaegeuk Kim {
14318b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
143239a53e0cSJaegeuk Kim }
143339a53e0cSJaegeuk Kim 
1434f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1435f83a2584SYunlei He {
1436f83a2584SYunlei He 	return sbi->discard_blks;
1437f83a2584SYunlei He }
1438f83a2584SYunlei He 
143939a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
144039a53e0cSJaegeuk Kim {
144139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
144239a53e0cSJaegeuk Kim 
144339a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
144439a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
144539a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
144639a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
144739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
144839a53e0cSJaegeuk Kim 
144939a53e0cSJaegeuk Kim 	return 0;
145039a53e0cSJaegeuk Kim }
145139a53e0cSJaegeuk Kim 
145255141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
145355141486SWanpeng Li {
145455141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
145555141486SWanpeng Li }
145655141486SWanpeng Li 
145739a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
145839a53e0cSJaegeuk Kim {
145939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
14601dbe4152SChangman Lee 	int offset;
14611dbe4152SChangman Lee 
146255141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
14631dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
14641dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
14651dbe4152SChangman Lee 		else
146665b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
14671dbe4152SChangman Lee 	} else {
14681dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
146925ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
147039a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
147139a53e0cSJaegeuk Kim 	}
14721dbe4152SChangman Lee }
147339a53e0cSJaegeuk Kim 
147439a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
147539a53e0cSJaegeuk Kim {
14768508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
147739a53e0cSJaegeuk Kim 
14788508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
147939a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
148039a53e0cSJaegeuk Kim 	return start_addr;
148139a53e0cSJaegeuk Kim }
148239a53e0cSJaegeuk Kim 
14838508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
14848508e44aSJaegeuk Kim {
14858508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
14868508e44aSJaegeuk Kim 
14878508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
14888508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
14898508e44aSJaegeuk Kim 	return start_addr;
14908508e44aSJaegeuk Kim }
14918508e44aSJaegeuk Kim 
14928508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
14938508e44aSJaegeuk Kim {
14948508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
14958508e44aSJaegeuk Kim }
14968508e44aSJaegeuk Kim 
149739a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
149839a53e0cSJaegeuk Kim {
149939a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
150039a53e0cSJaegeuk Kim }
150139a53e0cSJaegeuk Kim 
150239a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
1503ef86d709SGu Zheng 						struct inode *inode)
150439a53e0cSJaegeuk Kim {
150539a53e0cSJaegeuk Kim 	block_t	valid_block_count;
150639a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
150739a53e0cSJaegeuk Kim 
150839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
150939a53e0cSJaegeuk Kim 
1510ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1511cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
151239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
151339a53e0cSJaegeuk Kim 		return false;
151439a53e0cSJaegeuk Kim 	}
151539a53e0cSJaegeuk Kim 
1516ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1517cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
151839a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
151939a53e0cSJaegeuk Kim 		return false;
152039a53e0cSJaegeuk Kim 	}
152139a53e0cSJaegeuk Kim 
152239a53e0cSJaegeuk Kim 	if (inode)
15238edd03c8SJaegeuk Kim 		f2fs_i_blocks_write(inode, 1, true);
1524ef86d709SGu Zheng 
1525ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1526ef86d709SGu Zheng 	sbi->total_valid_block_count++;
152739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
152839a53e0cSJaegeuk Kim 
152941382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
153039a53e0cSJaegeuk Kim 	return true;
153139a53e0cSJaegeuk Kim }
153239a53e0cSJaegeuk Kim 
153339a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1534ef86d709SGu Zheng 						struct inode *inode)
153539a53e0cSJaegeuk Kim {
153639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
153739a53e0cSJaegeuk Kim 
15389850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
15399850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
15409850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !inode->i_blocks);
154139a53e0cSJaegeuk Kim 
15428edd03c8SJaegeuk Kim 	f2fs_i_blocks_write(inode, 1, false);
1543ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1544ef86d709SGu Zheng 	sbi->total_valid_block_count--;
154539a53e0cSJaegeuk Kim 
154639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
154739a53e0cSJaegeuk Kim }
154839a53e0cSJaegeuk Kim 
154939a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
155039a53e0cSJaegeuk Kim {
15518b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
155239a53e0cSJaegeuk Kim }
155339a53e0cSJaegeuk Kim 
155439a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
155539a53e0cSJaegeuk Kim {
1556513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
155739a53e0cSJaegeuk Kim }
155839a53e0cSJaegeuk Kim 
15590e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
156039a53e0cSJaegeuk Kim {
1561513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
156239a53e0cSJaegeuk Kim }
156339a53e0cSJaegeuk Kim 
1564513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
156539a53e0cSJaegeuk Kim {
1566513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
156739a53e0cSJaegeuk Kim }
156839a53e0cSJaegeuk Kim 
1569a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1570a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1571a56c7c6fSJaegeuk Kim {
1572c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1573c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1574cac5a3d8SDongOh Shin 
1575c41f3cc3SJaegeuk Kim 	if (page)
1576c41f3cc3SJaegeuk Kim 		return page;
1577c41f3cc3SJaegeuk Kim 
157855523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
157955523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1580c41f3cc3SJaegeuk Kim 		return NULL;
158155523519SChao Yu 	}
1582c41f3cc3SJaegeuk Kim #endif
1583a56c7c6fSJaegeuk Kim 	if (!for_write)
1584a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1585a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1586a56c7c6fSJaegeuk Kim }
1587a56c7c6fSJaegeuk Kim 
15886e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
15896e2c64adSJaegeuk Kim {
15906e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
15916e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
15926e2c64adSJaegeuk Kim 
15936e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
15946e2c64adSJaegeuk Kim 	kunmap(dst);
15956e2c64adSJaegeuk Kim 	kunmap(src);
15966e2c64adSJaegeuk Kim }
15976e2c64adSJaegeuk Kim 
159839a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
159939a53e0cSJaegeuk Kim {
1600031fa8ccSJaegeuk Kim 	if (!page)
160139a53e0cSJaegeuk Kim 		return;
160239a53e0cSJaegeuk Kim 
160339a53e0cSJaegeuk Kim 	if (unlock) {
16049850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
160539a53e0cSJaegeuk Kim 		unlock_page(page);
160639a53e0cSJaegeuk Kim 	}
160709cbfeafSKirill A. Shutemov 	put_page(page);
160839a53e0cSJaegeuk Kim }
160939a53e0cSJaegeuk Kim 
161039a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
161139a53e0cSJaegeuk Kim {
161239a53e0cSJaegeuk Kim 	if (dn->node_page)
161339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
161439a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
161539a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
161639a53e0cSJaegeuk Kim 	dn->node_page = NULL;
161739a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
161839a53e0cSJaegeuk Kim }
161939a53e0cSJaegeuk Kim 
162039a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1621e8512d2eSGu Zheng 					size_t size)
162239a53e0cSJaegeuk Kim {
1623e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
162439a53e0cSJaegeuk Kim }
162539a53e0cSJaegeuk Kim 
16267bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
16277bd59381SGu Zheng 						gfp_t flags)
16287bd59381SGu Zheng {
16297bd59381SGu Zheng 	void *entry;
16307bd59381SGu Zheng 
163180c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
163280c54505SJaegeuk Kim 	if (!entry)
163380c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
16347bd59381SGu Zheng 	return entry;
16357bd59381SGu Zheng }
16367bd59381SGu Zheng 
1637740432f8SJaegeuk Kim static inline struct bio *f2fs_bio_alloc(int npages)
1638740432f8SJaegeuk Kim {
1639740432f8SJaegeuk Kim 	struct bio *bio;
1640740432f8SJaegeuk Kim 
1641740432f8SJaegeuk Kim 	/* No failure on bio allocation */
1642740432f8SJaegeuk Kim 	bio = bio_alloc(GFP_NOIO, npages);
164380c54505SJaegeuk Kim 	if (!bio)
164480c54505SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1645740432f8SJaegeuk Kim 	return bio;
1646740432f8SJaegeuk Kim }
1647740432f8SJaegeuk Kim 
16489be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
16499be32d72SJaegeuk Kim 				unsigned long index, void *item)
16509be32d72SJaegeuk Kim {
16519be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
16529be32d72SJaegeuk Kim 		cond_resched();
16539be32d72SJaegeuk Kim }
16549be32d72SJaegeuk Kim 
165539a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
165639a53e0cSJaegeuk Kim 
165739a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
165839a53e0cSJaegeuk Kim {
165945590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
1660cac5a3d8SDongOh Shin 
166139a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
166239a53e0cSJaegeuk Kim }
166339a53e0cSJaegeuk Kim 
166439a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
166539a53e0cSJaegeuk Kim {
166639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
166739a53e0cSJaegeuk Kim }
166839a53e0cSJaegeuk Kim 
166939a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
167039a53e0cSJaegeuk Kim 		unsigned int offset)
167139a53e0cSJaegeuk Kim {
167239a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
167339a53e0cSJaegeuk Kim 	__le32 *addr_array;
1674cac5a3d8SDongOh Shin 
167545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
167639a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
167739a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
167839a53e0cSJaegeuk Kim }
167939a53e0cSJaegeuk Kim 
168039a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
168139a53e0cSJaegeuk Kim {
168239a53e0cSJaegeuk Kim 	int mask;
168339a53e0cSJaegeuk Kim 
168439a53e0cSJaegeuk Kim 	addr += (nr >> 3);
168539a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
168639a53e0cSJaegeuk Kim 	return mask & *addr;
168739a53e0cSJaegeuk Kim }
168839a53e0cSJaegeuk Kim 
1689a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1690a66cdd98SJaegeuk Kim {
1691a66cdd98SJaegeuk Kim 	int mask;
1692a66cdd98SJaegeuk Kim 
1693a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1694a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1695a66cdd98SJaegeuk Kim 	*addr |= mask;
1696a66cdd98SJaegeuk Kim }
1697a66cdd98SJaegeuk Kim 
1698a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1699a66cdd98SJaegeuk Kim {
1700a66cdd98SJaegeuk Kim 	int mask;
1701a66cdd98SJaegeuk Kim 
1702a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1703a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1704a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1705a66cdd98SJaegeuk Kim }
1706a66cdd98SJaegeuk Kim 
170752aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
170839a53e0cSJaegeuk Kim {
170939a53e0cSJaegeuk Kim 	int mask;
171039a53e0cSJaegeuk Kim 	int ret;
171139a53e0cSJaegeuk Kim 
171239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
171339a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
171439a53e0cSJaegeuk Kim 	ret = mask & *addr;
171539a53e0cSJaegeuk Kim 	*addr |= mask;
171639a53e0cSJaegeuk Kim 	return ret;
171739a53e0cSJaegeuk Kim }
171839a53e0cSJaegeuk Kim 
171952aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
172039a53e0cSJaegeuk Kim {
172139a53e0cSJaegeuk Kim 	int mask;
172239a53e0cSJaegeuk Kim 	int ret;
172339a53e0cSJaegeuk Kim 
172439a53e0cSJaegeuk Kim 	addr += (nr >> 3);
172539a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
172639a53e0cSJaegeuk Kim 	ret = mask & *addr;
172739a53e0cSJaegeuk Kim 	*addr &= ~mask;
172839a53e0cSJaegeuk Kim 	return ret;
172939a53e0cSJaegeuk Kim }
173039a53e0cSJaegeuk Kim 
1731c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1732c6ac4c0eSGu Zheng {
1733c6ac4c0eSGu Zheng 	int mask;
1734c6ac4c0eSGu Zheng 
1735c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1736c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1737c6ac4c0eSGu Zheng 	*addr ^= mask;
1738c6ac4c0eSGu Zheng }
1739c6ac4c0eSGu Zheng 
174039a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
174139a53e0cSJaegeuk Kim enum {
174239a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1743b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
174426de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
1745ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
174639a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
174739a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
174839a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1749c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1750c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1751444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
17521001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
175334d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1754fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1755fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
175688b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
175788b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
17585fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
175902a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
17603c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
17611e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
1762b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
1763510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
1764d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
1765c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
1766dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
1767ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
176839a53e0cSJaegeuk Kim };
176939a53e0cSJaegeuk Kim 
1770205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
1771205b9822SJaegeuk Kim 						int flag, bool set)
177239a53e0cSJaegeuk Kim {
1773205b9822SJaegeuk Kim 	switch (flag) {
1774205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
1775205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
1776205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
1777205b9822SJaegeuk Kim 		if (set)
1778205b9822SJaegeuk Kim 			return;
1779205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
1780205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
17817c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
1782205b9822SJaegeuk Kim 	}
178339a53e0cSJaegeuk Kim }
178439a53e0cSJaegeuk Kim 
178591942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
178639a53e0cSJaegeuk Kim {
178791942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
178891942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
1789205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
179039a53e0cSJaegeuk Kim }
179139a53e0cSJaegeuk Kim 
179291942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
179339a53e0cSJaegeuk Kim {
179491942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
179539a53e0cSJaegeuk Kim }
179639a53e0cSJaegeuk Kim 
179791942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
179839a53e0cSJaegeuk Kim {
179991942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
180091942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
1801205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
180239a53e0cSJaegeuk Kim }
180339a53e0cSJaegeuk Kim 
180491942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
1805444c580fSJaegeuk Kim {
180691942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
180791942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
18087c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
180939a53e0cSJaegeuk Kim }
181039a53e0cSJaegeuk Kim 
1811a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
1812a1961246SJaegeuk Kim {
1813a1961246SJaegeuk Kim 	if (inc)
1814a1961246SJaegeuk Kim 		inc_nlink(inode);
1815a1961246SJaegeuk Kim 	else
1816a1961246SJaegeuk Kim 		drop_nlink(inode);
18177c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1818a1961246SJaegeuk Kim }
1819a1961246SJaegeuk Kim 
18208edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
18218edd03c8SJaegeuk Kim 					blkcnt_t diff, bool add)
18228edd03c8SJaegeuk Kim {
182326de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
182426de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
182526de9b11SJaegeuk Kim 
18268edd03c8SJaegeuk Kim 	inode->i_blocks = add ? inode->i_blocks + diff :
18278edd03c8SJaegeuk Kim 				inode->i_blocks - diff;
18287c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
182926de9b11SJaegeuk Kim 	if (clean || recover)
183026de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
18318edd03c8SJaegeuk Kim }
18328edd03c8SJaegeuk Kim 
1833fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
1834fc9581c8SJaegeuk Kim {
183526de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
183626de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
183726de9b11SJaegeuk Kim 
1838fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
1839fc9581c8SJaegeuk Kim 		return;
1840fc9581c8SJaegeuk Kim 
1841fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
18427c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
184326de9b11SJaegeuk Kim 	if (clean || recover)
184426de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
184526de9b11SJaegeuk Kim }
184626de9b11SJaegeuk Kim 
1847205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
1848205b9822SJaegeuk Kim {
1849205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
18507c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1851205b9822SJaegeuk Kim }
1852205b9822SJaegeuk Kim 
1853205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
1854205b9822SJaegeuk Kim {
1855205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
18567c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1857205b9822SJaegeuk Kim }
1858205b9822SJaegeuk Kim 
1859205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
1860205b9822SJaegeuk Kim {
1861205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
18627c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1863205b9822SJaegeuk Kim }
1864205b9822SJaegeuk Kim 
186591942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
1866444c580fSJaegeuk Kim {
1867205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
1868205b9822SJaegeuk Kim 
1869444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
1870205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
18711001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
1872205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
187334d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
1874205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
1875b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
1876205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
1877510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
1878205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
1879444c580fSJaegeuk Kim }
1880444c580fSJaegeuk Kim 
188191942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
1882444c580fSJaegeuk Kim {
1883444c580fSJaegeuk Kim 	ri->i_inline = 0;
1884444c580fSJaegeuk Kim 
188591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
1886444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
188791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
18881001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
188991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
189034d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
189191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
1892b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
189391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
1894510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
1895444c580fSJaegeuk Kim }
1896444c580fSJaegeuk Kim 
1897987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
1898987c7c31SChao Yu {
189991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
1900987c7c31SChao Yu }
1901987c7c31SChao Yu 
190281ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
1903de93653fSJaegeuk Kim {
190481ca7350SChao Yu 	if (f2fs_has_inline_xattr(inode))
1905de93653fSJaegeuk Kim 		return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1906de93653fSJaegeuk Kim 	return DEF_ADDRS_PER_INODE;
1907de93653fSJaegeuk Kim }
1908de93653fSJaegeuk Kim 
190965985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
191065985d93SJaegeuk Kim {
1911695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
1912cac5a3d8SDongOh Shin 
191365985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
191465985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
191565985d93SJaegeuk Kim }
191665985d93SJaegeuk Kim 
191765985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
191865985d93SJaegeuk Kim {
1919987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
192065985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
192165985d93SJaegeuk Kim 	else
192265985d93SJaegeuk Kim 		return 0;
192365985d93SJaegeuk Kim }
192465985d93SJaegeuk Kim 
19250dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
19260dbdc2aeSJaegeuk Kim {
192791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
19280dbdc2aeSJaegeuk Kim }
19290dbdc2aeSJaegeuk Kim 
1930b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
1931b3d208f9SJaegeuk Kim {
193291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
1933b3d208f9SJaegeuk Kim }
1934b3d208f9SJaegeuk Kim 
1935510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
1936510022a8SJaegeuk Kim {
193791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
1938510022a8SJaegeuk Kim }
1939510022a8SJaegeuk Kim 
194088b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
194188b88a66SJaegeuk Kim {
194291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
194388b88a66SJaegeuk Kim }
194488b88a66SJaegeuk Kim 
19455fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
19465fe45743SChao Yu {
19475fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
19485fe45743SChao Yu }
19495fe45743SChao Yu 
195002a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
195102a1335fSJaegeuk Kim {
195291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
195302a1335fSJaegeuk Kim }
195402a1335fSJaegeuk Kim 
19553c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
19563c6c2bebSJaegeuk Kim {
195791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
19583c6c2bebSJaegeuk Kim }
19593c6c2bebSJaegeuk Kim 
19601e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
19611e84371fSJaegeuk Kim {
196291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
19631e84371fSJaegeuk Kim }
19641e84371fSJaegeuk Kim 
19651001b347SHuajun Li static inline void *inline_data_addr(struct page *page)
19661001b347SHuajun Li {
1967695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
1968cac5a3d8SDongOh Shin 
19691001b347SHuajun Li 	return (void *)&(ri->i_addr[1]);
19701001b347SHuajun Li }
19711001b347SHuajun Li 
197234d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
197334d67debSChao Yu {
197491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
197534d67debSChao Yu }
197634d67debSChao Yu 
19779486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
19789486ba44SJaegeuk Kim {
19799486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
19809486ba44SJaegeuk Kim 		kunmap(page);
19819486ba44SJaegeuk Kim }
19829486ba44SJaegeuk Kim 
1983b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
1984b5492af7SJaegeuk Kim {
1985b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
1986b5492af7SJaegeuk Kim }
1987b5492af7SJaegeuk Kim 
1988b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
1989b5492af7SJaegeuk Kim {
1990b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
19917c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1992b5492af7SJaegeuk Kim }
1993b5492af7SJaegeuk Kim 
1994b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
1995b5492af7SJaegeuk Kim {
1996b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
19977c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1998b5492af7SJaegeuk Kim }
1999b5492af7SJaegeuk Kim 
200026787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
200126787236SJaegeuk Kim {
200226787236SJaegeuk Kim 	if (dsync) {
200326787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
200426787236SJaegeuk Kim 		bool ret;
200526787236SJaegeuk Kim 
200626787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
200726787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
200826787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
200926787236SJaegeuk Kim 		return ret;
201026787236SJaegeuk Kim 	}
201126787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
201226787236SJaegeuk Kim 			file_keep_isize(inode) ||
201326787236SJaegeuk Kim 			i_size_read(inode) & PAGE_MASK)
201426787236SJaegeuk Kim 		return false;
201526787236SJaegeuk Kim 	return F2FS_I(inode)->last_disk_size == i_size_read(inode);
2016267378d4SChao Yu }
2017267378d4SChao Yu 
2018267378d4SChao Yu static inline int f2fs_readonly(struct super_block *sb)
2019267378d4SChao Yu {
2020267378d4SChao Yu 	return sb->s_flags & MS_RDONLY;
2021267378d4SChao Yu }
202239a53e0cSJaegeuk Kim 
202339a53e0cSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
202439a53e0cSJaegeuk Kim {
2025aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
202639a53e0cSJaegeuk Kim }
202739a53e0cSJaegeuk Kim 
2028eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2029eaa693f4SJaegeuk Kim {
2030eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2031eaa693f4SJaegeuk Kim 		return true;
2032eaa693f4SJaegeuk Kim 
2033eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2034eaa693f4SJaegeuk Kim 		return true;
2035eaa693f4SJaegeuk Kim 
2036eaa693f4SJaegeuk Kim 	return false;
2037eaa693f4SJaegeuk Kim }
2038eaa693f4SJaegeuk Kim 
20393e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
20403e72f721SJaegeuk Kim {
20413e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
204291942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
20433e72f721SJaegeuk Kim 		return false;
20443e72f721SJaegeuk Kim 
2045886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
20463e72f721SJaegeuk Kim }
20473e72f721SJaegeuk Kim 
20481ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
20491ecc0c5cSChao Yu 					size_t size, gfp_t flags)
20500414b004SJaegeuk Kim {
20512c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
205255523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
205355523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
20542c63feadSJaegeuk Kim 		return NULL;
205555523519SChao Yu 	}
20562c63feadSJaegeuk Kim #endif
20570414b004SJaegeuk Kim 	return kmalloc(size, flags);
20580414b004SJaegeuk Kim }
20590414b004SJaegeuk Kim 
206039307a8eSJaegeuk Kim static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
206139307a8eSJaegeuk Kim {
206239307a8eSJaegeuk Kim 	void *ret;
206339307a8eSJaegeuk Kim 
206439307a8eSJaegeuk Kim 	ret = kmalloc(size, flags | __GFP_NOWARN);
206539307a8eSJaegeuk Kim 	if (!ret)
206639307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags, PAGE_KERNEL);
206739307a8eSJaegeuk Kim 	return ret;
206839307a8eSJaegeuk Kim }
206939307a8eSJaegeuk Kim 
207039307a8eSJaegeuk Kim static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
207139307a8eSJaegeuk Kim {
207239307a8eSJaegeuk Kim 	void *ret;
207339307a8eSJaegeuk Kim 
207439307a8eSJaegeuk Kim 	ret = kzalloc(size, flags | __GFP_NOWARN);
207539307a8eSJaegeuk Kim 	if (!ret)
207639307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
207739307a8eSJaegeuk Kim 	return ret;
207839307a8eSJaegeuk Kim }
207939307a8eSJaegeuk Kim 
208039a53e0cSJaegeuk Kim #define get_inode_mode(i) \
208191942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
208239a53e0cSJaegeuk Kim 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
208339a53e0cSJaegeuk Kim 
208439a53e0cSJaegeuk Kim /*
20851e1bb4baSJaegeuk Kim  * file.c
208639a53e0cSJaegeuk Kim  */
2087cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2088cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2089cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2090cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2091a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2092a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2093cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2094cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2095cac5a3d8SDongOh Shin int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2096cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2097cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
209839a53e0cSJaegeuk Kim 
209939a53e0cSJaegeuk Kim /*
210039a53e0cSJaegeuk Kim  * inode.c
210139a53e0cSJaegeuk Kim  */
2102cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2103cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2104cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2105cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2106cac5a3d8SDongOh Shin int update_inode(struct inode *inode, struct page *node_page);
2107cac5a3d8SDongOh Shin int update_inode_page(struct inode *inode);
2108cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2109cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2110cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
211139a53e0cSJaegeuk Kim 
211239a53e0cSJaegeuk Kim /*
211339a53e0cSJaegeuk Kim  * namei.c
211439a53e0cSJaegeuk Kim  */
211539a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
211639a53e0cSJaegeuk Kim 
211739a53e0cSJaegeuk Kim /*
211839a53e0cSJaegeuk Kim  * dir.c
211939a53e0cSJaegeuk Kim  */
2120cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2121cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2122cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2123cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2124cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2125cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2126cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2127cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2128cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2129cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2130cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2131cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2132cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2133cac5a3d8SDongOh Shin 			unsigned int current_depth);
2134cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2135cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2136cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2137cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2138cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2139cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2140cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2141cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2142cac5a3d8SDongOh Shin 			struct page **page);
2143cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2144cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2145cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2146cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2147cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2148cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2149cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2150cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2151cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2152cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2153cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2154cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2155cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2156cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2157cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2158cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
215939a53e0cSJaegeuk Kim 
2160b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2161b7f7a5e0SAl Viro {
21622b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2163510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2164b7f7a5e0SAl Viro }
2165b7f7a5e0SAl Viro 
216639a53e0cSJaegeuk Kim /*
216739a53e0cSJaegeuk Kim  * super.c
216839a53e0cSJaegeuk Kim  */
2169cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2170cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2171cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2172cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2173a07ef784SNamjae Jeon extern __printf(3, 4)
2174cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2175984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
217639a53e0cSJaegeuk Kim 
217739a53e0cSJaegeuk Kim /*
217839a53e0cSJaegeuk Kim  * hash.c
217939a53e0cSJaegeuk Kim  */
2180cac5a3d8SDongOh Shin f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info);
218139a53e0cSJaegeuk Kim 
218239a53e0cSJaegeuk Kim /*
218339a53e0cSJaegeuk Kim  * node.c
218439a53e0cSJaegeuk Kim  */
218539a53e0cSJaegeuk Kim struct dnode_of_data;
218639a53e0cSJaegeuk Kim struct node_info;
218739a53e0cSJaegeuk Kim 
2188cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2189cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2190cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2191cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2192cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2193cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2194cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2195cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2196cac5a3d8SDongOh Shin int truncate_xattr_node(struct inode *inode, struct page *page);
2197cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2198cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2199cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
2200cac5a3d8SDongOh Shin struct page *new_node_page(struct dnode_of_data *dn,
2201cac5a3d8SDongOh Shin 			unsigned int ofs, struct page *ipage);
2202cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2203cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2204cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2205cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2206cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2207cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2208cac5a3d8SDongOh Shin int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc);
220922ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2210cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2211cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2212cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2213cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2214cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2215d260081cSChao Yu int recover_xattr_data(struct inode *inode, struct page *page,
2216cac5a3d8SDongOh Shin 			block_t blkaddr);
2217cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2218cac5a3d8SDongOh Shin int restore_node_summary(struct f2fs_sb_info *sbi,
2219cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
222022ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2221cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2222cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
22236e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
222439a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
222539a53e0cSJaegeuk Kim 
222639a53e0cSJaegeuk Kim /*
222739a53e0cSJaegeuk Kim  * segment.c
222839a53e0cSJaegeuk Kim  */
2229cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
2230cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
22318c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2232cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2233cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2234cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2235cac5a3d8SDongOh Shin int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2236cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2237cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2238cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2239cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2240cac5a3d8SDongOh Shin void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
2241d431413fSChao Yu void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2242cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2243cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2244cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2245cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2246cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2247cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2248cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2249cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2250cac5a3d8SDongOh Shin void write_meta_page(struct f2fs_sb_info *sbi, struct page *page);
2251cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2252cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2253d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2254cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2255cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2256cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2257cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2258cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2259cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2260cac5a3d8SDongOh Shin 			bool recover_newaddr);
2261cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2262cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2263cac5a3d8SDongOh Shin 			struct f2fs_summary *sum, int type);
2264cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2265cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2266cac5a3d8SDongOh Shin void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2267cac5a3d8SDongOh Shin 			block_t blkaddr);
2268cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2269cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2270cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2271cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2272cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2273cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2274cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
22757fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
22767fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
227739a53e0cSJaegeuk Kim 
227839a53e0cSJaegeuk Kim /*
227939a53e0cSJaegeuk Kim  * checkpoint.c
228039a53e0cSJaegeuk Kim  */
2281cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2282cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2283cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2284cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2285cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2286cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2287cac5a3d8SDongOh Shin 			int type, bool sync);
2288cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2289cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2290cac5a3d8SDongOh Shin 			long nr_to_write);
2291cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2292cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2293cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2294cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2295cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2296cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2297cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2298cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2299cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2300cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2301cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2302cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2303cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2304cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2305cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2306cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
23076e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
230839a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
230939a53e0cSJaegeuk Kim 
231039a53e0cSJaegeuk Kim /*
231139a53e0cSJaegeuk Kim  * data.c
231239a53e0cSJaegeuk Kim  */
2313cac5a3d8SDongOh Shin void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
2314cac5a3d8SDongOh Shin 			int rw);
2315cac5a3d8SDongOh Shin void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
2316942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2317942fd319SJaegeuk Kim 				enum page_type type, int rw);
2318cac5a3d8SDongOh Shin void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi);
2319cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2320cac5a3d8SDongOh Shin int f2fs_submit_page_mbio(struct f2fs_io_info *fio);
2321cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2322cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2323cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2324cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2325cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2326cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2327cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2328cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2329cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2330cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2331cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2332cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2333cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2334cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2335cac5a3d8SDongOh Shin 			bool for_write);
2336cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2337cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2338cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2339cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2340cac5a3d8SDongOh Shin 			int create, int flag);
2341cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2342cac5a3d8SDongOh Shin 			u64 start, u64 len);
2343cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2344cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2345cac5a3d8SDongOh Shin 			unsigned int length);
2346cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
23475b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2348cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2349cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
23505b7a487cSWeichao Guo #endif
235139a53e0cSJaegeuk Kim 
235239a53e0cSJaegeuk Kim /*
235339a53e0cSJaegeuk Kim  * gc.c
235439a53e0cSJaegeuk Kim  */
2355cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2356cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2357cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2358cac5a3d8SDongOh Shin int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background);
2359cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
236039a53e0cSJaegeuk Kim 
236139a53e0cSJaegeuk Kim /*
236239a53e0cSJaegeuk Kim  * recovery.c
236339a53e0cSJaegeuk Kim  */
2364cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2365cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
236639a53e0cSJaegeuk Kim 
236739a53e0cSJaegeuk Kim /*
236839a53e0cSJaegeuk Kim  * debug.c
236939a53e0cSJaegeuk Kim  */
237039a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
237139a53e0cSJaegeuk Kim struct f2fs_stat_info {
237239a53e0cSJaegeuk Kim 	struct list_head stat_list;
237339a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
237439a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
237539a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
23765b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
23775b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2378c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
237935782b23SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
238035782b23SJaegeuk Kim 	int inmem_pages;
23810f18b462SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, ndirty_all;
2382b8559dc2SChao Yu 	int nats, dirty_nats, sits, dirty_sits, free_nids, alloc_nids;
238339a53e0cSJaegeuk Kim 	int total_count, utilization;
23848b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
23858b8dd65fSChao Yu 	int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
23865f32366aSChao Yu 	int nr_discard_cmd;
2387a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2388648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2389f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
239039a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
239139a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
239239a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
239339a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
239442190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
239539a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2396e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
239739a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2398e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
239939a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
240039a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
240139a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
240239a53e0cSJaegeuk Kim 
240339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
240439a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2405b9a2c252SChangman Lee 	unsigned int inplace_count;
24069edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
240739a53e0cSJaegeuk Kim };
240839a53e0cSJaegeuk Kim 
2409963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2410963d4f7dSGu Zheng {
2411963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2412963d4f7dSGu Zheng }
2413963d4f7dSGu Zheng 
2414942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
241542190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
241639a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2417dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
241833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
241933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
24205b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
24215b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
24225b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
24235b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2424d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2425d5e8f6c9SChao Yu 	do {								\
2426d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2427d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2428d5e8f6c9SChao Yu 	} while (0)
2429d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2430d5e8f6c9SChao Yu 	do {								\
2431d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2432d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2433d5e8f6c9SChao Yu 	} while (0)
24340dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
24350dbdc2aeSJaegeuk Kim 	do {								\
24360dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
243703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
24380dbdc2aeSJaegeuk Kim 	} while (0)
24390dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
24400dbdc2aeSJaegeuk Kim 	do {								\
24410dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
244203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
24430dbdc2aeSJaegeuk Kim 	} while (0)
24443289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
24453289c061SJaegeuk Kim 	do {								\
24463289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
244703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
24483289c061SJaegeuk Kim 	} while (0)
24493289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
24503289c061SJaegeuk Kim 	do {								\
24513289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
245203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
24533289c061SJaegeuk Kim 	} while (0)
2454dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2455dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2456dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2457dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2458b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2459b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
246026a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
2461cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
246226a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
2463cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
246426a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
246526a28a0cSJaegeuk Kim 	do {								\
246626a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
246726a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
246826a28a0cSJaegeuk Kim 		if (cur > max)						\
246926a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
247026a28a0cSJaegeuk Kim 	} while (0)
2471648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
2472648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2473648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
2474648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2475648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
2476648d50baSChao Yu 	do {								\
2477648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
2478648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
2479648d50baSChao Yu 		if (cur > max)						\
2480648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
2481648d50baSChao Yu 	} while (0)
2482e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
248339a53e0cSJaegeuk Kim 	do {								\
2484963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
248568afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
248668afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
248739a53e0cSJaegeuk Kim 			si->data_segs++;				\
2488e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2489e1235983SChangman Lee 		} else {						\
249039a53e0cSJaegeuk Kim 			si->node_segs++;				\
2491e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2492e1235983SChangman Lee 		}							\
249339a53e0cSJaegeuk Kim 	} while (0)
249439a53e0cSJaegeuk Kim 
249539a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
249668afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
249739a53e0cSJaegeuk Kim 
2498e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
249939a53e0cSJaegeuk Kim 	do {								\
2500963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
250139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
250239a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
250368afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
250439a53e0cSJaegeuk Kim 	} while (0)
250539a53e0cSJaegeuk Kim 
2506e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
250739a53e0cSJaegeuk Kim 	do {								\
2508963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
250939a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
251039a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
251168afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
251239a53e0cSJaegeuk Kim 	} while (0)
251339a53e0cSJaegeuk Kim 
2514cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
2515cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
2516787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
25174589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
251839a53e0cSJaegeuk Kim #else
2519942e0be6SChangman Lee #define stat_inc_cp_count(si)
252042190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)
252139a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
2522dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(si)
252333fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)
252433fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)
2525dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)
2526029e13ccSChao Yu #define stat_inc_rbtree_node_hit(sb)
252791c481ffSChao Yu #define stat_inc_largest_node_hit(sbi)
252891c481ffSChao Yu #define stat_inc_cached_node_hit(sbi)
2529d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)
2530d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)
25310dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)
25320dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)
25333289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)
25343289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)
253526a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)
253626a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)
253726a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)
2538648d50baSChao Yu #define stat_inc_volatile_write(inode)
2539648d50baSChao Yu #define stat_dec_volatile_write(inode)
2540648d50baSChao Yu #define stat_update_max_volatile_write(inode)
2541dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)
2542dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)
2543b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)
2544e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)
254539a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
2546e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)
2547e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)
254839a53e0cSJaegeuk Kim 
254939a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
255039a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2551787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
25524589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
255339a53e0cSJaegeuk Kim #endif
255439a53e0cSJaegeuk Kim 
255539a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
255639a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
255739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
255839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
255939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
256039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
256139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
256239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
2563cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
256439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
256529e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
25661001b347SHuajun Li 
2567e18c65b2SHuajun Li /*
2568e18c65b2SHuajun Li  * inline.c
2569e18c65b2SHuajun Li  */
2570cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
2571cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
2572cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
2573bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2574cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
2575cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2576cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
2577cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
2578cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
2579cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2580cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2581cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2582cac5a3d8SDongOh Shin 			struct page *ipage);
2583cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2584cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2585cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2586cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2587cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2588cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
2589cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2590cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
2591cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
2592cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
2593cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
2594cde4de12SJaegeuk Kim 
2595cde4de12SJaegeuk Kim /*
25962658e50dSJaegeuk Kim  * shrinker.c
25972658e50dSJaegeuk Kim  */
2598cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
2599cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2600cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2601cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2602cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2603cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
26042658e50dSJaegeuk Kim 
26052658e50dSJaegeuk Kim /*
2606a28ef1f5SChao Yu  * extent_cache.c
2607a28ef1f5SChao Yu  */
2608*004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2609*004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
2610*004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2611*004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
2612*004b6862SChao Yu 				unsigned int ofs);
2613*004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2614*004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
2615*004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
2616*004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
2617*004b6862SChao Yu 		bool force);
2618cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2619cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2620cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
2621cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
2622cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
2623cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2624cac5a3d8SDongOh Shin 			struct extent_info *ei);
2625cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
262619b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
2627cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
2628cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
2629a28ef1f5SChao Yu int __init create_extent_cache(void);
2630a28ef1f5SChao Yu void destroy_extent_cache(void);
2631a28ef1f5SChao Yu 
2632a28ef1f5SChao Yu /*
2633cde4de12SJaegeuk Kim  * crypto support
2634cde4de12SJaegeuk Kim  */
26350b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
2636cde4de12SJaegeuk Kim {
2637cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
2638cde4de12SJaegeuk Kim }
2639cde4de12SJaegeuk Kim 
2640cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
2641cde4de12SJaegeuk Kim {
2642cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2643cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2644cde4de12SJaegeuk Kim #endif
2645cde4de12SJaegeuk Kim }
2646cde4de12SJaegeuk Kim 
2647cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2648cde4de12SJaegeuk Kim {
26490b81d077SJaegeuk Kim 	return bio->bi_private != NULL;
2650cde4de12SJaegeuk Kim }
2651cde4de12SJaegeuk Kim 
2652cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2653cde4de12SJaegeuk Kim {
2654cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2655cde4de12SJaegeuk Kim }
2656f424f664SJaegeuk Kim 
26570bfd7a09SDamien Le Moal static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
265852763a4bSJaegeuk Kim {
26590bfd7a09SDamien Le Moal 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
266052763a4bSJaegeuk Kim }
266152763a4bSJaegeuk Kim 
2662178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
2663178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
26643c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
2665178053e2SDamien Le Moal {
2666178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
26673c62be17SJaegeuk Kim 	int i;
2668178053e2SDamien Le Moal 
26693c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
26703c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
26713c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
26723c62be17SJaegeuk Kim 	return -EINVAL;
2673178053e2SDamien Le Moal }
2674178053e2SDamien Le Moal #endif
2675178053e2SDamien Le Moal 
267696ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
267796ba2decSDamien Le Moal {
267896ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
267996ba2decSDamien Le Moal 
268096ba2decSDamien Le Moal 	return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
268152763a4bSJaegeuk Kim }
268252763a4bSJaegeuk Kim 
268352763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
268452763a4bSJaegeuk Kim {
268552763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
268652763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
268752763a4bSJaegeuk Kim 
268852763a4bSJaegeuk Kim 	switch (mt) {
268952763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
269052763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
269152763a4bSJaegeuk Kim 		break;
269252763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
269352763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
269452763a4bSJaegeuk Kim 		break;
269552763a4bSJaegeuk Kim 	}
269652763a4bSJaegeuk Kim }
269752763a4bSJaegeuk Kim 
2698fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
2699fcc85a4dSJaegeuk Kim {
2700fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2701886f56f9SAl Viro 	umode_t mode = inode->i_mode;
2702fcc85a4dSJaegeuk Kim 
2703fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2704fcc85a4dSJaegeuk Kim #else
2705fcc85a4dSJaegeuk Kim 	return 0;
2706fcc85a4dSJaegeuk Kim #endif
2707fcc85a4dSJaegeuk Kim }
2708fcc85a4dSJaegeuk Kim 
270939a53e0cSJaegeuk Kim #endif
2710