xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 0921835c9544c1dc3d2093df6932f1b207eec487)
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 #include <linux/fscrypt.h>
28734f0d24SDave Chinner 
295d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
309850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
315d56b671SJaegeuk Kim #else
329850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
339850cf4aSJaegeuk Kim 	do {								\
349850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
359850cf4aSJaegeuk Kim 			WARN_ON(1);					\
36caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
379850cf4aSJaegeuk Kim 		}							\
389850cf4aSJaegeuk Kim 	} while (0)
395d56b671SJaegeuk Kim #endif
405d56b671SJaegeuk Kim 
412c63feadSJaegeuk Kim enum {
422c63feadSJaegeuk Kim 	FAULT_KMALLOC,
43628b3d14SChao Yu 	FAULT_KVMALLOC,
44c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4501eccef7SChao Yu 	FAULT_PAGE_GET,
46d62fe971SChao Yu 	FAULT_ALLOC_BIO,
47cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
48cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
49cb78942bSJaegeuk Kim 	FAULT_BLOCK,
50cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5153aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5214b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
536f5c2ed0SChao Yu 	FAULT_READ_IO,
540f348028SChao Yu 	FAULT_CHECKPOINT,
55b83dcfe6SChao Yu 	FAULT_DISCARD,
566f5c2ed0SChao Yu 	FAULT_WRITE_IO,
572c63feadSJaegeuk Kim 	FAULT_MAX,
582c63feadSJaegeuk Kim };
592c63feadSJaegeuk Kim 
607fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
61d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
62d494500aSChao Yu 
6308796897SSheng Yong struct f2fs_fault_info {
6408796897SSheng Yong 	atomic_t inject_ops;
6508796897SSheng Yong 	unsigned int inject_rate;
6608796897SSheng Yong 	unsigned int inject_type;
6708796897SSheng Yong };
6808796897SSheng Yong 
6919880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7068afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
712c63feadSJaegeuk Kim #endif
722c63feadSJaegeuk Kim 
7339a53e0cSJaegeuk Kim /*
7439a53e0cSJaegeuk Kim  * For mount options
7539a53e0cSJaegeuk Kim  */
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
83444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
841001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8534d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8634d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8734d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
88d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8989672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
906aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
91343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9273faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9336abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9436abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
950abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
960abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
975c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
984b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
996afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1007e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1014354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
10239a53e0cSJaegeuk Kim 
10363189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10463189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10563189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10663189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10739a53e0cSJaegeuk Kim 
10839a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10939a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11039a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11139a53e0cSJaegeuk Kim 
112a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
113a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
114a9841c4dSJaegeuk Kim 			 * address format, __le32.
115a9841c4dSJaegeuk Kim 			 */
11639a53e0cSJaegeuk Kim typedef u32 nid_t;
11739a53e0cSJaegeuk Kim 
11839a53e0cSJaegeuk Kim struct f2fs_mount_info {
11939a53e0cSJaegeuk Kim 	unsigned int opt;
12063189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12163189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12263189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12363189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12463189b78SChao Yu 	int active_logs;		/* # of active logs */
12563189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12663189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12763189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12863189b78SChao Yu #endif
12963189b78SChao Yu #ifdef CONFIG_QUOTA
13063189b78SChao Yu 	/* Names of quota files with journalled quota */
13163189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13263189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13363189b78SChao Yu #endif
13463189b78SChao Yu 	/* For which write hints are passed down to block layer */
13563189b78SChao Yu 	int whint_mode;
13663189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13763189b78SChao Yu 	int fsync_mode;			/* fsync policy */
138ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
1394d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1404d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1414d3aed70SDaniel Rosenberg 					 */
14239a53e0cSJaegeuk Kim };
14339a53e0cSJaegeuk Kim 
144cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1450bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
146e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1477a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1485c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
149704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1506afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
151234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1521c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
153b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
15453fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
155d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
156cde4de12SJaegeuk Kim 
1577beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1587beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1597beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1607beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1617beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1627beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1637beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
16476f105a2SJaegeuk Kim 
16539a53e0cSJaegeuk Kim /*
1667c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1677c2e5963SJaegeuk Kim  */
1687c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1697c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1707c2e5963SJaegeuk Kim 
1717c2e5963SJaegeuk Kim /*
17239a53e0cSJaegeuk Kim  * For checkpoint manager
17339a53e0cSJaegeuk Kim  */
17439a53e0cSJaegeuk Kim enum {
17539a53e0cSJaegeuk Kim 	NAT_BITMAP,
17639a53e0cSJaegeuk Kim 	SIT_BITMAP
17739a53e0cSJaegeuk Kim };
17839a53e0cSJaegeuk Kim 
179c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
180c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
181c473f1a9SChao Yu #define	CP_SYNC		0x00000004
182c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
183c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1841f43e2adSChao Yu #define CP_TRIMMED	0x00000020
1854354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
18675ab4cb8SJaegeuk Kim 
1874ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
188ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
189969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
190f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
191969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1928bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
19360b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
194dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
1954354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
196db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
19703f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
198bba681cbSJaegeuk Kim 
19975ab4cb8SJaegeuk Kim struct cp_control {
20075ab4cb8SJaegeuk Kim 	int reason;
2014b2fecc8SJaegeuk Kim 	__u64 trim_start;
2024b2fecc8SJaegeuk Kim 	__u64 trim_end;
2034b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
20475ab4cb8SJaegeuk Kim };
20575ab4cb8SJaegeuk Kim 
206662befdaSChao Yu /*
207e1da7872SChao Yu  * indicate meta/data type
208662befdaSChao Yu  */
209662befdaSChao Yu enum {
210662befdaSChao Yu 	META_CP,
211662befdaSChao Yu 	META_NAT,
21281c1a0f1SChao Yu 	META_SIT,
2134c521f49SJaegeuk Kim 	META_SSA,
214b63e7be5SChao Yu 	META_MAX,
2154c521f49SJaegeuk Kim 	META_POR,
21693770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
21793770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
21893770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
21993770ab7SChao Yu 					 * strong check on range and segment
22093770ab7SChao Yu 					 * bitmap but no warning due to race
22193770ab7SChao Yu 					 * condition of read on truncated area
22293770ab7SChao Yu 					 * by extent_cache
22393770ab7SChao Yu 					 */
224e1da7872SChao Yu 	META_GENERIC,
225662befdaSChao Yu };
226662befdaSChao Yu 
2276451e041SJaegeuk Kim /* for the list of ino */
2286451e041SJaegeuk Kim enum {
2296451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
230fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
231fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2320a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
23339d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2346451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2356451e041SJaegeuk Kim };
2366451e041SJaegeuk Kim 
2376451e041SJaegeuk Kim struct ino_entry {
23839a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
23939a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
24039d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
24139a53e0cSJaegeuk Kim };
24239a53e0cSJaegeuk Kim 
2432710fd7eSChao Yu /* for the list of inodes to be GCed */
24406292073SChao Yu struct inode_entry {
24539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
24639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
24739a53e0cSJaegeuk Kim };
24839a53e0cSJaegeuk Kim 
24950fa53ecSChao Yu struct fsync_node_entry {
25050fa53ecSChao Yu 	struct list_head list;	/* list head */
25150fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
25250fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
25350fa53ecSChao Yu };
25450fa53ecSChao Yu 
255a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2567fd9e544SJaegeuk Kim struct discard_entry {
2577fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
258a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
259a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2607fd9e544SJaegeuk Kim };
2617fd9e544SJaegeuk Kim 
262969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
263969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
264969d1b18SChao Yu 
265ba48a33eSChao Yu /* max discard pend list number */
266ba48a33eSChao Yu #define MAX_PLIST_NUM		512
267ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
2682f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
269ba48a33eSChao Yu 
27015469963SJaegeuk Kim enum {
27135ec7d57SChao Yu 	D_PREP,			/* initial */
27235ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
27335ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
27435ec7d57SChao Yu 	D_DONE,			/* finished */
27515469963SJaegeuk Kim };
27615469963SJaegeuk Kim 
277004b6862SChao Yu struct discard_info {
278b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
279b01a9201SJaegeuk Kim 	block_t len;			/* length */
280004b6862SChao Yu 	block_t start;			/* actual start address in dev */
281004b6862SChao Yu };
282004b6862SChao Yu 
283b01a9201SJaegeuk Kim struct discard_cmd {
284004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
285004b6862SChao Yu 	union {
286004b6862SChao Yu 		struct {
287004b6862SChao Yu 			block_t lstart;	/* logical start address */
288004b6862SChao Yu 			block_t len;	/* length */
289004b6862SChao Yu 			block_t start;	/* actual start address in dev */
290004b6862SChao Yu 		};
291004b6862SChao Yu 		struct discard_info di;	/* discard info */
292004b6862SChao Yu 
293004b6862SChao Yu 	};
294b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
295b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
296c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
297ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2989a744b92SChao Yu 	unsigned char state;		/* state */
29972691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
300c81abe34SJaegeuk Kim 	int error;			/* bio error */
30135ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
30235ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
303275b66b0SChao Yu };
304275b66b0SChao Yu 
30578997b56SChao Yu enum {
30678997b56SChao Yu 	DPOLICY_BG,
30778997b56SChao Yu 	DPOLICY_FORCE,
30878997b56SChao Yu 	DPOLICY_FSTRIM,
30978997b56SChao Yu 	DPOLICY_UMOUNT,
31078997b56SChao Yu 	MAX_DPOLICY,
31178997b56SChao Yu };
31278997b56SChao Yu 
313ecc9aa00SChao Yu struct discard_policy {
31478997b56SChao Yu 	int type;			/* type of discard */
315ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
316f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
317ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
318ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
319ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
320ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
321ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
32220ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
32378997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
32403f2c02dSJaegeuk Kim 	int timeout;			/* discard timeout for put_super */
325ecc9aa00SChao Yu };
326ecc9aa00SChao Yu 
3270b54fb84SJaegeuk Kim struct discard_cmd_control {
32815469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
32946f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
330ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
33146f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3328412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
33315469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
334969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
33515469963SJaegeuk Kim 	struct mutex cmd_lock;
336d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
337d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
338969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
339d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
34020ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3418b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
34272691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3435f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3444dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
34567fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
34639a53e0cSJaegeuk Kim };
34739a53e0cSJaegeuk Kim 
34839a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
34939a53e0cSJaegeuk Kim struct fsync_inode_entry {
35039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
35139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
352c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
353c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
35439a53e0cSJaegeuk Kim };
35539a53e0cSJaegeuk Kim 
35668afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
35768afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
35839a53e0cSJaegeuk Kim 
35968afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
36068afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
36168afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
36268afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
36339a53e0cSJaegeuk Kim 
364dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
365dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
366309cc2b6SJaegeuk Kim 
367dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
36839a53e0cSJaegeuk Kim {
369dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
370cac5a3d8SDongOh Shin 
371dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
37239a53e0cSJaegeuk Kim 	return before;
37339a53e0cSJaegeuk Kim }
37439a53e0cSJaegeuk Kim 
375dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
37639a53e0cSJaegeuk Kim {
377dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
378cac5a3d8SDongOh Shin 
379dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
38039a53e0cSJaegeuk Kim 	return before;
38139a53e0cSJaegeuk Kim }
38239a53e0cSJaegeuk Kim 
383dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
384dfc08a12SChao Yu 							int size, int type)
385184a5cd2SChao Yu {
386184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
387dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
388dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
389184a5cd2SChao Yu }
390184a5cd2SChao Yu 
39139a53e0cSJaegeuk Kim /*
392e9750824SNamjae Jeon  * ioctl commands
393e9750824SNamjae Jeon  */
394e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
395e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
396d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
397e9750824SNamjae Jeon 
39888b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
39988b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
40088b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
40102a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
4021e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
4031e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
404d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
405456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
406d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
407d07efb50SJaegeuk Kim 						struct f2fs_defragment)
4084dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
4094dd6f977SJaegeuk Kim 						struct f2fs_move_range)
410e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
411e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
41234dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
41334dc77adSJaegeuk Kim 						struct f2fs_gc_range)
414e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
4151ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
4161ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
417c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
41804f0b2eaSQiuyang Sun #define F2FS_IOC_RESIZE_FS		_IOW(F2FS_IOCTL_MAGIC, 16, __u64)
41988b88a66SJaegeuk Kim 
4200b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
4210b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
4220b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
423f424f664SJaegeuk Kim 
4241abff93dSJaegeuk Kim /*
4251abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4261abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4271abff93dSJaegeuk Kim  */
4281abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4291abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4301abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4311abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
432c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4330cd6d9b0SJaegeuk Kim #define F2FS_GOING_DOWN_NEED_FSCK	0x4	/* going down to trigger fsck */
4341abff93dSJaegeuk Kim 
435e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
436e9750824SNamjae Jeon /*
437e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
438e9750824SNamjae Jeon  */
439e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
440e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
44104ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
442e9750824SNamjae Jeon #endif
443e9750824SNamjae Jeon 
4442c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4452c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4462c1d0305SChao Yu 
44734dc77adSJaegeuk Kim struct f2fs_gc_range {
44834dc77adSJaegeuk Kim 	u32 sync;
44934dc77adSJaegeuk Kim 	u64 start;
45034dc77adSJaegeuk Kim 	u64 len;
45134dc77adSJaegeuk Kim };
45234dc77adSJaegeuk Kim 
453d323d005SChao Yu struct f2fs_defragment {
454d323d005SChao Yu 	u64 start;
455d323d005SChao Yu 	u64 len;
456d323d005SChao Yu };
457d323d005SChao Yu 
4584dd6f977SJaegeuk Kim struct f2fs_move_range {
4594dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4604dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4614dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4624dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4634dd6f977SJaegeuk Kim };
4644dd6f977SJaegeuk Kim 
465e066b83cSJaegeuk Kim struct f2fs_flush_device {
466e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
467e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
468e066b83cSJaegeuk Kim };
469e066b83cSJaegeuk Kim 
470f2470371SChao Yu /* for inline stuff */
471f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4727a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4736afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4747a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4757a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
476b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4776afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
478f2470371SChao Yu 
479f2470371SChao Yu /* for inline dir */
480f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
481f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
482f2470371SChao Yu 				BITS_PER_BYTE + 1))
483f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
484f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
485f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
486f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
487f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
488f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
489f2470371SChao Yu 
490e9750824SNamjae Jeon /*
49139a53e0cSJaegeuk Kim  * For INODE and NODE manager
49239a53e0cSJaegeuk Kim  */
4937b3cd7d6SJaegeuk Kim /* for directory operations */
4947b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
495d8c6822aSJaegeuk Kim 	struct inode *inode;
49676a9dd85SChao Yu 	void *bitmap;
4977b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4987b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4997b3cd7d6SJaegeuk Kim 	int max;
50076a9dd85SChao Yu 	int nr_bitmap;
5017b3cd7d6SJaegeuk Kim };
5027b3cd7d6SJaegeuk Kim 
50364c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
50464c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5057b3cd7d6SJaegeuk Kim {
506d8c6822aSJaegeuk Kim 	d->inode = inode;
5077b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
50876a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
509c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5107b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5117b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
51264c24ecbSTomohiro Kusumi }
513cac5a3d8SDongOh Shin 
51464c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
515f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
51664c24ecbSTomohiro Kusumi {
517f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
518f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
519f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
520f2470371SChao Yu 
52164c24ecbSTomohiro Kusumi 	d->inode = inode;
522f2470371SChao Yu 	d->max = entry_cnt;
523f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
524f2470371SChao Yu 	d->bitmap = t;
525f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
526f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
527f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5287b3cd7d6SJaegeuk Kim }
5297b3cd7d6SJaegeuk Kim 
530dbe6a5ffSJaegeuk Kim /*
531dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
532dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
533dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
53439a53e0cSJaegeuk Kim  */
535dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
536dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
537266e97a8SJaegeuk Kim enum {
538266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
539266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
540266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
541266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5424f4124d0SChao Yu 					 * by get_data_block.
54339a53e0cSJaegeuk Kim 					 */
544266e97a8SJaegeuk Kim };
545266e97a8SJaegeuk Kim 
5467735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5477735730dSChao Yu 
548af033b2aSChao Yu /* maximum retry quota flush count */
549af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
550af033b2aSChao Yu 
551a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
55239a53e0cSJaegeuk Kim 
553817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
554817202d9SChao Yu 
55539a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
55613054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
55713054c54SChao Yu 
55813054c54SChao Yu /* number of extent info in extent cache we try to shrink */
55913054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
560c11abd1aSJaegeuk Kim 
56154c2258cSChao Yu struct rb_entry {
56254c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
56354c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
56454c2258cSChao Yu 	unsigned int len;		/* length of the entry */
56554c2258cSChao Yu };
56654c2258cSChao Yu 
56739a53e0cSJaegeuk Kim struct extent_info {
56839a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
569111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
57054c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
57139a53e0cSJaegeuk Kim };
57239a53e0cSJaegeuk Kim 
57313054c54SChao Yu struct extent_node {
574c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
57513054c54SChao Yu 	struct extent_info ei;		/* extent info */
57654c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
577201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
57813054c54SChao Yu };
57913054c54SChao Yu 
58013054c54SChao Yu struct extent_tree {
58113054c54SChao Yu 	nid_t ino;			/* inode number */
5824dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
58362c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5843e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
585137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
58613054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
58768e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
588b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
58913054c54SChao Yu };
59013054c54SChao Yu 
59139a53e0cSJaegeuk Kim /*
592003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
593003a3e1dSJaegeuk Kim  *
594003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
595003a3e1dSJaegeuk Kim  */
596003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
597003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5987f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5997f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6007f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
601003a3e1dSJaegeuk Kim 
602003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
603003a3e1dSJaegeuk Kim 	block_t m_pblk;
604003a3e1dSJaegeuk Kim 	block_t m_lblk;
605003a3e1dSJaegeuk Kim 	unsigned int m_len;
606003a3e1dSJaegeuk Kim 	unsigned int m_flags;
607da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
608c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
609d5097be5SHyunchul Lee 	int m_seg_type;
610f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
611003a3e1dSJaegeuk Kim };
612003a3e1dSJaegeuk Kim 
613e2b4e2bcSChao Yu /* for flag in get_data_block */
614f2220c7fSQiuyang Sun enum {
615f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
616f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
617f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6180a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
619f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
620f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
621c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
622f2220c7fSQiuyang Sun };
623e2b4e2bcSChao Yu 
624003a3e1dSJaegeuk Kim /*
62539a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
62639a53e0cSJaegeuk Kim  */
62739a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
628354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
629cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
630e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
63126787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
632b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
63353fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
63439a53e0cSJaegeuk Kim 
635797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
636797c1cb5SChao Yu 
637b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
638b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
639b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
640b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
641b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
642b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
643cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
644cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
645cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
646e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
647e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
64826787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
64926787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
650b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
651b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
652b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
653cde4de12SJaegeuk Kim 
654ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
655ab9fa662SJaegeuk Kim 
6562ef79ecbSChao Yu enum {
6572ef79ecbSChao Yu 	GC_FAILURE_PIN,
6582ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6592ef79ecbSChao Yu 	MAX_GC_FAILURE
6602ef79ecbSChao Yu };
6612ef79ecbSChao Yu 
66239a53e0cSJaegeuk Kim struct f2fs_inode_info {
66339a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
66439a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
66539a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
66638431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6671ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6682ef79ecbSChao Yu 	/* for gc failure statistic */
6692ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6706666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
67139a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
67239a53e0cSJaegeuk Kim 
67339a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
67439a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
675d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
676204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
67739a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
67839a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
67988c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
680b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
68139a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
68226de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
68388b88a66SJaegeuk Kim 
6840abd675eSChao Yu #ifdef CONFIG_QUOTA
6850abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6860abd675eSChao Yu 
6870abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6880abd675eSChao Yu 	qsize_t i_reserved_quota;
6890abd675eSChao Yu #endif
6900f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6910f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
69257864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
69388b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6947a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
69588b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6963e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
697b2532c69SChao Yu 
698b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
699b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
7005a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
70127161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
702f2470371SChao Yu 
7037a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7045c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7056afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
70624b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
70724b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
70839a53e0cSJaegeuk Kim };
70939a53e0cSJaegeuk Kim 
71039a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
711bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
71239a53e0cSJaegeuk Kim {
713bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
714bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
715bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
71639a53e0cSJaegeuk Kim }
71739a53e0cSJaegeuk Kim 
71839a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
71939a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
72039a53e0cSJaegeuk Kim {
72139a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7224d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
72339a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
72439a53e0cSJaegeuk Kim }
72539a53e0cSJaegeuk Kim 
726429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
727429511cdSChao Yu 						u32 blk, unsigned int len)
728429511cdSChao Yu {
729429511cdSChao Yu 	ei->fofs = fofs;
730429511cdSChao Yu 	ei->blk = blk;
731429511cdSChao Yu 	ei->len = len;
732429511cdSChao Yu }
733429511cdSChao Yu 
734004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
73535ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
736004b6862SChao Yu {
7379a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
73835ec7d57SChao Yu 		(back->len + front->len <= max_len);
739004b6862SChao Yu }
740004b6862SChao Yu 
741004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
74235ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
743004b6862SChao Yu {
74435ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
745004b6862SChao Yu }
746004b6862SChao Yu 
747004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
74835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
749004b6862SChao Yu {
75035ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
751004b6862SChao Yu }
752004b6862SChao Yu 
753429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
754429511cdSChao Yu 						struct extent_info *front)
755429511cdSChao Yu {
756429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
757429511cdSChao Yu 			back->blk + back->len == front->blk);
758429511cdSChao Yu }
759429511cdSChao Yu 
760429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
761429511cdSChao Yu 						struct extent_info *back)
762429511cdSChao Yu {
763429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
764429511cdSChao Yu }
765429511cdSChao Yu 
766429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
767429511cdSChao Yu 						struct extent_info *front)
768429511cdSChao Yu {
769429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
770429511cdSChao Yu }
771429511cdSChao Yu 
772cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
773b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
774b430f726SZhikang Zhang 						struct extent_node *en)
7754abd3f5aSChao Yu {
776205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7774abd3f5aSChao Yu 		et->largest = en->ei;
778b430f726SZhikang Zhang 		et->largest_updated = true;
779205b9822SJaegeuk Kim 	}
7804abd3f5aSChao Yu }
7814abd3f5aSChao Yu 
7829a4ffdf5SChao Yu /*
7839a4ffdf5SChao Yu  * For free nid management
7849a4ffdf5SChao Yu  */
7859a4ffdf5SChao Yu enum nid_state {
7869a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7879a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7889a4ffdf5SChao Yu 	MAX_NID_STATE,
789b8559dc2SChao Yu };
790b8559dc2SChao Yu 
79139a53e0cSJaegeuk Kim struct f2fs_nm_info {
79239a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
79339a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
79404d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
79539a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
796cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
797ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7982304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
79939a53e0cSJaegeuk Kim 
80039a53e0cSJaegeuk Kim 	/* NAT cache management */
80139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
802309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
803b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
80439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
80522969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
806309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
807aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
80822ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
80939a53e0cSJaegeuk Kim 
81039a53e0cSJaegeuk Kim 	/* free node ids management */
8118a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
8129a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
8139a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
814b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
81539a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
816bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8174ac91242SChao Yu 	unsigned char *nat_block_bitmap;
818586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
81939a53e0cSJaegeuk Kim 
82039a53e0cSJaegeuk Kim 	/* for checkpoint */
82139a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
82222ad0b6aSJaegeuk Kim 
82322ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
82422ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
82522ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
82622ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
827599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
828599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
829599a09b2SChao Yu #endif
83039a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
83139a53e0cSJaegeuk Kim };
83239a53e0cSJaegeuk Kim 
83339a53e0cSJaegeuk Kim /*
83439a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
83539a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
83639a53e0cSJaegeuk Kim  * by the data offset in a file.
83739a53e0cSJaegeuk Kim  */
83839a53e0cSJaegeuk Kim struct dnode_of_data {
83939a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
84039a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
84139a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
84239a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
84339a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
84439a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
84593bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8463cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8473cf45747SChao Yu 	char max_level;			/* level of current page located */
84839a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
84939a53e0cSJaegeuk Kim };
85039a53e0cSJaegeuk Kim 
85139a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
85239a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
85339a53e0cSJaegeuk Kim {
854d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
85539a53e0cSJaegeuk Kim 	dn->inode = inode;
85639a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
85739a53e0cSJaegeuk Kim 	dn->node_page = npage;
85839a53e0cSJaegeuk Kim 	dn->nid = nid;
85939a53e0cSJaegeuk Kim }
86039a53e0cSJaegeuk Kim 
86139a53e0cSJaegeuk Kim /*
86239a53e0cSJaegeuk Kim  * For SIT manager
86339a53e0cSJaegeuk Kim  *
86439a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
86539a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
86639a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
86739a53e0cSJaegeuk Kim  * respectively.
86839a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
86939a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
87039a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
87139a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
87239a53e0cSJaegeuk Kim  * data and 8 for node logs.
87339a53e0cSJaegeuk Kim  */
87439a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
87539a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
87639a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
87739a53e0cSJaegeuk Kim 
87839a53e0cSJaegeuk Kim enum {
87939a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
88039a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
88139a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
88239a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
88339a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
88439a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
88538aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
88639a53e0cSJaegeuk Kim };
88739a53e0cSJaegeuk Kim 
8886b4afdd7SJaegeuk Kim struct flush_cmd {
8896b4afdd7SJaegeuk Kim 	struct completion wait;
890721bd4d5SGu Zheng 	struct llist_node llnode;
89139d787beSChao Yu 	nid_t ino;
8926b4afdd7SJaegeuk Kim 	int ret;
8936b4afdd7SJaegeuk Kim };
8946b4afdd7SJaegeuk Kim 
895a688b9d9SGu Zheng struct flush_cmd_control {
896a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
897a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8988b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
89972691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
900721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
901721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
902a688b9d9SGu Zheng };
903a688b9d9SGu Zheng 
90439a53e0cSJaegeuk Kim struct f2fs_sm_info {
90539a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
90639a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
90739a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
90839a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
90939a53e0cSJaegeuk Kim 
9102b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
9112b60311dSChao Yu 
91239a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
91339a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
91439a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
91539a53e0cSJaegeuk Kim 
91639a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
91739a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
91839a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
91939a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
92081eb8d6eSJaegeuk Kim 
92181eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
92281eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9237fd9e544SJaegeuk Kim 
924bba681cbSJaegeuk Kim 	/* for batched trimming */
925bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
926bba681cbSJaegeuk Kim 
927184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
928184a5cd2SChao Yu 
929216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
930216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
931c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
932853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
933ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
934a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
9356b4afdd7SJaegeuk Kim 
9366b4afdd7SJaegeuk Kim 	/* for flush command control */
937b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
938a688b9d9SGu Zheng 
9390b54fb84SJaegeuk Kim 	/* for discard command control */
9400b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
94139a53e0cSJaegeuk Kim };
94239a53e0cSJaegeuk Kim 
94339a53e0cSJaegeuk Kim /*
94439a53e0cSJaegeuk Kim  * For superblock
94539a53e0cSJaegeuk Kim  */
94639a53e0cSJaegeuk Kim /*
94739a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
94839a53e0cSJaegeuk Kim  *
94939a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
95039a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
95139a53e0cSJaegeuk Kim  */
95236951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
95339a53e0cSJaegeuk Kim enum count_type {
95439a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
955c227f912SChao Yu 	F2FS_DIRTY_DATA,
9562c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
95739a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
95839a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9598dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9600f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
96136951b38SChao Yu 	F2FS_WB_CP_DATA,
96236951b38SChao Yu 	F2FS_WB_DATA,
9635f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
9645f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
9655f9abab4SJaegeuk Kim 	F2FS_RD_META,
96602b16d0aSChao Yu 	F2FS_DIO_WRITE,
96702b16d0aSChao Yu 	F2FS_DIO_READ,
96839a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
96939a53e0cSJaegeuk Kim };
97039a53e0cSJaegeuk Kim 
97139a53e0cSJaegeuk Kim /*
972e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
97339a53e0cSJaegeuk Kim  * The available types are:
97439a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
97539a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
97639a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
97739a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
97839a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
97939a53e0cSJaegeuk Kim  *			with waiting the bio's completion
98039a53e0cSJaegeuk Kim  * ...			Only can be used with META.
98139a53e0cSJaegeuk Kim  */
9827d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
98339a53e0cSJaegeuk Kim enum page_type {
98439a53e0cSJaegeuk Kim 	DATA,
98539a53e0cSJaegeuk Kim 	NODE,
98639a53e0cSJaegeuk Kim 	META,
98739a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
98839a53e0cSJaegeuk Kim 	META_FLUSH,
9898ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9908ce67cb0SJaegeuk Kim 	INMEM_DROP,
9918c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
99228bc106bSChao Yu 	INMEM_REVOKE,
9938ce67cb0SJaegeuk Kim 	IPU,
9948ce67cb0SJaegeuk Kim 	OPU,
99539a53e0cSJaegeuk Kim };
99639a53e0cSJaegeuk Kim 
997a912b54dSJaegeuk Kim enum temp_type {
998a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
999a912b54dSJaegeuk Kim 	WARM,
1000a912b54dSJaegeuk Kim 	COLD,
1001a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1002a912b54dSJaegeuk Kim };
1003a912b54dSJaegeuk Kim 
1004cc15620bSJaegeuk Kim enum need_lock_type {
1005cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1006cc15620bSJaegeuk Kim 	LOCK_DONE,
1007cc15620bSJaegeuk Kim 	LOCK_RETRY,
1008cc15620bSJaegeuk Kim };
1009cc15620bSJaegeuk Kim 
1010a5fd5050SChao Yu enum cp_reason_type {
1011a5fd5050SChao Yu 	CP_NO_NEEDED,
1012a5fd5050SChao Yu 	CP_NON_REGULAR,
1013a5fd5050SChao Yu 	CP_HARDLINK,
1014a5fd5050SChao Yu 	CP_SB_NEED_CP,
1015a5fd5050SChao Yu 	CP_WRONG_PINO,
1016a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1017a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1018a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1019a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10200a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1021a5fd5050SChao Yu };
1022a5fd5050SChao Yu 
1023b0af6d49SChao Yu enum iostat_type {
1024b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
1025b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
1026b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1027b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1028b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1029b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1030b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1031b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1032b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1033b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1034b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1035b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1036b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1037b0af6d49SChao Yu 	NR_IO_TYPE,
1038b0af6d49SChao Yu };
1039b0af6d49SChao Yu 
1040458e6197SJaegeuk Kim struct f2fs_io_info {
104105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
104239d787beSChao Yu 	nid_t ino;		/* inode number */
1043458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1044a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
104504d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1046ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10477a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
104828bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
104905ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10504375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1051fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1052d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1053cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1054fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10556dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1056fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
1057b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1058578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
10598648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
10608648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
10617735730dSChao Yu 	unsigned char version;		/* version of the node */
1062458e6197SJaegeuk Kim };
1063458e6197SJaegeuk Kim 
106468afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10651ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1066458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10671ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10681ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1069458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1070df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1071fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1072fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10731ff7bd3bSJaegeuk Kim };
10741ff7bd3bSJaegeuk Kim 
10753c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10763c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10773c62be17SJaegeuk Kim struct f2fs_dev_info {
10783c62be17SJaegeuk Kim 	struct block_device *bdev;
10793c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10803c62be17SJaegeuk Kim 	unsigned int total_segments;
10813c62be17SJaegeuk Kim 	block_t start_blk;
10823c62be17SJaegeuk Kim 	block_t end_blk;
10833c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10843c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
108595175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
10863c62be17SJaegeuk Kim #endif
10873c62be17SJaegeuk Kim };
10883c62be17SJaegeuk Kim 
1089c227f912SChao Yu enum inode_type {
1090c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1091c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10920f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
109357864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1094c227f912SChao Yu 	NR_INODE_TYPE,
1095c227f912SChao Yu };
1096c227f912SChao Yu 
109767298804SChao Yu /* for inner inode cache management */
109867298804SChao Yu struct inode_management {
109967298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
110067298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
110167298804SChao Yu 	struct list_head ino_list;		/* inode list head */
110267298804SChao Yu 	unsigned long ino_num;			/* number of entries */
110367298804SChao Yu };
110467298804SChao Yu 
1105caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1106caf0047eSChao Yu enum {
1107caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1108caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1109caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1110caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1111df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1112bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
111383a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
11141378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
11154354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1116db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1117af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1118af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1119af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
112004f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1121caf0047eSChao Yu };
1122caf0047eSChao Yu 
11236beceb54SJaegeuk Kim enum {
11246beceb54SJaegeuk Kim 	CP_TIME,
1125d0239e1bSJaegeuk Kim 	REQ_TIME,
1126a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1127a7d10cf3SSahitya Tummala 	GC_TIME,
11284354994fSDaniel Rosenberg 	DISABLE_TIME,
112903f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
11306beceb54SJaegeuk Kim 	MAX_TIME,
11316beceb54SJaegeuk Kim };
11326beceb54SJaegeuk Kim 
11330cdd3195SHyunchul Lee enum {
11345b0e9539SJaegeuk Kim 	GC_NORMAL,
11355b0e9539SJaegeuk Kim 	GC_IDLE_CB,
11365b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
11375b0e9539SJaegeuk Kim 	GC_URGENT,
11385b0e9539SJaegeuk Kim };
11395b0e9539SJaegeuk Kim 
11405b0e9539SJaegeuk Kim enum {
11410cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
11420cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1143f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
11440cdd3195SHyunchul Lee };
11450cdd3195SHyunchul Lee 
114607939627SJaegeuk Kim enum {
114707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
114807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
114907939627SJaegeuk Kim };
115007939627SJaegeuk Kim 
115193cf93f1SJunling Zheng enum fsync_mode {
115293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
115393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1154d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
115593cf93f1SJunling Zheng };
115693cf93f1SJunling Zheng 
1157643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1158ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1159ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1160ff62af20SSheng Yong #else
1161ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1162ff62af20SSheng Yong #endif
1163ff62af20SSheng Yong 
116439a53e0cSJaegeuk Kim struct f2fs_sb_info {
116539a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11665e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
116739a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1168846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1169e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1170fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1171853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
117239a53e0cSJaegeuk Kim 
1173178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1174178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1175178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1176178053e2SDamien Le Moal #endif
1177178053e2SDamien Le Moal 
117839a53e0cSJaegeuk Kim 	/* for node-related operations */
117939a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
118039a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
118139a53e0cSJaegeuk Kim 
118239a53e0cSJaegeuk Kim 	/* for segment-related operations */
118339a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11841ff7bd3bSJaegeuk Kim 
11851ff7bd3bSJaegeuk Kim 	/* for bio operations */
1186a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1187107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1188107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11890a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
119039a53e0cSJaegeuk Kim 
119139a53e0cSJaegeuk Kim 	/* for checkpoint */
119239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11938508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1194aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
119539a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
119639936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1197b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1198b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
119959c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1200fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
12016beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
12026beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
120339a53e0cSJaegeuk Kim 
120467298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
12056451e041SJaegeuk Kim 
120650fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
120750fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
120850fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
120950fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
121050fa53ecSChao Yu 
12116451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
12120d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
121339a53e0cSJaegeuk Kim 
1214c227f912SChao Yu 	/* for inode management */
1215c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1216c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1217040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
121839a53e0cSJaegeuk Kim 
121913054c54SChao Yu 	/* for extent tree cache */
122013054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
12215e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
122213054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
122313054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
12247441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1225137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
122674fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
122713054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
122813054c54SChao Yu 
122939a53e0cSJaegeuk Kim 	/* basic filesystem units */
123039a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
123139a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
123239a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
123339a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
123439a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
123539a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
123639a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
123739a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
123839a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
123939a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
124039a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
124104f0b2eaSQiuyang Sun 	struct mutex resize_mutex;		/* for resize exclusion */
124239a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
124339a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1244e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1245ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1246f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
124739a53e0cSJaegeuk Kim 
124839a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
124939a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1250a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
125139a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1252daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
125380d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1254daeb433eSChao Yu 
12554354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
12564354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
12574354994fSDaniel Rosenberg 
1258292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1259db6ec53bSJaegeuk Kim 	struct rw_semaphore quota_sem;		/* blocking cp for flags */
1260292c196aSChao Yu 
1261523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
126235782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
126341382ec4SJaegeuk Kim 	/* # of allocated blocks */
126441382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
126539a53e0cSJaegeuk Kim 
1266687de7f1SJaegeuk Kim 	/* writeback control */
1267c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1268687de7f1SJaegeuk Kim 
1269513c5f37SJaegeuk Kim 	/* valid inode count */
1270513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1271513c5f37SJaegeuk Kim 
127239a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
127339a53e0cSJaegeuk Kim 
127439a53e0cSJaegeuk Kim 	/* for cleaning operations */
127539a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
127639a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12775ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12785b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1279e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
12802ef79ecbSChao Yu 	/* for skip statistic */
12812ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
12826f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
128339a53e0cSJaegeuk Kim 
12841ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12851ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12861ad71a27SJaegeuk Kim 
1287b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1288b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1289e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1290e3080b01SChao Yu 	unsigned int migration_granularity;
1291b1c57c1cSJaegeuk Kim 
129239a53e0cSJaegeuk Kim 	/*
129339a53e0cSJaegeuk Kim 	 * for stat information.
129439a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
129539a53e0cSJaegeuk Kim 	 */
129635b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
129739a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1298b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
129939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
130039a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1301b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
13025b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
13035b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
13045b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
13055b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1306d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
130703e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
130803e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
130926a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1310648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
131126a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1312648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
131339a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
1314274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1315274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
131633fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
131735b09d82SNamjae Jeon #endif
131839a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1319b59d0baeSNamjae Jeon 
1320b0af6d49SChao Yu 	/* For app/fs IO statistics */
1321b0af6d49SChao Yu 	spinlock_t iostat_lock;
1322b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1323b0af6d49SChao Yu 	bool iostat_enable;
1324b0af6d49SChao Yu 
1325b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1326b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1327b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
13282658e50dSJaegeuk Kim 
13292658e50dSJaegeuk Kim 	/* For shrinker support */
13302658e50dSJaegeuk Kim 	struct list_head s_list;
13313c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
13323c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
13331228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
13341228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
13352658e50dSJaegeuk Kim 	struct mutex umount_mutex;
13362658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
13378f1dbbbbSShuoran Liu 
13388f1dbbbbSShuoran Liu 	/* For write statistics */
13398f1dbbbbSShuoran Liu 	u64 sectors_written_start;
13408f1dbbbbSShuoran Liu 	u64 kbytes_written;
134143b6573bSKeith Mok 
134243b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
134343b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
13441ecc0c5cSChao Yu 
1345704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1346704956ecSChao Yu 	__u32 s_chksum_seed;
134739a53e0cSJaegeuk Kim };
134839a53e0cSJaegeuk Kim 
134902b16d0aSChao Yu struct f2fs_private_dio {
135002b16d0aSChao Yu 	struct inode *inode;
135102b16d0aSChao Yu 	void *orig_private;
135202b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
135302b16d0aSChao Yu 	bool write;
135402b16d0aSChao Yu };
135502b16d0aSChao Yu 
13561ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
135755523519SChao Yu #define f2fs_show_injection_info(type)					\
1358d75f773cSSakari Ailus 	printk_ratelimited("%sF2FS-fs : inject %s in %s of %pS\n",	\
13592d3a5856SGao Xiang 		KERN_INFO, f2fs_fault_name[type],			\
136055523519SChao Yu 		__func__, __builtin_return_address(0))
13611ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13621ecc0c5cSChao Yu {
136363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
13641ecc0c5cSChao Yu 
13651ecc0c5cSChao Yu 	if (!ffi->inject_rate)
13661ecc0c5cSChao Yu 		return false;
13671ecc0c5cSChao Yu 
13681ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
13691ecc0c5cSChao Yu 		return false;
13701ecc0c5cSChao Yu 
13711ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
13721ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
13731ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
13741ecc0c5cSChao Yu 		return true;
13751ecc0c5cSChao Yu 	}
13761ecc0c5cSChao Yu 	return false;
13771ecc0c5cSChao Yu }
13787fa750a1SArnd Bergmann #else
13797fa750a1SArnd Bergmann #define f2fs_show_injection_info(type) do { } while (0)
13807fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13817fa750a1SArnd Bergmann {
13827fa750a1SArnd Bergmann 	return false;
13837fa750a1SArnd Bergmann }
13841ecc0c5cSChao Yu #endif
13851ecc0c5cSChao Yu 
13860916878dSDamien Le Moal /*
13870916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
13880916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
13890916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
13900916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
13910916878dSDamien Le Moal  */
13920916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
13930916878dSDamien Le Moal {
13940916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
13950916878dSDamien Le Moal }
13960916878dSDamien Le Moal 
13978f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13988f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13998f1dbbbbSShuoran Liu  */
14008f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1401dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
140268afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
14038f1dbbbbSShuoran Liu 
14046beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
14056beceb54SJaegeuk Kim {
1406a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1407a7d10cf3SSahitya Tummala 
1408a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1409a7d10cf3SSahitya Tummala 
1410a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1411a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1412a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1413a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1414a7d10cf3SSahitya Tummala 	}
14156beceb54SJaegeuk Kim }
14166beceb54SJaegeuk Kim 
14176beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
14186beceb54SJaegeuk Kim {
14196bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
14206beceb54SJaegeuk Kim 
14216beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
14226beceb54SJaegeuk Kim }
14236beceb54SJaegeuk Kim 
1424a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1425a7d10cf3SSahitya Tummala 						int type)
1426a7d10cf3SSahitya Tummala {
1427a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1428a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1429a7d10cf3SSahitya Tummala 	long delta;
1430a7d10cf3SSahitya Tummala 
1431a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1432a7d10cf3SSahitya Tummala 	if (delta > 0)
1433a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1434a7d10cf3SSahitya Tummala 
1435a7d10cf3SSahitya Tummala 	return wait_ms;
1436a7d10cf3SSahitya Tummala }
1437a7d10cf3SSahitya Tummala 
143839a53e0cSJaegeuk Kim /*
143939a53e0cSJaegeuk Kim  * Inline functions
144039a53e0cSJaegeuk Kim  */
1441416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1442704956ecSChao Yu 			      const void *address, unsigned int length)
1443704956ecSChao Yu {
1444704956ecSChao Yu 	struct {
1445704956ecSChao Yu 		struct shash_desc shash;
1446704956ecSChao Yu 		char ctx[4];
1447704956ecSChao Yu 	} desc;
1448704956ecSChao Yu 	int err;
1449704956ecSChao Yu 
1450704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1451704956ecSChao Yu 
1452704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1453704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1454704956ecSChao Yu 
1455704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1456704956ecSChao Yu 	BUG_ON(err);
1457704956ecSChao Yu 
1458704956ecSChao Yu 	return *(u32 *)desc.ctx;
1459704956ecSChao Yu }
1460704956ecSChao Yu 
1461416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1462416d2dbbSChao Yu 			   unsigned int length)
1463416d2dbbSChao Yu {
1464416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1465416d2dbbSChao Yu }
1466416d2dbbSChao Yu 
1467416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1468416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1469416d2dbbSChao Yu {
1470416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1471416d2dbbSChao Yu }
1472416d2dbbSChao Yu 
1473416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1474416d2dbbSChao Yu 			      const void *address, unsigned int length)
1475416d2dbbSChao Yu {
1476416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1477416d2dbbSChao Yu }
1478416d2dbbSChao Yu 
147939a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
148039a53e0cSJaegeuk Kim {
148139a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
148239a53e0cSJaegeuk Kim }
148339a53e0cSJaegeuk Kim 
148439a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
148539a53e0cSJaegeuk Kim {
148639a53e0cSJaegeuk Kim 	return sb->s_fs_info;
148739a53e0cSJaegeuk Kim }
148839a53e0cSJaegeuk Kim 
14894081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
14904081363fSJaegeuk Kim {
14914081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
14924081363fSJaegeuk Kim }
14934081363fSJaegeuk Kim 
14944081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
14954081363fSJaegeuk Kim {
14964081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
14974081363fSJaegeuk Kim }
14984081363fSJaegeuk Kim 
14994081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
15004081363fSJaegeuk Kim {
15014969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
15024081363fSJaegeuk Kim }
15034081363fSJaegeuk Kim 
150439a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
150539a53e0cSJaegeuk Kim {
150639a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
150739a53e0cSJaegeuk Kim }
150839a53e0cSJaegeuk Kim 
150939a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
151039a53e0cSJaegeuk Kim {
151139a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
151239a53e0cSJaegeuk Kim }
151339a53e0cSJaegeuk Kim 
151445590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
151545590710SGu Zheng {
151645590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
151745590710SGu Zheng }
151845590710SGu Zheng 
151958bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
152058bfaf44SJaegeuk Kim {
152158bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
152258bfaf44SJaegeuk Kim }
152358bfaf44SJaegeuk Kim 
152439a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
152539a53e0cSJaegeuk Kim {
152639a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
152739a53e0cSJaegeuk Kim }
152839a53e0cSJaegeuk Kim 
152939a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
153039a53e0cSJaegeuk Kim {
153139a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
153239a53e0cSJaegeuk Kim }
153339a53e0cSJaegeuk Kim 
153439a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
153539a53e0cSJaegeuk Kim {
153639a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
153739a53e0cSJaegeuk Kim }
153839a53e0cSJaegeuk Kim 
153939a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
154039a53e0cSJaegeuk Kim {
154139a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
154239a53e0cSJaegeuk Kim }
154339a53e0cSJaegeuk Kim 
154439a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
154539a53e0cSJaegeuk Kim {
154639a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
154739a53e0cSJaegeuk Kim }
154839a53e0cSJaegeuk Kim 
15499df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
15509df27d98SGu Zheng {
15519df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
15529df27d98SGu Zheng }
15539df27d98SGu Zheng 
15544ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
15554ef51a8fSJaegeuk Kim {
15564ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
15574ef51a8fSJaegeuk Kim }
15584ef51a8fSJaegeuk Kim 
1559caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
156039a53e0cSJaegeuk Kim {
1561fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
156239a53e0cSJaegeuk Kim }
156339a53e0cSJaegeuk Kim 
1564caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
156539a53e0cSJaegeuk Kim {
1566fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1567caf0047eSChao Yu }
1568caf0047eSChao Yu 
1569caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1570caf0047eSChao Yu {
1571fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
157239a53e0cSJaegeuk Kim }
157339a53e0cSJaegeuk Kim 
1574d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1575d71b5564SJaegeuk Kim {
1576d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1577d71b5564SJaegeuk Kim }
1578d71b5564SJaegeuk Kim 
1579ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1580ea676733SJaegeuk Kim {
1581ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1582ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1583ea676733SJaegeuk Kim 	return 0;
1584ea676733SJaegeuk Kim }
1585ea676733SJaegeuk Kim 
1586ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1587ced2c7eaSKinglong Mee {
1588ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1589ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1590ced2c7eaSKinglong Mee }
1591ced2c7eaSKinglong Mee 
1592aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
159325ca923bSJaegeuk Kim {
159425ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1595aaec2b1dSChao Yu 
159625ca923bSJaegeuk Kim 	return ckpt_flags & f;
159725ca923bSJaegeuk Kim }
159825ca923bSJaegeuk Kim 
1599aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
160025ca923bSJaegeuk Kim {
1601aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1602aaec2b1dSChao Yu }
1603aaec2b1dSChao Yu 
1604aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1605aaec2b1dSChao Yu {
1606aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1607aaec2b1dSChao Yu 
1608aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
160925ca923bSJaegeuk Kim 	ckpt_flags |= f;
161025ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
161125ca923bSJaegeuk Kim }
161225ca923bSJaegeuk Kim 
1613aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
161425ca923bSJaegeuk Kim {
1615d1aa2453SChao Yu 	unsigned long flags;
1616d1aa2453SChao Yu 
1617d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1618aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1619d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1620aaec2b1dSChao Yu }
1621aaec2b1dSChao Yu 
1622aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1623aaec2b1dSChao Yu {
1624aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1625aaec2b1dSChao Yu 
1626aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
162725ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
162825ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
162925ca923bSJaegeuk Kim }
163025ca923bSJaegeuk Kim 
1631aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1632aaec2b1dSChao Yu {
1633d1aa2453SChao Yu 	unsigned long flags;
1634d1aa2453SChao Yu 
1635d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1636aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1637d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1638aaec2b1dSChao Yu }
1639aaec2b1dSChao Yu 
164022ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
164122ad0b6aSJaegeuk Kim {
1642d1aa2453SChao Yu 	unsigned long flags;
1643*0921835cSChao Yu 	unsigned char *nat_bits;
1644d1aa2453SChao Yu 
1645a742fd41SJaegeuk Kim 	/*
1646a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
1647a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
1648a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
1649a742fd41SJaegeuk Kim 	 */
165022ad0b6aSJaegeuk Kim 
165122ad0b6aSJaegeuk Kim 	if (lock)
1652d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
165322ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1654*0921835cSChao Yu 	nat_bits = NM_I(sbi)->nat_bits;
165522ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
165622ad0b6aSJaegeuk Kim 	if (lock)
1657d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
1658*0921835cSChao Yu 
1659*0921835cSChao Yu 	kvfree(nat_bits);
166022ad0b6aSJaegeuk Kim }
166122ad0b6aSJaegeuk Kim 
166222ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
166322ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
166422ad0b6aSJaegeuk Kim {
166522ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
166622ad0b6aSJaegeuk Kim 
1667c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
166822ad0b6aSJaegeuk Kim }
166922ad0b6aSJaegeuk Kim 
1670e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
167139a53e0cSJaegeuk Kim {
1672b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
167339a53e0cSJaegeuk Kim }
167439a53e0cSJaegeuk Kim 
1675cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1676cc15620bSJaegeuk Kim {
1677cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1678cc15620bSJaegeuk Kim }
1679cc15620bSJaegeuk Kim 
1680e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
168139a53e0cSJaegeuk Kim {
1682b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
168339936837SJaegeuk Kim }
168439936837SJaegeuk Kim 
1685e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
168639936837SJaegeuk Kim {
1687b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
168839936837SJaegeuk Kim }
168939936837SJaegeuk Kim 
1690e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
169139936837SJaegeuk Kim {
1692b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
169339a53e0cSJaegeuk Kim }
169439a53e0cSJaegeuk Kim 
1695119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1696119ee914SJaegeuk Kim {
1697119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1698119ee914SJaegeuk Kim 
1699119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1700119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1701119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1702119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1703119ee914SJaegeuk Kim 	return reason;
1704119ee914SJaegeuk Kim }
1705119ee914SJaegeuk Kim 
1706119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1707119ee914SJaegeuk Kim {
1708c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1709119ee914SJaegeuk Kim }
1710119ee914SJaegeuk Kim 
1711119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1712119ee914SJaegeuk Kim {
1713aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1714aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1715119ee914SJaegeuk Kim }
1716119ee914SJaegeuk Kim 
171739a53e0cSJaegeuk Kim /*
171839a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
171939a53e0cSJaegeuk Kim  */
172039a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
172139a53e0cSJaegeuk Kim {
17220eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
17230eb0adadSChao Yu 
1724000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
172539a53e0cSJaegeuk Kim }
172639a53e0cSJaegeuk Kim 
17274bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
17284bc8e9bcSChao Yu {
17294bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
17304bc8e9bcSChao Yu }
17314bc8e9bcSChao Yu 
1732d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1733a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
17347c2e5963SJaegeuk Kim {
1735d8a9a229SJaegeuk Kim 	if (!inode)
1736d8a9a229SJaegeuk Kim 		return true;
17377c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
17387c2e5963SJaegeuk Kim 		return false;
1739d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1740d8a9a229SJaegeuk Kim 		return true;
174163189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
17427c2e5963SJaegeuk Kim 		return true;
174363189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
174463189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
17457c2e5963SJaegeuk Kim 		return true;
1746a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1747162b27aeSJaegeuk Kim 		return true;
17487c2e5963SJaegeuk Kim 	return false;
17497c2e5963SJaegeuk Kim }
17507c2e5963SJaegeuk Kim 
17510abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
17520abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
175346008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
175439a53e0cSJaegeuk Kim {
17550abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1756daeb433eSChao Yu 	block_t avail_user_block_count;
17570abd675eSChao Yu 	int ret;
17580abd675eSChao Yu 
17590abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
17600abd675eSChao Yu 	if (ret)
17610abd675eSChao Yu 		return ret;
1762dd11a5dfSJaegeuk Kim 
176355523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
176455523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
17650abd675eSChao Yu 		release = *count;
17660abd675eSChao Yu 		goto enospc;
176755523519SChao Yu 	}
17687fa750a1SArnd Bergmann 
1769dd11a5dfSJaegeuk Kim 	/*
1770dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1771dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1772dd11a5dfSJaegeuk Kim 	 */
1773dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
177439a53e0cSJaegeuk Kim 
177539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17762555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
177780d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
177880d42145SYunlong Song 					sbi->current_reserved_blocks;
17797e65be49SJaegeuk Kim 
1780a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
178163189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1782a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1783a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
17844354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
1785a4c3ecaaSDaniel Rosenberg 		else
1786a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
1787a4c3ecaaSDaniel Rosenberg 	}
1788daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1789daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
17907e65be49SJaegeuk Kim 		if (diff > *count)
17917e65be49SJaegeuk Kim 			diff = *count;
1792dd11a5dfSJaegeuk Kim 		*count -= diff;
17930abd675eSChao Yu 		release = diff;
17947e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
179546008c6dSChao Yu 		if (!*count) {
179639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
17970abd675eSChao Yu 			goto enospc;
179839a53e0cSJaegeuk Kim 		}
179946008c6dSChao Yu 	}
180039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
180141382ec4SJaegeuk Kim 
180236b877afSDaniel Rosenberg 	if (unlikely(release)) {
180336b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
18040abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
180536b877afSDaniel Rosenberg 	}
18060abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
18070abd675eSChao Yu 	return 0;
18080abd675eSChao Yu 
18090abd675eSChao Yu enospc:
181036b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
18110abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
18120abd675eSChao Yu 	return -ENOSPC;
181339a53e0cSJaegeuk Kim }
181439a53e0cSJaegeuk Kim 
1815dcbb4c10SJoe Perches __printf(2, 3)
1816dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
1817dcbb4c10SJoe Perches 
1818dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
1819dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
1820dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
1821dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
1822dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
1823dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
1824dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
1825dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
1826dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
1827dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
1828dcbb4c10SJoe Perches 
1829da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
183039a53e0cSJaegeuk Kim 						struct inode *inode,
18310eb0adadSChao Yu 						block_t count)
183239a53e0cSJaegeuk Kim {
18330eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
18340eb0adadSChao Yu 
183539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
18369850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
183739a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
183880d42145SYunlong Song 	if (sbi->reserved_blocks &&
183980d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
184080d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
184180d42145SYunlong Song 					sbi->current_reserved_blocks + count);
184239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
18435e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
1844dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
18455e159cd3SChao Yu 			  inode->i_ino,
18465e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
18475e159cd3SChao Yu 			  (unsigned long long)sectors);
18485e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
18495e159cd3SChao Yu 		return;
18505e159cd3SChao Yu 	}
18510abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
185239a53e0cSJaegeuk Kim }
185339a53e0cSJaegeuk Kim 
185439a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
185539a53e0cSJaegeuk Kim {
185635782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
18577c4abcbeSChao Yu 
185820109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
185920109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
186020109873SChao Yu 			count_type == F2FS_DIRTY_META ||
186120109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
186220109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
1863caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
186439a53e0cSJaegeuk Kim }
186539a53e0cSJaegeuk Kim 
1866a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
186739a53e0cSJaegeuk Kim {
1868204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1869c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1870c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18712c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18722c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
187339a53e0cSJaegeuk Kim }
187439a53e0cSJaegeuk Kim 
187539a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
187639a53e0cSJaegeuk Kim {
187735782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
187839a53e0cSJaegeuk Kim }
187939a53e0cSJaegeuk Kim 
1880a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
188139a53e0cSJaegeuk Kim {
18825ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
18835ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
18841fe54f9dSJaegeuk Kim 		return;
18851fe54f9dSJaegeuk Kim 
1886204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1887c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1888c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18892c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18902c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
189139a53e0cSJaegeuk Kim }
189239a53e0cSJaegeuk Kim 
1893523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
189439a53e0cSJaegeuk Kim {
189535782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
189639a53e0cSJaegeuk Kim }
189739a53e0cSJaegeuk Kim 
1898204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1899f8b2c1f9SJaegeuk Kim {
1900204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1901f8b2c1f9SJaegeuk Kim }
1902f8b2c1f9SJaegeuk Kim 
19035ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
19045ac206cfSNamjae Jeon {
19053519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1906523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1907523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1908523be8a6SJaegeuk Kim 
1909523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
19105ac206cfSNamjae Jeon }
19115ac206cfSNamjae Jeon 
191239a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
191339a53e0cSJaegeuk Kim {
19148b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
191539a53e0cSJaegeuk Kim }
191639a53e0cSJaegeuk Kim 
1917f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1918f83a2584SYunlei He {
1919f83a2584SYunlei He 	return sbi->discard_blks;
1920f83a2584SYunlei He }
1921f83a2584SYunlei He 
192239a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
192339a53e0cSJaegeuk Kim {
192439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
192539a53e0cSJaegeuk Kim 
192639a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
192739a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
192839a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
192939a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
193039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
193139a53e0cSJaegeuk Kim 
193239a53e0cSJaegeuk Kim 	return 0;
193339a53e0cSJaegeuk Kim }
193439a53e0cSJaegeuk Kim 
193555141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
193655141486SWanpeng Li {
193755141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
193855141486SWanpeng Li }
193955141486SWanpeng Li 
194039a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
194139a53e0cSJaegeuk Kim {
194239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
19431dbe4152SChangman Lee 	int offset;
19441dbe4152SChangman Lee 
1945199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1946199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1947199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1948b471eb99SChao Yu 		/*
1949b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
1950b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
1951b471eb99SChao Yu 		 */
1952b471eb99SChao Yu 		return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
1953199bc3feSChao Yu 	}
1954199bc3feSChao Yu 
195555141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
19561dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
19571dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
19581dbe4152SChangman Lee 		else
195965b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
19601dbe4152SChangman Lee 	} else {
19611dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
196225ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
196339a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
196439a53e0cSJaegeuk Kim 	}
19651dbe4152SChangman Lee }
196639a53e0cSJaegeuk Kim 
196739a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
196839a53e0cSJaegeuk Kim {
19698508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
197039a53e0cSJaegeuk Kim 
19718508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
197239a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
197339a53e0cSJaegeuk Kim 	return start_addr;
197439a53e0cSJaegeuk Kim }
197539a53e0cSJaegeuk Kim 
19768508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
19778508e44aSJaegeuk Kim {
19788508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
19798508e44aSJaegeuk Kim 
19808508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
19818508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
19828508e44aSJaegeuk Kim 	return start_addr;
19838508e44aSJaegeuk Kim }
19848508e44aSJaegeuk Kim 
19858508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
19868508e44aSJaegeuk Kim {
19878508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
19888508e44aSJaegeuk Kim }
19898508e44aSJaegeuk Kim 
199039a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
199139a53e0cSJaegeuk Kim {
199239a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
199339a53e0cSJaegeuk Kim }
199439a53e0cSJaegeuk Kim 
19950abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1996000519f2SChao Yu 					struct inode *inode, bool is_inode)
199739a53e0cSJaegeuk Kim {
199839a53e0cSJaegeuk Kim 	block_t	valid_block_count;
1999a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2000af033b2aSChao Yu 	int err;
20010abd675eSChao Yu 
2002af033b2aSChao Yu 	if (is_inode) {
2003af033b2aSChao Yu 		if (inode) {
2004af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2005af033b2aSChao Yu 			if (err)
2006af033b2aSChao Yu 				return err;
2007af033b2aSChao Yu 		}
2008af033b2aSChao Yu 	} else {
2009af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2010af033b2aSChao Yu 		if (err)
2011af033b2aSChao Yu 			return err;
20120abd675eSChao Yu 	}
201339a53e0cSJaegeuk Kim 
2014812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2015812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
2016812c6056SChao Yu 		goto enospc;
2017812c6056SChao Yu 	}
2018812c6056SChao Yu 
201939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
202039a53e0cSJaegeuk Kim 
20217e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
20227e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
20237e65be49SJaegeuk Kim 
2024a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
202563189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2026a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
20274354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2028a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
20297e65be49SJaegeuk Kim 
2030a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
203139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
20320abd675eSChao Yu 		goto enospc;
203339a53e0cSJaegeuk Kim 	}
203439a53e0cSJaegeuk Kim 
2035ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2036cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
203739a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
20380abd675eSChao Yu 		goto enospc;
203939a53e0cSJaegeuk Kim 	}
204039a53e0cSJaegeuk Kim 
2041ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2042ef86d709SGu Zheng 	sbi->total_valid_block_count++;
204339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
204439a53e0cSJaegeuk Kim 
20450abd675eSChao Yu 	if (inode) {
20460abd675eSChao Yu 		if (is_inode)
20470abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
20480abd675eSChao Yu 		else
20490abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
20500abd675eSChao Yu 	}
20510abd675eSChao Yu 
205241382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
20530abd675eSChao Yu 	return 0;
20540abd675eSChao Yu 
20550abd675eSChao Yu enospc:
2056af033b2aSChao Yu 	if (is_inode) {
2057af033b2aSChao Yu 		if (inode)
2058af033b2aSChao Yu 			dquot_free_inode(inode);
2059af033b2aSChao Yu 	} else {
20600abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2061af033b2aSChao Yu 	}
20620abd675eSChao Yu 	return -ENOSPC;
206339a53e0cSJaegeuk Kim }
206439a53e0cSJaegeuk Kim 
206539a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2066000519f2SChao Yu 					struct inode *inode, bool is_inode)
206739a53e0cSJaegeuk Kim {
206839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
206939a53e0cSJaegeuk Kim 
20709850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
20719850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
207239a53e0cSJaegeuk Kim 
2073ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2074ef86d709SGu Zheng 	sbi->total_valid_block_count--;
207580d42145SYunlong Song 	if (sbi->reserved_blocks &&
207680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
207780d42145SYunlong Song 		sbi->current_reserved_blocks++;
207839a53e0cSJaegeuk Kim 
207939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
20800abd675eSChao Yu 
2081ea6d7e72SChao Yu 	if (is_inode) {
2082af033b2aSChao Yu 		dquot_free_inode(inode);
2083ea6d7e72SChao Yu 	} else {
2084ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2085dcbb4c10SJoe Perches 			f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
2086ea6d7e72SChao Yu 				  inode->i_ino,
2087ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2088ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2089ea6d7e72SChao Yu 			return;
2090ea6d7e72SChao Yu 		}
20910abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
209239a53e0cSJaegeuk Kim 	}
2093ea6d7e72SChao Yu }
209439a53e0cSJaegeuk Kim 
209539a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
209639a53e0cSJaegeuk Kim {
20978b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
209839a53e0cSJaegeuk Kim }
209939a53e0cSJaegeuk Kim 
210039a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
210139a53e0cSJaegeuk Kim {
2102513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
210339a53e0cSJaegeuk Kim }
210439a53e0cSJaegeuk Kim 
21050e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
210639a53e0cSJaegeuk Kim {
2107513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
210839a53e0cSJaegeuk Kim }
210939a53e0cSJaegeuk Kim 
2110513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
211139a53e0cSJaegeuk Kim {
2112513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
211339a53e0cSJaegeuk Kim }
211439a53e0cSJaegeuk Kim 
2115a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2116a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2117a56c7c6fSJaegeuk Kim {
211882cf4f13SChao Yu 	struct page *page;
2119cac5a3d8SDongOh Shin 
21207fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
212182cf4f13SChao Yu 		if (!for_write)
212282cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
212382cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
212482cf4f13SChao Yu 		else
212582cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2126c41f3cc3SJaegeuk Kim 		if (page)
2127c41f3cc3SJaegeuk Kim 			return page;
2128c41f3cc3SJaegeuk Kim 
212955523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
213055523519SChao Yu 			f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2131c41f3cc3SJaegeuk Kim 			return NULL;
213255523519SChao Yu 		}
21337fa750a1SArnd Bergmann 	}
21347fa750a1SArnd Bergmann 
2135a56c7c6fSJaegeuk Kim 	if (!for_write)
2136a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2137a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2138a56c7c6fSJaegeuk Kim }
2139a56c7c6fSJaegeuk Kim 
214001eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
214101eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
214201eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
214301eccef7SChao Yu {
214401eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
214501eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
214601eccef7SChao Yu 		return NULL;
214701eccef7SChao Yu 	}
21487fa750a1SArnd Bergmann 
214901eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
215001eccef7SChao Yu }
215101eccef7SChao Yu 
21526e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
21536e2c64adSJaegeuk Kim {
21546e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
21556e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
21566e2c64adSJaegeuk Kim 
21576e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
21586e2c64adSJaegeuk Kim 	kunmap(dst);
21596e2c64adSJaegeuk Kim 	kunmap(src);
21606e2c64adSJaegeuk Kim }
21616e2c64adSJaegeuk Kim 
216239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
216339a53e0cSJaegeuk Kim {
2164031fa8ccSJaegeuk Kim 	if (!page)
216539a53e0cSJaegeuk Kim 		return;
216639a53e0cSJaegeuk Kim 
216739a53e0cSJaegeuk Kim 	if (unlock) {
21689850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
216939a53e0cSJaegeuk Kim 		unlock_page(page);
217039a53e0cSJaegeuk Kim 	}
217109cbfeafSKirill A. Shutemov 	put_page(page);
217239a53e0cSJaegeuk Kim }
217339a53e0cSJaegeuk Kim 
217439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
217539a53e0cSJaegeuk Kim {
217639a53e0cSJaegeuk Kim 	if (dn->node_page)
217739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
217839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
217939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
218039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
218139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
218239a53e0cSJaegeuk Kim }
218339a53e0cSJaegeuk Kim 
218439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2185e8512d2eSGu Zheng 					size_t size)
218639a53e0cSJaegeuk Kim {
2187e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
218839a53e0cSJaegeuk Kim }
218939a53e0cSJaegeuk Kim 
21907bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
21917bd59381SGu Zheng 						gfp_t flags)
21927bd59381SGu Zheng {
21937bd59381SGu Zheng 	void *entry;
21947bd59381SGu Zheng 
219580c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
219680c54505SJaegeuk Kim 	if (!entry)
219780c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
21987bd59381SGu Zheng 	return entry;
21997bd59381SGu Zheng }
22007bd59381SGu Zheng 
2201d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2202d62fe971SChao Yu 						int npages, bool no_fail)
2203740432f8SJaegeuk Kim {
2204740432f8SJaegeuk Kim 	struct bio *bio;
2205740432f8SJaegeuk Kim 
2206d62fe971SChao Yu 	if (no_fail) {
2207740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2208740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
220980c54505SJaegeuk Kim 		if (!bio)
221080c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2211740432f8SJaegeuk Kim 		return bio;
2212740432f8SJaegeuk Kim 	}
2213d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2214d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2215d62fe971SChao Yu 		return NULL;
2216d62fe971SChao Yu 	}
22177fa750a1SArnd Bergmann 
2218d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2219d62fe971SChao Yu }
2220740432f8SJaegeuk Kim 
22215f9abab4SJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
22225f9abab4SJaegeuk Kim {
2223f7dfd9f3SPark Ju Hyung 	if (sbi->gc_mode == GC_URGENT)
2224f7dfd9f3SPark Ju Hyung 		return true;
2225f7dfd9f3SPark Ju Hyung 
22265f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
22275f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2228fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2229fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
223011ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
223111ac8ef8SJaegeuk Kim 		return false;
223211ac8ef8SJaegeuk Kim 
223356659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
223411ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
223511ac8ef8SJaegeuk Kim 		return false;
223611ac8ef8SJaegeuk Kim 
223711ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
223872691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
22395f9abab4SJaegeuk Kim 		return false;
224011ac8ef8SJaegeuk Kim 
22415f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
22425f9abab4SJaegeuk Kim }
22435f9abab4SJaegeuk Kim 
22449be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
22459be32d72SJaegeuk Kim 				unsigned long index, void *item)
22469be32d72SJaegeuk Kim {
22479be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
22489be32d72SJaegeuk Kim 		cond_resched();
22499be32d72SJaegeuk Kim }
22509be32d72SJaegeuk Kim 
225139a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
225239a53e0cSJaegeuk Kim 
225339a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
225439a53e0cSJaegeuk Kim {
225545590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2256cac5a3d8SDongOh Shin 
225739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
225839a53e0cSJaegeuk Kim }
225939a53e0cSJaegeuk Kim 
22607a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
22617a2af766SChao Yu {
22627a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
22637a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
22647a2af766SChao Yu }
22657a2af766SChao Yu 
226639a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
226739a53e0cSJaegeuk Kim {
226839a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
226939a53e0cSJaegeuk Kim }
227039a53e0cSJaegeuk Kim 
22717a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
22727a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
22737a2af766SChao Yu 			struct page *node_page, unsigned int offset)
227439a53e0cSJaegeuk Kim {
227539a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
227639a53e0cSJaegeuk Kim 	__le32 *addr_array;
22777a2af766SChao Yu 	int base = 0;
22787a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2279cac5a3d8SDongOh Shin 
228045590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
22817a2af766SChao Yu 
22827a2af766SChao Yu 	/* from GC path only */
2283979f492fSLiFan 	if (is_inode) {
2284979f492fSLiFan 		if (!inode)
22857a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2286979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
22877a2af766SChao Yu 			base = get_extra_isize(inode);
22887a2af766SChao Yu 	}
22897a2af766SChao Yu 
229039a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
22917a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
229239a53e0cSJaegeuk Kim }
229339a53e0cSJaegeuk Kim 
229439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
229539a53e0cSJaegeuk Kim {
229639a53e0cSJaegeuk Kim 	int mask;
229739a53e0cSJaegeuk Kim 
229839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
229939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
230039a53e0cSJaegeuk Kim 	return mask & *addr;
230139a53e0cSJaegeuk Kim }
230239a53e0cSJaegeuk Kim 
2303a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2304a66cdd98SJaegeuk Kim {
2305a66cdd98SJaegeuk Kim 	int mask;
2306a66cdd98SJaegeuk Kim 
2307a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2308a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2309a66cdd98SJaegeuk Kim 	*addr |= mask;
2310a66cdd98SJaegeuk Kim }
2311a66cdd98SJaegeuk Kim 
2312a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2313a66cdd98SJaegeuk Kim {
2314a66cdd98SJaegeuk Kim 	int mask;
2315a66cdd98SJaegeuk Kim 
2316a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2317a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2318a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2319a66cdd98SJaegeuk Kim }
2320a66cdd98SJaegeuk Kim 
232152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
232239a53e0cSJaegeuk Kim {
232339a53e0cSJaegeuk Kim 	int mask;
232439a53e0cSJaegeuk Kim 	int ret;
232539a53e0cSJaegeuk Kim 
232639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
232739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
232839a53e0cSJaegeuk Kim 	ret = mask & *addr;
232939a53e0cSJaegeuk Kim 	*addr |= mask;
233039a53e0cSJaegeuk Kim 	return ret;
233139a53e0cSJaegeuk Kim }
233239a53e0cSJaegeuk Kim 
233352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
233439a53e0cSJaegeuk Kim {
233539a53e0cSJaegeuk Kim 	int mask;
233639a53e0cSJaegeuk Kim 	int ret;
233739a53e0cSJaegeuk Kim 
233839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
233939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
234039a53e0cSJaegeuk Kim 	ret = mask & *addr;
234139a53e0cSJaegeuk Kim 	*addr &= ~mask;
234239a53e0cSJaegeuk Kim 	return ret;
234339a53e0cSJaegeuk Kim }
234439a53e0cSJaegeuk Kim 
2345c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2346c6ac4c0eSGu Zheng {
2347c6ac4c0eSGu Zheng 	int mask;
2348c6ac4c0eSGu Zheng 
2349c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2350c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2351c6ac4c0eSGu Zheng 	*addr ^= mask;
2352c6ac4c0eSGu Zheng }
2353c6ac4c0eSGu Zheng 
235459c84408SChao Yu /*
235536098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
235659c84408SChao Yu  */
235759c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
235859c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
235959c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
236059c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
236159c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
236259c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
236359c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
236459c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
236559c84408SChao Yu 
236659c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
236736098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
236836098557SEric Biggers 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL)
236959c84408SChao Yu 
237059c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
23715043a964SWang Shilong #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL))
237259c84408SChao Yu 
237359c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
237459c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
23755c57132eSChao Yu 
23765c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
23775c57132eSChao Yu {
23785c57132eSChao Yu 	if (S_ISDIR(mode))
23795c57132eSChao Yu 		return flags;
23805c57132eSChao Yu 	else if (S_ISREG(mode))
23815c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
23825c57132eSChao Yu 	else
23835c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
23845c57132eSChao Yu }
23855c57132eSChao Yu 
238639a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
238739a53e0cSJaegeuk Kim enum {
238839a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2389b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
239026de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2391ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
239239a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
239339a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
239439a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2395c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2396c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2397444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
23981001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
239934d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2400fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2401fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
240288b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
240388b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
24045fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
240502a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
24063c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
24071e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2408b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2409510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2410d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2411c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2412dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2413ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
24147a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
24155c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
24161ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
24172ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
241839a53e0cSJaegeuk Kim };
241939a53e0cSJaegeuk Kim 
2420205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2421205b9822SJaegeuk Kim 						int flag, bool set)
242239a53e0cSJaegeuk Kim {
2423205b9822SJaegeuk Kim 	switch (flag) {
2424205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2425205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2426205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
24279ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2428205b9822SJaegeuk Kim 		if (set)
2429205b9822SJaegeuk Kim 			return;
2430f5d5510eSJaegeuk Kim 		/* fall through */
2431205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2432205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
24331ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
24347c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2435205b9822SJaegeuk Kim 	}
243639a53e0cSJaegeuk Kim }
243739a53e0cSJaegeuk Kim 
243891942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
243939a53e0cSJaegeuk Kim {
244091942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
244191942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2442205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
244339a53e0cSJaegeuk Kim }
244439a53e0cSJaegeuk Kim 
244591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
244639a53e0cSJaegeuk Kim {
244791942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
244839a53e0cSJaegeuk Kim }
244939a53e0cSJaegeuk Kim 
245091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
245139a53e0cSJaegeuk Kim {
245291942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
245391942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2454205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
245539a53e0cSJaegeuk Kim }
245639a53e0cSJaegeuk Kim 
245791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2458444c580fSJaegeuk Kim {
245991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
246091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
24617c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
246239a53e0cSJaegeuk Kim }
246339a53e0cSJaegeuk Kim 
2464a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2465a1961246SJaegeuk Kim {
2466a1961246SJaegeuk Kim 	if (inc)
2467a1961246SJaegeuk Kim 		inc_nlink(inode);
2468a1961246SJaegeuk Kim 	else
2469a1961246SJaegeuk Kim 		drop_nlink(inode);
24707c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2471a1961246SJaegeuk Kim }
2472a1961246SJaegeuk Kim 
24738edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
24740abd675eSChao Yu 					block_t diff, bool add, bool claim)
24758edd03c8SJaegeuk Kim {
247626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
247726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
247826de9b11SJaegeuk Kim 
24790abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
24800abd675eSChao Yu 	if (add) {
24810abd675eSChao Yu 		if (claim)
24820abd675eSChao Yu 			dquot_claim_block(inode, diff);
24830abd675eSChao Yu 		else
24840abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
24850abd675eSChao Yu 	} else {
24860abd675eSChao Yu 		dquot_free_block(inode, diff);
24870abd675eSChao Yu 	}
24880abd675eSChao Yu 
24897c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
249026de9b11SJaegeuk Kim 	if (clean || recover)
249126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
24928edd03c8SJaegeuk Kim }
24938edd03c8SJaegeuk Kim 
2494fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2495fc9581c8SJaegeuk Kim {
249626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
249726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
249826de9b11SJaegeuk Kim 
2499fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2500fc9581c8SJaegeuk Kim 		return;
2501fc9581c8SJaegeuk Kim 
2502fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
25037c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
250426de9b11SJaegeuk Kim 	if (clean || recover)
250526de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
250626de9b11SJaegeuk Kim }
250726de9b11SJaegeuk Kim 
2508205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2509205b9822SJaegeuk Kim {
2510205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
25117c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2512205b9822SJaegeuk Kim }
2513205b9822SJaegeuk Kim 
25141ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
25151ad71a27SJaegeuk Kim 					unsigned int count)
25161ad71a27SJaegeuk Kim {
25172ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
25181ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
25191ad71a27SJaegeuk Kim }
25201ad71a27SJaegeuk Kim 
2521205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2522205b9822SJaegeuk Kim {
2523205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
25247c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2525205b9822SJaegeuk Kim }
2526205b9822SJaegeuk Kim 
2527205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2528205b9822SJaegeuk Kim {
2529205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
25307c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2531205b9822SJaegeuk Kim }
2532205b9822SJaegeuk Kim 
253391942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2534444c580fSJaegeuk Kim {
2535205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2536205b9822SJaegeuk Kim 
2537444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2538205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
25391001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2540205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
254134d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2542205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2543b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2544205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2545510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2546205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
25477a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
25487a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
25491ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
25501ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2551444c580fSJaegeuk Kim }
2552444c580fSJaegeuk Kim 
255391942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2554444c580fSJaegeuk Kim {
2555444c580fSJaegeuk Kim 	ri->i_inline = 0;
2556444c580fSJaegeuk Kim 
255791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2558444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
255991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
25601001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
256191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
256234d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
256391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2564b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
256591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2566510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
25677a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
25687a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
25691ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
25701ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
25717a2af766SChao Yu }
25727a2af766SChao Yu 
25737a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
25747a2af766SChao Yu {
25757a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2576444c580fSJaegeuk Kim }
2577444c580fSJaegeuk Kim 
2578987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2579987c7c31SChao Yu {
258091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2581987c7c31SChao Yu }
2582987c7c31SChao Yu 
258381ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2584de93653fSJaegeuk Kim {
2585d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2586d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
2587d02a6e61SChao Yu 	return ALIGN_DOWN(addrs, 1);
2588d02a6e61SChao Yu }
2589d02a6e61SChao Yu 
2590d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
2591d02a6e61SChao Yu {
2592d02a6e61SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, 1);
2593de93653fSJaegeuk Kim }
2594de93653fSJaegeuk Kim 
25956afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
259665985d93SJaegeuk Kim {
2597695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2598cac5a3d8SDongOh Shin 
259965985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2600b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
260165985d93SJaegeuk Kim }
260265985d93SJaegeuk Kim 
260365985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
260465985d93SJaegeuk Kim {
2605622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
26066afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
2607622927f3SChao Yu 	return 0;
260865985d93SJaegeuk Kim }
260965985d93SJaegeuk Kim 
26100dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
26110dbdc2aeSJaegeuk Kim {
261291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
26130dbdc2aeSJaegeuk Kim }
26140dbdc2aeSJaegeuk Kim 
2615b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2616b3d208f9SJaegeuk Kim {
261791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2618b3d208f9SJaegeuk Kim }
2619b3d208f9SJaegeuk Kim 
2620510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2621510022a8SJaegeuk Kim {
262291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2623510022a8SJaegeuk Kim }
2624510022a8SJaegeuk Kim 
26251ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
26261ad71a27SJaegeuk Kim {
26271ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
26281ad71a27SJaegeuk Kim }
26291ad71a27SJaegeuk Kim 
263088b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
263188b88a66SJaegeuk Kim {
263291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
263388b88a66SJaegeuk Kim }
263488b88a66SJaegeuk Kim 
26355fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
26365fe45743SChao Yu {
26375fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
26385fe45743SChao Yu }
26395fe45743SChao Yu 
264002a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
264102a1335fSJaegeuk Kim {
264291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
264302a1335fSJaegeuk Kim }
264402a1335fSJaegeuk Kim 
26453c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
26463c6c2bebSJaegeuk Kim {
264791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
26483c6c2bebSJaegeuk Kim }
26493c6c2bebSJaegeuk Kim 
26501e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
26511e84371fSJaegeuk Kim {
265291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
26531e84371fSJaegeuk Kim }
26541e84371fSJaegeuk Kim 
2655f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
26561001b347SHuajun Li {
2657695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
26587a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2659cac5a3d8SDongOh Shin 
26607a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
26611001b347SHuajun Li }
26621001b347SHuajun Li 
266334d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
266434d67debSChao Yu {
266591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
266634d67debSChao Yu }
266734d67debSChao Yu 
2668b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2669b5492af7SJaegeuk Kim {
2670b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2671b5492af7SJaegeuk Kim }
2672b5492af7SJaegeuk Kim 
2673b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2674b5492af7SJaegeuk Kim {
2675b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
26767c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2677b5492af7SJaegeuk Kim }
2678b5492af7SJaegeuk Kim 
2679b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2680b5492af7SJaegeuk Kim {
2681b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
26827c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2683b5492af7SJaegeuk Kim }
2684b5492af7SJaegeuk Kim 
268526787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
268626787236SJaegeuk Kim {
2687a0d00fadSChao Yu 	bool ret;
2688a0d00fadSChao Yu 
268926787236SJaegeuk Kim 	if (dsync) {
269026787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
269126787236SJaegeuk Kim 
269226787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
269326787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
269426787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
269526787236SJaegeuk Kim 		return ret;
269626787236SJaegeuk Kim 	}
269726787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
269826787236SJaegeuk Kim 			file_keep_isize(inode) ||
2699235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
270026787236SJaegeuk Kim 		return false;
2701a0d00fadSChao Yu 
270224b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2703214c2461SJaegeuk Kim 		return false;
270424b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2705214c2461SJaegeuk Kim 		return false;
270624b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2707214c2461SJaegeuk Kim 		return false;
270824b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2709214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2710214c2461SJaegeuk Kim 		return false;
2711214c2461SJaegeuk Kim 
2712a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2713a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2714a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2715a0d00fadSChao Yu 
2716a0d00fadSChao Yu 	return ret;
2717267378d4SChao Yu }
2718267378d4SChao Yu 
2719deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
272077888c1eSJaegeuk Kim {
2721deeedd71SChao Yu 	return sb_rdonly(sb);
272277888c1eSJaegeuk Kim }
272377888c1eSJaegeuk Kim 
2724744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2725744602cfSJaegeuk Kim {
2726aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2727744602cfSJaegeuk Kim }
2728744602cfSJaegeuk Kim 
2729eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2730eaa693f4SJaegeuk Kim {
2731eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2732eaa693f4SJaegeuk Kim 		return true;
2733eaa693f4SJaegeuk Kim 
2734eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2735eaa693f4SJaegeuk Kim 		return true;
2736eaa693f4SJaegeuk Kim 
2737eaa693f4SJaegeuk Kim 	return false;
2738eaa693f4SJaegeuk Kim }
2739eaa693f4SJaegeuk Kim 
27403e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
27413e72f721SJaegeuk Kim {
2742e4589fa5SSahitya Tummala 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2743e4589fa5SSahitya Tummala 
2744e4589fa5SSahitya Tummala 	if (!test_opt(sbi, EXTENT_CACHE) ||
274591942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
27463e72f721SJaegeuk Kim 		return false;
27473e72f721SJaegeuk Kim 
2748e4589fa5SSahitya Tummala 	/*
2749e4589fa5SSahitya Tummala 	 * for recovered files during mount do not create extents
2750e4589fa5SSahitya Tummala 	 * if shrinker is not registered.
2751e4589fa5SSahitya Tummala 	 */
2752e4589fa5SSahitya Tummala 	if (list_empty(&sbi->s_list))
2753e4589fa5SSahitya Tummala 		return false;
2754e4589fa5SSahitya Tummala 
2755886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
27563e72f721SJaegeuk Kim }
27573e72f721SJaegeuk Kim 
27581ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
27591ecc0c5cSChao Yu 					size_t size, gfp_t flags)
27600414b004SJaegeuk Kim {
27615222595dSJaegeuk Kim 	void *ret;
27625222595dSJaegeuk Kim 
276355523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
276455523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
27652c63feadSJaegeuk Kim 		return NULL;
276655523519SChao Yu 	}
27677fa750a1SArnd Bergmann 
27685222595dSJaegeuk Kim 	ret = kmalloc(size, flags);
27695222595dSJaegeuk Kim 	if (ret)
27705222595dSJaegeuk Kim 		return ret;
27715222595dSJaegeuk Kim 
27725222595dSJaegeuk Kim 	return kvmalloc(size, flags);
27730414b004SJaegeuk Kim }
27740414b004SJaegeuk Kim 
2775acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2776acbf054dSChao Yu 					size_t size, gfp_t flags)
2777acbf054dSChao Yu {
2778acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2779acbf054dSChao Yu }
2780acbf054dSChao Yu 
2781628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2782628b3d14SChao Yu 					size_t size, gfp_t flags)
2783628b3d14SChao Yu {
2784628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2785628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2786628b3d14SChao Yu 		return NULL;
2787628b3d14SChao Yu 	}
27887fa750a1SArnd Bergmann 
2789628b3d14SChao Yu 	return kvmalloc(size, flags);
2790628b3d14SChao Yu }
2791628b3d14SChao Yu 
2792628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2793628b3d14SChao Yu 					size_t size, gfp_t flags)
2794628b3d14SChao Yu {
2795628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2796628b3d14SChao Yu }
2797628b3d14SChao Yu 
27987a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2799f2470371SChao Yu {
28007a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2801f2470371SChao Yu }
2802f2470371SChao Yu 
28036afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
28046afc662eSChao Yu {
28056afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
28066afc662eSChao Yu }
28076afc662eSChao Yu 
28084d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
280991942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2810a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2811a6dda0e6SChristoph Hellwig 
28127a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
28137a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
28147a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
28157a2af766SChao Yu 
28165c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
28175c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
28182f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
28195c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
28202f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
28215c57132eSChao Yu 
2822b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2823b0af6d49SChao Yu {
2824b0af6d49SChao Yu 	int i;
2825b0af6d49SChao Yu 
2826b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2827b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2828b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2829b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2830b0af6d49SChao Yu }
2831b0af6d49SChao Yu 
2832b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2833b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2834b0af6d49SChao Yu {
2835b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2836b0af6d49SChao Yu 		return;
2837b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2838b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2839b0af6d49SChao Yu 
2840b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2841b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2842b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2843b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2844b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2845b0af6d49SChao Yu }
2846b0af6d49SChao Yu 
28472c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
28482c70c5e3SChao Yu 
28496dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
2850c9b60788SChao Yu 
2851e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2852e1da7872SChao Yu 					block_t blkaddr, int type);
2853e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2854e1da7872SChao Yu 					block_t blkaddr, int type)
2855e1da7872SChao Yu {
2856e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2857dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
2858e1da7872SChao Yu 			 blkaddr, type);
2859e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
2860e1da7872SChao Yu 	}
2861e1da7872SChao Yu }
2862e1da7872SChao Yu 
2863e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
28647b525dd0SChao Yu {
28657b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
28667b525dd0SChao Yu 		return false;
28677b525dd0SChao Yu 	return true;
28687b525dd0SChao Yu }
28697b525dd0SChao Yu 
2870240a5915SChao Yu static inline void f2fs_set_page_private(struct page *page,
2871240a5915SChao Yu 						unsigned long data)
2872240a5915SChao Yu {
2873240a5915SChao Yu 	if (PagePrivate(page))
2874240a5915SChao Yu 		return;
2875240a5915SChao Yu 
2876240a5915SChao Yu 	get_page(page);
2877240a5915SChao Yu 	SetPagePrivate(page);
2878240a5915SChao Yu 	set_page_private(page, data);
2879240a5915SChao Yu }
2880240a5915SChao Yu 
2881240a5915SChao Yu static inline void f2fs_clear_page_private(struct page *page)
2882240a5915SChao Yu {
2883240a5915SChao Yu 	if (!PagePrivate(page))
2884240a5915SChao Yu 		return;
2885240a5915SChao Yu 
2886240a5915SChao Yu 	set_page_private(page, 0);
2887240a5915SChao Yu 	ClearPagePrivate(page);
2888240a5915SChao Yu 	f2fs_put_page(page, 0);
2889240a5915SChao Yu }
2890240a5915SChao Yu 
289139a53e0cSJaegeuk Kim /*
289239a53e0cSJaegeuk Kim  * file.c
289339a53e0cSJaegeuk Kim  */
2894cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
28954d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2896c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
2897cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2898a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2899a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2900cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
29014d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
29024d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2903c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2904cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2905cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
290678130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
29071ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
290839a53e0cSJaegeuk Kim 
290939a53e0cSJaegeuk Kim /*
291039a53e0cSJaegeuk Kim  * inode.c
291139a53e0cSJaegeuk Kim  */
2912cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2913704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2914704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2915cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2916cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
29174d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
29184d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
29194d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2920cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2921cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
29224d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
292339a53e0cSJaegeuk Kim 
292439a53e0cSJaegeuk Kim /*
292539a53e0cSJaegeuk Kim  * namei.c
292639a53e0cSJaegeuk Kim  */
29274d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2928b6a06cbbSChao Yu 							bool hot, bool set);
292939a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
293039a53e0cSJaegeuk Kim 
293139a53e0cSJaegeuk Kim /*
293239a53e0cSJaegeuk Kim  * dir.c
293339a53e0cSJaegeuk Kim  */
29344d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
29354d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2936cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2937cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2938cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2939cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
29404d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2941cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
29424d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2943cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2944cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
29454d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2946cac5a3d8SDongOh Shin 			unsigned int current_depth);
29474d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2948cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2949cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2950cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2951cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2952cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2953cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2954cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2955cac5a3d8SDongOh Shin 			struct page **page);
2956cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2957cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2958cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2959cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2960cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2961cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2962cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2963cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
29644d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2965cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
29664d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2967cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2968cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2969cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2970cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2971cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
297239a53e0cSJaegeuk Kim 
2973b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2974b7f7a5e0SAl Viro {
29754d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2976510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2977b7f7a5e0SAl Viro }
2978b7f7a5e0SAl Viro 
297939a53e0cSJaegeuk Kim /*
298039a53e0cSJaegeuk Kim  * super.c
298139a53e0cSJaegeuk Kim  */
2982cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2983cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2984ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2985af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
29864b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2987cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2988cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
29894d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
299039a53e0cSJaegeuk Kim 
299139a53e0cSJaegeuk Kim /*
299239a53e0cSJaegeuk Kim  * hash.c
299339a53e0cSJaegeuk Kim  */
29946332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
29956332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
299639a53e0cSJaegeuk Kim 
299739a53e0cSJaegeuk Kim /*
299839a53e0cSJaegeuk Kim  * node.c
299939a53e0cSJaegeuk Kim  */
300039a53e0cSJaegeuk Kim struct dnode_of_data;
300139a53e0cSJaegeuk Kim struct node_info;
300239a53e0cSJaegeuk Kim 
30034d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
30044d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
300550fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
300650fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
300750fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
300850fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
30094d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
30104d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
30114d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
30127735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
30134d57b86dSChao Yu 						struct node_info *ni);
30144d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
30154d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
30164d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
30174d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
301850fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
301950fa53ecSChao Yu 					unsigned int seq_id);
30204d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
30214d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
30224d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
30234d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
30244d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
30254d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
302648018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
30274d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
302850fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
302950fa53ecSChao Yu 			unsigned int *seq_id);
30304d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
30314d57b86dSChao Yu 			struct writeback_control *wbc,
3032b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3033e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
30344d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
30354d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
30364d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
30374d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
30384d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
30394d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
30404d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
30417735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3042cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
3043edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30444d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
30454d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
30464d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
30474d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
304839a53e0cSJaegeuk Kim 
304939a53e0cSJaegeuk Kim /*
305039a53e0cSJaegeuk Kim  * segment.c
305139a53e0cSJaegeuk Kim  */
30524d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
30534d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
30544d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
30554d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
30564d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
30574d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3058cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3059cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
306039d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
30614d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
30621228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
30634d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
30644d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
30654d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
30664d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
30674d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
306803f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
30694d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
30704d57b86dSChao Yu 					struct cp_control *cpc);
30714354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
30724d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
30734d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
30744d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
30754d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
307604f0b2eaSQiuyang Sun void allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
307704f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
30784d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3079cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
30804d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
30814d57b86dSChao Yu 					struct cp_control *cpc);
30824d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
30834d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
30844d57b86dSChao Yu 					block_t blk_addr);
30854d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3086b0af6d49SChao Yu 						enum iostat_type io_type);
30874d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
30884d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
30894d57b86dSChao Yu 			struct f2fs_io_info *fio);
30904d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
30914d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3092cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3093cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
3094cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3095cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3096cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3097cac5a3d8SDongOh Shin 			bool recover_newaddr);
30984d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3099cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3100fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3101fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
3102cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3103bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
31040ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
31051e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
31061e78e8bdSSahitya Tummala 								block_t len);
31074d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
31084d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
31094d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3110cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
31114d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
31124d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
31134d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
31144d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
31154d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
31164d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
31174d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
31184d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
311939a53e0cSJaegeuk Kim 
312039a53e0cSJaegeuk Kim /*
312139a53e0cSJaegeuk Kim  * checkpoint.c
312239a53e0cSJaegeuk Kim  */
3123cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
31244d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
31254d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
31267735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
31274d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3128e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
31294d57b86dSChao Yu 					block_t blkaddr, int type);
31304d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3131cac5a3d8SDongOh Shin 			int type, bool sync);
31324d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
31334d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3134b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
31354d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
31364d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
31374d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
31384d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
31394d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
314039d787beSChao Yu 					unsigned int devidx, int type);
31414d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
314239d787beSChao Yu 					unsigned int devidx, int type);
3143cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
31444d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
31454d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
31464d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
31474d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
31484d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
31494d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
31504d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
31514d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
31524d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
315350fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
31544d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
31554d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
31564d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
31574d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
315839a53e0cSJaegeuk Kim 
315939a53e0cSJaegeuk Kim /*
316039a53e0cSJaegeuk Kim  * data.c
316139a53e0cSJaegeuk Kim  */
31626dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
31636dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
3164b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3165b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3166bab475c5SChao Yu 				struct inode *inode, struct page *page,
3167bab475c5SChao Yu 				nid_t ino, enum page_type type);
3168b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3169cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
31708648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3171fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3172cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3173cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3174cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
31754d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3176cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
31774d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
31784d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3179cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3180cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3181cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
31824d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3183cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
31844d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
31854d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3186cac5a3d8SDongOh Shin 			bool for_write);
31874d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3188cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
31894d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
319039a86958SChao Yu void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3191cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3192cac5a3d8SDongOh Shin 			int create, int flag);
3193cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3194cac5a3d8SDongOh Shin 			u64 start, u64 len);
31954d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
31964d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3197cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3198cac5a3d8SDongOh Shin 			unsigned int length);
3199cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
32005b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3201cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3202cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
32035b7a487cSWeichao Guo #endif
3204b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
32055ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
320639a53e0cSJaegeuk Kim 
320739a53e0cSJaegeuk Kim /*
320839a53e0cSJaegeuk Kim  * gc.c
320939a53e0cSJaegeuk Kim  */
32104d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
32114d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
32124d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3213e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3214e066b83cSJaegeuk Kim 			unsigned int segno);
32154d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
321604f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
321739a53e0cSJaegeuk Kim 
321839a53e0cSJaegeuk Kim /*
321939a53e0cSJaegeuk Kim  * recovery.c
322039a53e0cSJaegeuk Kim  */
32214d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
32224d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
322339a53e0cSJaegeuk Kim 
322439a53e0cSJaegeuk Kim /*
322539a53e0cSJaegeuk Kim  * debug.c
322639a53e0cSJaegeuk Kim  */
322739a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
322839a53e0cSJaegeuk Kim struct f2fs_stat_info {
322939a53e0cSJaegeuk Kim 	struct list_head stat_list;
323039a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
323139a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
323239a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
32335b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
32345b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3235c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
32362c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
32372c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
323835782b23SJaegeuk Kim 	int inmem_pages;
32392c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
32405b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
32415b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
324239a53e0cSJaegeuk Kim 	int total_count, utilization;
32438b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
32445f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
324502b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3246274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
324714d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
324814d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
32495f32366aSChao Yu 	int nr_discard_cmd;
3250d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3251a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3252648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3253f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
325439a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
325539a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
325639a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
325739a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
325842190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
325939a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3260e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
326139a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3262e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
32632ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
326439a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
326539a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
326639a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
326739a53e0cSJaegeuk Kim 
3268b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
326939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
327039a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3271b9a2c252SChangman Lee 	unsigned int inplace_count;
32729edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
327339a53e0cSJaegeuk Kim };
327439a53e0cSJaegeuk Kim 
3275963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3276963d4f7dSGu Zheng {
3277963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3278963d4f7dSGu Zheng }
3279963d4f7dSGu Zheng 
3280942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
328142190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
328239a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3283dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
3284274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3285274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
328633fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
328733fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
32885b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
32895b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
32905b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
32915b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3292d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3293d5e8f6c9SChao Yu 	do {								\
3294d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3295d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3296d5e8f6c9SChao Yu 	} while (0)
3297d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3298d5e8f6c9SChao Yu 	do {								\
3299d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3300d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3301d5e8f6c9SChao Yu 	} while (0)
33020dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
33030dbdc2aeSJaegeuk Kim 	do {								\
33040dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
330503e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
33060dbdc2aeSJaegeuk Kim 	} while (0)
33070dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
33080dbdc2aeSJaegeuk Kim 	do {								\
33090dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
331003e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
33110dbdc2aeSJaegeuk Kim 	} while (0)
33123289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
33133289c061SJaegeuk Kim 	do {								\
33143289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
331503e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
33163289c061SJaegeuk Kim 	} while (0)
33173289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
33183289c061SJaegeuk Kim 	do {								\
33193289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
332003e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
33213289c061SJaegeuk Kim 	} while (0)
3322b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3323b63e7be5SChao Yu 	do {								\
3324b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3325b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3326b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3327b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3328b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3329b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3330b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3331b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3332b63e7be5SChao Yu 	} while (0)
3333dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3334dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3335dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3336dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3337b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3338b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
333926a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3340cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
334126a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3342cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
334326a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
334426a28a0cSJaegeuk Kim 	do {								\
334526a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
334626a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
334726a28a0cSJaegeuk Kim 		if (cur > max)						\
334826a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
334926a28a0cSJaegeuk Kim 	} while (0)
3350648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3351648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3352648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3353648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3354648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3355648d50baSChao Yu 	do {								\
3356648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3357648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3358648d50baSChao Yu 		if (cur > max)						\
3359648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3360648d50baSChao Yu 	} while (0)
3361e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
336239a53e0cSJaegeuk Kim 	do {								\
3363963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
336468afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
336568afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
336639a53e0cSJaegeuk Kim 			si->data_segs++;				\
3367e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3368e1235983SChangman Lee 		} else {						\
336939a53e0cSJaegeuk Kim 			si->node_segs++;				\
3370e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3371e1235983SChangman Lee 		}							\
337239a53e0cSJaegeuk Kim 	} while (0)
337339a53e0cSJaegeuk Kim 
337439a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
337568afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
337639a53e0cSJaegeuk Kim 
3377e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
337839a53e0cSJaegeuk Kim 	do {								\
3379963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
338039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
338139a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
338268afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
338339a53e0cSJaegeuk Kim 	} while (0)
338439a53e0cSJaegeuk Kim 
3385e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
338639a53e0cSJaegeuk Kim 	do {								\
3387963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
338839a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
338939a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
339068afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
339139a53e0cSJaegeuk Kim 	} while (0)
339239a53e0cSJaegeuk Kim 
3393cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3394cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
339521acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
33964589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
339739a53e0cSJaegeuk Kim #else
3398d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3399d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3400d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3401d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3402274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3403274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3404d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3405d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3406d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3407d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3408d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3409d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3410d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3411d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3412d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3413d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3414d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3415d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3416d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3417d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3418d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3419d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3420d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3421d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3422b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3423d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3424d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3425d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3426d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3427d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3428d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3429d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
343039a53e0cSJaegeuk Kim 
343139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
343239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
343321acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
34344589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
343539a53e0cSJaegeuk Kim #endif
343639a53e0cSJaegeuk Kim 
343739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
343839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
343939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
344039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
344139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
344239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
344339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
344439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3445cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
344639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
34474d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
34481001b347SHuajun Li 
3449e18c65b2SHuajun Li /*
3450e18c65b2SHuajun Li  * inline.c
3451e18c65b2SHuajun Li  */
3452cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3453cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
34544d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
34554d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
34564d57b86dSChao Yu 						struct page *ipage, u64 from);
3457cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3458cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3459cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3460cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
34614d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
34624d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3463cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
34644d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3465cac5a3d8SDongOh Shin 			struct page *ipage);
3466cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3467cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3468cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34694d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
34704d57b86dSChao Yu 				struct page *page, struct inode *dir,
34714d57b86dSChao Yu 				struct inode *inode);
3472cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3473cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3474cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3475cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3476cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3477cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3478cde4de12SJaegeuk Kim 
3479cde4de12SJaegeuk Kim /*
34802658e50dSJaegeuk Kim  * shrinker.c
34812658e50dSJaegeuk Kim  */
3482cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3483cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3484cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3485cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3486cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3487cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
34882658e50dSJaegeuk Kim 
34892658e50dSJaegeuk Kim /*
3490a28ef1f5SChao Yu  * extent_cache.c
3491a28ef1f5SChao Yu  */
34924dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3493004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
34944d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
34954dada3fdSChao Yu 				struct rb_root_cached *root,
34964dada3fdSChao Yu 				struct rb_node **parent,
34974dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
34984dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3499004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3500004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3501004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
35024dada3fdSChao Yu 		bool force, bool *leftmost);
35034d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
35044dada3fdSChao Yu 						struct rb_root_cached *root);
3505cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3506cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3507cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3508cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3509cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3510cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3511cac5a3d8SDongOh Shin 			struct extent_info *ei);
3512cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
351319b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3514cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
35154d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
35164d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
35174d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3518a28ef1f5SChao Yu 
3519a28ef1f5SChao Yu /*
35208ceffcb2SChao Yu  * sysfs.c
35218ceffcb2SChao Yu  */
3522dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3523dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3524dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3525dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
35268ceffcb2SChao Yu 
35278ceffcb2SChao Yu /*
3528cde4de12SJaegeuk Kim  * crypto support
3529cde4de12SJaegeuk Kim  */
35301958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
35311958593eSJaegeuk Kim {
353262230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
35331958593eSJaegeuk Kim }
35341958593eSJaegeuk Kim 
3535cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3536cde4de12SJaegeuk Kim {
3537643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
3538cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
35399149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
3540cde4de12SJaegeuk Kim #endif
3541cde4de12SJaegeuk Kim }
3542cde4de12SJaegeuk Kim 
35436dbb1796SEric Biggers /*
35446dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
35456dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
35466dbb1796SEric Biggers  */
35476dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3548cde4de12SJaegeuk Kim {
35496dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3550cde4de12SJaegeuk Kim }
3551cde4de12SJaegeuk Kim 
3552ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
35537beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
3554ccd31cb2SSheng Yong { \
35557beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
3556cde4de12SJaegeuk Kim }
3557f424f664SJaegeuk Kim 
3558ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3559ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3560ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3561ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3562ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3563ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3564ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3565ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3566b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3567d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
35681c1d35dfSChao Yu 
3569178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
357095175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
357195175dafSDamien Le Moal 				    block_t blkaddr)
3572178053e2SDamien Le Moal {
3573178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3574178053e2SDamien Le Moal 
357595175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
3576178053e2SDamien Le Moal }
3577178053e2SDamien Le Moal #endif
3578178053e2SDamien Le Moal 
35797d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
358096ba2decSDamien Le Moal {
35817beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
35827d20c8abSChao Yu }
358396ba2decSDamien Le Moal 
35847f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
35857f3d7719SDamien Le Moal {
35867f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
35877f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
35887f3d7719SDamien Le Moal }
35897f3d7719SDamien Le Moal 
35907d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
35917d20c8abSChao Yu {
35927f3d7719SDamien Le Moal 	int i;
35937f3d7719SDamien Le Moal 
35947f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
35957f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
35967f3d7719SDamien Le Moal 
35977f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
35987f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
35997f3d7719SDamien Le Moal 			return true;
36007f3d7719SDamien Le Moal 	return false;
36017d20c8abSChao Yu }
36027d20c8abSChao Yu 
36037d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
36047d20c8abSChao Yu {
36057d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
36067d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
360752763a4bSJaegeuk Kim }
360852763a4bSJaegeuk Kim 
3609f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
3610f824deb5SChao Yu {
3611f824deb5SChao Yu 	int i;
3612f824deb5SChao Yu 
3613f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
3614f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
3615f824deb5SChao Yu 
3616f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
3617f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
3618f824deb5SChao Yu 			return true;
3619f824deb5SChao Yu 	return false;
3620f824deb5SChao Yu }
3621f824deb5SChao Yu 
3622f824deb5SChao Yu 
362352763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
362452763a4bSJaegeuk Kim {
362552763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
362652763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
362752763a4bSJaegeuk Kim 
362852763a4bSJaegeuk Kim 	switch (mt) {
362952763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
363052763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
363152763a4bSJaegeuk Kim 		break;
363252763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
363352763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
363452763a4bSJaegeuk Kim 		break;
363552763a4bSJaegeuk Kim 	}
363652763a4bSJaegeuk Kim }
363752763a4bSJaegeuk Kim 
3638fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3639fcc85a4dSJaegeuk Kim {
3640643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
3641886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3642fcc85a4dSJaegeuk Kim 
3643fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3644fcc85a4dSJaegeuk Kim #else
36456e45f2a5SGustavo A. R. Silva 	return false;
3646fcc85a4dSJaegeuk Kim #endif
3647fcc85a4dSJaegeuk Kim }
3648fcc85a4dSJaegeuk Kim 
3649f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
3650f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3651b91050a8SHyunchul Lee {
3652f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
3653f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
3654f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
3655f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
3656f847c699SChao Yu 
3657f847c699SChao Yu 	return align & blocksize_mask;
3658f847c699SChao Yu }
3659f847c699SChao Yu 
3660f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
3661f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3662f847c699SChao Yu {
3663f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3664f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3665f847c699SChao Yu 
3666f847c699SChao Yu 	return (test_opt(sbi, LFS) && (rw == WRITE) &&
3667f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
3668f847c699SChao Yu }
3669f847c699SChao Yu 
3670f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
3671f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3672f847c699SChao Yu {
3673f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3674f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3675f847c699SChao Yu 
3676f847c699SChao Yu 	if (f2fs_post_read_required(inode))
3677f847c699SChao Yu 		return true;
36780916878dSDamien Le Moal 	if (f2fs_is_multi_device(sbi))
3679f847c699SChao Yu 		return true;
3680f847c699SChao Yu 	/*
3681f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
3682f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
3683f847c699SChao Yu 	 */
36847beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
3685f847c699SChao Yu 		return true;
3686f847c699SChao Yu 	if (test_opt(sbi, LFS) && (rw == WRITE) &&
3687f847c699SChao Yu 				block_unaligned_IO(inode, iocb, iter))
3688f847c699SChao Yu 		return true;
36894969c06aSJaegeuk Kim 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) &&
36904969c06aSJaegeuk Kim 					!(inode->i_flags & S_SWAPFILE))
36914354994fSDaniel Rosenberg 		return true;
36924354994fSDaniel Rosenberg 
3693f847c699SChao Yu 	return false;
3694b91050a8SHyunchul Lee }
3695b91050a8SHyunchul Lee 
369683a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
3697d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3698d494500aSChao Yu 							unsigned int type);
369983a3bfdbSJaegeuk Kim #else
3700d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
370183a3bfdbSJaegeuk Kim #endif
370283a3bfdbSJaegeuk Kim 
3703af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
3704af033b2aSChao Yu {
3705af033b2aSChao Yu #ifdef CONFIG_QUOTA
37067beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
3707af033b2aSChao Yu 		return true;
3708af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
3709af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
3710af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
3711af033b2aSChao Yu 		return true;
3712af033b2aSChao Yu #endif
3713af033b2aSChao Yu 	return false;
3714af033b2aSChao Yu }
37150af725fcSJaegeuk Kim 
371610f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
371710f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
371810f966bbSChao Yu 
3719e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
3720