xref: /openbmc/linux/fs/f2fs/f2fs.h (revision db610a640eeeb268c36a4558414f28e1c269433e)
17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
2543b6573bSKeith Mok #include <crypto/hash.h>
2639a53e0cSJaegeuk Kim 
27734f0d24SDave Chinner #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
28734f0d24SDave Chinner #include <linux/fscrypt.h>
29734f0d24SDave Chinner 
305d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
319850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
325d56b671SJaegeuk Kim #else
339850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
349850cf4aSJaegeuk Kim 	do {								\
359850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
369850cf4aSJaegeuk Kim 			WARN_ON(1);					\
37caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
389850cf4aSJaegeuk Kim 		}							\
399850cf4aSJaegeuk Kim 	} while (0)
405d56b671SJaegeuk Kim #endif
415d56b671SJaegeuk Kim 
422c63feadSJaegeuk Kim enum {
432c63feadSJaegeuk Kim 	FAULT_KMALLOC,
44628b3d14SChao Yu 	FAULT_KVMALLOC,
45c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4601eccef7SChao Yu 	FAULT_PAGE_GET,
47d62fe971SChao Yu 	FAULT_ALLOC_BIO,
48cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
49cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
50cb78942bSJaegeuk Kim 	FAULT_BLOCK,
51cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5253aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5314b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
546f5c2ed0SChao Yu 	FAULT_READ_IO,
550f348028SChao Yu 	FAULT_CHECKPOINT,
56b83dcfe6SChao Yu 	FAULT_DISCARD,
576f5c2ed0SChao Yu 	FAULT_WRITE_IO,
582c63feadSJaegeuk Kim 	FAULT_MAX,
592c63feadSJaegeuk Kim };
602c63feadSJaegeuk Kim 
617fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
62d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
63d494500aSChao Yu 
6408796897SSheng Yong struct f2fs_fault_info {
6508796897SSheng Yong 	atomic_t inject_ops;
6608796897SSheng Yong 	unsigned int inject_rate;
6708796897SSheng Yong 	unsigned int inject_type;
6808796897SSheng Yong };
6908796897SSheng Yong 
7019880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7168afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
722c63feadSJaegeuk Kim #endif
732c63feadSJaegeuk Kim 
7439a53e0cSJaegeuk Kim /*
7539a53e0cSJaegeuk Kim  * For mount options
7639a53e0cSJaegeuk Kim  */
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
84444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
851001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8634d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8734d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8834d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
89d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9089672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
916aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
92343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9373faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9436abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9536abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
960abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
970abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
985c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
994b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1006afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1017e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1024354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
10339a53e0cSJaegeuk Kim 
10463189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10563189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10663189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10763189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10839a53e0cSJaegeuk Kim 
10939a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11039a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11139a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11239a53e0cSJaegeuk Kim 
113a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
114a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
115a9841c4dSJaegeuk Kim 			 * address format, __le32.
116a9841c4dSJaegeuk Kim 			 */
11739a53e0cSJaegeuk Kim typedef u32 nid_t;
11839a53e0cSJaegeuk Kim 
11939a53e0cSJaegeuk Kim struct f2fs_mount_info {
12039a53e0cSJaegeuk Kim 	unsigned int opt;
12163189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12263189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12363189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12463189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12563189b78SChao Yu 	int active_logs;		/* # of active logs */
12663189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12763189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12863189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12963189b78SChao Yu #endif
13063189b78SChao Yu #ifdef CONFIG_QUOTA
13163189b78SChao Yu 	/* Names of quota files with journalled quota */
13263189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13363189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13463189b78SChao Yu #endif
13563189b78SChao Yu 	/* For which write hints are passed down to block layer */
13663189b78SChao Yu 	int whint_mode;
13763189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13863189b78SChao Yu 	int fsync_mode;			/* fsync policy */
139ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
14039a53e0cSJaegeuk Kim };
14139a53e0cSJaegeuk Kim 
142cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1430bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
144e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1457a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1465c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
147704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1486afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
149234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1501c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
151b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
15253fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
153d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
154cde4de12SJaegeuk Kim 
1557beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1567beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1577beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1587beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1597beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1607beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1617beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
16276f105a2SJaegeuk Kim 
16339a53e0cSJaegeuk Kim /*
1647c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1657c2e5963SJaegeuk Kim  */
1667c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1677c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1687c2e5963SJaegeuk Kim 
1697c2e5963SJaegeuk Kim /*
17039a53e0cSJaegeuk Kim  * For checkpoint manager
17139a53e0cSJaegeuk Kim  */
17239a53e0cSJaegeuk Kim enum {
17339a53e0cSJaegeuk Kim 	NAT_BITMAP,
17439a53e0cSJaegeuk Kim 	SIT_BITMAP
17539a53e0cSJaegeuk Kim };
17639a53e0cSJaegeuk Kim 
177c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
178c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
179c473f1a9SChao Yu #define	CP_SYNC		0x00000004
180c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
181c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1821f43e2adSChao Yu #define CP_TRIMMED	0x00000020
1834354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
18475ab4cb8SJaegeuk Kim 
1854ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
186ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
187969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
188f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
189969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1908bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
19160b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
192dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
1934354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
194*db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
19503f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
196bba681cbSJaegeuk Kim 
19775ab4cb8SJaegeuk Kim struct cp_control {
19875ab4cb8SJaegeuk Kim 	int reason;
1994b2fecc8SJaegeuk Kim 	__u64 trim_start;
2004b2fecc8SJaegeuk Kim 	__u64 trim_end;
2014b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
20275ab4cb8SJaegeuk Kim };
20375ab4cb8SJaegeuk Kim 
204662befdaSChao Yu /*
205e1da7872SChao Yu  * indicate meta/data type
206662befdaSChao Yu  */
207662befdaSChao Yu enum {
208662befdaSChao Yu 	META_CP,
209662befdaSChao Yu 	META_NAT,
21081c1a0f1SChao Yu 	META_SIT,
2114c521f49SJaegeuk Kim 	META_SSA,
212b63e7be5SChao Yu 	META_MAX,
2134c521f49SJaegeuk Kim 	META_POR,
214e1da7872SChao Yu 	DATA_GENERIC,
215e1da7872SChao Yu 	META_GENERIC,
216662befdaSChao Yu };
217662befdaSChao Yu 
2186451e041SJaegeuk Kim /* for the list of ino */
2196451e041SJaegeuk Kim enum {
2206451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
221fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
222fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2230a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
22439d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2256451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2266451e041SJaegeuk Kim };
2276451e041SJaegeuk Kim 
2286451e041SJaegeuk Kim struct ino_entry {
22939a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
23039a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
23139d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
23239a53e0cSJaegeuk Kim };
23339a53e0cSJaegeuk Kim 
2342710fd7eSChao Yu /* for the list of inodes to be GCed */
23506292073SChao Yu struct inode_entry {
23639a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
23739a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
23839a53e0cSJaegeuk Kim };
23939a53e0cSJaegeuk Kim 
24050fa53ecSChao Yu struct fsync_node_entry {
24150fa53ecSChao Yu 	struct list_head list;	/* list head */
24250fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
24350fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
24450fa53ecSChao Yu };
24550fa53ecSChao Yu 
246a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2477fd9e544SJaegeuk Kim struct discard_entry {
2487fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
249a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
250a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2517fd9e544SJaegeuk Kim };
2527fd9e544SJaegeuk Kim 
253969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
254969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
255969d1b18SChao Yu 
256ba48a33eSChao Yu /* max discard pend list number */
257ba48a33eSChao Yu #define MAX_PLIST_NUM		512
258ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
2592f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
260ba48a33eSChao Yu 
26115469963SJaegeuk Kim enum {
26235ec7d57SChao Yu 	D_PREP,			/* initial */
26335ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
26435ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
26535ec7d57SChao Yu 	D_DONE,			/* finished */
26615469963SJaegeuk Kim };
26715469963SJaegeuk Kim 
268004b6862SChao Yu struct discard_info {
269b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
270b01a9201SJaegeuk Kim 	block_t len;			/* length */
271004b6862SChao Yu 	block_t start;			/* actual start address in dev */
272004b6862SChao Yu };
273004b6862SChao Yu 
274b01a9201SJaegeuk Kim struct discard_cmd {
275004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
276004b6862SChao Yu 	union {
277004b6862SChao Yu 		struct {
278004b6862SChao Yu 			block_t lstart;	/* logical start address */
279004b6862SChao Yu 			block_t len;	/* length */
280004b6862SChao Yu 			block_t start;	/* actual start address in dev */
281004b6862SChao Yu 		};
282004b6862SChao Yu 		struct discard_info di;	/* discard info */
283004b6862SChao Yu 
284004b6862SChao Yu 	};
285b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
286b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
287c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
288ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2899a744b92SChao Yu 	unsigned char state;		/* state */
29072691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
291c81abe34SJaegeuk Kim 	int error;			/* bio error */
29235ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
29335ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
294275b66b0SChao Yu };
295275b66b0SChao Yu 
29678997b56SChao Yu enum {
29778997b56SChao Yu 	DPOLICY_BG,
29878997b56SChao Yu 	DPOLICY_FORCE,
29978997b56SChao Yu 	DPOLICY_FSTRIM,
30078997b56SChao Yu 	DPOLICY_UMOUNT,
30178997b56SChao Yu 	MAX_DPOLICY,
30278997b56SChao Yu };
30378997b56SChao Yu 
304ecc9aa00SChao Yu struct discard_policy {
30578997b56SChao Yu 	int type;			/* type of discard */
306ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
307f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
308ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
309ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
310ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
311ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
312ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
31320ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
31478997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
31503f2c02dSJaegeuk Kim 	int timeout;			/* discard timeout for put_super */
316ecc9aa00SChao Yu };
317ecc9aa00SChao Yu 
3180b54fb84SJaegeuk Kim struct discard_cmd_control {
31915469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
32046f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
321ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
32246f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3238412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
32415469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
325969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
32615469963SJaegeuk Kim 	struct mutex cmd_lock;
327d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
328d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
329969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
330d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
33120ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3328b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
33372691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3345f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3354dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
33667fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
33739a53e0cSJaegeuk Kim };
33839a53e0cSJaegeuk Kim 
33939a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
34039a53e0cSJaegeuk Kim struct fsync_inode_entry {
34139a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
34239a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
343c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
344c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
34539a53e0cSJaegeuk Kim };
34639a53e0cSJaegeuk Kim 
34768afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
34868afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
34939a53e0cSJaegeuk Kim 
35068afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
35168afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
35268afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
35368afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
35439a53e0cSJaegeuk Kim 
355dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
356dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
357309cc2b6SJaegeuk Kim 
358dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
35939a53e0cSJaegeuk Kim {
360dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
361cac5a3d8SDongOh Shin 
362dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
36339a53e0cSJaegeuk Kim 	return before;
36439a53e0cSJaegeuk Kim }
36539a53e0cSJaegeuk Kim 
366dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
36739a53e0cSJaegeuk Kim {
368dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
369cac5a3d8SDongOh Shin 
370dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
37139a53e0cSJaegeuk Kim 	return before;
37239a53e0cSJaegeuk Kim }
37339a53e0cSJaegeuk Kim 
374dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
375dfc08a12SChao Yu 							int size, int type)
376184a5cd2SChao Yu {
377184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
378dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
379dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
380184a5cd2SChao Yu }
381184a5cd2SChao Yu 
38239a53e0cSJaegeuk Kim /*
383e9750824SNamjae Jeon  * ioctl commands
384e9750824SNamjae Jeon  */
385e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
386e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
387d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
388e9750824SNamjae Jeon 
38988b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
39088b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
39188b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
39202a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3931e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3941e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
395d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
396456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
397d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
398d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3994dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
4004dd6f977SJaegeuk Kim 						struct f2fs_move_range)
401e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
402e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
40334dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
40434dc77adSJaegeuk Kim 						struct f2fs_gc_range)
405e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
4061ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
4071ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
408c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
40988b88a66SJaegeuk Kim 
4100b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
4110b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
4120b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
413f424f664SJaegeuk Kim 
4141abff93dSJaegeuk Kim /*
4151abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4161abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4171abff93dSJaegeuk Kim  */
4181abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4191abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4201abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4211abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
422c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4230cd6d9b0SJaegeuk Kim #define F2FS_GOING_DOWN_NEED_FSCK	0x4	/* going down to trigger fsck */
4241abff93dSJaegeuk Kim 
425e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
426e9750824SNamjae Jeon /*
427e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
428e9750824SNamjae Jeon  */
429e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
430e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
43104ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
432e9750824SNamjae Jeon #endif
433e9750824SNamjae Jeon 
4342c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4352c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4362c1d0305SChao Yu 
43734dc77adSJaegeuk Kim struct f2fs_gc_range {
43834dc77adSJaegeuk Kim 	u32 sync;
43934dc77adSJaegeuk Kim 	u64 start;
44034dc77adSJaegeuk Kim 	u64 len;
44134dc77adSJaegeuk Kim };
44234dc77adSJaegeuk Kim 
443d323d005SChao Yu struct f2fs_defragment {
444d323d005SChao Yu 	u64 start;
445d323d005SChao Yu 	u64 len;
446d323d005SChao Yu };
447d323d005SChao Yu 
4484dd6f977SJaegeuk Kim struct f2fs_move_range {
4494dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4504dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4514dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4524dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4534dd6f977SJaegeuk Kim };
4544dd6f977SJaegeuk Kim 
455e066b83cSJaegeuk Kim struct f2fs_flush_device {
456e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
457e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
458e066b83cSJaegeuk Kim };
459e066b83cSJaegeuk Kim 
460f2470371SChao Yu /* for inline stuff */
461f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4626afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4637a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4646afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4657a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4667a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
467b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4686afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
469f2470371SChao Yu 
470f2470371SChao Yu /* for inline dir */
471f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
472f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
473f2470371SChao Yu 				BITS_PER_BYTE + 1))
474f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
475f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
476f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
477f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
478f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
479f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
480f2470371SChao Yu 
481e9750824SNamjae Jeon /*
48239a53e0cSJaegeuk Kim  * For INODE and NODE manager
48339a53e0cSJaegeuk Kim  */
4847b3cd7d6SJaegeuk Kim /* for directory operations */
4857b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
486d8c6822aSJaegeuk Kim 	struct inode *inode;
48776a9dd85SChao Yu 	void *bitmap;
4887b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4897b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4907b3cd7d6SJaegeuk Kim 	int max;
49176a9dd85SChao Yu 	int nr_bitmap;
4927b3cd7d6SJaegeuk Kim };
4937b3cd7d6SJaegeuk Kim 
49464c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
49564c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4967b3cd7d6SJaegeuk Kim {
497d8c6822aSJaegeuk Kim 	d->inode = inode;
4987b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
49976a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
500c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5017b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5027b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
50364c24ecbSTomohiro Kusumi }
504cac5a3d8SDongOh Shin 
50564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
506f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
50764c24ecbSTomohiro Kusumi {
508f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
509f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
510f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
511f2470371SChao Yu 
51264c24ecbSTomohiro Kusumi 	d->inode = inode;
513f2470371SChao Yu 	d->max = entry_cnt;
514f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
515f2470371SChao Yu 	d->bitmap = t;
516f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
517f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
518f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5197b3cd7d6SJaegeuk Kim }
5207b3cd7d6SJaegeuk Kim 
521dbe6a5ffSJaegeuk Kim /*
522dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
523dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
524dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
52539a53e0cSJaegeuk Kim  */
526dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
527dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
528266e97a8SJaegeuk Kim enum {
529266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
530266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
531266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
532266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5334f4124d0SChao Yu 					 * by get_data_block.
53439a53e0cSJaegeuk Kim 					 */
535266e97a8SJaegeuk Kim };
536266e97a8SJaegeuk Kim 
5377735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5387735730dSChao Yu 
539af033b2aSChao Yu /* maximum retry quota flush count */
540af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
541af033b2aSChao Yu 
542a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
54339a53e0cSJaegeuk Kim 
544817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
545817202d9SChao Yu 
54639a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
54713054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
54813054c54SChao Yu 
54913054c54SChao Yu /* number of extent info in extent cache we try to shrink */
55013054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
551c11abd1aSJaegeuk Kim 
55254c2258cSChao Yu struct rb_entry {
55354c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
55454c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
55554c2258cSChao Yu 	unsigned int len;		/* length of the entry */
55654c2258cSChao Yu };
55754c2258cSChao Yu 
55839a53e0cSJaegeuk Kim struct extent_info {
55939a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
560111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
56154c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
56239a53e0cSJaegeuk Kim };
56339a53e0cSJaegeuk Kim 
56413054c54SChao Yu struct extent_node {
565c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
56613054c54SChao Yu 	struct extent_info ei;		/* extent info */
56754c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
568201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
56913054c54SChao Yu };
57013054c54SChao Yu 
57113054c54SChao Yu struct extent_tree {
57213054c54SChao Yu 	nid_t ino;			/* inode number */
5734dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
57462c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5753e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
576137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
57713054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
57868e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
579b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
58013054c54SChao Yu };
58113054c54SChao Yu 
58239a53e0cSJaegeuk Kim /*
583003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
584003a3e1dSJaegeuk Kim  *
585003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
586003a3e1dSJaegeuk Kim  */
587003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
588003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5897f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5907f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5917f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
592003a3e1dSJaegeuk Kim 
593003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
594003a3e1dSJaegeuk Kim 	block_t m_pblk;
595003a3e1dSJaegeuk Kim 	block_t m_lblk;
596003a3e1dSJaegeuk Kim 	unsigned int m_len;
597003a3e1dSJaegeuk Kim 	unsigned int m_flags;
598da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
599c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
600d5097be5SHyunchul Lee 	int m_seg_type;
601f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
602003a3e1dSJaegeuk Kim };
603003a3e1dSJaegeuk Kim 
604e2b4e2bcSChao Yu /* for flag in get_data_block */
605f2220c7fSQiuyang Sun enum {
606f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
607f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
608f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6090a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
610f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
611f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
612c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
613f2220c7fSQiuyang Sun };
614e2b4e2bcSChao Yu 
615003a3e1dSJaegeuk Kim /*
61639a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
61739a53e0cSJaegeuk Kim  */
61839a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
619354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
620cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
621e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
62226787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
623b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
62453fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
62539a53e0cSJaegeuk Kim 
626797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
627797c1cb5SChao Yu 
628b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
629b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
630b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
631b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
632b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
633b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
634cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
635cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
636cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
637e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
638e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
63926787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
64026787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
641b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
642b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
643b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
644cde4de12SJaegeuk Kim 
645ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
646ab9fa662SJaegeuk Kim 
6472ef79ecbSChao Yu enum {
6482ef79ecbSChao Yu 	GC_FAILURE_PIN,
6492ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6502ef79ecbSChao Yu 	MAX_GC_FAILURE
6512ef79ecbSChao Yu };
6522ef79ecbSChao Yu 
65339a53e0cSJaegeuk Kim struct f2fs_inode_info {
65439a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
65539a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
65639a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
65738431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6581ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6592ef79ecbSChao Yu 	/* for gc failure statistic */
6602ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6616666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
66239a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
66339a53e0cSJaegeuk Kim 
66439a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
66539a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
666d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
667204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
66839a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
66939a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
67088c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
671b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
67239a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
67326de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
67488b88a66SJaegeuk Kim 
6750abd675eSChao Yu #ifdef CONFIG_QUOTA
6760abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6770abd675eSChao Yu 
6780abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6790abd675eSChao Yu 	qsize_t i_reserved_quota;
6800abd675eSChao Yu #endif
6810f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6820f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
68357864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
68488b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6857a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
68688b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6873e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
688b2532c69SChao Yu 
689b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
690b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
6915a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
69227161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
693f2470371SChao Yu 
6947a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6955c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6966afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
69724b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
69824b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
69939a53e0cSJaegeuk Kim };
70039a53e0cSJaegeuk Kim 
70139a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
702bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
70339a53e0cSJaegeuk Kim {
704bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
705bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
706bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
70739a53e0cSJaegeuk Kim }
70839a53e0cSJaegeuk Kim 
70939a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
71039a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
71139a53e0cSJaegeuk Kim {
71239a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7134d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
71439a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
71539a53e0cSJaegeuk Kim }
71639a53e0cSJaegeuk Kim 
717429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
718429511cdSChao Yu 						u32 blk, unsigned int len)
719429511cdSChao Yu {
720429511cdSChao Yu 	ei->fofs = fofs;
721429511cdSChao Yu 	ei->blk = blk;
722429511cdSChao Yu 	ei->len = len;
723429511cdSChao Yu }
724429511cdSChao Yu 
725004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
72635ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
727004b6862SChao Yu {
7289a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
72935ec7d57SChao Yu 		(back->len + front->len <= max_len);
730004b6862SChao Yu }
731004b6862SChao Yu 
732004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
73335ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
734004b6862SChao Yu {
73535ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
736004b6862SChao Yu }
737004b6862SChao Yu 
738004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
73935ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
740004b6862SChao Yu {
74135ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
742004b6862SChao Yu }
743004b6862SChao Yu 
744429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
745429511cdSChao Yu 						struct extent_info *front)
746429511cdSChao Yu {
747429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
748429511cdSChao Yu 			back->blk + back->len == front->blk);
749429511cdSChao Yu }
750429511cdSChao Yu 
751429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
752429511cdSChao Yu 						struct extent_info *back)
753429511cdSChao Yu {
754429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
755429511cdSChao Yu }
756429511cdSChao Yu 
757429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
758429511cdSChao Yu 						struct extent_info *front)
759429511cdSChao Yu {
760429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
761429511cdSChao Yu }
762429511cdSChao Yu 
763cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
764b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
765b430f726SZhikang Zhang 						struct extent_node *en)
7664abd3f5aSChao Yu {
767205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7684abd3f5aSChao Yu 		et->largest = en->ei;
769b430f726SZhikang Zhang 		et->largest_updated = true;
770205b9822SJaegeuk Kim 	}
7714abd3f5aSChao Yu }
7724abd3f5aSChao Yu 
7739a4ffdf5SChao Yu /*
7749a4ffdf5SChao Yu  * For free nid management
7759a4ffdf5SChao Yu  */
7769a4ffdf5SChao Yu enum nid_state {
7779a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7789a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7799a4ffdf5SChao Yu 	MAX_NID_STATE,
780b8559dc2SChao Yu };
781b8559dc2SChao Yu 
78239a53e0cSJaegeuk Kim struct f2fs_nm_info {
78339a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
78439a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
78504d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
78639a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
787cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
788ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7892304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
79039a53e0cSJaegeuk Kim 
79139a53e0cSJaegeuk Kim 	/* NAT cache management */
79239a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
793309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
794b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
79539a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
79622969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
797309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
798aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
79922ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
80039a53e0cSJaegeuk Kim 
80139a53e0cSJaegeuk Kim 	/* free node ids management */
8028a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
8039a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
8049a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
805b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
80639a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
807bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8084ac91242SChao Yu 	unsigned char *nat_block_bitmap;
809586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
81039a53e0cSJaegeuk Kim 
81139a53e0cSJaegeuk Kim 	/* for checkpoint */
81239a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
81322ad0b6aSJaegeuk Kim 
81422ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
81522ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
81622ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
81722ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
818599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
819599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
820599a09b2SChao Yu #endif
82139a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
82239a53e0cSJaegeuk Kim };
82339a53e0cSJaegeuk Kim 
82439a53e0cSJaegeuk Kim /*
82539a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
82639a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
82739a53e0cSJaegeuk Kim  * by the data offset in a file.
82839a53e0cSJaegeuk Kim  */
82939a53e0cSJaegeuk Kim struct dnode_of_data {
83039a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
83139a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
83239a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
83339a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
83439a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
83539a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
83693bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8373cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8383cf45747SChao Yu 	char max_level;			/* level of current page located */
83939a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
84039a53e0cSJaegeuk Kim };
84139a53e0cSJaegeuk Kim 
84239a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
84339a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
84439a53e0cSJaegeuk Kim {
845d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
84639a53e0cSJaegeuk Kim 	dn->inode = inode;
84739a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
84839a53e0cSJaegeuk Kim 	dn->node_page = npage;
84939a53e0cSJaegeuk Kim 	dn->nid = nid;
85039a53e0cSJaegeuk Kim }
85139a53e0cSJaegeuk Kim 
85239a53e0cSJaegeuk Kim /*
85339a53e0cSJaegeuk Kim  * For SIT manager
85439a53e0cSJaegeuk Kim  *
85539a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
85639a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
85739a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
85839a53e0cSJaegeuk Kim  * respectively.
85939a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
86039a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
86139a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
86239a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
86339a53e0cSJaegeuk Kim  * data and 8 for node logs.
86439a53e0cSJaegeuk Kim  */
86539a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
86639a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
86739a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
86839a53e0cSJaegeuk Kim 
86939a53e0cSJaegeuk Kim enum {
87039a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
87139a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
87239a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
87339a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
87439a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
87539a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
87638aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
87739a53e0cSJaegeuk Kim };
87839a53e0cSJaegeuk Kim 
8796b4afdd7SJaegeuk Kim struct flush_cmd {
8806b4afdd7SJaegeuk Kim 	struct completion wait;
881721bd4d5SGu Zheng 	struct llist_node llnode;
88239d787beSChao Yu 	nid_t ino;
8836b4afdd7SJaegeuk Kim 	int ret;
8846b4afdd7SJaegeuk Kim };
8856b4afdd7SJaegeuk Kim 
886a688b9d9SGu Zheng struct flush_cmd_control {
887a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
888a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8898b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
89072691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
891721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
892721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
893a688b9d9SGu Zheng };
894a688b9d9SGu Zheng 
89539a53e0cSJaegeuk Kim struct f2fs_sm_info {
89639a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
89739a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
89839a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
89939a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
90039a53e0cSJaegeuk Kim 
9012b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
9022b60311dSChao Yu 
90339a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
90439a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
90539a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
90639a53e0cSJaegeuk Kim 
90739a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
90839a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
90939a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
91039a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
91181eb8d6eSJaegeuk Kim 
91281eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
91381eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9147fd9e544SJaegeuk Kim 
915bba681cbSJaegeuk Kim 	/* for batched trimming */
916bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
917bba681cbSJaegeuk Kim 
918184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
919184a5cd2SChao Yu 
920216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
921216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
922c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
923853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
924ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
925a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
9266b4afdd7SJaegeuk Kim 
9276b4afdd7SJaegeuk Kim 	/* for flush command control */
928b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
929a688b9d9SGu Zheng 
9300b54fb84SJaegeuk Kim 	/* for discard command control */
9310b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
93239a53e0cSJaegeuk Kim };
93339a53e0cSJaegeuk Kim 
93439a53e0cSJaegeuk Kim /*
93539a53e0cSJaegeuk Kim  * For superblock
93639a53e0cSJaegeuk Kim  */
93739a53e0cSJaegeuk Kim /*
93839a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
93939a53e0cSJaegeuk Kim  *
94039a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
94139a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
94239a53e0cSJaegeuk Kim  */
94336951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
94439a53e0cSJaegeuk Kim enum count_type {
94539a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
946c227f912SChao Yu 	F2FS_DIRTY_DATA,
9472c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
94839a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
94939a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9508dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9510f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
95236951b38SChao Yu 	F2FS_WB_CP_DATA,
95336951b38SChao Yu 	F2FS_WB_DATA,
9545f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
9555f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
9565f9abab4SJaegeuk Kim 	F2FS_RD_META,
95702b16d0aSChao Yu 	F2FS_DIO_WRITE,
95802b16d0aSChao Yu 	F2FS_DIO_READ,
95939a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
96039a53e0cSJaegeuk Kim };
96139a53e0cSJaegeuk Kim 
96239a53e0cSJaegeuk Kim /*
963e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
96439a53e0cSJaegeuk Kim  * The available types are:
96539a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
96639a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
96739a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
96839a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
96939a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
97039a53e0cSJaegeuk Kim  *			with waiting the bio's completion
97139a53e0cSJaegeuk Kim  * ...			Only can be used with META.
97239a53e0cSJaegeuk Kim  */
9737d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
97439a53e0cSJaegeuk Kim enum page_type {
97539a53e0cSJaegeuk Kim 	DATA,
97639a53e0cSJaegeuk Kim 	NODE,
97739a53e0cSJaegeuk Kim 	META,
97839a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
97939a53e0cSJaegeuk Kim 	META_FLUSH,
9808ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9818ce67cb0SJaegeuk Kim 	INMEM_DROP,
9828c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
98328bc106bSChao Yu 	INMEM_REVOKE,
9848ce67cb0SJaegeuk Kim 	IPU,
9858ce67cb0SJaegeuk Kim 	OPU,
98639a53e0cSJaegeuk Kim };
98739a53e0cSJaegeuk Kim 
988a912b54dSJaegeuk Kim enum temp_type {
989a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
990a912b54dSJaegeuk Kim 	WARM,
991a912b54dSJaegeuk Kim 	COLD,
992a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
993a912b54dSJaegeuk Kim };
994a912b54dSJaegeuk Kim 
995cc15620bSJaegeuk Kim enum need_lock_type {
996cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
997cc15620bSJaegeuk Kim 	LOCK_DONE,
998cc15620bSJaegeuk Kim 	LOCK_RETRY,
999cc15620bSJaegeuk Kim };
1000cc15620bSJaegeuk Kim 
1001a5fd5050SChao Yu enum cp_reason_type {
1002a5fd5050SChao Yu 	CP_NO_NEEDED,
1003a5fd5050SChao Yu 	CP_NON_REGULAR,
1004a5fd5050SChao Yu 	CP_HARDLINK,
1005a5fd5050SChao Yu 	CP_SB_NEED_CP,
1006a5fd5050SChao Yu 	CP_WRONG_PINO,
1007a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1008a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1009a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1010a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10110a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1012a5fd5050SChao Yu };
1013a5fd5050SChao Yu 
1014b0af6d49SChao Yu enum iostat_type {
1015b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
1016b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
1017b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1018b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1019b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1020b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1021b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1022b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1023b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1024b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1025b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1026b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1027b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1028b0af6d49SChao Yu 	NR_IO_TYPE,
1029b0af6d49SChao Yu };
1030b0af6d49SChao Yu 
1031458e6197SJaegeuk Kim struct f2fs_io_info {
103205ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
103339d787beSChao Yu 	nid_t ino;		/* inode number */
1034458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1035a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
103604d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1037ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10387a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
103928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
104005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10414375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1042fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1043d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1044cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1045fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10460833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1047fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
1048b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1049578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
10507735730dSChao Yu 	unsigned char version;		/* version of the node */
1051458e6197SJaegeuk Kim };
1052458e6197SJaegeuk Kim 
105368afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10541ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1055458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10561ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10571ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1058458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1059df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1060fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1061fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10621ff7bd3bSJaegeuk Kim };
10631ff7bd3bSJaegeuk Kim 
10643c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10653c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10663c62be17SJaegeuk Kim struct f2fs_dev_info {
10673c62be17SJaegeuk Kim 	struct block_device *bdev;
10683c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10693c62be17SJaegeuk Kim 	unsigned int total_segments;
10703c62be17SJaegeuk Kim 	block_t start_blk;
10713c62be17SJaegeuk Kim 	block_t end_blk;
10723c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10733c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10743c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10753c62be17SJaegeuk Kim #endif
10763c62be17SJaegeuk Kim };
10773c62be17SJaegeuk Kim 
1078c227f912SChao Yu enum inode_type {
1079c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1080c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10810f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
108257864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1083c227f912SChao Yu 	NR_INODE_TYPE,
1084c227f912SChao Yu };
1085c227f912SChao Yu 
108667298804SChao Yu /* for inner inode cache management */
108767298804SChao Yu struct inode_management {
108867298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
108967298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
109067298804SChao Yu 	struct list_head ino_list;		/* inode list head */
109167298804SChao Yu 	unsigned long ino_num;			/* number of entries */
109267298804SChao Yu };
109367298804SChao Yu 
1094caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1095caf0047eSChao Yu enum {
1096caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1097caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1098caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1099caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1100df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1101bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
110283a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
11031378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
11044354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1105*db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1106af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1107af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1108af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
1109caf0047eSChao Yu };
1110caf0047eSChao Yu 
11116beceb54SJaegeuk Kim enum {
11126beceb54SJaegeuk Kim 	CP_TIME,
1113d0239e1bSJaegeuk Kim 	REQ_TIME,
1114a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1115a7d10cf3SSahitya Tummala 	GC_TIME,
11164354994fSDaniel Rosenberg 	DISABLE_TIME,
111703f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
11186beceb54SJaegeuk Kim 	MAX_TIME,
11196beceb54SJaegeuk Kim };
11206beceb54SJaegeuk Kim 
11210cdd3195SHyunchul Lee enum {
11225b0e9539SJaegeuk Kim 	GC_NORMAL,
11235b0e9539SJaegeuk Kim 	GC_IDLE_CB,
11245b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
11255b0e9539SJaegeuk Kim 	GC_URGENT,
11265b0e9539SJaegeuk Kim };
11275b0e9539SJaegeuk Kim 
11285b0e9539SJaegeuk Kim enum {
11290cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
11300cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1131f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
11320cdd3195SHyunchul Lee };
11330cdd3195SHyunchul Lee 
113407939627SJaegeuk Kim enum {
113507939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
113607939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
113707939627SJaegeuk Kim };
113807939627SJaegeuk Kim 
113993cf93f1SJunling Zheng enum fsync_mode {
114093cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
114193cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1142d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
114393cf93f1SJunling Zheng };
114493cf93f1SJunling Zheng 
1145ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1146ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1147ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1148ff62af20SSheng Yong #else
1149ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1150ff62af20SSheng Yong #endif
1151ff62af20SSheng Yong 
115239a53e0cSJaegeuk Kim struct f2fs_sb_info {
115339a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11545e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
115539a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1156846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1157e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1158fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1159853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
116039a53e0cSJaegeuk Kim 
1161178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1162178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1163178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1164178053e2SDamien Le Moal #endif
1165178053e2SDamien Le Moal 
116639a53e0cSJaegeuk Kim 	/* for node-related operations */
116739a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
116839a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
116939a53e0cSJaegeuk Kim 
117039a53e0cSJaegeuk Kim 	/* for segment-related operations */
117139a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11721ff7bd3bSJaegeuk Kim 
11731ff7bd3bSJaegeuk Kim 	/* for bio operations */
1174a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1175107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1176107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11770a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
117839a53e0cSJaegeuk Kim 
117939a53e0cSJaegeuk Kim 	/* for checkpoint */
118039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11818508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1182aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
118339a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
118439936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1185b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1186b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
118759c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1188fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11896beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11906beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
119139a53e0cSJaegeuk Kim 
119267298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11936451e041SJaegeuk Kim 
119450fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
119550fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
119650fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
119750fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
119850fa53ecSChao Yu 
11996451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
12000d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
120139a53e0cSJaegeuk Kim 
1202c227f912SChao Yu 	/* for inode management */
1203c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1204c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
120539a53e0cSJaegeuk Kim 
120613054c54SChao Yu 	/* for extent tree cache */
120713054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
12085e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
120913054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
121013054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
12117441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1212137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
121374fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
121413054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
121513054c54SChao Yu 
121639a53e0cSJaegeuk Kim 	/* basic filesystem units */
121739a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
121839a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
121939a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
122039a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
122139a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
122239a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
122339a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
122439a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
122539a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
122639a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
122739a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
122839a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
122939a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1230e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1231ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1232f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
123339a53e0cSJaegeuk Kim 
123439a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
123539a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1236a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
123739a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1238daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
123980d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1240daeb433eSChao Yu 
12414354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
12424354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
12434354994fSDaniel Rosenberg 
1244292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1245292c196aSChao Yu 
124639a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1247523be8a6SJaegeuk Kim 
1248523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
124935782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
125041382ec4SJaegeuk Kim 	/* # of allocated blocks */
125141382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
125239a53e0cSJaegeuk Kim 
1253687de7f1SJaegeuk Kim 	/* writeback control */
1254c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1255687de7f1SJaegeuk Kim 
1256513c5f37SJaegeuk Kim 	/* valid inode count */
1257513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1258513c5f37SJaegeuk Kim 
125939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
126039a53e0cSJaegeuk Kim 
126139a53e0cSJaegeuk Kim 	/* for cleaning operations */
126239a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
126339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12645ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12655b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1266e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
12672ef79ecbSChao Yu 	/* for skip statistic */
12682ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
12696f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
127039a53e0cSJaegeuk Kim 
12711ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12721ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12731ad71a27SJaegeuk Kim 
1274b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1275b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1276e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1277e3080b01SChao Yu 	unsigned int migration_granularity;
1278b1c57c1cSJaegeuk Kim 
127939a53e0cSJaegeuk Kim 	/*
128039a53e0cSJaegeuk Kim 	 * for stat information.
128139a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
128239a53e0cSJaegeuk Kim 	 */
128335b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
128439a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1285b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
128639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
128739a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1288b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12895b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12905b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12915b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12925b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1293d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
129403e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
129503e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
129626a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1297648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
129826a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1299648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
130039a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
1301274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1302274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
130333fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
130435b09d82SNamjae Jeon #endif
130539a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1306b59d0baeSNamjae Jeon 
1307b0af6d49SChao Yu 	/* For app/fs IO statistics */
1308b0af6d49SChao Yu 	spinlock_t iostat_lock;
1309b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1310b0af6d49SChao Yu 	bool iostat_enable;
1311b0af6d49SChao Yu 
1312b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1313b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1314b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
13152658e50dSJaegeuk Kim 
13162658e50dSJaegeuk Kim 	/* For shrinker support */
13172658e50dSJaegeuk Kim 	struct list_head s_list;
13183c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
13193c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
13201228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
13211228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
13222658e50dSJaegeuk Kim 	struct mutex umount_mutex;
13232658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
13248f1dbbbbSShuoran Liu 
13258f1dbbbbSShuoran Liu 	/* For write statistics */
13268f1dbbbbSShuoran Liu 	u64 sectors_written_start;
13278f1dbbbbSShuoran Liu 	u64 kbytes_written;
132843b6573bSKeith Mok 
132943b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
133043b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
13311ecc0c5cSChao Yu 
1332704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1333704956ecSChao Yu 	__u32 s_chksum_seed;
133439a53e0cSJaegeuk Kim };
133539a53e0cSJaegeuk Kim 
133602b16d0aSChao Yu struct f2fs_private_dio {
133702b16d0aSChao Yu 	struct inode *inode;
133802b16d0aSChao Yu 	void *orig_private;
133902b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
134002b16d0aSChao Yu 	bool write;
134102b16d0aSChao Yu };
134202b16d0aSChao Yu 
13431ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
134455523519SChao Yu #define f2fs_show_injection_info(type)					\
134522d7ea13SChao Yu 	printk_ratelimited("%sF2FS-fs : inject %s in %s of %pF\n",	\
13462d3a5856SGao Xiang 		KERN_INFO, f2fs_fault_name[type],			\
134755523519SChao Yu 		__func__, __builtin_return_address(0))
13481ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13491ecc0c5cSChao Yu {
135063189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
13511ecc0c5cSChao Yu 
13521ecc0c5cSChao Yu 	if (!ffi->inject_rate)
13531ecc0c5cSChao Yu 		return false;
13541ecc0c5cSChao Yu 
13551ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
13561ecc0c5cSChao Yu 		return false;
13571ecc0c5cSChao Yu 
13581ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
13591ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
13601ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
13611ecc0c5cSChao Yu 		return true;
13621ecc0c5cSChao Yu 	}
13631ecc0c5cSChao Yu 	return false;
13641ecc0c5cSChao Yu }
13657fa750a1SArnd Bergmann #else
13667fa750a1SArnd Bergmann #define f2fs_show_injection_info(type) do { } while (0)
13677fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13687fa750a1SArnd Bergmann {
13697fa750a1SArnd Bergmann 	return false;
13707fa750a1SArnd Bergmann }
13711ecc0c5cSChao Yu #endif
13721ecc0c5cSChao Yu 
13738f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13748f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13758f1dbbbbSShuoran Liu  */
13768f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1377dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
137868afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13798f1dbbbbSShuoran Liu 
13806beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13816beceb54SJaegeuk Kim {
1382a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1383a7d10cf3SSahitya Tummala 
1384a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1385a7d10cf3SSahitya Tummala 
1386a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1387a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1388a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1389a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1390a7d10cf3SSahitya Tummala 	}
13916beceb54SJaegeuk Kim }
13926beceb54SJaegeuk Kim 
13936beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13946beceb54SJaegeuk Kim {
13956bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13966beceb54SJaegeuk Kim 
13976beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13986beceb54SJaegeuk Kim }
13996beceb54SJaegeuk Kim 
1400a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1401a7d10cf3SSahitya Tummala 						int type)
1402a7d10cf3SSahitya Tummala {
1403a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1404a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1405a7d10cf3SSahitya Tummala 	long delta;
1406a7d10cf3SSahitya Tummala 
1407a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1408a7d10cf3SSahitya Tummala 	if (delta > 0)
1409a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1410a7d10cf3SSahitya Tummala 
1411a7d10cf3SSahitya Tummala 	return wait_ms;
1412a7d10cf3SSahitya Tummala }
1413a7d10cf3SSahitya Tummala 
141439a53e0cSJaegeuk Kim /*
141539a53e0cSJaegeuk Kim  * Inline functions
141639a53e0cSJaegeuk Kim  */
1417416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1418704956ecSChao Yu 			      const void *address, unsigned int length)
1419704956ecSChao Yu {
1420704956ecSChao Yu 	struct {
1421704956ecSChao Yu 		struct shash_desc shash;
1422704956ecSChao Yu 		char ctx[4];
1423704956ecSChao Yu 	} desc;
1424704956ecSChao Yu 	int err;
1425704956ecSChao Yu 
1426704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1427704956ecSChao Yu 
1428704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1429704956ecSChao Yu 	desc.shash.flags = 0;
1430704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1431704956ecSChao Yu 
1432704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1433704956ecSChao Yu 	BUG_ON(err);
1434704956ecSChao Yu 
1435704956ecSChao Yu 	return *(u32 *)desc.ctx;
1436704956ecSChao Yu }
1437704956ecSChao Yu 
1438416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1439416d2dbbSChao Yu 			   unsigned int length)
1440416d2dbbSChao Yu {
1441416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1442416d2dbbSChao Yu }
1443416d2dbbSChao Yu 
1444416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1445416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1446416d2dbbSChao Yu {
1447416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1448416d2dbbSChao Yu }
1449416d2dbbSChao Yu 
1450416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1451416d2dbbSChao Yu 			      const void *address, unsigned int length)
1452416d2dbbSChao Yu {
1453416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1454416d2dbbSChao Yu }
1455416d2dbbSChao Yu 
145639a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
145739a53e0cSJaegeuk Kim {
145839a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
145939a53e0cSJaegeuk Kim }
146039a53e0cSJaegeuk Kim 
146139a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
146239a53e0cSJaegeuk Kim {
146339a53e0cSJaegeuk Kim 	return sb->s_fs_info;
146439a53e0cSJaegeuk Kim }
146539a53e0cSJaegeuk Kim 
14664081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
14674081363fSJaegeuk Kim {
14684081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
14694081363fSJaegeuk Kim }
14704081363fSJaegeuk Kim 
14714081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
14724081363fSJaegeuk Kim {
14734081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
14744081363fSJaegeuk Kim }
14754081363fSJaegeuk Kim 
14764081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
14774081363fSJaegeuk Kim {
14784081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
14794081363fSJaegeuk Kim }
14804081363fSJaegeuk Kim 
148139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
148239a53e0cSJaegeuk Kim {
148339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
148439a53e0cSJaegeuk Kim }
148539a53e0cSJaegeuk Kim 
148639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
148739a53e0cSJaegeuk Kim {
148839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
148939a53e0cSJaegeuk Kim }
149039a53e0cSJaegeuk Kim 
149145590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
149245590710SGu Zheng {
149345590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
149445590710SGu Zheng }
149545590710SGu Zheng 
149658bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
149758bfaf44SJaegeuk Kim {
149858bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
149958bfaf44SJaegeuk Kim }
150058bfaf44SJaegeuk Kim 
150139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
150239a53e0cSJaegeuk Kim {
150339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
150439a53e0cSJaegeuk Kim }
150539a53e0cSJaegeuk Kim 
150639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
150739a53e0cSJaegeuk Kim {
150839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
150939a53e0cSJaegeuk Kim }
151039a53e0cSJaegeuk Kim 
151139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
151239a53e0cSJaegeuk Kim {
151339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
151439a53e0cSJaegeuk Kim }
151539a53e0cSJaegeuk Kim 
151639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
151739a53e0cSJaegeuk Kim {
151839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
151939a53e0cSJaegeuk Kim }
152039a53e0cSJaegeuk Kim 
152139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
152239a53e0cSJaegeuk Kim {
152339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
152439a53e0cSJaegeuk Kim }
152539a53e0cSJaegeuk Kim 
15269df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
15279df27d98SGu Zheng {
15289df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
15299df27d98SGu Zheng }
15309df27d98SGu Zheng 
15314ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
15324ef51a8fSJaegeuk Kim {
15334ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
15344ef51a8fSJaegeuk Kim }
15354ef51a8fSJaegeuk Kim 
1536caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
153739a53e0cSJaegeuk Kim {
1538fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
153939a53e0cSJaegeuk Kim }
154039a53e0cSJaegeuk Kim 
1541caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
154239a53e0cSJaegeuk Kim {
1543fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1544caf0047eSChao Yu }
1545caf0047eSChao Yu 
1546caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1547caf0047eSChao Yu {
1548fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
154939a53e0cSJaegeuk Kim }
155039a53e0cSJaegeuk Kim 
1551d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1552d71b5564SJaegeuk Kim {
1553d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1554d71b5564SJaegeuk Kim }
1555d71b5564SJaegeuk Kim 
1556ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1557ea676733SJaegeuk Kim {
1558ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1559ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1560ea676733SJaegeuk Kim 	return 0;
1561ea676733SJaegeuk Kim }
1562ea676733SJaegeuk Kim 
1563ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1564ced2c7eaSKinglong Mee {
1565ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1566ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1567ced2c7eaSKinglong Mee }
1568ced2c7eaSKinglong Mee 
1569aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
157025ca923bSJaegeuk Kim {
157125ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1572aaec2b1dSChao Yu 
157325ca923bSJaegeuk Kim 	return ckpt_flags & f;
157425ca923bSJaegeuk Kim }
157525ca923bSJaegeuk Kim 
1576aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
157725ca923bSJaegeuk Kim {
1578aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1579aaec2b1dSChao Yu }
1580aaec2b1dSChao Yu 
1581aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1582aaec2b1dSChao Yu {
1583aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1584aaec2b1dSChao Yu 
1585aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
158625ca923bSJaegeuk Kim 	ckpt_flags |= f;
158725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
158825ca923bSJaegeuk Kim }
158925ca923bSJaegeuk Kim 
1590aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
159125ca923bSJaegeuk Kim {
1592d1aa2453SChao Yu 	unsigned long flags;
1593d1aa2453SChao Yu 
1594d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1595aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1596d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1597aaec2b1dSChao Yu }
1598aaec2b1dSChao Yu 
1599aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1600aaec2b1dSChao Yu {
1601aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1602aaec2b1dSChao Yu 
1603aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
160425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
160525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
160625ca923bSJaegeuk Kim }
160725ca923bSJaegeuk Kim 
1608aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1609aaec2b1dSChao Yu {
1610d1aa2453SChao Yu 	unsigned long flags;
1611d1aa2453SChao Yu 
1612d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1613aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1614d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1615aaec2b1dSChao Yu }
1616aaec2b1dSChao Yu 
161722ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
161822ad0b6aSJaegeuk Kim {
1619d1aa2453SChao Yu 	unsigned long flags;
1620d1aa2453SChao Yu 
1621a742fd41SJaegeuk Kim 	/*
1622a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
1623a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
1624a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
1625a742fd41SJaegeuk Kim 	 */
162622ad0b6aSJaegeuk Kim 
162722ad0b6aSJaegeuk Kim 	if (lock)
1628d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
162922ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
16305222595dSJaegeuk Kim 	kvfree(NM_I(sbi)->nat_bits);
163122ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
163222ad0b6aSJaegeuk Kim 	if (lock)
1633d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
163422ad0b6aSJaegeuk Kim }
163522ad0b6aSJaegeuk Kim 
163622ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
163722ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
163822ad0b6aSJaegeuk Kim {
163922ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
164022ad0b6aSJaegeuk Kim 
1641c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
164222ad0b6aSJaegeuk Kim }
164322ad0b6aSJaegeuk Kim 
1644e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
164539a53e0cSJaegeuk Kim {
1646b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
164739a53e0cSJaegeuk Kim }
164839a53e0cSJaegeuk Kim 
1649cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1650cc15620bSJaegeuk Kim {
1651cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1652cc15620bSJaegeuk Kim }
1653cc15620bSJaegeuk Kim 
1654e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
165539a53e0cSJaegeuk Kim {
1656b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
165739936837SJaegeuk Kim }
165839936837SJaegeuk Kim 
1659e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
166039936837SJaegeuk Kim {
1661b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
166239936837SJaegeuk Kim }
166339936837SJaegeuk Kim 
1664e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
166539936837SJaegeuk Kim {
1666b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
166739a53e0cSJaegeuk Kim }
166839a53e0cSJaegeuk Kim 
1669119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1670119ee914SJaegeuk Kim {
1671119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1672119ee914SJaegeuk Kim 
1673119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1674119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1675119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1676119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1677119ee914SJaegeuk Kim 	return reason;
1678119ee914SJaegeuk Kim }
1679119ee914SJaegeuk Kim 
1680119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1681119ee914SJaegeuk Kim {
1682c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1683119ee914SJaegeuk Kim }
1684119ee914SJaegeuk Kim 
1685119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1686119ee914SJaegeuk Kim {
1687aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1688aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1689119ee914SJaegeuk Kim }
1690119ee914SJaegeuk Kim 
169139a53e0cSJaegeuk Kim /*
169239a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
169339a53e0cSJaegeuk Kim  */
169439a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
169539a53e0cSJaegeuk Kim {
16960eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16970eb0adadSChao Yu 
1698000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
169939a53e0cSJaegeuk Kim }
170039a53e0cSJaegeuk Kim 
17014bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
17024bc8e9bcSChao Yu {
17034bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
17044bc8e9bcSChao Yu }
17054bc8e9bcSChao Yu 
1706d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1707a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
17087c2e5963SJaegeuk Kim {
1709d8a9a229SJaegeuk Kim 	if (!inode)
1710d8a9a229SJaegeuk Kim 		return true;
17117c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
17127c2e5963SJaegeuk Kim 		return false;
1713d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1714d8a9a229SJaegeuk Kim 		return true;
171563189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
17167c2e5963SJaegeuk Kim 		return true;
171763189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
171863189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
17197c2e5963SJaegeuk Kim 		return true;
1720a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1721162b27aeSJaegeuk Kim 		return true;
17227c2e5963SJaegeuk Kim 	return false;
17237c2e5963SJaegeuk Kim }
17247c2e5963SJaegeuk Kim 
17250abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
17260abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
172746008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
172839a53e0cSJaegeuk Kim {
17290abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1730daeb433eSChao Yu 	block_t avail_user_block_count;
17310abd675eSChao Yu 	int ret;
17320abd675eSChao Yu 
17330abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
17340abd675eSChao Yu 	if (ret)
17350abd675eSChao Yu 		return ret;
1736dd11a5dfSJaegeuk Kim 
173755523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
173855523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
17390abd675eSChao Yu 		release = *count;
17400abd675eSChao Yu 		goto enospc;
174155523519SChao Yu 	}
17427fa750a1SArnd Bergmann 
1743dd11a5dfSJaegeuk Kim 	/*
1744dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1745dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1746dd11a5dfSJaegeuk Kim 	 */
1747dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
174839a53e0cSJaegeuk Kim 
174939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17502555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
175180d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
175280d42145SYunlong Song 					sbi->current_reserved_blocks;
17537e65be49SJaegeuk Kim 
1754a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
175563189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
17564354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
17574354994fSDaniel Rosenberg 		avail_user_block_count -= sbi->unusable_block_count;
1758daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1759daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
17607e65be49SJaegeuk Kim 		if (diff > *count)
17617e65be49SJaegeuk Kim 			diff = *count;
1762dd11a5dfSJaegeuk Kim 		*count -= diff;
17630abd675eSChao Yu 		release = diff;
17647e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
176546008c6dSChao Yu 		if (!*count) {
176639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
17670abd675eSChao Yu 			goto enospc;
176839a53e0cSJaegeuk Kim 		}
176946008c6dSChao Yu 	}
177039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
177141382ec4SJaegeuk Kim 
177236b877afSDaniel Rosenberg 	if (unlikely(release)) {
177336b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17740abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
177536b877afSDaniel Rosenberg 	}
17760abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
17770abd675eSChao Yu 	return 0;
17780abd675eSChao Yu 
17790abd675eSChao Yu enospc:
178036b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17810abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
17820abd675eSChao Yu 	return -ENOSPC;
178339a53e0cSJaegeuk Kim }
178439a53e0cSJaegeuk Kim 
1785da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
178639a53e0cSJaegeuk Kim 						struct inode *inode,
17870eb0adadSChao Yu 						block_t count)
178839a53e0cSJaegeuk Kim {
17890eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17900eb0adadSChao Yu 
179139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17929850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17930eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
179439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
179580d42145SYunlong Song 	if (sbi->reserved_blocks &&
179680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
179780d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
179880d42145SYunlong Song 					sbi->current_reserved_blocks + count);
179939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
18000abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
180139a53e0cSJaegeuk Kim }
180239a53e0cSJaegeuk Kim 
180339a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
180439a53e0cSJaegeuk Kim {
180535782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
18067c4abcbeSChao Yu 
180720109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
180820109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
180920109873SChao Yu 			count_type == F2FS_DIRTY_META ||
181020109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
181120109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
1812caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
181339a53e0cSJaegeuk Kim }
181439a53e0cSJaegeuk Kim 
1815a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
181639a53e0cSJaegeuk Kim {
1817204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1818c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1819c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18202c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18212c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
182239a53e0cSJaegeuk Kim }
182339a53e0cSJaegeuk Kim 
182439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
182539a53e0cSJaegeuk Kim {
182635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
182739a53e0cSJaegeuk Kim }
182839a53e0cSJaegeuk Kim 
1829a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
183039a53e0cSJaegeuk Kim {
18315ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
18325ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
18331fe54f9dSJaegeuk Kim 		return;
18341fe54f9dSJaegeuk Kim 
1835204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1836c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1837c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18382c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18392c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
184039a53e0cSJaegeuk Kim }
184139a53e0cSJaegeuk Kim 
1842523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
184339a53e0cSJaegeuk Kim {
184435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
184539a53e0cSJaegeuk Kim }
184639a53e0cSJaegeuk Kim 
1847204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1848f8b2c1f9SJaegeuk Kim {
1849204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1850f8b2c1f9SJaegeuk Kim }
1851f8b2c1f9SJaegeuk Kim 
18525ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
18535ac206cfSNamjae Jeon {
18543519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1855523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1856523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1857523be8a6SJaegeuk Kim 
1858523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
18595ac206cfSNamjae Jeon }
18605ac206cfSNamjae Jeon 
186139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
186239a53e0cSJaegeuk Kim {
18638b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
186439a53e0cSJaegeuk Kim }
186539a53e0cSJaegeuk Kim 
1866f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1867f83a2584SYunlei He {
1868f83a2584SYunlei He 	return sbi->discard_blks;
1869f83a2584SYunlei He }
1870f83a2584SYunlei He 
187139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
187239a53e0cSJaegeuk Kim {
187339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
187439a53e0cSJaegeuk Kim 
187539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
187639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
187739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
187839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
187939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
188039a53e0cSJaegeuk Kim 
188139a53e0cSJaegeuk Kim 	return 0;
188239a53e0cSJaegeuk Kim }
188339a53e0cSJaegeuk Kim 
188455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
188555141486SWanpeng Li {
188655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
188755141486SWanpeng Li }
188855141486SWanpeng Li 
188939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
189039a53e0cSJaegeuk Kim {
189139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18921dbe4152SChangman Lee 	int offset;
18931dbe4152SChangman Lee 
1894199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1895199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1896199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1897199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1898199bc3feSChao Yu 	}
1899199bc3feSChao Yu 
190055141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
19011dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
19021dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
19031dbe4152SChangman Lee 		else
190465b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
19051dbe4152SChangman Lee 	} else {
19061dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
190725ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
190839a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
190939a53e0cSJaegeuk Kim 	}
19101dbe4152SChangman Lee }
191139a53e0cSJaegeuk Kim 
191239a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
191339a53e0cSJaegeuk Kim {
19148508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
191539a53e0cSJaegeuk Kim 
19168508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
191739a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
191839a53e0cSJaegeuk Kim 	return start_addr;
191939a53e0cSJaegeuk Kim }
192039a53e0cSJaegeuk Kim 
19218508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
19228508e44aSJaegeuk Kim {
19238508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
19248508e44aSJaegeuk Kim 
19258508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
19268508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
19278508e44aSJaegeuk Kim 	return start_addr;
19288508e44aSJaegeuk Kim }
19298508e44aSJaegeuk Kim 
19308508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
19318508e44aSJaegeuk Kim {
19328508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
19338508e44aSJaegeuk Kim }
19348508e44aSJaegeuk Kim 
193539a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
193639a53e0cSJaegeuk Kim {
193739a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
193839a53e0cSJaegeuk Kim }
193939a53e0cSJaegeuk Kim 
19400abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1941000519f2SChao Yu 					struct inode *inode, bool is_inode)
194239a53e0cSJaegeuk Kim {
194339a53e0cSJaegeuk Kim 	block_t	valid_block_count;
194439a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
1945af033b2aSChao Yu 	int err;
19460abd675eSChao Yu 
1947af033b2aSChao Yu 	if (is_inode) {
1948af033b2aSChao Yu 		if (inode) {
1949af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
1950af033b2aSChao Yu 			if (err)
1951af033b2aSChao Yu 				return err;
1952af033b2aSChao Yu 		}
1953af033b2aSChao Yu 	} else {
1954af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
1955af033b2aSChao Yu 		if (err)
1956af033b2aSChao Yu 			return err;
19570abd675eSChao Yu 	}
195839a53e0cSJaegeuk Kim 
1959812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1960812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1961812c6056SChao Yu 		goto enospc;
1962812c6056SChao Yu 	}
1963812c6056SChao Yu 
196439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
196539a53e0cSJaegeuk Kim 
19667e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
19677e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
19687e65be49SJaegeuk Kim 
1969a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
197063189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
19714354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
19724354994fSDaniel Rosenberg 		valid_block_count += sbi->unusable_block_count;
19737e65be49SJaegeuk Kim 
19747e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
197539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19760abd675eSChao Yu 		goto enospc;
197739a53e0cSJaegeuk Kim 	}
197839a53e0cSJaegeuk Kim 
1979ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1980cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
198139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19820abd675eSChao Yu 		goto enospc;
198339a53e0cSJaegeuk Kim 	}
198439a53e0cSJaegeuk Kim 
1985ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1986ef86d709SGu Zheng 	sbi->total_valid_block_count++;
198739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
198839a53e0cSJaegeuk Kim 
19890abd675eSChao Yu 	if (inode) {
19900abd675eSChao Yu 		if (is_inode)
19910abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
19920abd675eSChao Yu 		else
19930abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
19940abd675eSChao Yu 	}
19950abd675eSChao Yu 
199641382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19970abd675eSChao Yu 	return 0;
19980abd675eSChao Yu 
19990abd675eSChao Yu enospc:
2000af033b2aSChao Yu 	if (is_inode) {
2001af033b2aSChao Yu 		if (inode)
2002af033b2aSChao Yu 			dquot_free_inode(inode);
2003af033b2aSChao Yu 	} else {
20040abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2005af033b2aSChao Yu 	}
20060abd675eSChao Yu 	return -ENOSPC;
200739a53e0cSJaegeuk Kim }
200839a53e0cSJaegeuk Kim 
200939a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2010000519f2SChao Yu 					struct inode *inode, bool is_inode)
201139a53e0cSJaegeuk Kim {
201239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
201339a53e0cSJaegeuk Kim 
20149850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
20159850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
2016000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
201739a53e0cSJaegeuk Kim 
2018ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2019ef86d709SGu Zheng 	sbi->total_valid_block_count--;
202080d42145SYunlong Song 	if (sbi->reserved_blocks &&
202180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
202280d42145SYunlong Song 		sbi->current_reserved_blocks++;
202339a53e0cSJaegeuk Kim 
202439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
20250abd675eSChao Yu 
2026af033b2aSChao Yu 	if (is_inode)
2027af033b2aSChao Yu 		dquot_free_inode(inode);
2028af033b2aSChao Yu 	else
20290abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
203039a53e0cSJaegeuk Kim }
203139a53e0cSJaegeuk Kim 
203239a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
203339a53e0cSJaegeuk Kim {
20348b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
203539a53e0cSJaegeuk Kim }
203639a53e0cSJaegeuk Kim 
203739a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
203839a53e0cSJaegeuk Kim {
2039513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
204039a53e0cSJaegeuk Kim }
204139a53e0cSJaegeuk Kim 
20420e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
204339a53e0cSJaegeuk Kim {
2044513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
204539a53e0cSJaegeuk Kim }
204639a53e0cSJaegeuk Kim 
2047513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
204839a53e0cSJaegeuk Kim {
2049513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
205039a53e0cSJaegeuk Kim }
205139a53e0cSJaegeuk Kim 
2052a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2053a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2054a56c7c6fSJaegeuk Kim {
205582cf4f13SChao Yu 	struct page *page;
2056cac5a3d8SDongOh Shin 
20577fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
205882cf4f13SChao Yu 		if (!for_write)
205982cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
206082cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
206182cf4f13SChao Yu 		else
206282cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2063c41f3cc3SJaegeuk Kim 		if (page)
2064c41f3cc3SJaegeuk Kim 			return page;
2065c41f3cc3SJaegeuk Kim 
206655523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
206755523519SChao Yu 			f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2068c41f3cc3SJaegeuk Kim 			return NULL;
206955523519SChao Yu 		}
20707fa750a1SArnd Bergmann 	}
20717fa750a1SArnd Bergmann 
2072a56c7c6fSJaegeuk Kim 	if (!for_write)
2073a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2074a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2075a56c7c6fSJaegeuk Kim }
2076a56c7c6fSJaegeuk Kim 
207701eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
207801eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
207901eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
208001eccef7SChao Yu {
208101eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
208201eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
208301eccef7SChao Yu 		return NULL;
208401eccef7SChao Yu 	}
20857fa750a1SArnd Bergmann 
208601eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
208701eccef7SChao Yu }
208801eccef7SChao Yu 
20896e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
20906e2c64adSJaegeuk Kim {
20916e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
20926e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
20936e2c64adSJaegeuk Kim 
20946e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
20956e2c64adSJaegeuk Kim 	kunmap(dst);
20966e2c64adSJaegeuk Kim 	kunmap(src);
20976e2c64adSJaegeuk Kim }
20986e2c64adSJaegeuk Kim 
209939a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
210039a53e0cSJaegeuk Kim {
2101031fa8ccSJaegeuk Kim 	if (!page)
210239a53e0cSJaegeuk Kim 		return;
210339a53e0cSJaegeuk Kim 
210439a53e0cSJaegeuk Kim 	if (unlock) {
21059850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
210639a53e0cSJaegeuk Kim 		unlock_page(page);
210739a53e0cSJaegeuk Kim 	}
210809cbfeafSKirill A. Shutemov 	put_page(page);
210939a53e0cSJaegeuk Kim }
211039a53e0cSJaegeuk Kim 
211139a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
211239a53e0cSJaegeuk Kim {
211339a53e0cSJaegeuk Kim 	if (dn->node_page)
211439a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
211539a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
211639a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
211739a53e0cSJaegeuk Kim 	dn->node_page = NULL;
211839a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
211939a53e0cSJaegeuk Kim }
212039a53e0cSJaegeuk Kim 
212139a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2122e8512d2eSGu Zheng 					size_t size)
212339a53e0cSJaegeuk Kim {
2124e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
212539a53e0cSJaegeuk Kim }
212639a53e0cSJaegeuk Kim 
21277bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
21287bd59381SGu Zheng 						gfp_t flags)
21297bd59381SGu Zheng {
21307bd59381SGu Zheng 	void *entry;
21317bd59381SGu Zheng 
213280c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
213380c54505SJaegeuk Kim 	if (!entry)
213480c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
21357bd59381SGu Zheng 	return entry;
21367bd59381SGu Zheng }
21377bd59381SGu Zheng 
2138d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2139d62fe971SChao Yu 						int npages, bool no_fail)
2140740432f8SJaegeuk Kim {
2141740432f8SJaegeuk Kim 	struct bio *bio;
2142740432f8SJaegeuk Kim 
2143d62fe971SChao Yu 	if (no_fail) {
2144740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2145740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
214680c54505SJaegeuk Kim 		if (!bio)
214780c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2148740432f8SJaegeuk Kim 		return bio;
2149740432f8SJaegeuk Kim 	}
2150d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2151d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2152d62fe971SChao Yu 		return NULL;
2153d62fe971SChao Yu 	}
21547fa750a1SArnd Bergmann 
2155d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2156d62fe971SChao Yu }
2157740432f8SJaegeuk Kim 
21585f9abab4SJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
21595f9abab4SJaegeuk Kim {
21605f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
21615f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2162fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2163fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
2164fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_WRITE) ||
216572691af6SJaegeuk Kim 		atomic_read(&SM_I(sbi)->dcc_info->queued_discard) ||
216672691af6SJaegeuk Kim 		atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
21675f9abab4SJaegeuk Kim 		return false;
21685f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
21695f9abab4SJaegeuk Kim }
21705f9abab4SJaegeuk Kim 
21719be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
21729be32d72SJaegeuk Kim 				unsigned long index, void *item)
21739be32d72SJaegeuk Kim {
21749be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
21759be32d72SJaegeuk Kim 		cond_resched();
21769be32d72SJaegeuk Kim }
21779be32d72SJaegeuk Kim 
217839a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
217939a53e0cSJaegeuk Kim 
218039a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
218139a53e0cSJaegeuk Kim {
218245590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2183cac5a3d8SDongOh Shin 
218439a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
218539a53e0cSJaegeuk Kim }
218639a53e0cSJaegeuk Kim 
21877a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
21887a2af766SChao Yu {
21897a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
21907a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
21917a2af766SChao Yu }
21927a2af766SChao Yu 
219339a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
219439a53e0cSJaegeuk Kim {
219539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
219639a53e0cSJaegeuk Kim }
219739a53e0cSJaegeuk Kim 
21987a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
21997a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
22007a2af766SChao Yu 			struct page *node_page, unsigned int offset)
220139a53e0cSJaegeuk Kim {
220239a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
220339a53e0cSJaegeuk Kim 	__le32 *addr_array;
22047a2af766SChao Yu 	int base = 0;
22057a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2206cac5a3d8SDongOh Shin 
220745590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
22087a2af766SChao Yu 
22097a2af766SChao Yu 	/* from GC path only */
2210979f492fSLiFan 	if (is_inode) {
2211979f492fSLiFan 		if (!inode)
22127a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2213979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
22147a2af766SChao Yu 			base = get_extra_isize(inode);
22157a2af766SChao Yu 	}
22167a2af766SChao Yu 
221739a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
22187a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
221939a53e0cSJaegeuk Kim }
222039a53e0cSJaegeuk Kim 
222139a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
222239a53e0cSJaegeuk Kim {
222339a53e0cSJaegeuk Kim 	int mask;
222439a53e0cSJaegeuk Kim 
222539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
222639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
222739a53e0cSJaegeuk Kim 	return mask & *addr;
222839a53e0cSJaegeuk Kim }
222939a53e0cSJaegeuk Kim 
2230a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2231a66cdd98SJaegeuk Kim {
2232a66cdd98SJaegeuk Kim 	int mask;
2233a66cdd98SJaegeuk Kim 
2234a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2235a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2236a66cdd98SJaegeuk Kim 	*addr |= mask;
2237a66cdd98SJaegeuk Kim }
2238a66cdd98SJaegeuk Kim 
2239a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2240a66cdd98SJaegeuk Kim {
2241a66cdd98SJaegeuk Kim 	int mask;
2242a66cdd98SJaegeuk Kim 
2243a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2244a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2245a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2246a66cdd98SJaegeuk Kim }
2247a66cdd98SJaegeuk Kim 
224852aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
224939a53e0cSJaegeuk Kim {
225039a53e0cSJaegeuk Kim 	int mask;
225139a53e0cSJaegeuk Kim 	int ret;
225239a53e0cSJaegeuk Kim 
225339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
225439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
225539a53e0cSJaegeuk Kim 	ret = mask & *addr;
225639a53e0cSJaegeuk Kim 	*addr |= mask;
225739a53e0cSJaegeuk Kim 	return ret;
225839a53e0cSJaegeuk Kim }
225939a53e0cSJaegeuk Kim 
226052aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
226139a53e0cSJaegeuk Kim {
226239a53e0cSJaegeuk Kim 	int mask;
226339a53e0cSJaegeuk Kim 	int ret;
226439a53e0cSJaegeuk Kim 
226539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
226639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
226739a53e0cSJaegeuk Kim 	ret = mask & *addr;
226839a53e0cSJaegeuk Kim 	*addr &= ~mask;
226939a53e0cSJaegeuk Kim 	return ret;
227039a53e0cSJaegeuk Kim }
227139a53e0cSJaegeuk Kim 
2272c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2273c6ac4c0eSGu Zheng {
2274c6ac4c0eSGu Zheng 	int mask;
2275c6ac4c0eSGu Zheng 
2276c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2277c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2278c6ac4c0eSGu Zheng 	*addr ^= mask;
2279c6ac4c0eSGu Zheng }
2280c6ac4c0eSGu Zheng 
228159c84408SChao Yu /*
228259c84408SChao Yu  * Inode flags
228359c84408SChao Yu  */
228459c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
228559c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
228659c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
228759c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
228859c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
228959c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
229059c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
229159c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
229259c84408SChao Yu /* Reserved for compression usage... */
229359c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
229459c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
229559c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
229659c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
229759c84408SChao Yu /* End compression flags --- maybe not all used */
229859c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
229959c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
230059c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
230159c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
230259c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
230359c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
230459c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
230559c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
230659c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
230759c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
23085d539245SJaegeuk Kim #define F2FS_NOCOW_FL			0x00800000 /* Do not cow file */
230959c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
231059c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
231159c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
231259c84408SChao Yu 
23135d539245SJaegeuk Kim #define F2FS_FL_USER_VISIBLE		0x30CBDFFF /* User visible flags */
231459c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
231559c84408SChao Yu 
231659c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
231759c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
231859c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
231959c84408SChao Yu 					 F2FS_APPEND_FL | \
232059c84408SChao Yu 					 F2FS_NODUMP_FL | \
232159c84408SChao Yu 					 F2FS_NOATIME_FL | \
232259c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
232359c84408SChao Yu 
232459c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
232559c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
232659c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
232759c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
232859c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
232959c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
233059c84408SChao Yu 
233159c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
233259c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
233359c84408SChao Yu 
233459c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
233559c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
23365c57132eSChao Yu 
23375c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
23385c57132eSChao Yu {
23395c57132eSChao Yu 	if (S_ISDIR(mode))
23405c57132eSChao Yu 		return flags;
23415c57132eSChao Yu 	else if (S_ISREG(mode))
23425c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
23435c57132eSChao Yu 	else
23445c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
23455c57132eSChao Yu }
23465c57132eSChao Yu 
234739a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
234839a53e0cSJaegeuk Kim enum {
234939a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2350b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
235126de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2352ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
235339a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
235439a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
235539a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2356c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2357c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2358444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
23591001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
236034d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2361fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2362fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
236388b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
236488b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
23655fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
236602a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
23673c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
23681e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2369b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2370510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2371d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2372c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2373dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2374ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
23757a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
23765c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
23771ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
23782ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
237939a53e0cSJaegeuk Kim };
238039a53e0cSJaegeuk Kim 
2381205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2382205b9822SJaegeuk Kim 						int flag, bool set)
238339a53e0cSJaegeuk Kim {
2384205b9822SJaegeuk Kim 	switch (flag) {
2385205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2386205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2387205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
23889ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2389205b9822SJaegeuk Kim 		if (set)
2390205b9822SJaegeuk Kim 			return;
2391f5d5510eSJaegeuk Kim 		/* fall through */
2392205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2393205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
23941ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
23957c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2396205b9822SJaegeuk Kim 	}
239739a53e0cSJaegeuk Kim }
239839a53e0cSJaegeuk Kim 
239991942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
240039a53e0cSJaegeuk Kim {
240191942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
240291942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2403205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
240439a53e0cSJaegeuk Kim }
240539a53e0cSJaegeuk Kim 
240691942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
240739a53e0cSJaegeuk Kim {
240891942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
240939a53e0cSJaegeuk Kim }
241039a53e0cSJaegeuk Kim 
241191942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
241239a53e0cSJaegeuk Kim {
241391942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
241491942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2415205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
241639a53e0cSJaegeuk Kim }
241739a53e0cSJaegeuk Kim 
241891942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2419444c580fSJaegeuk Kim {
242091942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
242191942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
24227c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
242339a53e0cSJaegeuk Kim }
242439a53e0cSJaegeuk Kim 
2425a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2426a1961246SJaegeuk Kim {
2427a1961246SJaegeuk Kim 	if (inc)
2428a1961246SJaegeuk Kim 		inc_nlink(inode);
2429a1961246SJaegeuk Kim 	else
2430a1961246SJaegeuk Kim 		drop_nlink(inode);
24317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2432a1961246SJaegeuk Kim }
2433a1961246SJaegeuk Kim 
24348edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
24350abd675eSChao Yu 					block_t diff, bool add, bool claim)
24368edd03c8SJaegeuk Kim {
243726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
243826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
243926de9b11SJaegeuk Kim 
24400abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
24410abd675eSChao Yu 	if (add) {
24420abd675eSChao Yu 		if (claim)
24430abd675eSChao Yu 			dquot_claim_block(inode, diff);
24440abd675eSChao Yu 		else
24450abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
24460abd675eSChao Yu 	} else {
24470abd675eSChao Yu 		dquot_free_block(inode, diff);
24480abd675eSChao Yu 	}
24490abd675eSChao Yu 
24507c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
245126de9b11SJaegeuk Kim 	if (clean || recover)
245226de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
24538edd03c8SJaegeuk Kim }
24548edd03c8SJaegeuk Kim 
2455fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2456fc9581c8SJaegeuk Kim {
245726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
245826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
245926de9b11SJaegeuk Kim 
2460fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2461fc9581c8SJaegeuk Kim 		return;
2462fc9581c8SJaegeuk Kim 
2463fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
24647c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
246526de9b11SJaegeuk Kim 	if (clean || recover)
246626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
246726de9b11SJaegeuk Kim }
246826de9b11SJaegeuk Kim 
2469205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2470205b9822SJaegeuk Kim {
2471205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
24727c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2473205b9822SJaegeuk Kim }
2474205b9822SJaegeuk Kim 
24751ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
24761ad71a27SJaegeuk Kim 					unsigned int count)
24771ad71a27SJaegeuk Kim {
24782ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
24791ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
24801ad71a27SJaegeuk Kim }
24811ad71a27SJaegeuk Kim 
2482205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2483205b9822SJaegeuk Kim {
2484205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
24857c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2486205b9822SJaegeuk Kim }
2487205b9822SJaegeuk Kim 
2488205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2489205b9822SJaegeuk Kim {
2490205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
24917c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2492205b9822SJaegeuk Kim }
2493205b9822SJaegeuk Kim 
249491942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2495444c580fSJaegeuk Kim {
2496205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2497205b9822SJaegeuk Kim 
2498444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2499205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
25001001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2501205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
250234d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2503205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2504b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2505205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2506510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2507205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
25087a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
25097a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
25101ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
25111ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2512444c580fSJaegeuk Kim }
2513444c580fSJaegeuk Kim 
251491942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2515444c580fSJaegeuk Kim {
2516444c580fSJaegeuk Kim 	ri->i_inline = 0;
2517444c580fSJaegeuk Kim 
251891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2519444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
252091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
25211001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
252291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
252334d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
252491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2525b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
252691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2527510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
25287a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
25297a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
25301ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
25311ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
25327a2af766SChao Yu }
25337a2af766SChao Yu 
25347a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
25357a2af766SChao Yu {
25367a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2537444c580fSJaegeuk Kim }
2538444c580fSJaegeuk Kim 
2539987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2540987c7c31SChao Yu {
254191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2542987c7c31SChao Yu }
2543987c7c31SChao Yu 
254481ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2545de93653fSJaegeuk Kim {
2546b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2547de93653fSJaegeuk Kim }
2548de93653fSJaegeuk Kim 
25496afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
255065985d93SJaegeuk Kim {
2551695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2552cac5a3d8SDongOh Shin 
255365985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2554b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
255565985d93SJaegeuk Kim }
255665985d93SJaegeuk Kim 
255765985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
255865985d93SJaegeuk Kim {
25596afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
256065985d93SJaegeuk Kim }
256165985d93SJaegeuk Kim 
25620dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
25630dbdc2aeSJaegeuk Kim {
256491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
25650dbdc2aeSJaegeuk Kim }
25660dbdc2aeSJaegeuk Kim 
2567b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2568b3d208f9SJaegeuk Kim {
256991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2570b3d208f9SJaegeuk Kim }
2571b3d208f9SJaegeuk Kim 
2572510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2573510022a8SJaegeuk Kim {
257491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2575510022a8SJaegeuk Kim }
2576510022a8SJaegeuk Kim 
25771ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
25781ad71a27SJaegeuk Kim {
25791ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
25801ad71a27SJaegeuk Kim }
25811ad71a27SJaegeuk Kim 
258288b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
258388b88a66SJaegeuk Kim {
258491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
258588b88a66SJaegeuk Kim }
258688b88a66SJaegeuk Kim 
25875fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
25885fe45743SChao Yu {
25895fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
25905fe45743SChao Yu }
25915fe45743SChao Yu 
259202a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
259302a1335fSJaegeuk Kim {
259491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
259502a1335fSJaegeuk Kim }
259602a1335fSJaegeuk Kim 
25973c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
25983c6c2bebSJaegeuk Kim {
259991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
26003c6c2bebSJaegeuk Kim }
26013c6c2bebSJaegeuk Kim 
26021e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
26031e84371fSJaegeuk Kim {
260491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
26051e84371fSJaegeuk Kim }
26061e84371fSJaegeuk Kim 
2607f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
26081001b347SHuajun Li {
2609695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
26107a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2611cac5a3d8SDongOh Shin 
26127a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
26131001b347SHuajun Li }
26141001b347SHuajun Li 
261534d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
261634d67debSChao Yu {
261791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
261834d67debSChao Yu }
261934d67debSChao Yu 
2620b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2621b5492af7SJaegeuk Kim {
2622b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2623b5492af7SJaegeuk Kim }
2624b5492af7SJaegeuk Kim 
2625b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2626b5492af7SJaegeuk Kim {
2627b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
26287c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2629b5492af7SJaegeuk Kim }
2630b5492af7SJaegeuk Kim 
2631b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2632b5492af7SJaegeuk Kim {
2633b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
26347c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2635b5492af7SJaegeuk Kim }
2636b5492af7SJaegeuk Kim 
263726787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
263826787236SJaegeuk Kim {
2639a0d00fadSChao Yu 	bool ret;
2640a0d00fadSChao Yu 
264126787236SJaegeuk Kim 	if (dsync) {
264226787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
264326787236SJaegeuk Kim 
264426787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
264526787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
264626787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
264726787236SJaegeuk Kim 		return ret;
264826787236SJaegeuk Kim 	}
264926787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
265026787236SJaegeuk Kim 			file_keep_isize(inode) ||
2651235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
265226787236SJaegeuk Kim 		return false;
2653a0d00fadSChao Yu 
265424b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2655214c2461SJaegeuk Kim 		return false;
265624b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2657214c2461SJaegeuk Kim 		return false;
265824b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2659214c2461SJaegeuk Kim 		return false;
266024b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2661214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2662214c2461SJaegeuk Kim 		return false;
2663214c2461SJaegeuk Kim 
2664a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2665a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2666a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2667a0d00fadSChao Yu 
2668a0d00fadSChao Yu 	return ret;
2669267378d4SChao Yu }
2670267378d4SChao Yu 
2671deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
267277888c1eSJaegeuk Kim {
2673deeedd71SChao Yu 	return sb_rdonly(sb);
267477888c1eSJaegeuk Kim }
267577888c1eSJaegeuk Kim 
2676744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2677744602cfSJaegeuk Kim {
2678aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2679744602cfSJaegeuk Kim }
2680744602cfSJaegeuk Kim 
2681eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2682eaa693f4SJaegeuk Kim {
2683eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2684eaa693f4SJaegeuk Kim 		return true;
2685eaa693f4SJaegeuk Kim 
2686eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2687eaa693f4SJaegeuk Kim 		return true;
2688eaa693f4SJaegeuk Kim 
2689eaa693f4SJaegeuk Kim 	return false;
2690eaa693f4SJaegeuk Kim }
2691eaa693f4SJaegeuk Kim 
26923e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
26933e72f721SJaegeuk Kim {
2694e4589fa5SSahitya Tummala 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2695e4589fa5SSahitya Tummala 
2696e4589fa5SSahitya Tummala 	if (!test_opt(sbi, EXTENT_CACHE) ||
269791942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
26983e72f721SJaegeuk Kim 		return false;
26993e72f721SJaegeuk Kim 
2700e4589fa5SSahitya Tummala 	/*
2701e4589fa5SSahitya Tummala 	 * for recovered files during mount do not create extents
2702e4589fa5SSahitya Tummala 	 * if shrinker is not registered.
2703e4589fa5SSahitya Tummala 	 */
2704e4589fa5SSahitya Tummala 	if (list_empty(&sbi->s_list))
2705e4589fa5SSahitya Tummala 		return false;
2706e4589fa5SSahitya Tummala 
2707886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
27083e72f721SJaegeuk Kim }
27093e72f721SJaegeuk Kim 
27101ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
27111ecc0c5cSChao Yu 					size_t size, gfp_t flags)
27120414b004SJaegeuk Kim {
27135222595dSJaegeuk Kim 	void *ret;
27145222595dSJaegeuk Kim 
271555523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
271655523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
27172c63feadSJaegeuk Kim 		return NULL;
271855523519SChao Yu 	}
27197fa750a1SArnd Bergmann 
27205222595dSJaegeuk Kim 	ret = kmalloc(size, flags);
27215222595dSJaegeuk Kim 	if (ret)
27225222595dSJaegeuk Kim 		return ret;
27235222595dSJaegeuk Kim 
27245222595dSJaegeuk Kim 	return kvmalloc(size, flags);
27250414b004SJaegeuk Kim }
27260414b004SJaegeuk Kim 
2727acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2728acbf054dSChao Yu 					size_t size, gfp_t flags)
2729acbf054dSChao Yu {
2730acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2731acbf054dSChao Yu }
2732acbf054dSChao Yu 
2733628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2734628b3d14SChao Yu 					size_t size, gfp_t flags)
2735628b3d14SChao Yu {
2736628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2737628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2738628b3d14SChao Yu 		return NULL;
2739628b3d14SChao Yu 	}
27407fa750a1SArnd Bergmann 
2741628b3d14SChao Yu 	return kvmalloc(size, flags);
2742628b3d14SChao Yu }
2743628b3d14SChao Yu 
2744628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2745628b3d14SChao Yu 					size_t size, gfp_t flags)
2746628b3d14SChao Yu {
2747628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2748628b3d14SChao Yu }
2749628b3d14SChao Yu 
27507a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2751f2470371SChao Yu {
27527a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2753f2470371SChao Yu }
2754f2470371SChao Yu 
27556afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
27566afc662eSChao Yu {
27576afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
27586afc662eSChao Yu }
27596afc662eSChao Yu 
27604d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
276191942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2762a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2763a6dda0e6SChristoph Hellwig 
27647a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
27657a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
27667a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
27677a2af766SChao Yu 
27685c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
27695c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
27702f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
27715c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
27722f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
27735c57132eSChao Yu 
2774b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2775b0af6d49SChao Yu {
2776b0af6d49SChao Yu 	int i;
2777b0af6d49SChao Yu 
2778b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2779b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2780b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2781b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2782b0af6d49SChao Yu }
2783b0af6d49SChao Yu 
2784b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2785b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2786b0af6d49SChao Yu {
2787b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2788b0af6d49SChao Yu 		return;
2789b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2790b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2791b0af6d49SChao Yu 
2792b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2793b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2794b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2795b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2796b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2797b0af6d49SChao Yu }
2798b0af6d49SChao Yu 
27992c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
28002c70c5e3SChao Yu 
28012f84babfSSheng Yong #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META &&	\
28022f84babfSSheng Yong 				(!is_read_io((fio)->op) || (fio)->is_meta))
2803c9b60788SChao Yu 
2804e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2805e1da7872SChao Yu 					block_t blkaddr, int type);
2806e1da7872SChao Yu void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2807e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2808e1da7872SChao Yu 					block_t blkaddr, int type)
2809e1da7872SChao Yu {
2810e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2811e1da7872SChao Yu 		f2fs_msg(sbi->sb, KERN_ERR,
2812e1da7872SChao Yu 			"invalid blkaddr: %u, type: %d, run fsck to fix.",
2813e1da7872SChao Yu 			blkaddr, type);
2814e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
2815e1da7872SChao Yu 	}
2816e1da7872SChao Yu }
2817e1da7872SChao Yu 
2818e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
28197b525dd0SChao Yu {
28207b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
28217b525dd0SChao Yu 		return false;
28227b525dd0SChao Yu 	return true;
28237b525dd0SChao Yu }
28247b525dd0SChao Yu 
2825e1da7872SChao Yu static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
2826e1da7872SChao Yu 						block_t blkaddr)
2827e1da7872SChao Yu {
2828e1da7872SChao Yu 	if (!__is_valid_data_blkaddr(blkaddr))
2829e1da7872SChao Yu 		return false;
2830e1da7872SChao Yu 	verify_blkaddr(sbi, blkaddr, DATA_GENERIC);
2831e1da7872SChao Yu 	return true;
2832e1da7872SChao Yu }
2833e1da7872SChao Yu 
283439a53e0cSJaegeuk Kim /*
283539a53e0cSJaegeuk Kim  * file.c
283639a53e0cSJaegeuk Kim  */
2837cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
28384d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2839af033b2aSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock,
2840af033b2aSChao Yu 							bool buf_write);
2841cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2842a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2843a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2844cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
28454d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
28464d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2847c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2848cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2849cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
285078130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
28511ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
285239a53e0cSJaegeuk Kim 
285339a53e0cSJaegeuk Kim /*
285439a53e0cSJaegeuk Kim  * inode.c
285539a53e0cSJaegeuk Kim  */
2856cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2857704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2858704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2859cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2860cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
28614d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
28624d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
28634d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2864cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2865cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
28664d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
286739a53e0cSJaegeuk Kim 
286839a53e0cSJaegeuk Kim /*
286939a53e0cSJaegeuk Kim  * namei.c
287039a53e0cSJaegeuk Kim  */
28714d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2872b6a06cbbSChao Yu 							bool hot, bool set);
287339a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
287439a53e0cSJaegeuk Kim 
287539a53e0cSJaegeuk Kim /*
287639a53e0cSJaegeuk Kim  * dir.c
287739a53e0cSJaegeuk Kim  */
28784d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
28794d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2880cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2881cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2882cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2883cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
28844d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2885cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
28864d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2887cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2888cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
28894d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2890cac5a3d8SDongOh Shin 			unsigned int current_depth);
28914d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2892cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2893cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2894cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2895cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2896cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2897cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2898cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2899cac5a3d8SDongOh Shin 			struct page **page);
2900cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2901cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2902cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2903cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2904cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2905cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2906cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2907cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
29084d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2909cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
29104d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2911cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2912cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2913cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2914cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2915cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
291639a53e0cSJaegeuk Kim 
2917b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2918b7f7a5e0SAl Viro {
29194d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2920510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2921b7f7a5e0SAl Viro }
2922b7f7a5e0SAl Viro 
292339a53e0cSJaegeuk Kim /*
292439a53e0cSJaegeuk Kim  * super.c
292539a53e0cSJaegeuk Kim  */
2926cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2927cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2928ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2929af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
29304b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2931cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2932cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2933a07ef784SNamjae Jeon extern __printf(3, 4)
2934cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
29354d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
293639a53e0cSJaegeuk Kim 
293739a53e0cSJaegeuk Kim /*
293839a53e0cSJaegeuk Kim  * hash.c
293939a53e0cSJaegeuk Kim  */
29406332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
29416332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
294239a53e0cSJaegeuk Kim 
294339a53e0cSJaegeuk Kim /*
294439a53e0cSJaegeuk Kim  * node.c
294539a53e0cSJaegeuk Kim  */
294639a53e0cSJaegeuk Kim struct dnode_of_data;
294739a53e0cSJaegeuk Kim struct node_info;
294839a53e0cSJaegeuk Kim 
29494d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
29504d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
295150fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
295250fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
295350fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
295450fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
29554d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
29564d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
29574d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
29587735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
29594d57b86dSChao Yu 						struct node_info *ni);
29604d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
29614d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
29624d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
29634d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
296450fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
296550fa53ecSChao Yu 					unsigned int seq_id);
29664d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
29674d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
29684d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
29694d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
29704d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
29714d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
297248018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
29734d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
297450fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
297550fa53ecSChao Yu 			unsigned int *seq_id);
29764d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
29774d57b86dSChao Yu 			struct writeback_control *wbc,
2978b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
2979e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
29804d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
29814d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
29824d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
29834d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
29844d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
29854d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
29864d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
29877735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2988cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
2989edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29904d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
29914d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
29924d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
29934d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
299439a53e0cSJaegeuk Kim 
299539a53e0cSJaegeuk Kim /*
299639a53e0cSJaegeuk Kim  * segment.c
299739a53e0cSJaegeuk Kim  */
29984d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
29994d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
30004d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
30014d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
30024d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
30034d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3004cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3005cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
300639d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
30074d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
30081228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
30094d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
30104d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
30114d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
30124d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
30134d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
301403f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
30154d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
30164d57b86dSChao Yu 					struct cp_control *cpc);
30174354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
30184354994fSDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi);
30194d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
30204d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
30214d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3022cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
30234d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
30244d57b86dSChao Yu 					struct cp_control *cpc);
30254d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
30264d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
30274d57b86dSChao Yu 					block_t blk_addr);
30284d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3029b0af6d49SChao Yu 						enum iostat_type io_type);
30304d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
30314d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
30324d57b86dSChao Yu 			struct f2fs_io_info *fio);
30334d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
30344d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3035cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3036cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
3037cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3038cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3039cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3040cac5a3d8SDongOh Shin 			bool recover_newaddr);
30414d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3042cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3043fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3044fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
3045cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3046bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
30470ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
30481e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
30491e78e8bdSSahitya Tummala 								block_t len);
30504d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
30514d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
30524d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3053cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
30544d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30554d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
30564d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
30574d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
30584d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
30594d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
30604d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
30614d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
306239a53e0cSJaegeuk Kim 
306339a53e0cSJaegeuk Kim /*
306439a53e0cSJaegeuk Kim  * checkpoint.c
306539a53e0cSJaegeuk Kim  */
3066cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
30674d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
30684d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
30697735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
30704d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3071e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
30724d57b86dSChao Yu 					block_t blkaddr, int type);
30734d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3074cac5a3d8SDongOh Shin 			int type, bool sync);
30754d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
30764d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3077b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
30784d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30794d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30804d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
30814d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
30824d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
308339d787beSChao Yu 					unsigned int devidx, int type);
30844d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
308539d787beSChao Yu 					unsigned int devidx, int type);
3086cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
30874d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
30884d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
30894d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
30904d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
30914d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
30924d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
30934d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
30944d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
30954d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
309650fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
30974d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30984d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
30994d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
31004d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
310139a53e0cSJaegeuk Kim 
310239a53e0cSJaegeuk Kim /*
310339a53e0cSJaegeuk Kim  * data.c
310439a53e0cSJaegeuk Kim  */
31056dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
31066dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
3107b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3108b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3109bab475c5SChao Yu 				struct inode *inode, struct page *page,
3110bab475c5SChao Yu 				nid_t ino, enum page_type type);
3111b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3112cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
3113fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3114cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3115cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3116cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
31174d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3118cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
31194d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
31204d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3121cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3122cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3123cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
31244d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3125cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
31264d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
31274d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3128cac5a3d8SDongOh Shin 			bool for_write);
31294d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3130cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
31314d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
313239a86958SChao Yu void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3133cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3134cac5a3d8SDongOh Shin 			int create, int flag);
3135cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3136cac5a3d8SDongOh Shin 			u64 start, u64 len);
31374d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
31384d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3139cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3140cac5a3d8SDongOh Shin 			unsigned int length);
3141cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
31425b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3143cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3144cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
31455b7a487cSWeichao Guo #endif
3146b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
31475ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
314839a53e0cSJaegeuk Kim 
314939a53e0cSJaegeuk Kim /*
315039a53e0cSJaegeuk Kim  * gc.c
315139a53e0cSJaegeuk Kim  */
31524d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
31534d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
31544d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3155e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3156e066b83cSJaegeuk Kim 			unsigned int segno);
31574d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
315839a53e0cSJaegeuk Kim 
315939a53e0cSJaegeuk Kim /*
316039a53e0cSJaegeuk Kim  * recovery.c
316139a53e0cSJaegeuk Kim  */
31624d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
31634d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
316439a53e0cSJaegeuk Kim 
316539a53e0cSJaegeuk Kim /*
316639a53e0cSJaegeuk Kim  * debug.c
316739a53e0cSJaegeuk Kim  */
316839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
316939a53e0cSJaegeuk Kim struct f2fs_stat_info {
317039a53e0cSJaegeuk Kim 	struct list_head stat_list;
317139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
317239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
317339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
31745b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
31755b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3176c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
31772c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
31782c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
317935782b23SJaegeuk Kim 	int inmem_pages;
31802c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
31815b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
31825b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
318339a53e0cSJaegeuk Kim 	int total_count, utilization;
31848b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
31855f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
318602b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3187274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
318814d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
318914d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
31905f32366aSChao Yu 	int nr_discard_cmd;
3191d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3192a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3193648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3194f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
319539a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
319639a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
319739a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
319839a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
319942190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
320039a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3201e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
320239a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3203e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
32042ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
320539a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
320639a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
320739a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
320839a53e0cSJaegeuk Kim 
3209b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
321039a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
321139a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3212b9a2c252SChangman Lee 	unsigned int inplace_count;
32139edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
321439a53e0cSJaegeuk Kim };
321539a53e0cSJaegeuk Kim 
3216963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3217963d4f7dSGu Zheng {
3218963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3219963d4f7dSGu Zheng }
3220963d4f7dSGu Zheng 
3221942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
322242190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
322339a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3224dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
3225274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3226274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
322733fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
322833fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
32295b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
32305b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
32315b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
32325b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3233d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3234d5e8f6c9SChao Yu 	do {								\
3235d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3236d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3237d5e8f6c9SChao Yu 	} while (0)
3238d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3239d5e8f6c9SChao Yu 	do {								\
3240d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3241d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3242d5e8f6c9SChao Yu 	} while (0)
32430dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
32440dbdc2aeSJaegeuk Kim 	do {								\
32450dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
324603e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
32470dbdc2aeSJaegeuk Kim 	} while (0)
32480dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
32490dbdc2aeSJaegeuk Kim 	do {								\
32500dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
325103e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
32520dbdc2aeSJaegeuk Kim 	} while (0)
32533289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
32543289c061SJaegeuk Kim 	do {								\
32553289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
325603e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
32573289c061SJaegeuk Kim 	} while (0)
32583289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
32593289c061SJaegeuk Kim 	do {								\
32603289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
326103e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
32623289c061SJaegeuk Kim 	} while (0)
3263b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3264b63e7be5SChao Yu 	do {								\
3265b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3266b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3267b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3268b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3269b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3270b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3271b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3272b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3273b63e7be5SChao Yu 	} while (0)
3274dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3275dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3276dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3277dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3278b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3279b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
328026a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3281cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
328226a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3283cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
328426a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
328526a28a0cSJaegeuk Kim 	do {								\
328626a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
328726a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
328826a28a0cSJaegeuk Kim 		if (cur > max)						\
328926a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
329026a28a0cSJaegeuk Kim 	} while (0)
3291648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3292648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3293648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3294648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3295648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3296648d50baSChao Yu 	do {								\
3297648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3298648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3299648d50baSChao Yu 		if (cur > max)						\
3300648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3301648d50baSChao Yu 	} while (0)
3302e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
330339a53e0cSJaegeuk Kim 	do {								\
3304963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
330568afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
330668afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
330739a53e0cSJaegeuk Kim 			si->data_segs++;				\
3308e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3309e1235983SChangman Lee 		} else {						\
331039a53e0cSJaegeuk Kim 			si->node_segs++;				\
3311e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3312e1235983SChangman Lee 		}							\
331339a53e0cSJaegeuk Kim 	} while (0)
331439a53e0cSJaegeuk Kim 
331539a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
331668afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
331739a53e0cSJaegeuk Kim 
3318e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
331939a53e0cSJaegeuk Kim 	do {								\
3320963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
332139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
332239a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
332368afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
332439a53e0cSJaegeuk Kim 	} while (0)
332539a53e0cSJaegeuk Kim 
3326e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
332739a53e0cSJaegeuk Kim 	do {								\
3328963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
332939a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
333039a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
333168afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
333239a53e0cSJaegeuk Kim 	} while (0)
333339a53e0cSJaegeuk Kim 
3334cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3335cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3336c20e57b3SGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
33374589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
333839a53e0cSJaegeuk Kim #else
3339d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3340d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3341d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3342d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3343274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3344274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3345d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3346d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3347d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3348d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3349d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3350d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3351d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3352d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3353d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3354d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3355d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3356d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3357d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3358d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3359d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3360d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3361d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3362d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3363b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3364d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3365d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3366d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3367d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3368d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3369d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3370d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
337139a53e0cSJaegeuk Kim 
337239a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
337339a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3374c20e57b3SGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
33754589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
337639a53e0cSJaegeuk Kim #endif
337739a53e0cSJaegeuk Kim 
337839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
337939a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
338039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
338139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
338239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
338339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
338439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
338539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3386cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
338739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
33884d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
33891001b347SHuajun Li 
3390e18c65b2SHuajun Li /*
3391e18c65b2SHuajun Li  * inline.c
3392e18c65b2SHuajun Li  */
3393cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3394cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
33954d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
33964d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
33974d57b86dSChao Yu 						struct page *ipage, u64 from);
3398cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3399cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3400cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3401cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
34024d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
34034d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3404cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
34054d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3406cac5a3d8SDongOh Shin 			struct page *ipage);
3407cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3408cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3409cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34104d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
34114d57b86dSChao Yu 				struct page *page, struct inode *dir,
34124d57b86dSChao Yu 				struct inode *inode);
3413cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3414cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3415cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3416cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3417cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3418cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3419cde4de12SJaegeuk Kim 
3420cde4de12SJaegeuk Kim /*
34212658e50dSJaegeuk Kim  * shrinker.c
34222658e50dSJaegeuk Kim  */
3423cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3424cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3425cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3426cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3427cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3428cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
34292658e50dSJaegeuk Kim 
34302658e50dSJaegeuk Kim /*
3431a28ef1f5SChao Yu  * extent_cache.c
3432a28ef1f5SChao Yu  */
34334dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3434004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
34354d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
34364dada3fdSChao Yu 				struct rb_root_cached *root,
34374dada3fdSChao Yu 				struct rb_node **parent,
34384dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
34394dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3440004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3441004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3442004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
34434dada3fdSChao Yu 		bool force, bool *leftmost);
34444d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
34454dada3fdSChao Yu 						struct rb_root_cached *root);
3446cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3447cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3448cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3449cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3450cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3451cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3452cac5a3d8SDongOh Shin 			struct extent_info *ei);
3453cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
345419b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3455cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
34564d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
34574d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
34584d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3459a28ef1f5SChao Yu 
3460a28ef1f5SChao Yu /*
34618ceffcb2SChao Yu  * sysfs.c
34628ceffcb2SChao Yu  */
3463dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3464dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3465dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3466dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
34678ceffcb2SChao Yu 
34688ceffcb2SChao Yu /*
3469cde4de12SJaegeuk Kim  * crypto support
3470cde4de12SJaegeuk Kim  */
34710b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3472cde4de12SJaegeuk Kim {
3473cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3474cde4de12SJaegeuk Kim }
3475cde4de12SJaegeuk Kim 
34761958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
34771958593eSJaegeuk Kim {
34781958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
34791958593eSJaegeuk Kim }
34801958593eSJaegeuk Kim 
3481cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3482cde4de12SJaegeuk Kim {
3483cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3484cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
34859149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
3486cde4de12SJaegeuk Kim #endif
3487cde4de12SJaegeuk Kim }
3488cde4de12SJaegeuk Kim 
34896dbb1796SEric Biggers /*
34906dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
34916dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
34926dbb1796SEric Biggers  */
34936dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3494cde4de12SJaegeuk Kim {
34956dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3496cde4de12SJaegeuk Kim }
3497cde4de12SJaegeuk Kim 
3498ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
34997beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
3500ccd31cb2SSheng Yong { \
35017beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
3502cde4de12SJaegeuk Kim }
3503f424f664SJaegeuk Kim 
3504ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3505ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3506ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3507ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3508ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3509ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3510ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3511ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3512b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3513d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
35141c1d35dfSChao Yu 
3515178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3516178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
35173c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3518178053e2SDamien Le Moal {
3519178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
35203c62be17SJaegeuk Kim 	int i;
3521178053e2SDamien Le Moal 
35223c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
35233c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
35243c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
35253c62be17SJaegeuk Kim 	return -EINVAL;
3526178053e2SDamien Le Moal }
3527178053e2SDamien Le Moal #endif
3528178053e2SDamien Le Moal 
35297d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
353096ba2decSDamien Le Moal {
35317beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
35327d20c8abSChao Yu }
353396ba2decSDamien Le Moal 
35347d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
35357d20c8abSChao Yu {
35367d20c8abSChao Yu 	return blk_queue_discard(bdev_get_queue(sbi->sb->s_bdev));
35377d20c8abSChao Yu }
35387d20c8abSChao Yu 
35397d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
35407d20c8abSChao Yu {
35417d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
35427d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
354352763a4bSJaegeuk Kim }
354452763a4bSJaegeuk Kim 
354552763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
354652763a4bSJaegeuk Kim {
354752763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
354852763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
354952763a4bSJaegeuk Kim 
355052763a4bSJaegeuk Kim 	switch (mt) {
355152763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
355252763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
355352763a4bSJaegeuk Kim 		break;
355452763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
355552763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
355652763a4bSJaegeuk Kim 		break;
355752763a4bSJaegeuk Kim 	}
355852763a4bSJaegeuk Kim }
355952763a4bSJaegeuk Kim 
3560fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3561fcc85a4dSJaegeuk Kim {
3562fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3563886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3564fcc85a4dSJaegeuk Kim 
3565fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3566fcc85a4dSJaegeuk Kim #else
35676e45f2a5SGustavo A. R. Silva 	return false;
3568fcc85a4dSJaegeuk Kim #endif
3569fcc85a4dSJaegeuk Kim }
3570fcc85a4dSJaegeuk Kim 
3571f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
3572f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3573b91050a8SHyunchul Lee {
3574f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
3575f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
3576f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
3577f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
3578f847c699SChao Yu 
3579f847c699SChao Yu 	return align & blocksize_mask;
3580f847c699SChao Yu }
3581f847c699SChao Yu 
3582f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
3583f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3584f847c699SChao Yu {
3585f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3586f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3587f847c699SChao Yu 
3588f847c699SChao Yu 	return (test_opt(sbi, LFS) && (rw == WRITE) &&
3589f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
3590f847c699SChao Yu }
3591f847c699SChao Yu 
3592f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
3593f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3594f847c699SChao Yu {
3595f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3596f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3597f847c699SChao Yu 
3598f847c699SChao Yu 	if (f2fs_post_read_required(inode))
3599f847c699SChao Yu 		return true;
3600f847c699SChao Yu 	if (sbi->s_ndevs)
3601f847c699SChao Yu 		return true;
3602f847c699SChao Yu 	/*
3603f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
3604f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
3605f847c699SChao Yu 	 */
36067beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
3607f847c699SChao Yu 		return true;
3608f847c699SChao Yu 	if (test_opt(sbi, LFS) && (rw == WRITE) &&
3609f847c699SChao Yu 				block_unaligned_IO(inode, iocb, iter))
3610f847c699SChao Yu 		return true;
36114354994fSDaniel Rosenberg 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
36124354994fSDaniel Rosenberg 		return true;
36134354994fSDaniel Rosenberg 
3614f847c699SChao Yu 	return false;
3615b91050a8SHyunchul Lee }
3616b91050a8SHyunchul Lee 
361783a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
3618d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3619d494500aSChao Yu 							unsigned int type);
362083a3bfdbSJaegeuk Kim #else
3621d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
362283a3bfdbSJaegeuk Kim #endif
362383a3bfdbSJaegeuk Kim 
362439a53e0cSJaegeuk Kim #endif
3625af033b2aSChao Yu 
3626af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
3627af033b2aSChao Yu {
3628af033b2aSChao Yu #ifdef CONFIG_QUOTA
36297beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
3630af033b2aSChao Yu 		return true;
3631af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
3632af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
3633af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
3634af033b2aSChao Yu 		return true;
3635af033b2aSChao Yu #endif
3636af033b2aSChao Yu 	return false;
3637af033b2aSChao Yu }
3638