xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 406657dd1868a4d7b12be04ed769d9215ec9a7d1)
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 
186dfc08a12SChao Yu #define nats_in_cursum(jnl)		(le16_to_cpu(jnl->n_nats))
187dfc08a12SChao Yu #define sits_in_cursum(jnl)		(le16_to_cpu(jnl->n_sits))
18839a53e0cSJaegeuk Kim 
189dfc08a12SChao Yu #define nat_in_journal(jnl, i)		(jnl->nat_j.entries[i].ne)
190dfc08a12SChao Yu #define nid_in_journal(jnl, i)		(jnl->nat_j.entries[i].nid)
191dfc08a12SChao Yu #define sit_in_journal(jnl, i)		(jnl->sit_j.entries[i].se)
192dfc08a12SChao Yu #define segno_in_journal(jnl, i)	(jnl->sit_j.entries[i].segno)
19339a53e0cSJaegeuk Kim 
194dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
195dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
196309cc2b6SJaegeuk Kim 
197dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
19839a53e0cSJaegeuk Kim {
199dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
200dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
20139a53e0cSJaegeuk Kim 	return before;
20239a53e0cSJaegeuk Kim }
20339a53e0cSJaegeuk Kim 
204dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
20539a53e0cSJaegeuk Kim {
206dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
207dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
20839a53e0cSJaegeuk Kim 	return before;
20939a53e0cSJaegeuk Kim }
21039a53e0cSJaegeuk Kim 
211dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
212dfc08a12SChao Yu 							int size, int type)
213184a5cd2SChao Yu {
214184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
215dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
216dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
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
394b439b103SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_DIO		4
39524b84912SJaegeuk Kim #define F2FS_GET_BLOCK_PRE_AIO		5
396e2b4e2bcSChao Yu 
397003a3e1dSJaegeuk Kim /*
39839a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
39939a53e0cSJaegeuk Kim  */
40039a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
401354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
402cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
403e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
40439a53e0cSJaegeuk Kim 
405b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
406b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
407b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
408b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
409b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
410b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
411cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
412cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
413cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
414e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
415e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
416cde4de12SJaegeuk Kim 
417cde4de12SJaegeuk Kim /* Encryption algorithms */
418cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_INVALID		0
419cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_XTS	1
420cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_GCM	2
421cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CBC	3
422cde4de12SJaegeuk Kim #define F2FS_ENCRYPTION_MODE_AES_256_CTS	4
423b5492af7SJaegeuk Kim 
424f424f664SJaegeuk Kim #include "f2fs_crypto.h"
425f424f664SJaegeuk Kim 
426ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
427ab9fa662SJaegeuk Kim 
42839a53e0cSJaegeuk Kim struct f2fs_inode_info {
42939a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
43039a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
43139a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
43238431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
43339a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
4346666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
43539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
43639a53e0cSJaegeuk Kim 
43739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
43839a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
439d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
440a7ffdbe2SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
44139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
44239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
44339a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
444e518ff81SJaegeuk Kim 	unsigned long long xattr_ver;	/* cp version of xattr modification */
44588b88a66SJaegeuk Kim 
4462710fd7eSChao Yu 	struct list_head dirty_list;	/* linked in global dirty list */
44788b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
44888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
449cde4de12SJaegeuk Kim 
4503e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
4513e72f721SJaegeuk Kim 
452cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
453cde4de12SJaegeuk Kim 	/* Encryption params */
454cde4de12SJaegeuk Kim 	struct f2fs_crypt_info *i_crypt_info;
455cde4de12SJaegeuk Kim #endif
45639a53e0cSJaegeuk Kim };
45739a53e0cSJaegeuk Kim 
45839a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
45939a53e0cSJaegeuk Kim 					struct f2fs_extent i_ext)
46039a53e0cSJaegeuk Kim {
46139a53e0cSJaegeuk Kim 	ext->fofs = le32_to_cpu(i_ext.fofs);
4624d0b0bd4SChao Yu 	ext->blk = le32_to_cpu(i_ext.blk);
46339a53e0cSJaegeuk Kim 	ext->len = le32_to_cpu(i_ext.len);
46439a53e0cSJaegeuk Kim }
46539a53e0cSJaegeuk Kim 
46639a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
46739a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
46839a53e0cSJaegeuk Kim {
46939a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
4704d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
47139a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
47239a53e0cSJaegeuk Kim }
47339a53e0cSJaegeuk Kim 
474429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
475429511cdSChao Yu 						u32 blk, unsigned int len)
476429511cdSChao Yu {
477429511cdSChao Yu 	ei->fofs = fofs;
478429511cdSChao Yu 	ei->blk = blk;
479429511cdSChao Yu 	ei->len = len;
480429511cdSChao Yu }
481429511cdSChao Yu 
4820bdee482SChao Yu static inline bool __is_extent_same(struct extent_info *ei1,
4830bdee482SChao Yu 						struct extent_info *ei2)
4840bdee482SChao Yu {
4850bdee482SChao Yu 	return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
4860bdee482SChao Yu 						ei1->len == ei2->len);
4870bdee482SChao Yu }
4880bdee482SChao Yu 
489429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
490429511cdSChao Yu 						struct extent_info *front)
491429511cdSChao Yu {
492429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
493429511cdSChao Yu 			back->blk + back->len == front->blk);
494429511cdSChao Yu }
495429511cdSChao Yu 
496429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
497429511cdSChao Yu 						struct extent_info *back)
498429511cdSChao Yu {
499429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
500429511cdSChao Yu }
501429511cdSChao Yu 
502429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
503429511cdSChao Yu 						struct extent_info *front)
504429511cdSChao Yu {
505429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
506429511cdSChao Yu }
507429511cdSChao Yu 
5084abd3f5aSChao Yu static inline void __try_update_largest_extent(struct extent_tree *et,
5094abd3f5aSChao Yu 						struct extent_node *en)
5104abd3f5aSChao Yu {
5114abd3f5aSChao Yu 	if (en->ei.len > et->largest.len)
5124abd3f5aSChao Yu 		et->largest = en->ei;
5134abd3f5aSChao Yu }
5144abd3f5aSChao Yu 
51539a53e0cSJaegeuk Kim struct f2fs_nm_info {
51639a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
51739a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
5187ee0eeabSJaegeuk Kim 	nid_t available_nids;		/* maximum available node ids */
51939a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
520cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
521ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
5222304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
52339a53e0cSJaegeuk Kim 
52439a53e0cSJaegeuk Kim 	/* NAT cache management */
52539a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
526309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
5278b26ef98SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
52839a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
529309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
530aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
53139a53e0cSJaegeuk Kim 
53239a53e0cSJaegeuk Kim 	/* free node ids management */
5338a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
53439a53e0cSJaegeuk Kim 	struct list_head free_nid_list;	/* a list for free nids */
53539a53e0cSJaegeuk Kim 	spinlock_t free_nid_list_lock;	/* protect free nid list */
53639a53e0cSJaegeuk Kim 	unsigned int fcnt;		/* the number of free node id */
53739a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
53839a53e0cSJaegeuk Kim 
53939a53e0cSJaegeuk Kim 	/* for checkpoint */
54039a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
54139a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
54239a53e0cSJaegeuk Kim };
54339a53e0cSJaegeuk Kim 
54439a53e0cSJaegeuk Kim /*
54539a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
54639a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
54739a53e0cSJaegeuk Kim  * by the data offset in a file.
54839a53e0cSJaegeuk Kim  */
54939a53e0cSJaegeuk Kim struct dnode_of_data {
55039a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
55139a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
55239a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
55339a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
55439a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
55539a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
55693bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
5573cf45747SChao Yu 	char cur_level;			/* level of hole node page */
5583cf45747SChao Yu 	char max_level;			/* level of current page located */
55939a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
56039a53e0cSJaegeuk Kim };
56139a53e0cSJaegeuk Kim 
56239a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
56339a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
56439a53e0cSJaegeuk Kim {
565d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
56639a53e0cSJaegeuk Kim 	dn->inode = inode;
56739a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
56839a53e0cSJaegeuk Kim 	dn->node_page = npage;
56939a53e0cSJaegeuk Kim 	dn->nid = nid;
57039a53e0cSJaegeuk Kim }
57139a53e0cSJaegeuk Kim 
57239a53e0cSJaegeuk Kim /*
57339a53e0cSJaegeuk Kim  * For SIT manager
57439a53e0cSJaegeuk Kim  *
57539a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
57639a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
57739a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
57839a53e0cSJaegeuk Kim  * respectively.
57939a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
58039a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
58139a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
58239a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
58339a53e0cSJaegeuk Kim  * data and 8 for node logs.
58439a53e0cSJaegeuk Kim  */
58539a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
58639a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
58739a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
58839a53e0cSJaegeuk Kim 
58939a53e0cSJaegeuk Kim enum {
59039a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
59139a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
59239a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
59339a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
59439a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
59539a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
59638aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
59738aa0889SJaegeuk Kim 	CURSEG_DIRECT_IO,	/* to use for the direct IO path */
59839a53e0cSJaegeuk Kim };
59939a53e0cSJaegeuk Kim 
6006b4afdd7SJaegeuk Kim struct flush_cmd {
6016b4afdd7SJaegeuk Kim 	struct completion wait;
602721bd4d5SGu Zheng 	struct llist_node llnode;
6036b4afdd7SJaegeuk Kim 	int ret;
6046b4afdd7SJaegeuk Kim };
6056b4afdd7SJaegeuk Kim 
606a688b9d9SGu Zheng struct flush_cmd_control {
607a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
608a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
609721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
610721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
611a688b9d9SGu Zheng };
612a688b9d9SGu Zheng 
61339a53e0cSJaegeuk Kim struct f2fs_sm_info {
61439a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
61539a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
61639a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
61739a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
61839a53e0cSJaegeuk Kim 
61939a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
62039a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
62139a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
62239a53e0cSJaegeuk Kim 
62339a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
62439a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
62539a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
62639a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
62781eb8d6eSJaegeuk Kim 
62881eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
62981eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
6307fd9e544SJaegeuk Kim 
6317fd9e544SJaegeuk Kim 	/* for small discard management */
6327fd9e544SJaegeuk Kim 	struct list_head discard_list;		/* 4KB discard list */
6337fd9e544SJaegeuk Kim 	int nr_discards;			/* # of discards in the list */
6347fd9e544SJaegeuk Kim 	int max_discards;			/* max. discards to be issued */
635216fbd64SJaegeuk Kim 
636bba681cbSJaegeuk Kim 	/* for batched trimming */
637bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
638bba681cbSJaegeuk Kim 
639184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
640184a5cd2SChao Yu 
641216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
642216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
643c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
6446b4afdd7SJaegeuk Kim 
6456b4afdd7SJaegeuk Kim 	/* for flush command control */
646a688b9d9SGu Zheng 	struct flush_cmd_control *cmd_control_info;
647a688b9d9SGu Zheng 
64839a53e0cSJaegeuk Kim };
64939a53e0cSJaegeuk Kim 
65039a53e0cSJaegeuk Kim /*
65139a53e0cSJaegeuk Kim  * For superblock
65239a53e0cSJaegeuk Kim  */
65339a53e0cSJaegeuk Kim /*
65439a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
65539a53e0cSJaegeuk Kim  *
65639a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
65739a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
65839a53e0cSJaegeuk Kim  */
65939a53e0cSJaegeuk Kim enum count_type {
66039a53e0cSJaegeuk Kim 	F2FS_WRITEBACK,
66139a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
662c227f912SChao Yu 	F2FS_DIRTY_DATA,
66339a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
66439a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
6658dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
66639a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
66739a53e0cSJaegeuk Kim };
66839a53e0cSJaegeuk Kim 
66939a53e0cSJaegeuk Kim /*
670e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
67139a53e0cSJaegeuk Kim  * The available types are:
67239a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
67339a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
67439a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
67539a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
67639a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
67739a53e0cSJaegeuk Kim  *			with waiting the bio's completion
67839a53e0cSJaegeuk Kim  * ...			Only can be used with META.
67939a53e0cSJaegeuk Kim  */
6807d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
68139a53e0cSJaegeuk Kim enum page_type {
68239a53e0cSJaegeuk Kim 	DATA,
68339a53e0cSJaegeuk Kim 	NODE,
68439a53e0cSJaegeuk Kim 	META,
68539a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
68639a53e0cSJaegeuk Kim 	META_FLUSH,
6878ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
6888ce67cb0SJaegeuk Kim 	INMEM_DROP,
68928bc106bSChao Yu 	INMEM_REVOKE,
6908ce67cb0SJaegeuk Kim 	IPU,
6918ce67cb0SJaegeuk Kim 	OPU,
69239a53e0cSJaegeuk Kim };
69339a53e0cSJaegeuk Kim 
694458e6197SJaegeuk Kim struct f2fs_io_info {
69505ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
696458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
6977e8f2308SGu Zheng 	int rw;			/* contains R/RS/W/WS with REQ_META/REQ_PRIO */
6987a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
69928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
70005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
7014375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
702458e6197SJaegeuk Kim };
703458e6197SJaegeuk Kim 
70493dfe2acSJaegeuk Kim #define is_read_io(rw)	(((rw) & 1) == READ)
7051ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
706458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
7071ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
7081ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
709458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
710df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
7111ff7bd3bSJaegeuk Kim };
7121ff7bd3bSJaegeuk Kim 
713c227f912SChao Yu enum inode_type {
714c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
715c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
716c227f912SChao Yu 	NR_INODE_TYPE,
717c227f912SChao Yu };
718c227f912SChao Yu 
71967298804SChao Yu /* for inner inode cache management */
72067298804SChao Yu struct inode_management {
72167298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
72267298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
72367298804SChao Yu 	struct list_head ino_list;		/* inode list head */
72467298804SChao Yu 	unsigned long ino_num;			/* number of entries */
72567298804SChao Yu };
72667298804SChao Yu 
727caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
728caf0047eSChao Yu enum {
729caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
730caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
731caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
732caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
733caf0047eSChao Yu };
734caf0047eSChao Yu 
7356beceb54SJaegeuk Kim enum {
7366beceb54SJaegeuk Kim 	CP_TIME,
737d0239e1bSJaegeuk Kim 	REQ_TIME,
7386beceb54SJaegeuk Kim 	MAX_TIME,
7396beceb54SJaegeuk Kim };
7406beceb54SJaegeuk Kim 
74139a53e0cSJaegeuk Kim struct f2fs_sb_info {
74239a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
7435e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
74439a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
745e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
746caf0047eSChao Yu 	int s_flag;				/* flags for sbi */
74739a53e0cSJaegeuk Kim 
74839a53e0cSJaegeuk Kim 	/* for node-related operations */
74939a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
75039a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
75139a53e0cSJaegeuk Kim 
75239a53e0cSJaegeuk Kim 	/* for segment-related operations */
75339a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
7541ff7bd3bSJaegeuk Kim 
7551ff7bd3bSJaegeuk Kim 	/* for bio operations */
756924b720bSChao Yu 	struct f2fs_bio_info read_io;			/* for read bios */
7571ff7bd3bSJaegeuk Kim 	struct f2fs_bio_info write_io[NR_PAGE_TYPE];	/* for write bios */
75839a53e0cSJaegeuk Kim 
75939a53e0cSJaegeuk Kim 	/* for checkpoint */
76039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
76139a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
76239936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
763e479556bSGu Zheng 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
764b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
7655463e7c1SJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
766fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
7676beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
7686beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
76939a53e0cSJaegeuk Kim 
77067298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
7716451e041SJaegeuk Kim 
7726451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
7730d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
77439a53e0cSJaegeuk Kim 
775c227f912SChao Yu 	/* for inode management */
776c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
777c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
77839a53e0cSJaegeuk Kim 
77913054c54SChao Yu 	/* for extent tree cache */
78013054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
78113054c54SChao Yu 	struct rw_semaphore extent_tree_lock;	/* locking extent radix tree */
78213054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
78313054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
7847441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
785137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
78674fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
78713054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
78813054c54SChao Yu 
78939a53e0cSJaegeuk Kim 	/* basic filesystem units */
79039a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
79139a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
79239a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
79339a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
79439a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
79539a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
79639a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
79739a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
79839a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
79939a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
80039a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
80139a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
80239a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
80339a53e0cSJaegeuk Kim 	unsigned int total_valid_inode_count;	/* valid inode count */
804e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
80539a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
806ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
80739a53e0cSJaegeuk Kim 
80839a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
80939a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
81039a53e0cSJaegeuk Kim 	block_t alloc_valid_block_count;	/* # of allocated blocks */
811a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
81239a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
81339a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
81439a53e0cSJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];	/* # of pages, see count_type */
81539a53e0cSJaegeuk Kim 
81639a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
81739a53e0cSJaegeuk Kim 
81839a53e0cSJaegeuk Kim 	/* for cleaning operations */
81939a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
82039a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
8215ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
82239a53e0cSJaegeuk Kim 
823b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
824b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
825b1c57c1cSJaegeuk Kim 
82639a53e0cSJaegeuk Kim 	/*
82739a53e0cSJaegeuk Kim 	 * for stat information.
82839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
82939a53e0cSJaegeuk Kim 	 */
83035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
83139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
83239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
83339a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
834b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
8355b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
8365b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
8375b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
8385b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
839d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
84003e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
84103e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
84239a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
84333fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
84435b09d82SNamjae Jeon #endif
84535b09d82SNamjae Jeon 	unsigned int last_victim[2];		/* last victim segment # */
84639a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
847b59d0baeSNamjae Jeon 
848b59d0baeSNamjae Jeon 	/* For sysfs suppport */
849b59d0baeSNamjae Jeon 	struct kobject s_kobj;
850b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
8512658e50dSJaegeuk Kim 
8522658e50dSJaegeuk Kim 	/* For shrinker support */
8532658e50dSJaegeuk Kim 	struct list_head s_list;
8542658e50dSJaegeuk Kim 	struct mutex umount_mutex;
8552658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
8568f1dbbbbSShuoran Liu 
8578f1dbbbbSShuoran Liu 	/* For write statistics */
8588f1dbbbbSShuoran Liu 	u64 sectors_written_start;
8598f1dbbbbSShuoran Liu 	u64 kbytes_written;
86039a53e0cSJaegeuk Kim };
86139a53e0cSJaegeuk Kim 
8628f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
8638f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
8648f1dbbbbSShuoran Liu  */
8658f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
8668f1dbbbbSShuoran Liu (((u64)part_stat_read(s->sb->s_bdev->bd_part, sectors[1]) -		 \
8678f1dbbbbSShuoran Liu 		s->sectors_written_start) >> 1)
8688f1dbbbbSShuoran Liu 
8696beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
8706beceb54SJaegeuk Kim {
8716beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
8726beceb54SJaegeuk Kim }
8736beceb54SJaegeuk Kim 
8746beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
8756beceb54SJaegeuk Kim {
8766beceb54SJaegeuk Kim 	struct timespec ts = {sbi->interval_time[type], 0};
8776beceb54SJaegeuk Kim 	unsigned long interval = timespec_to_jiffies(&ts);
8786beceb54SJaegeuk Kim 
8796beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
8806beceb54SJaegeuk Kim }
8816beceb54SJaegeuk Kim 
882d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
883d0239e1bSJaegeuk Kim {
884d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
885d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
886d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
887d0239e1bSJaegeuk Kim 
888d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
889d0239e1bSJaegeuk Kim 		return 0;
890d0239e1bSJaegeuk Kim 
891d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
892d0239e1bSJaegeuk Kim }
893d0239e1bSJaegeuk Kim 
89439a53e0cSJaegeuk Kim /*
89539a53e0cSJaegeuk Kim  * Inline functions
89639a53e0cSJaegeuk Kim  */
89739a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
89839a53e0cSJaegeuk Kim {
89939a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
90039a53e0cSJaegeuk Kim }
90139a53e0cSJaegeuk Kim 
90239a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
90339a53e0cSJaegeuk Kim {
90439a53e0cSJaegeuk Kim 	return sb->s_fs_info;
90539a53e0cSJaegeuk Kim }
90639a53e0cSJaegeuk Kim 
9074081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
9084081363fSJaegeuk Kim {
9094081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
9104081363fSJaegeuk Kim }
9114081363fSJaegeuk Kim 
9124081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
9134081363fSJaegeuk Kim {
9144081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
9154081363fSJaegeuk Kim }
9164081363fSJaegeuk Kim 
9174081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
9184081363fSJaegeuk Kim {
9194081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
9204081363fSJaegeuk Kim }
9214081363fSJaegeuk Kim 
92239a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
92339a53e0cSJaegeuk Kim {
92439a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
92539a53e0cSJaegeuk Kim }
92639a53e0cSJaegeuk Kim 
92739a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
92839a53e0cSJaegeuk Kim {
92939a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
93039a53e0cSJaegeuk Kim }
93139a53e0cSJaegeuk Kim 
93245590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
93345590710SGu Zheng {
93445590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
93545590710SGu Zheng }
93645590710SGu Zheng 
93758bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
93858bfaf44SJaegeuk Kim {
93958bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
94058bfaf44SJaegeuk Kim }
94158bfaf44SJaegeuk Kim 
94239a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
94339a53e0cSJaegeuk Kim {
94439a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
94539a53e0cSJaegeuk Kim }
94639a53e0cSJaegeuk Kim 
94739a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
94839a53e0cSJaegeuk Kim {
94939a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
95039a53e0cSJaegeuk Kim }
95139a53e0cSJaegeuk Kim 
95239a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
95339a53e0cSJaegeuk Kim {
95439a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
95539a53e0cSJaegeuk Kim }
95639a53e0cSJaegeuk Kim 
95739a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
95839a53e0cSJaegeuk Kim {
95939a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
96039a53e0cSJaegeuk Kim }
96139a53e0cSJaegeuk Kim 
96239a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
96339a53e0cSJaegeuk Kim {
96439a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
96539a53e0cSJaegeuk Kim }
96639a53e0cSJaegeuk Kim 
9679df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
9689df27d98SGu Zheng {
9699df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
9709df27d98SGu Zheng }
9719df27d98SGu Zheng 
9724ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
9734ef51a8fSJaegeuk Kim {
9744ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
9754ef51a8fSJaegeuk Kim }
9764ef51a8fSJaegeuk Kim 
977caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
97839a53e0cSJaegeuk Kim {
979caf0047eSChao Yu 	return sbi->s_flag & (0x01 << type);
98039a53e0cSJaegeuk Kim }
98139a53e0cSJaegeuk Kim 
982caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
98339a53e0cSJaegeuk Kim {
984caf0047eSChao Yu 	sbi->s_flag |= (0x01 << type);
985caf0047eSChao Yu }
986caf0047eSChao Yu 
987caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
988caf0047eSChao Yu {
989caf0047eSChao Yu 	sbi->s_flag &= ~(0x01 << type);
99039a53e0cSJaegeuk Kim }
99139a53e0cSJaegeuk Kim 
992d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
993d71b5564SJaegeuk Kim {
994d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
995d71b5564SJaegeuk Kim }
996d71b5564SJaegeuk Kim 
99725ca923bSJaegeuk Kim static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
99825ca923bSJaegeuk Kim {
99925ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
100025ca923bSJaegeuk Kim 	return ckpt_flags & f;
100125ca923bSJaegeuk Kim }
100225ca923bSJaegeuk Kim 
100325ca923bSJaegeuk Kim static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
100425ca923bSJaegeuk Kim {
100525ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
100625ca923bSJaegeuk Kim 	ckpt_flags |= f;
100725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
100825ca923bSJaegeuk Kim }
100925ca923bSJaegeuk Kim 
101025ca923bSJaegeuk Kim static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
101125ca923bSJaegeuk Kim {
101225ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
101325ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
101425ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
101525ca923bSJaegeuk Kim }
101625ca923bSJaegeuk Kim 
1017e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
101839a53e0cSJaegeuk Kim {
1019e479556bSGu Zheng 	down_read(&sbi->cp_rwsem);
102039a53e0cSJaegeuk Kim }
102139a53e0cSJaegeuk Kim 
1022e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
102339a53e0cSJaegeuk Kim {
1024e479556bSGu Zheng 	up_read(&sbi->cp_rwsem);
102539936837SJaegeuk Kim }
102639936837SJaegeuk Kim 
1027e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
102839936837SJaegeuk Kim {
10290daaad97SJaegeuk Kim 	f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
103039936837SJaegeuk Kim }
103139936837SJaegeuk Kim 
1032e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
103339936837SJaegeuk Kim {
1034e479556bSGu Zheng 	up_write(&sbi->cp_rwsem);
103539a53e0cSJaegeuk Kim }
103639a53e0cSJaegeuk Kim 
1037119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1038119ee914SJaegeuk Kim {
1039119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1040119ee914SJaegeuk Kim 
1041119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1042119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1043119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1044119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1045119ee914SJaegeuk Kim 	return reason;
1046119ee914SJaegeuk Kim }
1047119ee914SJaegeuk Kim 
1048119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1049119ee914SJaegeuk Kim {
1050119ee914SJaegeuk Kim 	return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
1051119ee914SJaegeuk Kim }
1052119ee914SJaegeuk Kim 
1053119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1054119ee914SJaegeuk Kim {
1055119ee914SJaegeuk Kim 	return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
1056119ee914SJaegeuk Kim 			is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
1057119ee914SJaegeuk Kim }
1058119ee914SJaegeuk Kim 
105939a53e0cSJaegeuk Kim /*
106039a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
106139a53e0cSJaegeuk Kim  */
1062064e0823SNamjae Jeon static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
106339a53e0cSJaegeuk Kim {
1064d6b7d4b3SChao Yu 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1065d6b7d4b3SChao Yu 		return -EINVAL;
1066cfb271d4SChao Yu 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1067064e0823SNamjae Jeon 		return -EINVAL;
1068064e0823SNamjae Jeon 	return 0;
106939a53e0cSJaegeuk Kim }
107039a53e0cSJaegeuk Kim 
107139a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
107239a53e0cSJaegeuk Kim 
107339a53e0cSJaegeuk Kim /*
107439a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
107539a53e0cSJaegeuk Kim  */
107639a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
107739a53e0cSJaegeuk Kim {
107839a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
10796c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
108039a53e0cSJaegeuk Kim 	else
10816c311ec6SChris Fries 		return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
108239a53e0cSJaegeuk Kim }
108339a53e0cSJaegeuk Kim 
10844bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
10854bc8e9bcSChao Yu {
10864bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
10874bc8e9bcSChao Yu }
10884bc8e9bcSChao Yu 
108939a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
109039a53e0cSJaegeuk Kim 				 struct inode *inode, blkcnt_t count)
109139a53e0cSJaegeuk Kim {
109239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
109339a53e0cSJaegeuk Kim 
109439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
109539a53e0cSJaegeuk Kim 	valid_block_count =
109639a53e0cSJaegeuk Kim 		sbi->total_valid_block_count + (block_t)count;
1097cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
109839a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
109939a53e0cSJaegeuk Kim 		return false;
110039a53e0cSJaegeuk Kim 	}
110139a53e0cSJaegeuk Kim 	inode->i_blocks += count;
110239a53e0cSJaegeuk Kim 	sbi->total_valid_block_count = valid_block_count;
110339a53e0cSJaegeuk Kim 	sbi->alloc_valid_block_count += (block_t)count;
110439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
110539a53e0cSJaegeuk Kim 	return true;
110639a53e0cSJaegeuk Kim }
110739a53e0cSJaegeuk Kim 
1108da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
110939a53e0cSJaegeuk Kim 						struct inode *inode,
111039a53e0cSJaegeuk Kim 						blkcnt_t count)
111139a53e0cSJaegeuk Kim {
111239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
11139850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
11149850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, inode->i_blocks < count);
111539a53e0cSJaegeuk Kim 	inode->i_blocks -= count;
111639a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
111739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
111839a53e0cSJaegeuk Kim }
111939a53e0cSJaegeuk Kim 
112039a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
112139a53e0cSJaegeuk Kim {
112239a53e0cSJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
1123caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
112439a53e0cSJaegeuk Kim }
112539a53e0cSJaegeuk Kim 
1126a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
112739a53e0cSJaegeuk Kim {
1128a7ffdbe2SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1129c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1130c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
113139a53e0cSJaegeuk Kim }
113239a53e0cSJaegeuk Kim 
113339a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
113439a53e0cSJaegeuk Kim {
113539a53e0cSJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
113639a53e0cSJaegeuk Kim }
113739a53e0cSJaegeuk Kim 
1138a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
113939a53e0cSJaegeuk Kim {
11405ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
11415ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
11421fe54f9dSJaegeuk Kim 		return;
11431fe54f9dSJaegeuk Kim 
1144a7ffdbe2SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1145c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1146c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
114739a53e0cSJaegeuk Kim }
114839a53e0cSJaegeuk Kim 
114939a53e0cSJaegeuk Kim static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
115039a53e0cSJaegeuk Kim {
115139a53e0cSJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
115239a53e0cSJaegeuk Kim }
115339a53e0cSJaegeuk Kim 
1154a7ffdbe2SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1155f8b2c1f9SJaegeuk Kim {
1156a7ffdbe2SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1157f8b2c1f9SJaegeuk Kim }
1158f8b2c1f9SJaegeuk Kim 
11595ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
11605ac206cfSNamjae Jeon {
11613519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
11625ac206cfSNamjae Jeon 	return ((get_pages(sbi, block_type) + pages_per_sec - 1)
11635ac206cfSNamjae Jeon 			>> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
11645ac206cfSNamjae Jeon }
11655ac206cfSNamjae Jeon 
116639a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
116739a53e0cSJaegeuk Kim {
11688b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
116939a53e0cSJaegeuk Kim }
117039a53e0cSJaegeuk Kim 
117139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
117239a53e0cSJaegeuk Kim {
117339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
117439a53e0cSJaegeuk Kim 
117539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
117639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
117739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
117839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
117939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
118039a53e0cSJaegeuk Kim 
118139a53e0cSJaegeuk Kim 	return 0;
118239a53e0cSJaegeuk Kim }
118339a53e0cSJaegeuk Kim 
118455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
118555141486SWanpeng Li {
118655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
118755141486SWanpeng Li }
118855141486SWanpeng Li 
118939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
119039a53e0cSJaegeuk Kim {
119139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
11921dbe4152SChangman Lee 	int offset;
11931dbe4152SChangman Lee 
119455141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
11951dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
11961dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
11971dbe4152SChangman Lee 		else
119865b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
11991dbe4152SChangman Lee 	} else {
12001dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
120125ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
120239a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
120339a53e0cSJaegeuk Kim 	}
12041dbe4152SChangman Lee }
120539a53e0cSJaegeuk Kim 
120639a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
120739a53e0cSJaegeuk Kim {
120839a53e0cSJaegeuk Kim 	block_t start_addr;
120939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1210d71b5564SJaegeuk Kim 	unsigned long long ckpt_version = cur_cp_version(ckpt);
121139a53e0cSJaegeuk Kim 
121225ca923bSJaegeuk Kim 	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
121339a53e0cSJaegeuk Kim 
121439a53e0cSJaegeuk Kim 	/*
121539a53e0cSJaegeuk Kim 	 * odd numbered checkpoint should at cp segment 0
1216e1c42045Sarter97 	 * and even segment must be at cp segment 1
121739a53e0cSJaegeuk Kim 	 */
121839a53e0cSJaegeuk Kim 	if (!(ckpt_version & 1))
121939a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
122039a53e0cSJaegeuk Kim 
122139a53e0cSJaegeuk Kim 	return start_addr;
122239a53e0cSJaegeuk Kim }
122339a53e0cSJaegeuk Kim 
122439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
122539a53e0cSJaegeuk Kim {
122639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
122739a53e0cSJaegeuk Kim }
122839a53e0cSJaegeuk Kim 
122939a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
1230ef86d709SGu Zheng 						struct inode *inode)
123139a53e0cSJaegeuk Kim {
123239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
123339a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
123439a53e0cSJaegeuk Kim 
123539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
123639a53e0cSJaegeuk Kim 
1237ef86d709SGu Zheng 	valid_block_count = sbi->total_valid_block_count + 1;
1238cfb271d4SChao Yu 	if (unlikely(valid_block_count > sbi->user_block_count)) {
123939a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
124039a53e0cSJaegeuk Kim 		return false;
124139a53e0cSJaegeuk Kim 	}
124239a53e0cSJaegeuk Kim 
1243ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1244cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
124539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
124639a53e0cSJaegeuk Kim 		return false;
124739a53e0cSJaegeuk Kim 	}
124839a53e0cSJaegeuk Kim 
124939a53e0cSJaegeuk Kim 	if (inode)
1250ef86d709SGu Zheng 		inode->i_blocks++;
1251ef86d709SGu Zheng 
1252ef86d709SGu Zheng 	sbi->alloc_valid_block_count++;
1253ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1254ef86d709SGu Zheng 	sbi->total_valid_block_count++;
125539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
125639a53e0cSJaegeuk Kim 
125739a53e0cSJaegeuk Kim 	return true;
125839a53e0cSJaegeuk Kim }
125939a53e0cSJaegeuk Kim 
126039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1261ef86d709SGu Zheng 						struct inode *inode)
126239a53e0cSJaegeuk Kim {
126339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
126439a53e0cSJaegeuk Kim 
12659850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
12669850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
12679850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !inode->i_blocks);
126839a53e0cSJaegeuk Kim 
1269ef86d709SGu Zheng 	inode->i_blocks--;
1270ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1271ef86d709SGu Zheng 	sbi->total_valid_block_count--;
127239a53e0cSJaegeuk Kim 
127339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
127439a53e0cSJaegeuk Kim }
127539a53e0cSJaegeuk Kim 
127639a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
127739a53e0cSJaegeuk Kim {
12788b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
127939a53e0cSJaegeuk Kim }
128039a53e0cSJaegeuk Kim 
128139a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
128239a53e0cSJaegeuk Kim {
128339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
12849850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
128539a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count++;
128639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
128739a53e0cSJaegeuk Kim }
128839a53e0cSJaegeuk Kim 
12890e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
129039a53e0cSJaegeuk Kim {
129139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
12929850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
129339a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count--;
129439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
129539a53e0cSJaegeuk Kim }
129639a53e0cSJaegeuk Kim 
129739a53e0cSJaegeuk Kim static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
129839a53e0cSJaegeuk Kim {
12998b8343faSJaegeuk Kim 	return sbi->total_valid_inode_count;
130039a53e0cSJaegeuk Kim }
130139a53e0cSJaegeuk Kim 
1302a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1303a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1304a56c7c6fSJaegeuk Kim {
1305a56c7c6fSJaegeuk Kim 	if (!for_write)
1306a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1307a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1308a56c7c6fSJaegeuk Kim }
1309a56c7c6fSJaegeuk Kim 
13106e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
13116e2c64adSJaegeuk Kim {
13126e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
13136e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
13146e2c64adSJaegeuk Kim 
13156e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
13166e2c64adSJaegeuk Kim 	kunmap(dst);
13176e2c64adSJaegeuk Kim 	kunmap(src);
13186e2c64adSJaegeuk Kim }
13196e2c64adSJaegeuk Kim 
132039a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
132139a53e0cSJaegeuk Kim {
1322031fa8ccSJaegeuk Kim 	if (!page)
132339a53e0cSJaegeuk Kim 		return;
132439a53e0cSJaegeuk Kim 
132539a53e0cSJaegeuk Kim 	if (unlock) {
13269850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
132739a53e0cSJaegeuk Kim 		unlock_page(page);
132839a53e0cSJaegeuk Kim 	}
132939a53e0cSJaegeuk Kim 	page_cache_release(page);
133039a53e0cSJaegeuk Kim }
133139a53e0cSJaegeuk Kim 
133239a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
133339a53e0cSJaegeuk Kim {
133439a53e0cSJaegeuk Kim 	if (dn->node_page)
133539a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
133639a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
133739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
133839a53e0cSJaegeuk Kim 	dn->node_page = NULL;
133939a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
134039a53e0cSJaegeuk Kim }
134139a53e0cSJaegeuk Kim 
134239a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1343e8512d2eSGu Zheng 					size_t size)
134439a53e0cSJaegeuk Kim {
1345e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
134639a53e0cSJaegeuk Kim }
134739a53e0cSJaegeuk Kim 
13487bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
13497bd59381SGu Zheng 						gfp_t flags)
13507bd59381SGu Zheng {
13517bd59381SGu Zheng 	void *entry;
13527bd59381SGu Zheng 
135380c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
135480c54505SJaegeuk Kim 	if (!entry)
135580c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
13567bd59381SGu Zheng 	return entry;
13577bd59381SGu Zheng }
13587bd59381SGu Zheng 
1359740432f8SJaegeuk Kim static inline struct bio *f2fs_bio_alloc(int npages)
1360740432f8SJaegeuk Kim {
1361740432f8SJaegeuk Kim 	struct bio *bio;
1362740432f8SJaegeuk Kim 
1363740432f8SJaegeuk Kim 	/* No failure on bio allocation */
1364740432f8SJaegeuk Kim 	bio = bio_alloc(GFP_NOIO, npages);
136580c54505SJaegeuk Kim 	if (!bio)
136680c54505SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1367740432f8SJaegeuk Kim 	return bio;
1368740432f8SJaegeuk Kim }
1369740432f8SJaegeuk Kim 
13709be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
13719be32d72SJaegeuk Kim 				unsigned long index, void *item)
13729be32d72SJaegeuk Kim {
13739be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
13749be32d72SJaegeuk Kim 		cond_resched();
13759be32d72SJaegeuk Kim }
13769be32d72SJaegeuk Kim 
137739a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
137839a53e0cSJaegeuk Kim 
137939a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
138039a53e0cSJaegeuk Kim {
138145590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
138239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
138339a53e0cSJaegeuk Kim }
138439a53e0cSJaegeuk Kim 
138539a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
138639a53e0cSJaegeuk Kim {
138739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
138839a53e0cSJaegeuk Kim }
138939a53e0cSJaegeuk Kim 
139039a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
139139a53e0cSJaegeuk Kim 		unsigned int offset)
139239a53e0cSJaegeuk Kim {
139339a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
139439a53e0cSJaegeuk Kim 	__le32 *addr_array;
139545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
139639a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
139739a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
139839a53e0cSJaegeuk Kim }
139939a53e0cSJaegeuk Kim 
140039a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
140139a53e0cSJaegeuk Kim {
140239a53e0cSJaegeuk Kim 	int mask;
140339a53e0cSJaegeuk Kim 
140439a53e0cSJaegeuk Kim 	addr += (nr >> 3);
140539a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
140639a53e0cSJaegeuk Kim 	return mask & *addr;
140739a53e0cSJaegeuk Kim }
140839a53e0cSJaegeuk Kim 
1409a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
1410a66cdd98SJaegeuk Kim {
1411a66cdd98SJaegeuk Kim 	int mask;
1412a66cdd98SJaegeuk Kim 
1413a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1414a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1415a66cdd98SJaegeuk Kim 	*addr |= mask;
1416a66cdd98SJaegeuk Kim }
1417a66cdd98SJaegeuk Kim 
1418a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1419a66cdd98SJaegeuk Kim {
1420a66cdd98SJaegeuk Kim 	int mask;
1421a66cdd98SJaegeuk Kim 
1422a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
1423a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
1424a66cdd98SJaegeuk Kim 	*addr &= ~mask;
1425a66cdd98SJaegeuk Kim }
1426a66cdd98SJaegeuk Kim 
142752aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
142839a53e0cSJaegeuk Kim {
142939a53e0cSJaegeuk Kim 	int mask;
143039a53e0cSJaegeuk Kim 	int ret;
143139a53e0cSJaegeuk Kim 
143239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
143339a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
143439a53e0cSJaegeuk Kim 	ret = mask & *addr;
143539a53e0cSJaegeuk Kim 	*addr |= mask;
143639a53e0cSJaegeuk Kim 	return ret;
143739a53e0cSJaegeuk Kim }
143839a53e0cSJaegeuk Kim 
143952aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
144039a53e0cSJaegeuk Kim {
144139a53e0cSJaegeuk Kim 	int mask;
144239a53e0cSJaegeuk Kim 	int ret;
144339a53e0cSJaegeuk Kim 
144439a53e0cSJaegeuk Kim 	addr += (nr >> 3);
144539a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
144639a53e0cSJaegeuk Kim 	ret = mask & *addr;
144739a53e0cSJaegeuk Kim 	*addr &= ~mask;
144839a53e0cSJaegeuk Kim 	return ret;
144939a53e0cSJaegeuk Kim }
145039a53e0cSJaegeuk Kim 
1451c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
1452c6ac4c0eSGu Zheng {
1453c6ac4c0eSGu Zheng 	int mask;
1454c6ac4c0eSGu Zheng 
1455c6ac4c0eSGu Zheng 	addr += (nr >> 3);
1456c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
1457c6ac4c0eSGu Zheng 	*addr ^= mask;
1458c6ac4c0eSGu Zheng }
1459c6ac4c0eSGu Zheng 
146039a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
146139a53e0cSJaegeuk Kim enum {
146239a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
1463b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
1464ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
146539a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
146639a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
146739a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
1468c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
1469699489bbSJaegeuk Kim 	FI_UPDATE_DIR,		/* should update inode block for consistency */
147074d0b917SJaegeuk Kim 	FI_DELAY_IPUT,		/* used for the recovery */
1471c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
1472444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
14731001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
147434d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
1475fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
1476fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
147788b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
147888b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
147902a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
14803c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
14811e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
1482b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
1483510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
1484d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
1485c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
148639a53e0cSJaegeuk Kim };
148739a53e0cSJaegeuk Kim 
148839a53e0cSJaegeuk Kim static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
148939a53e0cSJaegeuk Kim {
149061e0f2d0SJaegeuk Kim 	if (!test_bit(flag, &fi->flags))
149139a53e0cSJaegeuk Kim 		set_bit(flag, &fi->flags);
149239a53e0cSJaegeuk Kim }
149339a53e0cSJaegeuk Kim 
149439a53e0cSJaegeuk Kim static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
149539a53e0cSJaegeuk Kim {
149639a53e0cSJaegeuk Kim 	return test_bit(flag, &fi->flags);
149739a53e0cSJaegeuk Kim }
149839a53e0cSJaegeuk Kim 
149939a53e0cSJaegeuk Kim static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
150039a53e0cSJaegeuk Kim {
150161e0f2d0SJaegeuk Kim 	if (test_bit(flag, &fi->flags))
150239a53e0cSJaegeuk Kim 		clear_bit(flag, &fi->flags);
150339a53e0cSJaegeuk Kim }
150439a53e0cSJaegeuk Kim 
150539a53e0cSJaegeuk Kim static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
150639a53e0cSJaegeuk Kim {
150739a53e0cSJaegeuk Kim 	fi->i_acl_mode = mode;
150839a53e0cSJaegeuk Kim 	set_inode_flag(fi, FI_ACL_MODE);
150939a53e0cSJaegeuk Kim }
151039a53e0cSJaegeuk Kim 
1511444c580fSJaegeuk Kim static inline void get_inline_info(struct f2fs_inode_info *fi,
1512444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1513444c580fSJaegeuk Kim {
1514444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
1515444c580fSJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_XATTR);
15161001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
15171001b347SHuajun Li 		set_inode_flag(fi, FI_INLINE_DATA);
151834d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
151934d67debSChao Yu 		set_inode_flag(fi, FI_INLINE_DENTRY);
1520b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
1521b3d208f9SJaegeuk Kim 		set_inode_flag(fi, FI_DATA_EXIST);
1522510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
1523510022a8SJaegeuk Kim 		set_inode_flag(fi, FI_INLINE_DOTS);
1524444c580fSJaegeuk Kim }
1525444c580fSJaegeuk Kim 
1526444c580fSJaegeuk Kim static inline void set_raw_inline(struct f2fs_inode_info *fi,
1527444c580fSJaegeuk Kim 					struct f2fs_inode *ri)
1528444c580fSJaegeuk Kim {
1529444c580fSJaegeuk Kim 	ri->i_inline = 0;
1530444c580fSJaegeuk Kim 
1531444c580fSJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_XATTR))
1532444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
15331001b347SHuajun Li 	if (is_inode_flag_set(fi, FI_INLINE_DATA))
15341001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
153534d67debSChao Yu 	if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
153634d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
1537b3d208f9SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_DATA_EXIST))
1538b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
1539510022a8SJaegeuk Kim 	if (is_inode_flag_set(fi, FI_INLINE_DOTS))
1540510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
1541444c580fSJaegeuk Kim }
1542444c580fSJaegeuk Kim 
1543987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
1544987c7c31SChao Yu {
1545987c7c31SChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
1546987c7c31SChao Yu }
1547987c7c31SChao Yu 
154881ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
1549de93653fSJaegeuk Kim {
155081ca7350SChao Yu 	if (f2fs_has_inline_xattr(inode))
1551de93653fSJaegeuk Kim 		return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1552de93653fSJaegeuk Kim 	return DEF_ADDRS_PER_INODE;
1553de93653fSJaegeuk Kim }
1554de93653fSJaegeuk Kim 
155565985d93SJaegeuk Kim static inline void *inline_xattr_addr(struct page *page)
155665985d93SJaegeuk Kim {
1557695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
155865985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
155965985d93SJaegeuk Kim 					F2FS_INLINE_XATTR_ADDRS]);
156065985d93SJaegeuk Kim }
156165985d93SJaegeuk Kim 
156265985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
156365985d93SJaegeuk Kim {
1564987c7c31SChao Yu 	if (f2fs_has_inline_xattr(inode))
156565985d93SJaegeuk Kim 		return F2FS_INLINE_XATTR_ADDRS << 2;
156665985d93SJaegeuk Kim 	else
156765985d93SJaegeuk Kim 		return 0;
156865985d93SJaegeuk Kim }
156965985d93SJaegeuk Kim 
15700dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
15710dbdc2aeSJaegeuk Kim {
15720dbdc2aeSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
15730dbdc2aeSJaegeuk Kim }
15740dbdc2aeSJaegeuk Kim 
1575b3d208f9SJaegeuk Kim static inline void f2fs_clear_inline_inode(struct inode *inode)
1576b3d208f9SJaegeuk Kim {
1577b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1578b3d208f9SJaegeuk Kim 	clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1579b3d208f9SJaegeuk Kim }
1580b3d208f9SJaegeuk Kim 
1581b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
1582b3d208f9SJaegeuk Kim {
1583b3d208f9SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
1584b3d208f9SJaegeuk Kim }
1585b3d208f9SJaegeuk Kim 
1586510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
1587510022a8SJaegeuk Kim {
1588510022a8SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
1589510022a8SJaegeuk Kim }
1590510022a8SJaegeuk Kim 
159188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
159288b88a66SJaegeuk Kim {
159388b88a66SJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
159488b88a66SJaegeuk Kim }
159588b88a66SJaegeuk Kim 
159602a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
159702a1335fSJaegeuk Kim {
159802a1335fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
159902a1335fSJaegeuk Kim }
160002a1335fSJaegeuk Kim 
16013c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
16023c6c2bebSJaegeuk Kim {
16033c6c2bebSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
16043c6c2bebSJaegeuk Kim }
16053c6c2bebSJaegeuk Kim 
16061e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
16071e84371fSJaegeuk Kim {
16081e84371fSJaegeuk Kim 	return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
16091e84371fSJaegeuk Kim }
16101e84371fSJaegeuk Kim 
16111001b347SHuajun Li static inline void *inline_data_addr(struct page *page)
16121001b347SHuajun Li {
1613695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
16141001b347SHuajun Li 	return (void *)&(ri->i_addr[1]);
16151001b347SHuajun Li }
16161001b347SHuajun Li 
161734d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
161834d67debSChao Yu {
161934d67debSChao Yu 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
162034d67debSChao Yu }
162134d67debSChao Yu 
16229486ba44SJaegeuk Kim static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
16239486ba44SJaegeuk Kim {
16249486ba44SJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
16259486ba44SJaegeuk Kim 		kunmap(page);
16269486ba44SJaegeuk Kim }
16279486ba44SJaegeuk Kim 
1628b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
1629b5492af7SJaegeuk Kim {
1630b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
1631b5492af7SJaegeuk Kim }
1632b5492af7SJaegeuk Kim 
1633b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
1634b5492af7SJaegeuk Kim {
1635b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
1636b5492af7SJaegeuk Kim }
1637b5492af7SJaegeuk Kim 
1638b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
1639b5492af7SJaegeuk Kim {
1640b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
1641b5492af7SJaegeuk Kim }
1642b5492af7SJaegeuk Kim 
164377888c1eSJaegeuk Kim static inline int f2fs_readonly(struct super_block *sb)
164477888c1eSJaegeuk Kim {
164577888c1eSJaegeuk Kim 	return sb->s_flags & MS_RDONLY;
164677888c1eSJaegeuk Kim }
164777888c1eSJaegeuk Kim 
16481e968fdfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
16491e968fdfSJaegeuk Kim {
16501e968fdfSJaegeuk Kim 	return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
16511e968fdfSJaegeuk Kim }
16521e968fdfSJaegeuk Kim 
1653744602cfSJaegeuk Kim static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
1654744602cfSJaegeuk Kim {
1655744602cfSJaegeuk Kim 	set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1656744602cfSJaegeuk Kim 	sbi->sb->s_flags |= MS_RDONLY;
1657744602cfSJaegeuk Kim }
1658744602cfSJaegeuk Kim 
1659eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
1660eaa693f4SJaegeuk Kim {
1661eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
1662eaa693f4SJaegeuk Kim 		return true;
1663eaa693f4SJaegeuk Kim 
1664eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
1665eaa693f4SJaegeuk Kim 		return true;
1666eaa693f4SJaegeuk Kim 
1667eaa693f4SJaegeuk Kim 	return false;
1668eaa693f4SJaegeuk Kim }
1669eaa693f4SJaegeuk Kim 
16703e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
16713e72f721SJaegeuk Kim {
16723e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
16733e72f721SJaegeuk Kim 			is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
16743e72f721SJaegeuk Kim 		return false;
16753e72f721SJaegeuk Kim 
1676886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
16773e72f721SJaegeuk Kim }
16783e72f721SJaegeuk Kim 
167939307a8eSJaegeuk Kim static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
168039307a8eSJaegeuk Kim {
168139307a8eSJaegeuk Kim 	void *ret;
168239307a8eSJaegeuk Kim 
168339307a8eSJaegeuk Kim 	ret = kmalloc(size, flags | __GFP_NOWARN);
168439307a8eSJaegeuk Kim 	if (!ret)
168539307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags, PAGE_KERNEL);
168639307a8eSJaegeuk Kim 	return ret;
168739307a8eSJaegeuk Kim }
168839307a8eSJaegeuk Kim 
168939307a8eSJaegeuk Kim static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
169039307a8eSJaegeuk Kim {
169139307a8eSJaegeuk Kim 	void *ret;
169239307a8eSJaegeuk Kim 
169339307a8eSJaegeuk Kim 	ret = kzalloc(size, flags | __GFP_NOWARN);
169439307a8eSJaegeuk Kim 	if (!ret)
169539307a8eSJaegeuk Kim 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
169639307a8eSJaegeuk Kim 	return ret;
169739307a8eSJaegeuk Kim }
169839307a8eSJaegeuk Kim 
1699a6dda0e6SChristoph Hellwig #define get_inode_mode(i) \
1700a6dda0e6SChristoph Hellwig 	((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
1701a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
1702a6dda0e6SChristoph Hellwig 
1703267378d4SChao Yu /* get offset of first page in next direct node */
170481ca7350SChao Yu #define PGOFS_OF_NEXT_DNODE(pgofs, inode)				\
170581ca7350SChao Yu 	((pgofs < ADDRS_PER_INODE(inode)) ? ADDRS_PER_INODE(inode) :	\
170681ca7350SChao Yu 	(pgofs - ADDRS_PER_INODE(inode) + ADDRS_PER_BLOCK) /	\
170781ca7350SChao Yu 	ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode))
1708267378d4SChao Yu 
170939a53e0cSJaegeuk Kim /*
171039a53e0cSJaegeuk Kim  * file.c
171139a53e0cSJaegeuk Kim  */
171239a53e0cSJaegeuk Kim int f2fs_sync_file(struct file *, loff_t, loff_t, int);
171339a53e0cSJaegeuk Kim void truncate_data_blocks(struct dnode_of_data *);
1714764aa3e9SJaegeuk Kim int truncate_blocks(struct inode *, u64, bool);
1715b0154891SChao Yu int f2fs_truncate(struct inode *, bool);
17162d4d9fb5SJaegeuk Kim int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
171739a53e0cSJaegeuk Kim int f2fs_setattr(struct dentry *, struct iattr *);
171839a53e0cSJaegeuk Kim int truncate_hole(struct inode *, pgoff_t, pgoff_t);
1719b292dcabSJaegeuk Kim int truncate_data_blocks_range(struct dnode_of_data *, int);
172039a53e0cSJaegeuk Kim long f2fs_ioctl(struct file *, unsigned int, unsigned long);
1721e9750824SNamjae Jeon long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
172239a53e0cSJaegeuk Kim 
172339a53e0cSJaegeuk Kim /*
172439a53e0cSJaegeuk Kim  * inode.c
172539a53e0cSJaegeuk Kim  */
172639a53e0cSJaegeuk Kim void f2fs_set_inode_flags(struct inode *);
172739a53e0cSJaegeuk Kim struct inode *f2fs_iget(struct super_block *, unsigned long);
17284660f9c0SJaegeuk Kim int try_to_free_nats(struct f2fs_sb_info *, int);
172912719ae1SJaegeuk Kim int update_inode(struct inode *, struct page *);
173012719ae1SJaegeuk Kim int update_inode_page(struct inode *);
173139a53e0cSJaegeuk Kim int f2fs_write_inode(struct inode *, struct writeback_control *);
173239a53e0cSJaegeuk Kim void f2fs_evict_inode(struct inode *);
173344c16156SJaegeuk Kim void handle_failed_inode(struct inode *);
173439a53e0cSJaegeuk Kim 
173539a53e0cSJaegeuk Kim /*
173639a53e0cSJaegeuk Kim  * namei.c
173739a53e0cSJaegeuk Kim  */
173839a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
173939a53e0cSJaegeuk Kim 
174039a53e0cSJaegeuk Kim /*
174139a53e0cSJaegeuk Kim  * dir.c
174239a53e0cSJaegeuk Kim  */
1743dbeacf02SChao Yu extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
1744510022a8SJaegeuk Kim void set_de_type(struct f2fs_dir_entry *, umode_t);
17456e22c691SJaegeuk Kim 
17466e22c691SJaegeuk Kim struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *,
17476e22c691SJaegeuk Kim 			f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
17487b3cd7d6SJaegeuk Kim bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
1749d8c6822aSJaegeuk Kim 			unsigned int, struct f2fs_str *);
1750062a3e7bSJaegeuk Kim void do_make_empty_dir(struct inode *, struct inode *,
1751062a3e7bSJaegeuk Kim 			struct f2fs_dentry_ptr *);
1752dbeacf02SChao Yu struct page *init_inode_metadata(struct inode *, struct inode *,
1753bce8d112SJaegeuk Kim 			const struct qstr *, struct page *);
1754dbeacf02SChao Yu void update_parent_metadata(struct inode *, struct inode *, unsigned int);
1755a82afa20SJaegeuk Kim int room_for_filename(const void *, int, int);
1756dbeacf02SChao Yu void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
175739a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
175839a53e0cSJaegeuk Kim 							struct page **);
175939a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
176039a53e0cSJaegeuk Kim ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
176139a53e0cSJaegeuk Kim void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
176239a53e0cSJaegeuk Kim 				struct page *, struct inode *);
1763e7d55452SJaegeuk Kim int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
1764510022a8SJaegeuk Kim void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
17653b4d732aSChao Yu 			const struct qstr *, f2fs_hash_t , unsigned int);
1766510022a8SJaegeuk Kim int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
1767510022a8SJaegeuk Kim 			umode_t);
1768dbeacf02SChao Yu void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
1769dbeacf02SChao Yu 							struct inode *);
1770b97a9b5dSJaegeuk Kim int f2fs_do_tmpfile(struct inode *, struct inode *);
177139a53e0cSJaegeuk Kim bool f2fs_empty_dir(struct inode *);
177239a53e0cSJaegeuk Kim 
1773b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
1774b7f7a5e0SAl Viro {
17752b0143b5SDavid Howells 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
1776510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
1777b7f7a5e0SAl Viro }
1778b7f7a5e0SAl Viro 
177939a53e0cSJaegeuk Kim /*
178039a53e0cSJaegeuk Kim  * super.c
178139a53e0cSJaegeuk Kim  */
1782c5bda1c8SChao Yu int f2fs_commit_super(struct f2fs_sb_info *, bool);
178339a53e0cSJaegeuk Kim int f2fs_sync_fs(struct super_block *, int);
1784a07ef784SNamjae Jeon extern __printf(3, 4)
1785a07ef784SNamjae Jeon void f2fs_msg(struct super_block *, const char *, const char *, ...);
1786984ec63cSShawn Lin int sanity_check_ckpt(struct f2fs_sb_info *sbi);
178739a53e0cSJaegeuk Kim 
178839a53e0cSJaegeuk Kim /*
178939a53e0cSJaegeuk Kim  * hash.c
179039a53e0cSJaegeuk Kim  */
1791eee6160fSGu Zheng f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
179239a53e0cSJaegeuk Kim 
179339a53e0cSJaegeuk Kim /*
179439a53e0cSJaegeuk Kim  * node.c
179539a53e0cSJaegeuk Kim  */
179639a53e0cSJaegeuk Kim struct dnode_of_data;
179739a53e0cSJaegeuk Kim struct node_info;
179839a53e0cSJaegeuk Kim 
17996fb03f3aSJaegeuk Kim bool available_free_memory(struct f2fs_sb_info *, int);
18002dcf51abSJaegeuk Kim int need_dentry_mark(struct f2fs_sb_info *, nid_t);
180188bd02c9SJaegeuk Kim bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
180288bd02c9SJaegeuk Kim bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
180339a53e0cSJaegeuk Kim void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
18043cf45747SChao Yu pgoff_t get_next_page_offset(struct dnode_of_data *, pgoff_t);
180539a53e0cSJaegeuk Kim int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
180639a53e0cSJaegeuk Kim int truncate_inode_blocks(struct inode *, pgoff_t);
18074f16fb0fSJaegeuk Kim int truncate_xattr_node(struct inode *, struct page *);
1808cfe58f9dSJaegeuk Kim int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
180913ec7297SChao Yu int remove_inode_page(struct inode *);
1810a014e037SJaegeuk Kim struct page *new_inode_page(struct inode *);
18118ae8f162SJaegeuk Kim struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
181239a53e0cSJaegeuk Kim void ra_node_page(struct f2fs_sb_info *, nid_t);
181339a53e0cSJaegeuk Kim struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
181439a53e0cSJaegeuk Kim struct page *get_node_page_ra(struct page *, int);
181539a53e0cSJaegeuk Kim void sync_inode_page(struct dnode_of_data *);
181639a53e0cSJaegeuk Kim int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
181739a53e0cSJaegeuk Kim bool alloc_nid(struct f2fs_sb_info *, nid_t *);
181839a53e0cSJaegeuk Kim void alloc_nid_done(struct f2fs_sb_info *, nid_t);
181939a53e0cSJaegeuk Kim void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
182031696580SChao Yu int try_to_free_nids(struct f2fs_sb_info *, int);
182170cfed88SChao Yu void recover_inline_xattr(struct inode *, struct page *);
18221c35a90eSJaegeuk Kim void recover_xattr_data(struct inode *, struct page *, block_t);
182339a53e0cSJaegeuk Kim int recover_inode_page(struct f2fs_sb_info *, struct page *);
182439a53e0cSJaegeuk Kim int restore_node_summary(struct f2fs_sb_info *, unsigned int,
182539a53e0cSJaegeuk Kim 				struct f2fs_summary_block *);
182639a53e0cSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *);
182739a53e0cSJaegeuk Kim int build_node_manager(struct f2fs_sb_info *);
182839a53e0cSJaegeuk Kim void destroy_node_manager(struct f2fs_sb_info *);
18296e6093a8SNamjae Jeon int __init create_node_manager_caches(void);
183039a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
183139a53e0cSJaegeuk Kim 
183239a53e0cSJaegeuk Kim /*
183339a53e0cSJaegeuk Kim  * segment.c
183439a53e0cSJaegeuk Kim  */
183588b88a66SJaegeuk Kim void register_inmem_page(struct inode *, struct page *);
183629b96b54SChao Yu void drop_inmem_pages(struct inode *);
183729b96b54SChao Yu int commit_inmem_pages(struct inode *);
18382c4db1a6SJaegeuk Kim void f2fs_balance_fs(struct f2fs_sb_info *, bool);
18394660f9c0SJaegeuk Kim void f2fs_balance_fs_bg(struct f2fs_sb_info *);
18406b4afdd7SJaegeuk Kim int f2fs_issue_flush(struct f2fs_sb_info *);
18412163d198SGu Zheng int create_flush_cmd_control(struct f2fs_sb_info *);
18422163d198SGu Zheng void destroy_flush_cmd_control(struct f2fs_sb_info *);
184339a53e0cSJaegeuk Kim void invalidate_blocks(struct f2fs_sb_info *, block_t);
18446e2c64adSJaegeuk Kim bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
18455e443818SJaegeuk Kim void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
1846836b5a63SJaegeuk Kim void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
18474b2fecc8SJaegeuk Kim void release_discard_addrs(struct f2fs_sb_info *);
1848e90c2d28SChao Yu bool discard_next_dnode(struct f2fs_sb_info *, block_t);
18493fa06d7bSChao Yu int npages_for_summary_flush(struct f2fs_sb_info *, bool);
185039a53e0cSJaegeuk Kim void allocate_new_segments(struct f2fs_sb_info *);
18514b2fecc8SJaegeuk Kim int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
185239a53e0cSJaegeuk Kim struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
1853381722d2SChao Yu void update_meta_page(struct f2fs_sb_info *, void *, block_t);
1854577e3495SJaegeuk Kim void write_meta_page(struct f2fs_sb_info *, struct page *);
185505ca3632SJaegeuk Kim void write_node_page(unsigned int, struct f2fs_io_info *);
185605ca3632SJaegeuk Kim void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
185705ca3632SJaegeuk Kim void rewrite_data_page(struct f2fs_io_info *);
18584356e48eSChao Yu void __f2fs_replace_block(struct f2fs_sb_info *, struct f2fs_summary *,
18594356e48eSChao Yu 					block_t, block_t, bool, bool);
1860528e3459SChao Yu void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
186128bc106bSChao Yu 				block_t, block_t, unsigned char, bool, bool);
1862bfad7c2dSJaegeuk Kim void allocate_data_block(struct f2fs_sb_info *, struct page *,
1863bfad7c2dSJaegeuk Kim 		block_t, block_t *, struct f2fs_summary *, int);
1864fec1d657SJaegeuk Kim void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
186508b39fbdSChao Yu void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *, block_t);
186639a53e0cSJaegeuk Kim void write_data_summaries(struct f2fs_sb_info *, block_t);
186739a53e0cSJaegeuk Kim void write_node_summaries(struct f2fs_sb_info *, block_t);
1868dfc08a12SChao Yu int lookup_journal_in_cursum(struct f2fs_journal *, int, unsigned int, int);
18694b2fecc8SJaegeuk Kim void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
187039a53e0cSJaegeuk Kim int build_segment_manager(struct f2fs_sb_info *);
187139a53e0cSJaegeuk Kim void destroy_segment_manager(struct f2fs_sb_info *);
18727fd9e544SJaegeuk Kim int __init create_segment_manager_caches(void);
18737fd9e544SJaegeuk Kim void destroy_segment_manager_caches(void);
187439a53e0cSJaegeuk Kim 
187539a53e0cSJaegeuk Kim /*
187639a53e0cSJaegeuk Kim  * checkpoint.c
187739a53e0cSJaegeuk Kim  */
187839a53e0cSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
187939a53e0cSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
18802b947003SChao Yu struct page *get_tmp_page(struct f2fs_sb_info *, pgoff_t);
1881f0c9cadaSChao Yu bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
188226879fb1SChao Yu int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int, bool);
1883635aee1fSChao Yu void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
188439a53e0cSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
1885a49324f1SChao Yu void add_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1886a49324f1SChao Yu void remove_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1887a49324f1SChao Yu void release_ino_entry(struct f2fs_sb_info *);
1888fff04f90SJaegeuk Kim bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
1889cbd56e7dSJaegeuk Kim int acquire_orphan_inode(struct f2fs_sb_info *);
1890cbd56e7dSJaegeuk Kim void release_orphan_inode(struct f2fs_sb_info *);
189139a53e0cSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *, nid_t);
189239a53e0cSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
18938c14bfadSChao Yu int recover_orphan_inodes(struct f2fs_sb_info *);
189439a53e0cSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *);
1895a7ffdbe2SJaegeuk Kim void update_dirty_page(struct inode *, struct page *);
18965deb8267SJaegeuk Kim void add_dirty_dir_inode(struct inode *);
1897c227f912SChao Yu void remove_dirty_inode(struct inode *);
18986d5a1495SChao Yu int sync_dirty_inodes(struct f2fs_sb_info *, enum inode_type);
1899c34f42e2SChao Yu int write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
19006451e041SJaegeuk Kim void init_ino_entry_info(struct f2fs_sb_info *);
19016e6093a8SNamjae Jeon int __init create_checkpoint_caches(void);
190239a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
190339a53e0cSJaegeuk Kim 
190439a53e0cSJaegeuk Kim /*
190539a53e0cSJaegeuk Kim  * data.c
190639a53e0cSJaegeuk Kim  */
1907458e6197SJaegeuk Kim void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
19080c3a5797SChao Yu void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *, struct inode *,
19090c3a5797SChao Yu 				struct page *, nid_t, enum page_type, int);
1910*406657ddSChao Yu void f2fs_flush_merged_bios(struct f2fs_sb_info *);
191105ca3632SJaegeuk Kim int f2fs_submit_page_bio(struct f2fs_io_info *);
191205ca3632SJaegeuk Kim void f2fs_submit_page_mbio(struct f2fs_io_info *);
1913216a620aSChao Yu void set_data_blkaddr(struct dnode_of_data *);
1914f28b3434SChao Yu void f2fs_update_data_blkaddr(struct dnode_of_data *, block_t);
191539a53e0cSJaegeuk Kim int reserve_new_block(struct dnode_of_data *);
1916759af1c9SFan Li int f2fs_get_block(struct dnode_of_data *, pgoff_t);
1917b439b103SJaegeuk Kim ssize_t f2fs_preallocate_blocks(struct kiocb *, struct iov_iter *);
1918b600965cSHuajun Li int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
1919a56c7c6fSJaegeuk Kim struct page *get_read_data_page(struct inode *, pgoff_t, int, bool);
192043f3eae1SJaegeuk Kim struct page *find_data_page(struct inode *, pgoff_t);
1921a56c7c6fSJaegeuk Kim struct page *get_lock_data_page(struct inode *, pgoff_t, bool);
192264aa7ed9SJaegeuk Kim struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
192305ca3632SJaegeuk Kim int do_write_data_page(struct f2fs_io_info *);
1924d323d005SChao Yu int f2fs_map_blocks(struct inode *, struct f2fs_map_blocks *, int, int);
19259ab70134SJaegeuk Kim int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
1926487261f3SChao Yu void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
1927487261f3SChao Yu int f2fs_release_page(struct page *, gfp_t);
192839a53e0cSJaegeuk Kim 
192939a53e0cSJaegeuk Kim /*
193039a53e0cSJaegeuk Kim  * gc.c
193139a53e0cSJaegeuk Kim  */
193239a53e0cSJaegeuk Kim int start_gc_thread(struct f2fs_sb_info *);
193339a53e0cSJaegeuk Kim void stop_gc_thread(struct f2fs_sb_info *);
193481ca7350SChao Yu block_t start_bidx_of_node(unsigned int, struct inode *);
1935d530d4d8SChao Yu int f2fs_gc(struct f2fs_sb_info *, bool);
193639a53e0cSJaegeuk Kim void build_gc_manager(struct f2fs_sb_info *);
193739a53e0cSJaegeuk Kim 
193839a53e0cSJaegeuk Kim /*
193939a53e0cSJaegeuk Kim  * recovery.c
194039a53e0cSJaegeuk Kim  */
19416ead1142SJaegeuk Kim int recover_fsync_data(struct f2fs_sb_info *);
194239a53e0cSJaegeuk Kim bool space_for_roll_forward(struct f2fs_sb_info *);
194339a53e0cSJaegeuk Kim 
194439a53e0cSJaegeuk Kim /*
194539a53e0cSJaegeuk Kim  * debug.c
194639a53e0cSJaegeuk Kim  */
194739a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
194839a53e0cSJaegeuk Kim struct f2fs_stat_info {
194939a53e0cSJaegeuk Kim 	struct list_head stat_list;
195039a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
195139a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
195239a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
19535b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
19545b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
1955c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
195633fbd510SChao Yu 	int ndirty_node, ndirty_meta;
195733fbd510SChao Yu 	int ndirty_dent, ndirty_dirs, ndirty_data, ndirty_files;
1958dd4e4b59SJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits, fnids;
195939a53e0cSJaegeuk Kim 	int total_count, utilization;
1960d5e8f6c9SChao Yu 	int bg_gc, inmem_pages, wb_pages;
1961d5e8f6c9SChao Yu 	int inline_xattr, inline_inode, inline_dir;
196239a53e0cSJaegeuk Kim 	unsigned int valid_count, valid_node_count, valid_inode_count;
196339a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
196439a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
196539a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
196639a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
196742190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
196839a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
1969e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
197039a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
1971e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
197239a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
197339a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
197439a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
197539a53e0cSJaegeuk Kim 
197639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
197739a53e0cSJaegeuk Kim 	unsigned int block_count[2];
1978b9a2c252SChangman Lee 	unsigned int inplace_count;
19799edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
198039a53e0cSJaegeuk Kim };
198139a53e0cSJaegeuk Kim 
1982963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1983963d4f7dSGu Zheng {
1984963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
1985963d4f7dSGu Zheng }
1986963d4f7dSGu Zheng 
1987942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
198842190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
198939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
1990dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
199133fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
199233fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
19935b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
19945b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
19955b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
19965b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
1997d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
1998d5e8f6c9SChao Yu 	do {								\
1999d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2000d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
2001d5e8f6c9SChao Yu 	} while (0)
2002d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
2003d5e8f6c9SChao Yu 	do {								\
2004d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
2005d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
2006d5e8f6c9SChao Yu 	} while (0)
20070dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
20080dbdc2aeSJaegeuk Kim 	do {								\
20090dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
201003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
20110dbdc2aeSJaegeuk Kim 	} while (0)
20120dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
20130dbdc2aeSJaegeuk Kim 	do {								\
20140dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
201503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
20160dbdc2aeSJaegeuk Kim 	} while (0)
20173289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
20183289c061SJaegeuk Kim 	do {								\
20193289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
202003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
20213289c061SJaegeuk Kim 	} while (0)
20223289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
20233289c061SJaegeuk Kim 	do {								\
20243289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
202503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
20263289c061SJaegeuk Kim 	} while (0)
2027dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
2028dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
2029dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
2030dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
2031b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
2032b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
2033e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
203439a53e0cSJaegeuk Kim 	do {								\
2035963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
203639a53e0cSJaegeuk Kim 		(si)->tot_segs++;					\
2037e1235983SChangman Lee 		if (type == SUM_TYPE_DATA) {				\
203839a53e0cSJaegeuk Kim 			si->data_segs++;				\
2039e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
2040e1235983SChangman Lee 		} else {						\
204139a53e0cSJaegeuk Kim 			si->node_segs++;				\
2042e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
2043e1235983SChangman Lee 		}							\
204439a53e0cSJaegeuk Kim 	} while (0)
204539a53e0cSJaegeuk Kim 
204639a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
204739a53e0cSJaegeuk Kim 	(si->tot_blks += (blks))
204839a53e0cSJaegeuk Kim 
2049e1235983SChangman Lee #define stat_inc_data_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->data_blks += (blks);				\
2054e1235983SChangman Lee 		si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0;	\
205539a53e0cSJaegeuk Kim 	} while (0)
205639a53e0cSJaegeuk Kim 
2057e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
205839a53e0cSJaegeuk Kim 	do {								\
2059963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
206039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
206139a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
2062e1235983SChangman Lee 		si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0;	\
206339a53e0cSJaegeuk Kim 	} while (0)
206439a53e0cSJaegeuk Kim 
206539a53e0cSJaegeuk Kim int f2fs_build_stats(struct f2fs_sb_info *);
206639a53e0cSJaegeuk Kim void f2fs_destroy_stats(struct f2fs_sb_info *);
2067787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
20684589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
206939a53e0cSJaegeuk Kim #else
2070942e0be6SChangman Lee #define stat_inc_cp_count(si)
207142190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)
207239a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
2073dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(si)
207433fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)
207533fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)
2076dcdfff65SJaegeuk Kim #define stat_inc_total_hit(sb)
2077029e13ccSChao Yu #define stat_inc_rbtree_node_hit(sb)
207891c481ffSChao Yu #define stat_inc_largest_node_hit(sbi)
207991c481ffSChao Yu #define stat_inc_cached_node_hit(sbi)
2080d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)
2081d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)
20820dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)
20830dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)
20843289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)
20853289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)
2086dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)
2087dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)
2088b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)
2089e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)
209039a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
2091e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)
2092e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)
209339a53e0cSJaegeuk Kim 
209439a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
209539a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2096787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
20974589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
209839a53e0cSJaegeuk Kim #endif
209939a53e0cSJaegeuk Kim 
210039a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
210139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
210239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
210339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
210439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
210539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
210639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
210739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
2108cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
210939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
211029e7043fSJaegeuk Kim extern struct kmem_cache *inode_entry_slab;
21111001b347SHuajun Li 
2112e18c65b2SHuajun Li /*
2113e18c65b2SHuajun Li  * inline.c
2114e18c65b2SHuajun Li  */
211501b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *);
211601b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *);
2117b3d208f9SJaegeuk Kim void read_inline_data(struct page *, struct page *);
21180bfcfccaSChao Yu bool truncate_inline_inode(struct page *, u64);
2119e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *, struct page *);
2120b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
2121b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *);
2122b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *, struct page *);
21230342fd30SJaegeuk Kim bool recover_inline_data(struct inode *, struct page *);
21246e22c691SJaegeuk Kim struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
21256e22c691SJaegeuk Kim 				struct f2fs_filename *, struct page **);
2126201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
2127201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
2128510022a8SJaegeuk Kim int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
2129510022a8SJaegeuk Kim 						nid_t, umode_t);
2130201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
2131201a05beSChao Yu 						struct inode *, struct inode *);
2132201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *);
2133d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *, struct dir_context *,
2134d8c6822aSJaegeuk Kim 						struct f2fs_str *);
213567f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *,
213667f8cf3cSJaegeuk Kim 		struct fiemap_extent_info *, __u64, __u64);
2137cde4de12SJaegeuk Kim 
2138cde4de12SJaegeuk Kim /*
21392658e50dSJaegeuk Kim  * shrinker.c
21402658e50dSJaegeuk Kim  */
21412658e50dSJaegeuk Kim unsigned long f2fs_shrink_count(struct shrinker *, struct shrink_control *);
21422658e50dSJaegeuk Kim unsigned long f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
21432658e50dSJaegeuk Kim void f2fs_join_shrinker(struct f2fs_sb_info *);
21442658e50dSJaegeuk Kim void f2fs_leave_shrinker(struct f2fs_sb_info *);
21452658e50dSJaegeuk Kim 
21462658e50dSJaegeuk Kim /*
2147a28ef1f5SChao Yu  * extent_cache.c
2148a28ef1f5SChao Yu  */
2149a28ef1f5SChao Yu unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
2150ed3d1256SJaegeuk Kim bool f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
2151a28ef1f5SChao Yu unsigned int f2fs_destroy_extent_node(struct inode *);
2152a28ef1f5SChao Yu void f2fs_destroy_extent_tree(struct inode *);
2153a28ef1f5SChao Yu bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
2154a28ef1f5SChao Yu void f2fs_update_extent_cache(struct dnode_of_data *);
215519b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
215619b2c30dSChao Yu 						pgoff_t, block_t, unsigned int);
2157a28ef1f5SChao Yu void init_extent_cache_info(struct f2fs_sb_info *);
2158a28ef1f5SChao Yu int __init create_extent_cache(void);
2159a28ef1f5SChao Yu void destroy_extent_cache(void);
2160a28ef1f5SChao Yu 
2161a28ef1f5SChao Yu /*
2162cde4de12SJaegeuk Kim  * crypto support
2163cde4de12SJaegeuk Kim  */
2164cde4de12SJaegeuk Kim static inline int f2fs_encrypted_inode(struct inode *inode)
2165cde4de12SJaegeuk Kim {
2166cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2167cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
2168cde4de12SJaegeuk Kim #else
2169cde4de12SJaegeuk Kim 	return 0;
2170cde4de12SJaegeuk Kim #endif
2171cde4de12SJaegeuk Kim }
2172cde4de12SJaegeuk Kim 
2173cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
2174cde4de12SJaegeuk Kim {
2175cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2176cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
2177cde4de12SJaegeuk Kim #endif
2178cde4de12SJaegeuk Kim }
2179cde4de12SJaegeuk Kim 
2180cde4de12SJaegeuk Kim static inline bool f2fs_bio_encrypted(struct bio *bio)
2181cde4de12SJaegeuk Kim {
2182cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2183cde4de12SJaegeuk Kim 	return unlikely(bio->bi_private != NULL);
2184cde4de12SJaegeuk Kim #else
2185cde4de12SJaegeuk Kim 	return false;
2186cde4de12SJaegeuk Kim #endif
2187cde4de12SJaegeuk Kim }
2188cde4de12SJaegeuk Kim 
2189cde4de12SJaegeuk Kim static inline int f2fs_sb_has_crypto(struct super_block *sb)
2190cde4de12SJaegeuk Kim {
2191cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2192cde4de12SJaegeuk Kim 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
2193cde4de12SJaegeuk Kim #else
2194cde4de12SJaegeuk Kim 	return 0;
2195cde4de12SJaegeuk Kim #endif
2196cde4de12SJaegeuk Kim }
2197f424f664SJaegeuk Kim 
2198fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
2199fcc85a4dSJaegeuk Kim {
2200fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
2201886f56f9SAl Viro 	umode_t mode = inode->i_mode;
2202fcc85a4dSJaegeuk Kim 
2203fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2204fcc85a4dSJaegeuk Kim #else
2205fcc85a4dSJaegeuk Kim 	return 0;
2206fcc85a4dSJaegeuk Kim #endif
2207fcc85a4dSJaegeuk Kim }
2208fcc85a4dSJaegeuk Kim 
2209f424f664SJaegeuk Kim /* crypto_policy.c */
2210f424f664SJaegeuk Kim int f2fs_is_child_context_consistent_with_parent(struct inode *,
2211f424f664SJaegeuk Kim 							struct inode *);
2212f424f664SJaegeuk Kim int f2fs_inherit_context(struct inode *, struct inode *, struct page *);
2213f424f664SJaegeuk Kim int f2fs_process_policy(const struct f2fs_encryption_policy *, struct inode *);
2214f424f664SJaegeuk Kim int f2fs_get_policy(struct inode *, struct f2fs_encryption_policy *);
221557e5055bSJaegeuk Kim 
221657e5055bSJaegeuk Kim /* crypt.c */
22178bacf6deSJaegeuk Kim extern struct kmem_cache *f2fs_crypt_info_cachep;
221857e5055bSJaegeuk Kim bool f2fs_valid_contents_enc_mode(uint32_t);
221957e5055bSJaegeuk Kim uint32_t f2fs_validate_encryption_key_size(uint32_t, uint32_t);
222057e5055bSJaegeuk Kim struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *);
222157e5055bSJaegeuk Kim void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *);
222257e5055bSJaegeuk Kim struct page *f2fs_encrypt(struct inode *, struct page *);
2223ce855a3bSJaegeuk Kim int f2fs_decrypt(struct page *);
222457e5055bSJaegeuk Kim void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
222557e5055bSJaegeuk Kim 
22260adda907SJaegeuk Kim /* crypto_key.c */
222726bf3dc7SJaegeuk Kim void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
22280adda907SJaegeuk Kim int _f2fs_get_encryption_info(struct inode *inode);
22290adda907SJaegeuk Kim 
22306b3bd08fSJaegeuk Kim /* crypto_fname.c */
22316b3bd08fSJaegeuk Kim bool f2fs_valid_filenames_enc_mode(uint32_t);
22326b3bd08fSJaegeuk Kim u32 f2fs_fname_crypto_round_up(u32, u32);
2233922ec355SChao Yu unsigned f2fs_fname_encrypted_size(struct inode *, u32);
22346b3bd08fSJaegeuk Kim int f2fs_fname_crypto_alloc_buffer(struct inode *, u32, struct f2fs_str *);
22356b3bd08fSJaegeuk Kim int f2fs_fname_disk_to_usr(struct inode *, f2fs_hash_t *,
22366b3bd08fSJaegeuk Kim 			const struct f2fs_str *, struct f2fs_str *);
22376b3bd08fSJaegeuk Kim int f2fs_fname_usr_to_disk(struct inode *, const struct qstr *,
22386b3bd08fSJaegeuk Kim 			struct f2fs_str *);
22396b3bd08fSJaegeuk Kim 
224057e5055bSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
224157e5055bSJaegeuk Kim void f2fs_restore_and_release_control_page(struct page **);
224257e5055bSJaegeuk Kim void f2fs_restore_control_page(struct page *);
224357e5055bSJaegeuk Kim 
2244cfc4d971SJaegeuk Kim int __init f2fs_init_crypto(void);
2245cfc4d971SJaegeuk Kim int f2fs_crypto_initialize(void);
224657e5055bSJaegeuk Kim void f2fs_exit_crypto(void);
22470adda907SJaegeuk Kim 
22480adda907SJaegeuk Kim int f2fs_has_encryption_key(struct inode *);
22490adda907SJaegeuk Kim 
22500adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *inode)
22510adda907SJaegeuk Kim {
22520adda907SJaegeuk Kim 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
22530adda907SJaegeuk Kim 
22540adda907SJaegeuk Kim 	if (!ci ||
22550adda907SJaegeuk Kim 		(ci->ci_keyring_key &&
22560adda907SJaegeuk Kim 		 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
22570adda907SJaegeuk Kim 					       (1 << KEY_FLAG_REVOKED) |
22580adda907SJaegeuk Kim 					       (1 << KEY_FLAG_DEAD)))))
22590adda907SJaegeuk Kim 		return _f2fs_get_encryption_info(inode);
22600adda907SJaegeuk Kim 	return 0;
22610adda907SJaegeuk Kim }
22626b3bd08fSJaegeuk Kim 
22636b3bd08fSJaegeuk Kim void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
22646b3bd08fSJaegeuk Kim int f2fs_fname_setup_filename(struct inode *, const struct qstr *,
22656b3bd08fSJaegeuk Kim 				int lookup, struct f2fs_filename *);
22666b3bd08fSJaegeuk Kim void f2fs_fname_free_filename(struct f2fs_filename *);
226757e5055bSJaegeuk Kim #else
226857e5055bSJaegeuk Kim static inline void f2fs_restore_and_release_control_page(struct page **p) { }
226957e5055bSJaegeuk Kim static inline void f2fs_restore_control_page(struct page *p) { }
227057e5055bSJaegeuk Kim 
2271cfc4d971SJaegeuk Kim static inline int __init f2fs_init_crypto(void) { return 0; }
227257e5055bSJaegeuk Kim static inline void f2fs_exit_crypto(void) { }
22730adda907SJaegeuk Kim 
22740adda907SJaegeuk Kim static inline int f2fs_has_encryption_key(struct inode *i) { return 0; }
22750adda907SJaegeuk Kim static inline int f2fs_get_encryption_info(struct inode *i) { return 0; }
22766b3bd08fSJaegeuk Kim static inline void f2fs_fname_crypto_free_buffer(struct f2fs_str *p) { }
22776b3bd08fSJaegeuk Kim 
22786b3bd08fSJaegeuk Kim static inline int f2fs_fname_setup_filename(struct inode *dir,
22796b3bd08fSJaegeuk Kim 					const struct qstr *iname,
22806b3bd08fSJaegeuk Kim 					int lookup, struct f2fs_filename *fname)
22816b3bd08fSJaegeuk Kim {
22826b3bd08fSJaegeuk Kim 	memset(fname, 0, sizeof(struct f2fs_filename));
22836b3bd08fSJaegeuk Kim 	fname->usr_fname = iname;
22846b3bd08fSJaegeuk Kim 	fname->disk_name.name = (unsigned char *)iname->name;
22856b3bd08fSJaegeuk Kim 	fname->disk_name.len = iname->len;
22866b3bd08fSJaegeuk Kim 	return 0;
22876b3bd08fSJaegeuk Kim }
22886b3bd08fSJaegeuk Kim 
22896b3bd08fSJaegeuk Kim static inline void f2fs_fname_free_filename(struct f2fs_filename *fname) { }
229057e5055bSJaegeuk Kim #endif
229139a53e0cSJaegeuk Kim #endif
2292