xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 9a4ffdf55811ff3382cdf44459ec17521bd47e5e)
10a8165d7SJaegeuk Kim /*
239a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
339a53e0cSJaegeuk Kim  *
439a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
539a53e0cSJaegeuk Kim  *             http://www.samsung.com/
639a53e0cSJaegeuk Kim  *
739a53e0cSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
839a53e0cSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
939a53e0cSJaegeuk Kim  * published by the Free Software Foundation.
1039a53e0cSJaegeuk Kim  */
1139a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
1239a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1339a53e0cSJaegeuk Kim 
1439a53e0cSJaegeuk Kim #include <linux/types.h>
1539a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1639a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1739a53e0cSJaegeuk Kim #include <linux/slab.h>
1839a53e0cSJaegeuk Kim #include <linux/crc32.h>
1939a53e0cSJaegeuk Kim #include <linux/magic.h>
20c2d715d1SJaegeuk Kim #include <linux/kobject.h>
217bd59381SGu Zheng #include <linux/sched.h>
2239307a8eSJaegeuk Kim #include <linux/vmalloc.h>
23740432f8SJaegeuk Kim #include <linux/bio.h>
24d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
250abd675eSChao Yu #include <linux/quotaops.h>
2646f47e48SEric Biggers #ifdef CONFIG_F2FS_FS_ENCRYPTION
2746f47e48SEric Biggers #include <linux/fscrypt_supp.h>
2846f47e48SEric Biggers #else
2946f47e48SEric Biggers #include <linux/fscrypt_notsupp.h>
3046f47e48SEric Biggers #endif
3143b6573bSKeith Mok #include <crypto/hash.h>
3239a53e0cSJaegeuk Kim 
335d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
355d56b671SJaegeuk Kim #else
369850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
379850cf4aSJaegeuk Kim 	do {								\
389850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
399850cf4aSJaegeuk Kim 			WARN_ON(1);					\
40caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
419850cf4aSJaegeuk Kim 		}							\
429850cf4aSJaegeuk Kim 	} while (0)
435d56b671SJaegeuk Kim #endif
445d56b671SJaegeuk Kim 
452c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
462c63feadSJaegeuk Kim enum {
472c63feadSJaegeuk Kim 	FAULT_KMALLOC,
48c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
49cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
50cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
51cb78942bSJaegeuk Kim 	FAULT_BLOCK,
52cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5353aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5414b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
558b038c70SChao Yu 	FAULT_IO,
560f348028SChao Yu 	FAULT_CHECKPOINT,
572c63feadSJaegeuk Kim 	FAULT_MAX,
582c63feadSJaegeuk Kim };
592c63feadSJaegeuk Kim 
6008796897SSheng Yong struct f2fs_fault_info {
6108796897SSheng Yong 	atomic_t inject_ops;
6208796897SSheng Yong 	unsigned int inject_rate;
6308796897SSheng Yong 	unsigned int inject_type;
6408796897SSheng Yong };
6508796897SSheng Yong 
662c63feadSJaegeuk Kim extern char *fault_name[FAULT_MAX];
6768afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
682c63feadSJaegeuk Kim #endif
692c63feadSJaegeuk Kim 
7039a53e0cSJaegeuk Kim /*
7139a53e0cSJaegeuk Kim  * For mount options
7239a53e0cSJaegeuk Kim  */
7339a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7439a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7539a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
80444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
811001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8234d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8334d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8434d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
85d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8689672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
876aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
88343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
8973faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9036abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9136abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
920abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
930abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
945c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
954b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
9639a53e0cSJaegeuk Kim 
9768afcf2dSTomohiro Kusumi #define clear_opt(sbi, option)	((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
9868afcf2dSTomohiro Kusumi #define set_opt(sbi, option)	((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
9968afcf2dSTomohiro Kusumi #define test_opt(sbi, option)	((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
10039a53e0cSJaegeuk Kim 
10139a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10239a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10339a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
10439a53e0cSJaegeuk Kim 
105a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
106a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
107a9841c4dSJaegeuk Kim 			 * address format, __le32.
108a9841c4dSJaegeuk Kim 			 */
10939a53e0cSJaegeuk Kim typedef u32 nid_t;
11039a53e0cSJaegeuk Kim 
11139a53e0cSJaegeuk Kim struct f2fs_mount_info {
11239a53e0cSJaegeuk Kim 	unsigned int	opt;
11339a53e0cSJaegeuk Kim };
11439a53e0cSJaegeuk Kim 
115cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1160bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
117e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1187a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1195c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
120704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
121cde4de12SJaegeuk Kim 
12276f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
12376f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
12476f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
125c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
12676f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
127c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
12876f105a2SJaegeuk Kim 
12939a53e0cSJaegeuk Kim /*
13039a53e0cSJaegeuk Kim  * For checkpoint manager
13139a53e0cSJaegeuk Kim  */
13239a53e0cSJaegeuk Kim enum {
13339a53e0cSJaegeuk Kim 	NAT_BITMAP,
13439a53e0cSJaegeuk Kim 	SIT_BITMAP
13539a53e0cSJaegeuk Kim };
13639a53e0cSJaegeuk Kim 
137c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
138c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
139c473f1a9SChao Yu #define	CP_SYNC		0x00000004
140c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
141c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1421f43e2adSChao Yu #define CP_TRIMMED	0x00000020
14375ab4cb8SJaegeuk Kim 
14447b89808SJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	2048
145bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
1464ddb1a4dSJaegeuk Kim 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
147a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
148a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
1494ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
15015469963SJaegeuk Kim #define DISCARD_ISSUE_RATE		8
151969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
152969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
15360b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
154dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
155bba681cbSJaegeuk Kim 
15675ab4cb8SJaegeuk Kim struct cp_control {
15775ab4cb8SJaegeuk Kim 	int reason;
1584b2fecc8SJaegeuk Kim 	__u64 trim_start;
1594b2fecc8SJaegeuk Kim 	__u64 trim_end;
1604b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
1614b2fecc8SJaegeuk Kim 	__u64 trimmed;
16275ab4cb8SJaegeuk Kim };
16375ab4cb8SJaegeuk Kim 
164662befdaSChao Yu /*
16581c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
166662befdaSChao Yu  */
167662befdaSChao Yu enum {
168662befdaSChao Yu 	META_CP,
169662befdaSChao Yu 	META_NAT,
17081c1a0f1SChao Yu 	META_SIT,
1714c521f49SJaegeuk Kim 	META_SSA,
1724c521f49SJaegeuk Kim 	META_POR,
173662befdaSChao Yu };
174662befdaSChao Yu 
1756451e041SJaegeuk Kim /* for the list of ino */
1766451e041SJaegeuk Kim enum {
1776451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
178fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
179fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1806451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1816451e041SJaegeuk Kim };
1826451e041SJaegeuk Kim 
1836451e041SJaegeuk Kim struct ino_entry {
18439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
18539a53e0cSJaegeuk Kim 	nid_t ino;		/* inode number */
18639a53e0cSJaegeuk Kim };
18739a53e0cSJaegeuk Kim 
1882710fd7eSChao Yu /* for the list of inodes to be GCed */
18906292073SChao Yu struct inode_entry {
19039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
19139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
19239a53e0cSJaegeuk Kim };
19339a53e0cSJaegeuk Kim 
194a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
1957fd9e544SJaegeuk Kim struct discard_entry {
1967fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
197a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
198a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
1997fd9e544SJaegeuk Kim };
2007fd9e544SJaegeuk Kim 
201969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
202969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
203969d1b18SChao Yu 
204ba48a33eSChao Yu /* max discard pend list number */
205ba48a33eSChao Yu #define MAX_PLIST_NUM		512
206ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
207ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
208ba48a33eSChao Yu 
209969d1b18SChao Yu #define P_ACTIVE	0x01
210969d1b18SChao Yu #define P_TRIM		0x02
211969d1b18SChao Yu #define plist_issue(tag)	(((tag) & P_ACTIVE) || ((tag) & P_TRIM))
212969d1b18SChao Yu 
21315469963SJaegeuk Kim enum {
21415469963SJaegeuk Kim 	D_PREP,
21515469963SJaegeuk Kim 	D_SUBMIT,
21615469963SJaegeuk Kim 	D_DONE,
21715469963SJaegeuk Kim };
21815469963SJaegeuk Kim 
219004b6862SChao Yu struct discard_info {
220b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
221b01a9201SJaegeuk Kim 	block_t len;			/* length */
222004b6862SChao Yu 	block_t start;			/* actual start address in dev */
223004b6862SChao Yu };
224004b6862SChao Yu 
225b01a9201SJaegeuk Kim struct discard_cmd {
226004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
227004b6862SChao Yu 	union {
228004b6862SChao Yu 		struct {
229004b6862SChao Yu 			block_t lstart;	/* logical start address */
230004b6862SChao Yu 			block_t len;	/* length */
231004b6862SChao Yu 			block_t start;	/* actual start address in dev */
232004b6862SChao Yu 		};
233004b6862SChao Yu 		struct discard_info di;	/* discard info */
234004b6862SChao Yu 
235004b6862SChao Yu 	};
236b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
237b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
238c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
239ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2409a744b92SChao Yu 	unsigned char state;		/* state */
241c81abe34SJaegeuk Kim 	int error;			/* bio error */
242275b66b0SChao Yu };
243275b66b0SChao Yu 
2440b54fb84SJaegeuk Kim struct discard_cmd_control {
24515469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
24646f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
247ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
248969d1b18SChao Yu 	unsigned char pend_list_tag[MAX_PLIST_NUM];/* tag for pending entries */
24946f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
25015469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
251969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
25215469963SJaegeuk Kim 	struct mutex cmd_lock;
253d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
254d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
255969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
256d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
2578b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
2588b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
2595f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
260004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
26139a53e0cSJaegeuk Kim };
26239a53e0cSJaegeuk Kim 
26339a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
26439a53e0cSJaegeuk Kim struct fsync_inode_entry {
26539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
26639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
267c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
268c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
26939a53e0cSJaegeuk Kim };
27039a53e0cSJaegeuk Kim 
27168afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
27268afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
27339a53e0cSJaegeuk Kim 
27468afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
27568afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
27668afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
27768afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
27839a53e0cSJaegeuk Kim 
279dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
280dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
281309cc2b6SJaegeuk Kim 
282dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
28339a53e0cSJaegeuk Kim {
284dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
285cac5a3d8SDongOh Shin 
286dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
28739a53e0cSJaegeuk Kim 	return before;
28839a53e0cSJaegeuk Kim }
28939a53e0cSJaegeuk Kim 
290dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
29139a53e0cSJaegeuk Kim {
292dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
293cac5a3d8SDongOh Shin 
294dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
29539a53e0cSJaegeuk Kim 	return before;
29639a53e0cSJaegeuk Kim }
29739a53e0cSJaegeuk Kim 
298dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
299dfc08a12SChao Yu 							int size, int type)
300184a5cd2SChao Yu {
301184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
302dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
303dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
304184a5cd2SChao Yu }
305184a5cd2SChao Yu 
30639a53e0cSJaegeuk Kim /*
307e9750824SNamjae Jeon  * ioctl commands
308e9750824SNamjae Jeon  */
309e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
310e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
311d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
312e9750824SNamjae Jeon 
31388b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
31488b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
31588b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
31602a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3171e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3181e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
319d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
320456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
321d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
322d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3234dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3244dd6f977SJaegeuk Kim 						struct f2fs_move_range)
325e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
326e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
32734dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
32834dc77adSJaegeuk Kim 						struct f2fs_gc_range)
329e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
33088b88a66SJaegeuk Kim 
3310b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3320b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3330b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
334f424f664SJaegeuk Kim 
3351abff93dSJaegeuk Kim /*
3361abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3371abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3381abff93dSJaegeuk Kim  */
3391abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3401abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3411abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3421abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
343c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3441abff93dSJaegeuk Kim 
345e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
346e9750824SNamjae Jeon /*
347e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
348e9750824SNamjae Jeon  */
349e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
350e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
35104ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
352e9750824SNamjae Jeon #endif
353e9750824SNamjae Jeon 
3542c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
3552c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
3562c1d0305SChao Yu 
35734dc77adSJaegeuk Kim struct f2fs_gc_range {
35834dc77adSJaegeuk Kim 	u32 sync;
35934dc77adSJaegeuk Kim 	u64 start;
36034dc77adSJaegeuk Kim 	u64 len;
36134dc77adSJaegeuk Kim };
36234dc77adSJaegeuk Kim 
363d323d005SChao Yu struct f2fs_defragment {
364d323d005SChao Yu 	u64 start;
365d323d005SChao Yu 	u64 len;
366d323d005SChao Yu };
367d323d005SChao Yu 
3684dd6f977SJaegeuk Kim struct f2fs_move_range {
3694dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
3704dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
3714dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
3724dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
3734dd6f977SJaegeuk Kim };
3744dd6f977SJaegeuk Kim 
375e066b83cSJaegeuk Kim struct f2fs_flush_device {
376e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
377e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
378e066b83cSJaegeuk Kim };
379e066b83cSJaegeuk Kim 
380f2470371SChao Yu /* for inline stuff */
381f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
3827a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
3837a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) * \
3847a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) - \
3857a2af766SChao Yu 				DEF_INLINE_RESERVED_SIZE - \
386f2470371SChao Yu 				F2FS_INLINE_XATTR_ADDRS))
387f2470371SChao Yu 
388f2470371SChao Yu /* for inline dir */
389f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
390f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
391f2470371SChao Yu 				BITS_PER_BYTE + 1))
392f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
393f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
394f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
395f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
396f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
397f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
398f2470371SChao Yu 
399e9750824SNamjae Jeon /*
40039a53e0cSJaegeuk Kim  * For INODE and NODE manager
40139a53e0cSJaegeuk Kim  */
4027b3cd7d6SJaegeuk Kim /* for directory operations */
4037b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
404d8c6822aSJaegeuk Kim 	struct inode *inode;
40576a9dd85SChao Yu 	void *bitmap;
4067b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4077b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4087b3cd7d6SJaegeuk Kim 	int max;
40976a9dd85SChao Yu 	int nr_bitmap;
4107b3cd7d6SJaegeuk Kim };
4117b3cd7d6SJaegeuk Kim 
41264c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
41364c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4147b3cd7d6SJaegeuk Kim {
415d8c6822aSJaegeuk Kim 	d->inode = inode;
4167b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
41776a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
4187b3cd7d6SJaegeuk Kim 	d->bitmap = &t->dentry_bitmap;
4197b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4207b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
42164c24ecbSTomohiro Kusumi }
422cac5a3d8SDongOh Shin 
42364c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
424f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
42564c24ecbSTomohiro Kusumi {
426f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
427f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
428f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
429f2470371SChao Yu 
43064c24ecbSTomohiro Kusumi 	d->inode = inode;
431f2470371SChao Yu 	d->max = entry_cnt;
432f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
433f2470371SChao Yu 	d->bitmap = t;
434f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
435f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
436f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
4377b3cd7d6SJaegeuk Kim }
4387b3cd7d6SJaegeuk Kim 
439dbe6a5ffSJaegeuk Kim /*
440dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
441dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
442dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
44339a53e0cSJaegeuk Kim  */
444dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
445dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
446266e97a8SJaegeuk Kim enum {
447266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
448266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
449266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
450266e97a8SJaegeuk Kim 					 * look up a node with readahead called
4514f4124d0SChao Yu 					 * by get_data_block.
45239a53e0cSJaegeuk Kim 					 */
453266e97a8SJaegeuk Kim };
454266e97a8SJaegeuk Kim 
455a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
45639a53e0cSJaegeuk Kim 
457817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
458817202d9SChao Yu 
45913054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
46013054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
46113054c54SChao Yu 
46239a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
46313054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
46413054c54SChao Yu 
46513054c54SChao Yu /* number of extent info in extent cache we try to shrink */
46613054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
467c11abd1aSJaegeuk Kim 
46854c2258cSChao Yu struct rb_entry {
46954c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
47054c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
47154c2258cSChao Yu 	unsigned int len;		/* length of the entry */
47254c2258cSChao Yu };
47354c2258cSChao Yu 
47439a53e0cSJaegeuk Kim struct extent_info {
47539a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
476111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
47754c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
47839a53e0cSJaegeuk Kim };
47939a53e0cSJaegeuk Kim 
48013054c54SChao Yu struct extent_node {
48154c2258cSChao Yu 	struct rb_node rb_node;
48254c2258cSChao Yu 	union {
48354c2258cSChao Yu 		struct {
48454c2258cSChao Yu 			unsigned int fofs;
48554c2258cSChao Yu 			unsigned int len;
48654c2258cSChao Yu 			u32 blk;
48754c2258cSChao Yu 		};
48813054c54SChao Yu 		struct extent_info ei;	/* extent info */
48954c2258cSChao Yu 
49054c2258cSChao Yu 	};
49154c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
492201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
49313054c54SChao Yu };
49413054c54SChao Yu 
49513054c54SChao Yu struct extent_tree {
49613054c54SChao Yu 	nid_t ino;			/* inode number */
49713054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
49862c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
4993e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
500137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
50113054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
50268e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
50313054c54SChao Yu };
50413054c54SChao Yu 
50539a53e0cSJaegeuk Kim /*
506003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
507003a3e1dSJaegeuk Kim  *
508003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
509003a3e1dSJaegeuk Kim  */
510003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
511003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5127f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5137f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5147f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
515003a3e1dSJaegeuk Kim 
516003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
517003a3e1dSJaegeuk Kim 	block_t m_pblk;
518003a3e1dSJaegeuk Kim 	block_t m_lblk;
519003a3e1dSJaegeuk Kim 	unsigned int m_len;
520003a3e1dSJaegeuk Kim 	unsigned int m_flags;
521da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
522003a3e1dSJaegeuk Kim };
523003a3e1dSJaegeuk Kim 
524e2b4e2bcSChao Yu /* for flag in get_data_block */
525f2220c7fSQiuyang Sun enum {
526f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
527f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
528f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
529f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
530f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
531f2220c7fSQiuyang Sun };
532e2b4e2bcSChao Yu 
533003a3e1dSJaegeuk Kim /*
53439a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
53539a53e0cSJaegeuk Kim  */
53639a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
537354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
538cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
539e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
54026787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
54139a53e0cSJaegeuk Kim 
542b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
543b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
544b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
545b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
546b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
547b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
548cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
549cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
550cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
551e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
552e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
55326787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
55426787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
555cde4de12SJaegeuk Kim 
556ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
557ab9fa662SJaegeuk Kim 
55839a53e0cSJaegeuk Kim struct f2fs_inode_info {
55939a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
56039a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
56139a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
56238431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
56339a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
5646666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
56539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
56639a53e0cSJaegeuk Kim 
56739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
56839a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
569d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
570204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
57139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
57239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
57388c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
574b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
57539a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
57626de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
57788b88a66SJaegeuk Kim 
5780abd675eSChao Yu #ifdef CONFIG_QUOTA
5790abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
5800abd675eSChao Yu 
5810abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
5820abd675eSChao Yu 	qsize_t i_reserved_quota;
5830abd675eSChao Yu #endif
5840f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
5850f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
58688b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
5877a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
58888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
5893e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
59082e0a5aaSChao Yu 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
5915a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
59227161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
593f2470371SChao Yu 
5947a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
5955c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
59639a53e0cSJaegeuk Kim };
59739a53e0cSJaegeuk Kim 
59839a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
599bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
60039a53e0cSJaegeuk Kim {
601bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
602bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
603bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
60439a53e0cSJaegeuk Kim }
60539a53e0cSJaegeuk Kim 
60639a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
60739a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
60839a53e0cSJaegeuk Kim {
60939a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6104d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
61139a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
61239a53e0cSJaegeuk Kim }
61339a53e0cSJaegeuk Kim 
614429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
615429511cdSChao Yu 						u32 blk, unsigned int len)
616429511cdSChao Yu {
617429511cdSChao Yu 	ei->fofs = fofs;
618429511cdSChao Yu 	ei->blk = blk;
619429511cdSChao Yu 	ei->len = len;
620429511cdSChao Yu }
621429511cdSChao Yu 
622004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
623004b6862SChao Yu 						struct discard_info *front)
624004b6862SChao Yu {
625004b6862SChao Yu 	return back->lstart + back->len == front->lstart;
626004b6862SChao Yu }
627004b6862SChao Yu 
628004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
629004b6862SChao Yu 						struct discard_info *back)
630004b6862SChao Yu {
631004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
632004b6862SChao Yu }
633004b6862SChao Yu 
634004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
635004b6862SChao Yu 						struct discard_info *front)
636004b6862SChao Yu {
637004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
638004b6862SChao Yu }
639004b6862SChao Yu 
640429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
641429511cdSChao Yu 						struct extent_info *front)
642429511cdSChao Yu {
643429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
644429511cdSChao Yu 			back->blk + back->len == front->blk);
645429511cdSChao Yu }
646429511cdSChao Yu 
647429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
648429511cdSChao Yu 						struct extent_info *back)
649429511cdSChao Yu {
650429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
651429511cdSChao Yu }
652429511cdSChao Yu 
653429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
654429511cdSChao Yu 						struct extent_info *front)
655429511cdSChao Yu {
656429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
657429511cdSChao Yu }
658429511cdSChao Yu 
659cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
660205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
661205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
6624abd3f5aSChao Yu {
663205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
6644abd3f5aSChao Yu 		et->largest = en->ei;
6657c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
666205b9822SJaegeuk Kim 	}
6674abd3f5aSChao Yu }
6684abd3f5aSChao Yu 
669*9a4ffdf5SChao Yu /*
670*9a4ffdf5SChao Yu  * For free nid management
671*9a4ffdf5SChao Yu  */
672*9a4ffdf5SChao Yu enum nid_state {
673*9a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
674*9a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
675*9a4ffdf5SChao Yu 	MAX_NID_STATE,
676b8559dc2SChao Yu };
677b8559dc2SChao Yu 
67839a53e0cSJaegeuk Kim struct f2fs_nm_info {
67939a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
68039a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
68104d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
68239a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
683cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
684ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
6852304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
68639a53e0cSJaegeuk Kim 
68739a53e0cSJaegeuk Kim 	/* NAT cache management */
68839a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
689309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
690b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
69139a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
692309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
693aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
69422ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
69539a53e0cSJaegeuk Kim 
69639a53e0cSJaegeuk Kim 	/* free node ids management */
6978a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
698*9a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
699*9a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
700b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
70139a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
7024ac91242SChao Yu 	unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
7034ac91242SChao Yu 	unsigned char *nat_block_bitmap;
704586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
70539a53e0cSJaegeuk Kim 
70639a53e0cSJaegeuk Kim 	/* for checkpoint */
70739a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
70822ad0b6aSJaegeuk Kim 
70922ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
71022ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
71122ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
71222ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
713599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
714599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
715599a09b2SChao Yu #endif
71639a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
71739a53e0cSJaegeuk Kim };
71839a53e0cSJaegeuk Kim 
71939a53e0cSJaegeuk Kim /*
72039a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
72139a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
72239a53e0cSJaegeuk Kim  * by the data offset in a file.
72339a53e0cSJaegeuk Kim  */
72439a53e0cSJaegeuk Kim struct dnode_of_data {
72539a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
72639a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
72739a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
72839a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
72939a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
73039a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
73193bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
7323cf45747SChao Yu 	char cur_level;			/* level of hole node page */
7333cf45747SChao Yu 	char max_level;			/* level of current page located */
73439a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
73539a53e0cSJaegeuk Kim };
73639a53e0cSJaegeuk Kim 
73739a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
73839a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
73939a53e0cSJaegeuk Kim {
740d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
74139a53e0cSJaegeuk Kim 	dn->inode = inode;
74239a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
74339a53e0cSJaegeuk Kim 	dn->node_page = npage;
74439a53e0cSJaegeuk Kim 	dn->nid = nid;
74539a53e0cSJaegeuk Kim }
74639a53e0cSJaegeuk Kim 
74739a53e0cSJaegeuk Kim /*
74839a53e0cSJaegeuk Kim  * For SIT manager
74939a53e0cSJaegeuk Kim  *
75039a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
75139a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
75239a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
75339a53e0cSJaegeuk Kim  * respectively.
75439a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
75539a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
75639a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
75739a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
75839a53e0cSJaegeuk Kim  * data and 8 for node logs.
75939a53e0cSJaegeuk Kim  */
76039a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
76139a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
76239a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
76339a53e0cSJaegeuk Kim 
76439a53e0cSJaegeuk Kim enum {
76539a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
76639a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
76739a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
76839a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
76939a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
77039a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
77138aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
77239a53e0cSJaegeuk Kim };
77339a53e0cSJaegeuk Kim 
7746b4afdd7SJaegeuk Kim struct flush_cmd {
7756b4afdd7SJaegeuk Kim 	struct completion wait;
776721bd4d5SGu Zheng 	struct llist_node llnode;
7776b4afdd7SJaegeuk Kim 	int ret;
7786b4afdd7SJaegeuk Kim };
7796b4afdd7SJaegeuk Kim 
780a688b9d9SGu Zheng struct flush_cmd_control {
781a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
782a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
7838b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
7848b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
785721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
786721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
787a688b9d9SGu Zheng };
788a688b9d9SGu Zheng 
78939a53e0cSJaegeuk Kim struct f2fs_sm_info {
79039a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
79139a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
79239a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
79339a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
79439a53e0cSJaegeuk Kim 
79539a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
79639a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
79739a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
79839a53e0cSJaegeuk Kim 
79939a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
80039a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
80139a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
80239a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
80381eb8d6eSJaegeuk Kim 
80481eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
80581eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8067fd9e544SJaegeuk Kim 
807bba681cbSJaegeuk Kim 	/* for batched trimming */
808bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
809bba681cbSJaegeuk Kim 
810184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
811184a5cd2SChao Yu 
812216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
813216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
814c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
815ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
8166b4afdd7SJaegeuk Kim 
8176b4afdd7SJaegeuk Kim 	/* for flush command control */
818b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
819a688b9d9SGu Zheng 
8200b54fb84SJaegeuk Kim 	/* for discard command control */
8210b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
82239a53e0cSJaegeuk Kim };
82339a53e0cSJaegeuk Kim 
82439a53e0cSJaegeuk Kim /*
82539a53e0cSJaegeuk Kim  * For superblock
82639a53e0cSJaegeuk Kim  */
82739a53e0cSJaegeuk Kim /*
82839a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
82939a53e0cSJaegeuk Kim  *
83039a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
83139a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
83239a53e0cSJaegeuk Kim  */
83336951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
83439a53e0cSJaegeuk Kim enum count_type {
83539a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
836c227f912SChao Yu 	F2FS_DIRTY_DATA,
83739a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
83839a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
8398dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
8400f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
84136951b38SChao Yu 	F2FS_WB_CP_DATA,
84236951b38SChao Yu 	F2FS_WB_DATA,
84339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
84439a53e0cSJaegeuk Kim };
84539a53e0cSJaegeuk Kim 
84639a53e0cSJaegeuk Kim /*
847e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
84839a53e0cSJaegeuk Kim  * The available types are:
84939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
85039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
85139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
85239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
85339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
85439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
85539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
85639a53e0cSJaegeuk Kim  */
8577d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
85839a53e0cSJaegeuk Kim enum page_type {
85939a53e0cSJaegeuk Kim 	DATA,
86039a53e0cSJaegeuk Kim 	NODE,
86139a53e0cSJaegeuk Kim 	META,
86239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
86339a53e0cSJaegeuk Kim 	META_FLUSH,
8648ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
8658ce67cb0SJaegeuk Kim 	INMEM_DROP,
8668c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
86728bc106bSChao Yu 	INMEM_REVOKE,
8688ce67cb0SJaegeuk Kim 	IPU,
8698ce67cb0SJaegeuk Kim 	OPU,
87039a53e0cSJaegeuk Kim };
87139a53e0cSJaegeuk Kim 
872a912b54dSJaegeuk Kim enum temp_type {
873a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
874a912b54dSJaegeuk Kim 	WARM,
875a912b54dSJaegeuk Kim 	COLD,
876a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
877a912b54dSJaegeuk Kim };
878a912b54dSJaegeuk Kim 
879cc15620bSJaegeuk Kim enum need_lock_type {
880cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
881cc15620bSJaegeuk Kim 	LOCK_DONE,
882cc15620bSJaegeuk Kim 	LOCK_RETRY,
883cc15620bSJaegeuk Kim };
884cc15620bSJaegeuk Kim 
885b0af6d49SChao Yu enum iostat_type {
886b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
887b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
888b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
889b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
890b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
891b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
892b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
893b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
894b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
895b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
896b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
897b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
898b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
899b0af6d49SChao Yu 	NR_IO_TYPE,
900b0af6d49SChao Yu };
901b0af6d49SChao Yu 
902458e6197SJaegeuk Kim struct f2fs_io_info {
90305ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
904458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
905a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
90604d328deSMike Christie 	int op;			/* contains REQ_OP_ */
907ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
9087a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
90928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
91005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
9114375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
912fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
913d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
914cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
915fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
916b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
917458e6197SJaegeuk Kim };
918458e6197SJaegeuk Kim 
91968afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
9201ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
921458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
9221ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
9231ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
924458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
925df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
926fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
927fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
9281ff7bd3bSJaegeuk Kim };
9291ff7bd3bSJaegeuk Kim 
9303c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
9313c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
9323c62be17SJaegeuk Kim struct f2fs_dev_info {
9333c62be17SJaegeuk Kim 	struct block_device *bdev;
9343c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
9353c62be17SJaegeuk Kim 	unsigned int total_segments;
9363c62be17SJaegeuk Kim 	block_t start_blk;
9373c62be17SJaegeuk Kim 	block_t end_blk;
9383c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
9393c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
9403c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
9413c62be17SJaegeuk Kim #endif
9423c62be17SJaegeuk Kim };
9433c62be17SJaegeuk Kim 
944c227f912SChao Yu enum inode_type {
945c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
946c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
9470f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
948c227f912SChao Yu 	NR_INODE_TYPE,
949c227f912SChao Yu };
950c227f912SChao Yu 
95167298804SChao Yu /* for inner inode cache management */
95267298804SChao Yu struct inode_management {
95367298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
95467298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
95567298804SChao Yu 	struct list_head ino_list;		/* inode list head */
95667298804SChao Yu 	unsigned long ino_num;			/* number of entries */
95767298804SChao Yu };
95867298804SChao Yu 
959caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
960caf0047eSChao Yu enum {
961caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
962caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
963caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
964caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
965df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
966bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
967caf0047eSChao Yu };
968caf0047eSChao Yu 
9696beceb54SJaegeuk Kim enum {
9706beceb54SJaegeuk Kim 	CP_TIME,
971d0239e1bSJaegeuk Kim 	REQ_TIME,
9726beceb54SJaegeuk Kim 	MAX_TIME,
9736beceb54SJaegeuk Kim };
9746beceb54SJaegeuk Kim 
97539a53e0cSJaegeuk Kim struct f2fs_sb_info {
97639a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
9775e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
97839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
979e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
980fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
98139a53e0cSJaegeuk Kim 
982178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
983178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
984178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
985178053e2SDamien Le Moal #endif
986178053e2SDamien Le Moal 
98739a53e0cSJaegeuk Kim 	/* for node-related operations */
98839a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
98939a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
99039a53e0cSJaegeuk Kim 
99139a53e0cSJaegeuk Kim 	/* for segment-related operations */
99239a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
9931ff7bd3bSJaegeuk Kim 
9941ff7bd3bSJaegeuk Kim 	/* for bio operations */
995a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
996e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
997e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
9980a595ebaSJaegeuk Kim 	int write_io_size_bits;			/* Write IO size bits */
9990a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
100039a53e0cSJaegeuk Kim 
100139a53e0cSJaegeuk Kim 	/* for checkpoint */
100239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
10038508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1004aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
100539a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
100639936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1007b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1008b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
100959c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1010fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
10116beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
10126beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
101339a53e0cSJaegeuk Kim 
101467298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
10156451e041SJaegeuk Kim 
10166451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
10170d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
101839a53e0cSJaegeuk Kim 
1019c227f912SChao Yu 	/* for inode management */
1020c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1021c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
102239a53e0cSJaegeuk Kim 
102313054c54SChao Yu 	/* for extent tree cache */
102413054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
10255e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
102613054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
102713054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
10287441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1029137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
103074fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
103113054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
103213054c54SChao Yu 
103339a53e0cSJaegeuk Kim 	/* basic filesystem units */
103439a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
103539a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
103639a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
103739a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
103839a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
103939a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
104039a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
104139a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
104239a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
104339a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
104439a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
104539a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
104639a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1047e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
104839a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
1049ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
105039a53e0cSJaegeuk Kim 
105139a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
105239a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1053a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
105439a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1055daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
1056daeb433eSChao Yu 
105739a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1058523be8a6SJaegeuk Kim 
1059523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
106035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
106141382ec4SJaegeuk Kim 	/* # of allocated blocks */
106241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
106339a53e0cSJaegeuk Kim 
1064687de7f1SJaegeuk Kim 	/* writeback control */
1065687de7f1SJaegeuk Kim 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1066687de7f1SJaegeuk Kim 
1067513c5f37SJaegeuk Kim 	/* valid inode count */
1068513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1069513c5f37SJaegeuk Kim 
107039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
107139a53e0cSJaegeuk Kim 
107239a53e0cSJaegeuk Kim 	/* for cleaning operations */
107339a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
107439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
10755ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
107639a53e0cSJaegeuk Kim 
1077e93b9865SHou Pengyang 	/* threshold for converting bg victims for fg */
1078e93b9865SHou Pengyang 	u64 fggc_threshold;
1079e93b9865SHou Pengyang 
1080b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1081b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1082b1c57c1cSJaegeuk Kim 
108339a53e0cSJaegeuk Kim 	/*
108439a53e0cSJaegeuk Kim 	 * for stat information.
108539a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
108639a53e0cSJaegeuk Kim 	 */
108735b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
108839a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
108939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
109039a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1091b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
10925b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
10935b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
10945b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
10955b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1096d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
109703e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
109803e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
109926a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1100648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
110126a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1102648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
110339a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
110433fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
110535b09d82SNamjae Jeon #endif
110639a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1107b59d0baeSNamjae Jeon 
1108b0af6d49SChao Yu 	/* For app/fs IO statistics */
1109b0af6d49SChao Yu 	spinlock_t iostat_lock;
1110b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1111b0af6d49SChao Yu 	bool iostat_enable;
1112b0af6d49SChao Yu 
1113b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1114b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1115b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
11162658e50dSJaegeuk Kim 
11172658e50dSJaegeuk Kim 	/* For shrinker support */
11182658e50dSJaegeuk Kim 	struct list_head s_list;
11193c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
11203c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
11212658e50dSJaegeuk Kim 	struct mutex umount_mutex;
11222658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
11238f1dbbbbSShuoran Liu 
11248f1dbbbbSShuoran Liu 	/* For write statistics */
11258f1dbbbbSShuoran Liu 	u64 sectors_written_start;
11268f1dbbbbSShuoran Liu 	u64 kbytes_written;
112743b6573bSKeith Mok 
112843b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
112943b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
11301ecc0c5cSChao Yu 
1131704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1132704956ecSChao Yu 	__u32 s_chksum_seed;
1133704956ecSChao Yu 
11341ecc0c5cSChao Yu 	/* For fault injection */
11351ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
11361ecc0c5cSChao Yu 	struct f2fs_fault_info fault_info;
11371ecc0c5cSChao Yu #endif
11384b2414d0SChao Yu 
11394b2414d0SChao Yu #ifdef CONFIG_QUOTA
11404b2414d0SChao Yu 	/* Names of quota files with journalled quota */
11414b2414d0SChao Yu 	char *s_qf_names[MAXQUOTAS];
11424b2414d0SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
11434b2414d0SChao Yu #endif
114439a53e0cSJaegeuk Kim };
114539a53e0cSJaegeuk Kim 
11461ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
114755523519SChao Yu #define f2fs_show_injection_info(type)				\
114855523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
114955523519SChao Yu 		KERN_INFO, fault_name[type],			\
115055523519SChao Yu 		__func__, __builtin_return_address(0))
11511ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
11521ecc0c5cSChao Yu {
11531ecc0c5cSChao Yu 	struct f2fs_fault_info *ffi = &sbi->fault_info;
11541ecc0c5cSChao Yu 
11551ecc0c5cSChao Yu 	if (!ffi->inject_rate)
11561ecc0c5cSChao Yu 		return false;
11571ecc0c5cSChao Yu 
11581ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
11591ecc0c5cSChao Yu 		return false;
11601ecc0c5cSChao Yu 
11611ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
11621ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
11631ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
11641ecc0c5cSChao Yu 		return true;
11651ecc0c5cSChao Yu 	}
11661ecc0c5cSChao Yu 	return false;
11671ecc0c5cSChao Yu }
11681ecc0c5cSChao Yu #endif
11691ecc0c5cSChao Yu 
11708f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
11718f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
11728f1dbbbbSShuoran Liu  */
11738f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
117468afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
117568afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
11768f1dbbbbSShuoran Liu 
11776beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
11786beceb54SJaegeuk Kim {
11796beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
11806beceb54SJaegeuk Kim }
11816beceb54SJaegeuk Kim 
11826beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
11836beceb54SJaegeuk Kim {
11846beceb54SJaegeuk Kim 	struct timespec ts = {sbi->interval_time[type], 0};
11856beceb54SJaegeuk Kim 	unsigned long interval = timespec_to_jiffies(&ts);
11866beceb54SJaegeuk Kim 
11876beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
11886beceb54SJaegeuk Kim }
11896beceb54SJaegeuk Kim 
1190d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1191d0239e1bSJaegeuk Kim {
1192d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1193d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1194d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1195d0239e1bSJaegeuk Kim 
1196d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1197d0239e1bSJaegeuk Kim 		return 0;
1198d0239e1bSJaegeuk Kim 
1199d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1200d0239e1bSJaegeuk Kim }
1201d0239e1bSJaegeuk Kim 
120239a53e0cSJaegeuk Kim /*
120339a53e0cSJaegeuk Kim  * Inline functions
120439a53e0cSJaegeuk Kim  */
120543b6573bSKeith Mok static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
120643b6573bSKeith Mok 			   unsigned int length)
120743b6573bSKeith Mok {
120843b6573bSKeith Mok 	SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
120943b6573bSKeith Mok 	u32 *ctx = (u32 *)shash_desc_ctx(shash);
1210d41519a6SDavid Miller 	u32 retval;
121143b6573bSKeith Mok 	int err;
121243b6573bSKeith Mok 
121343b6573bSKeith Mok 	shash->tfm = sbi->s_chksum_driver;
121443b6573bSKeith Mok 	shash->flags = 0;
121543b6573bSKeith Mok 	*ctx = F2FS_SUPER_MAGIC;
121643b6573bSKeith Mok 
121743b6573bSKeith Mok 	err = crypto_shash_update(shash, address, length);
121843b6573bSKeith Mok 	BUG_ON(err);
121943b6573bSKeith Mok 
1220d41519a6SDavid Miller 	retval = *ctx;
1221d41519a6SDavid Miller 	barrier_data(ctx);
1222d41519a6SDavid Miller 	return retval;
122343b6573bSKeith Mok }
122443b6573bSKeith Mok 
122543b6573bSKeith Mok static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
122643b6573bSKeith Mok 				  void *buf, size_t buf_size)
122743b6573bSKeith Mok {
122843b6573bSKeith Mok 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
122943b6573bSKeith Mok }
123043b6573bSKeith Mok 
1231704956ecSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1232704956ecSChao Yu 			      const void *address, unsigned int length)
1233704956ecSChao Yu {
1234704956ecSChao Yu 	struct {
1235704956ecSChao Yu 		struct shash_desc shash;
1236704956ecSChao Yu 		char ctx[4];
1237704956ecSChao Yu 	} desc;
1238704956ecSChao Yu 	int err;
1239704956ecSChao Yu 
1240704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1241704956ecSChao Yu 
1242704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1243704956ecSChao Yu 	desc.shash.flags = 0;
1244704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1245704956ecSChao Yu 
1246704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1247704956ecSChao Yu 	BUG_ON(err);
1248704956ecSChao Yu 
1249704956ecSChao Yu 	return *(u32 *)desc.ctx;
1250704956ecSChao Yu }
1251704956ecSChao Yu 
125239a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
125339a53e0cSJaegeuk Kim {
125439a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
125539a53e0cSJaegeuk Kim }
125639a53e0cSJaegeuk Kim 
125739a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
125839a53e0cSJaegeuk Kim {
125939a53e0cSJaegeuk Kim 	return sb->s_fs_info;
126039a53e0cSJaegeuk Kim }
126139a53e0cSJaegeuk Kim 
12624081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
12634081363fSJaegeuk Kim {
12644081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
12654081363fSJaegeuk Kim }
12664081363fSJaegeuk Kim 
12674081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
12684081363fSJaegeuk Kim {
12694081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
12704081363fSJaegeuk Kim }
12714081363fSJaegeuk Kim 
12724081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
12734081363fSJaegeuk Kim {
12744081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
12754081363fSJaegeuk Kim }
12764081363fSJaegeuk Kim 
127739a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
127839a53e0cSJaegeuk Kim {
127939a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
128039a53e0cSJaegeuk Kim }
128139a53e0cSJaegeuk Kim 
128239a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
128339a53e0cSJaegeuk Kim {
128439a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
128539a53e0cSJaegeuk Kim }
128639a53e0cSJaegeuk Kim 
128745590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
128845590710SGu Zheng {
128945590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
129045590710SGu Zheng }
129145590710SGu Zheng 
129258bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
129358bfaf44SJaegeuk Kim {
129458bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
129558bfaf44SJaegeuk Kim }
129658bfaf44SJaegeuk Kim 
129739a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
129839a53e0cSJaegeuk Kim {
129939a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
130039a53e0cSJaegeuk Kim }
130139a53e0cSJaegeuk Kim 
130239a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
130339a53e0cSJaegeuk Kim {
130439a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
130539a53e0cSJaegeuk Kim }
130639a53e0cSJaegeuk Kim 
130739a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
130839a53e0cSJaegeuk Kim {
130939a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
131039a53e0cSJaegeuk Kim }
131139a53e0cSJaegeuk Kim 
131239a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
131339a53e0cSJaegeuk Kim {
131439a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
131539a53e0cSJaegeuk Kim }
131639a53e0cSJaegeuk Kim 
131739a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
131839a53e0cSJaegeuk Kim {
131939a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
132039a53e0cSJaegeuk Kim }
132139a53e0cSJaegeuk Kim 
13229df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
13239df27d98SGu Zheng {
13249df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
13259df27d98SGu Zheng }
13269df27d98SGu Zheng 
13274ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
13284ef51a8fSJaegeuk Kim {
13294ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
13304ef51a8fSJaegeuk Kim }
13314ef51a8fSJaegeuk Kim 
1332caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
133339a53e0cSJaegeuk Kim {
1334fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
133539a53e0cSJaegeuk Kim }
133639a53e0cSJaegeuk Kim 
1337caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
133839a53e0cSJaegeuk Kim {
1339fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1340caf0047eSChao Yu }
1341caf0047eSChao Yu 
1342caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1343caf0047eSChao Yu {
1344fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
134539a53e0cSJaegeuk Kim }
134639a53e0cSJaegeuk Kim 
1347d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1348d71b5564SJaegeuk Kim {
1349d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1350d71b5564SJaegeuk Kim }
1351d71b5564SJaegeuk Kim 
1352ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1353ced2c7eaSKinglong Mee {
1354ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1355ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1356ced2c7eaSKinglong Mee }
1357ced2c7eaSKinglong Mee 
1358aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
135925ca923bSJaegeuk Kim {
136025ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1361aaec2b1dSChao Yu 
136225ca923bSJaegeuk Kim 	return ckpt_flags & f;
136325ca923bSJaegeuk Kim }
136425ca923bSJaegeuk Kim 
1365aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
136625ca923bSJaegeuk Kim {
1367aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1368aaec2b1dSChao Yu }
1369aaec2b1dSChao Yu 
1370aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1371aaec2b1dSChao Yu {
1372aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1373aaec2b1dSChao Yu 
1374aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
137525ca923bSJaegeuk Kim 	ckpt_flags |= f;
137625ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
137725ca923bSJaegeuk Kim }
137825ca923bSJaegeuk Kim 
1379aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
138025ca923bSJaegeuk Kim {
1381d1aa2453SChao Yu 	unsigned long flags;
1382d1aa2453SChao Yu 
1383d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1384aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1385d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1386aaec2b1dSChao Yu }
1387aaec2b1dSChao Yu 
1388aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1389aaec2b1dSChao Yu {
1390aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1391aaec2b1dSChao Yu 
1392aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
139325ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
139425ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
139525ca923bSJaegeuk Kim }
139625ca923bSJaegeuk Kim 
1397aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1398aaec2b1dSChao Yu {
1399d1aa2453SChao Yu 	unsigned long flags;
1400d1aa2453SChao Yu 
1401d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1402aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1403d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1404aaec2b1dSChao Yu }
1405aaec2b1dSChao Yu 
140622ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
140722ad0b6aSJaegeuk Kim {
1408d1aa2453SChao Yu 	unsigned long flags;
1409d1aa2453SChao Yu 
141022ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
141122ad0b6aSJaegeuk Kim 
141222ad0b6aSJaegeuk Kim 	if (lock)
1413d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
141422ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
141522ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
141622ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
141722ad0b6aSJaegeuk Kim 	if (lock)
1418d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
141922ad0b6aSJaegeuk Kim }
142022ad0b6aSJaegeuk Kim 
142122ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
142222ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
142322ad0b6aSJaegeuk Kim {
142422ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
142522ad0b6aSJaegeuk Kim 
1426c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
142722ad0b6aSJaegeuk Kim }
142822ad0b6aSJaegeuk Kim 
1429e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
143039a53e0cSJaegeuk Kim {
1431b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
143239a53e0cSJaegeuk Kim }
143339a53e0cSJaegeuk Kim 
1434cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1435cc15620bSJaegeuk Kim {
1436cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1437cc15620bSJaegeuk Kim }
1438cc15620bSJaegeuk Kim 
1439e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
144039a53e0cSJaegeuk Kim {
1441b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
144239936837SJaegeuk Kim }
144339936837SJaegeuk Kim 
1444e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
144539936837SJaegeuk Kim {
1446b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
144739936837SJaegeuk Kim }
144839936837SJaegeuk Kim 
1449e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
145039936837SJaegeuk Kim {
1451b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
145239a53e0cSJaegeuk Kim }
145339a53e0cSJaegeuk Kim 
1454119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1455119ee914SJaegeuk Kim {
1456119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1457119ee914SJaegeuk Kim 
1458119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1459119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1460119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1461119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1462119ee914SJaegeuk Kim 	return reason;
1463119ee914SJaegeuk Kim }
1464119ee914SJaegeuk Kim 
1465119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1466119ee914SJaegeuk Kim {
1467c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1468119ee914SJaegeuk Kim }
1469119ee914SJaegeuk Kim 
1470119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1471119ee914SJaegeuk Kim {
1472aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1473aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1474119ee914SJaegeuk Kim }
1475119ee914SJaegeuk Kim 
147639a53e0cSJaegeuk Kim /*
147739a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
147839a53e0cSJaegeuk Kim  */
1479064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
148039a53e0cSJaegeuk Kim {
1481d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1482d6b7d4b3SChao Yu 		return -EINVAL;
1483cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1484064e0823SNamjae Jeon 		return -EINVAL;
1485064e0823SNamjae Jeon 	return 0;
148639a53e0cSJaegeuk Kim }
148739a53e0cSJaegeuk Kim 
148839a53e0cSJaegeuk Kim /*
148939a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
149039a53e0cSJaegeuk Kim  */
149139a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
149239a53e0cSJaegeuk Kim {
14930eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
14940eb0adadSChao Yu 
1495000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
149639a53e0cSJaegeuk Kim }
149739a53e0cSJaegeuk Kim 
14984bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
14994bc8e9bcSChao Yu {
15004bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
15014bc8e9bcSChao Yu }
15024bc8e9bcSChao Yu 
15030abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
15040abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
150546008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
150639a53e0cSJaegeuk Kim {
15070abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1508daeb433eSChao Yu 	block_t avail_user_block_count;
15090abd675eSChao Yu 	int ret;
15100abd675eSChao Yu 
15110abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
15120abd675eSChao Yu 	if (ret)
15130abd675eSChao Yu 		return ret;
1514dd11a5dfSJaegeuk Kim 
1515cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
151655523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
151755523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
15180abd675eSChao Yu 		release = *count;
15190abd675eSChao Yu 		goto enospc;
152055523519SChao Yu 	}
1521cb78942bSJaegeuk Kim #endif
1522dd11a5dfSJaegeuk Kim 	/*
1523dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1524dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1525dd11a5dfSJaegeuk Kim 	 */
1526dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1527cfb271d4SChao Yu 
152839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
15292555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
1530daeb433eSChao Yu 	avail_user_block_count = sbi->user_block_count - sbi->reserved_blocks;
1531daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1532daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
1533dd11a5dfSJaegeuk Kim 		*count -= diff;
15340abd675eSChao Yu 		release = diff;
1535daeb433eSChao Yu 		sbi->total_valid_block_count = avail_user_block_count;
153646008c6dSChao Yu 		if (!*count) {
153739a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1538dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
15390abd675eSChao Yu 			goto enospc;
154039a53e0cSJaegeuk Kim 		}
154146008c6dSChao Yu 	}
154239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
154341382ec4SJaegeuk Kim 
15440abd675eSChao Yu 	if (release)
15450abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
15460abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
15470abd675eSChao Yu 	return 0;
15480abd675eSChao Yu 
15490abd675eSChao Yu enospc:
15500abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
15510abd675eSChao Yu 	return -ENOSPC;
155239a53e0cSJaegeuk Kim }
155339a53e0cSJaegeuk Kim 
1554da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
155539a53e0cSJaegeuk Kim 						struct inode *inode,
15560eb0adadSChao Yu 						block_t count)
155739a53e0cSJaegeuk Kim {
15580eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
15590eb0adadSChao Yu 
156039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
15619850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
15620eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
156339a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
156439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
15650abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
156639a53e0cSJaegeuk Kim }
156739a53e0cSJaegeuk Kim 
156839a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
156939a53e0cSJaegeuk Kim {
157035782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
15717c4abcbeSChao Yu 
157236951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
157336951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
15747c4abcbeSChao Yu 		return;
15757c4abcbeSChao Yu 
1576caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
157739a53e0cSJaegeuk Kim }
157839a53e0cSJaegeuk Kim 
1579a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
158039a53e0cSJaegeuk Kim {
1581204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1582c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1583c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
158439a53e0cSJaegeuk Kim }
158539a53e0cSJaegeuk Kim 
158639a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
158739a53e0cSJaegeuk Kim {
158835782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
158939a53e0cSJaegeuk Kim }
159039a53e0cSJaegeuk Kim 
1591a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
159239a53e0cSJaegeuk Kim {
15935ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
15945ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
15951fe54f9dSJaegeuk Kim 		return;
15961fe54f9dSJaegeuk Kim 
1597204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1598c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1599c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
160039a53e0cSJaegeuk Kim }
160139a53e0cSJaegeuk Kim 
1602523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
160339a53e0cSJaegeuk Kim {
160435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
160539a53e0cSJaegeuk Kim }
160639a53e0cSJaegeuk Kim 
1607204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1608f8b2c1f9SJaegeuk Kim {
1609204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1610f8b2c1f9SJaegeuk Kim }
1611f8b2c1f9SJaegeuk Kim 
16125ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
16135ac206cfSNamjae Jeon {
16143519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1615523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1616523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1617523be8a6SJaegeuk Kim 
1618523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
16195ac206cfSNamjae Jeon }
16205ac206cfSNamjae Jeon 
162139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
162239a53e0cSJaegeuk Kim {
16238b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
162439a53e0cSJaegeuk Kim }
162539a53e0cSJaegeuk Kim 
1626f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1627f83a2584SYunlei He {
1628f83a2584SYunlei He 	return sbi->discard_blks;
1629f83a2584SYunlei He }
1630f83a2584SYunlei He 
163139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
163239a53e0cSJaegeuk Kim {
163339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
163439a53e0cSJaegeuk Kim 
163539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
163639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
163739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
163839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
163939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
164039a53e0cSJaegeuk Kim 
164139a53e0cSJaegeuk Kim 	return 0;
164239a53e0cSJaegeuk Kim }
164339a53e0cSJaegeuk Kim 
164455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
164555141486SWanpeng Li {
164655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
164755141486SWanpeng Li }
164855141486SWanpeng Li 
164939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
165039a53e0cSJaegeuk Kim {
165139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
16521dbe4152SChangman Lee 	int offset;
16531dbe4152SChangman Lee 
165455141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
16551dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
16561dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
16571dbe4152SChangman Lee 		else
165865b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
16591dbe4152SChangman Lee 	} else {
16601dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
166125ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
166239a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
166339a53e0cSJaegeuk Kim 	}
16641dbe4152SChangman Lee }
166539a53e0cSJaegeuk Kim 
166639a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
166739a53e0cSJaegeuk Kim {
16688508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
166939a53e0cSJaegeuk Kim 
16708508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
167139a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
167239a53e0cSJaegeuk Kim 	return start_addr;
167339a53e0cSJaegeuk Kim }
167439a53e0cSJaegeuk Kim 
16758508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
16768508e44aSJaegeuk Kim {
16778508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
16788508e44aSJaegeuk Kim 
16798508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
16808508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
16818508e44aSJaegeuk Kim 	return start_addr;
16828508e44aSJaegeuk Kim }
16838508e44aSJaegeuk Kim 
16848508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
16858508e44aSJaegeuk Kim {
16868508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
16878508e44aSJaegeuk Kim }
16888508e44aSJaegeuk Kim 
168939a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
169039a53e0cSJaegeuk Kim {
169139a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
169239a53e0cSJaegeuk Kim }
169339a53e0cSJaegeuk Kim 
16940abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1695000519f2SChao Yu 					struct inode *inode, bool is_inode)
169639a53e0cSJaegeuk Kim {
169739a53e0cSJaegeuk Kim 	block_t	valid_block_count;
169839a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
16990abd675eSChao Yu 	bool quota = inode && !is_inode;
17000abd675eSChao Yu 
17010abd675eSChao Yu 	if (quota) {
17020abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
17030abd675eSChao Yu 		if (ret)
17040abd675eSChao Yu 			return ret;
17050abd675eSChao Yu 	}
170639a53e0cSJaegeuk Kim 
170739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
170839a53e0cSJaegeuk Kim 
1709ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1710daeb433eSChao Yu 	if (unlikely(valid_block_count + sbi->reserved_blocks >
1711daeb433eSChao Yu 						sbi->user_block_count)) {
171239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
17130abd675eSChao Yu 		goto enospc;
171439a53e0cSJaegeuk Kim 	}
171539a53e0cSJaegeuk Kim 
1716ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1717cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
171839a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
17190abd675eSChao Yu 		goto enospc;
172039a53e0cSJaegeuk Kim 	}
172139a53e0cSJaegeuk Kim 
1722ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1723ef86d709SGu Zheng 	sbi->total_valid_block_count++;
172439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
172539a53e0cSJaegeuk Kim 
17260abd675eSChao Yu 	if (inode) {
17270abd675eSChao Yu 		if (is_inode)
17280abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
17290abd675eSChao Yu 		else
17300abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
17310abd675eSChao Yu 	}
17320abd675eSChao Yu 
173341382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
17340abd675eSChao Yu 	return 0;
17350abd675eSChao Yu 
17360abd675eSChao Yu enospc:
17370abd675eSChao Yu 	if (quota)
17380abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
17390abd675eSChao Yu 	return -ENOSPC;
174039a53e0cSJaegeuk Kim }
174139a53e0cSJaegeuk Kim 
174239a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1743000519f2SChao Yu 					struct inode *inode, bool is_inode)
174439a53e0cSJaegeuk Kim {
174539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
174639a53e0cSJaegeuk Kim 
17479850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
17489850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1749000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
175039a53e0cSJaegeuk Kim 
1751ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1752ef86d709SGu Zheng 	sbi->total_valid_block_count--;
175339a53e0cSJaegeuk Kim 
175439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17550abd675eSChao Yu 
17560abd675eSChao Yu 	if (!is_inode)
17570abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
175839a53e0cSJaegeuk Kim }
175939a53e0cSJaegeuk Kim 
176039a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
176139a53e0cSJaegeuk Kim {
17628b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
176339a53e0cSJaegeuk Kim }
176439a53e0cSJaegeuk Kim 
176539a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
176639a53e0cSJaegeuk Kim {
1767513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
176839a53e0cSJaegeuk Kim }
176939a53e0cSJaegeuk Kim 
17700e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
177139a53e0cSJaegeuk Kim {
1772513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
177339a53e0cSJaegeuk Kim }
177439a53e0cSJaegeuk Kim 
1775513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
177639a53e0cSJaegeuk Kim {
1777513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
177839a53e0cSJaegeuk Kim }
177939a53e0cSJaegeuk Kim 
1780a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1781a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1782a56c7c6fSJaegeuk Kim {
1783c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1784c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1785cac5a3d8SDongOh Shin 
1786c41f3cc3SJaegeuk Kim 	if (page)
1787c41f3cc3SJaegeuk Kim 		return page;
1788c41f3cc3SJaegeuk Kim 
178955523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
179055523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1791c41f3cc3SJaegeuk Kim 		return NULL;
179255523519SChao Yu 	}
1793c41f3cc3SJaegeuk Kim #endif
1794a56c7c6fSJaegeuk Kim 	if (!for_write)
1795a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1796a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1797a56c7c6fSJaegeuk Kim }
1798a56c7c6fSJaegeuk Kim 
17996e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
18006e2c64adSJaegeuk Kim {
18016e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
18026e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
18036e2c64adSJaegeuk Kim 
18046e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
18056e2c64adSJaegeuk Kim 	kunmap(dst);
18066e2c64adSJaegeuk Kim 	kunmap(src);
18076e2c64adSJaegeuk Kim }
18086e2c64adSJaegeuk Kim 
180939a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
181039a53e0cSJaegeuk Kim {
1811031fa8ccSJaegeuk Kim 	if (!page)
181239a53e0cSJaegeuk Kim 		return;
181339a53e0cSJaegeuk Kim 
181439a53e0cSJaegeuk Kim 	if (unlock) {
18159850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
181639a53e0cSJaegeuk Kim 		unlock_page(page);
181739a53e0cSJaegeuk Kim 	}
181809cbfeafSKirill A. Shutemov 	put_page(page);
181939a53e0cSJaegeuk Kim }
182039a53e0cSJaegeuk Kim 
182139a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
182239a53e0cSJaegeuk Kim {
182339a53e0cSJaegeuk Kim 	if (dn->node_page)
182439a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
182539a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
182639a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
182739a53e0cSJaegeuk Kim 	dn->node_page = NULL;
182839a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
182939a53e0cSJaegeuk Kim }
183039a53e0cSJaegeuk Kim 
183139a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1832e8512d2eSGu Zheng 					size_t size)
183339a53e0cSJaegeuk Kim {
1834e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
183539a53e0cSJaegeuk Kim }
183639a53e0cSJaegeuk Kim 
18377bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
18387bd59381SGu Zheng 						gfp_t flags)
18397bd59381SGu Zheng {
18407bd59381SGu Zheng 	void *entry;
18417bd59381SGu Zheng 
184280c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
184380c54505SJaegeuk Kim 	if (!entry)
184480c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
18457bd59381SGu Zheng 	return entry;
18467bd59381SGu Zheng }
18477bd59381SGu Zheng 
1848740432f8SJaegeuk Kim static inline struct bio *f2fs_bio_alloc(int npages)
1849740432f8SJaegeuk Kim {
1850740432f8SJaegeuk Kim 	struct bio *bio;
1851740432f8SJaegeuk Kim 
1852740432f8SJaegeuk Kim 	/* No failure on bio allocation */
1853740432f8SJaegeuk Kim 	bio = bio_alloc(GFP_NOIO, npages);
185480c54505SJaegeuk Kim 	if (!bio)
185580c54505SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1856740432f8SJaegeuk Kim 	return bio;
1857740432f8SJaegeuk Kim }
1858740432f8SJaegeuk Kim 
18599be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
18609be32d72SJaegeuk Kim 				unsigned long index, void *item)
18619be32d72SJaegeuk Kim {
18629be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
18639be32d72SJaegeuk Kim 		cond_resched();
18649be32d72SJaegeuk Kim }
18659be32d72SJaegeuk Kim 
186639a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
186739a53e0cSJaegeuk Kim 
186839a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
186939a53e0cSJaegeuk Kim {
187045590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
1871cac5a3d8SDongOh Shin 
187239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
187339a53e0cSJaegeuk Kim }
187439a53e0cSJaegeuk Kim 
18757a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
18767a2af766SChao Yu {
18777a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
18787a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
18797a2af766SChao Yu }
18807a2af766SChao Yu 
188139a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
188239a53e0cSJaegeuk Kim {
188339a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
188439a53e0cSJaegeuk Kim }
188539a53e0cSJaegeuk Kim 
18867a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
18877a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
18887a2af766SChao Yu 			struct page *node_page, unsigned int offset)
188939a53e0cSJaegeuk Kim {
189039a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
189139a53e0cSJaegeuk Kim 	__le32 *addr_array;
18927a2af766SChao Yu 	int base = 0;
18937a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
1894cac5a3d8SDongOh Shin 
189545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
18967a2af766SChao Yu 
18977a2af766SChao Yu 	/* from GC path only */
18987a2af766SChao Yu 	if (!inode) {
18997a2af766SChao Yu 		if (is_inode)
19007a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
19017a2af766SChao Yu 	} else if (f2fs_has_extra_attr(inode) && is_inode) {
19027a2af766SChao Yu 		base = get_extra_isize(inode);
19037a2af766SChao Yu 	}
19047a2af766SChao Yu 
190539a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
19067a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
190739a53e0cSJaegeuk Kim }
190839a53e0cSJaegeuk Kim 
190939a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
191039a53e0cSJaegeuk Kim {
191139a53e0cSJaegeuk Kim 	int mask;
191239a53e0cSJaegeuk Kim 
191339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
191439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
191539a53e0cSJaegeuk Kim 	return mask & *addr;
191639a53e0cSJaegeuk Kim }
191739a53e0cSJaegeuk Kim 
1918a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1919a66cdd98SJaegeuk Kim {
1920a66cdd98SJaegeuk Kim 	int mask;
1921a66cdd98SJaegeuk Kim 
1922a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1923a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1924a66cdd98SJaegeuk Kim 	*addr |= mask;
1925a66cdd98SJaegeuk Kim }
1926a66cdd98SJaegeuk Kim 
1927a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1928a66cdd98SJaegeuk Kim {
1929a66cdd98SJaegeuk Kim 	int mask;
1930a66cdd98SJaegeuk Kim 
1931a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1932a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1933a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1934a66cdd98SJaegeuk Kim }
1935a66cdd98SJaegeuk Kim 
193652aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
193739a53e0cSJaegeuk Kim {
193839a53e0cSJaegeuk Kim 	int mask;
193939a53e0cSJaegeuk Kim 	int ret;
194039a53e0cSJaegeuk Kim 
194139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
194239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
194339a53e0cSJaegeuk Kim 	ret = mask & *addr;
194439a53e0cSJaegeuk Kim 	*addr |= mask;
194539a53e0cSJaegeuk Kim 	return ret;
194639a53e0cSJaegeuk Kim }
194739a53e0cSJaegeuk Kim 
194852aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
194939a53e0cSJaegeuk Kim {
195039a53e0cSJaegeuk Kim 	int mask;
195139a53e0cSJaegeuk Kim 	int ret;
195239a53e0cSJaegeuk Kim 
195339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
195439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
195539a53e0cSJaegeuk Kim 	ret = mask & *addr;
195639a53e0cSJaegeuk Kim 	*addr &= ~mask;
195739a53e0cSJaegeuk Kim 	return ret;
195839a53e0cSJaegeuk Kim }
195939a53e0cSJaegeuk Kim 
1960c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1961c6ac4c0eSGu Zheng {
1962c6ac4c0eSGu Zheng 	int mask;
1963c6ac4c0eSGu Zheng 
1964c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1965c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1966c6ac4c0eSGu Zheng 	*addr ^= mask;
1967c6ac4c0eSGu Zheng }
1968c6ac4c0eSGu Zheng 
19695c57132eSChao Yu #define F2FS_REG_FLMASK		(~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
19705c57132eSChao Yu #define F2FS_OTHER_FLMASK	(FS_NODUMP_FL | FS_NOATIME_FL)
19715c57132eSChao Yu #define F2FS_FL_INHERITED	(FS_PROJINHERIT_FL)
19725c57132eSChao Yu 
19735c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
19745c57132eSChao Yu {
19755c57132eSChao Yu 	if (S_ISDIR(mode))
19765c57132eSChao Yu 		return flags;
19775c57132eSChao Yu 	else if (S_ISREG(mode))
19785c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
19795c57132eSChao Yu 	else
19805c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
19815c57132eSChao Yu }
19825c57132eSChao Yu 
198339a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
198439a53e0cSJaegeuk Kim enum {
198539a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1986b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
198726de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
1988ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
198939a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
199039a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
199139a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1992c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1993c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1994444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
19951001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
199634d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1997fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1998fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
199988b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
200088b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
20015fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
200202a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
20033c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
20041e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2005b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2006510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2007d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2008c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2009dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2010ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
20117a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
20125c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
201339a53e0cSJaegeuk Kim };
201439a53e0cSJaegeuk Kim 
2015205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2016205b9822SJaegeuk Kim 						int flag, bool set)
201739a53e0cSJaegeuk Kim {
2018205b9822SJaegeuk Kim 	switch (flag) {
2019205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2020205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2021205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
2022205b9822SJaegeuk Kim 		if (set)
2023205b9822SJaegeuk Kim 			return;
2024205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2025205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
20267c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2027205b9822SJaegeuk Kim 	}
202839a53e0cSJaegeuk Kim }
202939a53e0cSJaegeuk Kim 
203091942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
203139a53e0cSJaegeuk Kim {
203291942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
203391942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2034205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
203539a53e0cSJaegeuk Kim }
203639a53e0cSJaegeuk Kim 
203791942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
203839a53e0cSJaegeuk Kim {
203991942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
204039a53e0cSJaegeuk Kim }
204139a53e0cSJaegeuk Kim 
204291942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
204339a53e0cSJaegeuk Kim {
204491942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
204591942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2046205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
204739a53e0cSJaegeuk Kim }
204839a53e0cSJaegeuk Kim 
204991942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2050444c580fSJaegeuk Kim {
205191942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
205291942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
20537c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
205439a53e0cSJaegeuk Kim }
205539a53e0cSJaegeuk Kim 
2056a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2057a1961246SJaegeuk Kim {
2058a1961246SJaegeuk Kim 	if (inc)
2059a1961246SJaegeuk Kim 		inc_nlink(inode);
2060a1961246SJaegeuk Kim 	else
2061a1961246SJaegeuk Kim 		drop_nlink(inode);
20627c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2063a1961246SJaegeuk Kim }
2064a1961246SJaegeuk Kim 
20658edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
20660abd675eSChao Yu 					block_t diff, bool add, bool claim)
20678edd03c8SJaegeuk Kim {
206826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
206926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
207026de9b11SJaegeuk Kim 
20710abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
20720abd675eSChao Yu 	if (add) {
20730abd675eSChao Yu 		if (claim)
20740abd675eSChao Yu 			dquot_claim_block(inode, diff);
20750abd675eSChao Yu 		else
20760abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
20770abd675eSChao Yu 	} else {
20780abd675eSChao Yu 		dquot_free_block(inode, diff);
20790abd675eSChao Yu 	}
20800abd675eSChao Yu 
20817c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
208226de9b11SJaegeuk Kim 	if (clean || recover)
208326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
20848edd03c8SJaegeuk Kim }
20858edd03c8SJaegeuk Kim 
2086fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2087fc9581c8SJaegeuk Kim {
208826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
208926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
209026de9b11SJaegeuk Kim 
2091fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2092fc9581c8SJaegeuk Kim 		return;
2093fc9581c8SJaegeuk Kim 
2094fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
20957c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
209626de9b11SJaegeuk Kim 	if (clean || recover)
209726de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
209826de9b11SJaegeuk Kim }
209926de9b11SJaegeuk Kim 
2100205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2101205b9822SJaegeuk Kim {
2102205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
21037c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2104205b9822SJaegeuk Kim }
2105205b9822SJaegeuk Kim 
2106205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2107205b9822SJaegeuk Kim {
2108205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
21097c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2110205b9822SJaegeuk Kim }
2111205b9822SJaegeuk Kim 
2112205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2113205b9822SJaegeuk Kim {
2114205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
21157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2116205b9822SJaegeuk Kim }
2117205b9822SJaegeuk Kim 
211891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2119444c580fSJaegeuk Kim {
2120205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2121205b9822SJaegeuk Kim 
2122444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2123205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
21241001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2125205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
212634d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2127205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2128b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2129205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2130510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2131205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
21327a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
21337a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
2134444c580fSJaegeuk Kim }
2135444c580fSJaegeuk Kim 
213691942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2137444c580fSJaegeuk Kim {
2138444c580fSJaegeuk Kim 	ri->i_inline = 0;
2139444c580fSJaegeuk Kim 
214091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2141444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
214291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
21431001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
214491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
214534d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
214691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2147b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
214891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2149510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
21507a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
21517a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
21527a2af766SChao Yu }
21537a2af766SChao Yu 
21547a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
21557a2af766SChao Yu {
21567a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2157444c580fSJaegeuk Kim }
2158444c580fSJaegeuk Kim 
2159987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2160987c7c31SChao Yu {
216191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2162987c7c31SChao Yu }
2163987c7c31SChao Yu 
216481ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2165de93653fSJaegeuk Kim {
216681ca7350SChao Yu 	if (f2fs_has_inline_xattr(inode))
21677a2af766SChao Yu 		return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS;
21687a2af766SChao Yu 	return CUR_ADDRS_PER_INODE(inode);
2169de93653fSJaegeuk Kim }
2170de93653fSJaegeuk Kim 
217165985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
217265985d93SJaegeuk Kim {
2173695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2174cac5a3d8SDongOh Shin 
217565985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
217665985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
217765985d93SJaegeuk Kim }
217865985d93SJaegeuk Kim 
217965985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
218065985d93SJaegeuk Kim {
2181987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
218265985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
218365985d93SJaegeuk Kim 	else
218465985d93SJaegeuk Kim 		return 0;
218565985d93SJaegeuk Kim }
218665985d93SJaegeuk Kim 
21870dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
21880dbdc2aeSJaegeuk Kim {
218991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
21900dbdc2aeSJaegeuk Kim }
21910dbdc2aeSJaegeuk Kim 
2192b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2193b3d208f9SJaegeuk Kim {
219491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2195b3d208f9SJaegeuk Kim }
2196b3d208f9SJaegeuk Kim 
2197510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2198510022a8SJaegeuk Kim {
219991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2200510022a8SJaegeuk Kim }
2201510022a8SJaegeuk Kim 
220288b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
220388b88a66SJaegeuk Kim {
220491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
220588b88a66SJaegeuk Kim }
220688b88a66SJaegeuk Kim 
22075fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
22085fe45743SChao Yu {
22095fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
22105fe45743SChao Yu }
22115fe45743SChao Yu 
221202a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
221302a1335fSJaegeuk Kim {
221491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
221502a1335fSJaegeuk Kim }
221602a1335fSJaegeuk Kim 
22173c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
22183c6c2bebSJaegeuk Kim {
221991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
22203c6c2bebSJaegeuk Kim }
22213c6c2bebSJaegeuk Kim 
22221e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
22231e84371fSJaegeuk Kim {
222491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
22251e84371fSJaegeuk Kim }
22261e84371fSJaegeuk Kim 
2227f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
22281001b347SHuajun Li {
2229695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
22307a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2231cac5a3d8SDongOh Shin 
22327a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
22331001b347SHuajun Li }
22341001b347SHuajun Li 
223534d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
223634d67debSChao Yu {
223791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
223834d67debSChao Yu }
223934d67debSChao Yu 
22409486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
22419486ba44SJaegeuk Kim {
22429486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
22439486ba44SJaegeuk Kim 		kunmap(page);
22449486ba44SJaegeuk Kim }
22459486ba44SJaegeuk Kim 
2246b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2247b5492af7SJaegeuk Kim {
2248b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2249b5492af7SJaegeuk Kim }
2250b5492af7SJaegeuk Kim 
2251b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2252b5492af7SJaegeuk Kim {
2253b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
22547c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2255b5492af7SJaegeuk Kim }
2256b5492af7SJaegeuk Kim 
2257b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2258b5492af7SJaegeuk Kim {
2259b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
22607c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2261b5492af7SJaegeuk Kim }
2262b5492af7SJaegeuk Kim 
226326787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
226426787236SJaegeuk Kim {
226526787236SJaegeuk Kim 	if (dsync) {
226626787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
226726787236SJaegeuk Kim 		bool ret;
226826787236SJaegeuk Kim 
226926787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
227026787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
227126787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
227226787236SJaegeuk Kim 		return ret;
227326787236SJaegeuk Kim 	}
227426787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
227526787236SJaegeuk Kim 			file_keep_isize(inode) ||
227626787236SJaegeuk Kim 			i_size_read(inode) & PAGE_MASK)
227726787236SJaegeuk Kim 		return false;
227826787236SJaegeuk Kim 	return F2FS_I(inode)->last_disk_size == i_size_read(inode);
2279267378d4SChao Yu }
2280267378d4SChao Yu 
2281267378d4SChao Yu static inline int f2fs_readonly(struct super_block *sb)
2282267378d4SChao Yu {
2283267378d4SChao Yu 	return sb->s_flags & MS_RDONLY;
2284267378d4SChao Yu }
228539a53e0cSJaegeuk Kim 
228639a53e0cSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
228739a53e0cSJaegeuk Kim {
2288aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
228939a53e0cSJaegeuk Kim }
229039a53e0cSJaegeuk Kim 
2291eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2292eaa693f4SJaegeuk Kim {
2293eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2294eaa693f4SJaegeuk Kim 		return true;
2295eaa693f4SJaegeuk Kim 
2296eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2297eaa693f4SJaegeuk Kim 		return true;
2298eaa693f4SJaegeuk Kim 
2299eaa693f4SJaegeuk Kim 	return false;
2300eaa693f4SJaegeuk Kim }
2301eaa693f4SJaegeuk Kim 
23023e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
23033e72f721SJaegeuk Kim {
23043e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
230591942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
23063e72f721SJaegeuk Kim 		return false;
23073e72f721SJaegeuk Kim 
2308886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
23093e72f721SJaegeuk Kim }
23103e72f721SJaegeuk Kim 
23111ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
23121ecc0c5cSChao Yu 					size_t size, gfp_t flags)
23130414b004SJaegeuk Kim {
23142c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
231555523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
231655523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
23172c63feadSJaegeuk Kim 		return NULL;
231855523519SChao Yu 	}
23192c63feadSJaegeuk Kim #endif
23200414b004SJaegeuk Kim 	return kmalloc(size, flags);
23210414b004SJaegeuk Kim }
23220414b004SJaegeuk Kim 
23237a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2324f2470371SChao Yu {
23257a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2326f2470371SChao Yu }
2327f2470371SChao Yu 
232839a53e0cSJaegeuk Kim #define get_inode_mode(i) \
232991942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
233039a53e0cSJaegeuk Kim 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
233139a53e0cSJaegeuk Kim 
23327a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
23337a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
23347a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
23357a2af766SChao Yu 
23365c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
23375c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
23385c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
23395c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
23405c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
23415c57132eSChao Yu 
2342b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2343b0af6d49SChao Yu {
2344b0af6d49SChao Yu 	int i;
2345b0af6d49SChao Yu 
2346b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2347b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2348b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2349b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2350b0af6d49SChao Yu }
2351b0af6d49SChao Yu 
2352b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2353b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2354b0af6d49SChao Yu {
2355b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2356b0af6d49SChao Yu 		return;
2357b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2358b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2359b0af6d49SChao Yu 
2360b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2361b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2362b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2363b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2364b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2365b0af6d49SChao Yu }
2366b0af6d49SChao Yu 
236739a53e0cSJaegeuk Kim /*
236839a53e0cSJaegeuk Kim  * file.c
236939a53e0cSJaegeuk Kim  */
2370cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2371cac5a3d8SDongOh Shin void truncate_data_blocks(struct dnode_of_data *dn);
2372cac5a3d8SDongOh Shin int truncate_blocks(struct inode *inode, u64 from, bool lock);
2373cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2374a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2375a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2376cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2377cac5a3d8SDongOh Shin int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2378cac5a3d8SDongOh Shin int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2379cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2380cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
238139a53e0cSJaegeuk Kim 
238239a53e0cSJaegeuk Kim /*
238339a53e0cSJaegeuk Kim  * inode.c
238439a53e0cSJaegeuk Kim  */
2385cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2386704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2387704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2388cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2389cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2390cac5a3d8SDongOh Shin int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2391cac5a3d8SDongOh Shin int update_inode(struct inode *inode, struct page *node_page);
2392cac5a3d8SDongOh Shin int update_inode_page(struct inode *inode);
2393cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2394cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
2395cac5a3d8SDongOh Shin void handle_failed_inode(struct inode *inode);
239639a53e0cSJaegeuk Kim 
239739a53e0cSJaegeuk Kim /*
239839a53e0cSJaegeuk Kim  * namei.c
239939a53e0cSJaegeuk Kim  */
240039a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
240139a53e0cSJaegeuk Kim 
240239a53e0cSJaegeuk Kim /*
240339a53e0cSJaegeuk Kim  * dir.c
240439a53e0cSJaegeuk Kim  */
2405cac5a3d8SDongOh Shin void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2406cac5a3d8SDongOh Shin unsigned char get_de_type(struct f2fs_dir_entry *de);
2407cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2408cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2409cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2410cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2411cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
2412cac5a3d8SDongOh Shin void do_make_empty_dir(struct inode *inode, struct inode *parent,
2413cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2414cac5a3d8SDongOh Shin struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2415cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2416cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
2417cac5a3d8SDongOh Shin void update_parent_metadata(struct inode *dir, struct inode *inode,
2418cac5a3d8SDongOh Shin 			unsigned int current_depth);
2419cac5a3d8SDongOh Shin int room_for_filename(const void *bitmap, int slots, int max_slots);
2420cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2421cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2422cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2423cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2424cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2425cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2426cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2427cac5a3d8SDongOh Shin 			struct page **page);
2428cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2429cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2430cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2431cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2432cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2433cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2434cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2435cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2436cac5a3d8SDongOh Shin int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2437cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2438cac5a3d8SDongOh Shin int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2439cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2440cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2441cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2442cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2443cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
244439a53e0cSJaegeuk Kim 
2445b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2446b7f7a5e0SAl Viro {
24472b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2448510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2449b7f7a5e0SAl Viro }
2450b7f7a5e0SAl Viro 
245139a53e0cSJaegeuk Kim /*
245239a53e0cSJaegeuk Kim  * super.c
245339a53e0cSJaegeuk Kim  */
2454cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2455cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
24564b2414d0SChao Yu void f2fs_enable_quota_files(struct f2fs_sb_info *sbi);
24574b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2458cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2459cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2460a07ef784SNamjae Jeon extern __printf(3, 4)
2461cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2462984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
246339a53e0cSJaegeuk Kim 
246439a53e0cSJaegeuk Kim /*
246539a53e0cSJaegeuk Kim  * hash.c
246639a53e0cSJaegeuk Kim  */
24676332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
24686332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
246939a53e0cSJaegeuk Kim 
247039a53e0cSJaegeuk Kim /*
247139a53e0cSJaegeuk Kim  * node.c
247239a53e0cSJaegeuk Kim  */
247339a53e0cSJaegeuk Kim struct dnode_of_data;
247439a53e0cSJaegeuk Kim struct node_info;
247539a53e0cSJaegeuk Kim 
2476cac5a3d8SDongOh Shin bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2477cac5a3d8SDongOh Shin int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2478cac5a3d8SDongOh Shin bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2479cac5a3d8SDongOh Shin bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2480cac5a3d8SDongOh Shin void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2481cac5a3d8SDongOh Shin pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2482cac5a3d8SDongOh Shin int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2483cac5a3d8SDongOh Shin int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2484cac5a3d8SDongOh Shin int truncate_xattr_node(struct inode *inode, struct page *page);
2485cac5a3d8SDongOh Shin int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2486cac5a3d8SDongOh Shin int remove_inode_page(struct inode *inode);
2487cac5a3d8SDongOh Shin struct page *new_inode_page(struct inode *inode);
24885f4ce6abSYunlei He struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2489cac5a3d8SDongOh Shin void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2490cac5a3d8SDongOh Shin struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2491cac5a3d8SDongOh Shin struct page *get_node_page_ra(struct page *parent, int start);
2492cac5a3d8SDongOh Shin void move_node_page(struct page *node_page, int gc_type);
2493cac5a3d8SDongOh Shin int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2494cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
2495401db79fSYunlong Song int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2496b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
249722ad0b6aSJaegeuk Kim void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2498cac5a3d8SDongOh Shin bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2499cac5a3d8SDongOh Shin void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2500cac5a3d8SDongOh Shin void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2501cac5a3d8SDongOh Shin int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2502cac5a3d8SDongOh Shin void recover_inline_xattr(struct inode *inode, struct page *page);
2503d260081cSChao Yu int recover_xattr_data(struct inode *inode, struct page *page,
2504cac5a3d8SDongOh Shin 			block_t blkaddr);
2505cac5a3d8SDongOh Shin int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2506cac5a3d8SDongOh Shin int restore_node_summary(struct f2fs_sb_info *sbi,
2507cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
250822ad0b6aSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2509cac5a3d8SDongOh Shin int build_node_manager(struct f2fs_sb_info *sbi);
2510cac5a3d8SDongOh Shin void destroy_node_manager(struct f2fs_sb_info *sbi);
25116e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
251239a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
251339a53e0cSJaegeuk Kim 
251439a53e0cSJaegeuk Kim /*
251539a53e0cSJaegeuk Kim  * segment.c
251639a53e0cSJaegeuk Kim  */
2517b3a97a2aSJaegeuk Kim bool need_SSR(struct f2fs_sb_info *sbi);
2518cac5a3d8SDongOh Shin void register_inmem_page(struct inode *inode, struct page *page);
2519cac5a3d8SDongOh Shin void drop_inmem_pages(struct inode *inode);
25208c242db9SJaegeuk Kim void drop_inmem_page(struct inode *inode, struct page *page);
2521cac5a3d8SDongOh Shin int commit_inmem_pages(struct inode *inode);
2522cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2523cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2524cac5a3d8SDongOh Shin int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2525cac5a3d8SDongOh Shin int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2526cac5a3d8SDongOh Shin void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2527cac5a3d8SDongOh Shin void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2528cac5a3d8SDongOh Shin bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2529cac5a3d8SDongOh Shin void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
2530cce13252SChao Yu void stop_discard_thread(struct f2fs_sb_info *sbi);
2531638164a2SChao Yu void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi, bool umount);
2532cac5a3d8SDongOh Shin void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2533cac5a3d8SDongOh Shin void release_discard_addrs(struct f2fs_sb_info *sbi);
2534cac5a3d8SDongOh Shin int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2535cac5a3d8SDongOh Shin void allocate_new_segments(struct f2fs_sb_info *sbi);
2536cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2537cac5a3d8SDongOh Shin bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2538cac5a3d8SDongOh Shin struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2539cac5a3d8SDongOh Shin void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2540b0af6d49SChao Yu void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2541b0af6d49SChao Yu 						enum iostat_type io_type);
2542cac5a3d8SDongOh Shin void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2543cac5a3d8SDongOh Shin void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2544d1b3e72dSJaegeuk Kim int rewrite_data_page(struct f2fs_io_info *fio);
2545cac5a3d8SDongOh Shin void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2546cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2547cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2548cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2549cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2550cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2551cac5a3d8SDongOh Shin 			bool recover_newaddr);
2552cac5a3d8SDongOh Shin void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2553cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2554fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2555fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2556cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2557cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2558d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2559cac5a3d8SDongOh Shin void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2560cac5a3d8SDongOh Shin void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2561cac5a3d8SDongOh Shin int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2562cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
2563cac5a3d8SDongOh Shin void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2564cac5a3d8SDongOh Shin int build_segment_manager(struct f2fs_sb_info *sbi);
2565cac5a3d8SDongOh Shin void destroy_segment_manager(struct f2fs_sb_info *sbi);
25667fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
25677fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
256839a53e0cSJaegeuk Kim 
256939a53e0cSJaegeuk Kim /*
257039a53e0cSJaegeuk Kim  * checkpoint.c
257139a53e0cSJaegeuk Kim  */
2572cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2573cac5a3d8SDongOh Shin struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2574cac5a3d8SDongOh Shin struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2575cac5a3d8SDongOh Shin struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2576cac5a3d8SDongOh Shin bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2577cac5a3d8SDongOh Shin int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2578cac5a3d8SDongOh Shin 			int type, bool sync);
2579cac5a3d8SDongOh Shin void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2580cac5a3d8SDongOh Shin long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2581b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
2582cac5a3d8SDongOh Shin void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2583cac5a3d8SDongOh Shin void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2584cac5a3d8SDongOh Shin void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2585cac5a3d8SDongOh Shin bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2586cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2587cac5a3d8SDongOh Shin int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2588cac5a3d8SDongOh Shin void release_orphan_inode(struct f2fs_sb_info *sbi);
2589cac5a3d8SDongOh Shin void add_orphan_inode(struct inode *inode);
2590cac5a3d8SDongOh Shin void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2591cac5a3d8SDongOh Shin int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2592cac5a3d8SDongOh Shin int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2593cac5a3d8SDongOh Shin void update_dirty_page(struct inode *inode, struct page *page);
2594cac5a3d8SDongOh Shin void remove_dirty_inode(struct inode *inode);
2595cac5a3d8SDongOh Shin int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2596cac5a3d8SDongOh Shin int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2597cac5a3d8SDongOh Shin void init_ino_entry_info(struct f2fs_sb_info *sbi);
25986e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
259939a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
260039a53e0cSJaegeuk Kim 
260139a53e0cSJaegeuk Kim /*
260239a53e0cSJaegeuk Kim  * data.c
260339a53e0cSJaegeuk Kim  */
2604b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2605b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2606942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2607b9109b0eSJaegeuk Kim 				enum page_type type);
2608b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2609cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2610b9109b0eSJaegeuk Kim int f2fs_submit_page_write(struct f2fs_io_info *fio);
2611cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2612cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2613cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2614cac5a3d8SDongOh Shin void set_data_blkaddr(struct dnode_of_data *dn);
2615cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2616cac5a3d8SDongOh Shin int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2617cac5a3d8SDongOh Shin int reserve_new_block(struct dnode_of_data *dn);
2618cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2619cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2620cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2621cac5a3d8SDongOh Shin struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2622cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
2623cac5a3d8SDongOh Shin struct page *find_data_page(struct inode *inode, pgoff_t index);
2624cac5a3d8SDongOh Shin struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2625cac5a3d8SDongOh Shin 			bool for_write);
2626cac5a3d8SDongOh Shin struct page *get_new_data_page(struct inode *inode,
2627cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
2628cac5a3d8SDongOh Shin int do_write_data_page(struct f2fs_io_info *fio);
2629cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2630cac5a3d8SDongOh Shin 			int create, int flag);
2631cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2632cac5a3d8SDongOh Shin 			u64 start, u64 len);
2633cac5a3d8SDongOh Shin void f2fs_set_page_dirty_nobuffers(struct page *page);
2634b0af6d49SChao Yu int __f2fs_write_data_pages(struct address_space *mapping,
2635b0af6d49SChao Yu 						struct writeback_control *wbc,
2636b0af6d49SChao Yu 						enum iostat_type io_type);
2637cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2638cac5a3d8SDongOh Shin 			unsigned int length);
2639cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
26405b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2641cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2642cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
26435b7a487cSWeichao Guo #endif
264439a53e0cSJaegeuk Kim 
264539a53e0cSJaegeuk Kim /*
264639a53e0cSJaegeuk Kim  * gc.c
264739a53e0cSJaegeuk Kim  */
2648cac5a3d8SDongOh Shin int start_gc_thread(struct f2fs_sb_info *sbi);
2649cac5a3d8SDongOh Shin void stop_gc_thread(struct f2fs_sb_info *sbi);
2650cac5a3d8SDongOh Shin block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2651e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2652e066b83cSJaegeuk Kim 			unsigned int segno);
2653cac5a3d8SDongOh Shin void build_gc_manager(struct f2fs_sb_info *sbi);
265439a53e0cSJaegeuk Kim 
265539a53e0cSJaegeuk Kim /*
265639a53e0cSJaegeuk Kim  * recovery.c
265739a53e0cSJaegeuk Kim  */
2658cac5a3d8SDongOh Shin int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2659cac5a3d8SDongOh Shin bool space_for_roll_forward(struct f2fs_sb_info *sbi);
266039a53e0cSJaegeuk Kim 
266139a53e0cSJaegeuk Kim /*
266239a53e0cSJaegeuk Kim  * debug.c
266339a53e0cSJaegeuk Kim  */
266439a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
266539a53e0cSJaegeuk Kim struct f2fs_stat_info {
266639a53e0cSJaegeuk Kim 	struct list_head stat_list;
266739a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
266839a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
266939a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
26705b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
26715b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
2672c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
267335782b23SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
267435782b23SJaegeuk Kim 	int inmem_pages;
26750f18b462SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, ndirty_all;
26765b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
26775b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
267839a53e0cSJaegeuk Kim 	int total_count, utilization;
26798b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
268014d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
268114d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
26825f32366aSChao Yu 	int nr_discard_cmd;
2683d84d1cbdSChao Yu 	unsigned int undiscard_blks;
2684a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2685648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2686f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
268739a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
268839a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
268939a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
269039a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
269142190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
269239a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2693e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
269439a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
2695e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
269639a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
269739a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
269839a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
269939a53e0cSJaegeuk Kim 
270039a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
270139a53e0cSJaegeuk Kim 	unsigned int block_count[2];
2702b9a2c252SChangman Lee 	unsigned int inplace_count;
27039edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
270439a53e0cSJaegeuk Kim };
270539a53e0cSJaegeuk Kim 
2706963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2707963d4f7dSGu Zheng {
2708963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
2709963d4f7dSGu Zheng }
2710963d4f7dSGu Zheng 
2711942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
271242190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
271339a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
2714dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
271533fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
271633fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
27175b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
27185b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
27195b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
27205b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
2721d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
2722d5e8f6c9SChao Yu 	do {								\
2723d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2724d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2725d5e8f6c9SChao Yu 	} while (0)
2726d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2727d5e8f6c9SChao Yu 	do {								\
2728d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2729d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2730d5e8f6c9SChao Yu 	} while (0)
27310dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
27320dbdc2aeSJaegeuk Kim 	do {								\
27330dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
273403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
27350dbdc2aeSJaegeuk Kim 	} while (0)
27360dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
27370dbdc2aeSJaegeuk Kim 	do {								\
27380dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
273903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
27400dbdc2aeSJaegeuk Kim 	} while (0)
27413289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
27423289c061SJaegeuk Kim 	do {								\
27433289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
274403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
27453289c061SJaegeuk Kim 	} while (0)
27463289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
27473289c061SJaegeuk Kim 	do {								\
27483289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
274903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
27503289c061SJaegeuk Kim 	} while (0)
2751dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2752dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2753dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2754dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2755b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2756b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
275726a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
2758cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
275926a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
2760cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
276126a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
276226a28a0cSJaegeuk Kim 	do {								\
276326a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
276426a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
276526a28a0cSJaegeuk Kim 		if (cur > max)						\
276626a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
276726a28a0cSJaegeuk Kim 	} while (0)
2768648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
2769648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2770648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
2771648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2772648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
2773648d50baSChao Yu 	do {								\
2774648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
2775648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
2776648d50baSChao Yu 		if (cur > max)						\
2777648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
2778648d50baSChao Yu 	} while (0)
2779e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
278039a53e0cSJaegeuk Kim 	do {								\
2781963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
278268afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
278368afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
278439a53e0cSJaegeuk Kim 			si->data_segs++;				\
2785e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2786e1235983SChangman Lee 		} else {						\
278739a53e0cSJaegeuk Kim 			si->node_segs++;				\
2788e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2789e1235983SChangman Lee 		}							\
279039a53e0cSJaegeuk Kim 	} while (0)
279139a53e0cSJaegeuk Kim 
279239a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
279368afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
279439a53e0cSJaegeuk Kim 
2795e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
279639a53e0cSJaegeuk Kim 	do {								\
2797963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
279839a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
279939a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
280068afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
280139a53e0cSJaegeuk Kim 	} while (0)
280239a53e0cSJaegeuk Kim 
2803e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
280439a53e0cSJaegeuk Kim 	do {								\
2805963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
280639a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
280739a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
280868afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
280939a53e0cSJaegeuk Kim 	} while (0)
281039a53e0cSJaegeuk Kim 
2811cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
2812cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
2813787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
28144589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
281539a53e0cSJaegeuk Kim #else
2816d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
2817d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
2818d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
2819d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
2820d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
2821d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
2822d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
2823d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
2824d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
2825d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
2826d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
2827d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
2828d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
2829d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
2830d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
2831d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
2832d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
2833d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
2834d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
2835d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
2836d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
2837d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
2838d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
2839d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
2840d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
2841d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
2842d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
2843d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
2844d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
284539a53e0cSJaegeuk Kim 
284639a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
284739a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2848787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
28494589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
285039a53e0cSJaegeuk Kim #endif
285139a53e0cSJaegeuk Kim 
285239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
285339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
285439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
285539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
285639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
285739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
285839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
285939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
2860cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
286139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
286229e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
28631001b347SHuajun Li 
2864e18c65b2SHuajun Li /*
2865e18c65b2SHuajun Li  * inline.c
2866e18c65b2SHuajun Li  */
2867cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
2868cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
2869cac5a3d8SDongOh Shin void read_inline_data(struct page *page, struct page *ipage);
2870bd4667cbSKinglong Mee void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2871cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
2872cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2873cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
2874cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
2875cac5a3d8SDongOh Shin bool recover_inline_data(struct inode *inode, struct page *npage);
2876cac5a3d8SDongOh Shin struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2877cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2878cac5a3d8SDongOh Shin int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2879cac5a3d8SDongOh Shin 			struct page *ipage);
2880cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2881cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2882cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2883cac5a3d8SDongOh Shin void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2884cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2885cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
2886cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2887cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
2888cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
2889cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
2890cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
2891cde4de12SJaegeuk Kim 
2892cde4de12SJaegeuk Kim /*
28932658e50dSJaegeuk Kim  * shrinker.c
28942658e50dSJaegeuk Kim  */
2895cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
2896cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2897cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2898cac5a3d8SDongOh Shin 			struct shrink_control *sc);
2899cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2900cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
29012658e50dSJaegeuk Kim 
29022658e50dSJaegeuk Kim /*
2903a28ef1f5SChao Yu  * extent_cache.c
2904a28ef1f5SChao Yu  */
2905004b6862SChao Yu struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2906004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
2907004b6862SChao Yu struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2908004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
2909004b6862SChao Yu 				unsigned int ofs);
2910004b6862SChao Yu struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2911004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
2912004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
2913004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
2914004b6862SChao Yu 		bool force);
2915df0f6b44SChao Yu bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2916df0f6b44SChao Yu 						struct rb_root *root);
2917cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2918cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2919cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
2920cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
2921cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
2922cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2923cac5a3d8SDongOh Shin 			struct extent_info *ei);
2924cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
292519b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
2926cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
2927cac5a3d8SDongOh Shin void init_extent_cache_info(struct f2fs_sb_info *sbi);
2928a28ef1f5SChao Yu int __init create_extent_cache(void);
2929a28ef1f5SChao Yu void destroy_extent_cache(void);
2930a28ef1f5SChao Yu 
2931a28ef1f5SChao Yu /*
29328ceffcb2SChao Yu  * sysfs.c
29338ceffcb2SChao Yu  */
2934dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
2935dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
2936dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
2937dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
29388ceffcb2SChao Yu 
29398ceffcb2SChao Yu /*
2940cde4de12SJaegeuk Kim  * crypto support
2941cde4de12SJaegeuk Kim  */
29420b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
2943cde4de12SJaegeuk Kim {
2944cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
2945cde4de12SJaegeuk Kim }
2946cde4de12SJaegeuk Kim 
29471958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
29481958593eSJaegeuk Kim {
29491958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
29501958593eSJaegeuk Kim }
29511958593eSJaegeuk Kim 
2952cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
2953cde4de12SJaegeuk Kim {
2954cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2955cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2956cde4de12SJaegeuk Kim #endif
2957cde4de12SJaegeuk Kim }
2958cde4de12SJaegeuk Kim 
2959cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2960cde4de12SJaegeuk Kim {
29610b81d077SJaegeuk Kim 	return bio->bi_private != NULL;
2962cde4de12SJaegeuk Kim }
2963cde4de12SJaegeuk Kim 
2964cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2965cde4de12SJaegeuk Kim {
2966cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2967cde4de12SJaegeuk Kim }
2968f424f664SJaegeuk Kim 
29690bfd7a09SDamien Le Moal static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
297052763a4bSJaegeuk Kim {
29710bfd7a09SDamien Le Moal 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
297252763a4bSJaegeuk Kim }
297352763a4bSJaegeuk Kim 
29747a2af766SChao Yu static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
29757a2af766SChao Yu {
29767a2af766SChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
29777a2af766SChao Yu }
29787a2af766SChao Yu 
29795c57132eSChao Yu static inline int f2fs_sb_has_project_quota(struct super_block *sb)
29805c57132eSChao Yu {
29815c57132eSChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
29825c57132eSChao Yu }
29835c57132eSChao Yu 
2984704956ecSChao Yu static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
2985704956ecSChao Yu {
2986704956ecSChao Yu 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
2987704956ecSChao Yu }
2988704956ecSChao Yu 
2989178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
2990178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
29913c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
2992178053e2SDamien Le Moal {
2993178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
29943c62be17SJaegeuk Kim 	int i;
2995178053e2SDamien Le Moal 
29963c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
29973c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
29983c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
29993c62be17SJaegeuk Kim 	return -EINVAL;
3000178053e2SDamien Le Moal }
3001178053e2SDamien Le Moal #endif
3002178053e2SDamien Le Moal 
300396ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
300496ba2decSDamien Le Moal {
300596ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
300696ba2decSDamien Le Moal 
300796ba2decSDamien Le Moal 	return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
300852763a4bSJaegeuk Kim }
300952763a4bSJaegeuk Kim 
301052763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
301152763a4bSJaegeuk Kim {
301252763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
301352763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
301452763a4bSJaegeuk Kim 
301552763a4bSJaegeuk Kim 	switch (mt) {
301652763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
301752763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
301852763a4bSJaegeuk Kim 		break;
301952763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
302052763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
302152763a4bSJaegeuk Kim 		break;
302252763a4bSJaegeuk Kim 	}
302352763a4bSJaegeuk Kim }
302452763a4bSJaegeuk Kim 
3025fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3026fcc85a4dSJaegeuk Kim {
3027fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3028886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3029fcc85a4dSJaegeuk Kim 
3030fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3031fcc85a4dSJaegeuk Kim #else
3032fcc85a4dSJaegeuk Kim 	return 0;
3033fcc85a4dSJaegeuk Kim #endif
3034fcc85a4dSJaegeuk Kim }
3035fcc85a4dSJaegeuk Kim 
303639a53e0cSJaegeuk Kim #endif
3037