xref: /openbmc/linux/fs/f2fs/f2fs.h (revision a28ef1f5aebe1068fc5fd65c4699c1c3b1e9094b)
10a8165d7SJaegeuk Kim /*
239a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
339a53e0cSJaegeuk Kim  *
439a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
539a53e0cSJaegeuk Kim  *             http://www.samsung.com/
639a53e0cSJaegeuk Kim  *
739a53e0cSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
839a53e0cSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
939a53e0cSJaegeuk Kim  * published by the Free Software Foundation.
1039a53e0cSJaegeuk Kim  */
1139a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
1239a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1339a53e0cSJaegeuk Kim 
1439a53e0cSJaegeuk Kim #include <linux/types.h>
1539a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1639a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1739a53e0cSJaegeuk Kim #include <linux/slab.h>
1839a53e0cSJaegeuk Kim #include <linux/crc32.h>
1939a53e0cSJaegeuk Kim #include <linux/magic.h>
20c2d715d1SJaegeuk Kim #include <linux/kobject.h>
217bd59381SGu Zheng #include <linux/sched.h>
2239a53e0cSJaegeuk Kim 
235d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
249850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
250daaad97SJaegeuk Kim #define f2fs_down_write(x, y)	down_write_nest_lock(x, y)
265d56b671SJaegeuk Kim #else
279850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
289850cf4aSJaegeuk Kim 	do {								\
299850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
309850cf4aSJaegeuk Kim 			WARN_ON(1);					\
31caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
329850cf4aSJaegeuk Kim 		}							\
339850cf4aSJaegeuk Kim 	} while (0)
340daaad97SJaegeuk Kim #define f2fs_down_write(x, y)	down_write(x)
355d56b671SJaegeuk Kim #endif
365d56b671SJaegeuk Kim 
3739a53e0cSJaegeuk Kim /*
3839a53e0cSJaegeuk Kim  * For mount options
3939a53e0cSJaegeuk Kim  */
4039a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
4139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
4239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
4339a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
4439a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
4539a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
4639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
47444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
481001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
4934d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
5034d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
5134d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
52d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
5389672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
5439a53e0cSJaegeuk Kim 
5539a53e0cSJaegeuk Kim #define clear_opt(sbi, option)	(sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
5639a53e0cSJaegeuk Kim #define set_opt(sbi, option)	(sbi->mount_opt.opt |= F2FS_MOUNT_##option)
5739a53e0cSJaegeuk Kim #define test_opt(sbi, option)	(sbi->mount_opt.opt & F2FS_MOUNT_##option)
5839a53e0cSJaegeuk Kim 
5939a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
6039a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
6139a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
6239a53e0cSJaegeuk Kim 
63a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
64a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
65a9841c4dSJaegeuk Kim 			 * address format, __le32.
66a9841c4dSJaegeuk Kim 			 */
6739a53e0cSJaegeuk Kim typedef u32 nid_t;
6839a53e0cSJaegeuk Kim 
6939a53e0cSJaegeuk Kim struct f2fs_mount_info {
7039a53e0cSJaegeuk Kim 	unsigned int	opt;
7139a53e0cSJaegeuk Kim };
7239a53e0cSJaegeuk Kim 
73cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT	0x0001
74cde4de12SJaegeuk Kim 
7576f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
7676f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
7776f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
7876f105a2SJaegeuk Kim 	F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
7976f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
8076f105a2SJaegeuk Kim 	F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
8176f105a2SJaegeuk Kim 
827e586fa0SJaegeuk Kim #define CRCPOLY_LE 0xedb88320
837e586fa0SJaegeuk Kim 
847e586fa0SJaegeuk Kim static inline __u32 f2fs_crc32(void *buf, size_t len)
8539a53e0cSJaegeuk Kim {
867e586fa0SJaegeuk Kim 	unsigned char *p = (unsigned char *)buf;
877e586fa0SJaegeuk Kim 	__u32 crc = F2FS_SUPER_MAGIC;
887e586fa0SJaegeuk Kim 	int i;
897e586fa0SJaegeuk Kim 
907e586fa0SJaegeuk Kim 	while (len--) {
917e586fa0SJaegeuk Kim 		crc ^= *p++;
927e586fa0SJaegeuk Kim 		for (i = 0; i < 8; i++)
937e586fa0SJaegeuk Kim 			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
947e586fa0SJaegeuk Kim 	}
957e586fa0SJaegeuk Kim 	return crc;
9639a53e0cSJaegeuk Kim }
9739a53e0cSJaegeuk Kim 
987e586fa0SJaegeuk Kim static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
9939a53e0cSJaegeuk Kim {
1007e586fa0SJaegeuk Kim 	return f2fs_crc32(buf, buf_size) == blk_crc;
10139a53e0cSJaegeuk Kim }
10239a53e0cSJaegeuk Kim 
10339a53e0cSJaegeuk Kim /*
10439a53e0cSJaegeuk Kim  * For checkpoint manager
10539a53e0cSJaegeuk Kim  */
10639a53e0cSJaegeuk Kim enum {
10739a53e0cSJaegeuk Kim 	NAT_BITMAP,
10839a53e0cSJaegeuk Kim 	SIT_BITMAP
10939a53e0cSJaegeuk Kim };
11039a53e0cSJaegeuk Kim 
11175ab4cb8SJaegeuk Kim enum {
11275ab4cb8SJaegeuk Kim 	CP_UMOUNT,
113119ee914SJaegeuk Kim 	CP_FASTBOOT,
11475ab4cb8SJaegeuk Kim 	CP_SYNC,
11510027551SJaegeuk Kim 	CP_RECOVERY,
1164b2fecc8SJaegeuk Kim 	CP_DISCARD,
11775ab4cb8SJaegeuk Kim };
11875ab4cb8SJaegeuk Kim 
119bba681cbSJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	32
120bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
121bba681cbSJaegeuk Kim 		(SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
122a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
123a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
124bba681cbSJaegeuk Kim 
12575ab4cb8SJaegeuk Kim struct cp_control {
12675ab4cb8SJaegeuk Kim 	int reason;
1274b2fecc8SJaegeuk Kim 	__u64 trim_start;
1284b2fecc8SJaegeuk Kim 	__u64 trim_end;
1294b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
1304b2fecc8SJaegeuk Kim 	__u64 trimmed;
13175ab4cb8SJaegeuk Kim };
13275ab4cb8SJaegeuk Kim 
133662befdaSChao Yu /*
13481c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
135662befdaSChao Yu  */
136662befdaSChao Yu enum {
137662befdaSChao Yu 	META_CP,
138662befdaSChao Yu 	META_NAT,
13981c1a0f1SChao Yu 	META_SIT,
1404c521f49SJaegeuk Kim 	META_SSA,
1414c521f49SJaegeuk Kim 	META_POR,
142662befdaSChao Yu };
143662befdaSChao Yu 
1446451e041SJaegeuk Kim /* for the list of ino */
1456451e041SJaegeuk Kim enum {
1466451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
147fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
148fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1496451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1506451e041SJaegeuk Kim };
1516451e041SJaegeuk Kim 
1526451e041SJaegeuk Kim struct ino_entry {
15339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
15439a53e0cSJaegeuk Kim 	nid_t ino;		/* inode number */
15539a53e0cSJaegeuk Kim };
15639a53e0cSJaegeuk Kim 
15706292073SChao Yu /*
15806292073SChao Yu  * for the list of directory inodes or gc inodes.
15906292073SChao Yu  * NOTE: there are two slab users for this structure, if we add/modify/delete
16006292073SChao Yu  * fields in structure for one of slab users, it may affect fields or size of
16106292073SChao Yu  * other one, in this condition, it's better to split both of slab and related
16206292073SChao Yu  * data structure.
16306292073SChao Yu  */
16406292073SChao Yu struct inode_entry {
16539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
16639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
16739a53e0cSJaegeuk Kim };
16839a53e0cSJaegeuk Kim 
1697fd9e544SJaegeuk Kim /* for the list of blockaddresses to be discarded */
1707fd9e544SJaegeuk Kim struct discard_entry {
1717fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
1727fd9e544SJaegeuk Kim 	block_t blkaddr;	/* block address to be discarded */
1737fd9e544SJaegeuk Kim 	int len;		/* # of consecutive blocks of the discard */
1747fd9e544SJaegeuk Kim };
1757fd9e544SJaegeuk Kim 
17639a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
17739a53e0cSJaegeuk Kim struct fsync_inode_entry {
17839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
17939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
180c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
181c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
182c52e1b10SJaegeuk Kim 	block_t last_inode;	/* block address locating the last inode */
18339a53e0cSJaegeuk Kim };
18439a53e0cSJaegeuk Kim 
18539a53e0cSJaegeuk Kim #define nats_in_cursum(sum)		(le16_to_cpu(sum->n_nats))
18639a53e0cSJaegeuk Kim #define sits_in_cursum(sum)		(le16_to_cpu(sum->n_sits))
18739a53e0cSJaegeuk Kim 
18839a53e0cSJaegeuk Kim #define nat_in_journal(sum, i)		(sum->nat_j.entries[i].ne)
18939a53e0cSJaegeuk Kim #define nid_in_journal(sum, i)		(sum->nat_j.entries[i].nid)
19039a53e0cSJaegeuk Kim #define sit_in_journal(sum, i)		(sum->sit_j.entries[i].se)
19139a53e0cSJaegeuk Kim #define segno_in_journal(sum, i)	(sum->sit_j.entries[i].segno)
19239a53e0cSJaegeuk Kim 
193309cc2b6SJaegeuk Kim #define MAX_NAT_JENTRIES(sum)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
194309cc2b6SJaegeuk Kim #define MAX_SIT_JENTRIES(sum)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
195309cc2b6SJaegeuk Kim 
19639a53e0cSJaegeuk Kim static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
19739a53e0cSJaegeuk Kim {
19839a53e0cSJaegeuk Kim 	int before = nats_in_cursum(rs);
19939a53e0cSJaegeuk Kim 	rs->n_nats = cpu_to_le16(before + i);
20039a53e0cSJaegeuk Kim 	return before;
20139a53e0cSJaegeuk Kim }
20239a53e0cSJaegeuk Kim 
20339a53e0cSJaegeuk Kim static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
20439a53e0cSJaegeuk Kim {
20539a53e0cSJaegeuk Kim 	int before = sits_in_cursum(rs);
20639a53e0cSJaegeuk Kim 	rs->n_sits = cpu_to_le16(before + i);
20739a53e0cSJaegeuk Kim 	return before;
20839a53e0cSJaegeuk Kim }
20939a53e0cSJaegeuk Kim 
210184a5cd2SChao Yu static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
211184a5cd2SChao Yu 								int type)
212184a5cd2SChao Yu {
213184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
214309cc2b6SJaegeuk Kim 		return size <= MAX_NAT_JENTRIES(sum);
215309cc2b6SJaegeuk Kim 	return size <= MAX_SIT_JENTRIES(sum);
216184a5cd2SChao Yu }
217184a5cd2SChao Yu 
21839a53e0cSJaegeuk Kim /*
219e9750824SNamjae Jeon  * ioctl commands
220e9750824SNamjae Jeon  */
221e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
222e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
223d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
224e9750824SNamjae Jeon 
22588b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
22688b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
22788b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
22802a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
2291e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
2301e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
23188b88a66SJaegeuk Kim 
232f424f664SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY					\
233f424f664SJaegeuk Kim 		_IOR('f', 19, struct f2fs_encryption_policy)
234f424f664SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT					\
235f424f664SJaegeuk Kim 		_IOW('f', 20, __u8[16])
236f424f664SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY					\
237f424f664SJaegeuk Kim 		_IOW('f', 21, struct f2fs_encryption_policy)
238f424f664SJaegeuk Kim 
2391abff93dSJaegeuk Kim /*
2401abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
2411abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
2421abff93dSJaegeuk Kim  */
2431abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
2441abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
2451abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
2461abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
2471abff93dSJaegeuk Kim 
248e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
249e9750824SNamjae Jeon /*
250e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
251e9750824SNamjae Jeon  */
252e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS             FS_IOC32_GETFLAGS
253e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS             FS_IOC32_SETFLAGS
254e9750824SNamjae Jeon #endif
255e9750824SNamjae Jeon 
256e9750824SNamjae Jeon /*
25739a53e0cSJaegeuk Kim  * For INODE and NODE manager
25839a53e0cSJaegeuk Kim  */
2597b3cd7d6SJaegeuk Kim /* for directory operations */
2606b3bd08fSJaegeuk Kim struct f2fs_str {
2616b3bd08fSJaegeuk Kim 	unsigned char *name;
2626b3bd08fSJaegeuk Kim 	u32 len;
2636b3bd08fSJaegeuk Kim };
2646b3bd08fSJaegeuk Kim 
2656b3bd08fSJaegeuk Kim struct f2fs_filename {
2666b3bd08fSJaegeuk Kim 	const struct qstr *usr_fname;
2676b3bd08fSJaegeuk Kim 	struct f2fs_str disk_name;
2686b3bd08fSJaegeuk Kim 	f2fs_hash_t hash;
2696b3bd08fSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2706b3bd08fSJaegeuk Kim 	struct f2fs_str crypto_buf;
2716b3bd08fSJaegeuk Kim #endif
2726b3bd08fSJaegeuk Kim };
2736b3bd08fSJaegeuk Kim 
2746b3bd08fSJaegeuk Kim #define FSTR_INIT(n, l)		{ .name = n, .len = l }
2756b3bd08fSJaegeuk Kim #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
2766b3bd08fSJaegeuk Kim #define fname_name(p)		((p)->disk_name.name)
2776b3bd08fSJaegeuk Kim #define fname_len(p)		((p)->disk_name.len)
2786b3bd08fSJaegeuk Kim 
2797b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
280d8c6822aSJaegeuk Kim 	struct inode *inode;
2817b3cd7d6SJaegeuk Kim 	const void *bitmap;
2827b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
2837b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
2847b3cd7d6SJaegeuk Kim 	int max;
2857b3cd7d6SJaegeuk Kim };
2867b3cd7d6SJaegeuk Kim 
287d8c6822aSJaegeuk Kim static inline void make_dentry_ptr(struct inode *inode,
288d8c6822aSJaegeuk Kim 		struct f2fs_dentry_ptr *d, void *src, int type)
2897b3cd7d6SJaegeuk Kim {
290d8c6822aSJaegeuk Kim 	d->inode = inode;
291d8c6822aSJaegeuk Kim 
2927b3cd7d6SJaegeuk Kim 	if (type == 1) {
2937b3cd7d6SJaegeuk Kim 		struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
2947b3cd7d6SJaegeuk Kim 		d->max = NR_DENTRY_IN_BLOCK;
2957b3cd7d6SJaegeuk Kim 		d->bitmap = &t->dentry_bitmap;
2967b3cd7d6SJaegeuk Kim 		d->dentry = t->dentry;
2977b3cd7d6SJaegeuk Kim 		d->filename = t->filename;
2987b3cd7d6SJaegeuk Kim 	} else {
2997b3cd7d6SJaegeuk Kim 		struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
3007b3cd7d6SJaegeuk Kim 		d->max = NR_INLINE_DENTRY;
3017b3cd7d6SJaegeuk Kim 		d->bitmap = &t->dentry_bitmap;
3027b3cd7d6SJaegeuk Kim 		d->dentry = t->dentry;
3037b3cd7d6SJaegeuk Kim 		d->filename = t->filename;
3047b3cd7d6SJaegeuk Kim 	}
3057b3cd7d6SJaegeuk Kim }
3067b3cd7d6SJaegeuk Kim 
307dbe6a5ffSJaegeuk Kim /*
308dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
309dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
310dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
31139a53e0cSJaegeuk Kim  */
312dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
313dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
314266e97a8SJaegeuk Kim enum {
315266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
316266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
317266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
318266e97a8SJaegeuk Kim 					 * look up a node with readahead called
3194f4124d0SChao Yu 					 * by get_data_block.
32039a53e0cSJaegeuk Kim 					 */
321266e97a8SJaegeuk Kim };
322266e97a8SJaegeuk Kim 
32339a53e0cSJaegeuk Kim #define F2FS_LINK_MAX		32000	/* maximum link count per file */
32439a53e0cSJaegeuk Kim 
325817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
326817202d9SChao Yu 
32713054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
32813054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
32913054c54SChao Yu 
33039a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
33113054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
33213054c54SChao Yu 
33313054c54SChao Yu /* number of extent info in extent cache we try to shrink */
33413054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
335c11abd1aSJaegeuk Kim 
33639a53e0cSJaegeuk Kim struct extent_info {
33739a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
3384d0b0bd4SChao Yu 	u32 blk;			/* start block address of the extent */
339111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
34039a53e0cSJaegeuk Kim };
34139a53e0cSJaegeuk Kim 
34213054c54SChao Yu struct extent_node {
34313054c54SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
34413054c54SChao Yu 	struct list_head list;		/* node in global extent list of sbi */
34513054c54SChao Yu 	struct extent_info ei;		/* extent info */
34613054c54SChao Yu };
34713054c54SChao Yu 
34813054c54SChao Yu struct extent_tree {
34913054c54SChao Yu 	nid_t ino;			/* inode number */
35013054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
35162c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
3523e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
35313054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
35413054c54SChao Yu 	atomic_t refcount;		/* reference count of rb-tree */
35513054c54SChao Yu 	unsigned int count;		/* # of extent node in rb-tree*/
35613054c54SChao Yu };
35713054c54SChao Yu 
35839a53e0cSJaegeuk Kim /*
359003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
360003a3e1dSJaegeuk Kim  *
361003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
362003a3e1dSJaegeuk Kim  */
363003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
364003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
3657f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
3667f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
3677f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
368003a3e1dSJaegeuk Kim 
369003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
370003a3e1dSJaegeuk Kim 	block_t m_pblk;
371003a3e1dSJaegeuk Kim 	block_t m_lblk;
372003a3e1dSJaegeuk Kim 	unsigned int m_len;
373003a3e1dSJaegeuk Kim 	unsigned int m_flags;
374003a3e1dSJaegeuk Kim };
375003a3e1dSJaegeuk Kim 
376003a3e1dSJaegeuk Kim /*
37739a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
37839a53e0cSJaegeuk Kim  */
37939a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
380354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
381cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
382e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
38339a53e0cSJaegeuk Kim 
384b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
385b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
386b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
387b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
388b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
389b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
390cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
391cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
392cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
393e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
394e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
395cde4de12SJaegeuk Kim 
396cde4de12SJaegeuk Kim /* Encryption algorithms */
397cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_INVALID		0
398cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_XTS	1
399cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_GCM	2
400cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CBC	3
401cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CTS	4
402b5492af7SJaegeuk Kim 
403f424f664SJaegeuk Kim #include "f2fs_crypto.h"
404f424f664SJaegeuk Kim 
405ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
406ab9fa662SJaegeuk Kim 
40739a53e0cSJaegeuk Kim struct f2fs_inode_info {
40839a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
40939a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
41039a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
41138431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
41239a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
4136666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
41439a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
41539a53e0cSJaegeuk Kim 
41639a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
41739a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
418d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
419a7ffdbe2SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
42039a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
42139a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
42239a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
423e518ff81SJaegeuk Kim 	unsigned long long xattr_ver;	/* cp version of xattr modification */
42406292073SChao Yu 	struct inode_entry *dirty_dir;	/* the pointer of dirty dir */
42588b88a66SJaegeuk Kim 
42634ba94baSJaegeuk Kim 	struct radix_tree_root inmem_root;	/* radix tree for inmem pages */
42788b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
42888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
429cde4de12SJaegeuk Kim 
4303e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
4313e72f721SJaegeuk Kim 
432cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
433cde4de12SJaegeuk Kim 	/* Encryption params */
434cde4de12SJaegeuk Kim 	struct f2fs_crypt_info *i_crypt_info;
435cde4de12SJaegeuk Kim #endif
43639a53e0cSJaegeuk Kim };
43739a53e0cSJaegeuk Kim 
43839a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
43939a53e0cSJaegeuk Kim 					struct f2fs_extent i_ext)
44039a53e0cSJaegeuk Kim {
44139a53e0cSJaegeuk Kim 	ext->fofs = le32_to_cpu(i_ext.fofs);
4424d0b0bd4SChao Yu 	ext->blk = le32_to_cpu(i_ext.blk);
44339a53e0cSJaegeuk Kim 	ext->len = le32_to_cpu(i_ext.len);
44439a53e0cSJaegeuk Kim }
44539a53e0cSJaegeuk Kim 
44639a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
44739a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
44839a53e0cSJaegeuk Kim {
44939a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
4504d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
45139a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
45239a53e0cSJaegeuk Kim }
45339a53e0cSJaegeuk Kim 
454429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
455429511cdSChao Yu 						u32 blk, unsigned int len)
456429511cdSChao Yu {
457429511cdSChao Yu 	ei->fofs = fofs;
458429511cdSChao Yu 	ei->blk = blk;
459429511cdSChao Yu 	ei->len = len;
460429511cdSChao Yu }
461429511cdSChao Yu 
4620bdee482SChao Yu static inline bool __is_extent_same(struct extent_info *ei1,
4630bdee482SChao Yu 						struct extent_info *ei2)
4640bdee482SChao Yu {
4650bdee482SChao Yu 	return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
4660bdee482SChao Yu 						ei1->len == ei2->len);
4670bdee482SChao Yu }
4680bdee482SChao Yu 
469429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
470429511cdSChao Yu 						struct extent_info *front)
471429511cdSChao Yu {
472429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
473429511cdSChao Yu 			back->blk + back->len == front->blk);
474429511cdSChao Yu }
475429511cdSChao Yu 
476429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
477429511cdSChao Yu 						struct extent_info *back)
478429511cdSChao Yu {
479429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
480429511cdSChao Yu }
481429511cdSChao Yu 
482429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
483429511cdSChao Yu 						struct extent_info *front)
484429511cdSChao Yu {
485429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
486429511cdSChao Yu }
487429511cdSChao Yu 
48839a53e0cSJaegeuk Kim struct f2fs_nm_info {
48939a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
49039a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
4917ee0eeabSJaegeuk Kim 	nid_t available_nids;		/* maximum available node ids */
49239a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
493cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
49439a53e0cSJaegeuk Kim 
49539a53e0cSJaegeuk Kim 	/* NAT cache management */
49639a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
497309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
4988b26ef98SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
49939a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
500309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
501aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
50239a53e0cSJaegeuk Kim 
50339a53e0cSJaegeuk Kim 	/* free node ids management */
5048a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
50539a53e0cSJaegeuk Kim 	struct list_head free_nid_list;	/* a list for free nids */
50639a53e0cSJaegeuk Kim 	spinlock_t free_nid_list_lock;	/* protect free nid list */
50739a53e0cSJaegeuk Kim 	unsigned int fcnt;		/* the number of free node id */
50839a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
50939a53e0cSJaegeuk Kim 
51039a53e0cSJaegeuk Kim 	/* for checkpoint */
51139a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
51239a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
51339a53e0cSJaegeuk Kim };
51439a53e0cSJaegeuk Kim 
51539a53e0cSJaegeuk Kim /*
51639a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
51739a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
51839a53e0cSJaegeuk Kim  * by the data offset in a file.
51939a53e0cSJaegeuk Kim  */
52039a53e0cSJaegeuk Kim struct dnode_of_data {
52139a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
52239a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
52339a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
52439a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
52539a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
52639a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
52739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
52839a53e0cSJaegeuk Kim };
52939a53e0cSJaegeuk Kim 
53039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
53139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
53239a53e0cSJaegeuk Kim {
533d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
53439a53e0cSJaegeuk Kim 	dn->inode = inode;
53539a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
53639a53e0cSJaegeuk Kim 	dn->node_page = npage;
53739a53e0cSJaegeuk Kim 	dn->nid = nid;
53839a53e0cSJaegeuk Kim }
53939a53e0cSJaegeuk Kim 
54039a53e0cSJaegeuk Kim /*
54139a53e0cSJaegeuk Kim  * For SIT manager
54239a53e0cSJaegeuk Kim  *
54339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
54439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
54539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
54639a53e0cSJaegeuk Kim  * respectively.
54739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
54839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
54939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
55039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
55139a53e0cSJaegeuk Kim  * data and 8 for node logs.
55239a53e0cSJaegeuk Kim  */
55339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
55439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
55539a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
55639a53e0cSJaegeuk Kim 
55739a53e0cSJaegeuk Kim enum {
55839a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
55939a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
56039a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
56139a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
56239a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
56339a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
56438aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
56538aa0889SJaegeuk Kim 	CURSEG_DIRECT_IO,	/* to use for the direct IO path */
56639a53e0cSJaegeuk Kim };
56739a53e0cSJaegeuk Kim 
5686b4afdd7SJaegeuk Kim struct flush_cmd {
5696b4afdd7SJaegeuk Kim 	struct completion wait;
570721bd4d5SGu Zheng 	struct llist_node llnode;
5716b4afdd7SJaegeuk Kim 	int ret;
5726b4afdd7SJaegeuk Kim };
5736b4afdd7SJaegeuk Kim 
574a688b9d9SGu Zheng struct flush_cmd_control {
575a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
576a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
577721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
578721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
579a688b9d9SGu Zheng };
580a688b9d9SGu Zheng 
58139a53e0cSJaegeuk Kim struct f2fs_sm_info {
58239a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
58339a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
58439a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
58539a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
58639a53e0cSJaegeuk Kim 
58739a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
58839a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
58939a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
59039a53e0cSJaegeuk Kim 
59139a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
59239a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
59339a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
59439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
59581eb8d6eSJaegeuk Kim 
59681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
59781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
5987fd9e544SJaegeuk Kim 
5997fd9e544SJaegeuk Kim 	/* for small discard management */
6007fd9e544SJaegeuk Kim 	struct list_head discard_list;		/* 4KB discard list */
6017fd9e544SJaegeuk Kim 	int nr_discards;			/* # of discards in the list */
6027fd9e544SJaegeuk Kim 	int max_discards;			/* max. discards to be issued */
603216fbd64SJaegeuk Kim 
604bba681cbSJaegeuk Kim 	/* for batched trimming */
605bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
606bba681cbSJaegeuk Kim 
607184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
608184a5cd2SChao Yu 
609216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
610216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
611c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
6126b4afdd7SJaegeuk Kim 
6136b4afdd7SJaegeuk Kim 	/* for flush command control */
614a688b9d9SGu Zheng 	struct flush_cmd_control *cmd_control_info;
615a688b9d9SGu Zheng 
61639a53e0cSJaegeuk Kim };
61739a53e0cSJaegeuk Kim 
61839a53e0cSJaegeuk Kim /*
61939a53e0cSJaegeuk Kim  * For superblock
62039a53e0cSJaegeuk Kim  */
62139a53e0cSJaegeuk Kim /*
62239a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
62339a53e0cSJaegeuk Kim  *
62439a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
62539a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
62639a53e0cSJaegeuk Kim  */
62739a53e0cSJaegeuk Kim enum count_type {
62839a53e0cSJaegeuk Kim 	F2FS_WRITEBACK,
62939a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
63039a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
63139a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
6328dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
63339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
63439a53e0cSJaegeuk Kim };
63539a53e0cSJaegeuk Kim 
63639a53e0cSJaegeuk Kim /*
637e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
63839a53e0cSJaegeuk Kim  * The available types are:
63939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
64039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
64139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
64239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
64339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
64439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
64539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
64639a53e0cSJaegeuk Kim  */
6477d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
64839a53e0cSJaegeuk Kim enum page_type {
64939a53e0cSJaegeuk Kim 	DATA,
65039a53e0cSJaegeuk Kim 	NODE,
65139a53e0cSJaegeuk Kim 	META,
65239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
65339a53e0cSJaegeuk Kim 	META_FLUSH,
6548ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
6558ce67cb0SJaegeuk Kim 	INMEM_DROP,
6568ce67cb0SJaegeuk Kim 	IPU,
6578ce67cb0SJaegeuk Kim 	OPU,
65839a53e0cSJaegeuk Kim };
65939a53e0cSJaegeuk Kim 
660458e6197SJaegeuk Kim struct f2fs_io_info {
66105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
662458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
6637e8f2308SGu Zheng 	int rw;			/* contains R/RS/W/WS with REQ_META/REQ_PRIO */
664cf04e8ebSJaegeuk Kim 	block_t blk_addr;	/* block address to be written */
66505ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
6664375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
667458e6197SJaegeuk Kim };
668458e6197SJaegeuk Kim 
66993dfe2acSJaegeuk Kim #define is_read_io(rw)	(((rw) & 1) == READ)
6701ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
671458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
6721ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
6731ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
674458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
675df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
6761ff7bd3bSJaegeuk Kim };
6771ff7bd3bSJaegeuk Kim 
67867298804SChao Yu /* for inner inode cache management */
67967298804SChao Yu struct inode_management {
68067298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
68167298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
68267298804SChao Yu 	struct list_head ino_list;		/* inode list head */
68367298804SChao Yu 	unsigned long ino_num;			/* number of entries */
68467298804SChao Yu };
68567298804SChao Yu 
686caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
687caf0047eSChao Yu enum {
688caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
689caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
690caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
691caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
692caf0047eSChao Yu };
693caf0047eSChao Yu 
69439a53e0cSJaegeuk Kim struct f2fs_sb_info {
69539a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
6965e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
69739a53e0cSJaegeuk Kim 	struct buffer_head *raw_super_buf;	/* buffer head of raw sb */
69839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
699caf0047eSChao Yu 	int s_flag;				/* flags for sbi */
70039a53e0cSJaegeuk Kim 
70139a53e0cSJaegeuk Kim 	/* for node-related operations */
70239a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
70339a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
70439a53e0cSJaegeuk Kim 
70539a53e0cSJaegeuk Kim 	/* for segment-related operations */
70639a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
7071ff7bd3bSJaegeuk Kim 
7081ff7bd3bSJaegeuk Kim 	/* for bio operations */
709924b720bSChao Yu 	struct f2fs_bio_info read_io;			/* for read bios */
7101ff7bd3bSJaegeuk Kim 	struct f2fs_bio_info write_io[NR_PAGE_TYPE];	/* for write bios */
71139a53e0cSJaegeuk Kim 
71239a53e0cSJaegeuk Kim 	/* for checkpoint */
71339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
71439a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
71539936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
716e479556bSGu Zheng 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
717b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
7185463e7c1SJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
719fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
72039a53e0cSJaegeuk Kim 
72167298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
7226451e041SJaegeuk Kim 
7236451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
7240d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
72539a53e0cSJaegeuk Kim 
72639a53e0cSJaegeuk Kim 	/* for directory inode management */
72739a53e0cSJaegeuk Kim 	struct list_head dir_inode_list;	/* dir inode list */
72839a53e0cSJaegeuk Kim 	spinlock_t dir_inode_lock;		/* for dir inode list lock */
72939a53e0cSJaegeuk Kim 
73013054c54SChao Yu 	/* for extent tree cache */
73113054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
73213054c54SChao Yu 	struct rw_semaphore extent_tree_lock;	/* locking extent radix tree */
73313054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
73413054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
73513054c54SChao Yu 	int total_ext_tree;			/* extent tree count */
73613054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
73713054c54SChao Yu 
73839a53e0cSJaegeuk Kim 	/* basic filesystem units */
73939a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
74039a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
74139a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
74239a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
74339a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
74439a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
74539a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
74639a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
74739a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
74839a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
74939a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
75039a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
75139a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
75239a53e0cSJaegeuk Kim 	unsigned int total_valid_inode_count;	/* valid inode count */
75339a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
754ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
75539a53e0cSJaegeuk Kim 
75639a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
75739a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
75839a53e0cSJaegeuk Kim 	block_t alloc_valid_block_count;	/* # of allocated blocks */
759a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
76039a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
76139a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
76239a53e0cSJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];	/* # of pages, see count_type */
76339a53e0cSJaegeuk Kim 
76439a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
76539a53e0cSJaegeuk Kim 
76639a53e0cSJaegeuk Kim 	/* for cleaning operations */
76739a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
76839a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
7695ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
77039a53e0cSJaegeuk Kim 
771b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
772b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
773b1c57c1cSJaegeuk Kim 
77439a53e0cSJaegeuk Kim 	/*
77539a53e0cSJaegeuk Kim 	 * for stat information.
77639a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
77739a53e0cSJaegeuk Kim 	 */
77835b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
77939a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
78039a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
78139a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
782b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
78339a53e0cSJaegeuk Kim 	int total_hit_ext, read_hit_ext;	/* extent cache hit ratio */
78403e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
78503e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
78639a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
78735b09d82SNamjae Jeon 	unsigned int n_dirty_dirs;		/* # of dir inodes */
78835b09d82SNamjae Jeon #endif
78935b09d82SNamjae Jeon 	unsigned int last_victim[2];		/* last victim segment # */
79039a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
791b59d0baeSNamjae Jeon 
792b59d0baeSNamjae Jeon 	/* For sysfs suppport */
793b59d0baeSNamjae Jeon 	struct kobject s_kobj;
794b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
7952658e50dSJaegeuk Kim 
7962658e50dSJaegeuk Kim 	/* For shrinker support */
7972658e50dSJaegeuk Kim 	struct list_head s_list;
7982658e50dSJaegeuk Kim 	struct mutex umount_mutex;
7992658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
80039a53e0cSJaegeuk Kim };
80139a53e0cSJaegeuk Kim 
80239a53e0cSJaegeuk Kim /*
80339a53e0cSJaegeuk Kim  * Inline functions
80439a53e0cSJaegeuk Kim  */
80539a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
80639a53e0cSJaegeuk Kim {
80739a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
80839a53e0cSJaegeuk Kim }
80939a53e0cSJaegeuk Kim 
81039a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
81139a53e0cSJaegeuk Kim {
81239a53e0cSJaegeuk Kim 	return sb->s_fs_info;
81339a53e0cSJaegeuk Kim }
81439a53e0cSJaegeuk Kim 
8154081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
8164081363fSJaegeuk Kim {
8174081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
8184081363fSJaegeuk Kim }
8194081363fSJaegeuk Kim 
8204081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
8214081363fSJaegeuk Kim {
8224081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
8234081363fSJaegeuk Kim }
8244081363fSJaegeuk Kim 
8254081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
8264081363fSJaegeuk Kim {
8274081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
8284081363fSJaegeuk Kim }
8294081363fSJaegeuk Kim 
83039a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
83139a53e0cSJaegeuk Kim {
83239a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
83339a53e0cSJaegeuk Kim }
83439a53e0cSJaegeuk Kim 
83539a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
83639a53e0cSJaegeuk Kim {
83739a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
83839a53e0cSJaegeuk Kim }
83939a53e0cSJaegeuk Kim 
84045590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
84145590710SGu Zheng {
84245590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
84345590710SGu Zheng }
84445590710SGu Zheng 
84558bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
84658bfaf44SJaegeuk Kim {
84758bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
84858bfaf44SJaegeuk Kim }
84958bfaf44SJaegeuk Kim 
85039a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
85139a53e0cSJaegeuk Kim {
85239a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
85339a53e0cSJaegeuk Kim }
85439a53e0cSJaegeuk Kim 
85539a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
85639a53e0cSJaegeuk Kim {
85739a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
85839a53e0cSJaegeuk Kim }
85939a53e0cSJaegeuk Kim 
86039a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
86139a53e0cSJaegeuk Kim {
86239a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
86339a53e0cSJaegeuk Kim }
86439a53e0cSJaegeuk Kim 
86539a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
86639a53e0cSJaegeuk Kim {
86739a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
86839a53e0cSJaegeuk Kim }
86939a53e0cSJaegeuk Kim 
87039a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
87139a53e0cSJaegeuk Kim {
87239a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
87339a53e0cSJaegeuk Kim }
87439a53e0cSJaegeuk Kim 
8759df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
8769df27d98SGu Zheng {
8779df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
8789df27d98SGu Zheng }
8799df27d98SGu Zheng 
8804ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
8814ef51a8fSJaegeuk Kim {
8824ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
8834ef51a8fSJaegeuk Kim }
8844ef51a8fSJaegeuk Kim 
885caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
88639a53e0cSJaegeuk Kim {
887caf0047eSChao Yu 	return sbi->s_flag & (0x01 << type);
88839a53e0cSJaegeuk Kim }
88939a53e0cSJaegeuk Kim 
890caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
89139a53e0cSJaegeuk Kim {
892caf0047eSChao Yu 	sbi->s_flag |= (0x01 << type);
893caf0047eSChao Yu }
894caf0047eSChao Yu 
895caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
896caf0047eSChao Yu {
897caf0047eSChao Yu 	sbi->s_flag &= ~(0x01 << type);
89839a53e0cSJaegeuk Kim }
89939a53e0cSJaegeuk Kim 
900d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
901d71b5564SJaegeuk Kim {
902d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
903d71b5564SJaegeuk Kim }
904d71b5564SJaegeuk Kim 
90525ca923bSJaegeuk Kim static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
90625ca923bSJaegeuk Kim {
90725ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
90825ca923bSJaegeuk Kim 	return ckpt_flags & f;
90925ca923bSJaegeuk Kim }
91025ca923bSJaegeuk Kim 
91125ca923bSJaegeuk Kim static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
91225ca923bSJaegeuk Kim {
91325ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
91425ca923bSJaegeuk Kim 	ckpt_flags |= f;
91525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
91625ca923bSJaegeuk Kim }
91725ca923bSJaegeuk Kim 
91825ca923bSJaegeuk Kim static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
91925ca923bSJaegeuk Kim {
92025ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
92125ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
92225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
92325ca923bSJaegeuk Kim }
92425ca923bSJaegeuk Kim 
925e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
92639a53e0cSJaegeuk Kim {
927e479556bSGu Zheng 	down_read(&sbi->cp_rwsem);
92839a53e0cSJaegeuk Kim }
92939a53e0cSJaegeuk Kim 
930e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
93139a53e0cSJaegeuk Kim {
932e479556bSGu Zheng 	up_read(&sbi->cp_rwsem);
93339936837SJaegeuk Kim }
93439936837SJaegeuk Kim 
935e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
93639936837SJaegeuk Kim {
9370daaad97SJaegeuk Kim 	f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
93839936837SJaegeuk Kim }
93939936837SJaegeuk Kim 
940e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
94139936837SJaegeuk Kim {
942e479556bSGu Zheng 	up_write(&sbi->cp_rwsem);
94339a53e0cSJaegeuk Kim }
94439a53e0cSJaegeuk Kim 
945119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
946119ee914SJaegeuk Kim {
947119ee914SJaegeuk Kim 	int reason = CP_SYNC;
948119ee914SJaegeuk Kim 
949119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
950119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
951119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
952119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
953119ee914SJaegeuk Kim 	return reason;
954119ee914SJaegeuk Kim }
955119ee914SJaegeuk Kim 
956119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
957119ee914SJaegeuk Kim {
958119ee914SJaegeuk Kim 	return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
959119ee914SJaegeuk Kim }
960119ee914SJaegeuk Kim 
961119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
962119ee914SJaegeuk Kim {
963119ee914SJaegeuk Kim 	return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
964119ee914SJaegeuk Kim 			is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
965119ee914SJaegeuk Kim }
966119ee914SJaegeuk Kim 
96739a53e0cSJaegeuk Kim /*
96839a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
96939a53e0cSJaegeuk Kim  */
970064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
97139a53e0cSJaegeuk Kim {
972d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
973d6b7d4b3SChao Yu 		return -EINVAL;
974cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
975064e0823SNamjae Jeon 		return -EINVAL;
976064e0823SNamjae Jeon 	return 0;
97739a53e0cSJaegeuk Kim }
97839a53e0cSJaegeuk Kim 
97939a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
98039a53e0cSJaegeuk Kim 
98139a53e0cSJaegeuk Kim /*
98239a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
98339a53e0cSJaegeuk Kim  */
98439a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
98539a53e0cSJaegeuk Kim {
98639a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
9876c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
98839a53e0cSJaegeuk Kim 	else
9896c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
99039a53e0cSJaegeuk Kim }
99139a53e0cSJaegeuk Kim 
9924bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
9934bc8e9bcSChao Yu {
9944bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
9954bc8e9bcSChao Yu }
9964bc8e9bcSChao Yu 
99739a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
99839a53e0cSJaegeuk Kim 				 struct inode *inode, blkcnt_t count)
99939a53e0cSJaegeuk Kim {
100039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
100139a53e0cSJaegeuk Kim 
100239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
100339a53e0cSJaegeuk Kim 	valid_block_count =
100439a53e0cSJaegeuk Kim 		sbi->total_valid_block_count + (block_t)count;
1005cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
100639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
100739a53e0cSJaegeuk Kim 		return false;
100839a53e0cSJaegeuk Kim 	}
100939a53e0cSJaegeuk Kim 	inode->i_blocks += count;
101039a53e0cSJaegeuk Kim 	sbi->total_valid_block_count = valid_block_count;
101139a53e0cSJaegeuk Kim 	sbi->alloc_valid_block_count += (block_t)count;
101239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
101339a53e0cSJaegeuk Kim 	return true;
101439a53e0cSJaegeuk Kim }
101539a53e0cSJaegeuk Kim 
1016da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
101739a53e0cSJaegeuk Kim 						struct inode *inode,
101839a53e0cSJaegeuk Kim 						blkcnt_t count)
101939a53e0cSJaegeuk Kim {
102039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
10219850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
10229850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, inode->i_blocks < count);
102339a53e0cSJaegeuk Kim 	inode->i_blocks -= count;
102439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
102539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
102639a53e0cSJaegeuk Kim }
102739a53e0cSJaegeuk Kim 
102839a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
102939a53e0cSJaegeuk Kim {
103039a53e0cSJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
1031caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
103239a53e0cSJaegeuk Kim }
103339a53e0cSJaegeuk Kim 
1034a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
103539a53e0cSJaegeuk Kim {
1036a7ffdbe2SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1037a7ffdbe2SJaegeuk Kim 	if (S_ISDIR(inode->i_mode))
10384081363fSJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
103939a53e0cSJaegeuk Kim }
104039a53e0cSJaegeuk Kim 
104139a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
104239a53e0cSJaegeuk Kim {
104339a53e0cSJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
104439a53e0cSJaegeuk Kim }
104539a53e0cSJaegeuk Kim 
1046a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
104739a53e0cSJaegeuk Kim {
10485ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
10495ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
10501fe54f9dSJaegeuk Kim 		return;
10511fe54f9dSJaegeuk Kim 
1052a7ffdbe2SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1053a7ffdbe2SJaegeuk Kim 
1054a7ffdbe2SJaegeuk Kim 	if (S_ISDIR(inode->i_mode))
10554081363fSJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
105639a53e0cSJaegeuk Kim }
105739a53e0cSJaegeuk Kim 
105839a53e0cSJaegeuk Kim static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
105939a53e0cSJaegeuk Kim {
106039a53e0cSJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
106139a53e0cSJaegeuk Kim }
106239a53e0cSJaegeuk Kim 
1063a7ffdbe2SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1064f8b2c1f9SJaegeuk Kim {
1065a7ffdbe2SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1066f8b2c1f9SJaegeuk Kim }
1067f8b2c1f9SJaegeuk Kim 
10685ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
10695ac206cfSNamjae Jeon {
10705ac206cfSNamjae Jeon 	unsigned int pages_per_sec = sbi->segs_per_sec *
10715ac206cfSNamjae Jeon 					(1 << sbi->log_blocks_per_seg);
10725ac206cfSNamjae Jeon 	return ((get_pages(sbi, block_type) + pages_per_sec - 1)
10735ac206cfSNamjae Jeon 			>> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
10745ac206cfSNamjae Jeon }
10755ac206cfSNamjae Jeon 
107639a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
107739a53e0cSJaegeuk Kim {
10788b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
107939a53e0cSJaegeuk Kim }
108039a53e0cSJaegeuk Kim 
108139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
108239a53e0cSJaegeuk Kim {
108339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
108439a53e0cSJaegeuk Kim 
108539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
108639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
108739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
108839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
108939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
109039a53e0cSJaegeuk Kim 
109139a53e0cSJaegeuk Kim 	return 0;
109239a53e0cSJaegeuk Kim }
109339a53e0cSJaegeuk Kim 
109455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
109555141486SWanpeng Li {
109655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
109755141486SWanpeng Li }
109855141486SWanpeng Li 
109939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
110039a53e0cSJaegeuk Kim {
110139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
11021dbe4152SChangman Lee 	int offset;
11031dbe4152SChangman Lee 
110455141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
11051dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
11061dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
11071dbe4152SChangman Lee 		else
110865b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
11091dbe4152SChangman Lee 	} else {
11101dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
111125ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
111239a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
111339a53e0cSJaegeuk Kim 	}
11141dbe4152SChangman Lee }
111539a53e0cSJaegeuk Kim 
111639a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
111739a53e0cSJaegeuk Kim {
111839a53e0cSJaegeuk Kim 	block_t start_addr;
111939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1120d71b5564SJaegeuk Kim 	unsigned long long ckpt_version = cur_cp_version(ckpt);
112139a53e0cSJaegeuk Kim 
112225ca923bSJaegeuk Kim 	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
112339a53e0cSJaegeuk Kim 
112439a53e0cSJaegeuk Kim 	/*
112539a53e0cSJaegeuk Kim 	 * odd numbered checkpoint should at cp segment 0
1126e1c42045Sarter97 	 * and even segment must be at cp segment 1
112739a53e0cSJaegeuk Kim 	 */
112839a53e0cSJaegeuk Kim 	if (!(ckpt_version & 1))
112939a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
113039a53e0cSJaegeuk Kim 
113139a53e0cSJaegeuk Kim 	return start_addr;
113239a53e0cSJaegeuk Kim }
113339a53e0cSJaegeuk Kim 
113439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
113539a53e0cSJaegeuk Kim {
113639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
113739a53e0cSJaegeuk Kim }
113839a53e0cSJaegeuk Kim 
113939a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
1140ef86d709SGu Zheng 						struct inode *inode)
114139a53e0cSJaegeuk Kim {
114239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
114339a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
114439a53e0cSJaegeuk Kim 
114539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
114639a53e0cSJaegeuk Kim 
1147ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1148cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
114939a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
115039a53e0cSJaegeuk Kim 		return false;
115139a53e0cSJaegeuk Kim 	}
115239a53e0cSJaegeuk Kim 
1153ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1154cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
115539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
115639a53e0cSJaegeuk Kim 		return false;
115739a53e0cSJaegeuk Kim 	}
115839a53e0cSJaegeuk Kim 
115939a53e0cSJaegeuk Kim 	if (inode)
1160ef86d709SGu Zheng 		inode->i_blocks++;
1161ef86d709SGu Zheng 
1162ef86d709SGu Zheng 	sbi->alloc_valid_block_count++;
1163ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1164ef86d709SGu Zheng 	sbi->total_valid_block_count++;
116539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
116639a53e0cSJaegeuk Kim 
116739a53e0cSJaegeuk Kim 	return true;
116839a53e0cSJaegeuk Kim }
116939a53e0cSJaegeuk Kim 
117039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1171ef86d709SGu Zheng 						struct inode *inode)
117239a53e0cSJaegeuk Kim {
117339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
117439a53e0cSJaegeuk Kim 
11759850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
11769850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
11779850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !inode->i_blocks);
117839a53e0cSJaegeuk Kim 
1179ef86d709SGu Zheng 	inode->i_blocks--;
1180ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1181ef86d709SGu Zheng 	sbi->total_valid_block_count--;
118239a53e0cSJaegeuk Kim 
118339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
118439a53e0cSJaegeuk Kim }
118539a53e0cSJaegeuk Kim 
118639a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
118739a53e0cSJaegeuk Kim {
11888b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
118939a53e0cSJaegeuk Kim }
119039a53e0cSJaegeuk Kim 
119139a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
119239a53e0cSJaegeuk Kim {
119339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
11949850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
119539a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count++;
119639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
119739a53e0cSJaegeuk Kim }
119839a53e0cSJaegeuk Kim 
11990e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
120039a53e0cSJaegeuk Kim {
120139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
12029850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
120339a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count--;
120439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
120539a53e0cSJaegeuk Kim }
120639a53e0cSJaegeuk Kim 
120739a53e0cSJaegeuk Kim static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
120839a53e0cSJaegeuk Kim {
12098b8343faSJaegeuk Kim 	return sbi->total_valid_inode_count;
121039a53e0cSJaegeuk Kim }
121139a53e0cSJaegeuk Kim 
121239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
121339a53e0cSJaegeuk Kim {
1214031fa8ccSJaegeuk Kim 	if (!page)
121539a53e0cSJaegeuk Kim 		return;
121639a53e0cSJaegeuk Kim 
121739a53e0cSJaegeuk Kim 	if (unlock) {
12189850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
121939a53e0cSJaegeuk Kim 		unlock_page(page);
122039a53e0cSJaegeuk Kim 	}
122139a53e0cSJaegeuk Kim 	page_cache_release(page);
122239a53e0cSJaegeuk Kim }
122339a53e0cSJaegeuk Kim 
122439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
122539a53e0cSJaegeuk Kim {
122639a53e0cSJaegeuk Kim 	if (dn->node_page)
122739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
122839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
122939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
123039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
123139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
123239a53e0cSJaegeuk Kim }
123339a53e0cSJaegeuk Kim 
123439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1235e8512d2eSGu Zheng 					size_t size)
123639a53e0cSJaegeuk Kim {
1237e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
123839a53e0cSJaegeuk Kim }
123939a53e0cSJaegeuk Kim 
12407bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
12417bd59381SGu Zheng 						gfp_t flags)
12427bd59381SGu Zheng {
12437bd59381SGu Zheng 	void *entry;
12447bd59381SGu Zheng retry:
12457bd59381SGu Zheng 	entry = kmem_cache_alloc(cachep, flags);
12467bd59381SGu Zheng 	if (!entry) {
12477bd59381SGu Zheng 		cond_resched();
12487bd59381SGu Zheng 		goto retry;
12497bd59381SGu Zheng 	}
12507bd59381SGu Zheng 
12517bd59381SGu Zheng 	return entry;
12527bd59381SGu Zheng }
12537bd59381SGu Zheng 
12549be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
12559be32d72SJaegeuk Kim 				unsigned long index, void *item)
12569be32d72SJaegeuk Kim {
12579be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
12589be32d72SJaegeuk Kim 		cond_resched();
12599be32d72SJaegeuk Kim }
12609be32d72SJaegeuk Kim 
126139a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
126239a53e0cSJaegeuk Kim 
126339a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
126439a53e0cSJaegeuk Kim {
126545590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
126639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
126739a53e0cSJaegeuk Kim }
126839a53e0cSJaegeuk Kim 
126939a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
127039a53e0cSJaegeuk Kim {
127139a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
127239a53e0cSJaegeuk Kim }
127339a53e0cSJaegeuk Kim 
127439a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
127539a53e0cSJaegeuk Kim 		unsigned int offset)
127639a53e0cSJaegeuk Kim {
127739a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
127839a53e0cSJaegeuk Kim 	__le32 *addr_array;
127945590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
128039a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
128139a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
128239a53e0cSJaegeuk Kim }
128339a53e0cSJaegeuk Kim 
128439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
128539a53e0cSJaegeuk Kim {
128639a53e0cSJaegeuk Kim 	int mask;
128739a53e0cSJaegeuk Kim 
128839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
128939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
129039a53e0cSJaegeuk Kim 	return mask & *addr;
129139a53e0cSJaegeuk Kim }
129239a53e0cSJaegeuk Kim 
1293a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1294a66cdd98SJaegeuk Kim {
1295a66cdd98SJaegeuk Kim 	int mask;
1296a66cdd98SJaegeuk Kim 
1297a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1298a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1299a66cdd98SJaegeuk Kim 	*addr |= mask;
1300a66cdd98SJaegeuk Kim }
1301a66cdd98SJaegeuk Kim 
1302a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1303a66cdd98SJaegeuk Kim {
1304a66cdd98SJaegeuk Kim 	int mask;
1305a66cdd98SJaegeuk Kim 
1306a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1307a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1308a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1309a66cdd98SJaegeuk Kim }
1310a66cdd98SJaegeuk Kim 
131152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
131239a53e0cSJaegeuk Kim {
131339a53e0cSJaegeuk Kim 	int mask;
131439a53e0cSJaegeuk Kim 	int ret;
131539a53e0cSJaegeuk Kim 
131639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
131739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
131839a53e0cSJaegeuk Kim 	ret = mask & *addr;
131939a53e0cSJaegeuk Kim 	*addr |= mask;
132039a53e0cSJaegeuk Kim 	return ret;
132139a53e0cSJaegeuk Kim }
132239a53e0cSJaegeuk Kim 
132352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
132439a53e0cSJaegeuk Kim {
132539a53e0cSJaegeuk Kim 	int mask;
132639a53e0cSJaegeuk Kim 	int ret;
132739a53e0cSJaegeuk Kim 
132839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
132939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
133039a53e0cSJaegeuk Kim 	ret = mask & *addr;
133139a53e0cSJaegeuk Kim 	*addr &= ~mask;
133239a53e0cSJaegeuk Kim 	return ret;
133339a53e0cSJaegeuk Kim }
133439a53e0cSJaegeuk Kim 
1335c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1336c6ac4c0eSGu Zheng {
1337c6ac4c0eSGu Zheng 	int mask;
1338c6ac4c0eSGu Zheng 
1339c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1340c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1341c6ac4c0eSGu Zheng 	*addr ^= mask;
1342c6ac4c0eSGu Zheng }
1343c6ac4c0eSGu Zheng 
134439a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
134539a53e0cSJaegeuk Kim enum {
134639a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1347b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
1348ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
134939a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
135039a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
135139a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1352c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1353699489bbSJaegeuk Kim 	FI_UPDATE_DIR,		/* should update inode block for consistency */
135474d0b917SJaegeuk Kim 	FI_DELAY_IPUT,		/* used for the recovery */
1355c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1356444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
13571001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
135834d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1359fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1360fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
136188b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
136288b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
136302a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
13643c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
13651e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
1366b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
1367510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
136839a53e0cSJaegeuk Kim };
136939a53e0cSJaegeuk Kim 
137039a53e0cSJaegeuk Kim static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
137139a53e0cSJaegeuk Kim {
137261e0f2d0SJaegeuk Kim 	if (!test_bit(flag, &fi->flags))
137339a53e0cSJaegeuk Kim 		set_bit(flag, &fi->flags);
137439a53e0cSJaegeuk Kim }
137539a53e0cSJaegeuk Kim 
137639a53e0cSJaegeuk Kim static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
137739a53e0cSJaegeuk Kim {
137839a53e0cSJaegeuk Kim 	return test_bit(flag, &fi->flags);
137939a53e0cSJaegeuk Kim }
138039a53e0cSJaegeuk Kim 
138139a53e0cSJaegeuk Kim static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
138239a53e0cSJaegeuk Kim {
138361e0f2d0SJaegeuk Kim 	if (test_bit(flag, &fi->flags))
138439a53e0cSJaegeuk Kim 		clear_bit(flag, &fi->flags);
138539a53e0cSJaegeuk Kim }
138639a53e0cSJaegeuk Kim 
138739a53e0cSJaegeuk Kim static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
138839a53e0cSJaegeuk Kim {
138939a53e0cSJaegeuk Kim 	fi->i_acl_mode = mode;
139039a53e0cSJaegeuk Kim 	set_inode_flag(fi, FI_ACL_MODE);
139139a53e0cSJaegeuk Kim }
139239a53e0cSJaegeuk Kim 
1393444c580fSJaegeuk Kim static inline void get_inline_info(struct f2fs_inode_info *fi,
1394444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1395444c580fSJaegeuk Kim {
1396444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
1397444c580fSJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_XATTR);
13981001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
13991001b347SHuajun Li 		set_inode_flag(fi, FI_INLINE_DATA);
140034d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
140134d67debSChao Yu 		set_inode_flag(fi, FI_INLINE_DENTRY);
1402b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
1403b3d208f9SJaegeuk Kim 		set_inode_flag(fi, FI_DATA_EXIST);
1404510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
1405510022a8SJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_DOTS);
1406444c580fSJaegeuk Kim }
1407444c580fSJaegeuk Kim 
1408444c580fSJaegeuk Kim static inline void set_raw_inline(struct f2fs_inode_info *fi,
1409444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1410444c580fSJaegeuk Kim {
1411444c580fSJaegeuk Kim 	ri->i_inline = 0;
1412444c580fSJaegeuk Kim 
1413444c580fSJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_XATTR))
1414444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
14151001b347SHuajun Li 	if (is_inode_flag_set(fi, FI_INLINE_DATA))
14161001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
141734d67debSChao Yu 	if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
141834d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
1419b3d208f9SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_DATA_EXIST))
1420b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
1421510022a8SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_DOTS))
1422510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
1423444c580fSJaegeuk Kim }
1424444c580fSJaegeuk Kim 
1425987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
1426987c7c31SChao Yu {
1427987c7c31SChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
1428987c7c31SChao Yu }
1429987c7c31SChao Yu 
1430de93653fSJaegeuk Kim static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
1431de93653fSJaegeuk Kim {
1432987c7c31SChao Yu 	if (f2fs_has_inline_xattr(&fi->vfs_inode))
1433de93653fSJaegeuk Kim 		return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1434de93653fSJaegeuk Kim 	return DEF_ADDRS_PER_INODE;
1435de93653fSJaegeuk Kim }
1436de93653fSJaegeuk Kim 
143765985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
143865985d93SJaegeuk Kim {
1439695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
144065985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
144165985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
144265985d93SJaegeuk Kim }
144365985d93SJaegeuk Kim 
144465985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
144565985d93SJaegeuk Kim {
1446987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
144765985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
144865985d93SJaegeuk Kim 	else
144965985d93SJaegeuk Kim 		return 0;
145065985d93SJaegeuk Kim }
145165985d93SJaegeuk Kim 
14520dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
14530dbdc2aeSJaegeuk Kim {
14540dbdc2aeSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
14550dbdc2aeSJaegeuk Kim }
14560dbdc2aeSJaegeuk Kim 
1457b3d208f9SJaegeuk Kim static inline void f2fs_clear_inline_inode(struct inode *inode)
1458b3d208f9SJaegeuk Kim {
1459b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1460b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1461b3d208f9SJaegeuk Kim }
1462b3d208f9SJaegeuk Kim 
1463b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
1464b3d208f9SJaegeuk Kim {
1465b3d208f9SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
1466b3d208f9SJaegeuk Kim }
1467b3d208f9SJaegeuk Kim 
1468510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
1469510022a8SJaegeuk Kim {
1470510022a8SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
1471510022a8SJaegeuk Kim }
1472510022a8SJaegeuk Kim 
147388b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
147488b88a66SJaegeuk Kim {
147588b88a66SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
147688b88a66SJaegeuk Kim }
147788b88a66SJaegeuk Kim 
147802a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
147902a1335fSJaegeuk Kim {
148002a1335fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
148102a1335fSJaegeuk Kim }
148202a1335fSJaegeuk Kim 
14833c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
14843c6c2bebSJaegeuk Kim {
14853c6c2bebSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
14863c6c2bebSJaegeuk Kim }
14873c6c2bebSJaegeuk Kim 
14881e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
14891e84371fSJaegeuk Kim {
14901e84371fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
14911e84371fSJaegeuk Kim }
14921e84371fSJaegeuk Kim 
14931001b347SHuajun Li static inline void *inline_data_addr(struct page *page)
14941001b347SHuajun Li {
1495695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
14961001b347SHuajun Li 	return (void *)&(ri->i_addr[1]);
14971001b347SHuajun Li }
14981001b347SHuajun Li 
149934d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
150034d67debSChao Yu {
150134d67debSChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
150234d67debSChao Yu }
150334d67debSChao Yu 
15049486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
15059486ba44SJaegeuk Kim {
15069486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
15079486ba44SJaegeuk Kim 		kunmap(page);
15089486ba44SJaegeuk Kim }
15099486ba44SJaegeuk Kim 
1510b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
1511b5492af7SJaegeuk Kim {
1512b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
1513b5492af7SJaegeuk Kim }
1514b5492af7SJaegeuk Kim 
1515b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
1516b5492af7SJaegeuk Kim {
1517b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
1518b5492af7SJaegeuk Kim }
1519b5492af7SJaegeuk Kim 
1520b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
1521b5492af7SJaegeuk Kim {
1522b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
1523b5492af7SJaegeuk Kim }
1524b5492af7SJaegeuk Kim 
152577888c1eSJaegeuk Kim static inline int f2fs_readonly(struct super_block *sb)
152677888c1eSJaegeuk Kim {
152777888c1eSJaegeuk Kim 	return sb->s_flags & MS_RDONLY;
152877888c1eSJaegeuk Kim }
152977888c1eSJaegeuk Kim 
15301e968fdfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
15311e968fdfSJaegeuk Kim {
15321e968fdfSJaegeuk Kim 	return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
15331e968fdfSJaegeuk Kim }
15341e968fdfSJaegeuk Kim 
1535744602cfSJaegeuk Kim static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
1536744602cfSJaegeuk Kim {
1537744602cfSJaegeuk Kim 	set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1538744602cfSJaegeuk Kim 	sbi->sb->s_flags |= MS_RDONLY;
1539744602cfSJaegeuk Kim }
1540744602cfSJaegeuk Kim 
1541eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
1542eaa693f4SJaegeuk Kim {
1543eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
1544eaa693f4SJaegeuk Kim 		return true;
1545eaa693f4SJaegeuk Kim 
1546eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
1547eaa693f4SJaegeuk Kim 		return true;
1548eaa693f4SJaegeuk Kim 
1549eaa693f4SJaegeuk Kim 	return false;
1550eaa693f4SJaegeuk Kim }
1551eaa693f4SJaegeuk Kim 
15523e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
15533e72f721SJaegeuk Kim {
15543e72f721SJaegeuk Kim 	mode_t mode = inode->i_mode;
15553e72f721SJaegeuk Kim 
15563e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
15573e72f721SJaegeuk Kim 			is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
15583e72f721SJaegeuk Kim 		return false;
15593e72f721SJaegeuk Kim 
15603e72f721SJaegeuk Kim 	return S_ISREG(mode);
15613e72f721SJaegeuk Kim }
15623e72f721SJaegeuk Kim 
1563a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
1564a6dda0e6SChristoph Hellwig 	((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
1565a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
1566a6dda0e6SChristoph Hellwig 
1567267378d4SChao Yu /* get offset of first page in next direct node */
1568267378d4SChao Yu #define PGOFS_OF_NEXT_DNODE(pgofs, fi)				\
1569267378d4SChao Yu 	((pgofs < ADDRS_PER_INODE(fi)) ? ADDRS_PER_INODE(fi) :	\
1570267378d4SChao Yu 	(pgofs - ADDRS_PER_INODE(fi) + ADDRS_PER_BLOCK) /	\
1571267378d4SChao Yu 	ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi))
1572267378d4SChao Yu 
157339a53e0cSJaegeuk Kim /*
157439a53e0cSJaegeuk Kim  * file.c
157539a53e0cSJaegeuk Kim  */
157639a53e0cSJaegeuk Kim int f2fs_sync_file(struct file *, loff_t, loff_t, int);
157739a53e0cSJaegeuk Kim void truncate_data_blocks(struct dnode_of_data *);
1578764aa3e9SJaegeuk Kim int truncate_blocks(struct inode *, u64, bool);
157939a53e0cSJaegeuk Kim void f2fs_truncate(struct inode *);
15802d4d9fb5SJaegeuk Kim int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
158139a53e0cSJaegeuk Kim int f2fs_setattr(struct dentry *, struct iattr *);
158239a53e0cSJaegeuk Kim int truncate_hole(struct inode *, pgoff_t, pgoff_t);
1583b292dcabSJaegeuk Kim int truncate_data_blocks_range(struct dnode_of_data *, int);
158439a53e0cSJaegeuk Kim long f2fs_ioctl(struct file *, unsigned int, unsigned long);
1585e9750824SNamjae Jeon long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
158639a53e0cSJaegeuk Kim 
158739a53e0cSJaegeuk Kim /*
158839a53e0cSJaegeuk Kim  * inode.c
158939a53e0cSJaegeuk Kim  */
159039a53e0cSJaegeuk Kim void f2fs_set_inode_flags(struct inode *);
159139a53e0cSJaegeuk Kim struct inode *f2fs_iget(struct super_block *, unsigned long);
15924660f9c0SJaegeuk Kim int try_to_free_nats(struct f2fs_sb_info *, int);
159339a53e0cSJaegeuk Kim void update_inode(struct inode *, struct page *);
1594744602cfSJaegeuk Kim void update_inode_page(struct inode *);
159539a53e0cSJaegeuk Kim int f2fs_write_inode(struct inode *, struct writeback_control *);
159639a53e0cSJaegeuk Kim void f2fs_evict_inode(struct inode *);
159744c16156SJaegeuk Kim void handle_failed_inode(struct inode *);
159839a53e0cSJaegeuk Kim 
159939a53e0cSJaegeuk Kim /*
160039a53e0cSJaegeuk Kim  * namei.c
160139a53e0cSJaegeuk Kim  */
160239a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
160339a53e0cSJaegeuk Kim 
160439a53e0cSJaegeuk Kim /*
160539a53e0cSJaegeuk Kim  * dir.c
160639a53e0cSJaegeuk Kim  */
1607dbeacf02SChao Yu extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
1608510022a8SJaegeuk Kim void set_de_type(struct f2fs_dir_entry *, umode_t);
16096e22c691SJaegeuk Kim 
16106e22c691SJaegeuk Kim struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *,
16116e22c691SJaegeuk Kim 			f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
16127b3cd7d6SJaegeuk Kim bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
1613d8c6822aSJaegeuk Kim 			unsigned int, struct f2fs_str *);
1614062a3e7bSJaegeuk Kim void do_make_empty_dir(struct inode *, struct inode *,
1615062a3e7bSJaegeuk Kim 			struct f2fs_dentry_ptr *);
1616dbeacf02SChao Yu struct page *init_inode_metadata(struct inode *, struct inode *,
1617bce8d112SJaegeuk Kim 			const struct qstr *, struct page *);
1618dbeacf02SChao Yu void update_parent_metadata(struct inode *, struct inode *, unsigned int);
1619a82afa20SJaegeuk Kim int room_for_filename(const void *, int, int);
1620dbeacf02SChao Yu void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
162139a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
162239a53e0cSJaegeuk Kim 							struct page **);
162339a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
162439a53e0cSJaegeuk Kim ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
162539a53e0cSJaegeuk Kim void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
162639a53e0cSJaegeuk Kim 				struct page *, struct inode *);
1627e7d55452SJaegeuk Kim int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
1628510022a8SJaegeuk Kim void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
16293b4d732aSChao Yu 			const struct qstr *, f2fs_hash_t , unsigned int);
1630510022a8SJaegeuk Kim int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
1631510022a8SJaegeuk Kim 			umode_t);
1632dbeacf02SChao Yu void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
1633dbeacf02SChao Yu 							struct inode *);
1634b97a9b5dSJaegeuk Kim int f2fs_do_tmpfile(struct inode *, struct inode *);
163539a53e0cSJaegeuk Kim bool f2fs_empty_dir(struct inode *);
163639a53e0cSJaegeuk Kim 
1637b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
1638b7f7a5e0SAl Viro {
16392b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
1640510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
1641b7f7a5e0SAl Viro }
1642b7f7a5e0SAl Viro 
164339a53e0cSJaegeuk Kim /*
164439a53e0cSJaegeuk Kim  * super.c
164539a53e0cSJaegeuk Kim  */
1646c5bda1c8SChao Yu int f2fs_commit_super(struct f2fs_sb_info *, bool);
164739a53e0cSJaegeuk Kim int f2fs_sync_fs(struct super_block *, int);
1648a07ef784SNamjae Jeon extern __printf(3, 4)
1649a07ef784SNamjae Jeon void f2fs_msg(struct super_block *, const char *, const char *, ...);
165039a53e0cSJaegeuk Kim 
165139a53e0cSJaegeuk Kim /*
165239a53e0cSJaegeuk Kim  * hash.c
165339a53e0cSJaegeuk Kim  */
1654eee6160fSGu Zheng f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
165539a53e0cSJaegeuk Kim 
165639a53e0cSJaegeuk Kim /*
165739a53e0cSJaegeuk Kim  * node.c
165839a53e0cSJaegeuk Kim  */
165939a53e0cSJaegeuk Kim struct dnode_of_data;
166039a53e0cSJaegeuk Kim struct node_info;
166139a53e0cSJaegeuk Kim 
16626fb03f3aSJaegeuk Kim bool available_free_memory(struct f2fs_sb_info *, int);
16632dcf51abSJaegeuk Kim int need_dentry_mark(struct f2fs_sb_info *, nid_t);
166488bd02c9SJaegeuk Kim bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
166588bd02c9SJaegeuk Kim bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
166639a53e0cSJaegeuk Kim void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
166739a53e0cSJaegeuk Kim int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
166839a53e0cSJaegeuk Kim int truncate_inode_blocks(struct inode *, pgoff_t);
16694f16fb0fSJaegeuk Kim int truncate_xattr_node(struct inode *, struct page *);
1670cfe58f9dSJaegeuk Kim int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
167158e674d6SGu Zheng void remove_inode_page(struct inode *);
1672a014e037SJaegeuk Kim struct page *new_inode_page(struct inode *);
16738ae8f162SJaegeuk Kim struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
167439a53e0cSJaegeuk Kim void ra_node_page(struct f2fs_sb_info *, nid_t);
167539a53e0cSJaegeuk Kim struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
167639a53e0cSJaegeuk Kim struct page *get_node_page_ra(struct page *, int);
167739a53e0cSJaegeuk Kim void sync_inode_page(struct dnode_of_data *);
167839a53e0cSJaegeuk Kim int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
167939a53e0cSJaegeuk Kim bool alloc_nid(struct f2fs_sb_info *, nid_t *);
168039a53e0cSJaegeuk Kim void alloc_nid_done(struct f2fs_sb_info *, nid_t);
168139a53e0cSJaegeuk Kim void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
168270cfed88SChao Yu void recover_inline_xattr(struct inode *, struct page *);
16831c35a90eSJaegeuk Kim void recover_xattr_data(struct inode *, struct page *, block_t);
168439a53e0cSJaegeuk Kim int recover_inode_page(struct f2fs_sb_info *, struct page *);
168539a53e0cSJaegeuk Kim int restore_node_summary(struct f2fs_sb_info *, unsigned int,
168639a53e0cSJaegeuk Kim 				struct f2fs_summary_block *);
168739a53e0cSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *);
168839a53e0cSJaegeuk Kim int build_node_manager(struct f2fs_sb_info *);
168939a53e0cSJaegeuk Kim void destroy_node_manager(struct f2fs_sb_info *);
16906e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
169139a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
169239a53e0cSJaegeuk Kim 
169339a53e0cSJaegeuk Kim /*
169439a53e0cSJaegeuk Kim  * segment.c
169539a53e0cSJaegeuk Kim  */
169688b88a66SJaegeuk Kim void register_inmem_page(struct inode *, struct page *);
169788b88a66SJaegeuk Kim void commit_inmem_pages(struct inode *, bool);
169839a53e0cSJaegeuk Kim void f2fs_balance_fs(struct f2fs_sb_info *);
16994660f9c0SJaegeuk Kim void f2fs_balance_fs_bg(struct f2fs_sb_info *);
17006b4afdd7SJaegeuk Kim int f2fs_issue_flush(struct f2fs_sb_info *);
17012163d198SGu Zheng int create_flush_cmd_control(struct f2fs_sb_info *);
17022163d198SGu Zheng void destroy_flush_cmd_control(struct f2fs_sb_info *);
170339a53e0cSJaegeuk Kim void invalidate_blocks(struct f2fs_sb_info *, block_t);
17045e443818SJaegeuk Kim void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
1705836b5a63SJaegeuk Kim void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
17064b2fecc8SJaegeuk Kim void release_discard_addrs(struct f2fs_sb_info *);
1707cf2271e7SJaegeuk Kim void discard_next_dnode(struct f2fs_sb_info *, block_t);
17083fa06d7bSChao Yu int npages_for_summary_flush(struct f2fs_sb_info *, bool);
170939a53e0cSJaegeuk Kim void allocate_new_segments(struct f2fs_sb_info *);
17104b2fecc8SJaegeuk Kim int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
171139a53e0cSJaegeuk Kim struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
1712381722d2SChao Yu void update_meta_page(struct f2fs_sb_info *, void *, block_t);
1713577e3495SJaegeuk Kim void write_meta_page(struct f2fs_sb_info *, struct page *);
171405ca3632SJaegeuk Kim void write_node_page(unsigned int, struct f2fs_io_info *);
171505ca3632SJaegeuk Kim void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
171605ca3632SJaegeuk Kim void rewrite_data_page(struct f2fs_io_info *);
1717528e3459SChao Yu void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
1718528e3459SChao Yu 				block_t, block_t, unsigned char, bool);
1719bfad7c2dSJaegeuk Kim void allocate_data_block(struct f2fs_sb_info *, struct page *,
1720bfad7c2dSJaegeuk Kim 		block_t, block_t *, struct f2fs_summary *, int);
17215514f0aaSYuan Zhong void f2fs_wait_on_page_writeback(struct page *, enum page_type);
172239a53e0cSJaegeuk Kim void write_data_summaries(struct f2fs_sb_info *, block_t);
172339a53e0cSJaegeuk Kim void write_node_summaries(struct f2fs_sb_info *, block_t);
172439a53e0cSJaegeuk Kim int lookup_journal_in_cursum(struct f2fs_summary_block *,
172539a53e0cSJaegeuk Kim 					int, unsigned int, int);
17264b2fecc8SJaegeuk Kim void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
172739a53e0cSJaegeuk Kim int build_segment_manager(struct f2fs_sb_info *);
172839a53e0cSJaegeuk Kim void destroy_segment_manager(struct f2fs_sb_info *);
17297fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
17307fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
173139a53e0cSJaegeuk Kim 
173239a53e0cSJaegeuk Kim /*
173339a53e0cSJaegeuk Kim  * checkpoint.c
173439a53e0cSJaegeuk Kim  */
173539a53e0cSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
173639a53e0cSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
1737f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
17384c521f49SJaegeuk Kim int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
1739635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
174039a53e0cSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
1741fff04f90SJaegeuk Kim void add_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
1742fff04f90SJaegeuk Kim void remove_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
17436f12ac25SJaegeuk Kim void release_dirty_inode(struct f2fs_sb_info *);
1744fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
1745cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *);
1746cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *);
174739a53e0cSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *, nid_t);
174839a53e0cSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
17498f99a946SChao Yu void recover_orphan_inodes(struct f2fs_sb_info *);
175039a53e0cSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *);
1751a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *, struct page *);
17525deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *);
175339a53e0cSJaegeuk Kim void remove_dirty_dir_inode(struct inode *);
175439a53e0cSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *);
175575ab4cb8SJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
17566451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *);
17576e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
175839a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
175939a53e0cSJaegeuk Kim 
176039a53e0cSJaegeuk Kim /*
176139a53e0cSJaegeuk Kim  * data.c
176239a53e0cSJaegeuk Kim  */
1763458e6197SJaegeuk Kim void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
176405ca3632SJaegeuk Kim int f2fs_submit_page_bio(struct f2fs_io_info *);
176505ca3632SJaegeuk Kim void f2fs_submit_page_mbio(struct f2fs_io_info *);
1766216a620aSChao Yu void set_data_blkaddr(struct dnode_of_data *);
176739a53e0cSJaegeuk Kim int reserve_new_block(struct dnode_of_data *);
1768b600965cSHuajun Li int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
176943f3eae1SJaegeuk Kim struct page *get_read_data_page(struct inode *, pgoff_t, int);
177043f3eae1SJaegeuk Kim struct page *find_data_page(struct inode *, pgoff_t);
177139a53e0cSJaegeuk Kim struct page *get_lock_data_page(struct inode *, pgoff_t);
177264aa7ed9SJaegeuk Kim struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
177305ca3632SJaegeuk Kim int do_write_data_page(struct f2fs_io_info *);
17749ab70134SJaegeuk Kim int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
1775487261f3SChao Yu void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
1776487261f3SChao Yu int f2fs_release_page(struct page *, gfp_t);
177739a53e0cSJaegeuk Kim 
177839a53e0cSJaegeuk Kim /*
177939a53e0cSJaegeuk Kim  * gc.c
178039a53e0cSJaegeuk Kim  */
178139a53e0cSJaegeuk Kim int start_gc_thread(struct f2fs_sb_info *);
178239a53e0cSJaegeuk Kim void stop_gc_thread(struct f2fs_sb_info *);
1783de93653fSJaegeuk Kim block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
1784408e9375SJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *);
178539a53e0cSJaegeuk Kim void build_gc_manager(struct f2fs_sb_info *);
178639a53e0cSJaegeuk Kim 
178739a53e0cSJaegeuk Kim /*
178839a53e0cSJaegeuk Kim  * recovery.c
178939a53e0cSJaegeuk Kim  */
17906ead1142SJaegeuk Kim int recover_fsync_data(struct f2fs_sb_info *);
179139a53e0cSJaegeuk Kim bool space_for_roll_forward(struct f2fs_sb_info *);
179239a53e0cSJaegeuk Kim 
179339a53e0cSJaegeuk Kim /*
179439a53e0cSJaegeuk Kim  * debug.c
179539a53e0cSJaegeuk Kim  */
179639a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
179739a53e0cSJaegeuk Kim struct f2fs_stat_info {
179839a53e0cSJaegeuk Kim 	struct list_head stat_list;
179939a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
180039a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
180139a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
18024bf6fd9fSChao Yu 	int hit_ext, total_ext, ext_tree, ext_node;
180339a53e0cSJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
1804dd4e4b59SJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits, fnids;
180539a53e0cSJaegeuk Kim 	int total_count, utilization;
1806d24bdcbfSJaegeuk Kim 	int bg_gc, inline_inode, inline_dir, inmem_pages, wb_pages;
180739a53e0cSJaegeuk Kim 	unsigned int valid_count, valid_node_count, valid_inode_count;
180839a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
180939a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
181039a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
181139a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
1812942e0be6SChangman Lee 	int prefree_count, call_count, cp_count;
181339a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
1814e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
181539a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
1816e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
181739a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
181839a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
181939a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
182039a53e0cSJaegeuk Kim 
182139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
182239a53e0cSJaegeuk Kim 	unsigned int block_count[2];
1823b9a2c252SChangman Lee 	unsigned int inplace_count;
18246f0aacbcSJaegeuk Kim 	unsigned base_mem, cache_mem, page_mem;
182539a53e0cSJaegeuk Kim };
182639a53e0cSJaegeuk Kim 
1827963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1828963d4f7dSGu Zheng {
1829963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
1830963d4f7dSGu Zheng }
1831963d4f7dSGu Zheng 
1832942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
183339a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
1834dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
1835dcdfff65SJaegeuk Kim #define stat_inc_dirty_dir(sbi)		((sbi)->n_dirty_dirs++)
1836dcdfff65SJaegeuk Kim #define stat_dec_dirty_dir(sbi)		((sbi)->n_dirty_dirs--)
1837dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)		((F2FS_SB(sb))->total_hit_ext++)
1838dcdfff65SJaegeuk Kim #define stat_inc_read_hit(sb)		((F2FS_SB(sb))->read_hit_ext++)
18390dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
18400dbdc2aeSJaegeuk Kim 	do {								\
18410dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
184203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
18430dbdc2aeSJaegeuk Kim 	} while (0)
18440dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
18450dbdc2aeSJaegeuk Kim 	do {								\
18460dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
184703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
18480dbdc2aeSJaegeuk Kim 	} while (0)
18493289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
18503289c061SJaegeuk Kim 	do {								\
18513289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
185203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
18533289c061SJaegeuk Kim 	} while (0)
18543289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
18553289c061SJaegeuk Kim 	do {								\
18563289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
185703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
18583289c061SJaegeuk Kim 	} while (0)
1859dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
1860dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
1861dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
1862dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
1863b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
1864b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
1865e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
186639a53e0cSJaegeuk Kim 	do {								\
1867963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
186839a53e0cSJaegeuk Kim 		(si)->tot_segs++;					\
1869e1235983SChangman Lee 		if (type == SUM_TYPE_DATA) {				\
187039a53e0cSJaegeuk Kim 			si->data_segs++;				\
1871e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
1872e1235983SChangman Lee 		} else {						\
187339a53e0cSJaegeuk Kim 			si->node_segs++;				\
1874e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
1875e1235983SChangman Lee 		}							\
187639a53e0cSJaegeuk Kim 	} while (0)
187739a53e0cSJaegeuk Kim 
187839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
187939a53e0cSJaegeuk Kim 	(si->tot_blks += (blks))
188039a53e0cSJaegeuk Kim 
1881e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
188239a53e0cSJaegeuk Kim 	do {								\
1883963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
188439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
188539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
1886e1235983SChangman Lee 		si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0;	\
188739a53e0cSJaegeuk Kim 	} while (0)
188839a53e0cSJaegeuk Kim 
1889e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
189039a53e0cSJaegeuk Kim 	do {								\
1891963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
189239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
189339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
1894e1235983SChangman Lee 		si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0;	\
189539a53e0cSJaegeuk Kim 	} while (0)
189639a53e0cSJaegeuk Kim 
189739a53e0cSJaegeuk Kim int f2fs_build_stats(struct f2fs_sb_info *);
189839a53e0cSJaegeuk Kim void f2fs_destroy_stats(struct f2fs_sb_info *);
18996e6093a8SNamjae Jeon void __init f2fs_create_root_stats(void);
19004589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
190139a53e0cSJaegeuk Kim #else
1902942e0be6SChangman Lee #define stat_inc_cp_count(si)
190339a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
1904dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(si)
1905dcdfff65SJaegeuk Kim #define stat_inc_dirty_dir(sbi)
1906dcdfff65SJaegeuk Kim #define stat_dec_dirty_dir(sbi)
1907dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)
1908dcdfff65SJaegeuk Kim #define stat_inc_read_hit(sb)
19090dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)
19100dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)
19113289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)
19123289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)
1913dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)
1914dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)
1915b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)
1916e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)
191739a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
1918e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)
1919e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)
192039a53e0cSJaegeuk Kim 
192139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
192239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
19236e6093a8SNamjae Jeon static inline void __init f2fs_create_root_stats(void) { }
19244589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
192539a53e0cSJaegeuk Kim #endif
192639a53e0cSJaegeuk Kim 
192739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
192839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
192939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
193039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
193139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
193239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
193339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
193439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
1935cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
193639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
193729e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
19381001b347SHuajun Li 
1939e18c65b2SHuajun Li /*
1940e18c65b2SHuajun Li  * inline.c
1941e18c65b2SHuajun Li  */
194201b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *);
194301b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *);
1944b3d208f9SJaegeuk Kim void read_inline_data(struct page *, struct page *);
19450bfcfccaSChao Yu bool truncate_inline_inode(struct page *, u64);
1946e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *, struct page *);
1947b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
1948b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *);
1949b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *, struct page *);
19500342fd30SJaegeuk Kim bool recover_inline_data(struct inode *, struct page *);
19516e22c691SJaegeuk Kim struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
19526e22c691SJaegeuk Kim 				struct f2fs_filename *, struct page **);
1953201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
1954201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
1955510022a8SJaegeuk Kim int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
1956510022a8SJaegeuk Kim 						nid_t, umode_t);
1957201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
1958201a05beSChao Yu 						struct inode *, struct inode *);
1959201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *);
1960d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *, struct dir_context *,
1961d8c6822aSJaegeuk Kim 						struct f2fs_str *);
1962cde4de12SJaegeuk Kim 
1963cde4de12SJaegeuk Kim /*
19642658e50dSJaegeuk Kim  * shrinker.c
19652658e50dSJaegeuk Kim  */
19662658e50dSJaegeuk Kim unsigned long f2fs_shrink_count(struct shrinker *, struct shrink_control *);
19672658e50dSJaegeuk Kim unsigned long f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
19682658e50dSJaegeuk Kim void f2fs_join_shrinker(struct f2fs_sb_info *);
19692658e50dSJaegeuk Kim void f2fs_leave_shrinker(struct f2fs_sb_info *);
19702658e50dSJaegeuk Kim 
19712658e50dSJaegeuk Kim /*
1972*a28ef1f5SChao Yu  * extent_cache.c
1973*a28ef1f5SChao Yu  */
1974*a28ef1f5SChao Yu unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
1975*a28ef1f5SChao Yu void f2fs_drop_largest_extent(struct inode *, pgoff_t);
1976*a28ef1f5SChao Yu void f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
1977*a28ef1f5SChao Yu unsigned int f2fs_destroy_extent_node(struct inode *);
1978*a28ef1f5SChao Yu void f2fs_destroy_extent_tree(struct inode *);
1979*a28ef1f5SChao Yu bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
1980*a28ef1f5SChao Yu void f2fs_update_extent_cache(struct dnode_of_data *);
1981*a28ef1f5SChao Yu void init_extent_cache_info(struct f2fs_sb_info *);
1982*a28ef1f5SChao Yu int __init create_extent_cache(void);
1983*a28ef1f5SChao Yu void destroy_extent_cache(void);
1984*a28ef1f5SChao Yu 
1985*a28ef1f5SChao Yu /*
1986cde4de12SJaegeuk Kim  * crypto support
1987cde4de12SJaegeuk Kim  */
1988cde4de12SJaegeuk Kim static inline int f2fs_encrypted_inode(struct inode *inode)
1989cde4de12SJaegeuk Kim {
1990cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
1991cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
1992cde4de12SJaegeuk Kim #else
1993cde4de12SJaegeuk Kim 	return 0;
1994cde4de12SJaegeuk Kim #endif
1995cde4de12SJaegeuk Kim }
1996cde4de12SJaegeuk Kim 
1997cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
1998cde4de12SJaegeuk Kim {
1999cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2000cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2001cde4de12SJaegeuk Kim #endif
2002cde4de12SJaegeuk Kim }
2003cde4de12SJaegeuk Kim 
2004cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2005cde4de12SJaegeuk Kim {
2006cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2007cde4de12SJaegeuk Kim 	return unlikely(bio->bi_private != NULL);
2008cde4de12SJaegeuk Kim #else
2009cde4de12SJaegeuk Kim 	return false;
2010cde4de12SJaegeuk Kim #endif
2011cde4de12SJaegeuk Kim }
2012cde4de12SJaegeuk Kim 
2013cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2014cde4de12SJaegeuk Kim {
2015cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2016cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2017cde4de12SJaegeuk Kim #else
2018cde4de12SJaegeuk Kim 	return 0;
2019cde4de12SJaegeuk Kim #endif
2020cde4de12SJaegeuk Kim }
2021f424f664SJaegeuk Kim 
2022fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
2023fcc85a4dSJaegeuk Kim {
2024fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2025fcc85a4dSJaegeuk Kim 	mode_t mode = inode->i_mode;
2026fcc85a4dSJaegeuk Kim 
2027fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2028fcc85a4dSJaegeuk Kim #else
2029fcc85a4dSJaegeuk Kim 	return 0;
2030fcc85a4dSJaegeuk Kim #endif
2031fcc85a4dSJaegeuk Kim }
2032fcc85a4dSJaegeuk Kim 
2033f424f664SJaegeuk Kim /* crypto_policy.c */
2034f424f664SJaegeuk Kim int f2fs_is_child_context_consistent_with_parent(struct inode *,
2035f424f664SJaegeuk Kim 							struct inode *);
2036f424f664SJaegeuk Kim int f2fs_inherit_context(struct inode *, struct inode *, struct page *);
2037f424f664SJaegeuk Kim int f2fs_process_policy(const struct f2fs_encryption_policy *, struct inode *);
2038f424f664SJaegeuk Kim int f2fs_get_policy(struct inode *, struct f2fs_encryption_policy *);
203957e5055bSJaegeuk Kim 
204057e5055bSJaegeuk Kim /* crypt.c */
20418bacf6deSJaegeuk Kim extern struct kmem_cache *f2fs_crypt_info_cachep;
204257e5055bSJaegeuk Kim bool f2fs_valid_contents_enc_mode(uint32_t);
204357e5055bSJaegeuk Kim uint32_t f2fs_validate_encryption_key_size(uint32_t, uint32_t);
204457e5055bSJaegeuk Kim struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *);
204557e5055bSJaegeuk Kim void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *);
204657e5055bSJaegeuk Kim struct page *f2fs_encrypt(struct inode *, struct page *);
204757e5055bSJaegeuk Kim int f2fs_decrypt(struct f2fs_crypto_ctx *, struct page *);
204857e5055bSJaegeuk Kim int f2fs_decrypt_one(struct inode *, struct page *);
204957e5055bSJaegeuk Kim void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
205057e5055bSJaegeuk Kim 
20510adda907SJaegeuk Kim /* crypto_key.c */
205226bf3dc7SJaegeuk Kim void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
20530adda907SJaegeuk Kim int _f2fs_get_encryption_info(struct inode *inode);
20540adda907SJaegeuk Kim 
20556b3bd08fSJaegeuk Kim /* crypto_fname.c */
20566b3bd08fSJaegeuk Kim bool f2fs_valid_filenames_enc_mode(uint32_t);
20576b3bd08fSJaegeuk Kim u32 f2fs_fname_crypto_round_up(u32, u32);
20586b3bd08fSJaegeuk Kim int f2fs_fname_crypto_alloc_buffer(struct inode *, u32, struct f2fs_str *);
20596b3bd08fSJaegeuk Kim int f2fs_fname_disk_to_usr(struct inode *, f2fs_hash_t *,
20606b3bd08fSJaegeuk Kim 			const struct f2fs_str *, struct f2fs_str *);
20616b3bd08fSJaegeuk Kim int f2fs_fname_usr_to_disk(struct inode *, const struct qstr *,
20626b3bd08fSJaegeuk Kim 			struct f2fs_str *);
20636b3bd08fSJaegeuk Kim 
206457e5055bSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
206557e5055bSJaegeuk Kim void f2fs_restore_and_release_control_page(struct page **);
206657e5055bSJaegeuk Kim void f2fs_restore_control_page(struct page *);
206757e5055bSJaegeuk Kim 
2068cfc4d971SJaegeuk Kim int __init f2fs_init_crypto(void);
2069cfc4d971SJaegeuk Kim int f2fs_crypto_initialize(void);
207057e5055bSJaegeuk Kim void f2fs_exit_crypto(void);
20710adda907SJaegeuk Kim 
20720adda907SJaegeuk Kim int f2fs_has_encryption_key(struct inode *);
20730adda907SJaegeuk Kim 
20740adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *inode)
20750adda907SJaegeuk Kim {
20760adda907SJaegeuk Kim 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
20770adda907SJaegeuk Kim 
20780adda907SJaegeuk Kim 	if (!ci ||
20790adda907SJaegeuk Kim 		(ci->ci_keyring_key &&
20800adda907SJaegeuk Kim 		 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
20810adda907SJaegeuk Kim 					       (1 << KEY_FLAG_REVOKED) |
20820adda907SJaegeuk Kim 					       (1 << KEY_FLAG_DEAD)))))
20830adda907SJaegeuk Kim 		return _f2fs_get_encryption_info(inode);
20840adda907SJaegeuk Kim 	return 0;
20850adda907SJaegeuk Kim }
20866b3bd08fSJaegeuk Kim 
20876b3bd08fSJaegeuk Kim void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
20886b3bd08fSJaegeuk Kim int f2fs_fname_setup_filename(struct inode *, const struct qstr *,
20896b3bd08fSJaegeuk Kim 				int lookup, struct f2fs_filename *);
20906b3bd08fSJaegeuk Kim void f2fs_fname_free_filename(struct f2fs_filename *);
209157e5055bSJaegeuk Kim #else
209257e5055bSJaegeuk Kim static inline void f2fs_restore_and_release_control_page(struct page **p) { }
209357e5055bSJaegeuk Kim static inline void f2fs_restore_control_page(struct page *p) { }
209457e5055bSJaegeuk Kim 
2095cfc4d971SJaegeuk Kim static inline int __init f2fs_init_crypto(void) { return 0; }
209657e5055bSJaegeuk Kim static inline void f2fs_exit_crypto(void) { }
20970adda907SJaegeuk Kim 
20980adda907SJaegeuk Kim static inline int f2fs_has_encryption_key(struct inode *i) { return 0; }
20990adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *i) { return 0; }
21006b3bd08fSJaegeuk Kim static inline void f2fs_fname_crypto_free_buffer(struct f2fs_str *p) { }
21016b3bd08fSJaegeuk Kim 
21026b3bd08fSJaegeuk Kim static inline int f2fs_fname_setup_filename(struct inode *dir,
21036b3bd08fSJaegeuk Kim 					const struct qstr *iname,
21046b3bd08fSJaegeuk Kim 					int lookup, struct f2fs_filename *fname)
21056b3bd08fSJaegeuk Kim {
21066b3bd08fSJaegeuk Kim 	memset(fname, 0, sizeof(struct f2fs_filename));
21076b3bd08fSJaegeuk Kim 	fname->usr_fname = iname;
21086b3bd08fSJaegeuk Kim 	fname->disk_name.name = (unsigned char *)iname->name;
21096b3bd08fSJaegeuk Kim 	fname->disk_name.len = iname->len;
21106b3bd08fSJaegeuk Kim 	return 0;
21116b3bd08fSJaegeuk Kim }
21126b3bd08fSJaegeuk Kim 
21136b3bd08fSJaegeuk Kim static inline void f2fs_fname_free_filename(struct f2fs_filename *fname) { }
211457e5055bSJaegeuk Kim #endif
211539a53e0cSJaegeuk Kim #endif
2116