xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 6dbb17961f46b2eafcea2f2627aabb309553e068)
10a8165d7SJaegeuk Kim /*
239a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
339a53e0cSJaegeuk Kim  *
439a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
539a53e0cSJaegeuk Kim  *             http://www.samsung.com/
639a53e0cSJaegeuk Kim  *
739a53e0cSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
839a53e0cSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
939a53e0cSJaegeuk Kim  * published by the Free Software Foundation.
1039a53e0cSJaegeuk Kim  */
1139a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
1239a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1339a53e0cSJaegeuk Kim 
1439a53e0cSJaegeuk Kim #include <linux/types.h>
1539a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1639a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1739a53e0cSJaegeuk Kim #include <linux/slab.h>
1839a53e0cSJaegeuk Kim #include <linux/crc32.h>
1939a53e0cSJaegeuk Kim #include <linux/magic.h>
20c2d715d1SJaegeuk Kim #include <linux/kobject.h>
217bd59381SGu Zheng #include <linux/sched.h>
227c2e5963SJaegeuk Kim #include <linux/cred.h>
2339307a8eSJaegeuk Kim #include <linux/vmalloc.h>
24740432f8SJaegeuk Kim #include <linux/bio.h>
25d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
260abd675eSChao Yu #include <linux/quotaops.h>
2743b6573bSKeith Mok #include <crypto/hash.h>
2839a53e0cSJaegeuk Kim 
29734f0d24SDave Chinner #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
30734f0d24SDave Chinner #include <linux/fscrypt.h>
31734f0d24SDave Chinner 
325d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
339850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
345d56b671SJaegeuk Kim #else
359850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
369850cf4aSJaegeuk Kim 	do {								\
379850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
389850cf4aSJaegeuk Kim 			WARN_ON(1);					\
39caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
409850cf4aSJaegeuk Kim 		}							\
419850cf4aSJaegeuk Kim 	} while (0)
425d56b671SJaegeuk Kim #endif
435d56b671SJaegeuk Kim 
442c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
452c63feadSJaegeuk Kim enum {
462c63feadSJaegeuk Kim 	FAULT_KMALLOC,
47628b3d14SChao Yu 	FAULT_KVMALLOC,
48c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4901eccef7SChao Yu 	FAULT_PAGE_GET,
50d62fe971SChao Yu 	FAULT_ALLOC_BIO,
51cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
52cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
53cb78942bSJaegeuk Kim 	FAULT_BLOCK,
54cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5553aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5614b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
578b038c70SChao Yu 	FAULT_IO,
580f348028SChao Yu 	FAULT_CHECKPOINT,
592c63feadSJaegeuk Kim 	FAULT_MAX,
602c63feadSJaegeuk Kim };
612c63feadSJaegeuk Kim 
6208796897SSheng Yong struct f2fs_fault_info {
6308796897SSheng Yong 	atomic_t inject_ops;
6408796897SSheng Yong 	unsigned int inject_rate;
6508796897SSheng Yong 	unsigned int inject_type;
6608796897SSheng Yong };
6708796897SSheng Yong 
682c63feadSJaegeuk Kim extern char *fault_name[FAULT_MAX];
6968afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
702c63feadSJaegeuk Kim #endif
712c63feadSJaegeuk Kim 
7239a53e0cSJaegeuk Kim /*
7339a53e0cSJaegeuk Kim  * For mount options
7439a53e0cSJaegeuk Kim  */
7539a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
82444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
831001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8434d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8534d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8634d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
87d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8889672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
896aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
90343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9173faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9236abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9336abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
940abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
950abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
965c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
974b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
986afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
997e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
10039a53e0cSJaegeuk Kim 
10163189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10263189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10363189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10463189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10539a53e0cSJaegeuk Kim 
10639a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10739a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10839a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
10939a53e0cSJaegeuk Kim 
110a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
111a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
112a9841c4dSJaegeuk Kim 			 * address format, __le32.
113a9841c4dSJaegeuk Kim 			 */
11439a53e0cSJaegeuk Kim typedef u32 nid_t;
11539a53e0cSJaegeuk Kim 
11639a53e0cSJaegeuk Kim struct f2fs_mount_info {
11739a53e0cSJaegeuk Kim 	unsigned int opt;
11863189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
11963189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12063189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12163189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12263189b78SChao Yu 	int active_logs;		/* # of active logs */
12363189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12463189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12563189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12663189b78SChao Yu #endif
12763189b78SChao Yu #ifdef CONFIG_QUOTA
12863189b78SChao Yu 	/* Names of quota files with journalled quota */
12963189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13063189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13163189b78SChao Yu #endif
13263189b78SChao Yu 	/* For which write hints are passed down to block layer */
13363189b78SChao Yu 	int whint_mode;
13463189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13563189b78SChao Yu 	int fsync_mode;			/* fsync policy */
136ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
13739a53e0cSJaegeuk Kim };
13839a53e0cSJaegeuk Kim 
139cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1400bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
141e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1427a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1435c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
144704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1456afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
146234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1471c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
148b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
14953fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
150cde4de12SJaegeuk Kim 
15176f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
15276f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
15376f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
154c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
15576f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
156c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
15776f105a2SJaegeuk Kim 
15839a53e0cSJaegeuk Kim /*
1597c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1607c2e5963SJaegeuk Kim  */
1617c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1627c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1637c2e5963SJaegeuk Kim 
1647c2e5963SJaegeuk Kim /*
16539a53e0cSJaegeuk Kim  * For checkpoint manager
16639a53e0cSJaegeuk Kim  */
16739a53e0cSJaegeuk Kim enum {
16839a53e0cSJaegeuk Kim 	NAT_BITMAP,
16939a53e0cSJaegeuk Kim 	SIT_BITMAP
17039a53e0cSJaegeuk Kim };
17139a53e0cSJaegeuk Kim 
172c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
173c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
174c473f1a9SChao Yu #define	CP_SYNC		0x00000004
175c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
176c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1771f43e2adSChao Yu #define CP_TRIMMED	0x00000020
17875ab4cb8SJaegeuk Kim 
17947b89808SJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	2048
180bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
1814ddb1a4dSJaegeuk Kim 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
182a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
183a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
1844ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
185ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
186969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
187969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
18860b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
189dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
190bba681cbSJaegeuk Kim 
19175ab4cb8SJaegeuk Kim struct cp_control {
19275ab4cb8SJaegeuk Kim 	int reason;
1934b2fecc8SJaegeuk Kim 	__u64 trim_start;
1944b2fecc8SJaegeuk Kim 	__u64 trim_end;
1954b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
19675ab4cb8SJaegeuk Kim };
19775ab4cb8SJaegeuk Kim 
198662befdaSChao Yu /*
19981c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
200662befdaSChao Yu  */
201662befdaSChao Yu enum {
202662befdaSChao Yu 	META_CP,
203662befdaSChao Yu 	META_NAT,
20481c1a0f1SChao Yu 	META_SIT,
2054c521f49SJaegeuk Kim 	META_SSA,
2064c521f49SJaegeuk Kim 	META_POR,
207662befdaSChao Yu };
208662befdaSChao Yu 
2096451e041SJaegeuk Kim /* for the list of ino */
2106451e041SJaegeuk Kim enum {
2116451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
212fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
213fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2140a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
21539d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2166451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2176451e041SJaegeuk Kim };
2186451e041SJaegeuk Kim 
2196451e041SJaegeuk Kim struct ino_entry {
22039a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
22139a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
22239d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
22339a53e0cSJaegeuk Kim };
22439a53e0cSJaegeuk Kim 
2252710fd7eSChao Yu /* for the list of inodes to be GCed */
22606292073SChao Yu struct inode_entry {
22739a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
22839a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
22939a53e0cSJaegeuk Kim };
23039a53e0cSJaegeuk Kim 
231a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2327fd9e544SJaegeuk Kim struct discard_entry {
2337fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
234a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
235a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2367fd9e544SJaegeuk Kim };
2377fd9e544SJaegeuk Kim 
238969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
239969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
240969d1b18SChao Yu 
241ba48a33eSChao Yu /* max discard pend list number */
242ba48a33eSChao Yu #define MAX_PLIST_NUM		512
243ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
244ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
245ba48a33eSChao Yu 
24615469963SJaegeuk Kim enum {
24715469963SJaegeuk Kim 	D_PREP,
24815469963SJaegeuk Kim 	D_SUBMIT,
24915469963SJaegeuk Kim 	D_DONE,
25015469963SJaegeuk Kim };
25115469963SJaegeuk Kim 
252004b6862SChao Yu struct discard_info {
253b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
254b01a9201SJaegeuk Kim 	block_t len;			/* length */
255004b6862SChao Yu 	block_t start;			/* actual start address in dev */
256004b6862SChao Yu };
257004b6862SChao Yu 
258b01a9201SJaegeuk Kim struct discard_cmd {
259004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
260004b6862SChao Yu 	union {
261004b6862SChao Yu 		struct {
262004b6862SChao Yu 			block_t lstart;	/* logical start address */
263004b6862SChao Yu 			block_t len;	/* length */
264004b6862SChao Yu 			block_t start;	/* actual start address in dev */
265004b6862SChao Yu 		};
266004b6862SChao Yu 		struct discard_info di;	/* discard info */
267004b6862SChao Yu 
268004b6862SChao Yu 	};
269b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
270b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
271c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
272ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2739a744b92SChao Yu 	unsigned char state;		/* state */
274c81abe34SJaegeuk Kim 	int error;			/* bio error */
275275b66b0SChao Yu };
276275b66b0SChao Yu 
27778997b56SChao Yu enum {
27878997b56SChao Yu 	DPOLICY_BG,
27978997b56SChao Yu 	DPOLICY_FORCE,
28078997b56SChao Yu 	DPOLICY_FSTRIM,
28178997b56SChao Yu 	DPOLICY_UMOUNT,
28278997b56SChao Yu 	MAX_DPOLICY,
28378997b56SChao Yu };
28478997b56SChao Yu 
285ecc9aa00SChao Yu struct discard_policy {
28678997b56SChao Yu 	int type;			/* type of discard */
287ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
288ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
289ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
290ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
291ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
292ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
29378997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
294ecc9aa00SChao Yu };
295ecc9aa00SChao Yu 
2960b54fb84SJaegeuk Kim struct discard_cmd_control {
29715469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
29846f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
299ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
30046f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3018412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
30215469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
303969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
30415469963SJaegeuk Kim 	struct mutex cmd_lock;
305d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
306d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
307969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
308d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
3098b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
3108b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
3115f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
312004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
31339a53e0cSJaegeuk Kim };
31439a53e0cSJaegeuk Kim 
31539a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
31639a53e0cSJaegeuk Kim struct fsync_inode_entry {
31739a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
31839a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
319c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
320c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
32139a53e0cSJaegeuk Kim };
32239a53e0cSJaegeuk Kim 
32368afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
32468afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
32539a53e0cSJaegeuk Kim 
32668afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
32768afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
32868afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
32968afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
33039a53e0cSJaegeuk Kim 
331dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
332dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
333309cc2b6SJaegeuk Kim 
334dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
33539a53e0cSJaegeuk Kim {
336dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
337cac5a3d8SDongOh Shin 
338dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
33939a53e0cSJaegeuk Kim 	return before;
34039a53e0cSJaegeuk Kim }
34139a53e0cSJaegeuk Kim 
342dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
34339a53e0cSJaegeuk Kim {
344dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
345cac5a3d8SDongOh Shin 
346dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
34739a53e0cSJaegeuk Kim 	return before;
34839a53e0cSJaegeuk Kim }
34939a53e0cSJaegeuk Kim 
350dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
351dfc08a12SChao Yu 							int size, int type)
352184a5cd2SChao Yu {
353184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
354dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
355dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
356184a5cd2SChao Yu }
357184a5cd2SChao Yu 
35839a53e0cSJaegeuk Kim /*
359e9750824SNamjae Jeon  * ioctl commands
360e9750824SNamjae Jeon  */
361e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
362e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
363d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
364e9750824SNamjae Jeon 
36588b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
36688b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
36788b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
36802a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3691e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3701e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
371d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
372456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
373d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
374d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3754dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3764dd6f977SJaegeuk Kim 						struct f2fs_move_range)
377e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
378e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
37934dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
38034dc77adSJaegeuk Kim 						struct f2fs_gc_range)
381e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3821ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3831ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
384c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
38588b88a66SJaegeuk Kim 
3860b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3870b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3880b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
389f424f664SJaegeuk Kim 
3901abff93dSJaegeuk Kim /*
3911abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3921abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3931abff93dSJaegeuk Kim  */
3941abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3951abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3961abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3971abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
398c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3991abff93dSJaegeuk Kim 
400e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
401e9750824SNamjae Jeon /*
402e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
403e9750824SNamjae Jeon  */
404e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
405e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
40604ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
407e9750824SNamjae Jeon #endif
408e9750824SNamjae Jeon 
4092c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4102c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4112c1d0305SChao Yu 
41234dc77adSJaegeuk Kim struct f2fs_gc_range {
41334dc77adSJaegeuk Kim 	u32 sync;
41434dc77adSJaegeuk Kim 	u64 start;
41534dc77adSJaegeuk Kim 	u64 len;
41634dc77adSJaegeuk Kim };
41734dc77adSJaegeuk Kim 
418d323d005SChao Yu struct f2fs_defragment {
419d323d005SChao Yu 	u64 start;
420d323d005SChao Yu 	u64 len;
421d323d005SChao Yu };
422d323d005SChao Yu 
4234dd6f977SJaegeuk Kim struct f2fs_move_range {
4244dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4254dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4264dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4274dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4284dd6f977SJaegeuk Kim };
4294dd6f977SJaegeuk Kim 
430e066b83cSJaegeuk Kim struct f2fs_flush_device {
431e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
432e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
433e066b83cSJaegeuk Kim };
434e066b83cSJaegeuk Kim 
435f2470371SChao Yu /* for inline stuff */
436f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4376afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4387a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4396afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4407a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4417a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
442b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4436afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
444f2470371SChao Yu 
445f2470371SChao Yu /* for inline dir */
446f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
447f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
448f2470371SChao Yu 				BITS_PER_BYTE + 1))
449f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
450f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
451f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
452f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
453f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
454f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
455f2470371SChao Yu 
456e9750824SNamjae Jeon /*
45739a53e0cSJaegeuk Kim  * For INODE and NODE manager
45839a53e0cSJaegeuk Kim  */
4597b3cd7d6SJaegeuk Kim /* for directory operations */
4607b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
461d8c6822aSJaegeuk Kim 	struct inode *inode;
46276a9dd85SChao Yu 	void *bitmap;
4637b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4647b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4657b3cd7d6SJaegeuk Kim 	int max;
46676a9dd85SChao Yu 	int nr_bitmap;
4677b3cd7d6SJaegeuk Kim };
4687b3cd7d6SJaegeuk Kim 
46964c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
47064c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4717b3cd7d6SJaegeuk Kim {
472d8c6822aSJaegeuk Kim 	d->inode = inode;
4737b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
47476a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
475c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
4767b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4777b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
47864c24ecbSTomohiro Kusumi }
479cac5a3d8SDongOh Shin 
48064c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
481f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
48264c24ecbSTomohiro Kusumi {
483f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
484f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
485f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
486f2470371SChao Yu 
48764c24ecbSTomohiro Kusumi 	d->inode = inode;
488f2470371SChao Yu 	d->max = entry_cnt;
489f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
490f2470371SChao Yu 	d->bitmap = t;
491f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
492f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
493f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
4947b3cd7d6SJaegeuk Kim }
4957b3cd7d6SJaegeuk Kim 
496dbe6a5ffSJaegeuk Kim /*
497dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
498dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
499dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
50039a53e0cSJaegeuk Kim  */
501dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
502dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
503266e97a8SJaegeuk Kim enum {
504266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
505266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
506266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
507266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5084f4124d0SChao Yu 					 * by get_data_block.
50939a53e0cSJaegeuk Kim 					 */
510266e97a8SJaegeuk Kim };
511266e97a8SJaegeuk Kim 
512a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
51339a53e0cSJaegeuk Kim 
514817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
515817202d9SChao Yu 
51613054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
51713054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
51813054c54SChao Yu 
51939a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
52013054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
52113054c54SChao Yu 
52213054c54SChao Yu /* number of extent info in extent cache we try to shrink */
52313054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
524c11abd1aSJaegeuk Kim 
52554c2258cSChao Yu struct rb_entry {
52654c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
52754c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
52854c2258cSChao Yu 	unsigned int len;		/* length of the entry */
52954c2258cSChao Yu };
53054c2258cSChao Yu 
53139a53e0cSJaegeuk Kim struct extent_info {
53239a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
533111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
53454c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
53539a53e0cSJaegeuk Kim };
53639a53e0cSJaegeuk Kim 
53713054c54SChao Yu struct extent_node {
53854c2258cSChao Yu 	struct rb_node rb_node;
53954c2258cSChao Yu 	union {
54054c2258cSChao Yu 		struct {
54154c2258cSChao Yu 			unsigned int fofs;
54254c2258cSChao Yu 			unsigned int len;
54354c2258cSChao Yu 			u32 blk;
54454c2258cSChao Yu 		};
54513054c54SChao Yu 		struct extent_info ei;	/* extent info */
54654c2258cSChao Yu 
54754c2258cSChao Yu 	};
54854c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
549201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
55013054c54SChao Yu };
55113054c54SChao Yu 
55213054c54SChao Yu struct extent_tree {
55313054c54SChao Yu 	nid_t ino;			/* inode number */
55413054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
55562c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5563e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
557137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
55813054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
55968e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
56013054c54SChao Yu };
56113054c54SChao Yu 
56239a53e0cSJaegeuk Kim /*
563003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
564003a3e1dSJaegeuk Kim  *
565003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
566003a3e1dSJaegeuk Kim  */
567003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
568003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5697f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5707f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5717f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
572003a3e1dSJaegeuk Kim 
573003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
574003a3e1dSJaegeuk Kim 	block_t m_pblk;
575003a3e1dSJaegeuk Kim 	block_t m_lblk;
576003a3e1dSJaegeuk Kim 	unsigned int m_len;
577003a3e1dSJaegeuk Kim 	unsigned int m_flags;
578da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
579c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
580d5097be5SHyunchul Lee 	int m_seg_type;
581003a3e1dSJaegeuk Kim };
582003a3e1dSJaegeuk Kim 
583e2b4e2bcSChao Yu /* for flag in get_data_block */
584f2220c7fSQiuyang Sun enum {
585f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
586f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
587f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
588f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
589f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
590c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
591f2220c7fSQiuyang Sun };
592e2b4e2bcSChao Yu 
593003a3e1dSJaegeuk Kim /*
59439a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
59539a53e0cSJaegeuk Kim  */
59639a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
597354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
598cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
599e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
60026787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
601b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
60253fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
60339a53e0cSJaegeuk Kim 
604b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
605b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
606b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
607b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
608b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
609b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
610cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
611cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
612cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
613e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
614e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
61526787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
61626787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
617b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
618b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
619b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
620cde4de12SJaegeuk Kim 
621ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
622ab9fa662SJaegeuk Kim 
62339a53e0cSJaegeuk Kim struct f2fs_inode_info {
62439a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
62539a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
62639a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
62738431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6281ad71a27SJaegeuk Kim 	union {
6291ad71a27SJaegeuk Kim 		unsigned int i_current_depth;	/* only for directory depth */
6301ad71a27SJaegeuk Kim 		unsigned short i_gc_failures;	/* only for regular file */
6311ad71a27SJaegeuk Kim 	};
6326666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
63339a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
63439a53e0cSJaegeuk Kim 
63539a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
63639a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
637d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
638204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
63939a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
64039a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
64188c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
642b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
64339a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
64426de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
64588b88a66SJaegeuk Kim 
6460abd675eSChao Yu #ifdef CONFIG_QUOTA
6470abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6480abd675eSChao Yu 
6490abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6500abd675eSChao Yu 	qsize_t i_reserved_quota;
6510abd675eSChao Yu #endif
6520f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6530f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
65457864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
65588b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6567a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
65788b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6583e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
65982e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
6605a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
66127161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
662f2470371SChao Yu 
6637a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6645c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6656afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
6661c1d35dfSChao Yu 	struct timespec i_crtime;	/* inode creation time */
667214c2461SJaegeuk Kim 	struct timespec i_disk_time[4];	/* inode disk times */
66839a53e0cSJaegeuk Kim };
66939a53e0cSJaegeuk Kim 
67039a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
671bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
67239a53e0cSJaegeuk Kim {
673bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
674bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
675bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
67639a53e0cSJaegeuk Kim }
67739a53e0cSJaegeuk Kim 
67839a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
67939a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
68039a53e0cSJaegeuk Kim {
68139a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6824d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
68339a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
68439a53e0cSJaegeuk Kim }
68539a53e0cSJaegeuk Kim 
686429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
687429511cdSChao Yu 						u32 blk, unsigned int len)
688429511cdSChao Yu {
689429511cdSChao Yu 	ei->fofs = fofs;
690429511cdSChao Yu 	ei->blk = blk;
691429511cdSChao Yu 	ei->len = len;
692429511cdSChao Yu }
693429511cdSChao Yu 
694004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
695004b6862SChao Yu 						struct discard_info *front)
696004b6862SChao Yu {
697004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
698004b6862SChao Yu }
699004b6862SChao Yu 
700004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
701004b6862SChao Yu 						struct discard_info *back)
702004b6862SChao Yu {
703004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
704004b6862SChao Yu }
705004b6862SChao Yu 
706004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
707004b6862SChao Yu 						struct discard_info *front)
708004b6862SChao Yu {
709004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
710004b6862SChao Yu }
711004b6862SChao Yu 
712429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
713429511cdSChao Yu 						struct extent_info *front)
714429511cdSChao Yu {
715429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
716429511cdSChao Yu 			back->blk + back->len == front->blk);
717429511cdSChao Yu }
718429511cdSChao Yu 
719429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
720429511cdSChao Yu 						struct extent_info *back)
721429511cdSChao Yu {
722429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
723429511cdSChao Yu }
724429511cdSChao Yu 
725429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
726429511cdSChao Yu 						struct extent_info *front)
727429511cdSChao Yu {
728429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
729429511cdSChao Yu }
730429511cdSChao Yu 
731cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
732205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
733205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
7344abd3f5aSChao Yu {
735205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7364abd3f5aSChao Yu 		et->largest = en->ei;
7377c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
738205b9822SJaegeuk Kim 	}
7394abd3f5aSChao Yu }
7404abd3f5aSChao Yu 
7419a4ffdf5SChao Yu /*
7429a4ffdf5SChao Yu  * For free nid management
7439a4ffdf5SChao Yu  */
7449a4ffdf5SChao Yu enum nid_state {
7459a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7469a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7479a4ffdf5SChao Yu 	MAX_NID_STATE,
748b8559dc2SChao Yu };
749b8559dc2SChao Yu 
75039a53e0cSJaegeuk Kim struct f2fs_nm_info {
75139a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
75239a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
75304d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
75439a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
755cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
756ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7572304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
75839a53e0cSJaegeuk Kim 
75939a53e0cSJaegeuk Kim 	/* NAT cache management */
76039a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
761309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
762b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
76339a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
764309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
765aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
76622ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
76739a53e0cSJaegeuk Kim 
76839a53e0cSJaegeuk Kim 	/* free node ids management */
7698a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7709a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7719a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
772b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
77339a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
774bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
7754ac91242SChao Yu 	unsigned char *nat_block_bitmap;
776586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
77739a53e0cSJaegeuk Kim 
77839a53e0cSJaegeuk Kim 	/* for checkpoint */
77939a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
78022ad0b6aSJaegeuk Kim 
78122ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
78222ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
78322ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
78422ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
785599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
786599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
787599a09b2SChao Yu #endif
78839a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
78939a53e0cSJaegeuk Kim };
79039a53e0cSJaegeuk Kim 
79139a53e0cSJaegeuk Kim /*
79239a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
79339a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
79439a53e0cSJaegeuk Kim  * by the data offset in a file.
79539a53e0cSJaegeuk Kim  */
79639a53e0cSJaegeuk Kim struct dnode_of_data {
79739a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
79839a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
79939a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
80039a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
80139a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
80239a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
80393bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8043cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8053cf45747SChao Yu 	char max_level;			/* level of current page located */
80639a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
80739a53e0cSJaegeuk Kim };
80839a53e0cSJaegeuk Kim 
80939a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
81039a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
81139a53e0cSJaegeuk Kim {
812d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
81339a53e0cSJaegeuk Kim 	dn->inode = inode;
81439a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
81539a53e0cSJaegeuk Kim 	dn->node_page = npage;
81639a53e0cSJaegeuk Kim 	dn->nid = nid;
81739a53e0cSJaegeuk Kim }
81839a53e0cSJaegeuk Kim 
81939a53e0cSJaegeuk Kim /*
82039a53e0cSJaegeuk Kim  * For SIT manager
82139a53e0cSJaegeuk Kim  *
82239a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
82339a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
82439a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
82539a53e0cSJaegeuk Kim  * respectively.
82639a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
82739a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
82839a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
82939a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
83039a53e0cSJaegeuk Kim  * data and 8 for node logs.
83139a53e0cSJaegeuk Kim  */
83239a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
83339a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
83439a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
83539a53e0cSJaegeuk Kim 
83639a53e0cSJaegeuk Kim enum {
83739a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
83839a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
83939a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
84039a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
84139a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
84239a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
84338aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
84439a53e0cSJaegeuk Kim };
84539a53e0cSJaegeuk Kim 
8466b4afdd7SJaegeuk Kim struct flush_cmd {
8476b4afdd7SJaegeuk Kim 	struct completion wait;
848721bd4d5SGu Zheng 	struct llist_node llnode;
84939d787beSChao Yu 	nid_t ino;
8506b4afdd7SJaegeuk Kim 	int ret;
8516b4afdd7SJaegeuk Kim };
8526b4afdd7SJaegeuk Kim 
853a688b9d9SGu Zheng struct flush_cmd_control {
854a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
855a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8568b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8578b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
858721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
859721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
860a688b9d9SGu Zheng };
861a688b9d9SGu Zheng 
86239a53e0cSJaegeuk Kim struct f2fs_sm_info {
86339a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
86439a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
86539a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
86639a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
86739a53e0cSJaegeuk Kim 
8682b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8692b60311dSChao Yu 
87039a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
87139a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
87239a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
87339a53e0cSJaegeuk Kim 
87439a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
87539a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
87639a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
87739a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
87881eb8d6eSJaegeuk Kim 
87981eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
88081eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8817fd9e544SJaegeuk Kim 
882bba681cbSJaegeuk Kim 	/* for batched trimming */
883bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
884bba681cbSJaegeuk Kim 
885184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
886184a5cd2SChao Yu 
887216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
888216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
889c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
890ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
891a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
8926b4afdd7SJaegeuk Kim 
8936b4afdd7SJaegeuk Kim 	/* for flush command control */
894b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
895a688b9d9SGu Zheng 
8960b54fb84SJaegeuk Kim 	/* for discard command control */
8970b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
89839a53e0cSJaegeuk Kim };
89939a53e0cSJaegeuk Kim 
90039a53e0cSJaegeuk Kim /*
90139a53e0cSJaegeuk Kim  * For superblock
90239a53e0cSJaegeuk Kim  */
90339a53e0cSJaegeuk Kim /*
90439a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
90539a53e0cSJaegeuk Kim  *
90639a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
90739a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
90839a53e0cSJaegeuk Kim  */
90936951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
91039a53e0cSJaegeuk Kim enum count_type {
91139a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
912c227f912SChao Yu 	F2FS_DIRTY_DATA,
9132c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
91439a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
91539a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9168dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9170f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
91836951b38SChao Yu 	F2FS_WB_CP_DATA,
91936951b38SChao Yu 	F2FS_WB_DATA,
92039a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
92139a53e0cSJaegeuk Kim };
92239a53e0cSJaegeuk Kim 
92339a53e0cSJaegeuk Kim /*
924e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
92539a53e0cSJaegeuk Kim  * The available types are:
92639a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
92739a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
92839a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
92939a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
93039a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
93139a53e0cSJaegeuk Kim  *			with waiting the bio's completion
93239a53e0cSJaegeuk Kim  * ...			Only can be used with META.
93339a53e0cSJaegeuk Kim  */
9347d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
93539a53e0cSJaegeuk Kim enum page_type {
93639a53e0cSJaegeuk Kim 	DATA,
93739a53e0cSJaegeuk Kim 	NODE,
93839a53e0cSJaegeuk Kim 	META,
93939a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
94039a53e0cSJaegeuk Kim 	META_FLUSH,
9418ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9428ce67cb0SJaegeuk Kim 	INMEM_DROP,
9438c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
94428bc106bSChao Yu 	INMEM_REVOKE,
9458ce67cb0SJaegeuk Kim 	IPU,
9468ce67cb0SJaegeuk Kim 	OPU,
94739a53e0cSJaegeuk Kim };
94839a53e0cSJaegeuk Kim 
949a912b54dSJaegeuk Kim enum temp_type {
950a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
951a912b54dSJaegeuk Kim 	WARM,
952a912b54dSJaegeuk Kim 	COLD,
953a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
954a912b54dSJaegeuk Kim };
955a912b54dSJaegeuk Kim 
956cc15620bSJaegeuk Kim enum need_lock_type {
957cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
958cc15620bSJaegeuk Kim 	LOCK_DONE,
959cc15620bSJaegeuk Kim 	LOCK_RETRY,
960cc15620bSJaegeuk Kim };
961cc15620bSJaegeuk Kim 
962a5fd5050SChao Yu enum cp_reason_type {
963a5fd5050SChao Yu 	CP_NO_NEEDED,
964a5fd5050SChao Yu 	CP_NON_REGULAR,
965a5fd5050SChao Yu 	CP_HARDLINK,
966a5fd5050SChao Yu 	CP_SB_NEED_CP,
967a5fd5050SChao Yu 	CP_WRONG_PINO,
968a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
969a5fd5050SChao Yu 	CP_NODE_NEED_CP,
970a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
971a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
9720a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
973a5fd5050SChao Yu };
974a5fd5050SChao Yu 
975b0af6d49SChao Yu enum iostat_type {
976b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
977b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
978b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
979b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
980b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
981b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
982b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
983b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
984b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
985b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
986b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
987b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
988b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
989b0af6d49SChao Yu 	NR_IO_TYPE,
990b0af6d49SChao Yu };
991b0af6d49SChao Yu 
992458e6197SJaegeuk Kim struct f2fs_io_info {
99305ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
99439d787beSChao Yu 	nid_t ino;		/* inode number */
995458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
996a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
99704d328deSMike Christie 	int op;			/* contains REQ_OP_ */
998ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
9997a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
100028bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
100105ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10024375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1003fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1004d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1005cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1006fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10070833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1008b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1009578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
1010458e6197SJaegeuk Kim };
1011458e6197SJaegeuk Kim 
101268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10131ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1014458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10151ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10161ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1017458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1018df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1019fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1020fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10211ff7bd3bSJaegeuk Kim };
10221ff7bd3bSJaegeuk Kim 
10233c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10243c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10253c62be17SJaegeuk Kim struct f2fs_dev_info {
10263c62be17SJaegeuk Kim 	struct block_device *bdev;
10273c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10283c62be17SJaegeuk Kim 	unsigned int total_segments;
10293c62be17SJaegeuk Kim 	block_t start_blk;
10303c62be17SJaegeuk Kim 	block_t end_blk;
10313c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10323c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10333c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10343c62be17SJaegeuk Kim #endif
10353c62be17SJaegeuk Kim };
10363c62be17SJaegeuk Kim 
1037c227f912SChao Yu enum inode_type {
1038c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1039c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10400f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
104157864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1042c227f912SChao Yu 	NR_INODE_TYPE,
1043c227f912SChao Yu };
1044c227f912SChao Yu 
104567298804SChao Yu /* for inner inode cache management */
104667298804SChao Yu struct inode_management {
104767298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
104867298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
104967298804SChao Yu 	struct list_head ino_list;		/* inode list head */
105067298804SChao Yu 	unsigned long ino_num;			/* number of entries */
105167298804SChao Yu };
105267298804SChao Yu 
1053caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1054caf0047eSChao Yu enum {
1055caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1056caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1057caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1058caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1059df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1060bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
1061caf0047eSChao Yu };
1062caf0047eSChao Yu 
10636beceb54SJaegeuk Kim enum {
10646beceb54SJaegeuk Kim 	CP_TIME,
1065d0239e1bSJaegeuk Kim 	REQ_TIME,
10666beceb54SJaegeuk Kim 	MAX_TIME,
10676beceb54SJaegeuk Kim };
10686beceb54SJaegeuk Kim 
10690cdd3195SHyunchul Lee enum {
10700cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
10710cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1072f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
10730cdd3195SHyunchul Lee };
10740cdd3195SHyunchul Lee 
107507939627SJaegeuk Kim enum {
107607939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
107707939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
107807939627SJaegeuk Kim };
107907939627SJaegeuk Kim 
108093cf93f1SJunling Zheng enum fsync_mode {
108193cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
108293cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
108393cf93f1SJunling Zheng };
108493cf93f1SJunling Zheng 
1085ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1086ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1087ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1088ff62af20SSheng Yong #else
1089ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1090ff62af20SSheng Yong #endif
1091ff62af20SSheng Yong 
109239a53e0cSJaegeuk Kim struct f2fs_sb_info {
109339a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
10945e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
109539a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1096846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1097e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1098fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
109939a53e0cSJaegeuk Kim 
1100178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1101178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1102178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1103178053e2SDamien Le Moal #endif
1104178053e2SDamien Le Moal 
110539a53e0cSJaegeuk Kim 	/* for node-related operations */
110639a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
110739a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
110839a53e0cSJaegeuk Kim 
110939a53e0cSJaegeuk Kim 	/* for segment-related operations */
111039a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11111ff7bd3bSJaegeuk Kim 
11121ff7bd3bSJaegeuk Kim 	/* for bio operations */
1113a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1114e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1115e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
11160a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
111739a53e0cSJaegeuk Kim 
111839a53e0cSJaegeuk Kim 	/* for checkpoint */
111939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11208508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1121aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
112239a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
112339936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1124b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1125b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
112659c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1127fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11286beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11296beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
113039a53e0cSJaegeuk Kim 
113167298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11326451e041SJaegeuk Kim 
11336451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
11340d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
113539a53e0cSJaegeuk Kim 
1136c227f912SChao Yu 	/* for inode management */
1137c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1138c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
113939a53e0cSJaegeuk Kim 
114013054c54SChao Yu 	/* for extent tree cache */
114113054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11425e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
114313054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
114413054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11457441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1146137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
114774fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
114813054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
114913054c54SChao Yu 
115039a53e0cSJaegeuk Kim 	/* basic filesystem units */
115139a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
115239a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
115339a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
115439a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
115539a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
115639a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
115739a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
115839a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
115939a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
116039a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
116139a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
116239a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
116339a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1164e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1165ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1166a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1167f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
116839a53e0cSJaegeuk Kim 
116939a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
117039a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1171a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
117239a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1173daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
117480d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1175daeb433eSChao Yu 
1176292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1177292c196aSChao Yu 
117839a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1179523be8a6SJaegeuk Kim 
1180523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
118135782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
118241382ec4SJaegeuk Kim 	/* # of allocated blocks */
118341382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
118439a53e0cSJaegeuk Kim 
1185687de7f1SJaegeuk Kim 	/* writeback control */
1186687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1187687de7f1SJaegeuk Kim 
1188513c5f37SJaegeuk Kim 	/* valid inode count */
1189513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1190513c5f37SJaegeuk Kim 
119139a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
119239a53e0cSJaegeuk Kim 
119339a53e0cSJaegeuk Kim 	/* for cleaning operations */
119439a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
119539a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
11965ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
119739a53e0cSJaegeuk Kim 
1198e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
1199e93b9865SHou Pengyang 	u64 fggc_threshold;
1200e93b9865SHou Pengyang 
12011ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12021ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12031ad71a27SJaegeuk Kim 
1204b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1205b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1206b1c57c1cSJaegeuk Kim 
120739a53e0cSJaegeuk Kim 	/*
120839a53e0cSJaegeuk Kim 	 * for stat information.
120939a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
121039a53e0cSJaegeuk Kim 	 */
121135b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
121239a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
121339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
121439a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1215b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12165b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12175b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12185b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12195b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1220d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
122103e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
122203e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
122326a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1224648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
122526a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1226648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
122739a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
122833fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
122935b09d82SNamjae Jeon #endif
123039a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1231b59d0baeSNamjae Jeon 
1232b0af6d49SChao Yu 	/* For app/fs IO statistics */
1233b0af6d49SChao Yu 	spinlock_t iostat_lock;
1234b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1235b0af6d49SChao Yu 	bool iostat_enable;
1236b0af6d49SChao Yu 
1237b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1238b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1239b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12402658e50dSJaegeuk Kim 
12412658e50dSJaegeuk Kim 	/* For shrinker support */
12422658e50dSJaegeuk Kim 	struct list_head s_list;
12433c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12443c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
12451228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
12461228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
12472658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12482658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12498f1dbbbbSShuoran Liu 
12508f1dbbbbSShuoran Liu 	/* For write statistics */
12518f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12528f1dbbbbSShuoran Liu 	u64 kbytes_written;
125343b6573bSKeith Mok 
125443b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
125543b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12561ecc0c5cSChao Yu 
1257704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1258704956ecSChao Yu 	__u32 s_chksum_seed;
125939a53e0cSJaegeuk Kim };
126039a53e0cSJaegeuk Kim 
12611ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
126255523519SChao Yu #define f2fs_show_injection_info(type)				\
126355523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
126455523519SChao Yu 		KERN_INFO, fault_name[type],			\
126555523519SChao Yu 		__func__, __builtin_return_address(0))
12661ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12671ecc0c5cSChao Yu {
126863189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
12691ecc0c5cSChao Yu 
12701ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12711ecc0c5cSChao Yu 		return false;
12721ecc0c5cSChao Yu 
12731ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12741ecc0c5cSChao Yu 		return false;
12751ecc0c5cSChao Yu 
12761ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12771ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12781ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12791ecc0c5cSChao Yu 		return true;
12801ecc0c5cSChao Yu 	}
12811ecc0c5cSChao Yu 	return false;
12821ecc0c5cSChao Yu }
12831ecc0c5cSChao Yu #endif
12841ecc0c5cSChao Yu 
12858f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
12868f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
12878f1dbbbbSShuoran Liu  */
12888f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
128968afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
129068afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
12918f1dbbbbSShuoran Liu 
12926beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
12936beceb54SJaegeuk Kim {
12946beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
12956beceb54SJaegeuk Kim }
12966beceb54SJaegeuk Kim 
12976beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
12986beceb54SJaegeuk Kim {
12996bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13006beceb54SJaegeuk Kim 
13016beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13026beceb54SJaegeuk Kim }
13036beceb54SJaegeuk Kim 
1304d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1305d0239e1bSJaegeuk Kim {
1306d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1307d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1308d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1309d0239e1bSJaegeuk Kim 
1310d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1311d0239e1bSJaegeuk Kim 		return 0;
1312d0239e1bSJaegeuk Kim 
1313d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1314d0239e1bSJaegeuk Kim }
1315d0239e1bSJaegeuk Kim 
131639a53e0cSJaegeuk Kim /*
131739a53e0cSJaegeuk Kim  * Inline functions
131839a53e0cSJaegeuk Kim  */
1319416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1320704956ecSChao Yu 			      const void *address, unsigned int length)
1321704956ecSChao Yu {
1322704956ecSChao Yu 	struct {
1323704956ecSChao Yu 		struct shash_desc shash;
1324704956ecSChao Yu 		char ctx[4];
1325704956ecSChao Yu 	} desc;
1326704956ecSChao Yu 	int err;
1327704956ecSChao Yu 
1328704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1329704956ecSChao Yu 
1330704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1331704956ecSChao Yu 	desc.shash.flags = 0;
1332704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1333704956ecSChao Yu 
1334704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1335704956ecSChao Yu 	BUG_ON(err);
1336704956ecSChao Yu 
1337704956ecSChao Yu 	return *(u32 *)desc.ctx;
1338704956ecSChao Yu }
1339704956ecSChao Yu 
1340416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1341416d2dbbSChao Yu 			   unsigned int length)
1342416d2dbbSChao Yu {
1343416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1344416d2dbbSChao Yu }
1345416d2dbbSChao Yu 
1346416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1347416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1348416d2dbbSChao Yu {
1349416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1350416d2dbbSChao Yu }
1351416d2dbbSChao Yu 
1352416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1353416d2dbbSChao Yu 			      const void *address, unsigned int length)
1354416d2dbbSChao Yu {
1355416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1356416d2dbbSChao Yu }
1357416d2dbbSChao Yu 
135839a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
135939a53e0cSJaegeuk Kim {
136039a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
136139a53e0cSJaegeuk Kim }
136239a53e0cSJaegeuk Kim 
136339a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
136439a53e0cSJaegeuk Kim {
136539a53e0cSJaegeuk Kim 	return sb->s_fs_info;
136639a53e0cSJaegeuk Kim }
136739a53e0cSJaegeuk Kim 
13684081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13694081363fSJaegeuk Kim {
13704081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13714081363fSJaegeuk Kim }
13724081363fSJaegeuk Kim 
13734081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13744081363fSJaegeuk Kim {
13754081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13764081363fSJaegeuk Kim }
13774081363fSJaegeuk Kim 
13784081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13794081363fSJaegeuk Kim {
13804081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
13814081363fSJaegeuk Kim }
13824081363fSJaegeuk Kim 
138339a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
138439a53e0cSJaegeuk Kim {
138539a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
138639a53e0cSJaegeuk Kim }
138739a53e0cSJaegeuk Kim 
138839a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
138939a53e0cSJaegeuk Kim {
139039a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
139139a53e0cSJaegeuk Kim }
139239a53e0cSJaegeuk Kim 
139345590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
139445590710SGu Zheng {
139545590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
139645590710SGu Zheng }
139745590710SGu Zheng 
139858bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
139958bfaf44SJaegeuk Kim {
140058bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
140158bfaf44SJaegeuk Kim }
140258bfaf44SJaegeuk Kim 
140339a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
140439a53e0cSJaegeuk Kim {
140539a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
140639a53e0cSJaegeuk Kim }
140739a53e0cSJaegeuk Kim 
140839a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
140939a53e0cSJaegeuk Kim {
141039a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
141139a53e0cSJaegeuk Kim }
141239a53e0cSJaegeuk Kim 
141339a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
141439a53e0cSJaegeuk Kim {
141539a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
141639a53e0cSJaegeuk Kim }
141739a53e0cSJaegeuk Kim 
141839a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
141939a53e0cSJaegeuk Kim {
142039a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
142139a53e0cSJaegeuk Kim }
142239a53e0cSJaegeuk Kim 
142339a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
142439a53e0cSJaegeuk Kim {
142539a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
142639a53e0cSJaegeuk Kim }
142739a53e0cSJaegeuk Kim 
14289df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
14299df27d98SGu Zheng {
14309df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
14319df27d98SGu Zheng }
14329df27d98SGu Zheng 
14334ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
14344ef51a8fSJaegeuk Kim {
14354ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
14364ef51a8fSJaegeuk Kim }
14374ef51a8fSJaegeuk Kim 
1438caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
143939a53e0cSJaegeuk Kim {
1440fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
144139a53e0cSJaegeuk Kim }
144239a53e0cSJaegeuk Kim 
1443caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
144439a53e0cSJaegeuk Kim {
1445fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1446caf0047eSChao Yu }
1447caf0047eSChao Yu 
1448caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1449caf0047eSChao Yu {
1450fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
145139a53e0cSJaegeuk Kim }
145239a53e0cSJaegeuk Kim 
1453d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1454d71b5564SJaegeuk Kim {
1455d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1456d71b5564SJaegeuk Kim }
1457d71b5564SJaegeuk Kim 
1458ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1459ea676733SJaegeuk Kim {
1460ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1461ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1462ea676733SJaegeuk Kim 	return 0;
1463ea676733SJaegeuk Kim }
1464ea676733SJaegeuk Kim 
1465ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1466ced2c7eaSKinglong Mee {
1467ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1468ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1469ced2c7eaSKinglong Mee }
1470ced2c7eaSKinglong Mee 
1471aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
147225ca923bSJaegeuk Kim {
147325ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1474aaec2b1dSChao Yu 
147525ca923bSJaegeuk Kim 	return ckpt_flags & f;
147625ca923bSJaegeuk Kim }
147725ca923bSJaegeuk Kim 
1478aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
147925ca923bSJaegeuk Kim {
1480aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1481aaec2b1dSChao Yu }
1482aaec2b1dSChao Yu 
1483aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1484aaec2b1dSChao Yu {
1485aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1486aaec2b1dSChao Yu 
1487aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
148825ca923bSJaegeuk Kim 	ckpt_flags |= f;
148925ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
149025ca923bSJaegeuk Kim }
149125ca923bSJaegeuk Kim 
1492aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
149325ca923bSJaegeuk Kim {
1494d1aa2453SChao Yu 	unsigned long flags;
1495d1aa2453SChao Yu 
1496d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1497aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1498d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1499aaec2b1dSChao Yu }
1500aaec2b1dSChao Yu 
1501aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1502aaec2b1dSChao Yu {
1503aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1504aaec2b1dSChao Yu 
1505aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
150625ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
150725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
150825ca923bSJaegeuk Kim }
150925ca923bSJaegeuk Kim 
1510aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1511aaec2b1dSChao Yu {
1512d1aa2453SChao Yu 	unsigned long flags;
1513d1aa2453SChao Yu 
1514d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1515aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1516d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1517aaec2b1dSChao Yu }
1518aaec2b1dSChao Yu 
151922ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
152022ad0b6aSJaegeuk Kim {
1521d1aa2453SChao Yu 	unsigned long flags;
1522d1aa2453SChao Yu 
152322ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
152422ad0b6aSJaegeuk Kim 
152522ad0b6aSJaegeuk Kim 	if (lock)
1526d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
152722ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
152822ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
152922ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
153022ad0b6aSJaegeuk Kim 	if (lock)
1531d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
153222ad0b6aSJaegeuk Kim }
153322ad0b6aSJaegeuk Kim 
153422ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
153522ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
153622ad0b6aSJaegeuk Kim {
153722ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
153822ad0b6aSJaegeuk Kim 
1539c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
154022ad0b6aSJaegeuk Kim }
154122ad0b6aSJaegeuk Kim 
1542e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
154339a53e0cSJaegeuk Kim {
1544b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
154539a53e0cSJaegeuk Kim }
154639a53e0cSJaegeuk Kim 
1547cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1548cc15620bSJaegeuk Kim {
1549cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1550cc15620bSJaegeuk Kim }
1551cc15620bSJaegeuk Kim 
1552e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
155339a53e0cSJaegeuk Kim {
1554b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
155539936837SJaegeuk Kim }
155639936837SJaegeuk Kim 
1557e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
155839936837SJaegeuk Kim {
1559b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
156039936837SJaegeuk Kim }
156139936837SJaegeuk Kim 
1562e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
156339936837SJaegeuk Kim {
1564b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
156539a53e0cSJaegeuk Kim }
156639a53e0cSJaegeuk Kim 
1567119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1568119ee914SJaegeuk Kim {
1569119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1570119ee914SJaegeuk Kim 
1571119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1572119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1573119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1574119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1575119ee914SJaegeuk Kim 	return reason;
1576119ee914SJaegeuk Kim }
1577119ee914SJaegeuk Kim 
1578119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1579119ee914SJaegeuk Kim {
1580c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1581119ee914SJaegeuk Kim }
1582119ee914SJaegeuk Kim 
1583119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1584119ee914SJaegeuk Kim {
1585aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1586aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1587119ee914SJaegeuk Kim }
1588119ee914SJaegeuk Kim 
158939a53e0cSJaegeuk Kim /*
159039a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
159139a53e0cSJaegeuk Kim  */
1592064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
159339a53e0cSJaegeuk Kim {
1594d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1595d6b7d4b3SChao Yu 		return -EINVAL;
1596cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1597064e0823SNamjae Jeon 		return -EINVAL;
1598064e0823SNamjae Jeon 	return 0;
159939a53e0cSJaegeuk Kim }
160039a53e0cSJaegeuk Kim 
160139a53e0cSJaegeuk Kim /*
160239a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
160339a53e0cSJaegeuk Kim  */
160439a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
160539a53e0cSJaegeuk Kim {
16060eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16070eb0adadSChao Yu 
1608000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
160939a53e0cSJaegeuk Kim }
161039a53e0cSJaegeuk Kim 
16114bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16124bc8e9bcSChao Yu {
16134bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16144bc8e9bcSChao Yu }
16154bc8e9bcSChao Yu 
1616d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1617d8a9a229SJaegeuk Kim 					struct inode *inode)
16187c2e5963SJaegeuk Kim {
1619d8a9a229SJaegeuk Kim 	if (!inode)
1620d8a9a229SJaegeuk Kim 		return true;
16217c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16227c2e5963SJaegeuk Kim 		return false;
1623d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1624d8a9a229SJaegeuk Kim 		return true;
162563189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
16267c2e5963SJaegeuk Kim 		return true;
162763189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
162863189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
16297c2e5963SJaegeuk Kim 		return true;
1630162b27aeSJaegeuk Kim 	if (capable(CAP_SYS_RESOURCE))
1631162b27aeSJaegeuk Kim 		return true;
16327c2e5963SJaegeuk Kim 	return false;
16337c2e5963SJaegeuk Kim }
16347c2e5963SJaegeuk Kim 
16350abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
16360abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
163746008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
163839a53e0cSJaegeuk Kim {
16390abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1640daeb433eSChao Yu 	block_t avail_user_block_count;
16410abd675eSChao Yu 	int ret;
16420abd675eSChao Yu 
16430abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16440abd675eSChao Yu 	if (ret)
16450abd675eSChao Yu 		return ret;
1646dd11a5dfSJaegeuk Kim 
1647cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
164855523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
164955523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16500abd675eSChao Yu 		release = *count;
16510abd675eSChao Yu 		goto enospc;
165255523519SChao Yu 	}
1653cb78942bSJaegeuk Kim #endif
1654dd11a5dfSJaegeuk Kim 	/*
1655dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1656dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1657dd11a5dfSJaegeuk Kim 	 */
1658dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1659cfb271d4SChao Yu 
166039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16612555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
166280d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
166380d42145SYunlong Song 					sbi->current_reserved_blocks;
16647e65be49SJaegeuk Kim 
1665d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
166663189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
16677e65be49SJaegeuk Kim 
1668daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1669daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16707e65be49SJaegeuk Kim 		if (diff > *count)
16717e65be49SJaegeuk Kim 			diff = *count;
1672dd11a5dfSJaegeuk Kim 		*count -= diff;
16730abd675eSChao Yu 		release = diff;
16747e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
167546008c6dSChao Yu 		if (!*count) {
167639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1677dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16780abd675eSChao Yu 			goto enospc;
167939a53e0cSJaegeuk Kim 		}
168046008c6dSChao Yu 	}
168139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
168241382ec4SJaegeuk Kim 
1683fab2adeeSLiFan 	if (unlikely(release))
16840abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16850abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16860abd675eSChao Yu 	return 0;
16870abd675eSChao Yu 
16880abd675eSChao Yu enospc:
16890abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16900abd675eSChao Yu 	return -ENOSPC;
169139a53e0cSJaegeuk Kim }
169239a53e0cSJaegeuk Kim 
1693da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
169439a53e0cSJaegeuk Kim 						struct inode *inode,
16950eb0adadSChao Yu 						block_t count)
169639a53e0cSJaegeuk Kim {
16970eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
16980eb0adadSChao Yu 
169939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17009850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17010eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
170239a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
170380d42145SYunlong Song 	if (sbi->reserved_blocks &&
170480d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
170580d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
170680d42145SYunlong Song 					sbi->current_reserved_blocks + count);
170739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17080abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
170939a53e0cSJaegeuk Kim }
171039a53e0cSJaegeuk Kim 
171139a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
171239a53e0cSJaegeuk Kim {
171335782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17147c4abcbeSChao Yu 
171536951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
171636951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17177c4abcbeSChao Yu 		return;
17187c4abcbeSChao Yu 
1719caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
172039a53e0cSJaegeuk Kim }
172139a53e0cSJaegeuk Kim 
1722a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
172339a53e0cSJaegeuk Kim {
1724204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1725c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1726c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17272c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17282c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
172939a53e0cSJaegeuk Kim }
173039a53e0cSJaegeuk Kim 
173139a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
173239a53e0cSJaegeuk Kim {
173335782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
173439a53e0cSJaegeuk Kim }
173539a53e0cSJaegeuk Kim 
1736a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
173739a53e0cSJaegeuk Kim {
17385ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17395ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17401fe54f9dSJaegeuk Kim 		return;
17411fe54f9dSJaegeuk Kim 
1742204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1743c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1744c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17452c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17462c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
174739a53e0cSJaegeuk Kim }
174839a53e0cSJaegeuk Kim 
1749523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
175039a53e0cSJaegeuk Kim {
175135782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
175239a53e0cSJaegeuk Kim }
175339a53e0cSJaegeuk Kim 
1754204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1755f8b2c1f9SJaegeuk Kim {
1756204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1757f8b2c1f9SJaegeuk Kim }
1758f8b2c1f9SJaegeuk Kim 
17595ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17605ac206cfSNamjae Jeon {
17613519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1762523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1763523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1764523be8a6SJaegeuk Kim 
1765523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17665ac206cfSNamjae Jeon }
17675ac206cfSNamjae Jeon 
176839a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
176939a53e0cSJaegeuk Kim {
17708b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
177139a53e0cSJaegeuk Kim }
177239a53e0cSJaegeuk Kim 
1773f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1774f83a2584SYunlei He {
1775f83a2584SYunlei He 	return sbi->discard_blks;
1776f83a2584SYunlei He }
1777f83a2584SYunlei He 
177839a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
177939a53e0cSJaegeuk Kim {
178039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
178139a53e0cSJaegeuk Kim 
178239a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
178339a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
178439a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
178539a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
178639a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
178739a53e0cSJaegeuk Kim 
178839a53e0cSJaegeuk Kim 	return 0;
178939a53e0cSJaegeuk Kim }
179039a53e0cSJaegeuk Kim 
179155141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
179255141486SWanpeng Li {
179355141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
179455141486SWanpeng Li }
179555141486SWanpeng Li 
179639a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
179739a53e0cSJaegeuk Kim {
179839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
17991dbe4152SChangman Lee 	int offset;
18001dbe4152SChangman Lee 
1801199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1802199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1803199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1804199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1805199bc3feSChao Yu 	}
1806199bc3feSChao Yu 
180755141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18081dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18091dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18101dbe4152SChangman Lee 		else
181165b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18121dbe4152SChangman Lee 	} else {
18131dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
181425ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
181539a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
181639a53e0cSJaegeuk Kim 	}
18171dbe4152SChangman Lee }
181839a53e0cSJaegeuk Kim 
181939a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
182039a53e0cSJaegeuk Kim {
18218508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
182239a53e0cSJaegeuk Kim 
18238508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
182439a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
182539a53e0cSJaegeuk Kim 	return start_addr;
182639a53e0cSJaegeuk Kim }
182739a53e0cSJaegeuk Kim 
18288508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
18298508e44aSJaegeuk Kim {
18308508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
18318508e44aSJaegeuk Kim 
18328508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
18338508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
18348508e44aSJaegeuk Kim 	return start_addr;
18358508e44aSJaegeuk Kim }
18368508e44aSJaegeuk Kim 
18378508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
18388508e44aSJaegeuk Kim {
18398508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
18408508e44aSJaegeuk Kim }
18418508e44aSJaegeuk Kim 
184239a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
184339a53e0cSJaegeuk Kim {
184439a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
184539a53e0cSJaegeuk Kim }
184639a53e0cSJaegeuk Kim 
18470abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1848000519f2SChao Yu 					struct inode *inode, bool is_inode)
184939a53e0cSJaegeuk Kim {
185039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
185139a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18520abd675eSChao Yu 	bool quota = inode && !is_inode;
18530abd675eSChao Yu 
18540abd675eSChao Yu 	if (quota) {
18550abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18560abd675eSChao Yu 		if (ret)
18570abd675eSChao Yu 			return ret;
18580abd675eSChao Yu 	}
185939a53e0cSJaegeuk Kim 
1860812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1861812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1862812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1863812c6056SChao Yu 		goto enospc;
1864812c6056SChao Yu 	}
1865812c6056SChao Yu #endif
1866812c6056SChao Yu 
186739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
186839a53e0cSJaegeuk Kim 
18697e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18707e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18717e65be49SJaegeuk Kim 
1872d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
187363189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
18747e65be49SJaegeuk Kim 
18757e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
187639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18770abd675eSChao Yu 		goto enospc;
187839a53e0cSJaegeuk Kim 	}
187939a53e0cSJaegeuk Kim 
1880ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1881cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
188239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18830abd675eSChao Yu 		goto enospc;
188439a53e0cSJaegeuk Kim 	}
188539a53e0cSJaegeuk Kim 
1886ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1887ef86d709SGu Zheng 	sbi->total_valid_block_count++;
188839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
188939a53e0cSJaegeuk Kim 
18900abd675eSChao Yu 	if (inode) {
18910abd675eSChao Yu 		if (is_inode)
18920abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
18930abd675eSChao Yu 		else
18940abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
18950abd675eSChao Yu 	}
18960abd675eSChao Yu 
189741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
18980abd675eSChao Yu 	return 0;
18990abd675eSChao Yu 
19000abd675eSChao Yu enospc:
19010abd675eSChao Yu 	if (quota)
19020abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
19030abd675eSChao Yu 	return -ENOSPC;
190439a53e0cSJaegeuk Kim }
190539a53e0cSJaegeuk Kim 
190639a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1907000519f2SChao Yu 					struct inode *inode, bool is_inode)
190839a53e0cSJaegeuk Kim {
190939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
191039a53e0cSJaegeuk Kim 
19119850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
19129850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1913000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
191439a53e0cSJaegeuk Kim 
1915ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1916ef86d709SGu Zheng 	sbi->total_valid_block_count--;
191780d42145SYunlong Song 	if (sbi->reserved_blocks &&
191880d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
191980d42145SYunlong Song 		sbi->current_reserved_blocks++;
192039a53e0cSJaegeuk Kim 
192139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19220abd675eSChao Yu 
19230abd675eSChao Yu 	if (!is_inode)
19240abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
192539a53e0cSJaegeuk Kim }
192639a53e0cSJaegeuk Kim 
192739a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
192839a53e0cSJaegeuk Kim {
19298b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
193039a53e0cSJaegeuk Kim }
193139a53e0cSJaegeuk Kim 
193239a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
193339a53e0cSJaegeuk Kim {
1934513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
193539a53e0cSJaegeuk Kim }
193639a53e0cSJaegeuk Kim 
19370e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
193839a53e0cSJaegeuk Kim {
1939513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
194039a53e0cSJaegeuk Kim }
194139a53e0cSJaegeuk Kim 
1942513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
194339a53e0cSJaegeuk Kim {
1944513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
194539a53e0cSJaegeuk Kim }
194639a53e0cSJaegeuk Kim 
1947a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1948a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1949a56c7c6fSJaegeuk Kim {
1950c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1951c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1952cac5a3d8SDongOh Shin 
1953c41f3cc3SJaegeuk Kim 	if (page)
1954c41f3cc3SJaegeuk Kim 		return page;
1955c41f3cc3SJaegeuk Kim 
195655523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
195755523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1958c41f3cc3SJaegeuk Kim 		return NULL;
195955523519SChao Yu 	}
1960c41f3cc3SJaegeuk Kim #endif
1961a56c7c6fSJaegeuk Kim 	if (!for_write)
1962a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1963a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1964a56c7c6fSJaegeuk Kim }
1965a56c7c6fSJaegeuk Kim 
196601eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
196701eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
196801eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
196901eccef7SChao Yu {
197001eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
197101eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
197201eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
197301eccef7SChao Yu 		return NULL;
197401eccef7SChao Yu 	}
197501eccef7SChao Yu #endif
197601eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
197701eccef7SChao Yu }
197801eccef7SChao Yu 
19796e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19806e2c64adSJaegeuk Kim {
19816e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19826e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19836e2c64adSJaegeuk Kim 
19846e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19856e2c64adSJaegeuk Kim 	kunmap(dst);
19866e2c64adSJaegeuk Kim 	kunmap(src);
19876e2c64adSJaegeuk Kim }
19886e2c64adSJaegeuk Kim 
198939a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
199039a53e0cSJaegeuk Kim {
1991031fa8ccSJaegeuk Kim 	if (!page)
199239a53e0cSJaegeuk Kim 		return;
199339a53e0cSJaegeuk Kim 
199439a53e0cSJaegeuk Kim 	if (unlock) {
19959850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
199639a53e0cSJaegeuk Kim 		unlock_page(page);
199739a53e0cSJaegeuk Kim 	}
199809cbfeafSKirill A. Shutemov 	put_page(page);
199939a53e0cSJaegeuk Kim }
200039a53e0cSJaegeuk Kim 
200139a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
200239a53e0cSJaegeuk Kim {
200339a53e0cSJaegeuk Kim 	if (dn->node_page)
200439a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
200539a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
200639a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
200739a53e0cSJaegeuk Kim 	dn->node_page = NULL;
200839a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
200939a53e0cSJaegeuk Kim }
201039a53e0cSJaegeuk Kim 
201139a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2012e8512d2eSGu Zheng 					size_t size)
201339a53e0cSJaegeuk Kim {
2014e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
201539a53e0cSJaegeuk Kim }
201639a53e0cSJaegeuk Kim 
20177bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20187bd59381SGu Zheng 						gfp_t flags)
20197bd59381SGu Zheng {
20207bd59381SGu Zheng 	void *entry;
20217bd59381SGu Zheng 
202280c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
202380c54505SJaegeuk Kim 	if (!entry)
202480c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
20257bd59381SGu Zheng 	return entry;
20267bd59381SGu Zheng }
20277bd59381SGu Zheng 
2028d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2029d62fe971SChao Yu 						int npages, bool no_fail)
2030740432f8SJaegeuk Kim {
2031740432f8SJaegeuk Kim 	struct bio *bio;
2032740432f8SJaegeuk Kim 
2033d62fe971SChao Yu 	if (no_fail) {
2034740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2035740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
203680c54505SJaegeuk Kim 		if (!bio)
203780c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2038740432f8SJaegeuk Kim 		return bio;
2039740432f8SJaegeuk Kim 	}
2040d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2041d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2042d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2043d62fe971SChao Yu 		return NULL;
2044d62fe971SChao Yu 	}
2045d62fe971SChao Yu #endif
2046d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2047d62fe971SChao Yu }
2048740432f8SJaegeuk Kim 
20499be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20509be32d72SJaegeuk Kim 				unsigned long index, void *item)
20519be32d72SJaegeuk Kim {
20529be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20539be32d72SJaegeuk Kim 		cond_resched();
20549be32d72SJaegeuk Kim }
20559be32d72SJaegeuk Kim 
205639a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
205739a53e0cSJaegeuk Kim 
205839a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
205939a53e0cSJaegeuk Kim {
206045590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2061cac5a3d8SDongOh Shin 
206239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
206339a53e0cSJaegeuk Kim }
206439a53e0cSJaegeuk Kim 
20657a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20667a2af766SChao Yu {
20677a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20687a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20697a2af766SChao Yu }
20707a2af766SChao Yu 
207139a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
207239a53e0cSJaegeuk Kim {
207339a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
207439a53e0cSJaegeuk Kim }
207539a53e0cSJaegeuk Kim 
20767a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20777a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20787a2af766SChao Yu 			struct page *node_page, unsigned int offset)
207939a53e0cSJaegeuk Kim {
208039a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
208139a53e0cSJaegeuk Kim 	__le32 *addr_array;
20827a2af766SChao Yu 	int base = 0;
20837a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2084cac5a3d8SDongOh Shin 
208545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20867a2af766SChao Yu 
20877a2af766SChao Yu 	/* from GC path only */
2088979f492fSLiFan 	if (is_inode) {
2089979f492fSLiFan 		if (!inode)
20907a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2091979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20927a2af766SChao Yu 			base = get_extra_isize(inode);
20937a2af766SChao Yu 	}
20947a2af766SChao Yu 
209539a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
20967a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
209739a53e0cSJaegeuk Kim }
209839a53e0cSJaegeuk Kim 
209939a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
210039a53e0cSJaegeuk Kim {
210139a53e0cSJaegeuk Kim 	int mask;
210239a53e0cSJaegeuk Kim 
210339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
210439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
210539a53e0cSJaegeuk Kim 	return mask & *addr;
210639a53e0cSJaegeuk Kim }
210739a53e0cSJaegeuk Kim 
2108a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2109a66cdd98SJaegeuk Kim {
2110a66cdd98SJaegeuk Kim 	int mask;
2111a66cdd98SJaegeuk Kim 
2112a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2113a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2114a66cdd98SJaegeuk Kim 	*addr |= mask;
2115a66cdd98SJaegeuk Kim }
2116a66cdd98SJaegeuk Kim 
2117a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2118a66cdd98SJaegeuk Kim {
2119a66cdd98SJaegeuk Kim 	int mask;
2120a66cdd98SJaegeuk Kim 
2121a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2122a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2123a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2124a66cdd98SJaegeuk Kim }
2125a66cdd98SJaegeuk Kim 
212652aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
212739a53e0cSJaegeuk Kim {
212839a53e0cSJaegeuk Kim 	int mask;
212939a53e0cSJaegeuk Kim 	int ret;
213039a53e0cSJaegeuk Kim 
213139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
213239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
213339a53e0cSJaegeuk Kim 	ret = mask & *addr;
213439a53e0cSJaegeuk Kim 	*addr |= mask;
213539a53e0cSJaegeuk Kim 	return ret;
213639a53e0cSJaegeuk Kim }
213739a53e0cSJaegeuk Kim 
213852aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
213939a53e0cSJaegeuk Kim {
214039a53e0cSJaegeuk Kim 	int mask;
214139a53e0cSJaegeuk Kim 	int ret;
214239a53e0cSJaegeuk Kim 
214339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
214439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
214539a53e0cSJaegeuk Kim 	ret = mask & *addr;
214639a53e0cSJaegeuk Kim 	*addr &= ~mask;
214739a53e0cSJaegeuk Kim 	return ret;
214839a53e0cSJaegeuk Kim }
214939a53e0cSJaegeuk Kim 
2150c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2151c6ac4c0eSGu Zheng {
2152c6ac4c0eSGu Zheng 	int mask;
2153c6ac4c0eSGu Zheng 
2154c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2155c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2156c6ac4c0eSGu Zheng 	*addr ^= mask;
2157c6ac4c0eSGu Zheng }
2158c6ac4c0eSGu Zheng 
21595c57132eSChao Yu #define F2FS_REG_FLMASK		(~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
21605c57132eSChao Yu #define F2FS_OTHER_FLMASK	(FS_NODUMP_FL | FS_NOATIME_FL)
21615c57132eSChao Yu #define F2FS_FL_INHERITED	(FS_PROJINHERIT_FL)
21625c57132eSChao Yu 
21635c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
21645c57132eSChao Yu {
21655c57132eSChao Yu 	if (S_ISDIR(mode))
21665c57132eSChao Yu 		return flags;
21675c57132eSChao Yu 	else if (S_ISREG(mode))
21685c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
21695c57132eSChao Yu 	else
21705c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
21715c57132eSChao Yu }
21725c57132eSChao Yu 
217339a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
217439a53e0cSJaegeuk Kim enum {
217539a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2176b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
217726de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2178ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
217939a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
218039a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
218139a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2182c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2183c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2184444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
21851001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
218634d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2187fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2188fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
218988b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
219088b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
21915fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
219202a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
21933c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
21941e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2195b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2196510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2197d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2198c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2199dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2200ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
22017a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
22025c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
22031ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
220439a53e0cSJaegeuk Kim };
220539a53e0cSJaegeuk Kim 
2206205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2207205b9822SJaegeuk Kim 						int flag, bool set)
220839a53e0cSJaegeuk Kim {
2209205b9822SJaegeuk Kim 	switch (flag) {
2210205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2211205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2212205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
22139ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2214205b9822SJaegeuk Kim 		if (set)
2215205b9822SJaegeuk Kim 			return;
2216205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2217205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
22181ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
22197c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2220205b9822SJaegeuk Kim 	}
222139a53e0cSJaegeuk Kim }
222239a53e0cSJaegeuk Kim 
222391942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
222439a53e0cSJaegeuk Kim {
222591942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
222691942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2227205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
222839a53e0cSJaegeuk Kim }
222939a53e0cSJaegeuk Kim 
223091942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
223139a53e0cSJaegeuk Kim {
223291942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
223339a53e0cSJaegeuk Kim }
223439a53e0cSJaegeuk Kim 
223591942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
223639a53e0cSJaegeuk Kim {
223791942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
223891942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2239205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
224039a53e0cSJaegeuk Kim }
224139a53e0cSJaegeuk Kim 
224291942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2243444c580fSJaegeuk Kim {
224491942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
224591942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
22467c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
224739a53e0cSJaegeuk Kim }
224839a53e0cSJaegeuk Kim 
2249a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2250a1961246SJaegeuk Kim {
2251a1961246SJaegeuk Kim 	if (inc)
2252a1961246SJaegeuk Kim 		inc_nlink(inode);
2253a1961246SJaegeuk Kim 	else
2254a1961246SJaegeuk Kim 		drop_nlink(inode);
22557c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2256a1961246SJaegeuk Kim }
2257a1961246SJaegeuk Kim 
22588edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
22590abd675eSChao Yu 					block_t diff, bool add, bool claim)
22608edd03c8SJaegeuk Kim {
226126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
226226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
226326de9b11SJaegeuk Kim 
22640abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
22650abd675eSChao Yu 	if (add) {
22660abd675eSChao Yu 		if (claim)
22670abd675eSChao Yu 			dquot_claim_block(inode, diff);
22680abd675eSChao Yu 		else
22690abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
22700abd675eSChao Yu 	} else {
22710abd675eSChao Yu 		dquot_free_block(inode, diff);
22720abd675eSChao Yu 	}
22730abd675eSChao Yu 
22747c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
227526de9b11SJaegeuk Kim 	if (clean || recover)
227626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
22778edd03c8SJaegeuk Kim }
22788edd03c8SJaegeuk Kim 
2279fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2280fc9581c8SJaegeuk Kim {
228126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
228226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
228326de9b11SJaegeuk Kim 
2284fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2285fc9581c8SJaegeuk Kim 		return;
2286fc9581c8SJaegeuk Kim 
2287fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
22887c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
228926de9b11SJaegeuk Kim 	if (clean || recover)
229026de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
229126de9b11SJaegeuk Kim }
229226de9b11SJaegeuk Kim 
2293205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2294205b9822SJaegeuk Kim {
2295205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
22967c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2297205b9822SJaegeuk Kim }
2298205b9822SJaegeuk Kim 
22991ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
23001ad71a27SJaegeuk Kim 					unsigned int count)
23011ad71a27SJaegeuk Kim {
23021ad71a27SJaegeuk Kim 	F2FS_I(inode)->i_gc_failures = count;
23031ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
23041ad71a27SJaegeuk Kim }
23051ad71a27SJaegeuk Kim 
2306205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2307205b9822SJaegeuk Kim {
2308205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
23097c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2310205b9822SJaegeuk Kim }
2311205b9822SJaegeuk Kim 
2312205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2313205b9822SJaegeuk Kim {
2314205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
23157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2316205b9822SJaegeuk Kim }
2317205b9822SJaegeuk Kim 
231891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2319444c580fSJaegeuk Kim {
2320205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2321205b9822SJaegeuk Kim 
2322444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2323205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
23241001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2325205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
232634d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2327205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2328b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2329205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2330510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2331205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
23327a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
23337a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
23341ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
23351ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2336444c580fSJaegeuk Kim }
2337444c580fSJaegeuk Kim 
233891942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2339444c580fSJaegeuk Kim {
2340444c580fSJaegeuk Kim 	ri->i_inline = 0;
2341444c580fSJaegeuk Kim 
234291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2343444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
234491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
23451001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
234691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
234734d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
234891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2349b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
235091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2351510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
23527a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
23537a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
23541ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
23551ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
23567a2af766SChao Yu }
23577a2af766SChao Yu 
23587a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
23597a2af766SChao Yu {
23607a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2361444c580fSJaegeuk Kim }
2362444c580fSJaegeuk Kim 
2363987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2364987c7c31SChao Yu {
236591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2366987c7c31SChao Yu }
2367987c7c31SChao Yu 
236881ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2369de93653fSJaegeuk Kim {
2370b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2371de93653fSJaegeuk Kim }
2372de93653fSJaegeuk Kim 
23736afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
237465985d93SJaegeuk Kim {
2375695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2376cac5a3d8SDongOh Shin 
237765985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2378b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
237965985d93SJaegeuk Kim }
238065985d93SJaegeuk Kim 
238165985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
238265985d93SJaegeuk Kim {
23836afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
238465985d93SJaegeuk Kim }
238565985d93SJaegeuk Kim 
23860dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
23870dbdc2aeSJaegeuk Kim {
238891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
23890dbdc2aeSJaegeuk Kim }
23900dbdc2aeSJaegeuk Kim 
2391b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2392b3d208f9SJaegeuk Kim {
239391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2394b3d208f9SJaegeuk Kim }
2395b3d208f9SJaegeuk Kim 
2396510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2397510022a8SJaegeuk Kim {
239891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2399510022a8SJaegeuk Kim }
2400510022a8SJaegeuk Kim 
24011ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
24021ad71a27SJaegeuk Kim {
24031ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
24041ad71a27SJaegeuk Kim }
24051ad71a27SJaegeuk Kim 
240688b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
240788b88a66SJaegeuk Kim {
240891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
240988b88a66SJaegeuk Kim }
241088b88a66SJaegeuk Kim 
24115fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
24125fe45743SChao Yu {
24135fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
24145fe45743SChao Yu }
24155fe45743SChao Yu 
241602a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
241702a1335fSJaegeuk Kim {
241891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
241902a1335fSJaegeuk Kim }
242002a1335fSJaegeuk Kim 
24213c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
24223c6c2bebSJaegeuk Kim {
242391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
24243c6c2bebSJaegeuk Kim }
24253c6c2bebSJaegeuk Kim 
24261e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
24271e84371fSJaegeuk Kim {
242891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
24291e84371fSJaegeuk Kim }
24301e84371fSJaegeuk Kim 
2431f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
24321001b347SHuajun Li {
2433695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
24347a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2435cac5a3d8SDongOh Shin 
24367a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
24371001b347SHuajun Li }
24381001b347SHuajun Li 
243934d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
244034d67debSChao Yu {
244191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
244234d67debSChao Yu }
244334d67debSChao Yu 
2444b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2445b5492af7SJaegeuk Kim {
2446b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2447b5492af7SJaegeuk Kim }
2448b5492af7SJaegeuk Kim 
2449b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2450b5492af7SJaegeuk Kim {
2451b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
24527c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2453b5492af7SJaegeuk Kim }
2454b5492af7SJaegeuk Kim 
2455b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2456b5492af7SJaegeuk Kim {
2457b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
24587c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2459b5492af7SJaegeuk Kim }
2460b5492af7SJaegeuk Kim 
246126787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
246226787236SJaegeuk Kim {
2463a0d00fadSChao Yu 	bool ret;
2464a0d00fadSChao Yu 
246526787236SJaegeuk Kim 	if (dsync) {
246626787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
246726787236SJaegeuk Kim 
246826787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
246926787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
247026787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
247126787236SJaegeuk Kim 		return ret;
247226787236SJaegeuk Kim 	}
247326787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
247426787236SJaegeuk Kim 			file_keep_isize(inode) ||
2475235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
247626787236SJaegeuk Kim 		return false;
2477a0d00fadSChao Yu 
2478214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2479214c2461SJaegeuk Kim 		return false;
2480214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2481214c2461SJaegeuk Kim 		return false;
2482214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2483214c2461SJaegeuk Kim 		return false;
2484214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2485214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2486214c2461SJaegeuk Kim 		return false;
2487214c2461SJaegeuk Kim 
2488a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2489a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2490a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2491a0d00fadSChao Yu 
2492a0d00fadSChao Yu 	return ret;
2493267378d4SChao Yu }
2494267378d4SChao Yu 
2495deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
249677888c1eSJaegeuk Kim {
2497deeedd71SChao Yu 	return sb_rdonly(sb);
249877888c1eSJaegeuk Kim }
249977888c1eSJaegeuk Kim 
2500744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2501744602cfSJaegeuk Kim {
2502aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2503744602cfSJaegeuk Kim }
2504744602cfSJaegeuk Kim 
2505eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2506eaa693f4SJaegeuk Kim {
2507eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2508eaa693f4SJaegeuk Kim 		return true;
2509eaa693f4SJaegeuk Kim 
2510eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2511eaa693f4SJaegeuk Kim 		return true;
2512eaa693f4SJaegeuk Kim 
2513eaa693f4SJaegeuk Kim 	return false;
2514eaa693f4SJaegeuk Kim }
2515eaa693f4SJaegeuk Kim 
25163e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
25173e72f721SJaegeuk Kim {
25183e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
251991942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
25203e72f721SJaegeuk Kim 		return false;
25213e72f721SJaegeuk Kim 
2522886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
25233e72f721SJaegeuk Kim }
25243e72f721SJaegeuk Kim 
25251ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
25261ecc0c5cSChao Yu 					size_t size, gfp_t flags)
25270414b004SJaegeuk Kim {
25282c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
252955523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
253055523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
25312c63feadSJaegeuk Kim 		return NULL;
253255523519SChao Yu 	}
25332c63feadSJaegeuk Kim #endif
25340414b004SJaegeuk Kim 	return kmalloc(size, flags);
25350414b004SJaegeuk Kim }
25360414b004SJaegeuk Kim 
2537acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2538acbf054dSChao Yu 					size_t size, gfp_t flags)
2539acbf054dSChao Yu {
2540acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2541acbf054dSChao Yu }
2542acbf054dSChao Yu 
2543628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2544628b3d14SChao Yu 					size_t size, gfp_t flags)
2545628b3d14SChao Yu {
2546628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2547628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2548628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2549628b3d14SChao Yu 		return NULL;
2550628b3d14SChao Yu 	}
2551628b3d14SChao Yu #endif
2552628b3d14SChao Yu 	return kvmalloc(size, flags);
2553628b3d14SChao Yu }
2554628b3d14SChao Yu 
2555628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2556628b3d14SChao Yu 					size_t size, gfp_t flags)
2557628b3d14SChao Yu {
2558628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2559628b3d14SChao Yu }
2560628b3d14SChao Yu 
25617a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2562f2470371SChao Yu {
25637a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2564f2470371SChao Yu }
2565f2470371SChao Yu 
25666afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
25676afc662eSChao Yu {
25686afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
25696afc662eSChao Yu }
25706afc662eSChao Yu 
2571a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
257291942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2573a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2574a6dda0e6SChristoph Hellwig 
25757a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
25767a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
25777a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
25787a2af766SChao Yu 
25795c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
25805c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
25815c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
25825c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
25835c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
25845c57132eSChao Yu 
2585b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2586b0af6d49SChao Yu {
2587b0af6d49SChao Yu 	int i;
2588b0af6d49SChao Yu 
2589b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2590b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2591b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2592b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2593b0af6d49SChao Yu }
2594b0af6d49SChao Yu 
2595b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2596b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2597b0af6d49SChao Yu {
2598b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2599b0af6d49SChao Yu 		return;
2600b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2601b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2602b0af6d49SChao Yu 
2603b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2604b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2605b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2606b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2607b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2608b0af6d49SChao Yu }
2609b0af6d49SChao Yu 
261039a53e0cSJaegeuk Kim /*
261139a53e0cSJaegeuk Kim  * file.c
261239a53e0cSJaegeuk Kim  */
2613cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2614cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2615cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2616cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2617a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2618a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2619cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2620cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2621f652e9d9SChao Yu void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2622c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2623cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2624cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
26251ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
262639a53e0cSJaegeuk Kim 
262739a53e0cSJaegeuk Kim /*
262839a53e0cSJaegeuk Kim  * inode.c
262939a53e0cSJaegeuk Kim  */
2630cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2631704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2632704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2633cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2634cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2635cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2636211a6fa0SYunlei He void update_inode(struct inode *inode, struct page *node_page);
2637211a6fa0SYunlei He void update_inode_page(struct inode *inode);
2638cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2639cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2640cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
264139a53e0cSJaegeuk Kim 
264239a53e0cSJaegeuk Kim /*
264339a53e0cSJaegeuk Kim  * namei.c
264439a53e0cSJaegeuk Kim  */
2645846ae671SChao Yu int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2646b6a06cbbSChao Yu 							bool hot, bool set);
264739a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
264839a53e0cSJaegeuk Kim 
264939a53e0cSJaegeuk Kim /*
265039a53e0cSJaegeuk Kim  * dir.c
265139a53e0cSJaegeuk Kim  */
2652cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2653cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2654cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2655cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2656cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2657cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2658cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2659cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2660cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2661cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2662cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2663cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2664cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2665cac5a3d8SDongOh Shin 			unsigned int current_depth);
2666cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2667cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2668cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2669cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2670cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2671cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2672cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2673cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2674cac5a3d8SDongOh Shin 			struct page **page);
2675cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2676cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2677cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2678cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2679cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2680cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2681cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2682cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2683cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2684cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2685cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2686cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2687cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2688cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2689cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2690cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
269139a53e0cSJaegeuk Kim 
2692b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2693b7f7a5e0SAl Viro {
26942b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2695510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2696b7f7a5e0SAl Viro }
2697b7f7a5e0SAl Viro 
269839a53e0cSJaegeuk Kim /*
269939a53e0cSJaegeuk Kim  * super.c
270039a53e0cSJaegeuk Kim  */
2701cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2702cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2703ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
27044b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2705cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2706cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2707a07ef784SNamjae Jeon extern __printf(3, 4)
2708cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2709984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
271039a53e0cSJaegeuk Kim 
271139a53e0cSJaegeuk Kim /*
271239a53e0cSJaegeuk Kim  * hash.c
271339a53e0cSJaegeuk Kim  */
27146332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
27156332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
271639a53e0cSJaegeuk Kim 
271739a53e0cSJaegeuk Kim /*
271839a53e0cSJaegeuk Kim  * node.c
271939a53e0cSJaegeuk Kim  */
272039a53e0cSJaegeuk Kim struct dnode_of_data;
272139a53e0cSJaegeuk Kim struct node_info;
272239a53e0cSJaegeuk Kim 
2723cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2724cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2725cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2726cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2727cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2728cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2729cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2730cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
27319c77f754SJaegeuk Kim int truncate_xattr_node(struct inode *inode);
2732cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2733cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2734cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
27355f4ce6abSYunlei He struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2736cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2737cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2738cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2739cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2740cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2741cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2742401db79fSYunlong Song int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2743b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
274422ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2745cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2746cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2747cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2748cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2749cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2750e17d488bSSheng Yong int recover_xattr_data(struct inode *inode, struct page *page);
2751cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2752c376fc0fSYunlei He void restore_node_summary(struct f2fs_sb_info *sbi,
2753cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
275422ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2755cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2756cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
27576e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
275839a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
275939a53e0cSJaegeuk Kim 
276039a53e0cSJaegeuk Kim /*
276139a53e0cSJaegeuk Kim  * segment.c
276239a53e0cSJaegeuk Kim  */
2763b3a97a2aSJaegeuk Kim bool need_SSR(struct f2fs_sb_info *sbi);
2764cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
276557864ae5SJaegeuk Kim void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
2766cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
27678c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2768cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2769cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2770cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
277139d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2772cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
27731228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2774cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2775cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2776cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
277778997b56SChao Yu void init_discard_policy(struct discard_policy *dpolicy, int discard_type,
277878997b56SChao Yu 						unsigned int granularity);
27797950e9acSChao Yu void drop_discard_cmd(struct f2fs_sb_info *sbi);
2780cce13252SChao Yu void stop_discard_thread(struct f2fs_sb_info *sbi);
2781cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2782cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2783cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2784cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2785cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2786cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2787cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2788cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2789cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2790b0af6d49SChao Yu void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2791b0af6d49SChao Yu 						enum iostat_type io_type);
2792cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2793cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2794d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2795cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2796cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2797cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2798cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2799cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2800cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2801cac5a3d8SDongOh Shin 			bool recover_newaddr);
2802cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2803cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2804fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2805fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2806cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2807cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2808d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2809cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2810cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2811cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2812cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2813cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2814cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2815cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
28167fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
28177fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
2818d5097be5SHyunchul Lee int rw_hint_to_seg_type(enum rw_hint hint);
28190cdd3195SHyunchul Lee enum rw_hint io_type_to_rw_hint(struct f2fs_sb_info *sbi, enum page_type type,
28200cdd3195SHyunchul Lee 				enum temp_type temp);
282139a53e0cSJaegeuk Kim 
282239a53e0cSJaegeuk Kim /*
282339a53e0cSJaegeuk Kim  * checkpoint.c
282439a53e0cSJaegeuk Kim  */
2825cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2826cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2827cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2828cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2829cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2830cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2831cac5a3d8SDongOh Shin 			int type, bool sync);
2832cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2833cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2834b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
2835cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2836cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2837cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2838cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
283939d787beSChao Yu void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
284039d787beSChao Yu 					unsigned int devidx, int type);
284139d787beSChao Yu bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
284239d787beSChao Yu 					unsigned int devidx, int type);
2843cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2844cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2845cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2846cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2847cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2848cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2849cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2850cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2851cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2852cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2853cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2854cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
28556e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
285639a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
285739a53e0cSJaegeuk Kim 
285839a53e0cSJaegeuk Kim /*
285939a53e0cSJaegeuk Kim  * data.c
286039a53e0cSJaegeuk Kim  */
2861*6dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
2862*6dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
2863b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2864b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2865942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2866b9109b0eSJaegeuk Kim 				enum page_type type);
2867b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2868cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2869b9109b0eSJaegeuk Kim int f2fs_submit_page_write(struct f2fs_io_info *fio);
2870cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2871cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2872cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2873cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2874cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2875cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2876cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2877cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2878cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2879cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2880cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2881cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2882cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2883cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2884cac5a3d8SDongOh Shin 			bool for_write);
2885cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2886cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2887cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2888cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2889cac5a3d8SDongOh Shin 			int create, int flag);
2890cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2891cac5a3d8SDongOh Shin 			u64 start, u64 len);
2892bb9e3bb8SChao Yu bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2893bb9e3bb8SChao Yu bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2894cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2895b0af6d49SChao Yu int __f2fs_write_data_pages(struct address_space *mapping,
2896b0af6d49SChao Yu 						struct writeback_control *wbc,
2897b0af6d49SChao Yu 						enum iostat_type io_type);
2898cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2899cac5a3d8SDongOh Shin 			unsigned int length);
2900cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
29015b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2902cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2903cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
29045b7a487cSWeichao Guo #endif
2905b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
290639a53e0cSJaegeuk Kim 
290739a53e0cSJaegeuk Kim /*
290839a53e0cSJaegeuk Kim  * gc.c
290939a53e0cSJaegeuk Kim  */
2910cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2911cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2912cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2913e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2914e066b83cSJaegeuk Kim 			unsigned int segno);
2915cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
291639a53e0cSJaegeuk Kim 
291739a53e0cSJaegeuk Kim /*
291839a53e0cSJaegeuk Kim  * recovery.c
291939a53e0cSJaegeuk Kim  */
2920cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2921cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
292239a53e0cSJaegeuk Kim 
292339a53e0cSJaegeuk Kim /*
292439a53e0cSJaegeuk Kim  * debug.c
292539a53e0cSJaegeuk Kim  */
292639a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
292739a53e0cSJaegeuk Kim struct f2fs_stat_info {
292839a53e0cSJaegeuk Kim 	struct list_head stat_list;
292939a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
293039a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
293139a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
29325b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
29335b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2934c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
29352c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
29362c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
293735782b23SJaegeuk Kim 	int inmem_pages;
29382c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
29395b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
29405b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
294139a53e0cSJaegeuk Kim 	int total_count, utilization;
29428b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
294314d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
294414d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
29455f32366aSChao Yu 	int nr_discard_cmd;
2946d84d1cbdSChao Yu 	unsigned int undiscard_blks;
2947a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2948648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2949f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
295039a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
295139a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
295239a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
295339a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
295442190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
295539a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2956e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
295739a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2958e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
295939a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
296039a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
296139a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
296239a53e0cSJaegeuk Kim 
296339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
296439a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2965b9a2c252SChangman Lee 	unsigned int inplace_count;
29669edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
296739a53e0cSJaegeuk Kim };
296839a53e0cSJaegeuk Kim 
2969963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2970963d4f7dSGu Zheng {
2971963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2972963d4f7dSGu Zheng }
2973963d4f7dSGu Zheng 
2974942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
297542190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
297639a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2977dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
297833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
297933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
29805b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
29815b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
29825b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
29835b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2984d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2985d5e8f6c9SChao Yu 	do {								\
2986d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2987d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2988d5e8f6c9SChao Yu 	} while (0)
2989d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2990d5e8f6c9SChao Yu 	do {								\
2991d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2992d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2993d5e8f6c9SChao Yu 	} while (0)
29940dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
29950dbdc2aeSJaegeuk Kim 	do {								\
29960dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
299703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
29980dbdc2aeSJaegeuk Kim 	} while (0)
29990dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
30000dbdc2aeSJaegeuk Kim 	do {								\
30010dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
300203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
30030dbdc2aeSJaegeuk Kim 	} while (0)
30043289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
30053289c061SJaegeuk Kim 	do {								\
30063289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
300703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
30083289c061SJaegeuk Kim 	} while (0)
30093289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
30103289c061SJaegeuk Kim 	do {								\
30113289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
301203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
30133289c061SJaegeuk Kim 	} while (0)
3014dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3015dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3016dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3017dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3018b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3019b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
302026a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3021cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
302226a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3023cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
302426a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
302526a28a0cSJaegeuk Kim 	do {								\
302626a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
302726a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
302826a28a0cSJaegeuk Kim 		if (cur > max)						\
302926a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
303026a28a0cSJaegeuk Kim 	} while (0)
3031648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3032648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3033648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3034648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3035648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3036648d50baSChao Yu 	do {								\
3037648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3038648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3039648d50baSChao Yu 		if (cur > max)						\
3040648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3041648d50baSChao Yu 	} while (0)
3042e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
304339a53e0cSJaegeuk Kim 	do {								\
3044963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
304568afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
304668afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
304739a53e0cSJaegeuk Kim 			si->data_segs++;				\
3048e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3049e1235983SChangman Lee 		} else {						\
305039a53e0cSJaegeuk Kim 			si->node_segs++;				\
3051e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3052e1235983SChangman Lee 		}							\
305339a53e0cSJaegeuk Kim 	} while (0)
305439a53e0cSJaegeuk Kim 
305539a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
305668afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
305739a53e0cSJaegeuk Kim 
3058e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
305939a53e0cSJaegeuk Kim 	do {								\
3060963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
306139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
306239a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
306368afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
306439a53e0cSJaegeuk Kim 	} while (0)
306539a53e0cSJaegeuk Kim 
3066e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
306739a53e0cSJaegeuk Kim 	do {								\
3068963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
306939a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
307039a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
307168afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
307239a53e0cSJaegeuk Kim 	} while (0)
307339a53e0cSJaegeuk Kim 
3074cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3075cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3076787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
30774589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
307839a53e0cSJaegeuk Kim #else
3079d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3080d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3081d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3082d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3083d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3084d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3085d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3086d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3087d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3088d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3089d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3090d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3091d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3092d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3093d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3094d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3095d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3096d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3097d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3098d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3099d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3100d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3101d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3102d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3103d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3104d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3105d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3106d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3107d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
310839a53e0cSJaegeuk Kim 
310939a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
311039a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3111787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
31124589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
311339a53e0cSJaegeuk Kim #endif
311439a53e0cSJaegeuk Kim 
311539a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
311639a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
311739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
311839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
311939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
312039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
312139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
312239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3123cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
312439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
312529e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
31261001b347SHuajun Li 
3127e18c65b2SHuajun Li /*
3128e18c65b2SHuajun Li  * inline.c
3129e18c65b2SHuajun Li  */
3130cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3131cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
3132cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
3133bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3134cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3135cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3136cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3137cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
3138cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
3139cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3140cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
3141cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3142cac5a3d8SDongOh Shin 			struct page *ipage);
3143cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3144cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3145cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3146cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3147cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3148cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3149cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3150cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3151cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3152cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3153cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3154cde4de12SJaegeuk Kim 
3155cde4de12SJaegeuk Kim /*
31562658e50dSJaegeuk Kim  * shrinker.c
31572658e50dSJaegeuk Kim  */
3158cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3159cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3160cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3161cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3162cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3163cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
31642658e50dSJaegeuk Kim 
31652658e50dSJaegeuk Kim /*
3166a28ef1f5SChao Yu  * extent_cache.c
3167a28ef1f5SChao Yu  */
3168004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3169004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
3170004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3171004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3172004b6862SChao Yu 				unsigned int ofs);
3173004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3174004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3175004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3176004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3177004b6862SChao Yu 		bool force);
3178df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3179df0f6b44SChao Yu 						struct rb_root *root);
3180cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3181cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3182cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3183cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3184cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3185cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3186cac5a3d8SDongOh Shin 			struct extent_info *ei);
3187cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
318819b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3189cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3190cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
3191a28ef1f5SChao Yu int __init create_extent_cache(void);
3192a28ef1f5SChao Yu void destroy_extent_cache(void);
3193a28ef1f5SChao Yu 
3194a28ef1f5SChao Yu /*
31958ceffcb2SChao Yu  * sysfs.c
31968ceffcb2SChao Yu  */
3197dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3198dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3199dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3200dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
32018ceffcb2SChao Yu 
32028ceffcb2SChao Yu /*
3203cde4de12SJaegeuk Kim  * crypto support
3204cde4de12SJaegeuk Kim  */
32050b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3206cde4de12SJaegeuk Kim {
3207cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3208cde4de12SJaegeuk Kim }
3209cde4de12SJaegeuk Kim 
32101958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
32111958593eSJaegeuk Kim {
32121958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
32131958593eSJaegeuk Kim }
32141958593eSJaegeuk Kim 
3215cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3216cde4de12SJaegeuk Kim {
3217cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3218cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
32192ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3220cde4de12SJaegeuk Kim #endif
3221cde4de12SJaegeuk Kim }
3222cde4de12SJaegeuk Kim 
3223*6dbb1796SEric Biggers /*
3224*6dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
3225*6dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
3226*6dbb1796SEric Biggers  */
3227*6dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3228cde4de12SJaegeuk Kim {
3229*6dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3230cde4de12SJaegeuk Kim }
3231cde4de12SJaegeuk Kim 
3232ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3233ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3234ccd31cb2SSheng Yong { \
3235ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3236cde4de12SJaegeuk Kim }
3237f424f664SJaegeuk Kim 
3238ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3239ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3240ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3241ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3242ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3243ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3244ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3245ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3246b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
32471c1d35dfSChao Yu 
3248178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3249178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
32503c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3251178053e2SDamien Le Moal {
3252178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
32533c62be17SJaegeuk Kim 	int i;
3254178053e2SDamien Le Moal 
32553c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
32563c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
32573c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
32583c62be17SJaegeuk Kim 	return -EINVAL;
3259178053e2SDamien Le Moal }
3260178053e2SDamien Le Moal #endif
3261178053e2SDamien Le Moal 
326296ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
326396ba2decSDamien Le Moal {
326496ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
326596ba2decSDamien Le Moal 
3266ccd31cb2SSheng Yong 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
326752763a4bSJaegeuk Kim }
326852763a4bSJaegeuk Kim 
326952763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
327052763a4bSJaegeuk Kim {
327152763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
327252763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
327352763a4bSJaegeuk Kim 
327452763a4bSJaegeuk Kim 	switch (mt) {
327552763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
327652763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
327752763a4bSJaegeuk Kim 		break;
327852763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
327952763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
328052763a4bSJaegeuk Kim 		break;
328152763a4bSJaegeuk Kim 	}
328252763a4bSJaegeuk Kim }
328352763a4bSJaegeuk Kim 
3284fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3285fcc85a4dSJaegeuk Kim {
3286fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3287886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3288fcc85a4dSJaegeuk Kim 
3289fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3290fcc85a4dSJaegeuk Kim #else
3291fcc85a4dSJaegeuk Kim 	return 0;
3292fcc85a4dSJaegeuk Kim #endif
3293fcc85a4dSJaegeuk Kim }
3294fcc85a4dSJaegeuk Kim 
3295b91050a8SHyunchul Lee static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3296b91050a8SHyunchul Lee {
3297*6dbb1796SEric Biggers 	return (f2fs_post_read_required(inode) ||
3298b91050a8SHyunchul Lee 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3299b91050a8SHyunchul Lee 			F2FS_I_SB(inode)->s_ndevs);
3300b91050a8SHyunchul Lee }
3301b91050a8SHyunchul Lee 
330239a53e0cSJaegeuk Kim #endif
3303