xref: /openbmc/linux/fs/f2fs/f2fs.h (revision e066b83c9b40f3a6951fb693ef0943fa1dfc40c2)
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 
191ba48a33eSChao Yu /* max discard pend list number */
192ba48a33eSChao Yu #define MAX_PLIST_NUM		512
193ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
194ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
195ba48a33eSChao Yu 
19615469963SJaegeuk Kim enum {
19715469963SJaegeuk Kim 	D_PREP,
19815469963SJaegeuk Kim 	D_SUBMIT,
19915469963SJaegeuk Kim 	D_DONE,
20015469963SJaegeuk Kim };
20115469963SJaegeuk Kim 
202004b6862SChao Yu struct discard_info {
203004b6862SChao Yu 	block_t lstart;			/* logical start address */
204004b6862SChao Yu 	block_t len;			/* length */
205004b6862SChao Yu 	block_t start;			/* actual start address in dev */
206004b6862SChao Yu };
207004b6862SChao Yu 
208b01a9201SJaegeuk Kim struct discard_cmd {
209004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
210004b6862SChao Yu 	union {
211004b6862SChao Yu 		struct {
212004b6862SChao Yu 			block_t lstart;	/* logical start address */
213004b6862SChao Yu 			block_t len;	/* length */
214004b6862SChao Yu 			block_t start;	/* actual start address in dev */
215004b6862SChao Yu 		};
216004b6862SChao Yu 		struct discard_info di;	/* discard info */
217004b6862SChao Yu 
218004b6862SChao Yu 	};
219b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
220b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
221c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
22215469963SJaegeuk Kim 	int state;			/* state */
223c81abe34SJaegeuk Kim 	int error;			/* bio error */
224275b66b0SChao Yu };
225275b66b0SChao Yu 
2260b54fb84SJaegeuk Kim struct discard_cmd_control {
22715469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
22846f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
229ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
23046f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
23115469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
23215469963SJaegeuk Kim 	struct mutex cmd_lock;
23346f84c2cSChao Yu 	int nr_discards;			/* # of discards in the list */
2340b54fb84SJaegeuk Kim 	int max_discards;			/* max. discards to be issued */
235d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
2368b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
2378b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
2385f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
239004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
24039a53e0cSJaegeuk Kim };
24139a53e0cSJaegeuk Kim 
24239a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
24339a53e0cSJaegeuk Kim struct fsync_inode_entry {
24439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
24539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
246c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
247c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
24839a53e0cSJaegeuk Kim };
24939a53e0cSJaegeuk Kim 
25068afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
25168afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
25239a53e0cSJaegeuk Kim 
25368afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
25468afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
25568afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
25668afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
25739a53e0cSJaegeuk Kim 
258dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
259dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
260309cc2b6SJaegeuk Kim 
261dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
26239a53e0cSJaegeuk Kim {
263dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
264cac5a3d8SDongOh Shin 
265dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
26639a53e0cSJaegeuk Kim 	return before;
26739a53e0cSJaegeuk Kim }
26839a53e0cSJaegeuk Kim 
269dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
27039a53e0cSJaegeuk Kim {
271dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
272cac5a3d8SDongOh Shin 
273dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
27439a53e0cSJaegeuk Kim 	return before;
27539a53e0cSJaegeuk Kim }
27639a53e0cSJaegeuk Kim 
277dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
278dfc08a12SChao Yu 							int size, int type)
279184a5cd2SChao Yu {
280184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
281dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
282dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
283184a5cd2SChao Yu }
284184a5cd2SChao Yu 
28539a53e0cSJaegeuk Kim /*
286e9750824SNamjae Jeon  * ioctl commands
287e9750824SNamjae Jeon  */
288e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
289e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
290d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
291e9750824SNamjae Jeon 
29288b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
29388b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
29488b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
29502a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
2961e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
2971e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
298c1c1b583SChao Yu #define F2FS_IOC_GARBAGE_COLLECT	_IO(F2FS_IOCTL_MAGIC, 6)
299456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
300d323d005SChao Yu #define F2FS_IOC_DEFRAGMENT		_IO(F2FS_IOCTL_MAGIC, 8)
3014dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3024dd6f977SJaegeuk Kim 						struct f2fs_move_range)
303*e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
304*e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
30588b88a66SJaegeuk Kim 
3060b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3070b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3080b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
309f424f664SJaegeuk Kim 
3101abff93dSJaegeuk Kim /*
3111abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3121abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3131abff93dSJaegeuk Kim  */
3141abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3151abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3161abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3171abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
318c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3191abff93dSJaegeuk Kim 
320e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
321e9750824SNamjae Jeon /*
322e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
323e9750824SNamjae Jeon  */
324e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
325e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
32604ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
327e9750824SNamjae Jeon #endif
328e9750824SNamjae Jeon 
329d323d005SChao Yu struct f2fs_defragment {
330d323d005SChao Yu 	u64 start;
331d323d005SChao Yu 	u64 len;
332d323d005SChao Yu };
333d323d005SChao Yu 
3344dd6f977SJaegeuk Kim struct f2fs_move_range {
3354dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
3364dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
3374dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
3384dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
3394dd6f977SJaegeuk Kim };
3404dd6f977SJaegeuk Kim 
341*e066b83cSJaegeuk Kim struct f2fs_flush_device {
342*e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
343*e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
344*e066b83cSJaegeuk Kim };
345*e066b83cSJaegeuk Kim 
346e9750824SNamjae Jeon /*
34739a53e0cSJaegeuk Kim  * For INODE and NODE manager
34839a53e0cSJaegeuk Kim  */
3497b3cd7d6SJaegeuk Kim /* for directory operations */
3507b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
351d8c6822aSJaegeuk Kim 	struct inode *inode;
3527b3cd7d6SJaegeuk Kim 	const void *bitmap;
3537b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
3547b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
3557b3cd7d6SJaegeuk Kim 	int max;
3567b3cd7d6SJaegeuk Kim };
3577b3cd7d6SJaegeuk Kim 
35864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
35964c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
3607b3cd7d6SJaegeuk Kim {
361d8c6822aSJaegeuk Kim 	d->inode = inode;
3627b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
3637b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
3647b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
3657b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
36664c24ecbSTomohiro Kusumi }
367cac5a3d8SDongOh Shin 
36864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
36964c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_inline_dentry *t)
37064c24ecbSTomohiro Kusumi {
37164c24ecbSTomohiro Kusumi 	d->inode = inode;
3727b3cd7d6SJaegeuk Kim 	d->max = NR_INLINE_DENTRY;
3737b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
3747b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
3757b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
3767b3cd7d6SJaegeuk Kim }
3777b3cd7d6SJaegeuk Kim 
378dbe6a5ffSJaegeuk Kim /*
379dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
380dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
381dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
38239a53e0cSJaegeuk Kim  */
383dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
384dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
385266e97a8SJaegeuk Kim enum {
386266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
387266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
388266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
389266e97a8SJaegeuk Kim 					 * look up a node with readahead called
3904f4124d0SChao Yu 					 * by get_data_block.
39139a53e0cSJaegeuk Kim 					 */
392266e97a8SJaegeuk Kim };
393266e97a8SJaegeuk Kim 
394a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
39539a53e0cSJaegeuk Kim 
396817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
397817202d9SChao Yu 
39813054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
39913054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
40013054c54SChao Yu 
40139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
40213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
40313054c54SChao Yu 
40413054c54SChao Yu /* number of extent info in extent cache we try to shrink */
40513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
406c11abd1aSJaegeuk Kim 
40754c2258cSChao Yu struct rb_entry {
40854c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
40954c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
41054c2258cSChao Yu 	unsigned int len;		/* length of the entry */
41154c2258cSChao Yu };
41254c2258cSChao Yu 
41339a53e0cSJaegeuk Kim struct extent_info {
41439a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
415111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
41654c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
41739a53e0cSJaegeuk Kim };
41839a53e0cSJaegeuk Kim 
41913054c54SChao Yu struct extent_node {
42054c2258cSChao Yu 	struct rb_node rb_node;
42154c2258cSChao Yu 	union {
42254c2258cSChao Yu 		struct {
42354c2258cSChao Yu 			unsigned int fofs;
42454c2258cSChao Yu 			unsigned int len;
42554c2258cSChao Yu 			u32 blk;
42654c2258cSChao Yu 		};
42713054c54SChao Yu 		struct extent_info ei;	/* extent info */
42854c2258cSChao Yu 
42954c2258cSChao Yu 	};
43054c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
431201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
43213054c54SChao Yu };
43313054c54SChao Yu 
43413054c54SChao Yu struct extent_tree {
43513054c54SChao Yu 	nid_t ino;			/* inode number */
43613054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
43762c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
4383e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
439137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
44013054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
44168e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
44213054c54SChao Yu };
44313054c54SChao Yu 
44439a53e0cSJaegeuk Kim /*
445003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
446003a3e1dSJaegeuk Kim  *
447003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
448003a3e1dSJaegeuk Kim  */
449003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
450003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
4517f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
4527f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
4537f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
454003a3e1dSJaegeuk Kim 
455003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
456003a3e1dSJaegeuk Kim 	block_t m_pblk;
457003a3e1dSJaegeuk Kim 	block_t m_lblk;
458003a3e1dSJaegeuk Kim 	unsigned int m_len;
459003a3e1dSJaegeuk Kim 	unsigned int m_flags;
460da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
461003a3e1dSJaegeuk Kim };
462003a3e1dSJaegeuk Kim 
463e2b4e2bcSChao Yu /* for flag in get_data_block */
464e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_READ		0
465e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_DIO		1
466e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_FIEMAP		2
467e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_BMAP		3
468b439b103SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_DIO		4
46924b84912SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_AIO		5
470e2b4e2bcSChao Yu 
471003a3e1dSJaegeuk Kim /*
47239a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
47339a53e0cSJaegeuk Kim  */
47439a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
475354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
476cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
477e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
47826787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
47939a53e0cSJaegeuk Kim 
480b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
481b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
482b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
483b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
484b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
485b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
486cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
487cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
488cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
489e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
490e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
49126787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
49226787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
493cde4de12SJaegeuk Kim 
494ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
495ab9fa662SJaegeuk Kim 
49639a53e0cSJaegeuk Kim struct f2fs_inode_info {
49739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
49839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
49939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
50038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
50139a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
5026666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
50339a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
50439a53e0cSJaegeuk Kim 
50539a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
50639a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
507d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
508204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
50939a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
51039a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
51188c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
51239a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
51326de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
51488b88a66SJaegeuk Kim 
5150f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
5160f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
51788b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
51888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
5193e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
52082e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
52139a53e0cSJaegeuk Kim };
52239a53e0cSJaegeuk Kim 
52339a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
524bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
52539a53e0cSJaegeuk Kim {
526bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
527bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
528bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
52939a53e0cSJaegeuk Kim }
53039a53e0cSJaegeuk Kim 
53139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
53239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
53339a53e0cSJaegeuk Kim {
53439a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
5354d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
53639a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
53739a53e0cSJaegeuk Kim }
53839a53e0cSJaegeuk Kim 
539429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
540429511cdSChao Yu 						u32 blk, unsigned int len)
541429511cdSChao Yu {
542429511cdSChao Yu 	ei->fofs = fofs;
543429511cdSChao Yu 	ei->blk = blk;
544429511cdSChao Yu 	ei->len = len;
545429511cdSChao Yu }
546429511cdSChao Yu 
547004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
548004b6862SChao Yu 						struct discard_info *front)
549004b6862SChao Yu {
550004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
551004b6862SChao Yu }
552004b6862SChao Yu 
553004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
554004b6862SChao Yu 						struct discard_info *back)
555004b6862SChao Yu {
556004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
557004b6862SChao Yu }
558004b6862SChao Yu 
559004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
560004b6862SChao Yu 						struct discard_info *front)
561004b6862SChao Yu {
562004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
563004b6862SChao Yu }
564004b6862SChao Yu 
565429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
566429511cdSChao Yu 						struct extent_info *front)
567429511cdSChao Yu {
568429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
569429511cdSChao Yu 			back->blk + back->len == front->blk);
570429511cdSChao Yu }
571429511cdSChao Yu 
572429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
573429511cdSChao Yu 						struct extent_info *back)
574429511cdSChao Yu {
575429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
576429511cdSChao Yu }
577429511cdSChao Yu 
578429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
579429511cdSChao Yu 						struct extent_info *front)
580429511cdSChao Yu {
581429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
582429511cdSChao Yu }
583429511cdSChao Yu 
584cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
585205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
586205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
5874abd3f5aSChao Yu {
588205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
5894abd3f5aSChao Yu 		et->largest = en->ei;
5907c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
591205b9822SJaegeuk Kim 	}
5924abd3f5aSChao Yu }
5934abd3f5aSChao Yu 
594b8559dc2SChao Yu enum nid_list {
595b8559dc2SChao Yu 	FREE_NID_LIST,
596b8559dc2SChao Yu 	ALLOC_NID_LIST,
597b8559dc2SChao Yu 	MAX_NID_LIST,
598b8559dc2SChao Yu };
599b8559dc2SChao Yu 
60039a53e0cSJaegeuk Kim struct f2fs_nm_info {
60139a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
60239a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
60304d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
60439a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
605cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
606ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
6072304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
60839a53e0cSJaegeuk Kim 
60939a53e0cSJaegeuk Kim 	/* NAT cache management */
61039a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
611309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
612b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
61339a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
614309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
615aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
61622ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
61739a53e0cSJaegeuk Kim 
61839a53e0cSJaegeuk Kim 	/* free node ids management */
6198a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
620b8559dc2SChao Yu 	struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
621b8559dc2SChao Yu 	unsigned int nid_cnt[MAX_NID_LIST];	/* the number of free node id */
622b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
62339a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
6244ac91242SChao Yu 	unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
6254ac91242SChao Yu 	unsigned char *nat_block_bitmap;
626586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
62739a53e0cSJaegeuk Kim 
62839a53e0cSJaegeuk Kim 	/* for checkpoint */
62939a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
63022ad0b6aSJaegeuk Kim 
63122ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
63222ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
63322ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
63422ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
635599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
636599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
637599a09b2SChao Yu #endif
63839a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
63939a53e0cSJaegeuk Kim };
64039a53e0cSJaegeuk Kim 
64139a53e0cSJaegeuk Kim /*
64239a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
64339a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
64439a53e0cSJaegeuk Kim  * by the data offset in a file.
64539a53e0cSJaegeuk Kim  */
64639a53e0cSJaegeuk Kim struct dnode_of_data {
64739a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
64839a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
64939a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
65039a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
65139a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
65239a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
65393bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
6543cf45747SChao Yu 	char cur_level;			/* level of hole node page */
6553cf45747SChao Yu 	char max_level;			/* level of current page located */
65639a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
65739a53e0cSJaegeuk Kim };
65839a53e0cSJaegeuk Kim 
65939a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
66039a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
66139a53e0cSJaegeuk Kim {
662d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
66339a53e0cSJaegeuk Kim 	dn->inode = inode;
66439a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
66539a53e0cSJaegeuk Kim 	dn->node_page = npage;
66639a53e0cSJaegeuk Kim 	dn->nid = nid;
66739a53e0cSJaegeuk Kim }
66839a53e0cSJaegeuk Kim 
66939a53e0cSJaegeuk Kim /*
67039a53e0cSJaegeuk Kim  * For SIT manager
67139a53e0cSJaegeuk Kim  *
67239a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
67339a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
67439a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
67539a53e0cSJaegeuk Kim  * respectively.
67639a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
67739a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
67839a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
67939a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
68039a53e0cSJaegeuk Kim  * data and 8 for node logs.
68139a53e0cSJaegeuk Kim  */
68239a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
68339a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
68439a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
68539a53e0cSJaegeuk Kim 
68639a53e0cSJaegeuk Kim enum {
68739a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
68839a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
68939a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
69039a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
69139a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
69239a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
69338aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
69439a53e0cSJaegeuk Kim };
69539a53e0cSJaegeuk Kim 
6966b4afdd7SJaegeuk Kim struct flush_cmd {
6976b4afdd7SJaegeuk Kim 	struct completion wait;
698721bd4d5SGu Zheng 	struct llist_node llnode;
6996b4afdd7SJaegeuk Kim 	int ret;
7006b4afdd7SJaegeuk Kim };
7016b4afdd7SJaegeuk Kim 
702a688b9d9SGu Zheng struct flush_cmd_control {
703a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
704a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
7058b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
7068b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
707721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
708721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
709a688b9d9SGu Zheng };
710a688b9d9SGu Zheng 
71139a53e0cSJaegeuk Kim struct f2fs_sm_info {
71239a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
71339a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
71439a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
71539a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
71639a53e0cSJaegeuk Kim 
71739a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
71839a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
71939a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
72039a53e0cSJaegeuk Kim 
72139a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
72239a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
72339a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
72439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
72581eb8d6eSJaegeuk Kim 
72681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
72781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
7287fd9e544SJaegeuk Kim 
729bba681cbSJaegeuk Kim 	/* for batched trimming */
730bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
731bba681cbSJaegeuk Kim 
732184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
733184a5cd2SChao Yu 
734216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
735216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
736c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
737ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
7386b4afdd7SJaegeuk Kim 
7396b4afdd7SJaegeuk Kim 	/* for flush command control */
740b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
741a688b9d9SGu Zheng 
7420b54fb84SJaegeuk Kim 	/* for discard command control */
7430b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
74439a53e0cSJaegeuk Kim };
74539a53e0cSJaegeuk Kim 
74639a53e0cSJaegeuk Kim /*
74739a53e0cSJaegeuk Kim  * For superblock
74839a53e0cSJaegeuk Kim  */
74939a53e0cSJaegeuk Kim /*
75039a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
75139a53e0cSJaegeuk Kim  *
75239a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
75339a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
75439a53e0cSJaegeuk Kim  */
75536951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
75639a53e0cSJaegeuk Kim enum count_type {
75739a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
758c227f912SChao Yu 	F2FS_DIRTY_DATA,
75939a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
76039a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
7618dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
7620f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
76336951b38SChao Yu 	F2FS_WB_CP_DATA,
76436951b38SChao Yu 	F2FS_WB_DATA,
76539a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
76639a53e0cSJaegeuk Kim };
76739a53e0cSJaegeuk Kim 
76839a53e0cSJaegeuk Kim /*
769e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
77039a53e0cSJaegeuk Kim  * The available types are:
77139a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
77239a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
77339a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
77439a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
77539a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
77639a53e0cSJaegeuk Kim  *			with waiting the bio's completion
77739a53e0cSJaegeuk Kim  * ...			Only can be used with META.
77839a53e0cSJaegeuk Kim  */
7797d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
78039a53e0cSJaegeuk Kim enum page_type {
78139a53e0cSJaegeuk Kim 	DATA,
78239a53e0cSJaegeuk Kim 	NODE,
78339a53e0cSJaegeuk Kim 	META,
78439a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
78539a53e0cSJaegeuk Kim 	META_FLUSH,
7868ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
7878ce67cb0SJaegeuk Kim 	INMEM_DROP,
7888c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
78928bc106bSChao Yu 	INMEM_REVOKE,
7908ce67cb0SJaegeuk Kim 	IPU,
7918ce67cb0SJaegeuk Kim 	OPU,
79239a53e0cSJaegeuk Kim };
79339a53e0cSJaegeuk Kim 
794458e6197SJaegeuk Kim struct f2fs_io_info {
79505ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
796458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
79704d328deSMike Christie 	int op;			/* contains REQ_OP_ */
798ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
7997a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
80028bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
80105ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
8024375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
803d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
804001c584cSChao Yu 	bool cp_rwsem_locked;	/* indicate cp_rwsem is held */
805458e6197SJaegeuk Kim };
806458e6197SJaegeuk Kim 
80768afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
8081ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
809458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
8101ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
8111ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
812458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
813df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
8141ff7bd3bSJaegeuk Kim };
8151ff7bd3bSJaegeuk Kim 
8163c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
8173c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
8183c62be17SJaegeuk Kim struct f2fs_dev_info {
8193c62be17SJaegeuk Kim 	struct block_device *bdev;
8203c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
8213c62be17SJaegeuk Kim 	unsigned int total_segments;
8223c62be17SJaegeuk Kim 	block_t start_blk;
8233c62be17SJaegeuk Kim 	block_t end_blk;
8243c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
8253c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
8263c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
8273c62be17SJaegeuk Kim #endif
8283c62be17SJaegeuk Kim };
8293c62be17SJaegeuk Kim 
830c227f912SChao Yu enum inode_type {
831c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
832c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
8330f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
834c227f912SChao Yu 	NR_INODE_TYPE,
835c227f912SChao Yu };
836c227f912SChao Yu 
83767298804SChao Yu /* for inner inode cache management */
83867298804SChao Yu struct inode_management {
83967298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
84067298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
84167298804SChao Yu 	struct list_head ino_list;		/* inode list head */
84267298804SChao Yu 	unsigned long ino_num;			/* number of entries */
84367298804SChao Yu };
84467298804SChao Yu 
845caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
846caf0047eSChao Yu enum {
847caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
848caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
849caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
850caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
851df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
852bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
853caf0047eSChao Yu };
854caf0047eSChao Yu 
8556beceb54SJaegeuk Kim enum {
8566beceb54SJaegeuk Kim 	CP_TIME,
857d0239e1bSJaegeuk Kim 	REQ_TIME,
8586beceb54SJaegeuk Kim 	MAX_TIME,
8596beceb54SJaegeuk Kim };
8606beceb54SJaegeuk Kim 
86139a53e0cSJaegeuk Kim struct f2fs_sb_info {
86239a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
8635e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
86439a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
865e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
866fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
86739a53e0cSJaegeuk Kim 
868178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
869178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
870178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
871178053e2SDamien Le Moal #endif
872178053e2SDamien Le Moal 
87339a53e0cSJaegeuk Kim 	/* for node-related operations */
87439a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
87539a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
87639a53e0cSJaegeuk Kim 
87739a53e0cSJaegeuk Kim 	/* for segment-related operations */
87839a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
8791ff7bd3bSJaegeuk Kim 
8801ff7bd3bSJaegeuk Kim 	/* for bio operations */
881924b720bSChao Yu 	struct f2fs_bio_info read_io;			/* for read bios */
8821ff7bd3bSJaegeuk Kim 	struct f2fs_bio_info write_io[NR_PAGE_TYPE];	/* for write bios */
8837dfeaa32SJaegeuk Kim 	struct mutex wio_mutex[NODE + 1];	/* bio ordering for NODE/DATA */
8840a595ebaSJaegeuk Kim 	int write_io_size_bits;			/* Write IO size bits */
8850a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
88639a53e0cSJaegeuk Kim 
88739a53e0cSJaegeuk Kim 	/* for checkpoint */
88839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
8898508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
890aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
89139a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
89239936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
893b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
894b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
89559c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
896fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
8976beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
8986beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
89939a53e0cSJaegeuk Kim 
90067298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
9016451e041SJaegeuk Kim 
9026451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
9030d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
90439a53e0cSJaegeuk Kim 
905c227f912SChao Yu 	/* for inode management */
906c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
907c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
90839a53e0cSJaegeuk Kim 
90913054c54SChao Yu 	/* for extent tree cache */
91013054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
9115e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
91213054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
91313054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
9147441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
915137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
91674fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
91713054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
91813054c54SChao Yu 
91939a53e0cSJaegeuk Kim 	/* basic filesystem units */
92039a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
92139a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
92239a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
92339a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
92439a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
92539a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
92639a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
92739a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
92839a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
92939a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
93039a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
93139a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
93239a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
933e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
93439a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
935ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
93639a53e0cSJaegeuk Kim 
93739a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
93839a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
939a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
94039a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
94139a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
942523be8a6SJaegeuk Kim 
943523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
94435782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
94541382ec4SJaegeuk Kim 	/* # of allocated blocks */
94641382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
94739a53e0cSJaegeuk Kim 
948687de7f1SJaegeuk Kim 	/* writeback control */
949687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
950687de7f1SJaegeuk Kim 
951513c5f37SJaegeuk Kim 	/* valid inode count */
952513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
953513c5f37SJaegeuk Kim 
95439a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
95539a53e0cSJaegeuk Kim 
95639a53e0cSJaegeuk Kim 	/* for cleaning operations */
95739a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
95839a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
9595ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
96039a53e0cSJaegeuk Kim 
961e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
962e93b9865SHou Pengyang 	u64 fggc_threshold;
963e93b9865SHou Pengyang 
964b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
965b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
966b1c57c1cSJaegeuk Kim 
96739a53e0cSJaegeuk Kim 	/*
96839a53e0cSJaegeuk Kim 	 * for stat information.
96939a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
97039a53e0cSJaegeuk Kim 	 */
97135b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
97239a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
97339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
97439a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
975b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
9765b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
9775b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
9785b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
9795b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
980d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
98103e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
98203e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
98326a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
984648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
98526a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
986648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
98739a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
98833fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
98935b09d82SNamjae Jeon #endif
99039a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
991b59d0baeSNamjae Jeon 
992b59d0baeSNamjae Jeon 	/* For sysfs suppport */
993b59d0baeSNamjae Jeon 	struct kobject s_kobj;
994b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
9952658e50dSJaegeuk Kim 
9962658e50dSJaegeuk Kim 	/* For shrinker support */
9972658e50dSJaegeuk Kim 	struct list_head s_list;
9983c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
9993c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
10002658e50dSJaegeuk Kim 	struct mutex umount_mutex;
10012658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
10028f1dbbbbSShuoran Liu 
10038f1dbbbbSShuoran Liu 	/* For write statistics */
10048f1dbbbbSShuoran Liu 	u64 sectors_written_start;
10058f1dbbbbSShuoran Liu 	u64 kbytes_written;
100643b6573bSKeith Mok 
100743b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
100843b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
10091ecc0c5cSChao Yu 
10101ecc0c5cSChao Yu 	/* For fault injection */
10111ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
10121ecc0c5cSChao Yu 	struct f2fs_fault_info fault_info;
10131ecc0c5cSChao Yu #endif
101439a53e0cSJaegeuk Kim };
101539a53e0cSJaegeuk Kim 
10161ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
101755523519SChao Yu #define f2fs_show_injection_info(type)				\
101855523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
101955523519SChao Yu 		KERN_INFO, fault_name[type],			\
102055523519SChao Yu 		__func__, __builtin_return_address(0))
10211ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
10221ecc0c5cSChao Yu {
10231ecc0c5cSChao Yu 	struct f2fs_fault_info *ffi = &sbi->fault_info;
10241ecc0c5cSChao Yu 
10251ecc0c5cSChao Yu 	if (!ffi->inject_rate)
10261ecc0c5cSChao Yu 		return false;
10271ecc0c5cSChao Yu 
10281ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
10291ecc0c5cSChao Yu 		return false;
10301ecc0c5cSChao Yu 
10311ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
10321ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
10331ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
10341ecc0c5cSChao Yu 		return true;
10351ecc0c5cSChao Yu 	}
10361ecc0c5cSChao Yu 	return false;
10371ecc0c5cSChao Yu }
10381ecc0c5cSChao Yu #endif
10391ecc0c5cSChao Yu 
10408f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
10418f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
10428f1dbbbbSShuoran Liu  */
10438f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
104468afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
104568afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
10468f1dbbbbSShuoran Liu 
10476beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
10486beceb54SJaegeuk Kim {
10496beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
10506beceb54SJaegeuk Kim }
10516beceb54SJaegeuk Kim 
10526beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
10536beceb54SJaegeuk Kim {
10546beceb54SJaegeuk Kim 	struct timespec ts = {sbi->interval_time[type], 0};
10556beceb54SJaegeuk Kim 	unsigned long interval = timespec_to_jiffies(&ts);
10566beceb54SJaegeuk Kim 
10576beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
10586beceb54SJaegeuk Kim }
10596beceb54SJaegeuk Kim 
1060d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1061d0239e1bSJaegeuk Kim {
1062d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1063d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1064d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1065d0239e1bSJaegeuk Kim 
1066d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1067d0239e1bSJaegeuk Kim 		return 0;
1068d0239e1bSJaegeuk Kim 
1069d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1070d0239e1bSJaegeuk Kim }
1071d0239e1bSJaegeuk Kim 
107239a53e0cSJaegeuk Kim /*
107339a53e0cSJaegeuk Kim  * Inline functions
107439a53e0cSJaegeuk Kim  */
107543b6573bSKeith Mok static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
107643b6573bSKeith Mok 			   unsigned int length)
107743b6573bSKeith Mok {
107843b6573bSKeith Mok 	SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
107943b6573bSKeith Mok 	u32 *ctx = (u32 *)shash_desc_ctx(shash);
108043b6573bSKeith Mok 	int err;
108143b6573bSKeith Mok 
108243b6573bSKeith Mok 	shash->tfm = sbi->s_chksum_driver;
108343b6573bSKeith Mok 	shash->flags = 0;
108443b6573bSKeith Mok 	*ctx = F2FS_SUPER_MAGIC;
108543b6573bSKeith Mok 
108643b6573bSKeith Mok 	err = crypto_shash_update(shash, address, length);
108743b6573bSKeith Mok 	BUG_ON(err);
108843b6573bSKeith Mok 
108943b6573bSKeith Mok 	return *ctx;
109043b6573bSKeith Mok }
109143b6573bSKeith Mok 
109243b6573bSKeith Mok static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
109343b6573bSKeith Mok 				  void *buf, size_t buf_size)
109443b6573bSKeith Mok {
109543b6573bSKeith Mok 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
109643b6573bSKeith Mok }
109743b6573bSKeith Mok 
109839a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
109939a53e0cSJaegeuk Kim {
110039a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
110139a53e0cSJaegeuk Kim }
110239a53e0cSJaegeuk Kim 
110339a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
110439a53e0cSJaegeuk Kim {
110539a53e0cSJaegeuk Kim 	return sb->s_fs_info;
110639a53e0cSJaegeuk Kim }
110739a53e0cSJaegeuk Kim 
11084081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
11094081363fSJaegeuk Kim {
11104081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
11114081363fSJaegeuk Kim }
11124081363fSJaegeuk Kim 
11134081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
11144081363fSJaegeuk Kim {
11154081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
11164081363fSJaegeuk Kim }
11174081363fSJaegeuk Kim 
11184081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
11194081363fSJaegeuk Kim {
11204081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
11214081363fSJaegeuk Kim }
11224081363fSJaegeuk Kim 
112339a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
112439a53e0cSJaegeuk Kim {
112539a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
112639a53e0cSJaegeuk Kim }
112739a53e0cSJaegeuk Kim 
112839a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
112939a53e0cSJaegeuk Kim {
113039a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
113139a53e0cSJaegeuk Kim }
113239a53e0cSJaegeuk Kim 
113345590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
113445590710SGu Zheng {
113545590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
113645590710SGu Zheng }
113745590710SGu Zheng 
113858bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
113958bfaf44SJaegeuk Kim {
114058bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
114158bfaf44SJaegeuk Kim }
114258bfaf44SJaegeuk Kim 
114339a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
114439a53e0cSJaegeuk Kim {
114539a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
114639a53e0cSJaegeuk Kim }
114739a53e0cSJaegeuk Kim 
114839a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
114939a53e0cSJaegeuk Kim {
115039a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
115139a53e0cSJaegeuk Kim }
115239a53e0cSJaegeuk Kim 
115339a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
115439a53e0cSJaegeuk Kim {
115539a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
115639a53e0cSJaegeuk Kim }
115739a53e0cSJaegeuk Kim 
115839a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
115939a53e0cSJaegeuk Kim {
116039a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
116139a53e0cSJaegeuk Kim }
116239a53e0cSJaegeuk Kim 
116339a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
116439a53e0cSJaegeuk Kim {
116539a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
116639a53e0cSJaegeuk Kim }
116739a53e0cSJaegeuk Kim 
11689df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
11699df27d98SGu Zheng {
11709df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
11719df27d98SGu Zheng }
11729df27d98SGu Zheng 
11734ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
11744ef51a8fSJaegeuk Kim {
11754ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
11764ef51a8fSJaegeuk Kim }
11774ef51a8fSJaegeuk Kim 
1178caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
117939a53e0cSJaegeuk Kim {
1180fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
118139a53e0cSJaegeuk Kim }
118239a53e0cSJaegeuk Kim 
1183caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
118439a53e0cSJaegeuk Kim {
1185fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1186caf0047eSChao Yu }
1187caf0047eSChao Yu 
1188caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1189caf0047eSChao Yu {
1190fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
119139a53e0cSJaegeuk Kim }
119239a53e0cSJaegeuk Kim 
1193d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1194d71b5564SJaegeuk Kim {
1195d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1196d71b5564SJaegeuk Kim }
1197d71b5564SJaegeuk Kim 
1198ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1199ced2c7eaSKinglong Mee {
1200ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1201ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1202ced2c7eaSKinglong Mee }
1203ced2c7eaSKinglong Mee 
1204aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
120525ca923bSJaegeuk Kim {
120625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1207aaec2b1dSChao Yu 
120825ca923bSJaegeuk Kim 	return ckpt_flags & f;
120925ca923bSJaegeuk Kim }
121025ca923bSJaegeuk Kim 
1211aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
121225ca923bSJaegeuk Kim {
1213aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1214aaec2b1dSChao Yu }
1215aaec2b1dSChao Yu 
1216aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1217aaec2b1dSChao Yu {
1218aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1219aaec2b1dSChao Yu 
1220aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
122125ca923bSJaegeuk Kim 	ckpt_flags |= f;
122225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
122325ca923bSJaegeuk Kim }
122425ca923bSJaegeuk Kim 
1225aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
122625ca923bSJaegeuk Kim {
1227aaec2b1dSChao Yu 	spin_lock(&sbi->cp_lock);
1228aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1229aaec2b1dSChao Yu 	spin_unlock(&sbi->cp_lock);
1230aaec2b1dSChao Yu }
1231aaec2b1dSChao Yu 
1232aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1233aaec2b1dSChao Yu {
1234aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1235aaec2b1dSChao Yu 
1236aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
123725ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
123825ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
123925ca923bSJaegeuk Kim }
124025ca923bSJaegeuk Kim 
1241aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1242aaec2b1dSChao Yu {
1243aaec2b1dSChao Yu 	spin_lock(&sbi->cp_lock);
1244aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1245aaec2b1dSChao Yu 	spin_unlock(&sbi->cp_lock);
1246aaec2b1dSChao Yu }
1247aaec2b1dSChao Yu 
124822ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
124922ad0b6aSJaegeuk Kim {
125022ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
125122ad0b6aSJaegeuk Kim 
125222ad0b6aSJaegeuk Kim 	if (lock)
125322ad0b6aSJaegeuk Kim 		spin_lock(&sbi->cp_lock);
125422ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
125522ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
125622ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
125722ad0b6aSJaegeuk Kim 	if (lock)
125822ad0b6aSJaegeuk Kim 		spin_unlock(&sbi->cp_lock);
125922ad0b6aSJaegeuk Kim }
126022ad0b6aSJaegeuk Kim 
126122ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
126222ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
126322ad0b6aSJaegeuk Kim {
126422ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
126522ad0b6aSJaegeuk Kim 
126622ad0b6aSJaegeuk Kim 	return (cpc) ? (cpc->reason == CP_UMOUNT) && set : set;
126722ad0b6aSJaegeuk Kim }
126822ad0b6aSJaegeuk Kim 
1269e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
127039a53e0cSJaegeuk Kim {
1271b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
127239a53e0cSJaegeuk Kim }
127339a53e0cSJaegeuk Kim 
1274e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
127539a53e0cSJaegeuk Kim {
1276b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
127739936837SJaegeuk Kim }
127839936837SJaegeuk Kim 
1279e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
128039936837SJaegeuk Kim {
1281b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
128239936837SJaegeuk Kim }
128339936837SJaegeuk Kim 
1284e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
128539936837SJaegeuk Kim {
1286b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
128739a53e0cSJaegeuk Kim }
128839a53e0cSJaegeuk Kim 
1289119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1290119ee914SJaegeuk Kim {
1291119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1292119ee914SJaegeuk Kim 
1293119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1294119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1295119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1296119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1297119ee914SJaegeuk Kim 	return reason;
1298119ee914SJaegeuk Kim }
1299119ee914SJaegeuk Kim 
1300119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1301119ee914SJaegeuk Kim {
1302119ee914SJaegeuk Kim 	return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
1303119ee914SJaegeuk Kim }
1304119ee914SJaegeuk Kim 
1305119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1306119ee914SJaegeuk Kim {
1307aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1308aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1309119ee914SJaegeuk Kim }
1310119ee914SJaegeuk Kim 
131139a53e0cSJaegeuk Kim /*
131239a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
131339a53e0cSJaegeuk Kim  */
1314064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
131539a53e0cSJaegeuk Kim {
1316d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1317d6b7d4b3SChao Yu 		return -EINVAL;
1318cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1319064e0823SNamjae Jeon 		return -EINVAL;
1320064e0823SNamjae Jeon 	return 0;
132139a53e0cSJaegeuk Kim }
132239a53e0cSJaegeuk Kim 
132339a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
132439a53e0cSJaegeuk Kim 
132539a53e0cSJaegeuk Kim /*
132639a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
132739a53e0cSJaegeuk Kim  */
132839a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
132939a53e0cSJaegeuk Kim {
133039a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
13316c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
133239a53e0cSJaegeuk Kim 	else
13336c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
133439a53e0cSJaegeuk Kim }
133539a53e0cSJaegeuk Kim 
13364bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
13374bc8e9bcSChao Yu {
13384bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
13394bc8e9bcSChao Yu }
13404bc8e9bcSChao Yu 
13418edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *, blkcnt_t, bool);
134239a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
134346008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
134439a53e0cSJaegeuk Kim {
1345dd11a5dfSJaegeuk Kim 	blkcnt_t diff;
1346dd11a5dfSJaegeuk Kim 
1347cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
134855523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
134955523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1350cb78942bSJaegeuk Kim 		return false;
135155523519SChao Yu 	}
1352cb78942bSJaegeuk Kim #endif
1353dd11a5dfSJaegeuk Kim 	/*
1354dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1355dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1356dd11a5dfSJaegeuk Kim 	 */
1357dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1358cfb271d4SChao Yu 
135939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
13602555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
13612555a2d5SJaegeuk Kim 	if (unlikely(sbi->total_valid_block_count > sbi->user_block_count)) {
1362dd11a5dfSJaegeuk Kim 		diff = sbi->total_valid_block_count - sbi->user_block_count;
1363dd11a5dfSJaegeuk Kim 		*count -= diff;
13642555a2d5SJaegeuk Kim 		sbi->total_valid_block_count = sbi->user_block_count;
136546008c6dSChao Yu 		if (!*count) {
136639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1367dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
136839a53e0cSJaegeuk Kim 			return false;
136939a53e0cSJaegeuk Kim 		}
137046008c6dSChao Yu 	}
137139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
137241382ec4SJaegeuk Kim 
13732555a2d5SJaegeuk Kim 	f2fs_i_blocks_write(inode, *count, true);
137439a53e0cSJaegeuk Kim 	return true;
137539a53e0cSJaegeuk Kim }
137639a53e0cSJaegeuk Kim 
1377da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
137839a53e0cSJaegeuk Kim 						struct inode *inode,
137939a53e0cSJaegeuk Kim 						blkcnt_t count)
138039a53e0cSJaegeuk Kim {
138139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
13829850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
13839850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, inode->i_blocks < count);
138439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
138539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
13862555a2d5SJaegeuk Kim 	f2fs_i_blocks_write(inode, count, false);
138739a53e0cSJaegeuk Kim }
138839a53e0cSJaegeuk Kim 
138939a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
139039a53e0cSJaegeuk Kim {
139135782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
13927c4abcbeSChao Yu 
139336951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
139436951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
13957c4abcbeSChao Yu 		return;
13967c4abcbeSChao Yu 
1397caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
139839a53e0cSJaegeuk Kim }
139939a53e0cSJaegeuk Kim 
1400a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
140139a53e0cSJaegeuk Kim {
1402204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1403c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1404c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
140539a53e0cSJaegeuk Kim }
140639a53e0cSJaegeuk Kim 
140739a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
140839a53e0cSJaegeuk Kim {
140935782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
141039a53e0cSJaegeuk Kim }
141139a53e0cSJaegeuk Kim 
1412a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
141339a53e0cSJaegeuk Kim {
14145ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
14155ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
14161fe54f9dSJaegeuk Kim 		return;
14171fe54f9dSJaegeuk Kim 
1418204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1419c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1420c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
142139a53e0cSJaegeuk Kim }
142239a53e0cSJaegeuk Kim 
1423523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
142439a53e0cSJaegeuk Kim {
142535782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
142639a53e0cSJaegeuk Kim }
142739a53e0cSJaegeuk Kim 
1428204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1429f8b2c1f9SJaegeuk Kim {
1430204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1431f8b2c1f9SJaegeuk Kim }
1432f8b2c1f9SJaegeuk Kim 
14335ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
14345ac206cfSNamjae Jeon {
14353519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1436523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1437523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1438523be8a6SJaegeuk Kim 
1439523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
14405ac206cfSNamjae Jeon }
14415ac206cfSNamjae Jeon 
144239a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
144339a53e0cSJaegeuk Kim {
14448b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
144539a53e0cSJaegeuk Kim }
144639a53e0cSJaegeuk Kim 
1447f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1448f83a2584SYunlei He {
1449f83a2584SYunlei He 	return sbi->discard_blks;
1450f83a2584SYunlei He }
1451f83a2584SYunlei He 
145239a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
145339a53e0cSJaegeuk Kim {
145439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
145539a53e0cSJaegeuk Kim 
145639a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
145739a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
145839a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
145939a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
146039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
146139a53e0cSJaegeuk Kim 
146239a53e0cSJaegeuk Kim 	return 0;
146339a53e0cSJaegeuk Kim }
146439a53e0cSJaegeuk Kim 
146555141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
146655141486SWanpeng Li {
146755141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
146855141486SWanpeng Li }
146955141486SWanpeng Li 
147039a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
147139a53e0cSJaegeuk Kim {
147239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
14731dbe4152SChangman Lee 	int offset;
14741dbe4152SChangman Lee 
147555141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
14761dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
14771dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
14781dbe4152SChangman Lee 		else
147965b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
14801dbe4152SChangman Lee 	} else {
14811dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
148225ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
148339a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
148439a53e0cSJaegeuk Kim 	}
14851dbe4152SChangman Lee }
148639a53e0cSJaegeuk Kim 
148739a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
148839a53e0cSJaegeuk Kim {
14898508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
149039a53e0cSJaegeuk Kim 
14918508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
149239a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
149339a53e0cSJaegeuk Kim 	return start_addr;
149439a53e0cSJaegeuk Kim }
149539a53e0cSJaegeuk Kim 
14968508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
14978508e44aSJaegeuk Kim {
14988508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
14998508e44aSJaegeuk Kim 
15008508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
15018508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
15028508e44aSJaegeuk Kim 	return start_addr;
15038508e44aSJaegeuk Kim }
15048508e44aSJaegeuk Kim 
15058508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
15068508e44aSJaegeuk Kim {
15078508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
15088508e44aSJaegeuk Kim }
15098508e44aSJaegeuk Kim 
151039a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
151139a53e0cSJaegeuk Kim {
151239a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
151339a53e0cSJaegeuk Kim }
151439a53e0cSJaegeuk Kim 
151539a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
1516ef86d709SGu Zheng 						struct inode *inode)
151739a53e0cSJaegeuk Kim {
151839a53e0cSJaegeuk Kim 	block_t	valid_block_count;
151939a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
152039a53e0cSJaegeuk Kim 
152139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
152239a53e0cSJaegeuk Kim 
1523ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1524cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
152539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
152639a53e0cSJaegeuk Kim 		return false;
152739a53e0cSJaegeuk Kim 	}
152839a53e0cSJaegeuk Kim 
1529ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1530cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
153139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
153239a53e0cSJaegeuk Kim 		return false;
153339a53e0cSJaegeuk Kim 	}
153439a53e0cSJaegeuk Kim 
153539a53e0cSJaegeuk Kim 	if (inode)
15368edd03c8SJaegeuk Kim 		f2fs_i_blocks_write(inode, 1, true);
1537ef86d709SGu Zheng 
1538ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1539ef86d709SGu Zheng 	sbi->total_valid_block_count++;
154039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
154139a53e0cSJaegeuk Kim 
154241382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
154339a53e0cSJaegeuk Kim 	return true;
154439a53e0cSJaegeuk Kim }
154539a53e0cSJaegeuk Kim 
154639a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1547ef86d709SGu Zheng 						struct inode *inode)
154839a53e0cSJaegeuk Kim {
154939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
155039a53e0cSJaegeuk Kim 
15519850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
15529850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
15539850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !inode->i_blocks);
155439a53e0cSJaegeuk Kim 
15558edd03c8SJaegeuk Kim 	f2fs_i_blocks_write(inode, 1, false);
1556ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1557ef86d709SGu Zheng 	sbi->total_valid_block_count--;
155839a53e0cSJaegeuk Kim 
155939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
156039a53e0cSJaegeuk Kim }
156139a53e0cSJaegeuk Kim 
156239a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
156339a53e0cSJaegeuk Kim {
15648b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
156539a53e0cSJaegeuk Kim }
156639a53e0cSJaegeuk Kim 
156739a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
156839a53e0cSJaegeuk Kim {
1569513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
157039a53e0cSJaegeuk Kim }
157139a53e0cSJaegeuk Kim 
15720e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
157339a53e0cSJaegeuk Kim {
1574513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
157539a53e0cSJaegeuk Kim }
157639a53e0cSJaegeuk Kim 
1577513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
157839a53e0cSJaegeuk Kim {
1579513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
158039a53e0cSJaegeuk Kim }
158139a53e0cSJaegeuk Kim 
1582a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1583a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1584a56c7c6fSJaegeuk Kim {
1585c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1586c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1587cac5a3d8SDongOh Shin 
1588c41f3cc3SJaegeuk Kim 	if (page)
1589c41f3cc3SJaegeuk Kim 		return page;
1590c41f3cc3SJaegeuk Kim 
159155523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
159255523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1593c41f3cc3SJaegeuk Kim 		return NULL;
159455523519SChao Yu 	}
1595c41f3cc3SJaegeuk Kim #endif
1596a56c7c6fSJaegeuk Kim 	if (!for_write)
1597a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1598a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1599a56c7c6fSJaegeuk Kim }
1600a56c7c6fSJaegeuk Kim 
16016e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
16026e2c64adSJaegeuk Kim {
16036e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
16046e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
16056e2c64adSJaegeuk Kim 
16066e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
16076e2c64adSJaegeuk Kim 	kunmap(dst);
16086e2c64adSJaegeuk Kim 	kunmap(src);
16096e2c64adSJaegeuk Kim }
16106e2c64adSJaegeuk Kim 
161139a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
161239a53e0cSJaegeuk Kim {
1613031fa8ccSJaegeuk Kim 	if (!page)
161439a53e0cSJaegeuk Kim 		return;
161539a53e0cSJaegeuk Kim 
161639a53e0cSJaegeuk Kim 	if (unlock) {
16179850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
161839a53e0cSJaegeuk Kim 		unlock_page(page);
161939a53e0cSJaegeuk Kim 	}
162009cbfeafSKirill A. Shutemov 	put_page(page);
162139a53e0cSJaegeuk Kim }
162239a53e0cSJaegeuk Kim 
162339a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
162439a53e0cSJaegeuk Kim {
162539a53e0cSJaegeuk Kim 	if (dn->node_page)
162639a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
162739a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
162839a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
162939a53e0cSJaegeuk Kim 	dn->node_page = NULL;
163039a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
163139a53e0cSJaegeuk Kim }
163239a53e0cSJaegeuk Kim 
163339a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1634e8512d2eSGu Zheng 					size_t size)
163539a53e0cSJaegeuk Kim {
1636e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
163739a53e0cSJaegeuk Kim }
163839a53e0cSJaegeuk Kim 
16397bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
16407bd59381SGu Zheng 						gfp_t flags)
16417bd59381SGu Zheng {
16427bd59381SGu Zheng 	void *entry;
16437bd59381SGu Zheng 
164480c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
164580c54505SJaegeuk Kim 	if (!entry)
164680c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
16477bd59381SGu Zheng 	return entry;
16487bd59381SGu Zheng }
16497bd59381SGu Zheng 
1650740432f8SJaegeuk Kim static inline struct bio *f2fs_bio_alloc(int npages)
1651740432f8SJaegeuk Kim {
1652740432f8SJaegeuk Kim 	struct bio *bio;
1653740432f8SJaegeuk Kim 
1654740432f8SJaegeuk Kim 	/* No failure on bio allocation */
1655740432f8SJaegeuk Kim 	bio = bio_alloc(GFP_NOIO, npages);
165680c54505SJaegeuk Kim 	if (!bio)
165780c54505SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1658740432f8SJaegeuk Kim 	return bio;
1659740432f8SJaegeuk Kim }
1660740432f8SJaegeuk Kim 
16619be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
16629be32d72SJaegeuk Kim 				unsigned long index, void *item)
16639be32d72SJaegeuk Kim {
16649be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
16659be32d72SJaegeuk Kim 		cond_resched();
16669be32d72SJaegeuk Kim }
16679be32d72SJaegeuk Kim 
166839a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
166939a53e0cSJaegeuk Kim 
167039a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
167139a53e0cSJaegeuk Kim {
167245590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
1673cac5a3d8SDongOh Shin 
167439a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
167539a53e0cSJaegeuk Kim }
167639a53e0cSJaegeuk Kim 
167739a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
167839a53e0cSJaegeuk Kim {
167939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
168039a53e0cSJaegeuk Kim }
168139a53e0cSJaegeuk Kim 
168239a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
168339a53e0cSJaegeuk Kim 		unsigned int offset)
168439a53e0cSJaegeuk Kim {
168539a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
168639a53e0cSJaegeuk Kim 	__le32 *addr_array;
1687cac5a3d8SDongOh Shin 
168845590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
168939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
169039a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
169139a53e0cSJaegeuk Kim }
169239a53e0cSJaegeuk Kim 
169339a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
169439a53e0cSJaegeuk Kim {
169539a53e0cSJaegeuk Kim 	int mask;
169639a53e0cSJaegeuk Kim 
169739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
169839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
169939a53e0cSJaegeuk Kim 	return mask & *addr;
170039a53e0cSJaegeuk Kim }
170139a53e0cSJaegeuk Kim 
1702a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1703a66cdd98SJaegeuk Kim {
1704a66cdd98SJaegeuk Kim 	int mask;
1705a66cdd98SJaegeuk Kim 
1706a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1707a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1708a66cdd98SJaegeuk Kim 	*addr |= mask;
1709a66cdd98SJaegeuk Kim }
1710a66cdd98SJaegeuk Kim 
1711a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1712a66cdd98SJaegeuk Kim {
1713a66cdd98SJaegeuk Kim 	int mask;
1714a66cdd98SJaegeuk Kim 
1715a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1716a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1717a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1718a66cdd98SJaegeuk Kim }
1719a66cdd98SJaegeuk Kim 
172052aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
172139a53e0cSJaegeuk Kim {
172239a53e0cSJaegeuk Kim 	int mask;
172339a53e0cSJaegeuk Kim 	int ret;
172439a53e0cSJaegeuk Kim 
172539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
172639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
172739a53e0cSJaegeuk Kim 	ret = mask & *addr;
172839a53e0cSJaegeuk Kim 	*addr |= mask;
172939a53e0cSJaegeuk Kim 	return ret;
173039a53e0cSJaegeuk Kim }
173139a53e0cSJaegeuk Kim 
173252aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
173339a53e0cSJaegeuk Kim {
173439a53e0cSJaegeuk Kim 	int mask;
173539a53e0cSJaegeuk Kim 	int ret;
173639a53e0cSJaegeuk Kim 
173739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
173839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
173939a53e0cSJaegeuk Kim 	ret = mask & *addr;
174039a53e0cSJaegeuk Kim 	*addr &= ~mask;
174139a53e0cSJaegeuk Kim 	return ret;
174239a53e0cSJaegeuk Kim }
174339a53e0cSJaegeuk Kim 
1744c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1745c6ac4c0eSGu Zheng {
1746c6ac4c0eSGu Zheng 	int mask;
1747c6ac4c0eSGu Zheng 
1748c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1749c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1750c6ac4c0eSGu Zheng 	*addr ^= mask;
1751c6ac4c0eSGu Zheng }
1752c6ac4c0eSGu Zheng 
175339a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
175439a53e0cSJaegeuk Kim enum {
175539a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1756b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
175726de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
1758ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
175939a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
176039a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
176139a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1762c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1763c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1764444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
17651001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
176634d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1767fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1768fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
176988b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
177088b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
17715fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
177202a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
17733c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
17741e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
1775b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
1776510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
1777d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
1778c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
1779dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
1780ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
178139a53e0cSJaegeuk Kim };
178239a53e0cSJaegeuk Kim 
1783205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
1784205b9822SJaegeuk Kim 						int flag, bool set)
178539a53e0cSJaegeuk Kim {
1786205b9822SJaegeuk Kim 	switch (flag) {
1787205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
1788205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
1789205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
1790205b9822SJaegeuk Kim 		if (set)
1791205b9822SJaegeuk Kim 			return;
1792205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
1793205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
17947c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
1795205b9822SJaegeuk Kim 	}
179639a53e0cSJaegeuk Kim }
179739a53e0cSJaegeuk Kim 
179891942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
179939a53e0cSJaegeuk Kim {
180091942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
180191942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
1802205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
180339a53e0cSJaegeuk Kim }
180439a53e0cSJaegeuk Kim 
180591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
180639a53e0cSJaegeuk Kim {
180791942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
180839a53e0cSJaegeuk Kim }
180939a53e0cSJaegeuk Kim 
181091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
181139a53e0cSJaegeuk Kim {
181291942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
181391942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
1814205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
181539a53e0cSJaegeuk Kim }
181639a53e0cSJaegeuk Kim 
181791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
1818444c580fSJaegeuk Kim {
181991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
182091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
18217c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
182239a53e0cSJaegeuk Kim }
182339a53e0cSJaegeuk Kim 
1824a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
1825a1961246SJaegeuk Kim {
1826a1961246SJaegeuk Kim 	if (inc)
1827a1961246SJaegeuk Kim 		inc_nlink(inode);
1828a1961246SJaegeuk Kim 	else
1829a1961246SJaegeuk Kim 		drop_nlink(inode);
18307c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1831a1961246SJaegeuk Kim }
1832a1961246SJaegeuk Kim 
18338edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
18348edd03c8SJaegeuk Kim 					blkcnt_t diff, bool add)
18358edd03c8SJaegeuk Kim {
183626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
183726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
183826de9b11SJaegeuk Kim 
18398edd03c8SJaegeuk Kim 	inode->i_blocks = add ? inode->i_blocks + diff :
18408edd03c8SJaegeuk Kim 				inode->i_blocks - diff;
18417c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
184226de9b11SJaegeuk Kim 	if (clean || recover)
184326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
18448edd03c8SJaegeuk Kim }
18458edd03c8SJaegeuk Kim 
1846fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
1847fc9581c8SJaegeuk Kim {
184826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
184926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
185026de9b11SJaegeuk Kim 
1851fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
1852fc9581c8SJaegeuk Kim 		return;
1853fc9581c8SJaegeuk Kim 
1854fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
18557c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
185626de9b11SJaegeuk Kim 	if (clean || recover)
185726de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
185826de9b11SJaegeuk Kim }
185926de9b11SJaegeuk Kim 
1860205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
1861205b9822SJaegeuk Kim {
1862205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
18637c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1864205b9822SJaegeuk Kim }
1865205b9822SJaegeuk Kim 
1866205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
1867205b9822SJaegeuk Kim {
1868205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
18697c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1870205b9822SJaegeuk Kim }
1871205b9822SJaegeuk Kim 
1872205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
1873205b9822SJaegeuk Kim {
1874205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
18757c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
1876205b9822SJaegeuk Kim }
1877205b9822SJaegeuk Kim 
187891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
1879444c580fSJaegeuk Kim {
1880205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
1881205b9822SJaegeuk Kim 
1882444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
1883205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
18841001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
1885205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
188634d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
1887205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
1888b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
1889205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
1890510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
1891205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
1892444c580fSJaegeuk Kim }
1893444c580fSJaegeuk Kim 
189491942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
1895444c580fSJaegeuk Kim {
1896444c580fSJaegeuk Kim 	ri->i_inline = 0;
1897444c580fSJaegeuk Kim 
189891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
1899444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
190091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
19011001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
190291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
190334d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
190491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
1905b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
190691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
1907510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
1908444c580fSJaegeuk Kim }
1909444c580fSJaegeuk Kim 
1910987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
1911987c7c31SChao Yu {
191291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
1913987c7c31SChao Yu }
1914987c7c31SChao Yu 
191581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
1916de93653fSJaegeuk Kim {
191781ca7350SChao Yu 	if (f2fs_has_inline_xattr(inode))
1918de93653fSJaegeuk Kim 		return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1919de93653fSJaegeuk Kim 	return DEF_ADDRS_PER_INODE;
1920de93653fSJaegeuk Kim }
1921de93653fSJaegeuk Kim 
192265985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
192365985d93SJaegeuk Kim {
1924695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
1925cac5a3d8SDongOh Shin 
192665985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
192765985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
192865985d93SJaegeuk Kim }
192965985d93SJaegeuk Kim 
193065985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
193165985d93SJaegeuk Kim {
1932987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
193365985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
193465985d93SJaegeuk Kim 	else
193565985d93SJaegeuk Kim 		return 0;
193665985d93SJaegeuk Kim }
193765985d93SJaegeuk Kim 
19380dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
19390dbdc2aeSJaegeuk Kim {
194091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
19410dbdc2aeSJaegeuk Kim }
19420dbdc2aeSJaegeuk Kim 
1943b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
1944b3d208f9SJaegeuk Kim {
194591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
1946b3d208f9SJaegeuk Kim }
1947b3d208f9SJaegeuk Kim 
1948510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
1949510022a8SJaegeuk Kim {
195091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
1951510022a8SJaegeuk Kim }
1952510022a8SJaegeuk Kim 
195388b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
195488b88a66SJaegeuk Kim {
195591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
195688b88a66SJaegeuk Kim }
195788b88a66SJaegeuk Kim 
19585fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
19595fe45743SChao Yu {
19605fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
19615fe45743SChao Yu }
19625fe45743SChao Yu 
196302a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
196402a1335fSJaegeuk Kim {
196591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
196602a1335fSJaegeuk Kim }
196702a1335fSJaegeuk Kim 
19683c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
19693c6c2bebSJaegeuk Kim {
197091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
19713c6c2bebSJaegeuk Kim }
19723c6c2bebSJaegeuk Kim 
19731e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
19741e84371fSJaegeuk Kim {
197591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
19761e84371fSJaegeuk Kim }
19771e84371fSJaegeuk Kim 
19781001b347SHuajun Li static inline void *inline_data_addr(struct page *page)
19791001b347SHuajun Li {
1980695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
1981cac5a3d8SDongOh Shin 
19821001b347SHuajun Li 	return (void *)&(ri->i_addr[1]);
19831001b347SHuajun Li }
19841001b347SHuajun Li 
198534d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
198634d67debSChao Yu {
198791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
198834d67debSChao Yu }
198934d67debSChao Yu 
19909486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
19919486ba44SJaegeuk Kim {
19929486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
19939486ba44SJaegeuk Kim 		kunmap(page);
19949486ba44SJaegeuk Kim }
19959486ba44SJaegeuk Kim 
1996b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
1997b5492af7SJaegeuk Kim {
1998b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
1999b5492af7SJaegeuk Kim }
2000b5492af7SJaegeuk Kim 
2001b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2002b5492af7SJaegeuk Kim {
2003b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
20047c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2005b5492af7SJaegeuk Kim }
2006b5492af7SJaegeuk Kim 
2007b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2008b5492af7SJaegeuk Kim {
2009b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
20107c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2011b5492af7SJaegeuk Kim }
2012b5492af7SJaegeuk Kim 
201326787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
201426787236SJaegeuk Kim {
201526787236SJaegeuk Kim 	if (dsync) {
201626787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
201726787236SJaegeuk Kim 		bool ret;
201826787236SJaegeuk Kim 
201926787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
202026787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
202126787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
202226787236SJaegeuk Kim 		return ret;
202326787236SJaegeuk Kim 	}
202426787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
202526787236SJaegeuk Kim 			file_keep_isize(inode) ||
202626787236SJaegeuk Kim 			i_size_read(inode) & PAGE_MASK)
202726787236SJaegeuk Kim 		return false;
202826787236SJaegeuk Kim 	return F2FS_I(inode)->last_disk_size == i_size_read(inode);
2029267378d4SChao Yu }
2030267378d4SChao Yu 
2031267378d4SChao Yu static inline int f2fs_readonly(struct super_block *sb)
2032267378d4SChao Yu {
2033267378d4SChao Yu 	return sb->s_flags & MS_RDONLY;
2034267378d4SChao Yu }
203539a53e0cSJaegeuk Kim 
203639a53e0cSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
203739a53e0cSJaegeuk Kim {
2038aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
203939a53e0cSJaegeuk Kim }
204039a53e0cSJaegeuk Kim 
2041eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2042eaa693f4SJaegeuk Kim {
2043eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2044eaa693f4SJaegeuk Kim 		return true;
2045eaa693f4SJaegeuk Kim 
2046eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2047eaa693f4SJaegeuk Kim 		return true;
2048eaa693f4SJaegeuk Kim 
2049eaa693f4SJaegeuk Kim 	return false;
2050eaa693f4SJaegeuk Kim }
2051eaa693f4SJaegeuk Kim 
20523e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
20533e72f721SJaegeuk Kim {
20543e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
205591942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
20563e72f721SJaegeuk Kim 		return false;
20573e72f721SJaegeuk Kim 
2058886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
20593e72f721SJaegeuk Kim }
20603e72f721SJaegeuk Kim 
20611ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
20621ecc0c5cSChao Yu 					size_t size, gfp_t flags)
20630414b004SJaegeuk Kim {
20642c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
206555523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
206655523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
20672c63feadSJaegeuk Kim 		return NULL;
206855523519SChao Yu 	}
20692c63feadSJaegeuk Kim #endif
20700414b004SJaegeuk Kim 	return kmalloc(size, flags);
20710414b004SJaegeuk Kim }
20720414b004SJaegeuk Kim 
207339307a8eSJaegeuk Kim static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
207439307a8eSJaegeuk Kim {
207539307a8eSJaegeuk Kim 	void *ret;
207639307a8eSJaegeuk Kim 
207739307a8eSJaegeuk Kim 	ret = kmalloc(size, flags | __GFP_NOWARN);
207839307a8eSJaegeuk Kim 	if (!ret)
207939307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags, PAGE_KERNEL);
208039307a8eSJaegeuk Kim 	return ret;
208139307a8eSJaegeuk Kim }
208239307a8eSJaegeuk Kim 
208339307a8eSJaegeuk Kim static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
208439307a8eSJaegeuk Kim {
208539307a8eSJaegeuk Kim 	void *ret;
208639307a8eSJaegeuk Kim 
208739307a8eSJaegeuk Kim 	ret = kzalloc(size, flags | __GFP_NOWARN);
208839307a8eSJaegeuk Kim 	if (!ret)
208939307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
209039307a8eSJaegeuk Kim 	return ret;
209139307a8eSJaegeuk Kim }
209239307a8eSJaegeuk Kim 
209339a53e0cSJaegeuk Kim #define get_inode_mode(i) \
209491942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
209539a53e0cSJaegeuk Kim 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
209639a53e0cSJaegeuk Kim 
209739a53e0cSJaegeuk Kim /*
20981e1bb4baSJaegeuk Kim  * file.c
209939a53e0cSJaegeuk Kim  */
2100cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2101cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2102cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2103cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2104a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2105a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2106cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2107cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2108cac5a3d8SDongOh Shin int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2109cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2110cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
211139a53e0cSJaegeuk Kim 
211239a53e0cSJaegeuk Kim /*
211339a53e0cSJaegeuk Kim  * inode.c
211439a53e0cSJaegeuk Kim  */
2115cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2116cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2117cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2118cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2119cac5a3d8SDongOh Shin int update_inode(struct inode *inode, struct page *node_page);
2120cac5a3d8SDongOh Shin int update_inode_page(struct inode *inode);
2121cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2122cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2123cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
212439a53e0cSJaegeuk Kim 
212539a53e0cSJaegeuk Kim /*
212639a53e0cSJaegeuk Kim  * namei.c
212739a53e0cSJaegeuk Kim  */
212839a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
212939a53e0cSJaegeuk Kim 
213039a53e0cSJaegeuk Kim /*
213139a53e0cSJaegeuk Kim  * dir.c
213239a53e0cSJaegeuk Kim  */
2133cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2134cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2135cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2136cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2137cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2138cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2139cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2140cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2141cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2142cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2143cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2144cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2145cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2146cac5a3d8SDongOh Shin 			unsigned int current_depth);
2147cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2148cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2149cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2150cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2151cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2152cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2153cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2154cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2155cac5a3d8SDongOh Shin 			struct page **page);
2156cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2157cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2158cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2159cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2160cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2161cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2162cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2163cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2164cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2165cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2166cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2167cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2168cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2169cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2170cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2171cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
217239a53e0cSJaegeuk Kim 
2173b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2174b7f7a5e0SAl Viro {
21752b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2176510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2177b7f7a5e0SAl Viro }
2178b7f7a5e0SAl Viro 
217939a53e0cSJaegeuk Kim /*
218039a53e0cSJaegeuk Kim  * super.c
218139a53e0cSJaegeuk Kim  */
2182cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2183cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2184cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2185cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2186a07ef784SNamjae Jeon extern __printf(3, 4)
2187cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2188984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
218939a53e0cSJaegeuk Kim 
219039a53e0cSJaegeuk Kim /*
219139a53e0cSJaegeuk Kim  * hash.c
219239a53e0cSJaegeuk Kim  */
2193cac5a3d8SDongOh Shin f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info);
219439a53e0cSJaegeuk Kim 
219539a53e0cSJaegeuk Kim /*
219639a53e0cSJaegeuk Kim  * node.c
219739a53e0cSJaegeuk Kim  */
219839a53e0cSJaegeuk Kim struct dnode_of_data;
219939a53e0cSJaegeuk Kim struct node_info;
220039a53e0cSJaegeuk Kim 
2201cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2202cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2203cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2204cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2205cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2206cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2207cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2208cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2209cac5a3d8SDongOh Shin int truncate_xattr_node(struct inode *inode, struct page *page);
2210cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2211cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2212cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
2213cac5a3d8SDongOh Shin struct page *new_node_page(struct dnode_of_data *dn,
2214cac5a3d8SDongOh Shin 			unsigned int ofs, struct page *ipage);
2215cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2216cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2217cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2218cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2219cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2220cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2221cac5a3d8SDongOh Shin int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc);
222222ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2223cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2224cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2225cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2226cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2227cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2228d260081cSChao Yu int recover_xattr_data(struct inode *inode, struct page *page,
2229cac5a3d8SDongOh Shin 			block_t blkaddr);
2230cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2231cac5a3d8SDongOh Shin int restore_node_summary(struct f2fs_sb_info *sbi,
2232cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
223322ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2234cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2235cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
22366e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
223739a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
223839a53e0cSJaegeuk Kim 
223939a53e0cSJaegeuk Kim /*
224039a53e0cSJaegeuk Kim  * segment.c
224139a53e0cSJaegeuk Kim  */
2242cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
2243cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
22448c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2245cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2246cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2247cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2248cac5a3d8SDongOh Shin int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2249cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2250cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2251cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2252cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2253cac5a3d8SDongOh Shin void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
2254d431413fSChao Yu void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2255cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2256cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2257cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2258cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2259cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2260cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2261cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2262cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2263cac5a3d8SDongOh Shin void write_meta_page(struct f2fs_sb_info *sbi, struct page *page);
2264cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2265cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2266d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2267cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2268cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2269cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2270cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2271cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2272cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2273cac5a3d8SDongOh Shin 			bool recover_newaddr);
2274cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2275cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2276cac5a3d8SDongOh Shin 			struct f2fs_summary *sum, int type);
2277cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2278cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2279cac5a3d8SDongOh Shin void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2280cac5a3d8SDongOh Shin 			block_t blkaddr);
2281cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2282cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2283cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2284cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2285cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2286cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2287cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
22887fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
22897fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
229039a53e0cSJaegeuk Kim 
229139a53e0cSJaegeuk Kim /*
229239a53e0cSJaegeuk Kim  * checkpoint.c
229339a53e0cSJaegeuk Kim  */
2294cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2295cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2296cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2297cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2298cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2299cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2300cac5a3d8SDongOh Shin 			int type, bool sync);
2301cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2302cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2303cac5a3d8SDongOh Shin 			long nr_to_write);
2304cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2305cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2306cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2307cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2308cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2309cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2310cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2311cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2312cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2313cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2314cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2315cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2316cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2317cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2318cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2319cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
23206e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
232139a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
232239a53e0cSJaegeuk Kim 
232339a53e0cSJaegeuk Kim /*
232439a53e0cSJaegeuk Kim  * data.c
232539a53e0cSJaegeuk Kim  */
2326cac5a3d8SDongOh Shin void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
2327cac5a3d8SDongOh Shin 			int rw);
2328cac5a3d8SDongOh Shin void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
2329942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2330942fd319SJaegeuk Kim 				enum page_type type, int rw);
2331cac5a3d8SDongOh Shin void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi);
2332cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2333cac5a3d8SDongOh Shin int f2fs_submit_page_mbio(struct f2fs_io_info *fio);
2334cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2335cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2336cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2337cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2338cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2339cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2340cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2341cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2342cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2343cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2344cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2345cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2346cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2347cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2348cac5a3d8SDongOh Shin 			bool for_write);
2349cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2350cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2351cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2352cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2353cac5a3d8SDongOh Shin 			int create, int flag);
2354cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2355cac5a3d8SDongOh Shin 			u64 start, u64 len);
2356cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2357cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2358cac5a3d8SDongOh Shin 			unsigned int length);
2359cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
23605b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2361cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2362cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
23635b7a487cSWeichao Guo #endif
236439a53e0cSJaegeuk Kim 
236539a53e0cSJaegeuk Kim /*
236639a53e0cSJaegeuk Kim  * gc.c
236739a53e0cSJaegeuk Kim  */
2368cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2369cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2370cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2371*e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2372*e066b83cSJaegeuk Kim 			unsigned int segno);
2373cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
237439a53e0cSJaegeuk Kim 
237539a53e0cSJaegeuk Kim /*
237639a53e0cSJaegeuk Kim  * recovery.c
237739a53e0cSJaegeuk Kim  */
2378cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2379cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
238039a53e0cSJaegeuk Kim 
238139a53e0cSJaegeuk Kim /*
238239a53e0cSJaegeuk Kim  * debug.c
238339a53e0cSJaegeuk Kim  */
238439a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
238539a53e0cSJaegeuk Kim struct f2fs_stat_info {
238639a53e0cSJaegeuk Kim 	struct list_head stat_list;
238739a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
238839a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
238939a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
23905b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
23915b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2392c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
239335782b23SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
239435782b23SJaegeuk Kim 	int inmem_pages;
23950f18b462SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, ndirty_all;
2396b8559dc2SChao Yu 	int nats, dirty_nats, sits, dirty_sits, free_nids, alloc_nids;
239739a53e0cSJaegeuk Kim 	int total_count, utilization;
23988b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
23998b8dd65fSChao Yu 	int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
24005f32366aSChao Yu 	int nr_discard_cmd;
2401d84d1cbdSChao Yu 	unsigned int undiscard_blks;
2402a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2403648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2404f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
240539a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
240639a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
240739a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
240839a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
240942190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
241039a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2411e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
241239a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2413e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
241439a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
241539a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
241639a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
241739a53e0cSJaegeuk Kim 
241839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
241939a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2420b9a2c252SChangman Lee 	unsigned int inplace_count;
24219edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
242239a53e0cSJaegeuk Kim };
242339a53e0cSJaegeuk Kim 
2424963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2425963d4f7dSGu Zheng {
2426963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2427963d4f7dSGu Zheng }
2428963d4f7dSGu Zheng 
2429942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
243042190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
243139a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2432dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
243333fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
243433fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
24355b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
24365b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
24375b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
24385b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2439d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2440d5e8f6c9SChao Yu 	do {								\
2441d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2442d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2443d5e8f6c9SChao Yu 	} while (0)
2444d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2445d5e8f6c9SChao Yu 	do {								\
2446d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2447d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2448d5e8f6c9SChao Yu 	} while (0)
24490dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
24500dbdc2aeSJaegeuk Kim 	do {								\
24510dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
245203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
24530dbdc2aeSJaegeuk Kim 	} while (0)
24540dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
24550dbdc2aeSJaegeuk Kim 	do {								\
24560dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
245703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
24580dbdc2aeSJaegeuk Kim 	} while (0)
24593289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
24603289c061SJaegeuk Kim 	do {								\
24613289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
246203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
24633289c061SJaegeuk Kim 	} while (0)
24643289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
24653289c061SJaegeuk Kim 	do {								\
24663289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
246703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
24683289c061SJaegeuk Kim 	} while (0)
2469dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2470dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2471dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2472dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2473b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2474b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
247526a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
2476cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
247726a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
2478cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
247926a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
248026a28a0cSJaegeuk Kim 	do {								\
248126a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
248226a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
248326a28a0cSJaegeuk Kim 		if (cur > max)						\
248426a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
248526a28a0cSJaegeuk Kim 	} while (0)
2486648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
2487648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2488648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
2489648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2490648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
2491648d50baSChao Yu 	do {								\
2492648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
2493648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
2494648d50baSChao Yu 		if (cur > max)						\
2495648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
2496648d50baSChao Yu 	} while (0)
2497e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
249839a53e0cSJaegeuk Kim 	do {								\
2499963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
250068afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
250168afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
250239a53e0cSJaegeuk Kim 			si->data_segs++;				\
2503e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2504e1235983SChangman Lee 		} else {						\
250539a53e0cSJaegeuk Kim 			si->node_segs++;				\
2506e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2507e1235983SChangman Lee 		}							\
250839a53e0cSJaegeuk Kim 	} while (0)
250939a53e0cSJaegeuk Kim 
251039a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
251168afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
251239a53e0cSJaegeuk Kim 
2513e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
251439a53e0cSJaegeuk Kim 	do {								\
2515963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
251639a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
251739a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
251868afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
251939a53e0cSJaegeuk Kim 	} while (0)
252039a53e0cSJaegeuk Kim 
2521e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
252239a53e0cSJaegeuk Kim 	do {								\
2523963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
252439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
252539a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
252668afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
252739a53e0cSJaegeuk Kim 	} while (0)
252839a53e0cSJaegeuk Kim 
2529cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
2530cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
2531787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
25324589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
253339a53e0cSJaegeuk Kim #else
2534942e0be6SChangman Lee #define stat_inc_cp_count(si)
253542190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)
253639a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
2537dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(si)
253833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)
253933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)
2540dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)
2541029e13ccSChao Yu #define stat_inc_rbtree_node_hit(sb)
254291c481ffSChao Yu #define stat_inc_largest_node_hit(sbi)
254391c481ffSChao Yu #define stat_inc_cached_node_hit(sbi)
2544d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)
2545d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)
25460dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)
25470dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)
25483289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)
25493289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)
255026a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)
255126a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)
255226a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)
2553648d50baSChao Yu #define stat_inc_volatile_write(inode)
2554648d50baSChao Yu #define stat_dec_volatile_write(inode)
2555648d50baSChao Yu #define stat_update_max_volatile_write(inode)
2556dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)
2557dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)
2558b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)
2559e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)
256039a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
2561e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)
2562e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)
256339a53e0cSJaegeuk Kim 
256439a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
256539a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2566787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
25674589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
256839a53e0cSJaegeuk Kim #endif
256939a53e0cSJaegeuk Kim 
257039a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
257139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
257239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
257339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
257439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
257539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
257639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
257739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
2578cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
257939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
258029e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
25811001b347SHuajun Li 
2582e18c65b2SHuajun Li /*
2583e18c65b2SHuajun Li  * inline.c
2584e18c65b2SHuajun Li  */
2585cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
2586cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
2587cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
2588bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2589cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
2590cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2591cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
2592cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
2593cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
2594cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2595cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2596cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2597cac5a3d8SDongOh Shin 			struct page *ipage);
2598cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2599cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2600cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2601cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2602cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2603cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
2604cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2605cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
2606cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
2607cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
2608cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
2609cde4de12SJaegeuk Kim 
2610cde4de12SJaegeuk Kim /*
26112658e50dSJaegeuk Kim  * shrinker.c
26122658e50dSJaegeuk Kim  */
2613cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
2614cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2615cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2616cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2617cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2618cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
26192658e50dSJaegeuk Kim 
26202658e50dSJaegeuk Kim /*
2621a28ef1f5SChao Yu  * extent_cache.c
2622a28ef1f5SChao Yu  */
2623004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2624004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
2625004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2626004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
2627004b6862SChao Yu 				unsigned int ofs);
2628004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2629004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
2630004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
2631004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
2632004b6862SChao Yu 		bool force);
2633df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2634df0f6b44SChao Yu 						struct rb_root *root);
2635cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2636cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2637cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
2638cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
2639cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
2640cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2641cac5a3d8SDongOh Shin 			struct extent_info *ei);
2642cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
264319b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
2644cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
2645cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
2646a28ef1f5SChao Yu int __init create_extent_cache(void);
2647a28ef1f5SChao Yu void destroy_extent_cache(void);
2648a28ef1f5SChao Yu 
2649a28ef1f5SChao Yu /*
2650cde4de12SJaegeuk Kim  * crypto support
2651cde4de12SJaegeuk Kim  */
26520b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
2653cde4de12SJaegeuk Kim {
2654cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
2655cde4de12SJaegeuk Kim }
2656cde4de12SJaegeuk Kim 
2657cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
2658cde4de12SJaegeuk Kim {
2659cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2660cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2661cde4de12SJaegeuk Kim #endif
2662cde4de12SJaegeuk Kim }
2663cde4de12SJaegeuk Kim 
2664cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2665cde4de12SJaegeuk Kim {
26660b81d077SJaegeuk Kim 	return bio->bi_private != NULL;
2667cde4de12SJaegeuk Kim }
2668cde4de12SJaegeuk Kim 
2669cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2670cde4de12SJaegeuk Kim {
2671cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2672cde4de12SJaegeuk Kim }
2673f424f664SJaegeuk Kim 
26740bfd7a09SDamien Le Moal static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
267552763a4bSJaegeuk Kim {
26760bfd7a09SDamien Le Moal 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
267752763a4bSJaegeuk Kim }
267852763a4bSJaegeuk Kim 
2679178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
2680178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
26813c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
2682178053e2SDamien Le Moal {
2683178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
26843c62be17SJaegeuk Kim 	int i;
2685178053e2SDamien Le Moal 
26863c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
26873c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
26883c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
26893c62be17SJaegeuk Kim 	return -EINVAL;
2690178053e2SDamien Le Moal }
2691178053e2SDamien Le Moal #endif
2692178053e2SDamien Le Moal 
269396ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
269496ba2decSDamien Le Moal {
269596ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
269696ba2decSDamien Le Moal 
269796ba2decSDamien Le Moal 	return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
269852763a4bSJaegeuk Kim }
269952763a4bSJaegeuk Kim 
270052763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
270152763a4bSJaegeuk Kim {
270252763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
270352763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
270452763a4bSJaegeuk Kim 
270552763a4bSJaegeuk Kim 	switch (mt) {
270652763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
270752763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
270852763a4bSJaegeuk Kim 		break;
270952763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
271052763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
271152763a4bSJaegeuk Kim 		break;
271252763a4bSJaegeuk Kim 	}
271352763a4bSJaegeuk Kim }
271452763a4bSJaegeuk Kim 
2715fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
2716fcc85a4dSJaegeuk Kim {
2717fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2718886f56f9SAl Viro 	umode_t mode = inode->i_mode;
2719fcc85a4dSJaegeuk Kim 
2720fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2721fcc85a4dSJaegeuk Kim #else
2722fcc85a4dSJaegeuk Kim 	return 0;
2723fcc85a4dSJaegeuk Kim #endif
2724fcc85a4dSJaegeuk Kim }
2725fcc85a4dSJaegeuk Kim 
272639a53e0cSJaegeuk Kim #endif
2727