xref: /openbmc/linux/fs/f2fs/f2fs.h (revision b6a06cbbb5f7fd03589cff9178314af04c568826)
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 
10168afcf2dSTomohiro Kusumi #define clear_opt(sbi, option)	((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
10268afcf2dSTomohiro Kusumi #define set_opt(sbi, option)	((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
10368afcf2dSTomohiro Kusumi #define test_opt(sbi, option)	((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
10439a53e0cSJaegeuk Kim 
10539a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10639a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10739a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
10839a53e0cSJaegeuk Kim 
109a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
110a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
111a9841c4dSJaegeuk Kim 			 * address format, __le32.
112a9841c4dSJaegeuk Kim 			 */
11339a53e0cSJaegeuk Kim typedef u32 nid_t;
11439a53e0cSJaegeuk Kim 
11539a53e0cSJaegeuk Kim struct f2fs_mount_info {
11639a53e0cSJaegeuk Kim 	unsigned int	opt;
11739a53e0cSJaegeuk Kim };
11839a53e0cSJaegeuk Kim 
119cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1200bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
121e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1227a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1235c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
124704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1256afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
126234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1271c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
128cde4de12SJaegeuk Kim 
12976f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
13076f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
13176f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
132c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
13376f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
134c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
13576f105a2SJaegeuk Kim 
13639a53e0cSJaegeuk Kim /*
1377c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1387c2e5963SJaegeuk Kim  */
1397c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1407c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1417c2e5963SJaegeuk Kim 
1427c2e5963SJaegeuk Kim /*
14339a53e0cSJaegeuk Kim  * For checkpoint manager
14439a53e0cSJaegeuk Kim  */
14539a53e0cSJaegeuk Kim enum {
14639a53e0cSJaegeuk Kim 	NAT_BITMAP,
14739a53e0cSJaegeuk Kim 	SIT_BITMAP
14839a53e0cSJaegeuk Kim };
14939a53e0cSJaegeuk Kim 
150c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
151c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
152c473f1a9SChao Yu #define	CP_SYNC		0x00000004
153c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
154c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1551f43e2adSChao Yu #define CP_TRIMMED	0x00000020
15675ab4cb8SJaegeuk Kim 
15747b89808SJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	2048
158bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
1594ddb1a4dSJaegeuk Kim 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
160a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
161a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
1624ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
163ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
164969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
165969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
16660b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
167dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
168bba681cbSJaegeuk Kim 
16975ab4cb8SJaegeuk Kim struct cp_control {
17075ab4cb8SJaegeuk Kim 	int reason;
1714b2fecc8SJaegeuk Kim 	__u64 trim_start;
1724b2fecc8SJaegeuk Kim 	__u64 trim_end;
1734b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
17475ab4cb8SJaegeuk Kim };
17575ab4cb8SJaegeuk Kim 
176662befdaSChao Yu /*
17781c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
178662befdaSChao Yu  */
179662befdaSChao Yu enum {
180662befdaSChao Yu 	META_CP,
181662befdaSChao Yu 	META_NAT,
18281c1a0f1SChao Yu 	META_SIT,
1834c521f49SJaegeuk Kim 	META_SSA,
1844c521f49SJaegeuk Kim 	META_POR,
185662befdaSChao Yu };
186662befdaSChao Yu 
1876451e041SJaegeuk Kim /* for the list of ino */
1886451e041SJaegeuk Kim enum {
1896451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
190fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
191fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1920a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
19339d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
1946451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1956451e041SJaegeuk Kim };
1966451e041SJaegeuk Kim 
1976451e041SJaegeuk Kim struct ino_entry {
19839a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
19939a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
20039d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
20139a53e0cSJaegeuk Kim };
20239a53e0cSJaegeuk Kim 
2032710fd7eSChao Yu /* for the list of inodes to be GCed */
20406292073SChao Yu struct inode_entry {
20539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
20639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
20739a53e0cSJaegeuk Kim };
20839a53e0cSJaegeuk Kim 
209a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2107fd9e544SJaegeuk Kim struct discard_entry {
2117fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
212a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
213a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2147fd9e544SJaegeuk Kim };
2157fd9e544SJaegeuk Kim 
216969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
217969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
218969d1b18SChao Yu 
219ba48a33eSChao Yu /* max discard pend list number */
220ba48a33eSChao Yu #define MAX_PLIST_NUM		512
221ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
222ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
223ba48a33eSChao Yu 
22415469963SJaegeuk Kim enum {
22515469963SJaegeuk Kim 	D_PREP,
22615469963SJaegeuk Kim 	D_SUBMIT,
22715469963SJaegeuk Kim 	D_DONE,
22815469963SJaegeuk Kim };
22915469963SJaegeuk Kim 
230004b6862SChao Yu struct discard_info {
231b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
232b01a9201SJaegeuk Kim 	block_t len;			/* length */
233004b6862SChao Yu 	block_t start;			/* actual start address in dev */
234004b6862SChao Yu };
235004b6862SChao Yu 
236b01a9201SJaegeuk Kim struct discard_cmd {
237004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
238004b6862SChao Yu 	union {
239004b6862SChao Yu 		struct {
240004b6862SChao Yu 			block_t lstart;	/* logical start address */
241004b6862SChao Yu 			block_t len;	/* length */
242004b6862SChao Yu 			block_t start;	/* actual start address in dev */
243004b6862SChao Yu 		};
244004b6862SChao Yu 		struct discard_info di;	/* discard info */
245004b6862SChao Yu 
246004b6862SChao Yu 	};
247b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
248b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
249c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
250ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2519a744b92SChao Yu 	unsigned char state;		/* state */
252c81abe34SJaegeuk Kim 	int error;			/* bio error */
253275b66b0SChao Yu };
254275b66b0SChao Yu 
25578997b56SChao Yu enum {
25678997b56SChao Yu 	DPOLICY_BG,
25778997b56SChao Yu 	DPOLICY_FORCE,
25878997b56SChao Yu 	DPOLICY_FSTRIM,
25978997b56SChao Yu 	DPOLICY_UMOUNT,
26078997b56SChao Yu 	MAX_DPOLICY,
26178997b56SChao Yu };
26278997b56SChao Yu 
263ecc9aa00SChao Yu struct discard_policy {
26478997b56SChao Yu 	int type;			/* type of discard */
265ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
266ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
267ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
268ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
269ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
270ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
27178997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
272ecc9aa00SChao Yu };
273ecc9aa00SChao Yu 
2740b54fb84SJaegeuk Kim struct discard_cmd_control {
27515469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
27646f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
277ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
27846f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
2798412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
28015469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
281969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
28215469963SJaegeuk Kim 	struct mutex cmd_lock;
283d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
284d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
285969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
286d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
2878b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
2888b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
2895f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
290004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
29139a53e0cSJaegeuk Kim };
29239a53e0cSJaegeuk Kim 
29339a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
29439a53e0cSJaegeuk Kim struct fsync_inode_entry {
29539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
297c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
298c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
29939a53e0cSJaegeuk Kim };
30039a53e0cSJaegeuk Kim 
30168afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
30268afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
30339a53e0cSJaegeuk Kim 
30468afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
30568afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
30668afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
30768afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
30839a53e0cSJaegeuk Kim 
309dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
310dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
311309cc2b6SJaegeuk Kim 
312dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
31339a53e0cSJaegeuk Kim {
314dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
315cac5a3d8SDongOh Shin 
316dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
31739a53e0cSJaegeuk Kim 	return before;
31839a53e0cSJaegeuk Kim }
31939a53e0cSJaegeuk Kim 
320dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
32139a53e0cSJaegeuk Kim {
322dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
323cac5a3d8SDongOh Shin 
324dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
32539a53e0cSJaegeuk Kim 	return before;
32639a53e0cSJaegeuk Kim }
32739a53e0cSJaegeuk Kim 
328dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
329dfc08a12SChao Yu 							int size, int type)
330184a5cd2SChao Yu {
331184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
332dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
333dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
334184a5cd2SChao Yu }
335184a5cd2SChao Yu 
33639a53e0cSJaegeuk Kim /*
337e9750824SNamjae Jeon  * ioctl commands
338e9750824SNamjae Jeon  */
339e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
340e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
341d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
342e9750824SNamjae Jeon 
34388b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
34488b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
34588b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
34602a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3471e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3481e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
349d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
350456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
351d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
352d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3534dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3544dd6f977SJaegeuk Kim 						struct f2fs_move_range)
355e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
356e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
35734dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
35834dc77adSJaegeuk Kim 						struct f2fs_gc_range)
359e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3601ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3611ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
362c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
36388b88a66SJaegeuk Kim 
3640b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3650b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3660b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
367f424f664SJaegeuk Kim 
3681abff93dSJaegeuk Kim /*
3691abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3701abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3711abff93dSJaegeuk Kim  */
3721abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3731abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3741abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3751abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
376c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3771abff93dSJaegeuk Kim 
378e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
379e9750824SNamjae Jeon /*
380e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
381e9750824SNamjae Jeon  */
382e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
383e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
38404ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
385e9750824SNamjae Jeon #endif
386e9750824SNamjae Jeon 
3872c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
3882c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
3892c1d0305SChao Yu 
39034dc77adSJaegeuk Kim struct f2fs_gc_range {
39134dc77adSJaegeuk Kim 	u32 sync;
39234dc77adSJaegeuk Kim 	u64 start;
39334dc77adSJaegeuk Kim 	u64 len;
39434dc77adSJaegeuk Kim };
39534dc77adSJaegeuk Kim 
396d323d005SChao Yu struct f2fs_defragment {
397d323d005SChao Yu 	u64 start;
398d323d005SChao Yu 	u64 len;
399d323d005SChao Yu };
400d323d005SChao Yu 
4014dd6f977SJaegeuk Kim struct f2fs_move_range {
4024dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4034dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4044dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4054dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4064dd6f977SJaegeuk Kim };
4074dd6f977SJaegeuk Kim 
408e066b83cSJaegeuk Kim struct f2fs_flush_device {
409e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
410e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
411e066b83cSJaegeuk Kim };
412e066b83cSJaegeuk Kim 
413f2470371SChao Yu /* for inline stuff */
414f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4156afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4167a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4176afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4187a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4197a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
420b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4216afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
422f2470371SChao Yu 
423f2470371SChao Yu /* for inline dir */
424f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
425f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
426f2470371SChao Yu 				BITS_PER_BYTE + 1))
427f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
428f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
429f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
430f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
431f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
432f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
433f2470371SChao Yu 
434e9750824SNamjae Jeon /*
43539a53e0cSJaegeuk Kim  * For INODE and NODE manager
43639a53e0cSJaegeuk Kim  */
4377b3cd7d6SJaegeuk Kim /* for directory operations */
4387b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
439d8c6822aSJaegeuk Kim 	struct inode *inode;
44076a9dd85SChao Yu 	void *bitmap;
4417b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4427b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4437b3cd7d6SJaegeuk Kim 	int max;
44476a9dd85SChao Yu 	int nr_bitmap;
4457b3cd7d6SJaegeuk Kim };
4467b3cd7d6SJaegeuk Kim 
44764c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
44864c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4497b3cd7d6SJaegeuk Kim {
450d8c6822aSJaegeuk Kim 	d->inode = inode;
4517b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
45276a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
4537b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
4547b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4557b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
45664c24ecbSTomohiro Kusumi }
457cac5a3d8SDongOh Shin 
45864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
459f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
46064c24ecbSTomohiro Kusumi {
461f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
462f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
463f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
464f2470371SChao Yu 
46564c24ecbSTomohiro Kusumi 	d->inode = inode;
466f2470371SChao Yu 	d->max = entry_cnt;
467f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
468f2470371SChao Yu 	d->bitmap = t;
469f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
470f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
471f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
4727b3cd7d6SJaegeuk Kim }
4737b3cd7d6SJaegeuk Kim 
474dbe6a5ffSJaegeuk Kim /*
475dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
476dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
477dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
47839a53e0cSJaegeuk Kim  */
479dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
480dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
481266e97a8SJaegeuk Kim enum {
482266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
483266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
484266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
485266e97a8SJaegeuk Kim 					 * look up a node with readahead called
4864f4124d0SChao Yu 					 * by get_data_block.
48739a53e0cSJaegeuk Kim 					 */
488266e97a8SJaegeuk Kim };
489266e97a8SJaegeuk Kim 
490a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
49139a53e0cSJaegeuk Kim 
492817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
493817202d9SChao Yu 
49413054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
49513054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
49613054c54SChao Yu 
49739a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
49813054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
49913054c54SChao Yu 
50013054c54SChao Yu /* number of extent info in extent cache we try to shrink */
50113054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
502c11abd1aSJaegeuk Kim 
50354c2258cSChao Yu struct rb_entry {
50454c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
50554c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
50654c2258cSChao Yu 	unsigned int len;		/* length of the entry */
50754c2258cSChao Yu };
50854c2258cSChao Yu 
50939a53e0cSJaegeuk Kim struct extent_info {
51039a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
511111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
51254c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
51339a53e0cSJaegeuk Kim };
51439a53e0cSJaegeuk Kim 
51513054c54SChao Yu struct extent_node {
51654c2258cSChao Yu 	struct rb_node rb_node;
51754c2258cSChao Yu 	union {
51854c2258cSChao Yu 		struct {
51954c2258cSChao Yu 			unsigned int fofs;
52054c2258cSChao Yu 			unsigned int len;
52154c2258cSChao Yu 			u32 blk;
52254c2258cSChao Yu 		};
52313054c54SChao Yu 		struct extent_info ei;	/* extent info */
52454c2258cSChao Yu 
52554c2258cSChao Yu 	};
52654c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
527201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
52813054c54SChao Yu };
52913054c54SChao Yu 
53013054c54SChao Yu struct extent_tree {
53113054c54SChao Yu 	nid_t ino;			/* inode number */
53213054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
53362c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5343e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
535137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
53613054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
53768e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
53813054c54SChao Yu };
53913054c54SChao Yu 
54039a53e0cSJaegeuk Kim /*
541003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
542003a3e1dSJaegeuk Kim  *
543003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
544003a3e1dSJaegeuk Kim  */
545003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
546003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5477f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5487f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5497f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
550003a3e1dSJaegeuk Kim 
551003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
552003a3e1dSJaegeuk Kim 	block_t m_pblk;
553003a3e1dSJaegeuk Kim 	block_t m_lblk;
554003a3e1dSJaegeuk Kim 	unsigned int m_len;
555003a3e1dSJaegeuk Kim 	unsigned int m_flags;
556da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
557c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
558d5097be5SHyunchul Lee 	int m_seg_type;
559003a3e1dSJaegeuk Kim };
560003a3e1dSJaegeuk Kim 
561e2b4e2bcSChao Yu /* for flag in get_data_block */
562f2220c7fSQiuyang Sun enum {
563f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
564f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
565f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
566f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
567f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
568c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
569f2220c7fSQiuyang Sun };
570e2b4e2bcSChao Yu 
571003a3e1dSJaegeuk Kim /*
57239a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
57339a53e0cSJaegeuk Kim  */
57439a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
575354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
576cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
577e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
57826787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
579*b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
58039a53e0cSJaegeuk Kim 
581b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
582b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
583b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
584b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
585b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
586b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
587cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
588cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
589cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
590e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
591e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
59226787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
59326787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
594*b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
595*b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
596*b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
597cde4de12SJaegeuk Kim 
598ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
599ab9fa662SJaegeuk Kim 
60039a53e0cSJaegeuk Kim struct f2fs_inode_info {
60139a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
60239a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
60339a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
60438431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6051ad71a27SJaegeuk Kim 	union {
6061ad71a27SJaegeuk Kim 		unsigned int i_current_depth;	/* only for directory depth */
6071ad71a27SJaegeuk Kim 		unsigned short i_gc_failures;	/* only for regular file */
6081ad71a27SJaegeuk Kim 	};
6096666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
61039a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
61139a53e0cSJaegeuk Kim 
61239a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
61339a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
614d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
615204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
61639a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
61739a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
61888c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
619b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
62039a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
62126de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
62288b88a66SJaegeuk Kim 
6230abd675eSChao Yu #ifdef CONFIG_QUOTA
6240abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6250abd675eSChao Yu 
6260abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6270abd675eSChao Yu 	qsize_t i_reserved_quota;
6280abd675eSChao Yu #endif
6290f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6300f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
63157864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
63288b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6337a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
63488b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6353e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
63682e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
6375a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
63827161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
639f2470371SChao Yu 
6407a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6415c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6426afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
6431c1d35dfSChao Yu 	struct timespec i_crtime;	/* inode creation time */
64439a53e0cSJaegeuk Kim };
64539a53e0cSJaegeuk Kim 
64639a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
647bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
64839a53e0cSJaegeuk Kim {
649bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
650bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
651bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
65239a53e0cSJaegeuk Kim }
65339a53e0cSJaegeuk Kim 
65439a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
65539a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
65639a53e0cSJaegeuk Kim {
65739a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6584d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
65939a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
66039a53e0cSJaegeuk Kim }
66139a53e0cSJaegeuk Kim 
662429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
663429511cdSChao Yu 						u32 blk, unsigned int len)
664429511cdSChao Yu {
665429511cdSChao Yu 	ei->fofs = fofs;
666429511cdSChao Yu 	ei->blk = blk;
667429511cdSChao Yu 	ei->len = len;
668429511cdSChao Yu }
669429511cdSChao Yu 
670004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
671004b6862SChao Yu 						struct discard_info *front)
672004b6862SChao Yu {
673004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
674004b6862SChao Yu }
675004b6862SChao Yu 
676004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
677004b6862SChao Yu 						struct discard_info *back)
678004b6862SChao Yu {
679004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
680004b6862SChao Yu }
681004b6862SChao Yu 
682004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
683004b6862SChao Yu 						struct discard_info *front)
684004b6862SChao Yu {
685004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
686004b6862SChao Yu }
687004b6862SChao Yu 
688429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
689429511cdSChao Yu 						struct extent_info *front)
690429511cdSChao Yu {
691429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
692429511cdSChao Yu 			back->blk + back->len == front->blk);
693429511cdSChao Yu }
694429511cdSChao Yu 
695429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
696429511cdSChao Yu 						struct extent_info *back)
697429511cdSChao Yu {
698429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
699429511cdSChao Yu }
700429511cdSChao Yu 
701429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
702429511cdSChao Yu 						struct extent_info *front)
703429511cdSChao Yu {
704429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
705429511cdSChao Yu }
706429511cdSChao Yu 
707cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
708205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
709205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
7104abd3f5aSChao Yu {
711205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7124abd3f5aSChao Yu 		et->largest = en->ei;
7137c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
714205b9822SJaegeuk Kim 	}
7154abd3f5aSChao Yu }
7164abd3f5aSChao Yu 
7179a4ffdf5SChao Yu /*
7189a4ffdf5SChao Yu  * For free nid management
7199a4ffdf5SChao Yu  */
7209a4ffdf5SChao Yu enum nid_state {
7219a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7229a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7239a4ffdf5SChao Yu 	MAX_NID_STATE,
724b8559dc2SChao Yu };
725b8559dc2SChao Yu 
72639a53e0cSJaegeuk Kim struct f2fs_nm_info {
72739a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
72839a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
72904d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
73039a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
731cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
732ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7332304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
73439a53e0cSJaegeuk Kim 
73539a53e0cSJaegeuk Kim 	/* NAT cache management */
73639a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
737309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
738b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
73939a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
740309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
741aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
74222ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
74339a53e0cSJaegeuk Kim 
74439a53e0cSJaegeuk Kim 	/* free node ids management */
7458a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7469a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7479a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
748b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
74939a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
7504ac91242SChao Yu 	unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
7514ac91242SChao Yu 	unsigned char *nat_block_bitmap;
752586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
75339a53e0cSJaegeuk Kim 
75439a53e0cSJaegeuk Kim 	/* for checkpoint */
75539a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
75622ad0b6aSJaegeuk Kim 
75722ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
75822ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
75922ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
76022ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
761599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
762599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
763599a09b2SChao Yu #endif
76439a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
76539a53e0cSJaegeuk Kim };
76639a53e0cSJaegeuk Kim 
76739a53e0cSJaegeuk Kim /*
76839a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
76939a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
77039a53e0cSJaegeuk Kim  * by the data offset in a file.
77139a53e0cSJaegeuk Kim  */
77239a53e0cSJaegeuk Kim struct dnode_of_data {
77339a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
77439a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
77539a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
77639a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
77739a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
77839a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
77993bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
7803cf45747SChao Yu 	char cur_level;			/* level of hole node page */
7813cf45747SChao Yu 	char max_level;			/* level of current page located */
78239a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
78339a53e0cSJaegeuk Kim };
78439a53e0cSJaegeuk Kim 
78539a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
78639a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
78739a53e0cSJaegeuk Kim {
788d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
78939a53e0cSJaegeuk Kim 	dn->inode = inode;
79039a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
79139a53e0cSJaegeuk Kim 	dn->node_page = npage;
79239a53e0cSJaegeuk Kim 	dn->nid = nid;
79339a53e0cSJaegeuk Kim }
79439a53e0cSJaegeuk Kim 
79539a53e0cSJaegeuk Kim /*
79639a53e0cSJaegeuk Kim  * For SIT manager
79739a53e0cSJaegeuk Kim  *
79839a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
79939a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
80039a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
80139a53e0cSJaegeuk Kim  * respectively.
80239a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
80339a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
80439a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
80539a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
80639a53e0cSJaegeuk Kim  * data and 8 for node logs.
80739a53e0cSJaegeuk Kim  */
80839a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
80939a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
81039a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
81139a53e0cSJaegeuk Kim 
81239a53e0cSJaegeuk Kim enum {
81339a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
81439a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
81539a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
81639a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
81739a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
81839a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
81938aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
82039a53e0cSJaegeuk Kim };
82139a53e0cSJaegeuk Kim 
8226b4afdd7SJaegeuk Kim struct flush_cmd {
8236b4afdd7SJaegeuk Kim 	struct completion wait;
824721bd4d5SGu Zheng 	struct llist_node llnode;
82539d787beSChao Yu 	nid_t ino;
8266b4afdd7SJaegeuk Kim 	int ret;
8276b4afdd7SJaegeuk Kim };
8286b4afdd7SJaegeuk Kim 
829a688b9d9SGu Zheng struct flush_cmd_control {
830a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
831a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8328b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8338b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
834721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
835721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
836a688b9d9SGu Zheng };
837a688b9d9SGu Zheng 
83839a53e0cSJaegeuk Kim struct f2fs_sm_info {
83939a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
84039a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
84139a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
84239a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
84339a53e0cSJaegeuk Kim 
8442b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8452b60311dSChao Yu 
84639a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
84739a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
84839a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
84939a53e0cSJaegeuk Kim 
85039a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
85139a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
85239a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
85339a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
85481eb8d6eSJaegeuk Kim 
85581eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
85681eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8577fd9e544SJaegeuk Kim 
858bba681cbSJaegeuk Kim 	/* for batched trimming */
859bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
860bba681cbSJaegeuk Kim 
861184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
862184a5cd2SChao Yu 
863216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
864216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
865c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
866ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
867a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
8686b4afdd7SJaegeuk Kim 
8696b4afdd7SJaegeuk Kim 	/* for flush command control */
870b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
871a688b9d9SGu Zheng 
8720b54fb84SJaegeuk Kim 	/* for discard command control */
8730b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
87439a53e0cSJaegeuk Kim };
87539a53e0cSJaegeuk Kim 
87639a53e0cSJaegeuk Kim /*
87739a53e0cSJaegeuk Kim  * For superblock
87839a53e0cSJaegeuk Kim  */
87939a53e0cSJaegeuk Kim /*
88039a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
88139a53e0cSJaegeuk Kim  *
88239a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
88339a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
88439a53e0cSJaegeuk Kim  */
88536951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
88639a53e0cSJaegeuk Kim enum count_type {
88739a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
888c227f912SChao Yu 	F2FS_DIRTY_DATA,
8892c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
89039a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
89139a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
8928dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
8930f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
89436951b38SChao Yu 	F2FS_WB_CP_DATA,
89536951b38SChao Yu 	F2FS_WB_DATA,
89639a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
89739a53e0cSJaegeuk Kim };
89839a53e0cSJaegeuk Kim 
89939a53e0cSJaegeuk Kim /*
900e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
90139a53e0cSJaegeuk Kim  * The available types are:
90239a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
90339a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
90439a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
90539a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
90639a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
90739a53e0cSJaegeuk Kim  *			with waiting the bio's completion
90839a53e0cSJaegeuk Kim  * ...			Only can be used with META.
90939a53e0cSJaegeuk Kim  */
9107d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
91139a53e0cSJaegeuk Kim enum page_type {
91239a53e0cSJaegeuk Kim 	DATA,
91339a53e0cSJaegeuk Kim 	NODE,
91439a53e0cSJaegeuk Kim 	META,
91539a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
91639a53e0cSJaegeuk Kim 	META_FLUSH,
9178ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9188ce67cb0SJaegeuk Kim 	INMEM_DROP,
9198c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
92028bc106bSChao Yu 	INMEM_REVOKE,
9218ce67cb0SJaegeuk Kim 	IPU,
9228ce67cb0SJaegeuk Kim 	OPU,
92339a53e0cSJaegeuk Kim };
92439a53e0cSJaegeuk Kim 
925a912b54dSJaegeuk Kim enum temp_type {
926a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
927a912b54dSJaegeuk Kim 	WARM,
928a912b54dSJaegeuk Kim 	COLD,
929a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
930a912b54dSJaegeuk Kim };
931a912b54dSJaegeuk Kim 
932cc15620bSJaegeuk Kim enum need_lock_type {
933cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
934cc15620bSJaegeuk Kim 	LOCK_DONE,
935cc15620bSJaegeuk Kim 	LOCK_RETRY,
936cc15620bSJaegeuk Kim };
937cc15620bSJaegeuk Kim 
938a5fd5050SChao Yu enum cp_reason_type {
939a5fd5050SChao Yu 	CP_NO_NEEDED,
940a5fd5050SChao Yu 	CP_NON_REGULAR,
941a5fd5050SChao Yu 	CP_HARDLINK,
942a5fd5050SChao Yu 	CP_SB_NEED_CP,
943a5fd5050SChao Yu 	CP_WRONG_PINO,
944a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
945a5fd5050SChao Yu 	CP_NODE_NEED_CP,
946a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
947a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
9480a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
949a5fd5050SChao Yu };
950a5fd5050SChao Yu 
951b0af6d49SChao Yu enum iostat_type {
952b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
953b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
954b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
955b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
956b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
957b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
958b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
959b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
960b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
961b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
962b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
963b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
964b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
965b0af6d49SChao Yu 	NR_IO_TYPE,
966b0af6d49SChao Yu };
967b0af6d49SChao Yu 
968458e6197SJaegeuk Kim struct f2fs_io_info {
96905ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
97039d787beSChao Yu 	nid_t ino;		/* inode number */
971458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
972a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
97304d328deSMike Christie 	int op;			/* contains REQ_OP_ */
974ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
9757a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
97628bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
97705ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
9784375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
979fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
980d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
981cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
982fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
983b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
984578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
985458e6197SJaegeuk Kim };
986458e6197SJaegeuk Kim 
98768afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
9881ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
989458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
9901ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
9911ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
992458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
993df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
994fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
995fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
9961ff7bd3bSJaegeuk Kim };
9971ff7bd3bSJaegeuk Kim 
9983c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
9993c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10003c62be17SJaegeuk Kim struct f2fs_dev_info {
10013c62be17SJaegeuk Kim 	struct block_device *bdev;
10023c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10033c62be17SJaegeuk Kim 	unsigned int total_segments;
10043c62be17SJaegeuk Kim 	block_t start_blk;
10053c62be17SJaegeuk Kim 	block_t end_blk;
10063c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10073c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10083c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10093c62be17SJaegeuk Kim #endif
10103c62be17SJaegeuk Kim };
10113c62be17SJaegeuk Kim 
1012c227f912SChao Yu enum inode_type {
1013c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1014c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10150f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
101657864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1017c227f912SChao Yu 	NR_INODE_TYPE,
1018c227f912SChao Yu };
1019c227f912SChao Yu 
102067298804SChao Yu /* for inner inode cache management */
102167298804SChao Yu struct inode_management {
102267298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
102367298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
102467298804SChao Yu 	struct list_head ino_list;		/* inode list head */
102567298804SChao Yu 	unsigned long ino_num;			/* number of entries */
102667298804SChao Yu };
102767298804SChao Yu 
1028caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1029caf0047eSChao Yu enum {
1030caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1031caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1032caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1033caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1034df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1035bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
1036caf0047eSChao Yu };
1037caf0047eSChao Yu 
10386beceb54SJaegeuk Kim enum {
10396beceb54SJaegeuk Kim 	CP_TIME,
1040d0239e1bSJaegeuk Kim 	REQ_TIME,
10416beceb54SJaegeuk Kim 	MAX_TIME,
10426beceb54SJaegeuk Kim };
10436beceb54SJaegeuk Kim 
10440cdd3195SHyunchul Lee enum {
10450cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
10460cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1047f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
10480cdd3195SHyunchul Lee };
10490cdd3195SHyunchul Lee 
105007939627SJaegeuk Kim enum {
105107939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
105207939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
105307939627SJaegeuk Kim };
105407939627SJaegeuk Kim 
105539a53e0cSJaegeuk Kim struct f2fs_sb_info {
105639a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
10575e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
105839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1059846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1060e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1061fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
106239a53e0cSJaegeuk Kim 
1063178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1064178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1065178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1066178053e2SDamien Le Moal #endif
1067178053e2SDamien Le Moal 
106839a53e0cSJaegeuk Kim 	/* for node-related operations */
106939a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
107039a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
107139a53e0cSJaegeuk Kim 
107239a53e0cSJaegeuk Kim 	/* for segment-related operations */
107339a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
10741ff7bd3bSJaegeuk Kim 
10751ff7bd3bSJaegeuk Kim 	/* for bio operations */
1076a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1077e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1078e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
10790a595ebaSJaegeuk Kim 	int write_io_size_bits;			/* Write IO size bits */
10800a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
108139a53e0cSJaegeuk Kim 
108239a53e0cSJaegeuk Kim 	/* for checkpoint */
108339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
10848508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1085aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
108639a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
108739936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1088b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1089b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
109059c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1091fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
10926beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
10936beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
109439a53e0cSJaegeuk Kim 
109567298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
10966451e041SJaegeuk Kim 
10976451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
10980d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
109939a53e0cSJaegeuk Kim 
1100c227f912SChao Yu 	/* for inode management */
1101c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1102c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
110339a53e0cSJaegeuk Kim 
110413054c54SChao Yu 	/* for extent tree cache */
110513054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11065e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
110713054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
110813054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11097441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1110137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
111174fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
111213054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
111313054c54SChao Yu 
111439a53e0cSJaegeuk Kim 	/* basic filesystem units */
111539a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
111639a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
111739a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
111839a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
111939a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
112039a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
112139a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
112239a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
112339a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
112439a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
112539a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
112639a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
112739a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1128e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
112939a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
1130ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
11316afc662eSChao Yu 	int inline_xattr_size;			/* inline xattr size */
1132a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1133f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
113439a53e0cSJaegeuk Kim 
113539a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
113639a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1137a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
113839a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1139daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
114080d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
11417e65be49SJaegeuk Kim 	block_t root_reserved_blocks;		/* root reserved blocks */
11427c2e5963SJaegeuk Kim 	kuid_t s_resuid;			/* reserved blocks for uid */
11437c2e5963SJaegeuk Kim 	kgid_t s_resgid;			/* reserved blocks for gid */
1144daeb433eSChao Yu 
1145292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1146292c196aSChao Yu 
114739a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1148523be8a6SJaegeuk Kim 
1149523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
115035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
115141382ec4SJaegeuk Kim 	/* # of allocated blocks */
115241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
115339a53e0cSJaegeuk Kim 
1154687de7f1SJaegeuk Kim 	/* writeback control */
1155687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1156687de7f1SJaegeuk Kim 
1157513c5f37SJaegeuk Kim 	/* valid inode count */
1158513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1159513c5f37SJaegeuk Kim 
116039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
116139a53e0cSJaegeuk Kim 
116239a53e0cSJaegeuk Kim 	/* for cleaning operations */
116339a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
116439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
11655ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
116639a53e0cSJaegeuk Kim 
1167e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
1168e93b9865SHou Pengyang 	u64 fggc_threshold;
1169e93b9865SHou Pengyang 
11701ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
11711ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
11721ad71a27SJaegeuk Kim 
1173b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1174b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1175b1c57c1cSJaegeuk Kim 
117639a53e0cSJaegeuk Kim 	/*
117739a53e0cSJaegeuk Kim 	 * for stat information.
117839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
117939a53e0cSJaegeuk Kim 	 */
118035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
118139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
118239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
118339a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1184b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
11855b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
11865b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
11875b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
11885b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1189d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
119003e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
119103e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
119226a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1193648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
119426a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1195648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
119639a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
119733fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
119835b09d82SNamjae Jeon #endif
119939a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1200b59d0baeSNamjae Jeon 
1201b0af6d49SChao Yu 	/* For app/fs IO statistics */
1202b0af6d49SChao Yu 	spinlock_t iostat_lock;
1203b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1204b0af6d49SChao Yu 	bool iostat_enable;
1205b0af6d49SChao Yu 
1206b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1207b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1208b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12092658e50dSJaegeuk Kim 
12102658e50dSJaegeuk Kim 	/* For shrinker support */
12112658e50dSJaegeuk Kim 	struct list_head s_list;
12123c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12133c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
12141228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
12151228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
12162658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12172658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12188f1dbbbbSShuoran Liu 
12198f1dbbbbSShuoran Liu 	/* For write statistics */
12208f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12218f1dbbbbSShuoran Liu 	u64 kbytes_written;
122243b6573bSKeith Mok 
122343b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
122443b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12251ecc0c5cSChao Yu 
1226704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1227704956ecSChao Yu 	__u32 s_chksum_seed;
1228704956ecSChao Yu 
12291ecc0c5cSChao Yu 	/* For fault injection */
12301ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12311ecc0c5cSChao Yu 	struct f2fs_fault_info fault_info;
12321ecc0c5cSChao Yu #endif
12334b2414d0SChao Yu 
12344b2414d0SChao Yu #ifdef CONFIG_QUOTA
12354b2414d0SChao Yu 	/* Names of quota files with journalled quota */
12364b2414d0SChao Yu 	char *s_qf_names[MAXQUOTAS];
12374b2414d0SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
12384b2414d0SChao Yu #endif
12390cdd3195SHyunchul Lee 	/* For which write hints are passed down to block layer */
12400cdd3195SHyunchul Lee 	int whint_mode;
124107939627SJaegeuk Kim 
124207939627SJaegeuk Kim 	/* segment allocation policy */
124307939627SJaegeuk Kim 	int alloc_mode;
124439a53e0cSJaegeuk Kim };
124539a53e0cSJaegeuk Kim 
12461ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
124755523519SChao Yu #define f2fs_show_injection_info(type)				\
124855523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
124955523519SChao Yu 		KERN_INFO, fault_name[type],			\
125055523519SChao Yu 		__func__, __builtin_return_address(0))
12511ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12521ecc0c5cSChao Yu {
12531ecc0c5cSChao Yu 	struct f2fs_fault_info *ffi = &sbi->fault_info;
12541ecc0c5cSChao Yu 
12551ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12561ecc0c5cSChao Yu 		return false;
12571ecc0c5cSChao Yu 
12581ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12591ecc0c5cSChao Yu 		return false;
12601ecc0c5cSChao Yu 
12611ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12621ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12631ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12641ecc0c5cSChao Yu 		return true;
12651ecc0c5cSChao Yu 	}
12661ecc0c5cSChao Yu 	return false;
12671ecc0c5cSChao Yu }
12681ecc0c5cSChao Yu #endif
12691ecc0c5cSChao Yu 
12708f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
12718f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
12728f1dbbbbSShuoran Liu  */
12738f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
127468afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
127568afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
12768f1dbbbbSShuoran Liu 
12776beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
12786beceb54SJaegeuk Kim {
12796beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
12806beceb54SJaegeuk Kim }
12816beceb54SJaegeuk Kim 
12826beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
12836beceb54SJaegeuk Kim {
12846bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
12856beceb54SJaegeuk Kim 
12866beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
12876beceb54SJaegeuk Kim }
12886beceb54SJaegeuk Kim 
1289d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1290d0239e1bSJaegeuk Kim {
1291d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1292d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1293d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1294d0239e1bSJaegeuk Kim 
1295d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1296d0239e1bSJaegeuk Kim 		return 0;
1297d0239e1bSJaegeuk Kim 
1298d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1299d0239e1bSJaegeuk Kim }
1300d0239e1bSJaegeuk Kim 
130139a53e0cSJaegeuk Kim /*
130239a53e0cSJaegeuk Kim  * Inline functions
130339a53e0cSJaegeuk Kim  */
1304416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1305704956ecSChao Yu 			      const void *address, unsigned int length)
1306704956ecSChao Yu {
1307704956ecSChao Yu 	struct {
1308704956ecSChao Yu 		struct shash_desc shash;
1309704956ecSChao Yu 		char ctx[4];
1310704956ecSChao Yu 	} desc;
1311704956ecSChao Yu 	int err;
1312704956ecSChao Yu 
1313704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1314704956ecSChao Yu 
1315704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1316704956ecSChao Yu 	desc.shash.flags = 0;
1317704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1318704956ecSChao Yu 
1319704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1320704956ecSChao Yu 	BUG_ON(err);
1321704956ecSChao Yu 
1322704956ecSChao Yu 	return *(u32 *)desc.ctx;
1323704956ecSChao Yu }
1324704956ecSChao Yu 
1325416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1326416d2dbbSChao Yu 			   unsigned int length)
1327416d2dbbSChao Yu {
1328416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1329416d2dbbSChao Yu }
1330416d2dbbSChao Yu 
1331416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1332416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1333416d2dbbSChao Yu {
1334416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1335416d2dbbSChao Yu }
1336416d2dbbSChao Yu 
1337416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1338416d2dbbSChao Yu 			      const void *address, unsigned int length)
1339416d2dbbSChao Yu {
1340416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1341416d2dbbSChao Yu }
1342416d2dbbSChao Yu 
134339a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
134439a53e0cSJaegeuk Kim {
134539a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
134639a53e0cSJaegeuk Kim }
134739a53e0cSJaegeuk Kim 
134839a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
134939a53e0cSJaegeuk Kim {
135039a53e0cSJaegeuk Kim 	return sb->s_fs_info;
135139a53e0cSJaegeuk Kim }
135239a53e0cSJaegeuk Kim 
13534081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13544081363fSJaegeuk Kim {
13554081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13564081363fSJaegeuk Kim }
13574081363fSJaegeuk Kim 
13584081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13594081363fSJaegeuk Kim {
13604081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13614081363fSJaegeuk Kim }
13624081363fSJaegeuk Kim 
13634081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13644081363fSJaegeuk Kim {
13654081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
13664081363fSJaegeuk Kim }
13674081363fSJaegeuk Kim 
136839a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
136939a53e0cSJaegeuk Kim {
137039a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
137139a53e0cSJaegeuk Kim }
137239a53e0cSJaegeuk Kim 
137339a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
137439a53e0cSJaegeuk Kim {
137539a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
137639a53e0cSJaegeuk Kim }
137739a53e0cSJaegeuk Kim 
137845590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
137945590710SGu Zheng {
138045590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
138145590710SGu Zheng }
138245590710SGu Zheng 
138358bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
138458bfaf44SJaegeuk Kim {
138558bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
138658bfaf44SJaegeuk Kim }
138758bfaf44SJaegeuk Kim 
138839a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
138939a53e0cSJaegeuk Kim {
139039a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
139139a53e0cSJaegeuk Kim }
139239a53e0cSJaegeuk Kim 
139339a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
139439a53e0cSJaegeuk Kim {
139539a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
139639a53e0cSJaegeuk Kim }
139739a53e0cSJaegeuk Kim 
139839a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
139939a53e0cSJaegeuk Kim {
140039a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
140139a53e0cSJaegeuk Kim }
140239a53e0cSJaegeuk Kim 
140339a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
140439a53e0cSJaegeuk Kim {
140539a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
140639a53e0cSJaegeuk Kim }
140739a53e0cSJaegeuk Kim 
140839a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
140939a53e0cSJaegeuk Kim {
141039a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
141139a53e0cSJaegeuk Kim }
141239a53e0cSJaegeuk Kim 
14139df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
14149df27d98SGu Zheng {
14159df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
14169df27d98SGu Zheng }
14179df27d98SGu Zheng 
14184ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
14194ef51a8fSJaegeuk Kim {
14204ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
14214ef51a8fSJaegeuk Kim }
14224ef51a8fSJaegeuk Kim 
1423caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
142439a53e0cSJaegeuk Kim {
1425fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
142639a53e0cSJaegeuk Kim }
142739a53e0cSJaegeuk Kim 
1428caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
142939a53e0cSJaegeuk Kim {
1430fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1431caf0047eSChao Yu }
1432caf0047eSChao Yu 
1433caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1434caf0047eSChao Yu {
1435fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
143639a53e0cSJaegeuk Kim }
143739a53e0cSJaegeuk Kim 
1438d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1439d71b5564SJaegeuk Kim {
1440d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1441d71b5564SJaegeuk Kim }
1442d71b5564SJaegeuk Kim 
1443ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1444ea676733SJaegeuk Kim {
1445ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1446ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1447ea676733SJaegeuk Kim 	return 0;
1448ea676733SJaegeuk Kim }
1449ea676733SJaegeuk Kim 
1450ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1451ced2c7eaSKinglong Mee {
1452ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1453ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1454ced2c7eaSKinglong Mee }
1455ced2c7eaSKinglong Mee 
1456aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
145725ca923bSJaegeuk Kim {
145825ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1459aaec2b1dSChao Yu 
146025ca923bSJaegeuk Kim 	return ckpt_flags & f;
146125ca923bSJaegeuk Kim }
146225ca923bSJaegeuk Kim 
1463aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
146425ca923bSJaegeuk Kim {
1465aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1466aaec2b1dSChao Yu }
1467aaec2b1dSChao Yu 
1468aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1469aaec2b1dSChao Yu {
1470aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1471aaec2b1dSChao Yu 
1472aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
147325ca923bSJaegeuk Kim 	ckpt_flags |= f;
147425ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
147525ca923bSJaegeuk Kim }
147625ca923bSJaegeuk Kim 
1477aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
147825ca923bSJaegeuk Kim {
1479d1aa2453SChao Yu 	unsigned long flags;
1480d1aa2453SChao Yu 
1481d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1482aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1483d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1484aaec2b1dSChao Yu }
1485aaec2b1dSChao Yu 
1486aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1487aaec2b1dSChao Yu {
1488aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1489aaec2b1dSChao Yu 
1490aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
149125ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
149225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
149325ca923bSJaegeuk Kim }
149425ca923bSJaegeuk Kim 
1495aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1496aaec2b1dSChao Yu {
1497d1aa2453SChao Yu 	unsigned long flags;
1498d1aa2453SChao Yu 
1499d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1500aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1501d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1502aaec2b1dSChao Yu }
1503aaec2b1dSChao Yu 
150422ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
150522ad0b6aSJaegeuk Kim {
1506d1aa2453SChao Yu 	unsigned long flags;
1507d1aa2453SChao Yu 
150822ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
150922ad0b6aSJaegeuk Kim 
151022ad0b6aSJaegeuk Kim 	if (lock)
1511d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
151222ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
151322ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
151422ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
151522ad0b6aSJaegeuk Kim 	if (lock)
1516d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
151722ad0b6aSJaegeuk Kim }
151822ad0b6aSJaegeuk Kim 
151922ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
152022ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
152122ad0b6aSJaegeuk Kim {
152222ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
152322ad0b6aSJaegeuk Kim 
1524c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
152522ad0b6aSJaegeuk Kim }
152622ad0b6aSJaegeuk Kim 
1527e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
152839a53e0cSJaegeuk Kim {
1529b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
153039a53e0cSJaegeuk Kim }
153139a53e0cSJaegeuk Kim 
1532cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1533cc15620bSJaegeuk Kim {
1534cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1535cc15620bSJaegeuk Kim }
1536cc15620bSJaegeuk Kim 
1537e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
153839a53e0cSJaegeuk Kim {
1539b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
154039936837SJaegeuk Kim }
154139936837SJaegeuk Kim 
1542e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
154339936837SJaegeuk Kim {
1544b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
154539936837SJaegeuk Kim }
154639936837SJaegeuk Kim 
1547e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
154839936837SJaegeuk Kim {
1549b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
155039a53e0cSJaegeuk Kim }
155139a53e0cSJaegeuk Kim 
1552119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1553119ee914SJaegeuk Kim {
1554119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1555119ee914SJaegeuk Kim 
1556119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1557119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1558119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1559119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1560119ee914SJaegeuk Kim 	return reason;
1561119ee914SJaegeuk Kim }
1562119ee914SJaegeuk Kim 
1563119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1564119ee914SJaegeuk Kim {
1565c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1566119ee914SJaegeuk Kim }
1567119ee914SJaegeuk Kim 
1568119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1569119ee914SJaegeuk Kim {
1570aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1571aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1572119ee914SJaegeuk Kim }
1573119ee914SJaegeuk Kim 
157439a53e0cSJaegeuk Kim /*
157539a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
157639a53e0cSJaegeuk Kim  */
1577064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
157839a53e0cSJaegeuk Kim {
1579d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1580d6b7d4b3SChao Yu 		return -EINVAL;
1581cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1582064e0823SNamjae Jeon 		return -EINVAL;
1583064e0823SNamjae Jeon 	return 0;
158439a53e0cSJaegeuk Kim }
158539a53e0cSJaegeuk Kim 
158639a53e0cSJaegeuk Kim /*
158739a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
158839a53e0cSJaegeuk Kim  */
158939a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
159039a53e0cSJaegeuk Kim {
15910eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
15920eb0adadSChao Yu 
1593000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
159439a53e0cSJaegeuk Kim }
159539a53e0cSJaegeuk Kim 
15964bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
15974bc8e9bcSChao Yu {
15984bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
15994bc8e9bcSChao Yu }
16004bc8e9bcSChao Yu 
1601d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1602d8a9a229SJaegeuk Kim 					struct inode *inode)
16037c2e5963SJaegeuk Kim {
1604d8a9a229SJaegeuk Kim 	if (!inode)
1605d8a9a229SJaegeuk Kim 		return true;
16067c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16077c2e5963SJaegeuk Kim 		return false;
1608d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1609d8a9a229SJaegeuk Kim 		return true;
16107c2e5963SJaegeuk Kim 	if (capable(CAP_SYS_RESOURCE))
16117c2e5963SJaegeuk Kim 		return true;
16127c2e5963SJaegeuk Kim 	if (uid_eq(sbi->s_resuid, current_fsuid()))
16137c2e5963SJaegeuk Kim 		return true;
16147c2e5963SJaegeuk Kim 	if (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) &&
16157c2e5963SJaegeuk Kim 					in_group_p(sbi->s_resgid))
16167c2e5963SJaegeuk Kim 		return true;
16177c2e5963SJaegeuk Kim 	return false;
16187c2e5963SJaegeuk Kim }
16197c2e5963SJaegeuk Kim 
16200abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
16210abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
162246008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
162339a53e0cSJaegeuk Kim {
16240abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1625daeb433eSChao Yu 	block_t avail_user_block_count;
16260abd675eSChao Yu 	int ret;
16270abd675eSChao Yu 
16280abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16290abd675eSChao Yu 	if (ret)
16300abd675eSChao Yu 		return ret;
1631dd11a5dfSJaegeuk Kim 
1632cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
163355523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
163455523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16350abd675eSChao Yu 		release = *count;
16360abd675eSChao Yu 		goto enospc;
163755523519SChao Yu 	}
1638cb78942bSJaegeuk Kim #endif
1639dd11a5dfSJaegeuk Kim 	/*
1640dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1641dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1642dd11a5dfSJaegeuk Kim 	 */
1643dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1644cfb271d4SChao Yu 
164539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16462555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
164780d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
164880d42145SYunlong Song 					sbi->current_reserved_blocks;
16497e65be49SJaegeuk Kim 
1650d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
16517e65be49SJaegeuk Kim 		avail_user_block_count -= sbi->root_reserved_blocks;
16527e65be49SJaegeuk Kim 
1653daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1654daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16557e65be49SJaegeuk Kim 		if (diff > *count)
16567e65be49SJaegeuk Kim 			diff = *count;
1657dd11a5dfSJaegeuk Kim 		*count -= diff;
16580abd675eSChao Yu 		release = diff;
16597e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
166046008c6dSChao Yu 		if (!*count) {
166139a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1662dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16630abd675eSChao Yu 			goto enospc;
166439a53e0cSJaegeuk Kim 		}
166546008c6dSChao Yu 	}
166639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
166741382ec4SJaegeuk Kim 
1668fab2adeeSLiFan 	if (unlikely(release))
16690abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16700abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16710abd675eSChao Yu 	return 0;
16720abd675eSChao Yu 
16730abd675eSChao Yu enospc:
16740abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16750abd675eSChao Yu 	return -ENOSPC;
167639a53e0cSJaegeuk Kim }
167739a53e0cSJaegeuk Kim 
1678da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
167939a53e0cSJaegeuk Kim 						struct inode *inode,
16800eb0adadSChao Yu 						block_t count)
168139a53e0cSJaegeuk Kim {
16820eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
16830eb0adadSChao Yu 
168439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16859850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
16860eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
168739a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
168880d42145SYunlong Song 	if (sbi->reserved_blocks &&
168980d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
169080d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
169180d42145SYunlong Song 					sbi->current_reserved_blocks + count);
169239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
16930abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
169439a53e0cSJaegeuk Kim }
169539a53e0cSJaegeuk Kim 
169639a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
169739a53e0cSJaegeuk Kim {
169835782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
16997c4abcbeSChao Yu 
170036951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
170136951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17027c4abcbeSChao Yu 		return;
17037c4abcbeSChao Yu 
1704caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
170539a53e0cSJaegeuk Kim }
170639a53e0cSJaegeuk Kim 
1707a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
170839a53e0cSJaegeuk Kim {
1709204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1710c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1711c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17122c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17132c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
171439a53e0cSJaegeuk Kim }
171539a53e0cSJaegeuk Kim 
171639a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
171739a53e0cSJaegeuk Kim {
171835782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
171939a53e0cSJaegeuk Kim }
172039a53e0cSJaegeuk Kim 
1721a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
172239a53e0cSJaegeuk Kim {
17235ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17245ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17251fe54f9dSJaegeuk Kim 		return;
17261fe54f9dSJaegeuk Kim 
1727204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1728c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1729c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17302c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17312c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
173239a53e0cSJaegeuk Kim }
173339a53e0cSJaegeuk Kim 
1734523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
173539a53e0cSJaegeuk Kim {
173635782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
173739a53e0cSJaegeuk Kim }
173839a53e0cSJaegeuk Kim 
1739204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1740f8b2c1f9SJaegeuk Kim {
1741204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1742f8b2c1f9SJaegeuk Kim }
1743f8b2c1f9SJaegeuk Kim 
17445ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17455ac206cfSNamjae Jeon {
17463519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1747523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1748523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1749523be8a6SJaegeuk Kim 
1750523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17515ac206cfSNamjae Jeon }
17525ac206cfSNamjae Jeon 
175339a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
175439a53e0cSJaegeuk Kim {
17558b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
175639a53e0cSJaegeuk Kim }
175739a53e0cSJaegeuk Kim 
1758f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1759f83a2584SYunlei He {
1760f83a2584SYunlei He 	return sbi->discard_blks;
1761f83a2584SYunlei He }
1762f83a2584SYunlei He 
176339a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
176439a53e0cSJaegeuk Kim {
176539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
176639a53e0cSJaegeuk Kim 
176739a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
176839a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
176939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
177039a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
177139a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
177239a53e0cSJaegeuk Kim 
177339a53e0cSJaegeuk Kim 	return 0;
177439a53e0cSJaegeuk Kim }
177539a53e0cSJaegeuk Kim 
177655141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
177755141486SWanpeng Li {
177855141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
177955141486SWanpeng Li }
178055141486SWanpeng Li 
178139a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
178239a53e0cSJaegeuk Kim {
178339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
17841dbe4152SChangman Lee 	int offset;
17851dbe4152SChangman Lee 
1786199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1787199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1788199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1789199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1790199bc3feSChao Yu 	}
1791199bc3feSChao Yu 
179255141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
17931dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
17941dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
17951dbe4152SChangman Lee 		else
179665b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
17971dbe4152SChangman Lee 	} else {
17981dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
179925ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
180039a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
180139a53e0cSJaegeuk Kim 	}
18021dbe4152SChangman Lee }
180339a53e0cSJaegeuk Kim 
180439a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
180539a53e0cSJaegeuk Kim {
18068508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
180739a53e0cSJaegeuk Kim 
18088508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
180939a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
181039a53e0cSJaegeuk Kim 	return start_addr;
181139a53e0cSJaegeuk Kim }
181239a53e0cSJaegeuk Kim 
18138508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
18148508e44aSJaegeuk Kim {
18158508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
18168508e44aSJaegeuk Kim 
18178508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
18188508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
18198508e44aSJaegeuk Kim 	return start_addr;
18208508e44aSJaegeuk Kim }
18218508e44aSJaegeuk Kim 
18228508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
18238508e44aSJaegeuk Kim {
18248508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
18258508e44aSJaegeuk Kim }
18268508e44aSJaegeuk Kim 
182739a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
182839a53e0cSJaegeuk Kim {
182939a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
183039a53e0cSJaegeuk Kim }
183139a53e0cSJaegeuk Kim 
18320abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1833000519f2SChao Yu 					struct inode *inode, bool is_inode)
183439a53e0cSJaegeuk Kim {
183539a53e0cSJaegeuk Kim 	block_t	valid_block_count;
183639a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18370abd675eSChao Yu 	bool quota = inode && !is_inode;
18380abd675eSChao Yu 
18390abd675eSChao Yu 	if (quota) {
18400abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18410abd675eSChao Yu 		if (ret)
18420abd675eSChao Yu 			return ret;
18430abd675eSChao Yu 	}
184439a53e0cSJaegeuk Kim 
1845812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1846812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1847812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1848812c6056SChao Yu 		goto enospc;
1849812c6056SChao Yu 	}
1850812c6056SChao Yu #endif
1851812c6056SChao Yu 
185239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
185339a53e0cSJaegeuk Kim 
18547e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18557e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18567e65be49SJaegeuk Kim 
1857d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
18587e65be49SJaegeuk Kim 		valid_block_count += sbi->root_reserved_blocks;
18597e65be49SJaegeuk Kim 
18607e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
186139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18620abd675eSChao Yu 		goto enospc;
186339a53e0cSJaegeuk Kim 	}
186439a53e0cSJaegeuk Kim 
1865ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1866cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
186739a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18680abd675eSChao Yu 		goto enospc;
186939a53e0cSJaegeuk Kim 	}
187039a53e0cSJaegeuk Kim 
1871ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1872ef86d709SGu Zheng 	sbi->total_valid_block_count++;
187339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
187439a53e0cSJaegeuk Kim 
18750abd675eSChao Yu 	if (inode) {
18760abd675eSChao Yu 		if (is_inode)
18770abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
18780abd675eSChao Yu 		else
18790abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
18800abd675eSChao Yu 	}
18810abd675eSChao Yu 
188241382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
18830abd675eSChao Yu 	return 0;
18840abd675eSChao Yu 
18850abd675eSChao Yu enospc:
18860abd675eSChao Yu 	if (quota)
18870abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
18880abd675eSChao Yu 	return -ENOSPC;
188939a53e0cSJaegeuk Kim }
189039a53e0cSJaegeuk Kim 
189139a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1892000519f2SChao Yu 					struct inode *inode, bool is_inode)
189339a53e0cSJaegeuk Kim {
189439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
189539a53e0cSJaegeuk Kim 
18969850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
18979850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1898000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
189939a53e0cSJaegeuk Kim 
1900ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1901ef86d709SGu Zheng 	sbi->total_valid_block_count--;
190280d42145SYunlong Song 	if (sbi->reserved_blocks &&
190380d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
190480d42145SYunlong Song 		sbi->current_reserved_blocks++;
190539a53e0cSJaegeuk Kim 
190639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19070abd675eSChao Yu 
19080abd675eSChao Yu 	if (!is_inode)
19090abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
191039a53e0cSJaegeuk Kim }
191139a53e0cSJaegeuk Kim 
191239a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
191339a53e0cSJaegeuk Kim {
19148b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
191539a53e0cSJaegeuk Kim }
191639a53e0cSJaegeuk Kim 
191739a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
191839a53e0cSJaegeuk Kim {
1919513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
192039a53e0cSJaegeuk Kim }
192139a53e0cSJaegeuk Kim 
19220e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
192339a53e0cSJaegeuk Kim {
1924513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
192539a53e0cSJaegeuk Kim }
192639a53e0cSJaegeuk Kim 
1927513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
192839a53e0cSJaegeuk Kim {
1929513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
193039a53e0cSJaegeuk Kim }
193139a53e0cSJaegeuk Kim 
1932a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1933a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1934a56c7c6fSJaegeuk Kim {
1935c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1936c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1937cac5a3d8SDongOh Shin 
1938c41f3cc3SJaegeuk Kim 	if (page)
1939c41f3cc3SJaegeuk Kim 		return page;
1940c41f3cc3SJaegeuk Kim 
194155523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
194255523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1943c41f3cc3SJaegeuk Kim 		return NULL;
194455523519SChao Yu 	}
1945c41f3cc3SJaegeuk Kim #endif
1946a56c7c6fSJaegeuk Kim 	if (!for_write)
1947a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1948a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1949a56c7c6fSJaegeuk Kim }
1950a56c7c6fSJaegeuk Kim 
195101eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
195201eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
195301eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
195401eccef7SChao Yu {
195501eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
195601eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
195701eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
195801eccef7SChao Yu 		return NULL;
195901eccef7SChao Yu 	}
196001eccef7SChao Yu #endif
196101eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
196201eccef7SChao Yu }
196301eccef7SChao Yu 
19646e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19656e2c64adSJaegeuk Kim {
19666e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19676e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19686e2c64adSJaegeuk Kim 
19696e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19706e2c64adSJaegeuk Kim 	kunmap(dst);
19716e2c64adSJaegeuk Kim 	kunmap(src);
19726e2c64adSJaegeuk Kim }
19736e2c64adSJaegeuk Kim 
197439a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
197539a53e0cSJaegeuk Kim {
1976031fa8ccSJaegeuk Kim 	if (!page)
197739a53e0cSJaegeuk Kim 		return;
197839a53e0cSJaegeuk Kim 
197939a53e0cSJaegeuk Kim 	if (unlock) {
19809850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
198139a53e0cSJaegeuk Kim 		unlock_page(page);
198239a53e0cSJaegeuk Kim 	}
198309cbfeafSKirill A. Shutemov 	put_page(page);
198439a53e0cSJaegeuk Kim }
198539a53e0cSJaegeuk Kim 
198639a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
198739a53e0cSJaegeuk Kim {
198839a53e0cSJaegeuk Kim 	if (dn->node_page)
198939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
199039a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
199139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
199239a53e0cSJaegeuk Kim 	dn->node_page = NULL;
199339a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
199439a53e0cSJaegeuk Kim }
199539a53e0cSJaegeuk Kim 
199639a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1997e8512d2eSGu Zheng 					size_t size)
199839a53e0cSJaegeuk Kim {
1999e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
200039a53e0cSJaegeuk Kim }
200139a53e0cSJaegeuk Kim 
20027bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20037bd59381SGu Zheng 						gfp_t flags)
20047bd59381SGu Zheng {
20057bd59381SGu Zheng 	void *entry;
20067bd59381SGu Zheng 
200780c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
200880c54505SJaegeuk Kim 	if (!entry)
200980c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
20107bd59381SGu Zheng 	return entry;
20117bd59381SGu Zheng }
20127bd59381SGu Zheng 
2013d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2014d62fe971SChao Yu 						int npages, bool no_fail)
2015740432f8SJaegeuk Kim {
2016740432f8SJaegeuk Kim 	struct bio *bio;
2017740432f8SJaegeuk Kim 
2018d62fe971SChao Yu 	if (no_fail) {
2019740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2020740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
202180c54505SJaegeuk Kim 		if (!bio)
202280c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2023740432f8SJaegeuk Kim 		return bio;
2024740432f8SJaegeuk Kim 	}
2025d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2026d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2027d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2028d62fe971SChao Yu 		return NULL;
2029d62fe971SChao Yu 	}
2030d62fe971SChao Yu #endif
2031d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2032d62fe971SChao Yu }
2033740432f8SJaegeuk Kim 
20349be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20359be32d72SJaegeuk Kim 				unsigned long index, void *item)
20369be32d72SJaegeuk Kim {
20379be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20389be32d72SJaegeuk Kim 		cond_resched();
20399be32d72SJaegeuk Kim }
20409be32d72SJaegeuk Kim 
204139a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
204239a53e0cSJaegeuk Kim 
204339a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
204439a53e0cSJaegeuk Kim {
204545590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2046cac5a3d8SDongOh Shin 
204739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
204839a53e0cSJaegeuk Kim }
204939a53e0cSJaegeuk Kim 
20507a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20517a2af766SChao Yu {
20527a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20537a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20547a2af766SChao Yu }
20557a2af766SChao Yu 
205639a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
205739a53e0cSJaegeuk Kim {
205839a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
205939a53e0cSJaegeuk Kim }
206039a53e0cSJaegeuk Kim 
20617a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20627a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20637a2af766SChao Yu 			struct page *node_page, unsigned int offset)
206439a53e0cSJaegeuk Kim {
206539a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
206639a53e0cSJaegeuk Kim 	__le32 *addr_array;
20677a2af766SChao Yu 	int base = 0;
20687a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2069cac5a3d8SDongOh Shin 
207045590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20717a2af766SChao Yu 
20727a2af766SChao Yu 	/* from GC path only */
2073979f492fSLiFan 	if (is_inode) {
2074979f492fSLiFan 		if (!inode)
20757a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2076979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20777a2af766SChao Yu 			base = get_extra_isize(inode);
20787a2af766SChao Yu 	}
20797a2af766SChao Yu 
208039a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
20817a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
208239a53e0cSJaegeuk Kim }
208339a53e0cSJaegeuk Kim 
208439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
208539a53e0cSJaegeuk Kim {
208639a53e0cSJaegeuk Kim 	int mask;
208739a53e0cSJaegeuk Kim 
208839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
208939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
209039a53e0cSJaegeuk Kim 	return mask & *addr;
209139a53e0cSJaegeuk Kim }
209239a53e0cSJaegeuk Kim 
2093a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2094a66cdd98SJaegeuk Kim {
2095a66cdd98SJaegeuk Kim 	int mask;
2096a66cdd98SJaegeuk Kim 
2097a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2098a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2099a66cdd98SJaegeuk Kim 	*addr |= mask;
2100a66cdd98SJaegeuk Kim }
2101a66cdd98SJaegeuk Kim 
2102a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2103a66cdd98SJaegeuk Kim {
2104a66cdd98SJaegeuk Kim 	int mask;
2105a66cdd98SJaegeuk Kim 
2106a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2107a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2108a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2109a66cdd98SJaegeuk Kim }
2110a66cdd98SJaegeuk Kim 
211152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
211239a53e0cSJaegeuk Kim {
211339a53e0cSJaegeuk Kim 	int mask;
211439a53e0cSJaegeuk Kim 	int ret;
211539a53e0cSJaegeuk Kim 
211639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
211739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
211839a53e0cSJaegeuk Kim 	ret = mask & *addr;
211939a53e0cSJaegeuk Kim 	*addr |= mask;
212039a53e0cSJaegeuk Kim 	return ret;
212139a53e0cSJaegeuk Kim }
212239a53e0cSJaegeuk Kim 
212352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
212439a53e0cSJaegeuk Kim {
212539a53e0cSJaegeuk Kim 	int mask;
212639a53e0cSJaegeuk Kim 	int ret;
212739a53e0cSJaegeuk Kim 
212839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
212939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
213039a53e0cSJaegeuk Kim 	ret = mask & *addr;
213139a53e0cSJaegeuk Kim 	*addr &= ~mask;
213239a53e0cSJaegeuk Kim 	return ret;
213339a53e0cSJaegeuk Kim }
213439a53e0cSJaegeuk Kim 
2135c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2136c6ac4c0eSGu Zheng {
2137c6ac4c0eSGu Zheng 	int mask;
2138c6ac4c0eSGu Zheng 
2139c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2140c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2141c6ac4c0eSGu Zheng 	*addr ^= mask;
2142c6ac4c0eSGu Zheng }
2143c6ac4c0eSGu Zheng 
21445c57132eSChao Yu #define F2FS_REG_FLMASK		(~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
21455c57132eSChao Yu #define F2FS_OTHER_FLMASK	(FS_NODUMP_FL | FS_NOATIME_FL)
21465c57132eSChao Yu #define F2FS_FL_INHERITED	(FS_PROJINHERIT_FL)
21475c57132eSChao Yu 
21485c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
21495c57132eSChao Yu {
21505c57132eSChao Yu 	if (S_ISDIR(mode))
21515c57132eSChao Yu 		return flags;
21525c57132eSChao Yu 	else if (S_ISREG(mode))
21535c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
21545c57132eSChao Yu 	else
21555c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
21565c57132eSChao Yu }
21575c57132eSChao Yu 
215839a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
215939a53e0cSJaegeuk Kim enum {
216039a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2161b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
216226de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2163ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
216439a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
216539a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
216639a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2167c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2168c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2169444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
21701001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
217134d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2172fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2173fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
217488b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
217588b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
21765fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
217702a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
21783c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
21791e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2180b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2181510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2182d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2183c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2184dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2185ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
21867a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
21875c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
21881ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
218939a53e0cSJaegeuk Kim };
219039a53e0cSJaegeuk Kim 
2191205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2192205b9822SJaegeuk Kim 						int flag, bool set)
219339a53e0cSJaegeuk Kim {
2194205b9822SJaegeuk Kim 	switch (flag) {
2195205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2196205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2197205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
21989ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2199205b9822SJaegeuk Kim 		if (set)
2200205b9822SJaegeuk Kim 			return;
2201205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2202205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
22031ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
22047c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2205205b9822SJaegeuk Kim 	}
220639a53e0cSJaegeuk Kim }
220739a53e0cSJaegeuk Kim 
220891942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
220939a53e0cSJaegeuk Kim {
221091942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
221191942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2212205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
221339a53e0cSJaegeuk Kim }
221439a53e0cSJaegeuk Kim 
221591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
221639a53e0cSJaegeuk Kim {
221791942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
221839a53e0cSJaegeuk Kim }
221939a53e0cSJaegeuk Kim 
222091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
222139a53e0cSJaegeuk Kim {
222291942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
222391942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2224205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
222539a53e0cSJaegeuk Kim }
222639a53e0cSJaegeuk Kim 
222791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2228444c580fSJaegeuk Kim {
222991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
223091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
22317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
223239a53e0cSJaegeuk Kim }
223339a53e0cSJaegeuk Kim 
2234a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2235a1961246SJaegeuk Kim {
2236a1961246SJaegeuk Kim 	if (inc)
2237a1961246SJaegeuk Kim 		inc_nlink(inode);
2238a1961246SJaegeuk Kim 	else
2239a1961246SJaegeuk Kim 		drop_nlink(inode);
22407c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2241a1961246SJaegeuk Kim }
2242a1961246SJaegeuk Kim 
22438edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
22440abd675eSChao Yu 					block_t diff, bool add, bool claim)
22458edd03c8SJaegeuk Kim {
224626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
224726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
224826de9b11SJaegeuk Kim 
22490abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
22500abd675eSChao Yu 	if (add) {
22510abd675eSChao Yu 		if (claim)
22520abd675eSChao Yu 			dquot_claim_block(inode, diff);
22530abd675eSChao Yu 		else
22540abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
22550abd675eSChao Yu 	} else {
22560abd675eSChao Yu 		dquot_free_block(inode, diff);
22570abd675eSChao Yu 	}
22580abd675eSChao Yu 
22597c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
226026de9b11SJaegeuk Kim 	if (clean || recover)
226126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
22628edd03c8SJaegeuk Kim }
22638edd03c8SJaegeuk Kim 
2264fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2265fc9581c8SJaegeuk Kim {
226626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
226726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
226826de9b11SJaegeuk Kim 
2269fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2270fc9581c8SJaegeuk Kim 		return;
2271fc9581c8SJaegeuk Kim 
2272fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
22737c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
227426de9b11SJaegeuk Kim 	if (clean || recover)
227526de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
227626de9b11SJaegeuk Kim }
227726de9b11SJaegeuk Kim 
2278205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2279205b9822SJaegeuk Kim {
2280205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
22817c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2282205b9822SJaegeuk Kim }
2283205b9822SJaegeuk Kim 
22841ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
22851ad71a27SJaegeuk Kim 					unsigned int count)
22861ad71a27SJaegeuk Kim {
22871ad71a27SJaegeuk Kim 	F2FS_I(inode)->i_gc_failures = count;
22881ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
22891ad71a27SJaegeuk Kim }
22901ad71a27SJaegeuk Kim 
2291205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2292205b9822SJaegeuk Kim {
2293205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
22947c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2295205b9822SJaegeuk Kim }
2296205b9822SJaegeuk Kim 
2297205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2298205b9822SJaegeuk Kim {
2299205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
23007c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2301205b9822SJaegeuk Kim }
2302205b9822SJaegeuk Kim 
230391942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2304444c580fSJaegeuk Kim {
2305205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2306205b9822SJaegeuk Kim 
2307444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2308205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
23091001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2310205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
231134d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2312205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2313b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2314205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2315510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2316205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
23177a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
23187a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
23191ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
23201ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2321444c580fSJaegeuk Kim }
2322444c580fSJaegeuk Kim 
232391942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2324444c580fSJaegeuk Kim {
2325444c580fSJaegeuk Kim 	ri->i_inline = 0;
2326444c580fSJaegeuk Kim 
232791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2328444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
232991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
23301001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
233191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
233234d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
233391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2334b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
233591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2336510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
23377a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
23387a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
23391ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
23401ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
23417a2af766SChao Yu }
23427a2af766SChao Yu 
23437a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
23447a2af766SChao Yu {
23457a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2346444c580fSJaegeuk Kim }
2347444c580fSJaegeuk Kim 
2348987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2349987c7c31SChao Yu {
235091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2351987c7c31SChao Yu }
2352987c7c31SChao Yu 
235381ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2354de93653fSJaegeuk Kim {
2355b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2356de93653fSJaegeuk Kim }
2357de93653fSJaegeuk Kim 
23586afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
235965985d93SJaegeuk Kim {
2360695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2361cac5a3d8SDongOh Shin 
236265985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2363b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
236465985d93SJaegeuk Kim }
236565985d93SJaegeuk Kim 
236665985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
236765985d93SJaegeuk Kim {
23686afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
236965985d93SJaegeuk Kim }
237065985d93SJaegeuk Kim 
23710dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
23720dbdc2aeSJaegeuk Kim {
237391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
23740dbdc2aeSJaegeuk Kim }
23750dbdc2aeSJaegeuk Kim 
2376b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2377b3d208f9SJaegeuk Kim {
237891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2379b3d208f9SJaegeuk Kim }
2380b3d208f9SJaegeuk Kim 
2381510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2382510022a8SJaegeuk Kim {
238391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2384510022a8SJaegeuk Kim }
2385510022a8SJaegeuk Kim 
23861ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
23871ad71a27SJaegeuk Kim {
23881ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
23891ad71a27SJaegeuk Kim }
23901ad71a27SJaegeuk Kim 
239188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
239288b88a66SJaegeuk Kim {
239391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
239488b88a66SJaegeuk Kim }
239588b88a66SJaegeuk Kim 
23965fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
23975fe45743SChao Yu {
23985fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
23995fe45743SChao Yu }
24005fe45743SChao Yu 
240102a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
240202a1335fSJaegeuk Kim {
240391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
240402a1335fSJaegeuk Kim }
240502a1335fSJaegeuk Kim 
24063c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
24073c6c2bebSJaegeuk Kim {
240891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
24093c6c2bebSJaegeuk Kim }
24103c6c2bebSJaegeuk Kim 
24111e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
24121e84371fSJaegeuk Kim {
241391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
24141e84371fSJaegeuk Kim }
24151e84371fSJaegeuk Kim 
2416f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
24171001b347SHuajun Li {
2418695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
24197a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2420cac5a3d8SDongOh Shin 
24217a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
24221001b347SHuajun Li }
24231001b347SHuajun Li 
242434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
242534d67debSChao Yu {
242691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
242734d67debSChao Yu }
242834d67debSChao Yu 
2429b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2430b5492af7SJaegeuk Kim {
2431b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2432b5492af7SJaegeuk Kim }
2433b5492af7SJaegeuk Kim 
2434b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2435b5492af7SJaegeuk Kim {
2436b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
24377c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2438b5492af7SJaegeuk Kim }
2439b5492af7SJaegeuk Kim 
2440b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2441b5492af7SJaegeuk Kim {
2442b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
24437c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2444b5492af7SJaegeuk Kim }
2445b5492af7SJaegeuk Kim 
244626787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
244726787236SJaegeuk Kim {
2448a0d00fadSChao Yu 	bool ret;
2449a0d00fadSChao Yu 
245026787236SJaegeuk Kim 	if (dsync) {
245126787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
245226787236SJaegeuk Kim 
245326787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
245426787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
245526787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
245626787236SJaegeuk Kim 		return ret;
245726787236SJaegeuk Kim 	}
245826787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
245926787236SJaegeuk Kim 			file_keep_isize(inode) ||
246026787236SJaegeuk Kim 			i_size_read(inode) & PAGE_MASK)
246126787236SJaegeuk Kim 		return false;
2462a0d00fadSChao Yu 
2463a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2464a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2465a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2466a0d00fadSChao Yu 
2467a0d00fadSChao Yu 	return ret;
2468267378d4SChao Yu }
2469267378d4SChao Yu 
2470267378d4SChao Yu static inline int f2fs_readonly(struct super_block *sb)
2471267378d4SChao Yu {
24721751e8a6SLinus Torvalds 	return sb->s_flags & SB_RDONLY;
247377888c1eSJaegeuk Kim }
247477888c1eSJaegeuk Kim 
2475744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2476744602cfSJaegeuk Kim {
2477aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2478744602cfSJaegeuk Kim }
2479744602cfSJaegeuk Kim 
2480eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2481eaa693f4SJaegeuk Kim {
2482eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2483eaa693f4SJaegeuk Kim 		return true;
2484eaa693f4SJaegeuk Kim 
2485eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2486eaa693f4SJaegeuk Kim 		return true;
2487eaa693f4SJaegeuk Kim 
2488eaa693f4SJaegeuk Kim 	return false;
2489eaa693f4SJaegeuk Kim }
2490eaa693f4SJaegeuk Kim 
24913e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
24923e72f721SJaegeuk Kim {
24933e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
249491942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
24953e72f721SJaegeuk Kim 		return false;
24963e72f721SJaegeuk Kim 
2497886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
24983e72f721SJaegeuk Kim }
24993e72f721SJaegeuk Kim 
25001ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
25011ecc0c5cSChao Yu 					size_t size, gfp_t flags)
25020414b004SJaegeuk Kim {
25032c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
250455523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
250555523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
25062c63feadSJaegeuk Kim 		return NULL;
250755523519SChao Yu 	}
25082c63feadSJaegeuk Kim #endif
25090414b004SJaegeuk Kim 	return kmalloc(size, flags);
25100414b004SJaegeuk Kim }
25110414b004SJaegeuk Kim 
2512acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2513acbf054dSChao Yu 					size_t size, gfp_t flags)
2514acbf054dSChao Yu {
2515acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2516acbf054dSChao Yu }
2517acbf054dSChao Yu 
2518628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2519628b3d14SChao Yu 					size_t size, gfp_t flags)
2520628b3d14SChao Yu {
2521628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2522628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2523628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2524628b3d14SChao Yu 		return NULL;
2525628b3d14SChao Yu 	}
2526628b3d14SChao Yu #endif
2527628b3d14SChao Yu 	return kvmalloc(size, flags);
2528628b3d14SChao Yu }
2529628b3d14SChao Yu 
2530628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2531628b3d14SChao Yu 					size_t size, gfp_t flags)
2532628b3d14SChao Yu {
2533628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2534628b3d14SChao Yu }
2535628b3d14SChao Yu 
25367a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2537f2470371SChao Yu {
25387a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2539f2470371SChao Yu }
2540f2470371SChao Yu 
25416afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
25426afc662eSChao Yu {
25436afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
25446afc662eSChao Yu }
25456afc662eSChao Yu 
2546a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
254791942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2548a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2549a6dda0e6SChristoph Hellwig 
25507a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
25517a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
25527a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
25537a2af766SChao Yu 
25545c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
25555c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
25565c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
25575c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
25585c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
25595c57132eSChao Yu 
2560b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2561b0af6d49SChao Yu {
2562b0af6d49SChao Yu 	int i;
2563b0af6d49SChao Yu 
2564b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2565b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2566b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2567b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2568b0af6d49SChao Yu }
2569b0af6d49SChao Yu 
2570b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2571b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2572b0af6d49SChao Yu {
2573b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2574b0af6d49SChao Yu 		return;
2575b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2576b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2577b0af6d49SChao Yu 
2578b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2579b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2580b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2581b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2582b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2583b0af6d49SChao Yu }
2584b0af6d49SChao Yu 
258539a53e0cSJaegeuk Kim /*
258639a53e0cSJaegeuk Kim  * file.c
258739a53e0cSJaegeuk Kim  */
2588cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2589cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2590cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2591cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2592a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2593a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2594cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2595cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2596f652e9d9SChao Yu void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2597c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2598cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2599cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
26001ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
260139a53e0cSJaegeuk Kim 
260239a53e0cSJaegeuk Kim /*
260339a53e0cSJaegeuk Kim  * inode.c
260439a53e0cSJaegeuk Kim  */
2605cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2606704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2607704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2608cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2609cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2610cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2611211a6fa0SYunlei He void update_inode(struct inode *inode, struct page *node_page);
2612211a6fa0SYunlei He void update_inode_page(struct inode *inode);
2613cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2614cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2615cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
261639a53e0cSJaegeuk Kim 
261739a53e0cSJaegeuk Kim /*
261839a53e0cSJaegeuk Kim  * namei.c
261939a53e0cSJaegeuk Kim  */
2620846ae671SChao Yu int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2621*b6a06cbbSChao Yu 							bool hot, bool set);
262239a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
262339a53e0cSJaegeuk Kim 
262439a53e0cSJaegeuk Kim /*
262539a53e0cSJaegeuk Kim  * dir.c
262639a53e0cSJaegeuk Kim  */
2627cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2628cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2629cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2630cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2631cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2632cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2633cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2634cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2635cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2636cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2637cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2638cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2639cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2640cac5a3d8SDongOh Shin 			unsigned int current_depth);
2641cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2642cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2643cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2644cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2645cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2646cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2647cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2648cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2649cac5a3d8SDongOh Shin 			struct page **page);
2650cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2651cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2652cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2653cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2654cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2655cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2656cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2657cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2658cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2659cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2660cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2661cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2662cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2663cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2664cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2665cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
266639a53e0cSJaegeuk Kim 
2667b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2668b7f7a5e0SAl Viro {
26692b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2670510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2671b7f7a5e0SAl Viro }
2672b7f7a5e0SAl Viro 
267339a53e0cSJaegeuk Kim /*
267439a53e0cSJaegeuk Kim  * super.c
267539a53e0cSJaegeuk Kim  */
2676cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2677cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2678ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
26794b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2680cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2681cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2682a07ef784SNamjae Jeon extern __printf(3, 4)
2683cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2684984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
268539a53e0cSJaegeuk Kim 
268639a53e0cSJaegeuk Kim /*
268739a53e0cSJaegeuk Kim  * hash.c
268839a53e0cSJaegeuk Kim  */
26896332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
26906332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
269139a53e0cSJaegeuk Kim 
269239a53e0cSJaegeuk Kim /*
269339a53e0cSJaegeuk Kim  * node.c
269439a53e0cSJaegeuk Kim  */
269539a53e0cSJaegeuk Kim struct dnode_of_data;
269639a53e0cSJaegeuk Kim struct node_info;
269739a53e0cSJaegeuk Kim 
2698cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2699cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2700cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2701cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2702cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2703cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2704cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2705cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
27069c77f754SJaegeuk Kim int truncate_xattr_node(struct inode *inode);
2707cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2708cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2709cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
27105f4ce6abSYunlei He struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2711cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2712cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2713cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2714cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2715cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2716cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2717401db79fSYunlong Song int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2718b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
271922ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2720cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2721cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2722cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2723cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2724cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2725e17d488bSSheng Yong int recover_xattr_data(struct inode *inode, struct page *page);
2726cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2727c376fc0fSYunlei He void restore_node_summary(struct f2fs_sb_info *sbi,
2728cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
272922ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2730cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2731cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
27326e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
273339a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
273439a53e0cSJaegeuk Kim 
273539a53e0cSJaegeuk Kim /*
273639a53e0cSJaegeuk Kim  * segment.c
273739a53e0cSJaegeuk Kim  */
2738b3a97a2aSJaegeuk Kim bool need_SSR(struct f2fs_sb_info *sbi);
2739cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
274057864ae5SJaegeuk Kim void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
2741cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
27428c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2743cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2744cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2745cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
274639d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2747cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
27481228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2749cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2750cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2751cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
275278997b56SChao Yu void init_discard_policy(struct discard_policy *dpolicy, int discard_type,
275378997b56SChao Yu 						unsigned int granularity);
27547950e9acSChao Yu void drop_discard_cmd(struct f2fs_sb_info *sbi);
2755cce13252SChao Yu void stop_discard_thread(struct f2fs_sb_info *sbi);
2756cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2757cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2758cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2759cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2760cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2761cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2762cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2763cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2764cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2765b0af6d49SChao Yu void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2766b0af6d49SChao Yu 						enum iostat_type io_type);
2767cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2768cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2769d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2770cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2771cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2772cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2773cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2774cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2775cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2776cac5a3d8SDongOh Shin 			bool recover_newaddr);
2777cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2778cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2779fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2780fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2781cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2782cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2783d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2784cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2785cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2786cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2787cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2788cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2789cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2790cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
27917fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
27927fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
2793d5097be5SHyunchul Lee int rw_hint_to_seg_type(enum rw_hint hint);
27940cdd3195SHyunchul Lee enum rw_hint io_type_to_rw_hint(struct f2fs_sb_info *sbi, enum page_type type,
27950cdd3195SHyunchul Lee 				enum temp_type temp);
279639a53e0cSJaegeuk Kim 
279739a53e0cSJaegeuk Kim /*
279839a53e0cSJaegeuk Kim  * checkpoint.c
279939a53e0cSJaegeuk Kim  */
2800cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2801cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2802cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2803cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2804cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2805cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2806cac5a3d8SDongOh Shin 			int type, bool sync);
2807cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2808cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2809b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
2810cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2811cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2812cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2813cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
281439d787beSChao Yu void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
281539d787beSChao Yu 					unsigned int devidx, int type);
281639d787beSChao Yu bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
281739d787beSChao Yu 					unsigned int devidx, int type);
2818cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2819cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2820cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2821cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2822cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2823cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2824cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2825cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2826cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2827cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2828cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2829cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
28306e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
283139a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
283239a53e0cSJaegeuk Kim 
283339a53e0cSJaegeuk Kim /*
283439a53e0cSJaegeuk Kim  * data.c
283539a53e0cSJaegeuk Kim  */
2836b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2837b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2838942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2839b9109b0eSJaegeuk Kim 				enum page_type type);
2840b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2841cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2842b9109b0eSJaegeuk Kim int f2fs_submit_page_write(struct f2fs_io_info *fio);
2843cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2844cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2845cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2846cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2847cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2848cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2849cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2850cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2851cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2852cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2853cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2854cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2855cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2856cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2857cac5a3d8SDongOh Shin 			bool for_write);
2858cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2859cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2860cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2861cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2862cac5a3d8SDongOh Shin 			int create, int flag);
2863cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2864cac5a3d8SDongOh Shin 			u64 start, u64 len);
2865bb9e3bb8SChao Yu bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2866bb9e3bb8SChao Yu bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2867cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2868b0af6d49SChao Yu int __f2fs_write_data_pages(struct address_space *mapping,
2869b0af6d49SChao Yu 						struct writeback_control *wbc,
2870b0af6d49SChao Yu 						enum iostat_type io_type);
2871cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2872cac5a3d8SDongOh Shin 			unsigned int length);
2873cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
28745b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2875cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2876cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
28775b7a487cSWeichao Guo #endif
287839a53e0cSJaegeuk Kim 
287939a53e0cSJaegeuk Kim /*
288039a53e0cSJaegeuk Kim  * gc.c
288139a53e0cSJaegeuk Kim  */
2882cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2883cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2884cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2885e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2886e066b83cSJaegeuk Kim 			unsigned int segno);
2887cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
288839a53e0cSJaegeuk Kim 
288939a53e0cSJaegeuk Kim /*
289039a53e0cSJaegeuk Kim  * recovery.c
289139a53e0cSJaegeuk Kim  */
2892cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2893cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
289439a53e0cSJaegeuk Kim 
289539a53e0cSJaegeuk Kim /*
289639a53e0cSJaegeuk Kim  * debug.c
289739a53e0cSJaegeuk Kim  */
289839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
289939a53e0cSJaegeuk Kim struct f2fs_stat_info {
290039a53e0cSJaegeuk Kim 	struct list_head stat_list;
290139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
290239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
290339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
29045b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
29055b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2906c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
29072c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
29082c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
290935782b23SJaegeuk Kim 	int inmem_pages;
29102c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
29115b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
29125b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
291339a53e0cSJaegeuk Kim 	int total_count, utilization;
29148b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
291514d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
291614d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
29175f32366aSChao Yu 	int nr_discard_cmd;
2918d84d1cbdSChao Yu 	unsigned int undiscard_blks;
2919a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2920648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2921f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
292239a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
292339a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
292439a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
292539a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
292642190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
292739a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2928e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
292939a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2930e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
293139a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
293239a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
293339a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
293439a53e0cSJaegeuk Kim 
293539a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
293639a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2937b9a2c252SChangman Lee 	unsigned int inplace_count;
29389edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
293939a53e0cSJaegeuk Kim };
294039a53e0cSJaegeuk Kim 
2941963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2942963d4f7dSGu Zheng {
2943963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2944963d4f7dSGu Zheng }
2945963d4f7dSGu Zheng 
2946942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
294742190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
294839a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2949dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
295033fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
295133fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
29525b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
29535b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
29545b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
29555b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2956d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2957d5e8f6c9SChao Yu 	do {								\
2958d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2959d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2960d5e8f6c9SChao Yu 	} while (0)
2961d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2962d5e8f6c9SChao Yu 	do {								\
2963d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2964d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2965d5e8f6c9SChao Yu 	} while (0)
29660dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
29670dbdc2aeSJaegeuk Kim 	do {								\
29680dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
296903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
29700dbdc2aeSJaegeuk Kim 	} while (0)
29710dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
29720dbdc2aeSJaegeuk Kim 	do {								\
29730dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
297403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
29750dbdc2aeSJaegeuk Kim 	} while (0)
29763289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
29773289c061SJaegeuk Kim 	do {								\
29783289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
297903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
29803289c061SJaegeuk Kim 	} while (0)
29813289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
29823289c061SJaegeuk Kim 	do {								\
29833289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
298403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
29853289c061SJaegeuk Kim 	} while (0)
2986dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2987dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2988dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2989dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2990b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2991b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
299226a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
2993cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
299426a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
2995cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
299626a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
299726a28a0cSJaegeuk Kim 	do {								\
299826a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
299926a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
300026a28a0cSJaegeuk Kim 		if (cur > max)						\
300126a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
300226a28a0cSJaegeuk Kim 	} while (0)
3003648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3004648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3005648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3006648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3007648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3008648d50baSChao Yu 	do {								\
3009648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3010648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3011648d50baSChao Yu 		if (cur > max)						\
3012648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3013648d50baSChao Yu 	} while (0)
3014e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
301539a53e0cSJaegeuk Kim 	do {								\
3016963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
301768afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
301868afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
301939a53e0cSJaegeuk Kim 			si->data_segs++;				\
3020e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3021e1235983SChangman Lee 		} else {						\
302239a53e0cSJaegeuk Kim 			si->node_segs++;				\
3023e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3024e1235983SChangman Lee 		}							\
302539a53e0cSJaegeuk Kim 	} while (0)
302639a53e0cSJaegeuk Kim 
302739a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
302868afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
302939a53e0cSJaegeuk Kim 
3030e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
303139a53e0cSJaegeuk Kim 	do {								\
3032963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
303339a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
303439a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
303568afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
303639a53e0cSJaegeuk Kim 	} while (0)
303739a53e0cSJaegeuk Kim 
3038e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
303939a53e0cSJaegeuk Kim 	do {								\
3040963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
304139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
304239a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
304368afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
304439a53e0cSJaegeuk Kim 	} while (0)
304539a53e0cSJaegeuk Kim 
3046cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3047cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3048787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
30494589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
305039a53e0cSJaegeuk Kim #else
3051d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3052d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3053d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3054d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3055d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3056d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3057d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3058d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3059d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3060d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3061d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3062d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3063d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3064d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3065d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3066d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3067d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3068d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3069d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3070d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3071d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3072d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3073d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3074d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3075d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3076d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3077d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3078d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3079d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
308039a53e0cSJaegeuk Kim 
308139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
308239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3083787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
30844589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
308539a53e0cSJaegeuk Kim #endif
308639a53e0cSJaegeuk Kim 
308739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
308839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
308939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
309039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
309139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
309239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
309339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
309439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3095cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
309639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
309729e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
30981001b347SHuajun Li 
3099e18c65b2SHuajun Li /*
3100e18c65b2SHuajun Li  * inline.c
3101e18c65b2SHuajun Li  */
3102cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3103cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
3104cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
3105bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3106cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3107cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3108cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3109cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
3110cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
3111cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3112cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
3113cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3114cac5a3d8SDongOh Shin 			struct page *ipage);
3115cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3116cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3117cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3118cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3119cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3120cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3121cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3122cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3123cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3124cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3125cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3126cde4de12SJaegeuk Kim 
3127cde4de12SJaegeuk Kim /*
31282658e50dSJaegeuk Kim  * shrinker.c
31292658e50dSJaegeuk Kim  */
3130cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3131cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3132cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3133cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3134cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3135cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
31362658e50dSJaegeuk Kim 
31372658e50dSJaegeuk Kim /*
3138a28ef1f5SChao Yu  * extent_cache.c
3139a28ef1f5SChao Yu  */
3140004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3141004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
3142004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3143004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3144004b6862SChao Yu 				unsigned int ofs);
3145004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3146004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3147004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3148004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3149004b6862SChao Yu 		bool force);
3150df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3151df0f6b44SChao Yu 						struct rb_root *root);
3152cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3153cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3154cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3155cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3156cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3157cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3158cac5a3d8SDongOh Shin 			struct extent_info *ei);
3159cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
316019b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3161cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3162cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
3163a28ef1f5SChao Yu int __init create_extent_cache(void);
3164a28ef1f5SChao Yu void destroy_extent_cache(void);
3165a28ef1f5SChao Yu 
3166a28ef1f5SChao Yu /*
31678ceffcb2SChao Yu  * sysfs.c
31688ceffcb2SChao Yu  */
3169dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3170dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3171dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3172dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
31738ceffcb2SChao Yu 
31748ceffcb2SChao Yu /*
3175cde4de12SJaegeuk Kim  * crypto support
3176cde4de12SJaegeuk Kim  */
31770b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3178cde4de12SJaegeuk Kim {
3179cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3180cde4de12SJaegeuk Kim }
3181cde4de12SJaegeuk Kim 
31821958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
31831958593eSJaegeuk Kim {
31841958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
31851958593eSJaegeuk Kim }
31861958593eSJaegeuk Kim 
3187cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3188cde4de12SJaegeuk Kim {
3189cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3190cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
31912ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3192cde4de12SJaegeuk Kim #endif
3193cde4de12SJaegeuk Kim }
3194cde4de12SJaegeuk Kim 
3195cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
3196cde4de12SJaegeuk Kim {
31970b81d077SJaegeuk Kim 	return bio->bi_private != NULL;
3198cde4de12SJaegeuk Kim }
3199cde4de12SJaegeuk Kim 
3200ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3201ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3202ccd31cb2SSheng Yong { \
3203ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3204cde4de12SJaegeuk Kim }
3205f424f664SJaegeuk Kim 
3206ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3207ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3208ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3209ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3210ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3211ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3212ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3213ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
32141c1d35dfSChao Yu 
3215178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3216178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
32173c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3218178053e2SDamien Le Moal {
3219178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
32203c62be17SJaegeuk Kim 	int i;
3221178053e2SDamien Le Moal 
32223c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
32233c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
32243c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
32253c62be17SJaegeuk Kim 	return -EINVAL;
3226178053e2SDamien Le Moal }
3227178053e2SDamien Le Moal #endif
3228178053e2SDamien Le Moal 
322996ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
323096ba2decSDamien Le Moal {
323196ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
323296ba2decSDamien Le Moal 
3233ccd31cb2SSheng Yong 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
323452763a4bSJaegeuk Kim }
323552763a4bSJaegeuk Kim 
323652763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
323752763a4bSJaegeuk Kim {
323852763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
323952763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
324052763a4bSJaegeuk Kim 
324152763a4bSJaegeuk Kim 	switch (mt) {
324252763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
324352763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
324452763a4bSJaegeuk Kim 		break;
324552763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
324652763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
324752763a4bSJaegeuk Kim 		break;
324852763a4bSJaegeuk Kim 	}
324952763a4bSJaegeuk Kim }
325052763a4bSJaegeuk Kim 
3251fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3252fcc85a4dSJaegeuk Kim {
3253fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3254886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3255fcc85a4dSJaegeuk Kim 
3256fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3257fcc85a4dSJaegeuk Kim #else
3258fcc85a4dSJaegeuk Kim 	return 0;
3259fcc85a4dSJaegeuk Kim #endif
3260fcc85a4dSJaegeuk Kim }
3261fcc85a4dSJaegeuk Kim 
326239a53e0cSJaegeuk Kim #endif
3263