xref: /openbmc/linux/fs/f2fs/f2fs.h (revision b439b103a6c9eb3417f34b4a609d4e00b4c59aca)
10a8165d7SJaegeuk Kim /*
239a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
339a53e0cSJaegeuk Kim  *
439a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
539a53e0cSJaegeuk Kim  *             http://www.samsung.com/
639a53e0cSJaegeuk Kim  *
739a53e0cSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
839a53e0cSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
939a53e0cSJaegeuk Kim  * published by the Free Software Foundation.
1039a53e0cSJaegeuk Kim  */
1139a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
1239a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1339a53e0cSJaegeuk Kim 
1439a53e0cSJaegeuk Kim #include <linux/types.h>
1539a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1639a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1739a53e0cSJaegeuk Kim #include <linux/slab.h>
1839a53e0cSJaegeuk Kim #include <linux/crc32.h>
1939a53e0cSJaegeuk Kim #include <linux/magic.h>
20c2d715d1SJaegeuk Kim #include <linux/kobject.h>
217bd59381SGu Zheng #include <linux/sched.h>
2239307a8eSJaegeuk Kim #include <linux/vmalloc.h>
23740432f8SJaegeuk Kim #include <linux/bio.h>
24d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
2539a53e0cSJaegeuk Kim 
265d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
279850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
280daaad97SJaegeuk Kim #define f2fs_down_write(x, y)	down_write_nest_lock(x, y)
295d56b671SJaegeuk Kim #else
309850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
319850cf4aSJaegeuk Kim 	do {								\
329850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
339850cf4aSJaegeuk Kim 			WARN_ON(1);					\
34caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
359850cf4aSJaegeuk Kim 		}							\
369850cf4aSJaegeuk Kim 	} while (0)
370daaad97SJaegeuk Kim #define f2fs_down_write(x, y)	down_write(x)
385d56b671SJaegeuk Kim #endif
395d56b671SJaegeuk Kim 
4039a53e0cSJaegeuk Kim /*
4139a53e0cSJaegeuk Kim  * For mount options
4239a53e0cSJaegeuk Kim  */
4339a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
4439a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
4539a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
4639a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
4739a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
4839a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
4939a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
50444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
511001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
5234d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
5334d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
5434d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
55d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
5689672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
576aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
58343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
5939a53e0cSJaegeuk Kim 
6039a53e0cSJaegeuk Kim #define clear_opt(sbi, option)	(sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
6139a53e0cSJaegeuk Kim #define set_opt(sbi, option)	(sbi->mount_opt.opt |= F2FS_MOUNT_##option)
6239a53e0cSJaegeuk Kim #define test_opt(sbi, option)	(sbi->mount_opt.opt & F2FS_MOUNT_##option)
6339a53e0cSJaegeuk Kim 
6439a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
6539a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
6639a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
6739a53e0cSJaegeuk Kim 
68a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
69a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
70a9841c4dSJaegeuk Kim 			 * address format, __le32.
71a9841c4dSJaegeuk Kim 			 */
7239a53e0cSJaegeuk Kim typedef u32 nid_t;
7339a53e0cSJaegeuk Kim 
7439a53e0cSJaegeuk Kim struct f2fs_mount_info {
7539a53e0cSJaegeuk Kim 	unsigned int	opt;
7639a53e0cSJaegeuk Kim };
7739a53e0cSJaegeuk Kim 
78cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT	0x0001
79cde4de12SJaegeuk Kim 
8076f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
8176f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
8276f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
8376f105a2SJaegeuk Kim 	F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
8476f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
8576f105a2SJaegeuk Kim 	F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
8676f105a2SJaegeuk Kim 
877e586fa0SJaegeuk Kim #define CRCPOLY_LE 0xedb88320
887e586fa0SJaegeuk Kim 
897e586fa0SJaegeuk Kim static inline __u32 f2fs_crc32(void *buf, size_t len)
9039a53e0cSJaegeuk Kim {
917e586fa0SJaegeuk Kim 	unsigned char *p = (unsigned char *)buf;
927e586fa0SJaegeuk Kim 	__u32 crc = F2FS_SUPER_MAGIC;
937e586fa0SJaegeuk Kim 	int i;
947e586fa0SJaegeuk Kim 
957e586fa0SJaegeuk Kim 	while (len--) {
967e586fa0SJaegeuk Kim 		crc ^= *p++;
977e586fa0SJaegeuk Kim 		for (i = 0; i < 8; i++)
987e586fa0SJaegeuk Kim 			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
997e586fa0SJaegeuk Kim 	}
1007e586fa0SJaegeuk Kim 	return crc;
10139a53e0cSJaegeuk Kim }
10239a53e0cSJaegeuk Kim 
1037e586fa0SJaegeuk Kim static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
10439a53e0cSJaegeuk Kim {
1057e586fa0SJaegeuk Kim 	return f2fs_crc32(buf, buf_size) == blk_crc;
10639a53e0cSJaegeuk Kim }
10739a53e0cSJaegeuk Kim 
10839a53e0cSJaegeuk Kim /*
10939a53e0cSJaegeuk Kim  * For checkpoint manager
11039a53e0cSJaegeuk Kim  */
11139a53e0cSJaegeuk Kim enum {
11239a53e0cSJaegeuk Kim 	NAT_BITMAP,
11339a53e0cSJaegeuk Kim 	SIT_BITMAP
11439a53e0cSJaegeuk Kim };
11539a53e0cSJaegeuk Kim 
11675ab4cb8SJaegeuk Kim enum {
11775ab4cb8SJaegeuk Kim 	CP_UMOUNT,
118119ee914SJaegeuk Kim 	CP_FASTBOOT,
11975ab4cb8SJaegeuk Kim 	CP_SYNC,
12010027551SJaegeuk Kim 	CP_RECOVERY,
1214b2fecc8SJaegeuk Kim 	CP_DISCARD,
12275ab4cb8SJaegeuk Kim };
12375ab4cb8SJaegeuk Kim 
124bba681cbSJaegeuk Kim #define DEF_BATCHED_TRIM_SECTIONS	32
125bba681cbSJaegeuk Kim #define BATCHED_TRIM_SEGMENTS(sbi)	\
126bba681cbSJaegeuk Kim 		(SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
127a66cdd98SJaegeuk Kim #define BATCHED_TRIM_BLOCKS(sbi)	\
128a66cdd98SJaegeuk Kim 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
12960b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
130d0239e1bSJaegeuk Kim #define DEF_IDLE_INTERVAL		120	/* 2 mins */
131bba681cbSJaegeuk Kim 
13275ab4cb8SJaegeuk Kim struct cp_control {
13375ab4cb8SJaegeuk Kim 	int reason;
1344b2fecc8SJaegeuk Kim 	__u64 trim_start;
1354b2fecc8SJaegeuk Kim 	__u64 trim_end;
1364b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
1374b2fecc8SJaegeuk Kim 	__u64 trimmed;
13875ab4cb8SJaegeuk Kim };
13975ab4cb8SJaegeuk Kim 
140662befdaSChao Yu /*
14181c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
142662befdaSChao Yu  */
143662befdaSChao Yu enum {
144662befdaSChao Yu 	META_CP,
145662befdaSChao Yu 	META_NAT,
14681c1a0f1SChao Yu 	META_SIT,
1474c521f49SJaegeuk Kim 	META_SSA,
1484c521f49SJaegeuk Kim 	META_POR,
149662befdaSChao Yu };
150662befdaSChao Yu 
1516451e041SJaegeuk Kim /* for the list of ino */
1526451e041SJaegeuk Kim enum {
1536451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
154fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
155fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
1566451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
1576451e041SJaegeuk Kim };
1586451e041SJaegeuk Kim 
1596451e041SJaegeuk Kim struct ino_entry {
16039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
16139a53e0cSJaegeuk Kim 	nid_t ino;		/* inode number */
16239a53e0cSJaegeuk Kim };
16339a53e0cSJaegeuk Kim 
1642710fd7eSChao Yu /* for the list of inodes to be GCed */
16506292073SChao Yu struct inode_entry {
16639a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
16739a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
16839a53e0cSJaegeuk Kim };
16939a53e0cSJaegeuk Kim 
1707fd9e544SJaegeuk Kim /* for the list of blockaddresses to be discarded */
1717fd9e544SJaegeuk Kim struct discard_entry {
1727fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
1737fd9e544SJaegeuk Kim 	block_t blkaddr;	/* block address to be discarded */
1747fd9e544SJaegeuk Kim 	int len;		/* # of consecutive blocks of the discard */
1757fd9e544SJaegeuk Kim };
1767fd9e544SJaegeuk Kim 
17739a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
17839a53e0cSJaegeuk Kim struct fsync_inode_entry {
17939a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
18039a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
181c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
182c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
183c52e1b10SJaegeuk Kim 	block_t last_inode;	/* block address locating the last inode */
18439a53e0cSJaegeuk Kim };
18539a53e0cSJaegeuk Kim 
18639a53e0cSJaegeuk Kim #define nats_in_cursum(sum)		(le16_to_cpu(sum->n_nats))
18739a53e0cSJaegeuk Kim #define sits_in_cursum(sum)		(le16_to_cpu(sum->n_sits))
18839a53e0cSJaegeuk Kim 
18939a53e0cSJaegeuk Kim #define nat_in_journal(sum, i)		(sum->nat_j.entries[i].ne)
19039a53e0cSJaegeuk Kim #define nid_in_journal(sum, i)		(sum->nat_j.entries[i].nid)
19139a53e0cSJaegeuk Kim #define sit_in_journal(sum, i)		(sum->sit_j.entries[i].se)
19239a53e0cSJaegeuk Kim #define segno_in_journal(sum, i)	(sum->sit_j.entries[i].segno)
19339a53e0cSJaegeuk Kim 
194309cc2b6SJaegeuk Kim #define MAX_NAT_JENTRIES(sum)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
195309cc2b6SJaegeuk Kim #define MAX_SIT_JENTRIES(sum)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
196309cc2b6SJaegeuk Kim 
19739a53e0cSJaegeuk Kim static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
19839a53e0cSJaegeuk Kim {
19939a53e0cSJaegeuk Kim 	int before = nats_in_cursum(rs);
20039a53e0cSJaegeuk Kim 	rs->n_nats = cpu_to_le16(before + i);
20139a53e0cSJaegeuk Kim 	return before;
20239a53e0cSJaegeuk Kim }
20339a53e0cSJaegeuk Kim 
20439a53e0cSJaegeuk Kim static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
20539a53e0cSJaegeuk Kim {
20639a53e0cSJaegeuk Kim 	int before = sits_in_cursum(rs);
20739a53e0cSJaegeuk Kim 	rs->n_sits = cpu_to_le16(before + i);
20839a53e0cSJaegeuk Kim 	return before;
20939a53e0cSJaegeuk Kim }
21039a53e0cSJaegeuk Kim 
211184a5cd2SChao Yu static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
212184a5cd2SChao Yu 								int type)
213184a5cd2SChao Yu {
214184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
215309cc2b6SJaegeuk Kim 		return size <= MAX_NAT_JENTRIES(sum);
216309cc2b6SJaegeuk Kim 	return size <= MAX_SIT_JENTRIES(sum);
217184a5cd2SChao Yu }
218184a5cd2SChao Yu 
21939a53e0cSJaegeuk Kim /*
220e9750824SNamjae Jeon  * ioctl commands
221e9750824SNamjae Jeon  */
222e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
223e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
224d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
225e9750824SNamjae Jeon 
22688b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
22788b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
22888b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
22902a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
2301e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
2311e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
232c1c1b583SChao Yu #define F2FS_IOC_GARBAGE_COLLECT	_IO(F2FS_IOCTL_MAGIC, 6)
233456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
234d323d005SChao Yu #define F2FS_IOC_DEFRAGMENT		_IO(F2FS_IOCTL_MAGIC, 8)
23588b88a66SJaegeuk Kim 
236f424f664SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY					\
237f424f664SJaegeuk Kim 		_IOR('f', 19, struct f2fs_encryption_policy)
238f424f664SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT					\
239f424f664SJaegeuk Kim 		_IOW('f', 20, __u8[16])
240f424f664SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY					\
241f424f664SJaegeuk Kim 		_IOW('f', 21, struct f2fs_encryption_policy)
242f424f664SJaegeuk Kim 
2431abff93dSJaegeuk Kim /*
2441abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
2451abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
2461abff93dSJaegeuk Kim  */
2471abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
2481abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
2491abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
2501abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
251c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
2521abff93dSJaegeuk Kim 
253e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
254e9750824SNamjae Jeon /*
255e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
256e9750824SNamjae Jeon  */
257e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
258e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
25904ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
260e9750824SNamjae Jeon #endif
261e9750824SNamjae Jeon 
262d323d005SChao Yu struct f2fs_defragment {
263d323d005SChao Yu 	u64 start;
264d323d005SChao Yu 	u64 len;
265d323d005SChao Yu };
266d323d005SChao Yu 
267e9750824SNamjae Jeon /*
26839a53e0cSJaegeuk Kim  * For INODE and NODE manager
26939a53e0cSJaegeuk Kim  */
2707b3cd7d6SJaegeuk Kim /* for directory operations */
2716b3bd08fSJaegeuk Kim struct f2fs_str {
2726b3bd08fSJaegeuk Kim 	unsigned char *name;
2736b3bd08fSJaegeuk Kim 	u32 len;
2746b3bd08fSJaegeuk Kim };
2756b3bd08fSJaegeuk Kim 
2766b3bd08fSJaegeuk Kim struct f2fs_filename {
2776b3bd08fSJaegeuk Kim 	const struct qstr *usr_fname;
2786b3bd08fSJaegeuk Kim 	struct f2fs_str disk_name;
2796b3bd08fSJaegeuk Kim 	f2fs_hash_t hash;
2806b3bd08fSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2816b3bd08fSJaegeuk Kim 	struct f2fs_str crypto_buf;
2826b3bd08fSJaegeuk Kim #endif
2836b3bd08fSJaegeuk Kim };
2846b3bd08fSJaegeuk Kim 
2856b3bd08fSJaegeuk Kim #define FSTR_INIT(n, l)		{ .name = n, .len = l }
2866b3bd08fSJaegeuk Kim #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
2876b3bd08fSJaegeuk Kim #define fname_name(p)		((p)->disk_name.name)
2886b3bd08fSJaegeuk Kim #define fname_len(p)		((p)->disk_name.len)
2896b3bd08fSJaegeuk Kim 
2907b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
291d8c6822aSJaegeuk Kim 	struct inode *inode;
2927b3cd7d6SJaegeuk Kim 	const void *bitmap;
2937b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
2947b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
2957b3cd7d6SJaegeuk Kim 	int max;
2967b3cd7d6SJaegeuk Kim };
2977b3cd7d6SJaegeuk Kim 
298d8c6822aSJaegeuk Kim static inline void make_dentry_ptr(struct inode *inode,
299d8c6822aSJaegeuk Kim 		struct f2fs_dentry_ptr *d, void *src, int type)
3007b3cd7d6SJaegeuk Kim {
301d8c6822aSJaegeuk Kim 	d->inode = inode;
302d8c6822aSJaegeuk Kim 
3037b3cd7d6SJaegeuk Kim 	if (type == 1) {
3047b3cd7d6SJaegeuk Kim 		struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
3057b3cd7d6SJaegeuk Kim 		d->max = NR_DENTRY_IN_BLOCK;
3067b3cd7d6SJaegeuk Kim 		d->bitmap = &t->dentry_bitmap;
3077b3cd7d6SJaegeuk Kim 		d->dentry = t->dentry;
3087b3cd7d6SJaegeuk Kim 		d->filename = t->filename;
3097b3cd7d6SJaegeuk Kim 	} else {
3107b3cd7d6SJaegeuk Kim 		struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
3117b3cd7d6SJaegeuk Kim 		d->max = NR_INLINE_DENTRY;
3127b3cd7d6SJaegeuk Kim 		d->bitmap = &t->dentry_bitmap;
3137b3cd7d6SJaegeuk Kim 		d->dentry = t->dentry;
3147b3cd7d6SJaegeuk Kim 		d->filename = t->filename;
3157b3cd7d6SJaegeuk Kim 	}
3167b3cd7d6SJaegeuk Kim }
3177b3cd7d6SJaegeuk Kim 
318dbe6a5ffSJaegeuk Kim /*
319dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
320dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
321dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
32239a53e0cSJaegeuk Kim  */
323dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
324dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
325266e97a8SJaegeuk Kim enum {
326266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
327266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
328266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
329266e97a8SJaegeuk Kim 					 * look up a node with readahead called
3304f4124d0SChao Yu 					 * by get_data_block.
33139a53e0cSJaegeuk Kim 					 */
332266e97a8SJaegeuk Kim };
333266e97a8SJaegeuk Kim 
334a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
33539a53e0cSJaegeuk Kim 
336817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
337817202d9SChao Yu 
33813054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
33913054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
34013054c54SChao Yu 
34139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
34213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
34313054c54SChao Yu 
34413054c54SChao Yu /* number of extent info in extent cache we try to shrink */
34513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
346c11abd1aSJaegeuk Kim 
34739a53e0cSJaegeuk Kim struct extent_info {
34839a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
3494d0b0bd4SChao Yu 	u32 blk;			/* start block address of the extent */
350111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
35139a53e0cSJaegeuk Kim };
35239a53e0cSJaegeuk Kim 
35313054c54SChao Yu struct extent_node {
35413054c54SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
35513054c54SChao Yu 	struct list_head list;		/* node in global extent list of sbi */
35613054c54SChao Yu 	struct extent_info ei;		/* extent info */
357201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
35813054c54SChao Yu };
35913054c54SChao Yu 
36013054c54SChao Yu struct extent_tree {
36113054c54SChao Yu 	nid_t ino;			/* inode number */
36213054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
36362c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
3643e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
365137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
36613054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
36768e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
36813054c54SChao Yu };
36913054c54SChao Yu 
37039a53e0cSJaegeuk Kim /*
371003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
372003a3e1dSJaegeuk Kim  *
373003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
374003a3e1dSJaegeuk Kim  */
375003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
376003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
3777f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
3787f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
3797f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
380003a3e1dSJaegeuk Kim 
381003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
382003a3e1dSJaegeuk Kim 	block_t m_pblk;
383003a3e1dSJaegeuk Kim 	block_t m_lblk;
384003a3e1dSJaegeuk Kim 	unsigned int m_len;
385003a3e1dSJaegeuk Kim 	unsigned int m_flags;
386da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
387003a3e1dSJaegeuk Kim };
388003a3e1dSJaegeuk Kim 
389e2b4e2bcSChao Yu /* for flag in get_data_block */
390e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_READ		0
391e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_DIO		1
392e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_FIEMAP		2
393e2b4e2bcSChao Yu #define F2FS_GET_BLOCK_BMAP		3
394*b439b103SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_DIO		4
395e2b4e2bcSChao Yu 
396003a3e1dSJaegeuk Kim /*
39739a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
39839a53e0cSJaegeuk Kim  */
39939a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
400354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
401cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
402e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
40339a53e0cSJaegeuk Kim 
404b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
405b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
406b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
407b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
408b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
409b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
410cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
411cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
412cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
413e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
414e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
415cde4de12SJaegeuk Kim 
416cde4de12SJaegeuk Kim /* Encryption algorithms */
417cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_INVALID		0
418cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_XTS	1
419cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_GCM	2
420cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CBC	3
421cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CTS	4
422b5492af7SJaegeuk Kim 
423f424f664SJaegeuk Kim #include "f2fs_crypto.h"
424f424f664SJaegeuk Kim 
425ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
426ab9fa662SJaegeuk Kim 
42739a53e0cSJaegeuk Kim struct f2fs_inode_info {
42839a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
42939a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
43039a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
43138431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
43239a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
4336666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
43439a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
43539a53e0cSJaegeuk Kim 
43639a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
43739a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
438d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
439a7ffdbe2SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
44039a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
44139a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
44239a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
443e518ff81SJaegeuk Kim 	unsigned long long xattr_ver;	/* cp version of xattr modification */
44488b88a66SJaegeuk Kim 
4452710fd7eSChao Yu 	struct list_head dirty_list;	/* linked in global dirty list */
44688b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
44788b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
448cde4de12SJaegeuk Kim 
4493e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
4503e72f721SJaegeuk Kim 
451cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
452cde4de12SJaegeuk Kim 	/* Encryption params */
453cde4de12SJaegeuk Kim 	struct f2fs_crypt_info *i_crypt_info;
454cde4de12SJaegeuk Kim #endif
45539a53e0cSJaegeuk Kim };
45639a53e0cSJaegeuk Kim 
45739a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
45839a53e0cSJaegeuk Kim 					struct f2fs_extent i_ext)
45939a53e0cSJaegeuk Kim {
46039a53e0cSJaegeuk Kim 	ext->fofs = le32_to_cpu(i_ext.fofs);
4614d0b0bd4SChao Yu 	ext->blk = le32_to_cpu(i_ext.blk);
46239a53e0cSJaegeuk Kim 	ext->len = le32_to_cpu(i_ext.len);
46339a53e0cSJaegeuk Kim }
46439a53e0cSJaegeuk Kim 
46539a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
46639a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
46739a53e0cSJaegeuk Kim {
46839a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
4694d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
47039a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
47139a53e0cSJaegeuk Kim }
47239a53e0cSJaegeuk Kim 
473429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
474429511cdSChao Yu 						u32 blk, unsigned int len)
475429511cdSChao Yu {
476429511cdSChao Yu 	ei->fofs = fofs;
477429511cdSChao Yu 	ei->blk = blk;
478429511cdSChao Yu 	ei->len = len;
479429511cdSChao Yu }
480429511cdSChao Yu 
4810bdee482SChao Yu static inline bool __is_extent_same(struct extent_info *ei1,
4820bdee482SChao Yu 						struct extent_info *ei2)
4830bdee482SChao Yu {
4840bdee482SChao Yu 	return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
4850bdee482SChao Yu 						ei1->len == ei2->len);
4860bdee482SChao Yu }
4870bdee482SChao Yu 
488429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
489429511cdSChao Yu 						struct extent_info *front)
490429511cdSChao Yu {
491429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
492429511cdSChao Yu 			back->blk + back->len == front->blk);
493429511cdSChao Yu }
494429511cdSChao Yu 
495429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
496429511cdSChao Yu 						struct extent_info *back)
497429511cdSChao Yu {
498429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
499429511cdSChao Yu }
500429511cdSChao Yu 
501429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
502429511cdSChao Yu 						struct extent_info *front)
503429511cdSChao Yu {
504429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
505429511cdSChao Yu }
506429511cdSChao Yu 
5074abd3f5aSChao Yu static inline void __try_update_largest_extent(struct extent_tree *et,
5084abd3f5aSChao Yu 						struct extent_node *en)
5094abd3f5aSChao Yu {
5104abd3f5aSChao Yu 	if (en->ei.len > et->largest.len)
5114abd3f5aSChao Yu 		et->largest = en->ei;
5124abd3f5aSChao Yu }
5134abd3f5aSChao Yu 
51439a53e0cSJaegeuk Kim struct f2fs_nm_info {
51539a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
51639a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
5177ee0eeabSJaegeuk Kim 	nid_t available_nids;		/* maximum available node ids */
51839a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
519cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
520ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
5212304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
52239a53e0cSJaegeuk Kim 
52339a53e0cSJaegeuk Kim 	/* NAT cache management */
52439a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
525309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
5268b26ef98SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
52739a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
528309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
529aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
53039a53e0cSJaegeuk Kim 
53139a53e0cSJaegeuk Kim 	/* free node ids management */
5328a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
53339a53e0cSJaegeuk Kim 	struct list_head free_nid_list;	/* a list for free nids */
53439a53e0cSJaegeuk Kim 	spinlock_t free_nid_list_lock;	/* protect free nid list */
53539a53e0cSJaegeuk Kim 	unsigned int fcnt;		/* the number of free node id */
53639a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
53739a53e0cSJaegeuk Kim 
53839a53e0cSJaegeuk Kim 	/* for checkpoint */
53939a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
54039a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
54139a53e0cSJaegeuk Kim };
54239a53e0cSJaegeuk Kim 
54339a53e0cSJaegeuk Kim /*
54439a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
54539a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
54639a53e0cSJaegeuk Kim  * by the data offset in a file.
54739a53e0cSJaegeuk Kim  */
54839a53e0cSJaegeuk Kim struct dnode_of_data {
54939a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
55039a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
55139a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
55239a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
55339a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
55439a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
55593bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
5563cf45747SChao Yu 	char cur_level;			/* level of hole node page */
5573cf45747SChao Yu 	char max_level;			/* level of current page located */
55839a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
55939a53e0cSJaegeuk Kim };
56039a53e0cSJaegeuk Kim 
56139a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
56239a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
56339a53e0cSJaegeuk Kim {
564d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
56539a53e0cSJaegeuk Kim 	dn->inode = inode;
56639a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
56739a53e0cSJaegeuk Kim 	dn->node_page = npage;
56839a53e0cSJaegeuk Kim 	dn->nid = nid;
56939a53e0cSJaegeuk Kim }
57039a53e0cSJaegeuk Kim 
57139a53e0cSJaegeuk Kim /*
57239a53e0cSJaegeuk Kim  * For SIT manager
57339a53e0cSJaegeuk Kim  *
57439a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
57539a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
57639a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
57739a53e0cSJaegeuk Kim  * respectively.
57839a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
57939a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
58039a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
58139a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
58239a53e0cSJaegeuk Kim  * data and 8 for node logs.
58339a53e0cSJaegeuk Kim  */
58439a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
58539a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
58639a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
58739a53e0cSJaegeuk Kim 
58839a53e0cSJaegeuk Kim enum {
58939a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
59039a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
59139a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
59239a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
59339a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
59439a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
59538aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
59638aa0889SJaegeuk Kim 	CURSEG_DIRECT_IO,	/* to use for the direct IO path */
59739a53e0cSJaegeuk Kim };
59839a53e0cSJaegeuk Kim 
5996b4afdd7SJaegeuk Kim struct flush_cmd {
6006b4afdd7SJaegeuk Kim 	struct completion wait;
601721bd4d5SGu Zheng 	struct llist_node llnode;
6026b4afdd7SJaegeuk Kim 	int ret;
6036b4afdd7SJaegeuk Kim };
6046b4afdd7SJaegeuk Kim 
605a688b9d9SGu Zheng struct flush_cmd_control {
606a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
607a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
608721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
609721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
610a688b9d9SGu Zheng };
611a688b9d9SGu Zheng 
61239a53e0cSJaegeuk Kim struct f2fs_sm_info {
61339a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
61439a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
61539a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
61639a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
61739a53e0cSJaegeuk Kim 
61839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
61939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
62039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
62139a53e0cSJaegeuk Kim 
62239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
62339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
62439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
62539a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
62681eb8d6eSJaegeuk Kim 
62781eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
62881eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
6297fd9e544SJaegeuk Kim 
6307fd9e544SJaegeuk Kim 	/* for small discard management */
6317fd9e544SJaegeuk Kim 	struct list_head discard_list;		/* 4KB discard list */
6327fd9e544SJaegeuk Kim 	int nr_discards;			/* # of discards in the list */
6337fd9e544SJaegeuk Kim 	int max_discards;			/* max. discards to be issued */
634216fbd64SJaegeuk Kim 
635bba681cbSJaegeuk Kim 	/* for batched trimming */
636bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
637bba681cbSJaegeuk Kim 
638184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
639184a5cd2SChao Yu 
640216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
641216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
642c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
6436b4afdd7SJaegeuk Kim 
6446b4afdd7SJaegeuk Kim 	/* for flush command control */
645a688b9d9SGu Zheng 	struct flush_cmd_control *cmd_control_info;
646a688b9d9SGu Zheng 
64739a53e0cSJaegeuk Kim };
64839a53e0cSJaegeuk Kim 
64939a53e0cSJaegeuk Kim /*
65039a53e0cSJaegeuk Kim  * For superblock
65139a53e0cSJaegeuk Kim  */
65239a53e0cSJaegeuk Kim /*
65339a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
65439a53e0cSJaegeuk Kim  *
65539a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
65639a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
65739a53e0cSJaegeuk Kim  */
65839a53e0cSJaegeuk Kim enum count_type {
65939a53e0cSJaegeuk Kim 	F2FS_WRITEBACK,
66039a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
661c227f912SChao Yu 	F2FS_DIRTY_DATA,
66239a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
66339a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
6648dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
66539a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
66639a53e0cSJaegeuk Kim };
66739a53e0cSJaegeuk Kim 
66839a53e0cSJaegeuk Kim /*
669e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
67039a53e0cSJaegeuk Kim  * The available types are:
67139a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
67239a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
67339a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
67439a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
67539a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
67639a53e0cSJaegeuk Kim  *			with waiting the bio's completion
67739a53e0cSJaegeuk Kim  * ...			Only can be used with META.
67839a53e0cSJaegeuk Kim  */
6797d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
68039a53e0cSJaegeuk Kim enum page_type {
68139a53e0cSJaegeuk Kim 	DATA,
68239a53e0cSJaegeuk Kim 	NODE,
68339a53e0cSJaegeuk Kim 	META,
68439a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
68539a53e0cSJaegeuk Kim 	META_FLUSH,
6868ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
6878ce67cb0SJaegeuk Kim 	INMEM_DROP,
6888ce67cb0SJaegeuk Kim 	IPU,
6898ce67cb0SJaegeuk Kim 	OPU,
69039a53e0cSJaegeuk Kim };
69139a53e0cSJaegeuk Kim 
692458e6197SJaegeuk Kim struct f2fs_io_info {
69305ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
694458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
6957e8f2308SGu Zheng 	int rw;			/* contains R/RS/W/WS with REQ_META/REQ_PRIO */
696cf04e8ebSJaegeuk Kim 	block_t blk_addr;	/* block address to be written */
69705ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
6984375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
699458e6197SJaegeuk Kim };
700458e6197SJaegeuk Kim 
70193dfe2acSJaegeuk Kim #define is_read_io(rw)	(((rw) & 1) == READ)
7021ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
703458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
7041ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
7051ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
706458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
707df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
7081ff7bd3bSJaegeuk Kim };
7091ff7bd3bSJaegeuk Kim 
710c227f912SChao Yu enum inode_type {
711c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
712c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
713c227f912SChao Yu 	NR_INODE_TYPE,
714c227f912SChao Yu };
715c227f912SChao Yu 
71667298804SChao Yu /* for inner inode cache management */
71767298804SChao Yu struct inode_management {
71867298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
71967298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
72067298804SChao Yu 	struct list_head ino_list;		/* inode list head */
72167298804SChao Yu 	unsigned long ino_num;			/* number of entries */
72267298804SChao Yu };
72367298804SChao Yu 
724caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
725caf0047eSChao Yu enum {
726caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
727caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
728caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
729caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
730caf0047eSChao Yu };
731caf0047eSChao Yu 
7326beceb54SJaegeuk Kim enum {
7336beceb54SJaegeuk Kim 	CP_TIME,
734d0239e1bSJaegeuk Kim 	REQ_TIME,
7356beceb54SJaegeuk Kim 	MAX_TIME,
7366beceb54SJaegeuk Kim };
7376beceb54SJaegeuk Kim 
73839a53e0cSJaegeuk Kim struct f2fs_sb_info {
73939a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
7405e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
74139a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
742e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
743caf0047eSChao Yu 	int s_flag;				/* flags for sbi */
74439a53e0cSJaegeuk Kim 
74539a53e0cSJaegeuk Kim 	/* for node-related operations */
74639a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
74739a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
74839a53e0cSJaegeuk Kim 
74939a53e0cSJaegeuk Kim 	/* for segment-related operations */
75039a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
7511ff7bd3bSJaegeuk Kim 
7521ff7bd3bSJaegeuk Kim 	/* for bio operations */
753924b720bSChao Yu 	struct f2fs_bio_info read_io;			/* for read bios */
7541ff7bd3bSJaegeuk Kim 	struct f2fs_bio_info write_io[NR_PAGE_TYPE];	/* for write bios */
75539a53e0cSJaegeuk Kim 
75639a53e0cSJaegeuk Kim 	/* for checkpoint */
75739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
75839a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
75939936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
760e479556bSGu Zheng 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
761b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
7625463e7c1SJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
763fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
7646beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
7656beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
76639a53e0cSJaegeuk Kim 
76767298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
7686451e041SJaegeuk Kim 
7696451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
7700d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
77139a53e0cSJaegeuk Kim 
772c227f912SChao Yu 	/* for inode management */
773c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
774c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
77539a53e0cSJaegeuk Kim 
77613054c54SChao Yu 	/* for extent tree cache */
77713054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
77813054c54SChao Yu 	struct rw_semaphore extent_tree_lock;	/* locking extent radix tree */
77913054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
78013054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
7817441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
782137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
78374fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
78413054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
78513054c54SChao Yu 
78639a53e0cSJaegeuk Kim 	/* basic filesystem units */
78739a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
78839a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
78939a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
79039a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
79139a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
79239a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
79339a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
79439a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
79539a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
79639a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
79739a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
79839a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
79939a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
80039a53e0cSJaegeuk Kim 	unsigned int total_valid_inode_count;	/* valid inode count */
801e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
80239a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
803ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
80439a53e0cSJaegeuk Kim 
80539a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
80639a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
80739a53e0cSJaegeuk Kim 	block_t alloc_valid_block_count;	/* # of allocated blocks */
808a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
80939a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
81039a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
81139a53e0cSJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];	/* # of pages, see count_type */
81239a53e0cSJaegeuk Kim 
81339a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
81439a53e0cSJaegeuk Kim 
81539a53e0cSJaegeuk Kim 	/* for cleaning operations */
81639a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
81739a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
8185ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
81939a53e0cSJaegeuk Kim 
820b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
821b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
822b1c57c1cSJaegeuk Kim 
82339a53e0cSJaegeuk Kim 	/*
82439a53e0cSJaegeuk Kim 	 * for stat information.
82539a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
82639a53e0cSJaegeuk Kim 	 */
82735b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
82839a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
82939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
83039a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
831b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
8325b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
8335b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
8345b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
8355b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
836d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
83703e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
83803e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
83939a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
84033fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
84135b09d82SNamjae Jeon #endif
84235b09d82SNamjae Jeon 	unsigned int last_victim[2];		/* last victim segment # */
84339a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
844b59d0baeSNamjae Jeon 
845b59d0baeSNamjae Jeon 	/* For sysfs suppport */
846b59d0baeSNamjae Jeon 	struct kobject s_kobj;
847b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
8482658e50dSJaegeuk Kim 
8492658e50dSJaegeuk Kim 	/* For shrinker support */
8502658e50dSJaegeuk Kim 	struct list_head s_list;
8512658e50dSJaegeuk Kim 	struct mutex umount_mutex;
8522658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
8538f1dbbbbSShuoran Liu 
8548f1dbbbbSShuoran Liu 	/* For write statistics */
8558f1dbbbbSShuoran Liu 	u64 sectors_written_start;
8568f1dbbbbSShuoran Liu 	u64 kbytes_written;
85739a53e0cSJaegeuk Kim };
85839a53e0cSJaegeuk Kim 
8598f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
8608f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
8618f1dbbbbSShuoran Liu  */
8628f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
8638f1dbbbbSShuoran Liu (((u64)part_stat_read(s->sb->s_bdev->bd_part, sectors[1]) -		 \
8648f1dbbbbSShuoran Liu 		s->sectors_written_start) >> 1)
8658f1dbbbbSShuoran Liu 
8666beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
8676beceb54SJaegeuk Kim {
8686beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
8696beceb54SJaegeuk Kim }
8706beceb54SJaegeuk Kim 
8716beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
8726beceb54SJaegeuk Kim {
8736beceb54SJaegeuk Kim 	struct timespec ts = {sbi->interval_time[type], 0};
8746beceb54SJaegeuk Kim 	unsigned long interval = timespec_to_jiffies(&ts);
8756beceb54SJaegeuk Kim 
8766beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
8776beceb54SJaegeuk Kim }
8786beceb54SJaegeuk Kim 
879d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
880d0239e1bSJaegeuk Kim {
881d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
882d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
883d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
884d0239e1bSJaegeuk Kim 
885d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
886d0239e1bSJaegeuk Kim 		return 0;
887d0239e1bSJaegeuk Kim 
888d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
889d0239e1bSJaegeuk Kim }
890d0239e1bSJaegeuk Kim 
89139a53e0cSJaegeuk Kim /*
89239a53e0cSJaegeuk Kim  * Inline functions
89339a53e0cSJaegeuk Kim  */
89439a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
89539a53e0cSJaegeuk Kim {
89639a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
89739a53e0cSJaegeuk Kim }
89839a53e0cSJaegeuk Kim 
89939a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
90039a53e0cSJaegeuk Kim {
90139a53e0cSJaegeuk Kim 	return sb->s_fs_info;
90239a53e0cSJaegeuk Kim }
90339a53e0cSJaegeuk Kim 
9044081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
9054081363fSJaegeuk Kim {
9064081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
9074081363fSJaegeuk Kim }
9084081363fSJaegeuk Kim 
9094081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
9104081363fSJaegeuk Kim {
9114081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
9124081363fSJaegeuk Kim }
9134081363fSJaegeuk Kim 
9144081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
9154081363fSJaegeuk Kim {
9164081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
9174081363fSJaegeuk Kim }
9184081363fSJaegeuk Kim 
91939a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
92039a53e0cSJaegeuk Kim {
92139a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
92239a53e0cSJaegeuk Kim }
92339a53e0cSJaegeuk Kim 
92439a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
92539a53e0cSJaegeuk Kim {
92639a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
92739a53e0cSJaegeuk Kim }
92839a53e0cSJaegeuk Kim 
92945590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
93045590710SGu Zheng {
93145590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
93245590710SGu Zheng }
93345590710SGu Zheng 
93458bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
93558bfaf44SJaegeuk Kim {
93658bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
93758bfaf44SJaegeuk Kim }
93858bfaf44SJaegeuk Kim 
93939a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
94039a53e0cSJaegeuk Kim {
94139a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
94239a53e0cSJaegeuk Kim }
94339a53e0cSJaegeuk Kim 
94439a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
94539a53e0cSJaegeuk Kim {
94639a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
94739a53e0cSJaegeuk Kim }
94839a53e0cSJaegeuk Kim 
94939a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
95039a53e0cSJaegeuk Kim {
95139a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
95239a53e0cSJaegeuk Kim }
95339a53e0cSJaegeuk Kim 
95439a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
95539a53e0cSJaegeuk Kim {
95639a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
95739a53e0cSJaegeuk Kim }
95839a53e0cSJaegeuk Kim 
95939a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
96039a53e0cSJaegeuk Kim {
96139a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
96239a53e0cSJaegeuk Kim }
96339a53e0cSJaegeuk Kim 
9649df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
9659df27d98SGu Zheng {
9669df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
9679df27d98SGu Zheng }
9689df27d98SGu Zheng 
9694ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
9704ef51a8fSJaegeuk Kim {
9714ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
9724ef51a8fSJaegeuk Kim }
9734ef51a8fSJaegeuk Kim 
974caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
97539a53e0cSJaegeuk Kim {
976caf0047eSChao Yu 	return sbi->s_flag & (0x01 << type);
97739a53e0cSJaegeuk Kim }
97839a53e0cSJaegeuk Kim 
979caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
98039a53e0cSJaegeuk Kim {
981caf0047eSChao Yu 	sbi->s_flag |= (0x01 << type);
982caf0047eSChao Yu }
983caf0047eSChao Yu 
984caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
985caf0047eSChao Yu {
986caf0047eSChao Yu 	sbi->s_flag &= ~(0x01 << type);
98739a53e0cSJaegeuk Kim }
98839a53e0cSJaegeuk Kim 
989d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
990d71b5564SJaegeuk Kim {
991d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
992d71b5564SJaegeuk Kim }
993d71b5564SJaegeuk Kim 
99425ca923bSJaegeuk Kim static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
99525ca923bSJaegeuk Kim {
99625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
99725ca923bSJaegeuk Kim 	return ckpt_flags & f;
99825ca923bSJaegeuk Kim }
99925ca923bSJaegeuk Kim 
100025ca923bSJaegeuk Kim static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
100125ca923bSJaegeuk Kim {
100225ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
100325ca923bSJaegeuk Kim 	ckpt_flags |= f;
100425ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
100525ca923bSJaegeuk Kim }
100625ca923bSJaegeuk Kim 
100725ca923bSJaegeuk Kim static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
100825ca923bSJaegeuk Kim {
100925ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
101025ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
101125ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
101225ca923bSJaegeuk Kim }
101325ca923bSJaegeuk Kim 
1014e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
101539a53e0cSJaegeuk Kim {
1016e479556bSGu Zheng 	down_read(&sbi->cp_rwsem);
101739a53e0cSJaegeuk Kim }
101839a53e0cSJaegeuk Kim 
1019e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
102039a53e0cSJaegeuk Kim {
1021e479556bSGu Zheng 	up_read(&sbi->cp_rwsem);
102239936837SJaegeuk Kim }
102339936837SJaegeuk Kim 
1024e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
102539936837SJaegeuk Kim {
10260daaad97SJaegeuk Kim 	f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
102739936837SJaegeuk Kim }
102839936837SJaegeuk Kim 
1029e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
103039936837SJaegeuk Kim {
1031e479556bSGu Zheng 	up_write(&sbi->cp_rwsem);
103239a53e0cSJaegeuk Kim }
103339a53e0cSJaegeuk Kim 
1034119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1035119ee914SJaegeuk Kim {
1036119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1037119ee914SJaegeuk Kim 
1038119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1039119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1040119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1041119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1042119ee914SJaegeuk Kim 	return reason;
1043119ee914SJaegeuk Kim }
1044119ee914SJaegeuk Kim 
1045119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1046119ee914SJaegeuk Kim {
1047119ee914SJaegeuk Kim 	return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
1048119ee914SJaegeuk Kim }
1049119ee914SJaegeuk Kim 
1050119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1051119ee914SJaegeuk Kim {
1052119ee914SJaegeuk Kim 	return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
1053119ee914SJaegeuk Kim 			is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
1054119ee914SJaegeuk Kim }
1055119ee914SJaegeuk Kim 
105639a53e0cSJaegeuk Kim /*
105739a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
105839a53e0cSJaegeuk Kim  */
1059064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
106039a53e0cSJaegeuk Kim {
1061d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1062d6b7d4b3SChao Yu 		return -EINVAL;
1063cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1064064e0823SNamjae Jeon 		return -EINVAL;
1065064e0823SNamjae Jeon 	return 0;
106639a53e0cSJaegeuk Kim }
106739a53e0cSJaegeuk Kim 
106839a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
106939a53e0cSJaegeuk Kim 
107039a53e0cSJaegeuk Kim /*
107139a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
107239a53e0cSJaegeuk Kim  */
107339a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
107439a53e0cSJaegeuk Kim {
107539a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
10766c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
107739a53e0cSJaegeuk Kim 	else
10786c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
107939a53e0cSJaegeuk Kim }
108039a53e0cSJaegeuk Kim 
10814bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
10824bc8e9bcSChao Yu {
10834bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
10844bc8e9bcSChao Yu }
10854bc8e9bcSChao Yu 
108639a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
108739a53e0cSJaegeuk Kim 				 struct inode *inode, blkcnt_t count)
108839a53e0cSJaegeuk Kim {
108939a53e0cSJaegeuk Kim 	block_t	valid_block_count;
109039a53e0cSJaegeuk Kim 
109139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
109239a53e0cSJaegeuk Kim 	valid_block_count =
109339a53e0cSJaegeuk Kim 		sbi->total_valid_block_count + (block_t)count;
1094cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
109539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
109639a53e0cSJaegeuk Kim 		return false;
109739a53e0cSJaegeuk Kim 	}
109839a53e0cSJaegeuk Kim 	inode->i_blocks += count;
109939a53e0cSJaegeuk Kim 	sbi->total_valid_block_count = valid_block_count;
110039a53e0cSJaegeuk Kim 	sbi->alloc_valid_block_count += (block_t)count;
110139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
110239a53e0cSJaegeuk Kim 	return true;
110339a53e0cSJaegeuk Kim }
110439a53e0cSJaegeuk Kim 
1105da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
110639a53e0cSJaegeuk Kim 						struct inode *inode,
110739a53e0cSJaegeuk Kim 						blkcnt_t count)
110839a53e0cSJaegeuk Kim {
110939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
11109850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
11119850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, inode->i_blocks < count);
111239a53e0cSJaegeuk Kim 	inode->i_blocks -= count;
111339a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
111439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
111539a53e0cSJaegeuk Kim }
111639a53e0cSJaegeuk Kim 
111739a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
111839a53e0cSJaegeuk Kim {
111939a53e0cSJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
1120caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
112139a53e0cSJaegeuk Kim }
112239a53e0cSJaegeuk Kim 
1123a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
112439a53e0cSJaegeuk Kim {
1125a7ffdbe2SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1126c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1127c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
112839a53e0cSJaegeuk Kim }
112939a53e0cSJaegeuk Kim 
113039a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
113139a53e0cSJaegeuk Kim {
113239a53e0cSJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
113339a53e0cSJaegeuk Kim }
113439a53e0cSJaegeuk Kim 
1135a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
113639a53e0cSJaegeuk Kim {
11375ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
11385ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
11391fe54f9dSJaegeuk Kim 		return;
11401fe54f9dSJaegeuk Kim 
1141a7ffdbe2SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1142c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1143c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
114439a53e0cSJaegeuk Kim }
114539a53e0cSJaegeuk Kim 
114639a53e0cSJaegeuk Kim static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
114739a53e0cSJaegeuk Kim {
114839a53e0cSJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
114939a53e0cSJaegeuk Kim }
115039a53e0cSJaegeuk Kim 
1151a7ffdbe2SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1152f8b2c1f9SJaegeuk Kim {
1153a7ffdbe2SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1154f8b2c1f9SJaegeuk Kim }
1155f8b2c1f9SJaegeuk Kim 
11565ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
11575ac206cfSNamjae Jeon {
11583519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
11595ac206cfSNamjae Jeon 	return ((get_pages(sbi, block_type) + pages_per_sec - 1)
11605ac206cfSNamjae Jeon 			>> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
11615ac206cfSNamjae Jeon }
11625ac206cfSNamjae Jeon 
116339a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
116439a53e0cSJaegeuk Kim {
11658b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
116639a53e0cSJaegeuk Kim }
116739a53e0cSJaegeuk Kim 
116839a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
116939a53e0cSJaegeuk Kim {
117039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
117139a53e0cSJaegeuk Kim 
117239a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
117339a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
117439a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
117539a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
117639a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
117739a53e0cSJaegeuk Kim 
117839a53e0cSJaegeuk Kim 	return 0;
117939a53e0cSJaegeuk Kim }
118039a53e0cSJaegeuk Kim 
118155141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
118255141486SWanpeng Li {
118355141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
118455141486SWanpeng Li }
118555141486SWanpeng Li 
118639a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
118739a53e0cSJaegeuk Kim {
118839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
11891dbe4152SChangman Lee 	int offset;
11901dbe4152SChangman Lee 
119155141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
11921dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
11931dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
11941dbe4152SChangman Lee 		else
119565b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
11961dbe4152SChangman Lee 	} else {
11971dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
119825ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
119939a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
120039a53e0cSJaegeuk Kim 	}
12011dbe4152SChangman Lee }
120239a53e0cSJaegeuk Kim 
120339a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
120439a53e0cSJaegeuk Kim {
120539a53e0cSJaegeuk Kim 	block_t start_addr;
120639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1207d71b5564SJaegeuk Kim 	unsigned long long ckpt_version = cur_cp_version(ckpt);
120839a53e0cSJaegeuk Kim 
120925ca923bSJaegeuk Kim 	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
121039a53e0cSJaegeuk Kim 
121139a53e0cSJaegeuk Kim 	/*
121239a53e0cSJaegeuk Kim 	 * odd numbered checkpoint should at cp segment 0
1213e1c42045Sarter97 	 * and even segment must be at cp segment 1
121439a53e0cSJaegeuk Kim 	 */
121539a53e0cSJaegeuk Kim 	if (!(ckpt_version & 1))
121639a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
121739a53e0cSJaegeuk Kim 
121839a53e0cSJaegeuk Kim 	return start_addr;
121939a53e0cSJaegeuk Kim }
122039a53e0cSJaegeuk Kim 
122139a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
122239a53e0cSJaegeuk Kim {
122339a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
122439a53e0cSJaegeuk Kim }
122539a53e0cSJaegeuk Kim 
122639a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
1227ef86d709SGu Zheng 						struct inode *inode)
122839a53e0cSJaegeuk Kim {
122939a53e0cSJaegeuk Kim 	block_t	valid_block_count;
123039a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
123139a53e0cSJaegeuk Kim 
123239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
123339a53e0cSJaegeuk Kim 
1234ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1235cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
123639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
123739a53e0cSJaegeuk Kim 		return false;
123839a53e0cSJaegeuk Kim 	}
123939a53e0cSJaegeuk Kim 
1240ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1241cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
124239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
124339a53e0cSJaegeuk Kim 		return false;
124439a53e0cSJaegeuk Kim 	}
124539a53e0cSJaegeuk Kim 
124639a53e0cSJaegeuk Kim 	if (inode)
1247ef86d709SGu Zheng 		inode->i_blocks++;
1248ef86d709SGu Zheng 
1249ef86d709SGu Zheng 	sbi->alloc_valid_block_count++;
1250ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1251ef86d709SGu Zheng 	sbi->total_valid_block_count++;
125239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
125339a53e0cSJaegeuk Kim 
125439a53e0cSJaegeuk Kim 	return true;
125539a53e0cSJaegeuk Kim }
125639a53e0cSJaegeuk Kim 
125739a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1258ef86d709SGu Zheng 						struct inode *inode)
125939a53e0cSJaegeuk Kim {
126039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
126139a53e0cSJaegeuk Kim 
12629850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
12639850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
12649850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !inode->i_blocks);
126539a53e0cSJaegeuk Kim 
1266ef86d709SGu Zheng 	inode->i_blocks--;
1267ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1268ef86d709SGu Zheng 	sbi->total_valid_block_count--;
126939a53e0cSJaegeuk Kim 
127039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
127139a53e0cSJaegeuk Kim }
127239a53e0cSJaegeuk Kim 
127339a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
127439a53e0cSJaegeuk Kim {
12758b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
127639a53e0cSJaegeuk Kim }
127739a53e0cSJaegeuk Kim 
127839a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
127939a53e0cSJaegeuk Kim {
128039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
12819850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
128239a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count++;
128339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
128439a53e0cSJaegeuk Kim }
128539a53e0cSJaegeuk Kim 
12860e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
128739a53e0cSJaegeuk Kim {
128839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
12899850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
129039a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count--;
129139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
129239a53e0cSJaegeuk Kim }
129339a53e0cSJaegeuk Kim 
129439a53e0cSJaegeuk Kim static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
129539a53e0cSJaegeuk Kim {
12968b8343faSJaegeuk Kim 	return sbi->total_valid_inode_count;
129739a53e0cSJaegeuk Kim }
129839a53e0cSJaegeuk Kim 
1299a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1300a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1301a56c7c6fSJaegeuk Kim {
1302a56c7c6fSJaegeuk Kim 	if (!for_write)
1303a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1304a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1305a56c7c6fSJaegeuk Kim }
1306a56c7c6fSJaegeuk Kim 
13076e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
13086e2c64adSJaegeuk Kim {
13096e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
13106e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
13116e2c64adSJaegeuk Kim 
13126e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
13136e2c64adSJaegeuk Kim 	kunmap(dst);
13146e2c64adSJaegeuk Kim 	kunmap(src);
13156e2c64adSJaegeuk Kim }
13166e2c64adSJaegeuk Kim 
131739a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
131839a53e0cSJaegeuk Kim {
1319031fa8ccSJaegeuk Kim 	if (!page)
132039a53e0cSJaegeuk Kim 		return;
132139a53e0cSJaegeuk Kim 
132239a53e0cSJaegeuk Kim 	if (unlock) {
13239850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
132439a53e0cSJaegeuk Kim 		unlock_page(page);
132539a53e0cSJaegeuk Kim 	}
132639a53e0cSJaegeuk Kim 	page_cache_release(page);
132739a53e0cSJaegeuk Kim }
132839a53e0cSJaegeuk Kim 
132939a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
133039a53e0cSJaegeuk Kim {
133139a53e0cSJaegeuk Kim 	if (dn->node_page)
133239a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
133339a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
133439a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
133539a53e0cSJaegeuk Kim 	dn->node_page = NULL;
133639a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
133739a53e0cSJaegeuk Kim }
133839a53e0cSJaegeuk Kim 
133939a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1340e8512d2eSGu Zheng 					size_t size)
134139a53e0cSJaegeuk Kim {
1342e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
134339a53e0cSJaegeuk Kim }
134439a53e0cSJaegeuk Kim 
13457bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
13467bd59381SGu Zheng 						gfp_t flags)
13477bd59381SGu Zheng {
13487bd59381SGu Zheng 	void *entry;
13497bd59381SGu Zheng 
135080c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
135180c54505SJaegeuk Kim 	if (!entry)
135280c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
13537bd59381SGu Zheng 	return entry;
13547bd59381SGu Zheng }
13557bd59381SGu Zheng 
1356740432f8SJaegeuk Kim static inline struct bio *f2fs_bio_alloc(int npages)
1357740432f8SJaegeuk Kim {
1358740432f8SJaegeuk Kim 	struct bio *bio;
1359740432f8SJaegeuk Kim 
1360740432f8SJaegeuk Kim 	/* No failure on bio allocation */
1361740432f8SJaegeuk Kim 	bio = bio_alloc(GFP_NOIO, npages);
136280c54505SJaegeuk Kim 	if (!bio)
136380c54505SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1364740432f8SJaegeuk Kim 	return bio;
1365740432f8SJaegeuk Kim }
1366740432f8SJaegeuk Kim 
13679be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
13689be32d72SJaegeuk Kim 				unsigned long index, void *item)
13699be32d72SJaegeuk Kim {
13709be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
13719be32d72SJaegeuk Kim 		cond_resched();
13729be32d72SJaegeuk Kim }
13739be32d72SJaegeuk Kim 
137439a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
137539a53e0cSJaegeuk Kim 
137639a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
137739a53e0cSJaegeuk Kim {
137845590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
137939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
138039a53e0cSJaegeuk Kim }
138139a53e0cSJaegeuk Kim 
138239a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
138339a53e0cSJaegeuk Kim {
138439a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
138539a53e0cSJaegeuk Kim }
138639a53e0cSJaegeuk Kim 
138739a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
138839a53e0cSJaegeuk Kim 		unsigned int offset)
138939a53e0cSJaegeuk Kim {
139039a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
139139a53e0cSJaegeuk Kim 	__le32 *addr_array;
139245590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
139339a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
139439a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
139539a53e0cSJaegeuk Kim }
139639a53e0cSJaegeuk Kim 
139739a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
139839a53e0cSJaegeuk Kim {
139939a53e0cSJaegeuk Kim 	int mask;
140039a53e0cSJaegeuk Kim 
140139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
140239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
140339a53e0cSJaegeuk Kim 	return mask & *addr;
140439a53e0cSJaegeuk Kim }
140539a53e0cSJaegeuk Kim 
1406a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1407a66cdd98SJaegeuk Kim {
1408a66cdd98SJaegeuk Kim 	int mask;
1409a66cdd98SJaegeuk Kim 
1410a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1411a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1412a66cdd98SJaegeuk Kim 	*addr |= mask;
1413a66cdd98SJaegeuk Kim }
1414a66cdd98SJaegeuk Kim 
1415a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1416a66cdd98SJaegeuk Kim {
1417a66cdd98SJaegeuk Kim 	int mask;
1418a66cdd98SJaegeuk Kim 
1419a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1420a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1421a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1422a66cdd98SJaegeuk Kim }
1423a66cdd98SJaegeuk Kim 
142452aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
142539a53e0cSJaegeuk Kim {
142639a53e0cSJaegeuk Kim 	int mask;
142739a53e0cSJaegeuk Kim 	int ret;
142839a53e0cSJaegeuk Kim 
142939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
143039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
143139a53e0cSJaegeuk Kim 	ret = mask & *addr;
143239a53e0cSJaegeuk Kim 	*addr |= mask;
143339a53e0cSJaegeuk Kim 	return ret;
143439a53e0cSJaegeuk Kim }
143539a53e0cSJaegeuk Kim 
143652aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
143739a53e0cSJaegeuk Kim {
143839a53e0cSJaegeuk Kim 	int mask;
143939a53e0cSJaegeuk Kim 	int ret;
144039a53e0cSJaegeuk Kim 
144139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
144239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
144339a53e0cSJaegeuk Kim 	ret = mask & *addr;
144439a53e0cSJaegeuk Kim 	*addr &= ~mask;
144539a53e0cSJaegeuk Kim 	return ret;
144639a53e0cSJaegeuk Kim }
144739a53e0cSJaegeuk Kim 
1448c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1449c6ac4c0eSGu Zheng {
1450c6ac4c0eSGu Zheng 	int mask;
1451c6ac4c0eSGu Zheng 
1452c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1453c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1454c6ac4c0eSGu Zheng 	*addr ^= mask;
1455c6ac4c0eSGu Zheng }
1456c6ac4c0eSGu Zheng 
145739a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
145839a53e0cSJaegeuk Kim enum {
145939a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1460b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
1461ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
146239a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
146339a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
146439a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1465c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1466699489bbSJaegeuk Kim 	FI_UPDATE_DIR,		/* should update inode block for consistency */
146774d0b917SJaegeuk Kim 	FI_DELAY_IPUT,		/* used for the recovery */
1468c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1469444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
14701001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
147134d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1472fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1473fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
147488b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
147588b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
147602a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
14773c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
14781e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
1479b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
1480510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
1481d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
1482c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
148339a53e0cSJaegeuk Kim };
148439a53e0cSJaegeuk Kim 
148539a53e0cSJaegeuk Kim static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
148639a53e0cSJaegeuk Kim {
148761e0f2d0SJaegeuk Kim 	if (!test_bit(flag, &fi->flags))
148839a53e0cSJaegeuk Kim 		set_bit(flag, &fi->flags);
148939a53e0cSJaegeuk Kim }
149039a53e0cSJaegeuk Kim 
149139a53e0cSJaegeuk Kim static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
149239a53e0cSJaegeuk Kim {
149339a53e0cSJaegeuk Kim 	return test_bit(flag, &fi->flags);
149439a53e0cSJaegeuk Kim }
149539a53e0cSJaegeuk Kim 
149639a53e0cSJaegeuk Kim static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
149739a53e0cSJaegeuk Kim {
149861e0f2d0SJaegeuk Kim 	if (test_bit(flag, &fi->flags))
149939a53e0cSJaegeuk Kim 		clear_bit(flag, &fi->flags);
150039a53e0cSJaegeuk Kim }
150139a53e0cSJaegeuk Kim 
150239a53e0cSJaegeuk Kim static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
150339a53e0cSJaegeuk Kim {
150439a53e0cSJaegeuk Kim 	fi->i_acl_mode = mode;
150539a53e0cSJaegeuk Kim 	set_inode_flag(fi, FI_ACL_MODE);
150639a53e0cSJaegeuk Kim }
150739a53e0cSJaegeuk Kim 
1508444c580fSJaegeuk Kim static inline void get_inline_info(struct f2fs_inode_info *fi,
1509444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1510444c580fSJaegeuk Kim {
1511444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
1512444c580fSJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_XATTR);
15131001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
15141001b347SHuajun Li 		set_inode_flag(fi, FI_INLINE_DATA);
151534d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
151634d67debSChao Yu 		set_inode_flag(fi, FI_INLINE_DENTRY);
1517b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
1518b3d208f9SJaegeuk Kim 		set_inode_flag(fi, FI_DATA_EXIST);
1519510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
1520510022a8SJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_DOTS);
1521444c580fSJaegeuk Kim }
1522444c580fSJaegeuk Kim 
1523444c580fSJaegeuk Kim static inline void set_raw_inline(struct f2fs_inode_info *fi,
1524444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1525444c580fSJaegeuk Kim {
1526444c580fSJaegeuk Kim 	ri->i_inline = 0;
1527444c580fSJaegeuk Kim 
1528444c580fSJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_XATTR))
1529444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
15301001b347SHuajun Li 	if (is_inode_flag_set(fi, FI_INLINE_DATA))
15311001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
153234d67debSChao Yu 	if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
153334d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
1534b3d208f9SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_DATA_EXIST))
1535b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
1536510022a8SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_DOTS))
1537510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
1538444c580fSJaegeuk Kim }
1539444c580fSJaegeuk Kim 
1540987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
1541987c7c31SChao Yu {
1542987c7c31SChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
1543987c7c31SChao Yu }
1544987c7c31SChao Yu 
154581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
1546de93653fSJaegeuk Kim {
154781ca7350SChao Yu 	if (f2fs_has_inline_xattr(inode))
1548de93653fSJaegeuk Kim 		return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1549de93653fSJaegeuk Kim 	return DEF_ADDRS_PER_INODE;
1550de93653fSJaegeuk Kim }
1551de93653fSJaegeuk Kim 
155265985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
155365985d93SJaegeuk Kim {
1554695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
155565985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
155665985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
155765985d93SJaegeuk Kim }
155865985d93SJaegeuk Kim 
155965985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
156065985d93SJaegeuk Kim {
1561987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
156265985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
156365985d93SJaegeuk Kim 	else
156465985d93SJaegeuk Kim 		return 0;
156565985d93SJaegeuk Kim }
156665985d93SJaegeuk Kim 
15670dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
15680dbdc2aeSJaegeuk Kim {
15690dbdc2aeSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
15700dbdc2aeSJaegeuk Kim }
15710dbdc2aeSJaegeuk Kim 
1572b3d208f9SJaegeuk Kim static inline void f2fs_clear_inline_inode(struct inode *inode)
1573b3d208f9SJaegeuk Kim {
1574b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1575b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1576b3d208f9SJaegeuk Kim }
1577b3d208f9SJaegeuk Kim 
1578b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
1579b3d208f9SJaegeuk Kim {
1580b3d208f9SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
1581b3d208f9SJaegeuk Kim }
1582b3d208f9SJaegeuk Kim 
1583510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
1584510022a8SJaegeuk Kim {
1585510022a8SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
1586510022a8SJaegeuk Kim }
1587510022a8SJaegeuk Kim 
158888b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
158988b88a66SJaegeuk Kim {
159088b88a66SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
159188b88a66SJaegeuk Kim }
159288b88a66SJaegeuk Kim 
159302a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
159402a1335fSJaegeuk Kim {
159502a1335fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
159602a1335fSJaegeuk Kim }
159702a1335fSJaegeuk Kim 
15983c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
15993c6c2bebSJaegeuk Kim {
16003c6c2bebSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
16013c6c2bebSJaegeuk Kim }
16023c6c2bebSJaegeuk Kim 
16031e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
16041e84371fSJaegeuk Kim {
16051e84371fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
16061e84371fSJaegeuk Kim }
16071e84371fSJaegeuk Kim 
16081001b347SHuajun Li static inline void *inline_data_addr(struct page *page)
16091001b347SHuajun Li {
1610695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
16111001b347SHuajun Li 	return (void *)&(ri->i_addr[1]);
16121001b347SHuajun Li }
16131001b347SHuajun Li 
161434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
161534d67debSChao Yu {
161634d67debSChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
161734d67debSChao Yu }
161834d67debSChao Yu 
16199486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
16209486ba44SJaegeuk Kim {
16219486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
16229486ba44SJaegeuk Kim 		kunmap(page);
16239486ba44SJaegeuk Kim }
16249486ba44SJaegeuk Kim 
1625b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
1626b5492af7SJaegeuk Kim {
1627b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
1628b5492af7SJaegeuk Kim }
1629b5492af7SJaegeuk Kim 
1630b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
1631b5492af7SJaegeuk Kim {
1632b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
1633b5492af7SJaegeuk Kim }
1634b5492af7SJaegeuk Kim 
1635b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
1636b5492af7SJaegeuk Kim {
1637b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
1638b5492af7SJaegeuk Kim }
1639b5492af7SJaegeuk Kim 
164077888c1eSJaegeuk Kim static inline int f2fs_readonly(struct super_block *sb)
164177888c1eSJaegeuk Kim {
164277888c1eSJaegeuk Kim 	return sb->s_flags & MS_RDONLY;
164377888c1eSJaegeuk Kim }
164477888c1eSJaegeuk Kim 
16451e968fdfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
16461e968fdfSJaegeuk Kim {
16471e968fdfSJaegeuk Kim 	return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
16481e968fdfSJaegeuk Kim }
16491e968fdfSJaegeuk Kim 
1650744602cfSJaegeuk Kim static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
1651744602cfSJaegeuk Kim {
1652744602cfSJaegeuk Kim 	set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1653744602cfSJaegeuk Kim 	sbi->sb->s_flags |= MS_RDONLY;
1654744602cfSJaegeuk Kim }
1655744602cfSJaegeuk Kim 
1656eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
1657eaa693f4SJaegeuk Kim {
1658eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
1659eaa693f4SJaegeuk Kim 		return true;
1660eaa693f4SJaegeuk Kim 
1661eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
1662eaa693f4SJaegeuk Kim 		return true;
1663eaa693f4SJaegeuk Kim 
1664eaa693f4SJaegeuk Kim 	return false;
1665eaa693f4SJaegeuk Kim }
1666eaa693f4SJaegeuk Kim 
16673e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
16683e72f721SJaegeuk Kim {
16693e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
16703e72f721SJaegeuk Kim 			is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
16713e72f721SJaegeuk Kim 		return false;
16723e72f721SJaegeuk Kim 
1673886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
16743e72f721SJaegeuk Kim }
16753e72f721SJaegeuk Kim 
167639307a8eSJaegeuk Kim static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
167739307a8eSJaegeuk Kim {
167839307a8eSJaegeuk Kim 	void *ret;
167939307a8eSJaegeuk Kim 
168039307a8eSJaegeuk Kim 	ret = kmalloc(size, flags | __GFP_NOWARN);
168139307a8eSJaegeuk Kim 	if (!ret)
168239307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags, PAGE_KERNEL);
168339307a8eSJaegeuk Kim 	return ret;
168439307a8eSJaegeuk Kim }
168539307a8eSJaegeuk Kim 
168639307a8eSJaegeuk Kim static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
168739307a8eSJaegeuk Kim {
168839307a8eSJaegeuk Kim 	void *ret;
168939307a8eSJaegeuk Kim 
169039307a8eSJaegeuk Kim 	ret = kzalloc(size, flags | __GFP_NOWARN);
169139307a8eSJaegeuk Kim 	if (!ret)
169239307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
169339307a8eSJaegeuk Kim 	return ret;
169439307a8eSJaegeuk Kim }
169539307a8eSJaegeuk Kim 
1696a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
1697a6dda0e6SChristoph Hellwig 	((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
1698a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
1699a6dda0e6SChristoph Hellwig 
1700267378d4SChao Yu /* get offset of first page in next direct node */
170181ca7350SChao Yu #define PGOFS_OF_NEXT_DNODE(pgofs, inode)				\
170281ca7350SChao Yu 	((pgofs < ADDRS_PER_INODE(inode)) ? ADDRS_PER_INODE(inode) :	\
170381ca7350SChao Yu 	(pgofs - ADDRS_PER_INODE(inode) + ADDRS_PER_BLOCK) /	\
170481ca7350SChao Yu 	ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode))
1705267378d4SChao Yu 
170639a53e0cSJaegeuk Kim /*
170739a53e0cSJaegeuk Kim  * file.c
170839a53e0cSJaegeuk Kim  */
170939a53e0cSJaegeuk Kim int f2fs_sync_file(struct file *, loff_t, loff_t, int);
171039a53e0cSJaegeuk Kim void truncate_data_blocks(struct dnode_of_data *);
1711764aa3e9SJaegeuk Kim int truncate_blocks(struct inode *, u64, bool);
1712b0154891SChao Yu int f2fs_truncate(struct inode *, bool);
17132d4d9fb5SJaegeuk Kim int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
171439a53e0cSJaegeuk Kim int f2fs_setattr(struct dentry *, struct iattr *);
171539a53e0cSJaegeuk Kim int truncate_hole(struct inode *, pgoff_t, pgoff_t);
1716b292dcabSJaegeuk Kim int truncate_data_blocks_range(struct dnode_of_data *, int);
171739a53e0cSJaegeuk Kim long f2fs_ioctl(struct file *, unsigned int, unsigned long);
1718e9750824SNamjae Jeon long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
171939a53e0cSJaegeuk Kim 
172039a53e0cSJaegeuk Kim /*
172139a53e0cSJaegeuk Kim  * inode.c
172239a53e0cSJaegeuk Kim  */
172339a53e0cSJaegeuk Kim void f2fs_set_inode_flags(struct inode *);
172439a53e0cSJaegeuk Kim struct inode *f2fs_iget(struct super_block *, unsigned long);
17254660f9c0SJaegeuk Kim int try_to_free_nats(struct f2fs_sb_info *, int);
172612719ae1SJaegeuk Kim int update_inode(struct inode *, struct page *);
172712719ae1SJaegeuk Kim int update_inode_page(struct inode *);
172839a53e0cSJaegeuk Kim int f2fs_write_inode(struct inode *, struct writeback_control *);
172939a53e0cSJaegeuk Kim void f2fs_evict_inode(struct inode *);
173044c16156SJaegeuk Kim void handle_failed_inode(struct inode *);
173139a53e0cSJaegeuk Kim 
173239a53e0cSJaegeuk Kim /*
173339a53e0cSJaegeuk Kim  * namei.c
173439a53e0cSJaegeuk Kim  */
173539a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
173639a53e0cSJaegeuk Kim 
173739a53e0cSJaegeuk Kim /*
173839a53e0cSJaegeuk Kim  * dir.c
173939a53e0cSJaegeuk Kim  */
1740dbeacf02SChao Yu extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
1741510022a8SJaegeuk Kim void set_de_type(struct f2fs_dir_entry *, umode_t);
17426e22c691SJaegeuk Kim 
17436e22c691SJaegeuk Kim struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *,
17446e22c691SJaegeuk Kim 			f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
17457b3cd7d6SJaegeuk Kim bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
1746d8c6822aSJaegeuk Kim 			unsigned int, struct f2fs_str *);
1747062a3e7bSJaegeuk Kim void do_make_empty_dir(struct inode *, struct inode *,
1748062a3e7bSJaegeuk Kim 			struct f2fs_dentry_ptr *);
1749dbeacf02SChao Yu struct page *init_inode_metadata(struct inode *, struct inode *,
1750bce8d112SJaegeuk Kim 			const struct qstr *, struct page *);
1751dbeacf02SChao Yu void update_parent_metadata(struct inode *, struct inode *, unsigned int);
1752a82afa20SJaegeuk Kim int room_for_filename(const void *, int, int);
1753dbeacf02SChao Yu void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
175439a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
175539a53e0cSJaegeuk Kim 							struct page **);
175639a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
175739a53e0cSJaegeuk Kim ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
175839a53e0cSJaegeuk Kim void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
175939a53e0cSJaegeuk Kim 				struct page *, struct inode *);
1760e7d55452SJaegeuk Kim int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
1761510022a8SJaegeuk Kim void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
17623b4d732aSChao Yu 			const struct qstr *, f2fs_hash_t , unsigned int);
1763510022a8SJaegeuk Kim int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
1764510022a8SJaegeuk Kim 			umode_t);
1765dbeacf02SChao Yu void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
1766dbeacf02SChao Yu 							struct inode *);
1767b97a9b5dSJaegeuk Kim int f2fs_do_tmpfile(struct inode *, struct inode *);
176839a53e0cSJaegeuk Kim bool f2fs_empty_dir(struct inode *);
176939a53e0cSJaegeuk Kim 
1770b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
1771b7f7a5e0SAl Viro {
17722b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
1773510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
1774b7f7a5e0SAl Viro }
1775b7f7a5e0SAl Viro 
177639a53e0cSJaegeuk Kim /*
177739a53e0cSJaegeuk Kim  * super.c
177839a53e0cSJaegeuk Kim  */
1779c5bda1c8SChao Yu int f2fs_commit_super(struct f2fs_sb_info *, bool);
178039a53e0cSJaegeuk Kim int f2fs_sync_fs(struct super_block *, int);
1781a07ef784SNamjae Jeon extern __printf(3, 4)
1782a07ef784SNamjae Jeon void f2fs_msg(struct super_block *, const char *, const char *, ...);
178339a53e0cSJaegeuk Kim 
178439a53e0cSJaegeuk Kim /*
178539a53e0cSJaegeuk Kim  * hash.c
178639a53e0cSJaegeuk Kim  */
1787eee6160fSGu Zheng f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
178839a53e0cSJaegeuk Kim 
178939a53e0cSJaegeuk Kim /*
179039a53e0cSJaegeuk Kim  * node.c
179139a53e0cSJaegeuk Kim  */
179239a53e0cSJaegeuk Kim struct dnode_of_data;
179339a53e0cSJaegeuk Kim struct node_info;
179439a53e0cSJaegeuk Kim 
17956fb03f3aSJaegeuk Kim bool available_free_memory(struct f2fs_sb_info *, int);
17962dcf51abSJaegeuk Kim int need_dentry_mark(struct f2fs_sb_info *, nid_t);
179788bd02c9SJaegeuk Kim bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
179888bd02c9SJaegeuk Kim bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
179939a53e0cSJaegeuk Kim void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
18003cf45747SChao Yu pgoff_t get_next_page_offset(struct dnode_of_data *, pgoff_t);
180139a53e0cSJaegeuk Kim int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
180239a53e0cSJaegeuk Kim int truncate_inode_blocks(struct inode *, pgoff_t);
18034f16fb0fSJaegeuk Kim int truncate_xattr_node(struct inode *, struct page *);
1804cfe58f9dSJaegeuk Kim int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
180513ec7297SChao Yu int remove_inode_page(struct inode *);
1806a014e037SJaegeuk Kim struct page *new_inode_page(struct inode *);
18078ae8f162SJaegeuk Kim struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
180839a53e0cSJaegeuk Kim void ra_node_page(struct f2fs_sb_info *, nid_t);
180939a53e0cSJaegeuk Kim struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
181039a53e0cSJaegeuk Kim struct page *get_node_page_ra(struct page *, int);
181139a53e0cSJaegeuk Kim void sync_inode_page(struct dnode_of_data *);
181239a53e0cSJaegeuk Kim int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
181339a53e0cSJaegeuk Kim bool alloc_nid(struct f2fs_sb_info *, nid_t *);
181439a53e0cSJaegeuk Kim void alloc_nid_done(struct f2fs_sb_info *, nid_t);
181539a53e0cSJaegeuk Kim void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
181631696580SChao Yu int try_to_free_nids(struct f2fs_sb_info *, int);
181770cfed88SChao Yu void recover_inline_xattr(struct inode *, struct page *);
18181c35a90eSJaegeuk Kim void recover_xattr_data(struct inode *, struct page *, block_t);
181939a53e0cSJaegeuk Kim int recover_inode_page(struct f2fs_sb_info *, struct page *);
182039a53e0cSJaegeuk Kim int restore_node_summary(struct f2fs_sb_info *, unsigned int,
182139a53e0cSJaegeuk Kim 				struct f2fs_summary_block *);
182239a53e0cSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *);
182339a53e0cSJaegeuk Kim int build_node_manager(struct f2fs_sb_info *);
182439a53e0cSJaegeuk Kim void destroy_node_manager(struct f2fs_sb_info *);
18256e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
182639a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
182739a53e0cSJaegeuk Kim 
182839a53e0cSJaegeuk Kim /*
182939a53e0cSJaegeuk Kim  * segment.c
183039a53e0cSJaegeuk Kim  */
183188b88a66SJaegeuk Kim void register_inmem_page(struct inode *, struct page *);
1832edb27deeSJaegeuk Kim int commit_inmem_pages(struct inode *, bool);
18332c4db1a6SJaegeuk Kim void f2fs_balance_fs(struct f2fs_sb_info *, bool);
18344660f9c0SJaegeuk Kim void f2fs_balance_fs_bg(struct f2fs_sb_info *);
18356b4afdd7SJaegeuk Kim int f2fs_issue_flush(struct f2fs_sb_info *);
18362163d198SGu Zheng int create_flush_cmd_control(struct f2fs_sb_info *);
18372163d198SGu Zheng void destroy_flush_cmd_control(struct f2fs_sb_info *);
183839a53e0cSJaegeuk Kim void invalidate_blocks(struct f2fs_sb_info *, block_t);
18396e2c64adSJaegeuk Kim bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
18405e443818SJaegeuk Kim void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
1841836b5a63SJaegeuk Kim void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
18424b2fecc8SJaegeuk Kim void release_discard_addrs(struct f2fs_sb_info *);
1843e90c2d28SChao Yu bool discard_next_dnode(struct f2fs_sb_info *, block_t);
18443fa06d7bSChao Yu int npages_for_summary_flush(struct f2fs_sb_info *, bool);
184539a53e0cSJaegeuk Kim void allocate_new_segments(struct f2fs_sb_info *);
18464b2fecc8SJaegeuk Kim int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
184739a53e0cSJaegeuk Kim struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
1848381722d2SChao Yu void update_meta_page(struct f2fs_sb_info *, void *, block_t);
1849577e3495SJaegeuk Kim void write_meta_page(struct f2fs_sb_info *, struct page *);
185005ca3632SJaegeuk Kim void write_node_page(unsigned int, struct f2fs_io_info *);
185105ca3632SJaegeuk Kim void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
185205ca3632SJaegeuk Kim void rewrite_data_page(struct f2fs_io_info *);
1853528e3459SChao Yu void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
1854528e3459SChao Yu 				block_t, block_t, unsigned char, bool);
1855bfad7c2dSJaegeuk Kim void allocate_data_block(struct f2fs_sb_info *, struct page *,
1856bfad7c2dSJaegeuk Kim 		block_t, block_t *, struct f2fs_summary *, int);
1857fec1d657SJaegeuk Kim void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
185808b39fbdSChao Yu void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *, block_t);
185939a53e0cSJaegeuk Kim void write_data_summaries(struct f2fs_sb_info *, block_t);
186039a53e0cSJaegeuk Kim void write_node_summaries(struct f2fs_sb_info *, block_t);
186139a53e0cSJaegeuk Kim int lookup_journal_in_cursum(struct f2fs_summary_block *,
186239a53e0cSJaegeuk Kim 					int, unsigned int, int);
18634b2fecc8SJaegeuk Kim void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
186439a53e0cSJaegeuk Kim int build_segment_manager(struct f2fs_sb_info *);
186539a53e0cSJaegeuk Kim void destroy_segment_manager(struct f2fs_sb_info *);
18667fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
18677fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
186839a53e0cSJaegeuk Kim 
186939a53e0cSJaegeuk Kim /*
187039a53e0cSJaegeuk Kim  * checkpoint.c
187139a53e0cSJaegeuk Kim  */
187239a53e0cSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
187339a53e0cSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
18742b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *, pgoff_t);
1875f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
187626879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int, bool);
1877635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
187839a53e0cSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
1879a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1880a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1881a49324f1SChao Yu void release_ino_entry(struct f2fs_sb_info *);
1882fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
1883cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *);
1884cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *);
188539a53e0cSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *, nid_t);
188639a53e0cSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
18878c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *);
188839a53e0cSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *);
1889a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *, struct page *);
18905deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *);
1891c227f912SChao Yu void remove_dirty_inode(struct inode *);
18926d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *, enum inode_type);
1893c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
18946451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *);
18956e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
189639a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
189739a53e0cSJaegeuk Kim 
189839a53e0cSJaegeuk Kim /*
189939a53e0cSJaegeuk Kim  * data.c
190039a53e0cSJaegeuk Kim  */
1901458e6197SJaegeuk Kim void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
19020c3a5797SChao Yu void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *, struct inode *,
19030c3a5797SChao Yu 				struct page *, nid_t, enum page_type, int);
190405ca3632SJaegeuk Kim int f2fs_submit_page_bio(struct f2fs_io_info *);
190505ca3632SJaegeuk Kim void f2fs_submit_page_mbio(struct f2fs_io_info *);
1906216a620aSChao Yu void set_data_blkaddr(struct dnode_of_data *);
190739a53e0cSJaegeuk Kim int reserve_new_block(struct dnode_of_data *);
1908759af1c9SFan Li int f2fs_get_block(struct dnode_of_data *, pgoff_t);
1909*b439b103SJaegeuk Kim ssize_t f2fs_preallocate_blocks(struct kiocb *, struct iov_iter *);
1910b600965cSHuajun Li int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
1911a56c7c6fSJaegeuk Kim struct page *get_read_data_page(struct inode *, pgoff_t, int, bool);
191243f3eae1SJaegeuk Kim struct page *find_data_page(struct inode *, pgoff_t);
1913a56c7c6fSJaegeuk Kim struct page *get_lock_data_page(struct inode *, pgoff_t, bool);
191464aa7ed9SJaegeuk Kim struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
191505ca3632SJaegeuk Kim int do_write_data_page(struct f2fs_io_info *);
1916d323d005SChao Yu int f2fs_map_blocks(struct inode *, struct f2fs_map_blocks *, int, int);
19179ab70134SJaegeuk Kim int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
1918487261f3SChao Yu void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
1919487261f3SChao Yu int f2fs_release_page(struct page *, gfp_t);
192039a53e0cSJaegeuk Kim 
192139a53e0cSJaegeuk Kim /*
192239a53e0cSJaegeuk Kim  * gc.c
192339a53e0cSJaegeuk Kim  */
192439a53e0cSJaegeuk Kim int start_gc_thread(struct f2fs_sb_info *);
192539a53e0cSJaegeuk Kim void stop_gc_thread(struct f2fs_sb_info *);
192681ca7350SChao Yu block_t start_bidx_of_node(unsigned int, struct inode *);
1927d530d4d8SChao Yu int f2fs_gc(struct f2fs_sb_info *, bool);
192839a53e0cSJaegeuk Kim void build_gc_manager(struct f2fs_sb_info *);
192939a53e0cSJaegeuk Kim 
193039a53e0cSJaegeuk Kim /*
193139a53e0cSJaegeuk Kim  * recovery.c
193239a53e0cSJaegeuk Kim  */
19336ead1142SJaegeuk Kim int recover_fsync_data(struct f2fs_sb_info *);
193439a53e0cSJaegeuk Kim bool space_for_roll_forward(struct f2fs_sb_info *);
193539a53e0cSJaegeuk Kim 
193639a53e0cSJaegeuk Kim /*
193739a53e0cSJaegeuk Kim  * debug.c
193839a53e0cSJaegeuk Kim  */
193939a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
194039a53e0cSJaegeuk Kim struct f2fs_stat_info {
194139a53e0cSJaegeuk Kim 	struct list_head stat_list;
194239a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
194339a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
194439a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
19455b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
19465b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
1947c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
194833fbd510SChao Yu 	int ndirty_node, ndirty_meta;
194933fbd510SChao Yu 	int ndirty_dent, ndirty_dirs, ndirty_data, ndirty_files;
1950dd4e4b59SJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits, fnids;
195139a53e0cSJaegeuk Kim 	int total_count, utilization;
1952d5e8f6c9SChao Yu 	int bg_gc, inmem_pages, wb_pages;
1953d5e8f6c9SChao Yu 	int inline_xattr, inline_inode, inline_dir;
195439a53e0cSJaegeuk Kim 	unsigned int valid_count, valid_node_count, valid_inode_count;
195539a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
195639a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
195739a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
195839a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
195942190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
196039a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
1961e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
196239a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
1963e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
196439a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
196539a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
196639a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
196739a53e0cSJaegeuk Kim 
196839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
196939a53e0cSJaegeuk Kim 	unsigned int block_count[2];
1970b9a2c252SChangman Lee 	unsigned int inplace_count;
19719edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
197239a53e0cSJaegeuk Kim };
197339a53e0cSJaegeuk Kim 
1974963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1975963d4f7dSGu Zheng {
1976963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
1977963d4f7dSGu Zheng }
1978963d4f7dSGu Zheng 
1979942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
198042190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
198139a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
1982dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
198333fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
198433fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
19855b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
19865b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
19875b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
19885b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
1989d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
1990d5e8f6c9SChao Yu 	do {								\
1991d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
1992d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
1993d5e8f6c9SChao Yu 	} while (0)
1994d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
1995d5e8f6c9SChao Yu 	do {								\
1996d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
1997d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
1998d5e8f6c9SChao Yu 	} while (0)
19990dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
20000dbdc2aeSJaegeuk Kim 	do {								\
20010dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
200203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
20030dbdc2aeSJaegeuk Kim 	} while (0)
20040dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
20050dbdc2aeSJaegeuk Kim 	do {								\
20060dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
200703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
20080dbdc2aeSJaegeuk Kim 	} while (0)
20093289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
20103289c061SJaegeuk Kim 	do {								\
20113289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
201203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
20133289c061SJaegeuk Kim 	} while (0)
20143289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
20153289c061SJaegeuk Kim 	do {								\
20163289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
201703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
20183289c061SJaegeuk Kim 	} while (0)
2019dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2020dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2021dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2022dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2023b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2024b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
2025e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
202639a53e0cSJaegeuk Kim 	do {								\
2027963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
202839a53e0cSJaegeuk Kim 		(si)->tot_segs++;					\
2029e1235983SChangman Lee 		if (type == SUM_TYPE_DATA) {				\
203039a53e0cSJaegeuk Kim 			si->data_segs++;				\
2031e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2032e1235983SChangman Lee 		} else {						\
203339a53e0cSJaegeuk Kim 			si->node_segs++;				\
2034e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2035e1235983SChangman Lee 		}							\
203639a53e0cSJaegeuk Kim 	} while (0)
203739a53e0cSJaegeuk Kim 
203839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
203939a53e0cSJaegeuk Kim 	(si->tot_blks += (blks))
204039a53e0cSJaegeuk Kim 
2041e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
204239a53e0cSJaegeuk Kim 	do {								\
2043963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
204439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
204539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
2046e1235983SChangman Lee 		si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0;	\
204739a53e0cSJaegeuk Kim 	} while (0)
204839a53e0cSJaegeuk Kim 
2049e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
205039a53e0cSJaegeuk Kim 	do {								\
2051963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
205239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
205339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
2054e1235983SChangman Lee 		si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0;	\
205539a53e0cSJaegeuk Kim 	} while (0)
205639a53e0cSJaegeuk Kim 
205739a53e0cSJaegeuk Kim int f2fs_build_stats(struct f2fs_sb_info *);
205839a53e0cSJaegeuk Kim void f2fs_destroy_stats(struct f2fs_sb_info *);
2059787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
20604589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
206139a53e0cSJaegeuk Kim #else
2062942e0be6SChangman Lee #define stat_inc_cp_count(si)
206342190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)
206439a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
2065dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(si)
206633fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)
206733fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)
2068dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)
2069029e13ccSChao Yu #define stat_inc_rbtree_node_hit(sb)
207091c481ffSChao Yu #define stat_inc_largest_node_hit(sbi)
207191c481ffSChao Yu #define stat_inc_cached_node_hit(sbi)
2072d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)
2073d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)
20740dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)
20750dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)
20763289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)
20773289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)
2078dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)
2079dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)
2080b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)
2081e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)
208239a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
2083e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)
2084e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)
208539a53e0cSJaegeuk Kim 
208639a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
208739a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2088787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
20894589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
209039a53e0cSJaegeuk Kim #endif
209139a53e0cSJaegeuk Kim 
209239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
209339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
209439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
209539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
209639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
209739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
209839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
209939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
2100cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
210139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
210229e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
21031001b347SHuajun Li 
2104e18c65b2SHuajun Li /*
2105e18c65b2SHuajun Li  * inline.c
2106e18c65b2SHuajun Li  */
210701b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *);
210801b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *);
2109b3d208f9SJaegeuk Kim void read_inline_data(struct page *, struct page *);
21100bfcfccaSChao Yu bool truncate_inline_inode(struct page *, u64);
2111e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *, struct page *);
2112b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
2113b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *);
2114b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *, struct page *);
21150342fd30SJaegeuk Kim bool recover_inline_data(struct inode *, struct page *);
21166e22c691SJaegeuk Kim struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
21176e22c691SJaegeuk Kim 				struct f2fs_filename *, struct page **);
2118201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
2119201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
2120510022a8SJaegeuk Kim int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
2121510022a8SJaegeuk Kim 						nid_t, umode_t);
2122201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
2123201a05beSChao Yu 						struct inode *, struct inode *);
2124201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *);
2125d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *, struct dir_context *,
2126d8c6822aSJaegeuk Kim 						struct f2fs_str *);
212767f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *,
212867f8cf3cSJaegeuk Kim 		struct fiemap_extent_info *, __u64, __u64);
2129cde4de12SJaegeuk Kim 
2130cde4de12SJaegeuk Kim /*
21312658e50dSJaegeuk Kim  * shrinker.c
21322658e50dSJaegeuk Kim  */
21332658e50dSJaegeuk Kim unsigned long f2fs_shrink_count(struct shrinker *, struct shrink_control *);
21342658e50dSJaegeuk Kim unsigned long f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
21352658e50dSJaegeuk Kim void f2fs_join_shrinker(struct f2fs_sb_info *);
21362658e50dSJaegeuk Kim void f2fs_leave_shrinker(struct f2fs_sb_info *);
21372658e50dSJaegeuk Kim 
21382658e50dSJaegeuk Kim /*
2139a28ef1f5SChao Yu  * extent_cache.c
2140a28ef1f5SChao Yu  */
2141a28ef1f5SChao Yu unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
2142ed3d1256SJaegeuk Kim bool f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
2143a28ef1f5SChao Yu unsigned int f2fs_destroy_extent_node(struct inode *);
2144a28ef1f5SChao Yu void f2fs_destroy_extent_tree(struct inode *);
2145a28ef1f5SChao Yu bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
2146a28ef1f5SChao Yu void f2fs_update_extent_cache(struct dnode_of_data *);
214719b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
214819b2c30dSChao Yu 						pgoff_t, block_t, unsigned int);
2149a28ef1f5SChao Yu void init_extent_cache_info(struct f2fs_sb_info *);
2150a28ef1f5SChao Yu int __init create_extent_cache(void);
2151a28ef1f5SChao Yu void destroy_extent_cache(void);
2152a28ef1f5SChao Yu 
2153a28ef1f5SChao Yu /*
2154cde4de12SJaegeuk Kim  * crypto support
2155cde4de12SJaegeuk Kim  */
2156cde4de12SJaegeuk Kim static inline int f2fs_encrypted_inode(struct inode *inode)
2157cde4de12SJaegeuk Kim {
2158cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2159cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
2160cde4de12SJaegeuk Kim #else
2161cde4de12SJaegeuk Kim 	return 0;
2162cde4de12SJaegeuk Kim #endif
2163cde4de12SJaegeuk Kim }
2164cde4de12SJaegeuk Kim 
2165cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
2166cde4de12SJaegeuk Kim {
2167cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2168cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2169cde4de12SJaegeuk Kim #endif
2170cde4de12SJaegeuk Kim }
2171cde4de12SJaegeuk Kim 
2172cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2173cde4de12SJaegeuk Kim {
2174cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2175cde4de12SJaegeuk Kim 	return unlikely(bio->bi_private != NULL);
2176cde4de12SJaegeuk Kim #else
2177cde4de12SJaegeuk Kim 	return false;
2178cde4de12SJaegeuk Kim #endif
2179cde4de12SJaegeuk Kim }
2180cde4de12SJaegeuk Kim 
2181cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2182cde4de12SJaegeuk Kim {
2183cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2184cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2185cde4de12SJaegeuk Kim #else
2186cde4de12SJaegeuk Kim 	return 0;
2187cde4de12SJaegeuk Kim #endif
2188cde4de12SJaegeuk Kim }
2189f424f664SJaegeuk Kim 
2190fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
2191fcc85a4dSJaegeuk Kim {
2192fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2193886f56f9SAl Viro 	umode_t mode = inode->i_mode;
2194fcc85a4dSJaegeuk Kim 
2195fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2196fcc85a4dSJaegeuk Kim #else
2197fcc85a4dSJaegeuk Kim 	return 0;
2198fcc85a4dSJaegeuk Kim #endif
2199fcc85a4dSJaegeuk Kim }
2200fcc85a4dSJaegeuk Kim 
2201f424f664SJaegeuk Kim /* crypto_policy.c */
2202f424f664SJaegeuk Kim int f2fs_is_child_context_consistent_with_parent(struct inode *,
2203f424f664SJaegeuk Kim 							struct inode *);
2204f424f664SJaegeuk Kim int f2fs_inherit_context(struct inode *, struct inode *, struct page *);
2205f424f664SJaegeuk Kim int f2fs_process_policy(const struct f2fs_encryption_policy *, struct inode *);
2206f424f664SJaegeuk Kim int f2fs_get_policy(struct inode *, struct f2fs_encryption_policy *);
220757e5055bSJaegeuk Kim 
220857e5055bSJaegeuk Kim /* crypt.c */
22098bacf6deSJaegeuk Kim extern struct kmem_cache *f2fs_crypt_info_cachep;
221057e5055bSJaegeuk Kim bool f2fs_valid_contents_enc_mode(uint32_t);
221157e5055bSJaegeuk Kim uint32_t f2fs_validate_encryption_key_size(uint32_t, uint32_t);
221257e5055bSJaegeuk Kim struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *);
221357e5055bSJaegeuk Kim void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *);
221457e5055bSJaegeuk Kim struct page *f2fs_encrypt(struct inode *, struct page *);
221557e5055bSJaegeuk Kim int f2fs_decrypt(struct f2fs_crypto_ctx *, struct page *);
221657e5055bSJaegeuk Kim int f2fs_decrypt_one(struct inode *, struct page *);
221757e5055bSJaegeuk Kim void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
221857e5055bSJaegeuk Kim 
22190adda907SJaegeuk Kim /* crypto_key.c */
222026bf3dc7SJaegeuk Kim void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
22210adda907SJaegeuk Kim int _f2fs_get_encryption_info(struct inode *inode);
22220adda907SJaegeuk Kim 
22236b3bd08fSJaegeuk Kim /* crypto_fname.c */
22246b3bd08fSJaegeuk Kim bool f2fs_valid_filenames_enc_mode(uint32_t);
22256b3bd08fSJaegeuk Kim u32 f2fs_fname_crypto_round_up(u32, u32);
22266b3bd08fSJaegeuk Kim int f2fs_fname_crypto_alloc_buffer(struct inode *, u32, struct f2fs_str *);
22276b3bd08fSJaegeuk Kim int f2fs_fname_disk_to_usr(struct inode *, f2fs_hash_t *,
22286b3bd08fSJaegeuk Kim 			const struct f2fs_str *, struct f2fs_str *);
22296b3bd08fSJaegeuk Kim int f2fs_fname_usr_to_disk(struct inode *, const struct qstr *,
22306b3bd08fSJaegeuk Kim 			struct f2fs_str *);
22316b3bd08fSJaegeuk Kim 
223257e5055bSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
223357e5055bSJaegeuk Kim void f2fs_restore_and_release_control_page(struct page **);
223457e5055bSJaegeuk Kim void f2fs_restore_control_page(struct page *);
223557e5055bSJaegeuk Kim 
2236cfc4d971SJaegeuk Kim int __init f2fs_init_crypto(void);
2237cfc4d971SJaegeuk Kim int f2fs_crypto_initialize(void);
223857e5055bSJaegeuk Kim void f2fs_exit_crypto(void);
22390adda907SJaegeuk Kim 
22400adda907SJaegeuk Kim int f2fs_has_encryption_key(struct inode *);
22410adda907SJaegeuk Kim 
22420adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *inode)
22430adda907SJaegeuk Kim {
22440adda907SJaegeuk Kim 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
22450adda907SJaegeuk Kim 
22460adda907SJaegeuk Kim 	if (!ci ||
22470adda907SJaegeuk Kim 		(ci->ci_keyring_key &&
22480adda907SJaegeuk Kim 		 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
22490adda907SJaegeuk Kim 					       (1 << KEY_FLAG_REVOKED) |
22500adda907SJaegeuk Kim 					       (1 << KEY_FLAG_DEAD)))))
22510adda907SJaegeuk Kim 		return _f2fs_get_encryption_info(inode);
22520adda907SJaegeuk Kim 	return 0;
22530adda907SJaegeuk Kim }
22546b3bd08fSJaegeuk Kim 
22556b3bd08fSJaegeuk Kim void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
22566b3bd08fSJaegeuk Kim int f2fs_fname_setup_filename(struct inode *, const struct qstr *,
22576b3bd08fSJaegeuk Kim 				int lookup, struct f2fs_filename *);
22586b3bd08fSJaegeuk Kim void f2fs_fname_free_filename(struct f2fs_filename *);
225957e5055bSJaegeuk Kim #else
226057e5055bSJaegeuk Kim static inline void f2fs_restore_and_release_control_page(struct page **p) { }
226157e5055bSJaegeuk Kim static inline void f2fs_restore_control_page(struct page *p) { }
226257e5055bSJaegeuk Kim 
2263cfc4d971SJaegeuk Kim static inline int __init f2fs_init_crypto(void) { return 0; }
226457e5055bSJaegeuk Kim static inline void f2fs_exit_crypto(void) { }
22650adda907SJaegeuk Kim 
22660adda907SJaegeuk Kim static inline int f2fs_has_encryption_key(struct inode *i) { return 0; }
22670adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *i) { return 0; }
22686b3bd08fSJaegeuk Kim static inline void f2fs_fname_crypto_free_buffer(struct f2fs_str *p) { }
22696b3bd08fSJaegeuk Kim 
22706b3bd08fSJaegeuk Kim static inline int f2fs_fname_setup_filename(struct inode *dir,
22716b3bd08fSJaegeuk Kim 					const struct qstr *iname,
22726b3bd08fSJaegeuk Kim 					int lookup, struct f2fs_filename *fname)
22736b3bd08fSJaegeuk Kim {
22746b3bd08fSJaegeuk Kim 	memset(fname, 0, sizeof(struct f2fs_filename));
22756b3bd08fSJaegeuk Kim 	fname->usr_fname = iname;
22766b3bd08fSJaegeuk Kim 	fname->disk_name.name = (unsigned char *)iname->name;
22776b3bd08fSJaegeuk Kim 	fname->disk_name.len = iname->len;
22786b3bd08fSJaegeuk Kim 	return 0;
22796b3bd08fSJaegeuk Kim }
22806b3bd08fSJaegeuk Kim 
22816b3bd08fSJaegeuk Kim static inline void f2fs_fname_free_filename(struct f2fs_filename *fname) { }
228257e5055bSJaegeuk Kim #endif
228339a53e0cSJaegeuk Kim #endif
2284