xref: /openbmc/linux/fs/f2fs/f2fs.h (revision d8a9a22992f97cb3e2a7f6e706ecd510d5783e44)
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
127cde4de12SJaegeuk Kim 
12876f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
12976f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
13076f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
131c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
13276f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
133c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
13476f105a2SJaegeuk Kim 
13539a53e0cSJaegeuk Kim /*
1367c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1377c2e5963SJaegeuk Kim  */
1387c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1397c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1407c2e5963SJaegeuk Kim 
1417c2e5963SJaegeuk Kim /*
14239a53e0cSJaegeuk Kim  * For checkpoint manager
14339a53e0cSJaegeuk Kim  */
14439a53e0cSJaegeuk Kim enum {
14539a53e0cSJaegeuk Kim 	NAT_BITMAP,
14639a53e0cSJaegeuk Kim 	SIT_BITMAP
14739a53e0cSJaegeuk Kim };
14839a53e0cSJaegeuk Kim 
149c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
150c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
151c473f1a9SChao Yu #define	CP_SYNC		0x00000004
152c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
153c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1541f43e2adSChao Yu #define CP_TRIMMED	0x00000020
15575ab4cb8SJaegeuk Kim 
15647b89808SJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	2048
157bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
1584ddb1a4dSJaegeuk Kim 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
159a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
160a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
1614ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
162ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
163969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
164969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
16560b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
166dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
167bba681cbSJaegeuk Kim 
16875ab4cb8SJaegeuk Kim struct cp_control {
16975ab4cb8SJaegeuk Kim 	int reason;
1704b2fecc8SJaegeuk Kim 	__u64 trim_start;
1714b2fecc8SJaegeuk Kim 	__u64 trim_end;
1724b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
17375ab4cb8SJaegeuk Kim };
17475ab4cb8SJaegeuk Kim 
175662befdaSChao Yu /*
17681c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
177662befdaSChao Yu  */
178662befdaSChao Yu enum {
179662befdaSChao Yu 	META_CP,
180662befdaSChao Yu 	META_NAT,
18181c1a0f1SChao Yu 	META_SIT,
1824c521f49SJaegeuk Kim 	META_SSA,
1834c521f49SJaegeuk Kim 	META_POR,
184662befdaSChao Yu };
185662befdaSChao Yu 
1866451e041SJaegeuk Kim /* for the list of ino */
1876451e041SJaegeuk Kim enum {
1886451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
189fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
190fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1910a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
19239d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
1936451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1946451e041SJaegeuk Kim };
1956451e041SJaegeuk Kim 
1966451e041SJaegeuk Kim struct ino_entry {
19739a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
19839a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
19939d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
20039a53e0cSJaegeuk Kim };
20139a53e0cSJaegeuk Kim 
2022710fd7eSChao Yu /* for the list of inodes to be GCed */
20306292073SChao Yu struct inode_entry {
20439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
20539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
20639a53e0cSJaegeuk Kim };
20739a53e0cSJaegeuk Kim 
208a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2097fd9e544SJaegeuk Kim struct discard_entry {
2107fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
211a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
212a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2137fd9e544SJaegeuk Kim };
2147fd9e544SJaegeuk Kim 
215969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
216969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
217969d1b18SChao Yu 
218ba48a33eSChao Yu /* max discard pend list number */
219ba48a33eSChao Yu #define MAX_PLIST_NUM		512
220ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
221ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
222ba48a33eSChao Yu 
22315469963SJaegeuk Kim enum {
22415469963SJaegeuk Kim 	D_PREP,
22515469963SJaegeuk Kim 	D_SUBMIT,
22615469963SJaegeuk Kim 	D_DONE,
22715469963SJaegeuk Kim };
22815469963SJaegeuk Kim 
229004b6862SChao Yu struct discard_info {
230b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
231b01a9201SJaegeuk Kim 	block_t len;			/* length */
232004b6862SChao Yu 	block_t start;			/* actual start address in dev */
233004b6862SChao Yu };
234004b6862SChao Yu 
235b01a9201SJaegeuk Kim struct discard_cmd {
236004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
237004b6862SChao Yu 	union {
238004b6862SChao Yu 		struct {
239004b6862SChao Yu 			block_t lstart;	/* logical start address */
240004b6862SChao Yu 			block_t len;	/* length */
241004b6862SChao Yu 			block_t start;	/* actual start address in dev */
242004b6862SChao Yu 		};
243004b6862SChao Yu 		struct discard_info di;	/* discard info */
244004b6862SChao Yu 
245004b6862SChao Yu 	};
246b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
247b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
248c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
249ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2509a744b92SChao Yu 	unsigned char state;		/* state */
251c81abe34SJaegeuk Kim 	int error;			/* bio error */
252275b66b0SChao Yu };
253275b66b0SChao Yu 
25478997b56SChao Yu enum {
25578997b56SChao Yu 	DPOLICY_BG,
25678997b56SChao Yu 	DPOLICY_FORCE,
25778997b56SChao Yu 	DPOLICY_FSTRIM,
25878997b56SChao Yu 	DPOLICY_UMOUNT,
25978997b56SChao Yu 	MAX_DPOLICY,
26078997b56SChao Yu };
26178997b56SChao Yu 
262ecc9aa00SChao Yu struct discard_policy {
26378997b56SChao Yu 	int type;			/* type of discard */
264ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
265ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
266ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
267ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
268ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
269ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
27078997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
271ecc9aa00SChao Yu };
272ecc9aa00SChao Yu 
2730b54fb84SJaegeuk Kim struct discard_cmd_control {
27415469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
27546f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
276ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
27746f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
2788412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
27915469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
280969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
28115469963SJaegeuk Kim 	struct mutex cmd_lock;
282d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
283d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
284969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
285d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
2868b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
2878b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
2885f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
289004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
29039a53e0cSJaegeuk Kim };
29139a53e0cSJaegeuk Kim 
29239a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
29339a53e0cSJaegeuk Kim struct fsync_inode_entry {
29439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
296c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
297c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
29839a53e0cSJaegeuk Kim };
29939a53e0cSJaegeuk Kim 
30068afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
30168afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
30239a53e0cSJaegeuk Kim 
30368afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
30468afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
30568afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
30668afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
30739a53e0cSJaegeuk Kim 
308dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
309dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
310309cc2b6SJaegeuk Kim 
311dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
31239a53e0cSJaegeuk Kim {
313dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
314cac5a3d8SDongOh Shin 
315dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
31639a53e0cSJaegeuk Kim 	return before;
31739a53e0cSJaegeuk Kim }
31839a53e0cSJaegeuk Kim 
319dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
32039a53e0cSJaegeuk Kim {
321dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
322cac5a3d8SDongOh Shin 
323dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
32439a53e0cSJaegeuk Kim 	return before;
32539a53e0cSJaegeuk Kim }
32639a53e0cSJaegeuk Kim 
327dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
328dfc08a12SChao Yu 							int size, int type)
329184a5cd2SChao Yu {
330184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
331dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
332dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
333184a5cd2SChao Yu }
334184a5cd2SChao Yu 
33539a53e0cSJaegeuk Kim /*
336e9750824SNamjae Jeon  * ioctl commands
337e9750824SNamjae Jeon  */
338e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
339e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
340d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
341e9750824SNamjae Jeon 
34288b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
34388b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
34488b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
34502a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3461e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3471e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
348d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
349456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
350d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
351d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3524dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3534dd6f977SJaegeuk Kim 						struct f2fs_move_range)
354e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
355e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
35634dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
35734dc77adSJaegeuk Kim 						struct f2fs_gc_range)
358e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3591ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3601ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
361c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
36288b88a66SJaegeuk Kim 
3630b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3640b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3650b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
366f424f664SJaegeuk Kim 
3671abff93dSJaegeuk Kim /*
3681abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3691abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3701abff93dSJaegeuk Kim  */
3711abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3721abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3731abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3741abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
375c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3761abff93dSJaegeuk Kim 
377e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
378e9750824SNamjae Jeon /*
379e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
380e9750824SNamjae Jeon  */
381e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
382e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
38304ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
384e9750824SNamjae Jeon #endif
385e9750824SNamjae Jeon 
3862c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
3872c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
3882c1d0305SChao Yu 
38934dc77adSJaegeuk Kim struct f2fs_gc_range {
39034dc77adSJaegeuk Kim 	u32 sync;
39134dc77adSJaegeuk Kim 	u64 start;
39234dc77adSJaegeuk Kim 	u64 len;
39334dc77adSJaegeuk Kim };
39434dc77adSJaegeuk Kim 
395d323d005SChao Yu struct f2fs_defragment {
396d323d005SChao Yu 	u64 start;
397d323d005SChao Yu 	u64 len;
398d323d005SChao Yu };
399d323d005SChao Yu 
4004dd6f977SJaegeuk Kim struct f2fs_move_range {
4014dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4024dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4034dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4044dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4054dd6f977SJaegeuk Kim };
4064dd6f977SJaegeuk Kim 
407e066b83cSJaegeuk Kim struct f2fs_flush_device {
408e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
409e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
410e066b83cSJaegeuk Kim };
411e066b83cSJaegeuk Kim 
412f2470371SChao Yu /* for inline stuff */
413f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4146afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4157a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4166afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4176afc662eSChao Yu #define F2FS_INLINE_XATTR_ADDRS(inode)	get_inline_xattr_addrs(inode)
4187a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4197a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
4206afc662eSChao Yu 				F2FS_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
57939a53e0cSJaegeuk Kim 
580b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
581b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
582b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
583b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
584b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
585b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
586cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
587cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
588cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
589e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
590e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
59126787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
59226787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
593cde4de12SJaegeuk Kim 
594ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
595ab9fa662SJaegeuk Kim 
59639a53e0cSJaegeuk Kim struct f2fs_inode_info {
59739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
59839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
59939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
60038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6011ad71a27SJaegeuk Kim 	union {
6021ad71a27SJaegeuk Kim 		unsigned int i_current_depth;	/* only for directory depth */
6031ad71a27SJaegeuk Kim 		unsigned short i_gc_failures;	/* only for regular file */
6041ad71a27SJaegeuk Kim 	};
6056666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
60639a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
60739a53e0cSJaegeuk Kim 
60839a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
60939a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
610d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
611204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
61239a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
61339a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
61488c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
615b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
61639a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
61726de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
61888b88a66SJaegeuk Kim 
6190abd675eSChao Yu #ifdef CONFIG_QUOTA
6200abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6210abd675eSChao Yu 
6220abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6230abd675eSChao Yu 	qsize_t i_reserved_quota;
6240abd675eSChao Yu #endif
6250f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6260f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
62757864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
62888b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6297a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
63088b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6313e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
63282e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
6335a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
63427161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
635f2470371SChao Yu 
6367a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6375c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6386afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
63939a53e0cSJaegeuk Kim };
64039a53e0cSJaegeuk Kim 
64139a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
642bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
64339a53e0cSJaegeuk Kim {
644bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
645bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
646bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
64739a53e0cSJaegeuk Kim }
64839a53e0cSJaegeuk Kim 
64939a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
65039a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
65139a53e0cSJaegeuk Kim {
65239a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6534d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
65439a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
65539a53e0cSJaegeuk Kim }
65639a53e0cSJaegeuk Kim 
657429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
658429511cdSChao Yu 						u32 blk, unsigned int len)
659429511cdSChao Yu {
660429511cdSChao Yu 	ei->fofs = fofs;
661429511cdSChao Yu 	ei->blk = blk;
662429511cdSChao Yu 	ei->len = len;
663429511cdSChao Yu }
664429511cdSChao Yu 
665004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
666004b6862SChao Yu 						struct discard_info *front)
667004b6862SChao Yu {
668004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
669004b6862SChao Yu }
670004b6862SChao Yu 
671004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
672004b6862SChao Yu 						struct discard_info *back)
673004b6862SChao Yu {
674004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
675004b6862SChao Yu }
676004b6862SChao Yu 
677004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
678004b6862SChao Yu 						struct discard_info *front)
679004b6862SChao Yu {
680004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
681004b6862SChao Yu }
682004b6862SChao Yu 
683429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
684429511cdSChao Yu 						struct extent_info *front)
685429511cdSChao Yu {
686429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
687429511cdSChao Yu 			back->blk + back->len == front->blk);
688429511cdSChao Yu }
689429511cdSChao Yu 
690429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
691429511cdSChao Yu 						struct extent_info *back)
692429511cdSChao Yu {
693429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
694429511cdSChao Yu }
695429511cdSChao Yu 
696429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
697429511cdSChao Yu 						struct extent_info *front)
698429511cdSChao Yu {
699429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
700429511cdSChao Yu }
701429511cdSChao Yu 
702cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
703205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
704205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
7054abd3f5aSChao Yu {
706205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7074abd3f5aSChao Yu 		et->largest = en->ei;
7087c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
709205b9822SJaegeuk Kim 	}
7104abd3f5aSChao Yu }
7114abd3f5aSChao Yu 
7129a4ffdf5SChao Yu /*
7139a4ffdf5SChao Yu  * For free nid management
7149a4ffdf5SChao Yu  */
7159a4ffdf5SChao Yu enum nid_state {
7169a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7179a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7189a4ffdf5SChao Yu 	MAX_NID_STATE,
719b8559dc2SChao Yu };
720b8559dc2SChao Yu 
72139a53e0cSJaegeuk Kim struct f2fs_nm_info {
72239a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
72339a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
72404d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
72539a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
726cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
727ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7282304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
72939a53e0cSJaegeuk Kim 
73039a53e0cSJaegeuk Kim 	/* NAT cache management */
73139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
732309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
733b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
73439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
735309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
736aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
73722ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
73839a53e0cSJaegeuk Kim 
73939a53e0cSJaegeuk Kim 	/* free node ids management */
7408a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7419a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7429a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
743b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
74439a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
7454ac91242SChao Yu 	unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
7464ac91242SChao Yu 	unsigned char *nat_block_bitmap;
747586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
74839a53e0cSJaegeuk Kim 
74939a53e0cSJaegeuk Kim 	/* for checkpoint */
75039a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
75122ad0b6aSJaegeuk Kim 
75222ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
75322ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
75422ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
75522ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
756599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
757599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
758599a09b2SChao Yu #endif
75939a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
76039a53e0cSJaegeuk Kim };
76139a53e0cSJaegeuk Kim 
76239a53e0cSJaegeuk Kim /*
76339a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
76439a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
76539a53e0cSJaegeuk Kim  * by the data offset in a file.
76639a53e0cSJaegeuk Kim  */
76739a53e0cSJaegeuk Kim struct dnode_of_data {
76839a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
76939a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
77039a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
77139a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
77239a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
77339a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
77493bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
7753cf45747SChao Yu 	char cur_level;			/* level of hole node page */
7763cf45747SChao Yu 	char max_level;			/* level of current page located */
77739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
77839a53e0cSJaegeuk Kim };
77939a53e0cSJaegeuk Kim 
78039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
78139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
78239a53e0cSJaegeuk Kim {
783d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
78439a53e0cSJaegeuk Kim 	dn->inode = inode;
78539a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
78639a53e0cSJaegeuk Kim 	dn->node_page = npage;
78739a53e0cSJaegeuk Kim 	dn->nid = nid;
78839a53e0cSJaegeuk Kim }
78939a53e0cSJaegeuk Kim 
79039a53e0cSJaegeuk Kim /*
79139a53e0cSJaegeuk Kim  * For SIT manager
79239a53e0cSJaegeuk Kim  *
79339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
79439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
79539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
79639a53e0cSJaegeuk Kim  * respectively.
79739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
79839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
79939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
80039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
80139a53e0cSJaegeuk Kim  * data and 8 for node logs.
80239a53e0cSJaegeuk Kim  */
80339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
80439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
80539a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
80639a53e0cSJaegeuk Kim 
80739a53e0cSJaegeuk Kim enum {
80839a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
80939a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
81039a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
81139a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
81239a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
81339a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
81438aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
81539a53e0cSJaegeuk Kim };
81639a53e0cSJaegeuk Kim 
8176b4afdd7SJaegeuk Kim struct flush_cmd {
8186b4afdd7SJaegeuk Kim 	struct completion wait;
819721bd4d5SGu Zheng 	struct llist_node llnode;
82039d787beSChao Yu 	nid_t ino;
8216b4afdd7SJaegeuk Kim 	int ret;
8226b4afdd7SJaegeuk Kim };
8236b4afdd7SJaegeuk Kim 
824a688b9d9SGu Zheng struct flush_cmd_control {
825a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
826a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8278b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8288b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
829721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
830721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
831a688b9d9SGu Zheng };
832a688b9d9SGu Zheng 
83339a53e0cSJaegeuk Kim struct f2fs_sm_info {
83439a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
83539a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
83639a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
83739a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
83839a53e0cSJaegeuk Kim 
8392b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8402b60311dSChao Yu 
84139a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
84239a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
84339a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
84439a53e0cSJaegeuk Kim 
84539a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
84639a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
84739a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
84839a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
84981eb8d6eSJaegeuk Kim 
85081eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
85181eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8527fd9e544SJaegeuk Kim 
853bba681cbSJaegeuk Kim 	/* for batched trimming */
854bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
855bba681cbSJaegeuk Kim 
856184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
857184a5cd2SChao Yu 
858216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
859216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
860c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
861ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
862a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
8636b4afdd7SJaegeuk Kim 
8646b4afdd7SJaegeuk Kim 	/* for flush command control */
865b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
866a688b9d9SGu Zheng 
8670b54fb84SJaegeuk Kim 	/* for discard command control */
8680b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
86939a53e0cSJaegeuk Kim };
87039a53e0cSJaegeuk Kim 
87139a53e0cSJaegeuk Kim /*
87239a53e0cSJaegeuk Kim  * For superblock
87339a53e0cSJaegeuk Kim  */
87439a53e0cSJaegeuk Kim /*
87539a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
87639a53e0cSJaegeuk Kim  *
87739a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
87839a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
87939a53e0cSJaegeuk Kim  */
88036951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
88139a53e0cSJaegeuk Kim enum count_type {
88239a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
883c227f912SChao Yu 	F2FS_DIRTY_DATA,
8842c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
88539a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
88639a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
8878dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
8880f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
88936951b38SChao Yu 	F2FS_WB_CP_DATA,
89036951b38SChao Yu 	F2FS_WB_DATA,
89139a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
89239a53e0cSJaegeuk Kim };
89339a53e0cSJaegeuk Kim 
89439a53e0cSJaegeuk Kim /*
895e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
89639a53e0cSJaegeuk Kim  * The available types are:
89739a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
89839a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
89939a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
90039a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
90139a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
90239a53e0cSJaegeuk Kim  *			with waiting the bio's completion
90339a53e0cSJaegeuk Kim  * ...			Only can be used with META.
90439a53e0cSJaegeuk Kim  */
9057d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
90639a53e0cSJaegeuk Kim enum page_type {
90739a53e0cSJaegeuk Kim 	DATA,
90839a53e0cSJaegeuk Kim 	NODE,
90939a53e0cSJaegeuk Kim 	META,
91039a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
91139a53e0cSJaegeuk Kim 	META_FLUSH,
9128ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9138ce67cb0SJaegeuk Kim 	INMEM_DROP,
9148c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
91528bc106bSChao Yu 	INMEM_REVOKE,
9168ce67cb0SJaegeuk Kim 	IPU,
9178ce67cb0SJaegeuk Kim 	OPU,
91839a53e0cSJaegeuk Kim };
91939a53e0cSJaegeuk Kim 
920a912b54dSJaegeuk Kim enum temp_type {
921a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
922a912b54dSJaegeuk Kim 	WARM,
923a912b54dSJaegeuk Kim 	COLD,
924a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
925a912b54dSJaegeuk Kim };
926a912b54dSJaegeuk Kim 
927cc15620bSJaegeuk Kim enum need_lock_type {
928cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
929cc15620bSJaegeuk Kim 	LOCK_DONE,
930cc15620bSJaegeuk Kim 	LOCK_RETRY,
931cc15620bSJaegeuk Kim };
932cc15620bSJaegeuk Kim 
933a5fd5050SChao Yu enum cp_reason_type {
934a5fd5050SChao Yu 	CP_NO_NEEDED,
935a5fd5050SChao Yu 	CP_NON_REGULAR,
936a5fd5050SChao Yu 	CP_HARDLINK,
937a5fd5050SChao Yu 	CP_SB_NEED_CP,
938a5fd5050SChao Yu 	CP_WRONG_PINO,
939a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
940a5fd5050SChao Yu 	CP_NODE_NEED_CP,
941a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
942a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
9430a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
944a5fd5050SChao Yu };
945a5fd5050SChao Yu 
946b0af6d49SChao Yu enum iostat_type {
947b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
948b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
949b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
950b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
951b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
952b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
953b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
954b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
955b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
956b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
957b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
958b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
959b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
960b0af6d49SChao Yu 	NR_IO_TYPE,
961b0af6d49SChao Yu };
962b0af6d49SChao Yu 
963458e6197SJaegeuk Kim struct f2fs_io_info {
96405ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
96539d787beSChao Yu 	nid_t ino;		/* inode number */
966458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
967a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
96804d328deSMike Christie 	int op;			/* contains REQ_OP_ */
969ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
9707a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
97128bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
97205ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
9734375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
974fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
975d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
976cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
977fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
978b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
979578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
980458e6197SJaegeuk Kim };
981458e6197SJaegeuk Kim 
98268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
9831ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
984458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
9851ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
9861ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
987458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
988df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
989fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
990fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
9911ff7bd3bSJaegeuk Kim };
9921ff7bd3bSJaegeuk Kim 
9933c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
9943c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
9953c62be17SJaegeuk Kim struct f2fs_dev_info {
9963c62be17SJaegeuk Kim 	struct block_device *bdev;
9973c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
9983c62be17SJaegeuk Kim 	unsigned int total_segments;
9993c62be17SJaegeuk Kim 	block_t start_blk;
10003c62be17SJaegeuk Kim 	block_t end_blk;
10013c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10023c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10033c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10043c62be17SJaegeuk Kim #endif
10053c62be17SJaegeuk Kim };
10063c62be17SJaegeuk Kim 
1007c227f912SChao Yu enum inode_type {
1008c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1009c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10100f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
101157864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1012c227f912SChao Yu 	NR_INODE_TYPE,
1013c227f912SChao Yu };
1014c227f912SChao Yu 
101567298804SChao Yu /* for inner inode cache management */
101667298804SChao Yu struct inode_management {
101767298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
101867298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
101967298804SChao Yu 	struct list_head ino_list;		/* inode list head */
102067298804SChao Yu 	unsigned long ino_num;			/* number of entries */
102167298804SChao Yu };
102267298804SChao Yu 
1023caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1024caf0047eSChao Yu enum {
1025caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1026caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1027caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1028caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1029df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1030bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
1031caf0047eSChao Yu };
1032caf0047eSChao Yu 
10336beceb54SJaegeuk Kim enum {
10346beceb54SJaegeuk Kim 	CP_TIME,
1035d0239e1bSJaegeuk Kim 	REQ_TIME,
10366beceb54SJaegeuk Kim 	MAX_TIME,
10376beceb54SJaegeuk Kim };
10386beceb54SJaegeuk Kim 
103939a53e0cSJaegeuk Kim struct f2fs_sb_info {
104039a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
10415e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
104239a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1043e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1044fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
104539a53e0cSJaegeuk Kim 
1046178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1047178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1048178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1049178053e2SDamien Le Moal #endif
1050178053e2SDamien Le Moal 
105139a53e0cSJaegeuk Kim 	/* for node-related operations */
105239a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
105339a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
105439a53e0cSJaegeuk Kim 
105539a53e0cSJaegeuk Kim 	/* for segment-related operations */
105639a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
10571ff7bd3bSJaegeuk Kim 
10581ff7bd3bSJaegeuk Kim 	/* for bio operations */
1059a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1060e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1061e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
10620a595ebaSJaegeuk Kim 	int write_io_size_bits;			/* Write IO size bits */
10630a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
106439a53e0cSJaegeuk Kim 
106539a53e0cSJaegeuk Kim 	/* for checkpoint */
106639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
10678508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1068aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
106939a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
107039936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1071b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1072b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
107359c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1074fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
10756beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
10766beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
107739a53e0cSJaegeuk Kim 
107867298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
10796451e041SJaegeuk Kim 
10806451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
10810d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
108239a53e0cSJaegeuk Kim 
1083c227f912SChao Yu 	/* for inode management */
1084c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1085c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
108639a53e0cSJaegeuk Kim 
108713054c54SChao Yu 	/* for extent tree cache */
108813054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
10895e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
109013054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
109113054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
10927441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1093137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
109474fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
109513054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
109613054c54SChao Yu 
109739a53e0cSJaegeuk Kim 	/* basic filesystem units */
109839a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
109939a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
110039a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
110139a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
110239a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
110339a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
110439a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
110539a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
110639a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
110739a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
110839a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
110939a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
111039a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1111e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
111239a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
1113ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
11146afc662eSChao Yu 	int inline_xattr_size;			/* inline xattr size */
1115a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1116f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
111739a53e0cSJaegeuk Kim 
111839a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
111939a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1120a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
112139a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1122daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
112380d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
11247e65be49SJaegeuk Kim 	block_t root_reserved_blocks;		/* root reserved blocks */
11257c2e5963SJaegeuk Kim 	kuid_t s_resuid;			/* reserved blocks for uid */
11267c2e5963SJaegeuk Kim 	kgid_t s_resgid;			/* reserved blocks for gid */
1127daeb433eSChao Yu 
1128292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1129292c196aSChao Yu 
113039a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1131523be8a6SJaegeuk Kim 
1132523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
113335782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
113441382ec4SJaegeuk Kim 	/* # of allocated blocks */
113541382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
113639a53e0cSJaegeuk Kim 
1137687de7f1SJaegeuk Kim 	/* writeback control */
1138687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1139687de7f1SJaegeuk Kim 
1140513c5f37SJaegeuk Kim 	/* valid inode count */
1141513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1142513c5f37SJaegeuk Kim 
114339a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
114439a53e0cSJaegeuk Kim 
114539a53e0cSJaegeuk Kim 	/* for cleaning operations */
114639a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
114739a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
11485ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
114939a53e0cSJaegeuk Kim 
1150e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
1151e93b9865SHou Pengyang 	u64 fggc_threshold;
1152e93b9865SHou Pengyang 
11531ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
11541ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
11551ad71a27SJaegeuk Kim 
1156b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1157b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1158b1c57c1cSJaegeuk Kim 
115939a53e0cSJaegeuk Kim 	/*
116039a53e0cSJaegeuk Kim 	 * for stat information.
116139a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
116239a53e0cSJaegeuk Kim 	 */
116335b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
116439a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
116539a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
116639a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1167b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
11685b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
11695b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
11705b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
11715b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1172d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
117303e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
117403e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
117526a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1176648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
117726a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1178648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
117939a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
118033fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
118135b09d82SNamjae Jeon #endif
118239a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1183b59d0baeSNamjae Jeon 
1184b0af6d49SChao Yu 	/* For app/fs IO statistics */
1185b0af6d49SChao Yu 	spinlock_t iostat_lock;
1186b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1187b0af6d49SChao Yu 	bool iostat_enable;
1188b0af6d49SChao Yu 
1189b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1190b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1191b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
11922658e50dSJaegeuk Kim 
11932658e50dSJaegeuk Kim 	/* For shrinker support */
11942658e50dSJaegeuk Kim 	struct list_head s_list;
11953c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
11963c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
11971228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
11981228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
11992658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12002658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12018f1dbbbbSShuoran Liu 
12028f1dbbbbSShuoran Liu 	/* For write statistics */
12038f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12048f1dbbbbSShuoran Liu 	u64 kbytes_written;
120543b6573bSKeith Mok 
120643b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
120743b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12081ecc0c5cSChao Yu 
1209704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1210704956ecSChao Yu 	__u32 s_chksum_seed;
1211704956ecSChao Yu 
12121ecc0c5cSChao Yu 	/* For fault injection */
12131ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12141ecc0c5cSChao Yu 	struct f2fs_fault_info fault_info;
12151ecc0c5cSChao Yu #endif
12164b2414d0SChao Yu 
12174b2414d0SChao Yu #ifdef CONFIG_QUOTA
12184b2414d0SChao Yu 	/* Names of quota files with journalled quota */
12194b2414d0SChao Yu 	char *s_qf_names[MAXQUOTAS];
12204b2414d0SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
12214b2414d0SChao Yu #endif
122239a53e0cSJaegeuk Kim };
122339a53e0cSJaegeuk Kim 
12241ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
122555523519SChao Yu #define f2fs_show_injection_info(type)				\
122655523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
122755523519SChao Yu 		KERN_INFO, fault_name[type],			\
122855523519SChao Yu 		__func__, __builtin_return_address(0))
12291ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12301ecc0c5cSChao Yu {
12311ecc0c5cSChao Yu 	struct f2fs_fault_info *ffi = &sbi->fault_info;
12321ecc0c5cSChao Yu 
12331ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12341ecc0c5cSChao Yu 		return false;
12351ecc0c5cSChao Yu 
12361ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12371ecc0c5cSChao Yu 		return false;
12381ecc0c5cSChao Yu 
12391ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12401ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12411ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12421ecc0c5cSChao Yu 		return true;
12431ecc0c5cSChao Yu 	}
12441ecc0c5cSChao Yu 	return false;
12451ecc0c5cSChao Yu }
12461ecc0c5cSChao Yu #endif
12471ecc0c5cSChao Yu 
12488f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
12498f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
12508f1dbbbbSShuoran Liu  */
12518f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
125268afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
125368afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
12548f1dbbbbSShuoran Liu 
12556beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
12566beceb54SJaegeuk Kim {
12576beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
12586beceb54SJaegeuk Kim }
12596beceb54SJaegeuk Kim 
12606beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
12616beceb54SJaegeuk Kim {
12626bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
12636beceb54SJaegeuk Kim 
12646beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
12656beceb54SJaegeuk Kim }
12666beceb54SJaegeuk Kim 
1267d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1268d0239e1bSJaegeuk Kim {
1269d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1270d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1271d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1272d0239e1bSJaegeuk Kim 
1273d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1274d0239e1bSJaegeuk Kim 		return 0;
1275d0239e1bSJaegeuk Kim 
1276d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1277d0239e1bSJaegeuk Kim }
1278d0239e1bSJaegeuk Kim 
127939a53e0cSJaegeuk Kim /*
128039a53e0cSJaegeuk Kim  * Inline functions
128139a53e0cSJaegeuk Kim  */
1282416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1283704956ecSChao Yu 			      const void *address, unsigned int length)
1284704956ecSChao Yu {
1285704956ecSChao Yu 	struct {
1286704956ecSChao Yu 		struct shash_desc shash;
1287704956ecSChao Yu 		char ctx[4];
1288704956ecSChao Yu 	} desc;
1289704956ecSChao Yu 	int err;
1290704956ecSChao Yu 
1291704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1292704956ecSChao Yu 
1293704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1294704956ecSChao Yu 	desc.shash.flags = 0;
1295704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1296704956ecSChao Yu 
1297704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1298704956ecSChao Yu 	BUG_ON(err);
1299704956ecSChao Yu 
1300704956ecSChao Yu 	return *(u32 *)desc.ctx;
1301704956ecSChao Yu }
1302704956ecSChao Yu 
1303416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1304416d2dbbSChao Yu 			   unsigned int length)
1305416d2dbbSChao Yu {
1306416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1307416d2dbbSChao Yu }
1308416d2dbbSChao Yu 
1309416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1310416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1311416d2dbbSChao Yu {
1312416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1313416d2dbbSChao Yu }
1314416d2dbbSChao Yu 
1315416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1316416d2dbbSChao Yu 			      const void *address, unsigned int length)
1317416d2dbbSChao Yu {
1318416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1319416d2dbbSChao Yu }
1320416d2dbbSChao Yu 
132139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
132239a53e0cSJaegeuk Kim {
132339a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
132439a53e0cSJaegeuk Kim }
132539a53e0cSJaegeuk Kim 
132639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
132739a53e0cSJaegeuk Kim {
132839a53e0cSJaegeuk Kim 	return sb->s_fs_info;
132939a53e0cSJaegeuk Kim }
133039a53e0cSJaegeuk Kim 
13314081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13324081363fSJaegeuk Kim {
13334081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13344081363fSJaegeuk Kim }
13354081363fSJaegeuk Kim 
13364081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13374081363fSJaegeuk Kim {
13384081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13394081363fSJaegeuk Kim }
13404081363fSJaegeuk Kim 
13414081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13424081363fSJaegeuk Kim {
13434081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
13444081363fSJaegeuk Kim }
13454081363fSJaegeuk Kim 
134639a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
134739a53e0cSJaegeuk Kim {
134839a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
134939a53e0cSJaegeuk Kim }
135039a53e0cSJaegeuk Kim 
135139a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
135239a53e0cSJaegeuk Kim {
135339a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
135439a53e0cSJaegeuk Kim }
135539a53e0cSJaegeuk Kim 
135645590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
135745590710SGu Zheng {
135845590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
135945590710SGu Zheng }
136045590710SGu Zheng 
136158bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
136258bfaf44SJaegeuk Kim {
136358bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
136458bfaf44SJaegeuk Kim }
136558bfaf44SJaegeuk Kim 
136639a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
136739a53e0cSJaegeuk Kim {
136839a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
136939a53e0cSJaegeuk Kim }
137039a53e0cSJaegeuk Kim 
137139a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
137239a53e0cSJaegeuk Kim {
137339a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
137439a53e0cSJaegeuk Kim }
137539a53e0cSJaegeuk Kim 
137639a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
137739a53e0cSJaegeuk Kim {
137839a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
137939a53e0cSJaegeuk Kim }
138039a53e0cSJaegeuk Kim 
138139a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
138239a53e0cSJaegeuk Kim {
138339a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
138439a53e0cSJaegeuk Kim }
138539a53e0cSJaegeuk Kim 
138639a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
138739a53e0cSJaegeuk Kim {
138839a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
138939a53e0cSJaegeuk Kim }
139039a53e0cSJaegeuk Kim 
13919df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
13929df27d98SGu Zheng {
13939df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
13949df27d98SGu Zheng }
13959df27d98SGu Zheng 
13964ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
13974ef51a8fSJaegeuk Kim {
13984ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
13994ef51a8fSJaegeuk Kim }
14004ef51a8fSJaegeuk Kim 
1401caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
140239a53e0cSJaegeuk Kim {
1403fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
140439a53e0cSJaegeuk Kim }
140539a53e0cSJaegeuk Kim 
1406caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
140739a53e0cSJaegeuk Kim {
1408fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1409caf0047eSChao Yu }
1410caf0047eSChao Yu 
1411caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1412caf0047eSChao Yu {
1413fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
141439a53e0cSJaegeuk Kim }
141539a53e0cSJaegeuk Kim 
1416d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1417d71b5564SJaegeuk Kim {
1418d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1419d71b5564SJaegeuk Kim }
1420d71b5564SJaegeuk Kim 
1421ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1422ea676733SJaegeuk Kim {
1423ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1424ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1425ea676733SJaegeuk Kim 	return 0;
1426ea676733SJaegeuk Kim }
1427ea676733SJaegeuk Kim 
1428ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1429ced2c7eaSKinglong Mee {
1430ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1431ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1432ced2c7eaSKinglong Mee }
1433ced2c7eaSKinglong Mee 
1434aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
143525ca923bSJaegeuk Kim {
143625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1437aaec2b1dSChao Yu 
143825ca923bSJaegeuk Kim 	return ckpt_flags & f;
143925ca923bSJaegeuk Kim }
144025ca923bSJaegeuk Kim 
1441aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
144225ca923bSJaegeuk Kim {
1443aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1444aaec2b1dSChao Yu }
1445aaec2b1dSChao Yu 
1446aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1447aaec2b1dSChao Yu {
1448aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1449aaec2b1dSChao Yu 
1450aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
145125ca923bSJaegeuk Kim 	ckpt_flags |= f;
145225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
145325ca923bSJaegeuk Kim }
145425ca923bSJaegeuk Kim 
1455aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
145625ca923bSJaegeuk Kim {
1457d1aa2453SChao Yu 	unsigned long flags;
1458d1aa2453SChao Yu 
1459d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1460aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1461d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1462aaec2b1dSChao Yu }
1463aaec2b1dSChao Yu 
1464aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1465aaec2b1dSChao Yu {
1466aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1467aaec2b1dSChao Yu 
1468aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
146925ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
147025ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
147125ca923bSJaegeuk Kim }
147225ca923bSJaegeuk Kim 
1473aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1474aaec2b1dSChao Yu {
1475d1aa2453SChao Yu 	unsigned long flags;
1476d1aa2453SChao Yu 
1477d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1478aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1479d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1480aaec2b1dSChao Yu }
1481aaec2b1dSChao Yu 
148222ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
148322ad0b6aSJaegeuk Kim {
1484d1aa2453SChao Yu 	unsigned long flags;
1485d1aa2453SChao Yu 
148622ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
148722ad0b6aSJaegeuk Kim 
148822ad0b6aSJaegeuk Kim 	if (lock)
1489d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
149022ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
149122ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
149222ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
149322ad0b6aSJaegeuk Kim 	if (lock)
1494d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
149522ad0b6aSJaegeuk Kim }
149622ad0b6aSJaegeuk Kim 
149722ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
149822ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
149922ad0b6aSJaegeuk Kim {
150022ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
150122ad0b6aSJaegeuk Kim 
1502c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
150322ad0b6aSJaegeuk Kim }
150422ad0b6aSJaegeuk Kim 
1505e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
150639a53e0cSJaegeuk Kim {
1507b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
150839a53e0cSJaegeuk Kim }
150939a53e0cSJaegeuk Kim 
1510cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1511cc15620bSJaegeuk Kim {
1512cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1513cc15620bSJaegeuk Kim }
1514cc15620bSJaegeuk Kim 
1515e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
151639a53e0cSJaegeuk Kim {
1517b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
151839936837SJaegeuk Kim }
151939936837SJaegeuk Kim 
1520e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
152139936837SJaegeuk Kim {
1522b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
152339936837SJaegeuk Kim }
152439936837SJaegeuk Kim 
1525e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
152639936837SJaegeuk Kim {
1527b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
152839a53e0cSJaegeuk Kim }
152939a53e0cSJaegeuk Kim 
1530119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1531119ee914SJaegeuk Kim {
1532119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1533119ee914SJaegeuk Kim 
1534119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1535119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1536119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1537119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1538119ee914SJaegeuk Kim 	return reason;
1539119ee914SJaegeuk Kim }
1540119ee914SJaegeuk Kim 
1541119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1542119ee914SJaegeuk Kim {
1543c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1544119ee914SJaegeuk Kim }
1545119ee914SJaegeuk Kim 
1546119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1547119ee914SJaegeuk Kim {
1548aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1549aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1550119ee914SJaegeuk Kim }
1551119ee914SJaegeuk Kim 
155239a53e0cSJaegeuk Kim /*
155339a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
155439a53e0cSJaegeuk Kim  */
1555064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
155639a53e0cSJaegeuk Kim {
1557d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1558d6b7d4b3SChao Yu 		return -EINVAL;
1559cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1560064e0823SNamjae Jeon 		return -EINVAL;
1561064e0823SNamjae Jeon 	return 0;
156239a53e0cSJaegeuk Kim }
156339a53e0cSJaegeuk Kim 
156439a53e0cSJaegeuk Kim /*
156539a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
156639a53e0cSJaegeuk Kim  */
156739a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
156839a53e0cSJaegeuk Kim {
15690eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
15700eb0adadSChao Yu 
1571000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
157239a53e0cSJaegeuk Kim }
157339a53e0cSJaegeuk Kim 
15744bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
15754bc8e9bcSChao Yu {
15764bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
15774bc8e9bcSChao Yu }
15784bc8e9bcSChao Yu 
1579*d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1580*d8a9a229SJaegeuk Kim 					struct inode *inode)
15817c2e5963SJaegeuk Kim {
1582*d8a9a229SJaegeuk Kim 	if (!inode)
1583*d8a9a229SJaegeuk Kim 		return true;
15847c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
15857c2e5963SJaegeuk Kim 		return false;
1586*d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1587*d8a9a229SJaegeuk Kim 		return true;
15887c2e5963SJaegeuk Kim 	if (capable(CAP_SYS_RESOURCE))
15897c2e5963SJaegeuk Kim 		return true;
15907c2e5963SJaegeuk Kim 	if (uid_eq(sbi->s_resuid, current_fsuid()))
15917c2e5963SJaegeuk Kim 		return true;
15927c2e5963SJaegeuk Kim 	if (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) &&
15937c2e5963SJaegeuk Kim 					in_group_p(sbi->s_resgid))
15947c2e5963SJaegeuk Kim 		return true;
15957c2e5963SJaegeuk Kim 	return false;
15967c2e5963SJaegeuk Kim }
15977c2e5963SJaegeuk Kim 
15980abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
15990abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
160046008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
160139a53e0cSJaegeuk Kim {
16020abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1603daeb433eSChao Yu 	block_t avail_user_block_count;
16040abd675eSChao Yu 	int ret;
16050abd675eSChao Yu 
16060abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16070abd675eSChao Yu 	if (ret)
16080abd675eSChao Yu 		return ret;
1609dd11a5dfSJaegeuk Kim 
1610cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
161155523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
161255523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16130abd675eSChao Yu 		release = *count;
16140abd675eSChao Yu 		goto enospc;
161555523519SChao Yu 	}
1616cb78942bSJaegeuk Kim #endif
1617dd11a5dfSJaegeuk Kim 	/*
1618dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1619dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1620dd11a5dfSJaegeuk Kim 	 */
1621dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1622cfb271d4SChao Yu 
162339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16242555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
162580d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
162680d42145SYunlong Song 					sbi->current_reserved_blocks;
16277e65be49SJaegeuk Kim 
1628*d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
16297e65be49SJaegeuk Kim 		avail_user_block_count -= sbi->root_reserved_blocks;
16307e65be49SJaegeuk Kim 
1631daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1632daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16337e65be49SJaegeuk Kim 		if (diff > *count)
16347e65be49SJaegeuk Kim 			diff = *count;
1635dd11a5dfSJaegeuk Kim 		*count -= diff;
16360abd675eSChao Yu 		release = diff;
16377e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
163846008c6dSChao Yu 		if (!*count) {
163939a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1640dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16410abd675eSChao Yu 			goto enospc;
164239a53e0cSJaegeuk Kim 		}
164346008c6dSChao Yu 	}
164439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
164541382ec4SJaegeuk Kim 
1646fab2adeeSLiFan 	if (unlikely(release))
16470abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16480abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16490abd675eSChao Yu 	return 0;
16500abd675eSChao Yu 
16510abd675eSChao Yu enospc:
16520abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16530abd675eSChao Yu 	return -ENOSPC;
165439a53e0cSJaegeuk Kim }
165539a53e0cSJaegeuk Kim 
1656da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
165739a53e0cSJaegeuk Kim 						struct inode *inode,
16580eb0adadSChao Yu 						block_t count)
165939a53e0cSJaegeuk Kim {
16600eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
16610eb0adadSChao Yu 
166239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16639850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
16640eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
166539a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
166680d42145SYunlong Song 	if (sbi->reserved_blocks &&
166780d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
166880d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
166980d42145SYunlong Song 					sbi->current_reserved_blocks + count);
167039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
16710abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
167239a53e0cSJaegeuk Kim }
167339a53e0cSJaegeuk Kim 
167439a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
167539a53e0cSJaegeuk Kim {
167635782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
16777c4abcbeSChao Yu 
167836951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
167936951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
16807c4abcbeSChao Yu 		return;
16817c4abcbeSChao Yu 
1682caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
168339a53e0cSJaegeuk Kim }
168439a53e0cSJaegeuk Kim 
1685a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
168639a53e0cSJaegeuk Kim {
1687204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1688c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1689c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
16902c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
16912c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
169239a53e0cSJaegeuk Kim }
169339a53e0cSJaegeuk Kim 
169439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
169539a53e0cSJaegeuk Kim {
169635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
169739a53e0cSJaegeuk Kim }
169839a53e0cSJaegeuk Kim 
1699a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
170039a53e0cSJaegeuk Kim {
17015ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17025ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17031fe54f9dSJaegeuk Kim 		return;
17041fe54f9dSJaegeuk Kim 
1705204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1706c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1707c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17082c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17092c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
171039a53e0cSJaegeuk Kim }
171139a53e0cSJaegeuk Kim 
1712523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
171339a53e0cSJaegeuk Kim {
171435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
171539a53e0cSJaegeuk Kim }
171639a53e0cSJaegeuk Kim 
1717204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1718f8b2c1f9SJaegeuk Kim {
1719204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1720f8b2c1f9SJaegeuk Kim }
1721f8b2c1f9SJaegeuk Kim 
17225ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17235ac206cfSNamjae Jeon {
17243519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1725523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1726523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1727523be8a6SJaegeuk Kim 
1728523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17295ac206cfSNamjae Jeon }
17305ac206cfSNamjae Jeon 
173139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
173239a53e0cSJaegeuk Kim {
17338b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
173439a53e0cSJaegeuk Kim }
173539a53e0cSJaegeuk Kim 
1736f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1737f83a2584SYunlei He {
1738f83a2584SYunlei He 	return sbi->discard_blks;
1739f83a2584SYunlei He }
1740f83a2584SYunlei He 
174139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
174239a53e0cSJaegeuk Kim {
174339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
174439a53e0cSJaegeuk Kim 
174539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
174639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
174739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
174839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
174939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
175039a53e0cSJaegeuk Kim 
175139a53e0cSJaegeuk Kim 	return 0;
175239a53e0cSJaegeuk Kim }
175339a53e0cSJaegeuk Kim 
175455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
175555141486SWanpeng Li {
175655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
175755141486SWanpeng Li }
175855141486SWanpeng Li 
175939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
176039a53e0cSJaegeuk Kim {
176139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
17621dbe4152SChangman Lee 	int offset;
17631dbe4152SChangman Lee 
176455141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
17651dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
17661dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
17671dbe4152SChangman Lee 		else
176865b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
17691dbe4152SChangman Lee 	} else {
17701dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
177125ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
177239a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
177339a53e0cSJaegeuk Kim 	}
17741dbe4152SChangman Lee }
177539a53e0cSJaegeuk Kim 
177639a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
177739a53e0cSJaegeuk Kim {
17788508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
177939a53e0cSJaegeuk Kim 
17808508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
178139a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
178239a53e0cSJaegeuk Kim 	return start_addr;
178339a53e0cSJaegeuk Kim }
178439a53e0cSJaegeuk Kim 
17858508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
17868508e44aSJaegeuk Kim {
17878508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
17888508e44aSJaegeuk Kim 
17898508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
17908508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
17918508e44aSJaegeuk Kim 	return start_addr;
17928508e44aSJaegeuk Kim }
17938508e44aSJaegeuk Kim 
17948508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
17958508e44aSJaegeuk Kim {
17968508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
17978508e44aSJaegeuk Kim }
17988508e44aSJaegeuk Kim 
179939a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
180039a53e0cSJaegeuk Kim {
180139a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
180239a53e0cSJaegeuk Kim }
180339a53e0cSJaegeuk Kim 
18040abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1805000519f2SChao Yu 					struct inode *inode, bool is_inode)
180639a53e0cSJaegeuk Kim {
180739a53e0cSJaegeuk Kim 	block_t	valid_block_count;
180839a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18090abd675eSChao Yu 	bool quota = inode && !is_inode;
18100abd675eSChao Yu 
18110abd675eSChao Yu 	if (quota) {
18120abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18130abd675eSChao Yu 		if (ret)
18140abd675eSChao Yu 			return ret;
18150abd675eSChao Yu 	}
181639a53e0cSJaegeuk Kim 
1817812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1818812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1819812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1820812c6056SChao Yu 		goto enospc;
1821812c6056SChao Yu 	}
1822812c6056SChao Yu #endif
1823812c6056SChao Yu 
182439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
182539a53e0cSJaegeuk Kim 
18267e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18277e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18287e65be49SJaegeuk Kim 
1829*d8a9a229SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode))
18307e65be49SJaegeuk Kim 		valid_block_count += sbi->root_reserved_blocks;
18317e65be49SJaegeuk Kim 
18327e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
183339a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18340abd675eSChao Yu 		goto enospc;
183539a53e0cSJaegeuk Kim 	}
183639a53e0cSJaegeuk Kim 
1837ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1838cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
183939a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18400abd675eSChao Yu 		goto enospc;
184139a53e0cSJaegeuk Kim 	}
184239a53e0cSJaegeuk Kim 
1843ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1844ef86d709SGu Zheng 	sbi->total_valid_block_count++;
184539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
184639a53e0cSJaegeuk Kim 
18470abd675eSChao Yu 	if (inode) {
18480abd675eSChao Yu 		if (is_inode)
18490abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
18500abd675eSChao Yu 		else
18510abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
18520abd675eSChao Yu 	}
18530abd675eSChao Yu 
185441382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
18550abd675eSChao Yu 	return 0;
18560abd675eSChao Yu 
18570abd675eSChao Yu enospc:
18580abd675eSChao Yu 	if (quota)
18590abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
18600abd675eSChao Yu 	return -ENOSPC;
186139a53e0cSJaegeuk Kim }
186239a53e0cSJaegeuk Kim 
186339a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1864000519f2SChao Yu 					struct inode *inode, bool is_inode)
186539a53e0cSJaegeuk Kim {
186639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
186739a53e0cSJaegeuk Kim 
18689850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
18699850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1870000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
187139a53e0cSJaegeuk Kim 
1872ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1873ef86d709SGu Zheng 	sbi->total_valid_block_count--;
187480d42145SYunlong Song 	if (sbi->reserved_blocks &&
187580d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
187680d42145SYunlong Song 		sbi->current_reserved_blocks++;
187739a53e0cSJaegeuk Kim 
187839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
18790abd675eSChao Yu 
18800abd675eSChao Yu 	if (!is_inode)
18810abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
188239a53e0cSJaegeuk Kim }
188339a53e0cSJaegeuk Kim 
188439a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
188539a53e0cSJaegeuk Kim {
18868b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
188739a53e0cSJaegeuk Kim }
188839a53e0cSJaegeuk Kim 
188939a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
189039a53e0cSJaegeuk Kim {
1891513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
189239a53e0cSJaegeuk Kim }
189339a53e0cSJaegeuk Kim 
18940e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
189539a53e0cSJaegeuk Kim {
1896513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
189739a53e0cSJaegeuk Kim }
189839a53e0cSJaegeuk Kim 
1899513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
190039a53e0cSJaegeuk Kim {
1901513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
190239a53e0cSJaegeuk Kim }
190339a53e0cSJaegeuk Kim 
1904a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1905a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1906a56c7c6fSJaegeuk Kim {
1907c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1908c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1909cac5a3d8SDongOh Shin 
1910c41f3cc3SJaegeuk Kim 	if (page)
1911c41f3cc3SJaegeuk Kim 		return page;
1912c41f3cc3SJaegeuk Kim 
191355523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
191455523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1915c41f3cc3SJaegeuk Kim 		return NULL;
191655523519SChao Yu 	}
1917c41f3cc3SJaegeuk Kim #endif
1918a56c7c6fSJaegeuk Kim 	if (!for_write)
1919a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1920a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1921a56c7c6fSJaegeuk Kim }
1922a56c7c6fSJaegeuk Kim 
192301eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
192401eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
192501eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
192601eccef7SChao Yu {
192701eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
192801eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
192901eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
193001eccef7SChao Yu 		return NULL;
193101eccef7SChao Yu 	}
193201eccef7SChao Yu #endif
193301eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
193401eccef7SChao Yu }
193501eccef7SChao Yu 
19366e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19376e2c64adSJaegeuk Kim {
19386e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19396e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19406e2c64adSJaegeuk Kim 
19416e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19426e2c64adSJaegeuk Kim 	kunmap(dst);
19436e2c64adSJaegeuk Kim 	kunmap(src);
19446e2c64adSJaegeuk Kim }
19456e2c64adSJaegeuk Kim 
194639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
194739a53e0cSJaegeuk Kim {
1948031fa8ccSJaegeuk Kim 	if (!page)
194939a53e0cSJaegeuk Kim 		return;
195039a53e0cSJaegeuk Kim 
195139a53e0cSJaegeuk Kim 	if (unlock) {
19529850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
195339a53e0cSJaegeuk Kim 		unlock_page(page);
195439a53e0cSJaegeuk Kim 	}
195509cbfeafSKirill A. Shutemov 	put_page(page);
195639a53e0cSJaegeuk Kim }
195739a53e0cSJaegeuk Kim 
195839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
195939a53e0cSJaegeuk Kim {
196039a53e0cSJaegeuk Kim 	if (dn->node_page)
196139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
196239a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
196339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
196439a53e0cSJaegeuk Kim 	dn->node_page = NULL;
196539a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
196639a53e0cSJaegeuk Kim }
196739a53e0cSJaegeuk Kim 
196839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1969e8512d2eSGu Zheng 					size_t size)
197039a53e0cSJaegeuk Kim {
1971e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
197239a53e0cSJaegeuk Kim }
197339a53e0cSJaegeuk Kim 
19747bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
19757bd59381SGu Zheng 						gfp_t flags)
19767bd59381SGu Zheng {
19777bd59381SGu Zheng 	void *entry;
19787bd59381SGu Zheng 
197980c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
198080c54505SJaegeuk Kim 	if (!entry)
198180c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
19827bd59381SGu Zheng 	return entry;
19837bd59381SGu Zheng }
19847bd59381SGu Zheng 
1985d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
1986d62fe971SChao Yu 						int npages, bool no_fail)
1987740432f8SJaegeuk Kim {
1988740432f8SJaegeuk Kim 	struct bio *bio;
1989740432f8SJaegeuk Kim 
1990d62fe971SChao Yu 	if (no_fail) {
1991740432f8SJaegeuk Kim 		/* No failure on bio allocation */
1992740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
199380c54505SJaegeuk Kim 		if (!bio)
199480c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1995740432f8SJaegeuk Kim 		return bio;
1996740432f8SJaegeuk Kim 	}
1997d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1998d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
1999d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2000d62fe971SChao Yu 		return NULL;
2001d62fe971SChao Yu 	}
2002d62fe971SChao Yu #endif
2003d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2004d62fe971SChao Yu }
2005740432f8SJaegeuk Kim 
20069be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20079be32d72SJaegeuk Kim 				unsigned long index, void *item)
20089be32d72SJaegeuk Kim {
20099be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20109be32d72SJaegeuk Kim 		cond_resched();
20119be32d72SJaegeuk Kim }
20129be32d72SJaegeuk Kim 
201339a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
201439a53e0cSJaegeuk Kim 
201539a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
201639a53e0cSJaegeuk Kim {
201745590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2018cac5a3d8SDongOh Shin 
201939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
202039a53e0cSJaegeuk Kim }
202139a53e0cSJaegeuk Kim 
20227a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20237a2af766SChao Yu {
20247a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20257a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20267a2af766SChao Yu }
20277a2af766SChao Yu 
202839a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
202939a53e0cSJaegeuk Kim {
203039a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
203139a53e0cSJaegeuk Kim }
203239a53e0cSJaegeuk Kim 
20337a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20347a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20357a2af766SChao Yu 			struct page *node_page, unsigned int offset)
203639a53e0cSJaegeuk Kim {
203739a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
203839a53e0cSJaegeuk Kim 	__le32 *addr_array;
20397a2af766SChao Yu 	int base = 0;
20407a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2041cac5a3d8SDongOh Shin 
204245590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20437a2af766SChao Yu 
20447a2af766SChao Yu 	/* from GC path only */
2045979f492fSLiFan 	if (is_inode) {
2046979f492fSLiFan 		if (!inode)
20477a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2048979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20497a2af766SChao Yu 			base = get_extra_isize(inode);
20507a2af766SChao Yu 	}
20517a2af766SChao Yu 
205239a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
20537a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
205439a53e0cSJaegeuk Kim }
205539a53e0cSJaegeuk Kim 
205639a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
205739a53e0cSJaegeuk Kim {
205839a53e0cSJaegeuk Kim 	int mask;
205939a53e0cSJaegeuk Kim 
206039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
206139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
206239a53e0cSJaegeuk Kim 	return mask & *addr;
206339a53e0cSJaegeuk Kim }
206439a53e0cSJaegeuk Kim 
2065a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2066a66cdd98SJaegeuk Kim {
2067a66cdd98SJaegeuk Kim 	int mask;
2068a66cdd98SJaegeuk Kim 
2069a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2070a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2071a66cdd98SJaegeuk Kim 	*addr |= mask;
2072a66cdd98SJaegeuk Kim }
2073a66cdd98SJaegeuk Kim 
2074a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2075a66cdd98SJaegeuk Kim {
2076a66cdd98SJaegeuk Kim 	int mask;
2077a66cdd98SJaegeuk Kim 
2078a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2079a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2080a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2081a66cdd98SJaegeuk Kim }
2082a66cdd98SJaegeuk Kim 
208352aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
208439a53e0cSJaegeuk Kim {
208539a53e0cSJaegeuk Kim 	int mask;
208639a53e0cSJaegeuk Kim 	int ret;
208739a53e0cSJaegeuk Kim 
208839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
208939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
209039a53e0cSJaegeuk Kim 	ret = mask & *addr;
209139a53e0cSJaegeuk Kim 	*addr |= mask;
209239a53e0cSJaegeuk Kim 	return ret;
209339a53e0cSJaegeuk Kim }
209439a53e0cSJaegeuk Kim 
209552aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
209639a53e0cSJaegeuk Kim {
209739a53e0cSJaegeuk Kim 	int mask;
209839a53e0cSJaegeuk Kim 	int ret;
209939a53e0cSJaegeuk Kim 
210039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
210139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
210239a53e0cSJaegeuk Kim 	ret = mask & *addr;
210339a53e0cSJaegeuk Kim 	*addr &= ~mask;
210439a53e0cSJaegeuk Kim 	return ret;
210539a53e0cSJaegeuk Kim }
210639a53e0cSJaegeuk Kim 
2107c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2108c6ac4c0eSGu Zheng {
2109c6ac4c0eSGu Zheng 	int mask;
2110c6ac4c0eSGu Zheng 
2111c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2112c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2113c6ac4c0eSGu Zheng 	*addr ^= mask;
2114c6ac4c0eSGu Zheng }
2115c6ac4c0eSGu Zheng 
21165c57132eSChao Yu #define F2FS_REG_FLMASK		(~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
21175c57132eSChao Yu #define F2FS_OTHER_FLMASK	(FS_NODUMP_FL | FS_NOATIME_FL)
21185c57132eSChao Yu #define F2FS_FL_INHERITED	(FS_PROJINHERIT_FL)
21195c57132eSChao Yu 
21205c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
21215c57132eSChao Yu {
21225c57132eSChao Yu 	if (S_ISDIR(mode))
21235c57132eSChao Yu 		return flags;
21245c57132eSChao Yu 	else if (S_ISREG(mode))
21255c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
21265c57132eSChao Yu 	else
21275c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
21285c57132eSChao Yu }
21295c57132eSChao Yu 
213039a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
213139a53e0cSJaegeuk Kim enum {
213239a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2133b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
213426de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2135ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
213639a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
213739a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
213839a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2139c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2140c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2141444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
21421001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
214334d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2144fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2145fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
214688b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
214788b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
21485fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
214902a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
21503c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
21511e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2152b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2153510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2154d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2155c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2156dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2157ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
21587a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
21595c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
21601ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
216139a53e0cSJaegeuk Kim };
216239a53e0cSJaegeuk Kim 
2163205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2164205b9822SJaegeuk Kim 						int flag, bool set)
216539a53e0cSJaegeuk Kim {
2166205b9822SJaegeuk Kim 	switch (flag) {
2167205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2168205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2169205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
21709ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2171205b9822SJaegeuk Kim 		if (set)
2172205b9822SJaegeuk Kim 			return;
2173205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2174205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
21751ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
21767c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2177205b9822SJaegeuk Kim 	}
217839a53e0cSJaegeuk Kim }
217939a53e0cSJaegeuk Kim 
218091942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
218139a53e0cSJaegeuk Kim {
218291942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
218391942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2184205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
218539a53e0cSJaegeuk Kim }
218639a53e0cSJaegeuk Kim 
218791942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
218839a53e0cSJaegeuk Kim {
218991942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
219039a53e0cSJaegeuk Kim }
219139a53e0cSJaegeuk Kim 
219291942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
219339a53e0cSJaegeuk Kim {
219491942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
219591942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2196205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
219739a53e0cSJaegeuk Kim }
219839a53e0cSJaegeuk Kim 
219991942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2200444c580fSJaegeuk Kim {
220191942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
220291942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
22037c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
220439a53e0cSJaegeuk Kim }
220539a53e0cSJaegeuk Kim 
2206a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2207a1961246SJaegeuk Kim {
2208a1961246SJaegeuk Kim 	if (inc)
2209a1961246SJaegeuk Kim 		inc_nlink(inode);
2210a1961246SJaegeuk Kim 	else
2211a1961246SJaegeuk Kim 		drop_nlink(inode);
22127c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2213a1961246SJaegeuk Kim }
2214a1961246SJaegeuk Kim 
22158edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
22160abd675eSChao Yu 					block_t diff, bool add, bool claim)
22178edd03c8SJaegeuk Kim {
221826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
221926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
222026de9b11SJaegeuk Kim 
22210abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
22220abd675eSChao Yu 	if (add) {
22230abd675eSChao Yu 		if (claim)
22240abd675eSChao Yu 			dquot_claim_block(inode, diff);
22250abd675eSChao Yu 		else
22260abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
22270abd675eSChao Yu 	} else {
22280abd675eSChao Yu 		dquot_free_block(inode, diff);
22290abd675eSChao Yu 	}
22300abd675eSChao Yu 
22317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
223226de9b11SJaegeuk Kim 	if (clean || recover)
223326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
22348edd03c8SJaegeuk Kim }
22358edd03c8SJaegeuk Kim 
2236fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2237fc9581c8SJaegeuk Kim {
223826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
223926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
224026de9b11SJaegeuk Kim 
2241fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2242fc9581c8SJaegeuk Kim 		return;
2243fc9581c8SJaegeuk Kim 
2244fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
22457c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
224626de9b11SJaegeuk Kim 	if (clean || recover)
224726de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
224826de9b11SJaegeuk Kim }
224926de9b11SJaegeuk Kim 
2250205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2251205b9822SJaegeuk Kim {
2252205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
22537c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2254205b9822SJaegeuk Kim }
2255205b9822SJaegeuk Kim 
22561ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
22571ad71a27SJaegeuk Kim 					unsigned int count)
22581ad71a27SJaegeuk Kim {
22591ad71a27SJaegeuk Kim 	F2FS_I(inode)->i_gc_failures = count;
22601ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
22611ad71a27SJaegeuk Kim }
22621ad71a27SJaegeuk Kim 
2263205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2264205b9822SJaegeuk Kim {
2265205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
22667c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2267205b9822SJaegeuk Kim }
2268205b9822SJaegeuk Kim 
2269205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2270205b9822SJaegeuk Kim {
2271205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
22727c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2273205b9822SJaegeuk Kim }
2274205b9822SJaegeuk Kim 
227591942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2276444c580fSJaegeuk Kim {
2277205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2278205b9822SJaegeuk Kim 
2279444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2280205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
22811001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2282205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
228334d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2284205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2285b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2286205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2287510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2288205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
22897a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
22907a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
22911ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
22921ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2293444c580fSJaegeuk Kim }
2294444c580fSJaegeuk Kim 
229591942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2296444c580fSJaegeuk Kim {
2297444c580fSJaegeuk Kim 	ri->i_inline = 0;
2298444c580fSJaegeuk Kim 
229991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2300444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
230191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
23021001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
230391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
230434d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
230591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2306b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
230791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2308510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
23097a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
23107a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
23111ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
23121ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
23137a2af766SChao Yu }
23147a2af766SChao Yu 
23157a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
23167a2af766SChao Yu {
23177a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2318444c580fSJaegeuk Kim }
2319444c580fSJaegeuk Kim 
2320987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2321987c7c31SChao Yu {
232291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2323987c7c31SChao Yu }
2324987c7c31SChao Yu 
232581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2326de93653fSJaegeuk Kim {
23276afc662eSChao Yu 	return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS(inode);
2328de93653fSJaegeuk Kim }
2329de93653fSJaegeuk Kim 
23306afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
233165985d93SJaegeuk Kim {
2332695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2333cac5a3d8SDongOh Shin 
233465985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
23356afc662eSChao Yu 					F2FS_INLINE_XATTR_ADDRS(inode)]);
233665985d93SJaegeuk Kim }
233765985d93SJaegeuk Kim 
233865985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
233965985d93SJaegeuk Kim {
23406afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
234165985d93SJaegeuk Kim }
234265985d93SJaegeuk Kim 
23430dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
23440dbdc2aeSJaegeuk Kim {
234591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
23460dbdc2aeSJaegeuk Kim }
23470dbdc2aeSJaegeuk Kim 
2348b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2349b3d208f9SJaegeuk Kim {
235091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2351b3d208f9SJaegeuk Kim }
2352b3d208f9SJaegeuk Kim 
2353510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2354510022a8SJaegeuk Kim {
235591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2356510022a8SJaegeuk Kim }
2357510022a8SJaegeuk Kim 
23581ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
23591ad71a27SJaegeuk Kim {
23601ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
23611ad71a27SJaegeuk Kim }
23621ad71a27SJaegeuk Kim 
236388b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
236488b88a66SJaegeuk Kim {
236591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
236688b88a66SJaegeuk Kim }
236788b88a66SJaegeuk Kim 
23685fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
23695fe45743SChao Yu {
23705fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
23715fe45743SChao Yu }
23725fe45743SChao Yu 
237302a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
237402a1335fSJaegeuk Kim {
237591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
237602a1335fSJaegeuk Kim }
237702a1335fSJaegeuk Kim 
23783c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
23793c6c2bebSJaegeuk Kim {
238091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
23813c6c2bebSJaegeuk Kim }
23823c6c2bebSJaegeuk Kim 
23831e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
23841e84371fSJaegeuk Kim {
238591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
23861e84371fSJaegeuk Kim }
23871e84371fSJaegeuk Kim 
2388f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
23891001b347SHuajun Li {
2390695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
23917a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2392cac5a3d8SDongOh Shin 
23937a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
23941001b347SHuajun Li }
23951001b347SHuajun Li 
239634d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
239734d67debSChao Yu {
239891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
239934d67debSChao Yu }
240034d67debSChao Yu 
24019486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
24029486ba44SJaegeuk Kim {
24039486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
24049486ba44SJaegeuk Kim 		kunmap(page);
24059486ba44SJaegeuk Kim }
24069486ba44SJaegeuk Kim 
2407b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2408b5492af7SJaegeuk Kim {
2409b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2410b5492af7SJaegeuk Kim }
2411b5492af7SJaegeuk Kim 
2412b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2413b5492af7SJaegeuk Kim {
2414b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
24157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2416b5492af7SJaegeuk Kim }
2417b5492af7SJaegeuk Kim 
2418b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2419b5492af7SJaegeuk Kim {
2420b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
24217c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2422b5492af7SJaegeuk Kim }
2423b5492af7SJaegeuk Kim 
242426787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
242526787236SJaegeuk Kim {
2426a0d00fadSChao Yu 	bool ret;
2427a0d00fadSChao Yu 
242826787236SJaegeuk Kim 	if (dsync) {
242926787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
243026787236SJaegeuk Kim 
243126787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
243226787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
243326787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
243426787236SJaegeuk Kim 		return ret;
243526787236SJaegeuk Kim 	}
243626787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
243726787236SJaegeuk Kim 			file_keep_isize(inode) ||
243826787236SJaegeuk Kim 			i_size_read(inode) & PAGE_MASK)
243926787236SJaegeuk Kim 		return false;
2440a0d00fadSChao Yu 
2441a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2442a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2443a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2444a0d00fadSChao Yu 
2445a0d00fadSChao Yu 	return ret;
2446267378d4SChao Yu }
2447267378d4SChao Yu 
2448267378d4SChao Yu static inline int f2fs_readonly(struct super_block *sb)
2449267378d4SChao Yu {
24501751e8a6SLinus Torvalds 	return sb->s_flags & SB_RDONLY;
245177888c1eSJaegeuk Kim }
245277888c1eSJaegeuk Kim 
2453744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2454744602cfSJaegeuk Kim {
2455aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2456744602cfSJaegeuk Kim }
2457744602cfSJaegeuk Kim 
2458eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2459eaa693f4SJaegeuk Kim {
2460eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2461eaa693f4SJaegeuk Kim 		return true;
2462eaa693f4SJaegeuk Kim 
2463eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2464eaa693f4SJaegeuk Kim 		return true;
2465eaa693f4SJaegeuk Kim 
2466eaa693f4SJaegeuk Kim 	return false;
2467eaa693f4SJaegeuk Kim }
2468eaa693f4SJaegeuk Kim 
24693e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
24703e72f721SJaegeuk Kim {
24713e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
247291942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
24733e72f721SJaegeuk Kim 		return false;
24743e72f721SJaegeuk Kim 
2475886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
24763e72f721SJaegeuk Kim }
24773e72f721SJaegeuk Kim 
24781ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
24791ecc0c5cSChao Yu 					size_t size, gfp_t flags)
24800414b004SJaegeuk Kim {
24812c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
248255523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
248355523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
24842c63feadSJaegeuk Kim 		return NULL;
248555523519SChao Yu 	}
24862c63feadSJaegeuk Kim #endif
24870414b004SJaegeuk Kim 	return kmalloc(size, flags);
24880414b004SJaegeuk Kim }
24890414b004SJaegeuk Kim 
2490acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2491acbf054dSChao Yu 					size_t size, gfp_t flags)
2492acbf054dSChao Yu {
2493acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2494acbf054dSChao Yu }
2495acbf054dSChao Yu 
2496628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2497628b3d14SChao Yu 					size_t size, gfp_t flags)
2498628b3d14SChao Yu {
2499628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2500628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2501628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2502628b3d14SChao Yu 		return NULL;
2503628b3d14SChao Yu 	}
2504628b3d14SChao Yu #endif
2505628b3d14SChao Yu 	return kvmalloc(size, flags);
2506628b3d14SChao Yu }
2507628b3d14SChao Yu 
2508628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2509628b3d14SChao Yu 					size_t size, gfp_t flags)
2510628b3d14SChao Yu {
2511628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2512628b3d14SChao Yu }
2513628b3d14SChao Yu 
25147a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2515f2470371SChao Yu {
25167a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2517f2470371SChao Yu }
2518f2470371SChao Yu 
25196afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
25206afc662eSChao Yu {
25216afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
25226afc662eSChao Yu }
25236afc662eSChao Yu 
2524a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
252591942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2526a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2527a6dda0e6SChristoph Hellwig 
25287a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
25297a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
25307a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
25317a2af766SChao Yu 
25325c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
25335c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
25345c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
25355c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
25365c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
25375c57132eSChao Yu 
2538b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2539b0af6d49SChao Yu {
2540b0af6d49SChao Yu 	int i;
2541b0af6d49SChao Yu 
2542b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2543b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2544b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2545b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2546b0af6d49SChao Yu }
2547b0af6d49SChao Yu 
2548b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2549b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2550b0af6d49SChao Yu {
2551b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2552b0af6d49SChao Yu 		return;
2553b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2554b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2555b0af6d49SChao Yu 
2556b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2557b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2558b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2559b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2560b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2561b0af6d49SChao Yu }
2562b0af6d49SChao Yu 
256339a53e0cSJaegeuk Kim /*
256439a53e0cSJaegeuk Kim  * file.c
256539a53e0cSJaegeuk Kim  */
2566cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2567cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2568cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2569cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2570a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2571a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2572cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2573cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2574f652e9d9SChao Yu void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2575c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2576cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2577cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
25781ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
257939a53e0cSJaegeuk Kim 
258039a53e0cSJaegeuk Kim /*
258139a53e0cSJaegeuk Kim  * inode.c
258239a53e0cSJaegeuk Kim  */
2583cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2584704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2585704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2586cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2587cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2588cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2589211a6fa0SYunlei He void update_inode(struct inode *inode, struct page *node_page);
2590211a6fa0SYunlei He void update_inode_page(struct inode *inode);
2591cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2592cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2593cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
259439a53e0cSJaegeuk Kim 
259539a53e0cSJaegeuk Kim /*
259639a53e0cSJaegeuk Kim  * namei.c
259739a53e0cSJaegeuk Kim  */
259839a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
259939a53e0cSJaegeuk Kim 
260039a53e0cSJaegeuk Kim /*
260139a53e0cSJaegeuk Kim  * dir.c
260239a53e0cSJaegeuk Kim  */
2603cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2604cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2605cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2606cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2607cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2608cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2609cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2610cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2611cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2612cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2613cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2614cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2615cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2616cac5a3d8SDongOh Shin 			unsigned int current_depth);
2617cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2618cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2619cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2620cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2621cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2622cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2623cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2624cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2625cac5a3d8SDongOh Shin 			struct page **page);
2626cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2627cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2628cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2629cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2630cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2631cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2632cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2633cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2634cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2635cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2636cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2637cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2638cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2639cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2640cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2641cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
264239a53e0cSJaegeuk Kim 
2643b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2644b7f7a5e0SAl Viro {
26452b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2646510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2647b7f7a5e0SAl Viro }
2648b7f7a5e0SAl Viro 
264939a53e0cSJaegeuk Kim /*
265039a53e0cSJaegeuk Kim  * super.c
265139a53e0cSJaegeuk Kim  */
2652cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2653cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2654ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
26554b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2656cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2657cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2658a07ef784SNamjae Jeon extern __printf(3, 4)
2659cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2660984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
266139a53e0cSJaegeuk Kim 
266239a53e0cSJaegeuk Kim /*
266339a53e0cSJaegeuk Kim  * hash.c
266439a53e0cSJaegeuk Kim  */
26656332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
26666332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
266739a53e0cSJaegeuk Kim 
266839a53e0cSJaegeuk Kim /*
266939a53e0cSJaegeuk Kim  * node.c
267039a53e0cSJaegeuk Kim  */
267139a53e0cSJaegeuk Kim struct dnode_of_data;
267239a53e0cSJaegeuk Kim struct node_info;
267339a53e0cSJaegeuk Kim 
2674cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2675cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2676cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2677cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2678cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2679cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2680cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2681cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
26829c77f754SJaegeuk Kim int truncate_xattr_node(struct inode *inode);
2683cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2684cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2685cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
26865f4ce6abSYunlei He struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2687cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2688cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2689cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2690cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2691cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2692cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2693401db79fSYunlong Song int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2694b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
269522ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2696cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2697cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2698cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2699cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2700cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2701e17d488bSSheng Yong int recover_xattr_data(struct inode *inode, struct page *page);
2702cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2703c376fc0fSYunlei He void restore_node_summary(struct f2fs_sb_info *sbi,
2704cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
270522ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2706cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2707cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
27086e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
270939a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
271039a53e0cSJaegeuk Kim 
271139a53e0cSJaegeuk Kim /*
271239a53e0cSJaegeuk Kim  * segment.c
271339a53e0cSJaegeuk Kim  */
2714b3a97a2aSJaegeuk Kim bool need_SSR(struct f2fs_sb_info *sbi);
2715cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
271657864ae5SJaegeuk Kim void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
2717cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
27188c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2719cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2720cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2721cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
272239d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2723cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
27241228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2725cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2726cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2727cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
272878997b56SChao Yu void init_discard_policy(struct discard_policy *dpolicy, int discard_type,
272978997b56SChao Yu 						unsigned int granularity);
2730cce13252SChao Yu void stop_discard_thread(struct f2fs_sb_info *sbi);
2731cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2732cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2733cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2734cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2735cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2736cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2737cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2738cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2739cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2740b0af6d49SChao Yu void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2741b0af6d49SChao Yu 						enum iostat_type io_type);
2742cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2743cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2744d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2745cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2746cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2747cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2748cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2749cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2750cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2751cac5a3d8SDongOh Shin 			bool recover_newaddr);
2752cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2753cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2754fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2755fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2756cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2757cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2758d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2759cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2760cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2761cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2762cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2763cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2764cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2765cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
27667fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
27677fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
2768d5097be5SHyunchul Lee int rw_hint_to_seg_type(enum rw_hint hint);
276939a53e0cSJaegeuk Kim 
277039a53e0cSJaegeuk Kim /*
277139a53e0cSJaegeuk Kim  * checkpoint.c
277239a53e0cSJaegeuk Kim  */
2773cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2774cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2775cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2776cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2777cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2778cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2779cac5a3d8SDongOh Shin 			int type, bool sync);
2780cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2781cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2782b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
2783cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2784cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2785cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2786cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
278739d787beSChao Yu void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
278839d787beSChao Yu 					unsigned int devidx, int type);
278939d787beSChao Yu bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
279039d787beSChao Yu 					unsigned int devidx, int type);
2791cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2792cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2793cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2794cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2795cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2796cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2797cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2798cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2799cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2800cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2801cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2802cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
28036e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
280439a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
280539a53e0cSJaegeuk Kim 
280639a53e0cSJaegeuk Kim /*
280739a53e0cSJaegeuk Kim  * data.c
280839a53e0cSJaegeuk Kim  */
2809b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2810b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2811942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2812b9109b0eSJaegeuk Kim 				enum page_type type);
2813b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2814cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2815b9109b0eSJaegeuk Kim int f2fs_submit_page_write(struct f2fs_io_info *fio);
2816cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2817cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2818cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2819cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2820cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2821cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2822cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2823cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2824cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2825cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2826cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2827cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2828cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2829cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2830cac5a3d8SDongOh Shin 			bool for_write);
2831cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2832cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2833cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2834cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2835cac5a3d8SDongOh Shin 			int create, int flag);
2836cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2837cac5a3d8SDongOh Shin 			u64 start, u64 len);
2838cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2839b0af6d49SChao Yu int __f2fs_write_data_pages(struct address_space *mapping,
2840b0af6d49SChao Yu 						struct writeback_control *wbc,
2841b0af6d49SChao Yu 						enum iostat_type io_type);
2842cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2843cac5a3d8SDongOh Shin 			unsigned int length);
2844cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
28455b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2846cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2847cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
28485b7a487cSWeichao Guo #endif
284939a53e0cSJaegeuk Kim 
285039a53e0cSJaegeuk Kim /*
285139a53e0cSJaegeuk Kim  * gc.c
285239a53e0cSJaegeuk Kim  */
2853cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2854cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2855cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2856e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2857e066b83cSJaegeuk Kim 			unsigned int segno);
2858cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
285939a53e0cSJaegeuk Kim 
286039a53e0cSJaegeuk Kim /*
286139a53e0cSJaegeuk Kim  * recovery.c
286239a53e0cSJaegeuk Kim  */
2863cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2864cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
286539a53e0cSJaegeuk Kim 
286639a53e0cSJaegeuk Kim /*
286739a53e0cSJaegeuk Kim  * debug.c
286839a53e0cSJaegeuk Kim  */
286939a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
287039a53e0cSJaegeuk Kim struct f2fs_stat_info {
287139a53e0cSJaegeuk Kim 	struct list_head stat_list;
287239a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
287339a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
287439a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
28755b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
28765b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2877c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
28782c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
28792c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
288035782b23SJaegeuk Kim 	int inmem_pages;
28812c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
28825b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
28835b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
288439a53e0cSJaegeuk Kim 	int total_count, utilization;
28858b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
288614d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
288714d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
28885f32366aSChao Yu 	int nr_discard_cmd;
2889d84d1cbdSChao Yu 	unsigned int undiscard_blks;
2890a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2891648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2892f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
289339a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
289439a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
289539a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
289639a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
289742190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
289839a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2899e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
290039a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2901e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
290239a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
290339a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
290439a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
290539a53e0cSJaegeuk Kim 
290639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
290739a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2908b9a2c252SChangman Lee 	unsigned int inplace_count;
29099edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
291039a53e0cSJaegeuk Kim };
291139a53e0cSJaegeuk Kim 
2912963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2913963d4f7dSGu Zheng {
2914963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2915963d4f7dSGu Zheng }
2916963d4f7dSGu Zheng 
2917942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
291842190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
291939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2920dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
292133fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
292233fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
29235b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
29245b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
29255b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
29265b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2927d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2928d5e8f6c9SChao Yu 	do {								\
2929d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2930d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2931d5e8f6c9SChao Yu 	} while (0)
2932d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2933d5e8f6c9SChao Yu 	do {								\
2934d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2935d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2936d5e8f6c9SChao Yu 	} while (0)
29370dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
29380dbdc2aeSJaegeuk Kim 	do {								\
29390dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
294003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
29410dbdc2aeSJaegeuk Kim 	} while (0)
29420dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
29430dbdc2aeSJaegeuk Kim 	do {								\
29440dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
294503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
29460dbdc2aeSJaegeuk Kim 	} while (0)
29473289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
29483289c061SJaegeuk Kim 	do {								\
29493289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
295003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
29513289c061SJaegeuk Kim 	} while (0)
29523289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
29533289c061SJaegeuk Kim 	do {								\
29543289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
295503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
29563289c061SJaegeuk Kim 	} while (0)
2957dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2958dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2959dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2960dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2961b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2962b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
296326a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
2964cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
296526a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
2966cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
296726a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
296826a28a0cSJaegeuk Kim 	do {								\
296926a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
297026a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
297126a28a0cSJaegeuk Kim 		if (cur > max)						\
297226a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
297326a28a0cSJaegeuk Kim 	} while (0)
2974648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
2975648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2976648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
2977648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2978648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
2979648d50baSChao Yu 	do {								\
2980648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
2981648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
2982648d50baSChao Yu 		if (cur > max)						\
2983648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
2984648d50baSChao Yu 	} while (0)
2985e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
298639a53e0cSJaegeuk Kim 	do {								\
2987963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
298868afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
298968afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
299039a53e0cSJaegeuk Kim 			si->data_segs++;				\
2991e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2992e1235983SChangman Lee 		} else {						\
299339a53e0cSJaegeuk Kim 			si->node_segs++;				\
2994e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2995e1235983SChangman Lee 		}							\
299639a53e0cSJaegeuk Kim 	} while (0)
299739a53e0cSJaegeuk Kim 
299839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
299968afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
300039a53e0cSJaegeuk Kim 
3001e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
300239a53e0cSJaegeuk Kim 	do {								\
3003963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
300439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
300539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
300668afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
300739a53e0cSJaegeuk Kim 	} while (0)
300839a53e0cSJaegeuk Kim 
3009e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
301039a53e0cSJaegeuk Kim 	do {								\
3011963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
301239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
301339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
301468afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
301539a53e0cSJaegeuk Kim 	} while (0)
301639a53e0cSJaegeuk Kim 
3017cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3018cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3019787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
30204589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
302139a53e0cSJaegeuk Kim #else
3022d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3023d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3024d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3025d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3026d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3027d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3028d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3029d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3030d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3031d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3032d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3033d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3034d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3035d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3036d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3037d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3038d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3039d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3040d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3041d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3042d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3043d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3044d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3045d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3046d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3047d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3048d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3049d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3050d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
305139a53e0cSJaegeuk Kim 
305239a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
305339a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3054787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
30554589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
305639a53e0cSJaegeuk Kim #endif
305739a53e0cSJaegeuk Kim 
305839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
305939a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
306039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
306139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
306239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
306339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
306439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
306539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3066cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
306739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
306829e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
30691001b347SHuajun Li 
3070e18c65b2SHuajun Li /*
3071e18c65b2SHuajun Li  * inline.c
3072e18c65b2SHuajun Li  */
3073cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3074cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
3075cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
3076bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3077cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3078cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3079cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3080cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
3081cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
3082cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3083cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
3084cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3085cac5a3d8SDongOh Shin 			struct page *ipage);
3086cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3087cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3088cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3089cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3090cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3091cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3092cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3093cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3094cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3095cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3096cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3097cde4de12SJaegeuk Kim 
3098cde4de12SJaegeuk Kim /*
30992658e50dSJaegeuk Kim  * shrinker.c
31002658e50dSJaegeuk Kim  */
3101cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3102cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3103cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3104cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3105cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3106cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
31072658e50dSJaegeuk Kim 
31082658e50dSJaegeuk Kim /*
3109a28ef1f5SChao Yu  * extent_cache.c
3110a28ef1f5SChao Yu  */
3111004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3112004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
3113004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3114004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3115004b6862SChao Yu 				unsigned int ofs);
3116004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3117004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3118004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3119004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3120004b6862SChao Yu 		bool force);
3121df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3122df0f6b44SChao Yu 						struct rb_root *root);
3123cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3124cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3125cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3126cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3127cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3128cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3129cac5a3d8SDongOh Shin 			struct extent_info *ei);
3130cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
313119b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3132cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3133cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
3134a28ef1f5SChao Yu int __init create_extent_cache(void);
3135a28ef1f5SChao Yu void destroy_extent_cache(void);
3136a28ef1f5SChao Yu 
3137a28ef1f5SChao Yu /*
31388ceffcb2SChao Yu  * sysfs.c
31398ceffcb2SChao Yu  */
3140dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3141dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3142dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3143dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
31448ceffcb2SChao Yu 
31458ceffcb2SChao Yu /*
3146cde4de12SJaegeuk Kim  * crypto support
3147cde4de12SJaegeuk Kim  */
31480b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3149cde4de12SJaegeuk Kim {
3150cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3151cde4de12SJaegeuk Kim }
3152cde4de12SJaegeuk Kim 
31531958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
31541958593eSJaegeuk Kim {
31551958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
31561958593eSJaegeuk Kim }
31571958593eSJaegeuk Kim 
3158cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3159cde4de12SJaegeuk Kim {
3160cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3161cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
31622ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3163cde4de12SJaegeuk Kim #endif
3164cde4de12SJaegeuk Kim }
3165cde4de12SJaegeuk Kim 
3166cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
3167cde4de12SJaegeuk Kim {
31680b81d077SJaegeuk Kim 	return bio->bi_private != NULL;
3169cde4de12SJaegeuk Kim }
3170cde4de12SJaegeuk Kim 
3171cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
3172cde4de12SJaegeuk Kim {
3173cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
3174cde4de12SJaegeuk Kim }
3175f424f664SJaegeuk Kim 
31760bfd7a09SDamien Le Moal static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
317752763a4bSJaegeuk Kim {
31780bfd7a09SDamien Le Moal 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
317952763a4bSJaegeuk Kim }
318052763a4bSJaegeuk Kim 
31817a2af766SChao Yu static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
31827a2af766SChao Yu {
31837a2af766SChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
31847a2af766SChao Yu }
31857a2af766SChao Yu 
31865c57132eSChao Yu static inline int f2fs_sb_has_project_quota(struct super_block *sb)
31875c57132eSChao Yu {
31885c57132eSChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
31895c57132eSChao Yu }
31905c57132eSChao Yu 
3191704956ecSChao Yu static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
3192704956ecSChao Yu {
3193704956ecSChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
3194704956ecSChao Yu }
3195704956ecSChao Yu 
31966afc662eSChao Yu static inline int f2fs_sb_has_flexible_inline_xattr(struct super_block *sb)
31976afc662eSChao Yu {
31986afc662eSChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
31996afc662eSChao Yu }
32006afc662eSChao Yu 
3201234a9689SJaegeuk Kim static inline int f2fs_sb_has_quota_ino(struct super_block *sb)
3202234a9689SJaegeuk Kim {
3203234a9689SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_QUOTA_INO);
3204234a9689SJaegeuk Kim }
3205234a9689SJaegeuk Kim 
3206178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3207178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
32083c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3209178053e2SDamien Le Moal {
3210178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
32113c62be17SJaegeuk Kim 	int i;
3212178053e2SDamien Le Moal 
32133c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
32143c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
32153c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
32163c62be17SJaegeuk Kim 	return -EINVAL;
3217178053e2SDamien Le Moal }
3218178053e2SDamien Le Moal #endif
3219178053e2SDamien Le Moal 
322096ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
322196ba2decSDamien Le Moal {
322296ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
322396ba2decSDamien Le Moal 
322496ba2decSDamien Le Moal 	return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
322552763a4bSJaegeuk Kim }
322652763a4bSJaegeuk Kim 
322752763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
322852763a4bSJaegeuk Kim {
322952763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
323052763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
323152763a4bSJaegeuk Kim 
323252763a4bSJaegeuk Kim 	switch (mt) {
323352763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
323452763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
323552763a4bSJaegeuk Kim 		break;
323652763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
323752763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
323852763a4bSJaegeuk Kim 		break;
323952763a4bSJaegeuk Kim 	}
324052763a4bSJaegeuk Kim }
324152763a4bSJaegeuk Kim 
3242fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3243fcc85a4dSJaegeuk Kim {
3244fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3245886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3246fcc85a4dSJaegeuk Kim 
3247fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3248fcc85a4dSJaegeuk Kim #else
3249fcc85a4dSJaegeuk Kim 	return 0;
3250fcc85a4dSJaegeuk Kim #endif
3251fcc85a4dSJaegeuk Kim }
3252fcc85a4dSJaegeuk Kim 
325339a53e0cSJaegeuk Kim #endif
3254