xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 25ca923b2a766b9c93b63777ead351137533a623)
139a53e0cSJaegeuk 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>
2039a53e0cSJaegeuk Kim 
2139a53e0cSJaegeuk Kim /*
2239a53e0cSJaegeuk Kim  * For mount options
2339a53e0cSJaegeuk Kim  */
2439a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
2539a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
2639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
2739a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
2839a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
2939a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
3039a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
3139a53e0cSJaegeuk Kim 
3239a53e0cSJaegeuk Kim #define clear_opt(sbi, option)	(sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
3339a53e0cSJaegeuk Kim #define set_opt(sbi, option)	(sbi->mount_opt.opt |= F2FS_MOUNT_##option)
3439a53e0cSJaegeuk Kim #define test_opt(sbi, option)	(sbi->mount_opt.opt & F2FS_MOUNT_##option)
3539a53e0cSJaegeuk Kim 
3639a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
3739a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
3839a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
3939a53e0cSJaegeuk Kim 
4039a53e0cSJaegeuk Kim typedef u64 block_t;
4139a53e0cSJaegeuk Kim typedef u32 nid_t;
4239a53e0cSJaegeuk Kim 
4339a53e0cSJaegeuk Kim struct f2fs_mount_info {
4439a53e0cSJaegeuk Kim 	unsigned int	opt;
4539a53e0cSJaegeuk Kim };
4639a53e0cSJaegeuk Kim 
4739a53e0cSJaegeuk Kim static inline __u32 f2fs_crc32(void *buff, size_t len)
4839a53e0cSJaegeuk Kim {
4939a53e0cSJaegeuk Kim 	return crc32_le(F2FS_SUPER_MAGIC, buff, len);
5039a53e0cSJaegeuk Kim }
5139a53e0cSJaegeuk Kim 
5239a53e0cSJaegeuk Kim static inline bool f2fs_crc_valid(__u32 blk_crc, void *buff, size_t buff_size)
5339a53e0cSJaegeuk Kim {
5439a53e0cSJaegeuk Kim 	return f2fs_crc32(buff, buff_size) == blk_crc;
5539a53e0cSJaegeuk Kim }
5639a53e0cSJaegeuk Kim 
5739a53e0cSJaegeuk Kim /*
5839a53e0cSJaegeuk Kim  * For checkpoint manager
5939a53e0cSJaegeuk Kim  */
6039a53e0cSJaegeuk Kim enum {
6139a53e0cSJaegeuk Kim 	NAT_BITMAP,
6239a53e0cSJaegeuk Kim 	SIT_BITMAP
6339a53e0cSJaegeuk Kim };
6439a53e0cSJaegeuk Kim 
6539a53e0cSJaegeuk Kim /* for the list of orphan inodes */
6639a53e0cSJaegeuk Kim struct orphan_inode_entry {
6739a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
6839a53e0cSJaegeuk Kim 	nid_t ino;		/* inode number */
6939a53e0cSJaegeuk Kim };
7039a53e0cSJaegeuk Kim 
7139a53e0cSJaegeuk Kim /* for the list of directory inodes */
7239a53e0cSJaegeuk Kim struct dir_inode_entry {
7339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
7439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
7539a53e0cSJaegeuk Kim };
7639a53e0cSJaegeuk Kim 
7739a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
7839a53e0cSJaegeuk Kim struct fsync_inode_entry {
7939a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
8039a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
8139a53e0cSJaegeuk Kim 	block_t blkaddr;	/* block address locating the last inode */
8239a53e0cSJaegeuk Kim };
8339a53e0cSJaegeuk Kim 
8439a53e0cSJaegeuk Kim #define nats_in_cursum(sum)		(le16_to_cpu(sum->n_nats))
8539a53e0cSJaegeuk Kim #define sits_in_cursum(sum)		(le16_to_cpu(sum->n_sits))
8639a53e0cSJaegeuk Kim 
8739a53e0cSJaegeuk Kim #define nat_in_journal(sum, i)		(sum->nat_j.entries[i].ne)
8839a53e0cSJaegeuk Kim #define nid_in_journal(sum, i)		(sum->nat_j.entries[i].nid)
8939a53e0cSJaegeuk Kim #define sit_in_journal(sum, i)		(sum->sit_j.entries[i].se)
9039a53e0cSJaegeuk Kim #define segno_in_journal(sum, i)	(sum->sit_j.entries[i].segno)
9139a53e0cSJaegeuk Kim 
9239a53e0cSJaegeuk Kim static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
9339a53e0cSJaegeuk Kim {
9439a53e0cSJaegeuk Kim 	int before = nats_in_cursum(rs);
9539a53e0cSJaegeuk Kim 	rs->n_nats = cpu_to_le16(before + i);
9639a53e0cSJaegeuk Kim 	return before;
9739a53e0cSJaegeuk Kim }
9839a53e0cSJaegeuk Kim 
9939a53e0cSJaegeuk Kim static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
10039a53e0cSJaegeuk Kim {
10139a53e0cSJaegeuk Kim 	int before = sits_in_cursum(rs);
10239a53e0cSJaegeuk Kim 	rs->n_sits = cpu_to_le16(before + i);
10339a53e0cSJaegeuk Kim 	return before;
10439a53e0cSJaegeuk Kim }
10539a53e0cSJaegeuk Kim 
10639a53e0cSJaegeuk Kim /*
10739a53e0cSJaegeuk Kim  * For INODE and NODE manager
10839a53e0cSJaegeuk Kim  */
10939a53e0cSJaegeuk Kim #define XATTR_NODE_OFFSET	(-1)	/*
11039a53e0cSJaegeuk Kim 					 * store xattrs to one node block per
11139a53e0cSJaegeuk Kim 					 * file keeping -1 as its node offset to
11239a53e0cSJaegeuk Kim 					 * distinguish from index node blocks.
11339a53e0cSJaegeuk Kim 					 */
11439a53e0cSJaegeuk Kim #define RDONLY_NODE		1	/*
11539a53e0cSJaegeuk Kim 					 * specify a read-only mode when getting
11639a53e0cSJaegeuk Kim 					 * a node block. 0 is read-write mode.
11739a53e0cSJaegeuk Kim 					 * used by get_dnode_of_data().
11839a53e0cSJaegeuk Kim 					 */
11939a53e0cSJaegeuk Kim #define F2FS_LINK_MAX		32000	/* maximum link count per file */
12039a53e0cSJaegeuk Kim 
12139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
12239a53e0cSJaegeuk Kim struct extent_info {
12339a53e0cSJaegeuk Kim 	rwlock_t ext_lock;	/* rwlock for consistency */
12439a53e0cSJaegeuk Kim 	unsigned int fofs;	/* start offset in a file */
12539a53e0cSJaegeuk Kim 	u32 blk_addr;		/* start block address of the extent */
12639a53e0cSJaegeuk Kim 	unsigned int len;	/* lenth of the extent */
12739a53e0cSJaegeuk Kim };
12839a53e0cSJaegeuk Kim 
12939a53e0cSJaegeuk Kim /*
13039a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
13139a53e0cSJaegeuk Kim  */
13239a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
13339a53e0cSJaegeuk Kim 
13439a53e0cSJaegeuk Kim struct f2fs_inode_info {
13539a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
13639a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
13739a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
13839a53e0cSJaegeuk Kim 	unsigned int i_current_depth;	/* use only in directory structure */
13939a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
14039a53e0cSJaegeuk Kim 
14139a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
14239a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
14339a53e0cSJaegeuk Kim 	unsigned long long data_version;/* lastes version of data for fsync */
14439a53e0cSJaegeuk Kim 	atomic_t dirty_dents;		/* # of dirty dentry pages */
14539a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
14639a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
14739a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
14839a53e0cSJaegeuk Kim 	struct extent_info ext;		/* in-memory extent cache entry */
14939a53e0cSJaegeuk Kim };
15039a53e0cSJaegeuk Kim 
15139a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
15239a53e0cSJaegeuk Kim 					struct f2fs_extent i_ext)
15339a53e0cSJaegeuk Kim {
15439a53e0cSJaegeuk Kim 	write_lock(&ext->ext_lock);
15539a53e0cSJaegeuk Kim 	ext->fofs = le32_to_cpu(i_ext.fofs);
15639a53e0cSJaegeuk Kim 	ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
15739a53e0cSJaegeuk Kim 	ext->len = le32_to_cpu(i_ext.len);
15839a53e0cSJaegeuk Kim 	write_unlock(&ext->ext_lock);
15939a53e0cSJaegeuk Kim }
16039a53e0cSJaegeuk Kim 
16139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
16239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
16339a53e0cSJaegeuk Kim {
16439a53e0cSJaegeuk Kim 	read_lock(&ext->ext_lock);
16539a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
16639a53e0cSJaegeuk Kim 	i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
16739a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
16839a53e0cSJaegeuk Kim 	read_unlock(&ext->ext_lock);
16939a53e0cSJaegeuk Kim }
17039a53e0cSJaegeuk Kim 
17139a53e0cSJaegeuk Kim struct f2fs_nm_info {
17239a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
17339a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
17439a53e0cSJaegeuk Kim 	nid_t init_scan_nid;		/* the first nid to be scanned */
17539a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
17639a53e0cSJaegeuk Kim 
17739a53e0cSJaegeuk Kim 	/* NAT cache management */
17839a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
17939a53e0cSJaegeuk Kim 	rwlock_t nat_tree_lock;		/* protect nat_tree_lock */
18039a53e0cSJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
18139a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
18239a53e0cSJaegeuk Kim 	struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
18339a53e0cSJaegeuk Kim 
18439a53e0cSJaegeuk Kim 	/* free node ids management */
18539a53e0cSJaegeuk Kim 	struct list_head free_nid_list;	/* a list for free nids */
18639a53e0cSJaegeuk Kim 	spinlock_t free_nid_list_lock;	/* protect free nid list */
18739a53e0cSJaegeuk Kim 	unsigned int fcnt;		/* the number of free node id */
18839a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
18939a53e0cSJaegeuk Kim 
19039a53e0cSJaegeuk Kim 	/* for checkpoint */
19139a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
19239a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
19339a53e0cSJaegeuk Kim };
19439a53e0cSJaegeuk Kim 
19539a53e0cSJaegeuk Kim /*
19639a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
19739a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
19839a53e0cSJaegeuk Kim  * by the data offset in a file.
19939a53e0cSJaegeuk Kim  */
20039a53e0cSJaegeuk Kim struct dnode_of_data {
20139a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
20239a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
20339a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
20439a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
20539a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
20639a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
20739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
20839a53e0cSJaegeuk Kim };
20939a53e0cSJaegeuk Kim 
21039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
21139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
21239a53e0cSJaegeuk Kim {
21339a53e0cSJaegeuk Kim 	dn->inode = inode;
21439a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
21539a53e0cSJaegeuk Kim 	dn->node_page = npage;
21639a53e0cSJaegeuk Kim 	dn->nid = nid;
21739a53e0cSJaegeuk Kim 	dn->inode_page_locked = 0;
21839a53e0cSJaegeuk Kim }
21939a53e0cSJaegeuk Kim 
22039a53e0cSJaegeuk Kim /*
22139a53e0cSJaegeuk Kim  * For SIT manager
22239a53e0cSJaegeuk Kim  *
22339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
22439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
22539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
22639a53e0cSJaegeuk Kim  * respectively.
22739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
22839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
22939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
23039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
23139a53e0cSJaegeuk Kim  * data and 8 for node logs.
23239a53e0cSJaegeuk Kim  */
23339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
23439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
23539a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
23639a53e0cSJaegeuk Kim 
23739a53e0cSJaegeuk Kim enum {
23839a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
23939a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
24039a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
24139a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
24239a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
24339a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
24439a53e0cSJaegeuk Kim 	NO_CHECK_TYPE
24539a53e0cSJaegeuk Kim };
24639a53e0cSJaegeuk Kim 
24739a53e0cSJaegeuk Kim struct f2fs_sm_info {
24839a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
24939a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
25039a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
25139a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
25239a53e0cSJaegeuk Kim 
25339a53e0cSJaegeuk Kim 	struct list_head wblist_head;	/* list of under-writeback pages */
25439a53e0cSJaegeuk Kim 	spinlock_t wblist_lock;		/* lock for checkpoint */
25539a53e0cSJaegeuk Kim 
25639a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
25739a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
25839a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
25939a53e0cSJaegeuk Kim 
26039a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
26139a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
26239a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
26339a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
26439a53e0cSJaegeuk Kim };
26539a53e0cSJaegeuk Kim 
26639a53e0cSJaegeuk Kim /*
26739a53e0cSJaegeuk Kim  * For directory operation
26839a53e0cSJaegeuk Kim  */
26939a53e0cSJaegeuk Kim #define	NODE_DIR1_BLOCK		(ADDRS_PER_INODE + 1)
27039a53e0cSJaegeuk Kim #define	NODE_DIR2_BLOCK		(ADDRS_PER_INODE + 2)
27139a53e0cSJaegeuk Kim #define	NODE_IND1_BLOCK		(ADDRS_PER_INODE + 3)
27239a53e0cSJaegeuk Kim #define	NODE_IND2_BLOCK		(ADDRS_PER_INODE + 4)
27339a53e0cSJaegeuk Kim #define	NODE_DIND_BLOCK		(ADDRS_PER_INODE + 5)
27439a53e0cSJaegeuk Kim 
27539a53e0cSJaegeuk Kim /*
27639a53e0cSJaegeuk Kim  * For superblock
27739a53e0cSJaegeuk Kim  */
27839a53e0cSJaegeuk Kim /*
27939a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
28039a53e0cSJaegeuk Kim  *
28139a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
28239a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
28339a53e0cSJaegeuk Kim  */
28439a53e0cSJaegeuk Kim enum count_type {
28539a53e0cSJaegeuk Kim 	F2FS_WRITEBACK,
28639a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
28739a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
28839a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
28939a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
29039a53e0cSJaegeuk Kim };
29139a53e0cSJaegeuk Kim 
29239a53e0cSJaegeuk Kim /*
29339a53e0cSJaegeuk Kim  * FS_LOCK nesting subclasses for the lock validator:
29439a53e0cSJaegeuk Kim  *
29539a53e0cSJaegeuk Kim  * The locking order between these classes is
29639a53e0cSJaegeuk Kim  * RENAME -> DENTRY_OPS -> DATA_WRITE -> DATA_NEW
29739a53e0cSJaegeuk Kim  *    -> DATA_TRUNC -> NODE_WRITE -> NODE_NEW -> NODE_TRUNC
29839a53e0cSJaegeuk Kim  */
29939a53e0cSJaegeuk Kim enum lock_type {
30039a53e0cSJaegeuk Kim 	RENAME,		/* for renaming operations */
30139a53e0cSJaegeuk Kim 	DENTRY_OPS,	/* for directory operations */
30239a53e0cSJaegeuk Kim 	DATA_WRITE,	/* for data write */
30339a53e0cSJaegeuk Kim 	DATA_NEW,	/* for data allocation */
30439a53e0cSJaegeuk Kim 	DATA_TRUNC,	/* for data truncate */
30539a53e0cSJaegeuk Kim 	NODE_NEW,	/* for node allocation */
30639a53e0cSJaegeuk Kim 	NODE_TRUNC,	/* for node truncate */
30739a53e0cSJaegeuk Kim 	NODE_WRITE,	/* for node write */
30839a53e0cSJaegeuk Kim 	NR_LOCK_TYPE,
30939a53e0cSJaegeuk Kim };
31039a53e0cSJaegeuk Kim 
31139a53e0cSJaegeuk Kim /*
31239a53e0cSJaegeuk Kim  * The below are the page types of bios used in submti_bio().
31339a53e0cSJaegeuk Kim  * The available types are:
31439a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
31539a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
31639a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
31739a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
31839a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
31939a53e0cSJaegeuk Kim  *			with waiting the bio's completion
32039a53e0cSJaegeuk Kim  * ...			Only can be used with META.
32139a53e0cSJaegeuk Kim  */
32239a53e0cSJaegeuk Kim enum page_type {
32339a53e0cSJaegeuk Kim 	DATA,
32439a53e0cSJaegeuk Kim 	NODE,
32539a53e0cSJaegeuk Kim 	META,
32639a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
32739a53e0cSJaegeuk Kim 	META_FLUSH,
32839a53e0cSJaegeuk Kim };
32939a53e0cSJaegeuk Kim 
33039a53e0cSJaegeuk Kim struct f2fs_sb_info {
33139a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
33239a53e0cSJaegeuk Kim 	struct buffer_head *raw_super_buf;	/* buffer head of raw sb */
33339a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
33439a53e0cSJaegeuk Kim 	int s_dirty;				/* dirty flag for checkpoint */
33539a53e0cSJaegeuk Kim 
33639a53e0cSJaegeuk Kim 	/* for node-related operations */
33739a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
33839a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
33939a53e0cSJaegeuk Kim 
34039a53e0cSJaegeuk Kim 	/* for segment-related operations */
34139a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
34239a53e0cSJaegeuk Kim 	struct bio *bio[NR_PAGE_TYPE];		/* bios to merge */
34339a53e0cSJaegeuk Kim 	sector_t last_block_in_bio[NR_PAGE_TYPE];	/* last block number */
34439a53e0cSJaegeuk Kim 	struct rw_semaphore bio_sem;		/* IO semaphore */
34539a53e0cSJaegeuk Kim 
34639a53e0cSJaegeuk Kim 	/* for checkpoint */
34739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
34839a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
34939a53e0cSJaegeuk Kim 	struct mutex cp_mutex;			/* for checkpoint procedure */
35039a53e0cSJaegeuk Kim 	struct mutex fs_lock[NR_LOCK_TYPE];	/* for blocking FS operations */
35139a53e0cSJaegeuk Kim 	struct mutex write_inode;		/* mutex for write inode */
35239a53e0cSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
35339a53e0cSJaegeuk Kim 	int por_doing;				/* recovery is doing or not */
35439a53e0cSJaegeuk Kim 
35539a53e0cSJaegeuk Kim 	/* for orphan inode management */
35639a53e0cSJaegeuk Kim 	struct list_head orphan_inode_list;	/* orphan inode list */
35739a53e0cSJaegeuk Kim 	struct mutex orphan_inode_mutex;	/* for orphan inode list */
35839a53e0cSJaegeuk Kim 	unsigned int n_orphans;			/* # of orphan inodes */
35939a53e0cSJaegeuk Kim 
36039a53e0cSJaegeuk Kim 	/* for directory inode management */
36139a53e0cSJaegeuk Kim 	struct list_head dir_inode_list;	/* dir inode list */
36239a53e0cSJaegeuk Kim 	spinlock_t dir_inode_lock;		/* for dir inode list lock */
36339a53e0cSJaegeuk Kim 	unsigned int n_dirty_dirs;		/* # of dir inodes */
36439a53e0cSJaegeuk Kim 
36539a53e0cSJaegeuk Kim 	/* basic file system units */
36639a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
36739a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
36839a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
36939a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
37039a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
37139a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
37239a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
37339a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
37439a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
37539a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
37639a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
37739a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
37839a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
37939a53e0cSJaegeuk Kim 	unsigned int total_valid_inode_count;	/* valid inode count */
38039a53e0cSJaegeuk Kim 	int active_logs;			/* # of active logs */
38139a53e0cSJaegeuk Kim 
38239a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
38339a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
38439a53e0cSJaegeuk Kim 	block_t alloc_valid_block_count;	/* # of allocated blocks */
38539a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
38639a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
38739a53e0cSJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];	/* # of pages, see count_type */
38839a53e0cSJaegeuk Kim 
38939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
39039a53e0cSJaegeuk Kim 
39139a53e0cSJaegeuk Kim 	/* for cleaning operations */
39239a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
39339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
39439a53e0cSJaegeuk Kim 
39539a53e0cSJaegeuk Kim 	/*
39639a53e0cSJaegeuk Kim 	 * for stat information.
39739a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
39839a53e0cSJaegeuk Kim 	 */
39939a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
40039a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
40139a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
40239a53e0cSJaegeuk Kim 	unsigned int last_victim[2];		/* last victim segment # */
40339a53e0cSJaegeuk Kim 	int total_hit_ext, read_hit_ext;	/* extent cache hit ratio */
40439a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
40539a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
40639a53e0cSJaegeuk Kim };
40739a53e0cSJaegeuk Kim 
40839a53e0cSJaegeuk Kim /*
40939a53e0cSJaegeuk Kim  * Inline functions
41039a53e0cSJaegeuk Kim  */
41139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
41239a53e0cSJaegeuk Kim {
41339a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
41439a53e0cSJaegeuk Kim }
41539a53e0cSJaegeuk Kim 
41639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
41739a53e0cSJaegeuk Kim {
41839a53e0cSJaegeuk Kim 	return sb->s_fs_info;
41939a53e0cSJaegeuk Kim }
42039a53e0cSJaegeuk Kim 
42139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
42239a53e0cSJaegeuk Kim {
42339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
42439a53e0cSJaegeuk Kim }
42539a53e0cSJaegeuk Kim 
42639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
42739a53e0cSJaegeuk Kim {
42839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
42939a53e0cSJaegeuk Kim }
43039a53e0cSJaegeuk Kim 
43139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
43239a53e0cSJaegeuk Kim {
43339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
43439a53e0cSJaegeuk Kim }
43539a53e0cSJaegeuk Kim 
43639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
43739a53e0cSJaegeuk Kim {
43839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
43939a53e0cSJaegeuk Kim }
44039a53e0cSJaegeuk Kim 
44139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
44239a53e0cSJaegeuk Kim {
44339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
44439a53e0cSJaegeuk Kim }
44539a53e0cSJaegeuk Kim 
44639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
44739a53e0cSJaegeuk Kim {
44839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
44939a53e0cSJaegeuk Kim }
45039a53e0cSJaegeuk Kim 
45139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
45239a53e0cSJaegeuk Kim {
45339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
45439a53e0cSJaegeuk Kim }
45539a53e0cSJaegeuk Kim 
45639a53e0cSJaegeuk Kim static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
45739a53e0cSJaegeuk Kim {
45839a53e0cSJaegeuk Kim 	sbi->s_dirty = 1;
45939a53e0cSJaegeuk Kim }
46039a53e0cSJaegeuk Kim 
46139a53e0cSJaegeuk Kim static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
46239a53e0cSJaegeuk Kim {
46339a53e0cSJaegeuk Kim 	sbi->s_dirty = 0;
46439a53e0cSJaegeuk Kim }
46539a53e0cSJaegeuk Kim 
466*25ca923bSJaegeuk Kim static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
467*25ca923bSJaegeuk Kim {
468*25ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
469*25ca923bSJaegeuk Kim 	return ckpt_flags & f;
470*25ca923bSJaegeuk Kim }
471*25ca923bSJaegeuk Kim 
472*25ca923bSJaegeuk Kim static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
473*25ca923bSJaegeuk Kim {
474*25ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
475*25ca923bSJaegeuk Kim 	ckpt_flags |= f;
476*25ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
477*25ca923bSJaegeuk Kim }
478*25ca923bSJaegeuk Kim 
479*25ca923bSJaegeuk Kim static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
480*25ca923bSJaegeuk Kim {
481*25ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
482*25ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
483*25ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
484*25ca923bSJaegeuk Kim }
485*25ca923bSJaegeuk Kim 
48639a53e0cSJaegeuk Kim static inline void mutex_lock_op(struct f2fs_sb_info *sbi, enum lock_type t)
48739a53e0cSJaegeuk Kim {
48839a53e0cSJaegeuk Kim 	mutex_lock_nested(&sbi->fs_lock[t], t);
48939a53e0cSJaegeuk Kim }
49039a53e0cSJaegeuk Kim 
49139a53e0cSJaegeuk Kim static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, enum lock_type t)
49239a53e0cSJaegeuk Kim {
49339a53e0cSJaegeuk Kim 	mutex_unlock(&sbi->fs_lock[t]);
49439a53e0cSJaegeuk Kim }
49539a53e0cSJaegeuk Kim 
49639a53e0cSJaegeuk Kim /*
49739a53e0cSJaegeuk Kim  * Check whether the given nid is within node id range.
49839a53e0cSJaegeuk Kim  */
49939a53e0cSJaegeuk Kim static inline void check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
50039a53e0cSJaegeuk Kim {
50139a53e0cSJaegeuk Kim 	BUG_ON((nid >= NM_I(sbi)->max_nid));
50239a53e0cSJaegeuk Kim }
50339a53e0cSJaegeuk Kim 
50439a53e0cSJaegeuk Kim #define F2FS_DEFAULT_ALLOCATED_BLOCKS	1
50539a53e0cSJaegeuk Kim 
50639a53e0cSJaegeuk Kim /*
50739a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
50839a53e0cSJaegeuk Kim  */
50939a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
51039a53e0cSJaegeuk Kim {
51139a53e0cSJaegeuk Kim 	if (F2FS_I(inode)->i_xattr_nid)
51239a53e0cSJaegeuk Kim 		return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
51339a53e0cSJaegeuk Kim 	else
51439a53e0cSJaegeuk Kim 		return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
51539a53e0cSJaegeuk Kim }
51639a53e0cSJaegeuk Kim 
51739a53e0cSJaegeuk Kim static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
51839a53e0cSJaegeuk Kim 				 struct inode *inode, blkcnt_t count)
51939a53e0cSJaegeuk Kim {
52039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
52139a53e0cSJaegeuk Kim 
52239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
52339a53e0cSJaegeuk Kim 	valid_block_count =
52439a53e0cSJaegeuk Kim 		sbi->total_valid_block_count + (block_t)count;
52539a53e0cSJaegeuk Kim 	if (valid_block_count > sbi->user_block_count) {
52639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
52739a53e0cSJaegeuk Kim 		return false;
52839a53e0cSJaegeuk Kim 	}
52939a53e0cSJaegeuk Kim 	inode->i_blocks += count;
53039a53e0cSJaegeuk Kim 	sbi->total_valid_block_count = valid_block_count;
53139a53e0cSJaegeuk Kim 	sbi->alloc_valid_block_count += (block_t)count;
53239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
53339a53e0cSJaegeuk Kim 	return true;
53439a53e0cSJaegeuk Kim }
53539a53e0cSJaegeuk Kim 
53639a53e0cSJaegeuk Kim static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
53739a53e0cSJaegeuk Kim 						struct inode *inode,
53839a53e0cSJaegeuk Kim 						blkcnt_t count)
53939a53e0cSJaegeuk Kim {
54039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
54139a53e0cSJaegeuk Kim 	BUG_ON(sbi->total_valid_block_count < (block_t) count);
54239a53e0cSJaegeuk Kim 	BUG_ON(inode->i_blocks < count);
54339a53e0cSJaegeuk Kim 	inode->i_blocks -= count;
54439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
54539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
54639a53e0cSJaegeuk Kim 	return 0;
54739a53e0cSJaegeuk Kim }
54839a53e0cSJaegeuk Kim 
54939a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
55039a53e0cSJaegeuk Kim {
55139a53e0cSJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
55239a53e0cSJaegeuk Kim 	F2FS_SET_SB_DIRT(sbi);
55339a53e0cSJaegeuk Kim }
55439a53e0cSJaegeuk Kim 
55539a53e0cSJaegeuk Kim static inline void inode_inc_dirty_dents(struct inode *inode)
55639a53e0cSJaegeuk Kim {
55739a53e0cSJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_dents);
55839a53e0cSJaegeuk Kim }
55939a53e0cSJaegeuk Kim 
56039a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
56139a53e0cSJaegeuk Kim {
56239a53e0cSJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
56339a53e0cSJaegeuk Kim }
56439a53e0cSJaegeuk Kim 
56539a53e0cSJaegeuk Kim static inline void inode_dec_dirty_dents(struct inode *inode)
56639a53e0cSJaegeuk Kim {
56739a53e0cSJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_dents);
56839a53e0cSJaegeuk Kim }
56939a53e0cSJaegeuk Kim 
57039a53e0cSJaegeuk Kim static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
57139a53e0cSJaegeuk Kim {
57239a53e0cSJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
57339a53e0cSJaegeuk Kim }
57439a53e0cSJaegeuk Kim 
57539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
57639a53e0cSJaegeuk Kim {
57739a53e0cSJaegeuk Kim 	block_t ret;
57839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
57939a53e0cSJaegeuk Kim 	ret = sbi->total_valid_block_count;
58039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
58139a53e0cSJaegeuk Kim 	return ret;
58239a53e0cSJaegeuk Kim }
58339a53e0cSJaegeuk Kim 
58439a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
58539a53e0cSJaegeuk Kim {
58639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
58739a53e0cSJaegeuk Kim 
58839a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
58939a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
59039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
59139a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
59239a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
59339a53e0cSJaegeuk Kim 
59439a53e0cSJaegeuk Kim 	return 0;
59539a53e0cSJaegeuk Kim }
59639a53e0cSJaegeuk Kim 
59739a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
59839a53e0cSJaegeuk Kim {
59939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
600*25ca923bSJaegeuk Kim 	int offset = (flag == NAT_BITMAP) ?
601*25ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
60239a53e0cSJaegeuk Kim 	return &ckpt->sit_nat_version_bitmap + offset;
60339a53e0cSJaegeuk Kim }
60439a53e0cSJaegeuk Kim 
60539a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
60639a53e0cSJaegeuk Kim {
60739a53e0cSJaegeuk Kim 	block_t start_addr;
60839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
60939a53e0cSJaegeuk Kim 	unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
61039a53e0cSJaegeuk Kim 
611*25ca923bSJaegeuk Kim 	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
61239a53e0cSJaegeuk Kim 
61339a53e0cSJaegeuk Kim 	/*
61439a53e0cSJaegeuk Kim 	 * odd numbered checkpoint should at cp segment 0
61539a53e0cSJaegeuk Kim 	 * and even segent must be at cp segment 1
61639a53e0cSJaegeuk Kim 	 */
61739a53e0cSJaegeuk Kim 	if (!(ckpt_version & 1))
61839a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
61939a53e0cSJaegeuk Kim 
62039a53e0cSJaegeuk Kim 	return start_addr;
62139a53e0cSJaegeuk Kim }
62239a53e0cSJaegeuk Kim 
62339a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
62439a53e0cSJaegeuk Kim {
62539a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
62639a53e0cSJaegeuk Kim }
62739a53e0cSJaegeuk Kim 
62839a53e0cSJaegeuk Kim static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
62939a53e0cSJaegeuk Kim 						struct inode *inode,
63039a53e0cSJaegeuk Kim 						unsigned int count)
63139a53e0cSJaegeuk Kim {
63239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
63339a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
63439a53e0cSJaegeuk Kim 
63539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
63639a53e0cSJaegeuk Kim 
63739a53e0cSJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count + (block_t)count;
63839a53e0cSJaegeuk Kim 	sbi->alloc_valid_block_count += (block_t)count;
63939a53e0cSJaegeuk Kim 	valid_node_count = sbi->total_valid_node_count + count;
64039a53e0cSJaegeuk Kim 
64139a53e0cSJaegeuk Kim 	if (valid_block_count > sbi->user_block_count) {
64239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
64339a53e0cSJaegeuk Kim 		return false;
64439a53e0cSJaegeuk Kim 	}
64539a53e0cSJaegeuk Kim 
64639a53e0cSJaegeuk Kim 	if (valid_node_count > sbi->total_node_count) {
64739a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
64839a53e0cSJaegeuk Kim 		return false;
64939a53e0cSJaegeuk Kim 	}
65039a53e0cSJaegeuk Kim 
65139a53e0cSJaegeuk Kim 	if (inode)
65239a53e0cSJaegeuk Kim 		inode->i_blocks += count;
65339a53e0cSJaegeuk Kim 	sbi->total_valid_node_count = valid_node_count;
65439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count = valid_block_count;
65539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
65639a53e0cSJaegeuk Kim 
65739a53e0cSJaegeuk Kim 	return true;
65839a53e0cSJaegeuk Kim }
65939a53e0cSJaegeuk Kim 
66039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
66139a53e0cSJaegeuk Kim 						struct inode *inode,
66239a53e0cSJaegeuk Kim 						unsigned int count)
66339a53e0cSJaegeuk Kim {
66439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
66539a53e0cSJaegeuk Kim 
66639a53e0cSJaegeuk Kim 	BUG_ON(sbi->total_valid_block_count < count);
66739a53e0cSJaegeuk Kim 	BUG_ON(sbi->total_valid_node_count < count);
66839a53e0cSJaegeuk Kim 	BUG_ON(inode->i_blocks < count);
66939a53e0cSJaegeuk Kim 
67039a53e0cSJaegeuk Kim 	inode->i_blocks -= count;
67139a53e0cSJaegeuk Kim 	sbi->total_valid_node_count -= count;
67239a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
67339a53e0cSJaegeuk Kim 
67439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
67539a53e0cSJaegeuk Kim }
67639a53e0cSJaegeuk Kim 
67739a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
67839a53e0cSJaegeuk Kim {
67939a53e0cSJaegeuk Kim 	unsigned int ret;
68039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
68139a53e0cSJaegeuk Kim 	ret = sbi->total_valid_node_count;
68239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
68339a53e0cSJaegeuk Kim 	return ret;
68439a53e0cSJaegeuk Kim }
68539a53e0cSJaegeuk Kim 
68639a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
68739a53e0cSJaegeuk Kim {
68839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
68939a53e0cSJaegeuk Kim 	BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
69039a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count++;
69139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
69239a53e0cSJaegeuk Kim }
69339a53e0cSJaegeuk Kim 
69439a53e0cSJaegeuk Kim static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
69539a53e0cSJaegeuk Kim {
69639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
69739a53e0cSJaegeuk Kim 	BUG_ON(!sbi->total_valid_inode_count);
69839a53e0cSJaegeuk Kim 	sbi->total_valid_inode_count--;
69939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
70039a53e0cSJaegeuk Kim 	return 0;
70139a53e0cSJaegeuk Kim }
70239a53e0cSJaegeuk Kim 
70339a53e0cSJaegeuk Kim static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
70439a53e0cSJaegeuk Kim {
70539a53e0cSJaegeuk Kim 	unsigned int ret;
70639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
70739a53e0cSJaegeuk Kim 	ret = sbi->total_valid_inode_count;
70839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
70939a53e0cSJaegeuk Kim 	return ret;
71039a53e0cSJaegeuk Kim }
71139a53e0cSJaegeuk Kim 
71239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
71339a53e0cSJaegeuk Kim {
71439a53e0cSJaegeuk Kim 	if (!page || IS_ERR(page))
71539a53e0cSJaegeuk Kim 		return;
71639a53e0cSJaegeuk Kim 
71739a53e0cSJaegeuk Kim 	if (unlock) {
71839a53e0cSJaegeuk Kim 		BUG_ON(!PageLocked(page));
71939a53e0cSJaegeuk Kim 		unlock_page(page);
72039a53e0cSJaegeuk Kim 	}
72139a53e0cSJaegeuk Kim 	page_cache_release(page);
72239a53e0cSJaegeuk Kim }
72339a53e0cSJaegeuk Kim 
72439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
72539a53e0cSJaegeuk Kim {
72639a53e0cSJaegeuk Kim 	if (dn->node_page)
72739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
72839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
72939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
73039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
73139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
73239a53e0cSJaegeuk Kim }
73339a53e0cSJaegeuk Kim 
73439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
73539a53e0cSJaegeuk Kim 					size_t size, void (*ctor)(void *))
73639a53e0cSJaegeuk Kim {
73739a53e0cSJaegeuk Kim 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
73839a53e0cSJaegeuk Kim }
73939a53e0cSJaegeuk Kim 
74039a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
74139a53e0cSJaegeuk Kim 
74239a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
74339a53e0cSJaegeuk Kim {
74439a53e0cSJaegeuk Kim 	struct f2fs_node *p = (struct f2fs_node *)page_address(page);
74539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
74639a53e0cSJaegeuk Kim }
74739a53e0cSJaegeuk Kim 
74839a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
74939a53e0cSJaegeuk Kim {
75039a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
75139a53e0cSJaegeuk Kim }
75239a53e0cSJaegeuk Kim 
75339a53e0cSJaegeuk Kim static inline block_t datablock_addr(struct page *node_page,
75439a53e0cSJaegeuk Kim 		unsigned int offset)
75539a53e0cSJaegeuk Kim {
75639a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
75739a53e0cSJaegeuk Kim 	__le32 *addr_array;
75839a53e0cSJaegeuk Kim 	raw_node = (struct f2fs_node *)page_address(node_page);
75939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
76039a53e0cSJaegeuk Kim 	return le32_to_cpu(addr_array[offset]);
76139a53e0cSJaegeuk Kim }
76239a53e0cSJaegeuk Kim 
76339a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
76439a53e0cSJaegeuk Kim {
76539a53e0cSJaegeuk Kim 	int mask;
76639a53e0cSJaegeuk Kim 
76739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
76839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
76939a53e0cSJaegeuk Kim 	return mask & *addr;
77039a53e0cSJaegeuk Kim }
77139a53e0cSJaegeuk Kim 
77239a53e0cSJaegeuk Kim static inline int f2fs_set_bit(unsigned int nr, char *addr)
77339a53e0cSJaegeuk Kim {
77439a53e0cSJaegeuk Kim 	int mask;
77539a53e0cSJaegeuk Kim 	int ret;
77639a53e0cSJaegeuk Kim 
77739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
77839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
77939a53e0cSJaegeuk Kim 	ret = mask & *addr;
78039a53e0cSJaegeuk Kim 	*addr |= mask;
78139a53e0cSJaegeuk Kim 	return ret;
78239a53e0cSJaegeuk Kim }
78339a53e0cSJaegeuk Kim 
78439a53e0cSJaegeuk Kim static inline int f2fs_clear_bit(unsigned int nr, char *addr)
78539a53e0cSJaegeuk Kim {
78639a53e0cSJaegeuk Kim 	int mask;
78739a53e0cSJaegeuk Kim 	int ret;
78839a53e0cSJaegeuk Kim 
78939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
79039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
79139a53e0cSJaegeuk Kim 	ret = mask & *addr;
79239a53e0cSJaegeuk Kim 	*addr &= ~mask;
79339a53e0cSJaegeuk Kim 	return ret;
79439a53e0cSJaegeuk Kim }
79539a53e0cSJaegeuk Kim 
79639a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
79739a53e0cSJaegeuk Kim enum {
79839a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
79939a53e0cSJaegeuk Kim 	FI_NEED_CP,		/* need to do checkpoint during fsync */
80039a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
80139a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
80239a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
80339a53e0cSJaegeuk Kim };
80439a53e0cSJaegeuk Kim 
80539a53e0cSJaegeuk Kim static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
80639a53e0cSJaegeuk Kim {
80739a53e0cSJaegeuk Kim 	set_bit(flag, &fi->flags);
80839a53e0cSJaegeuk Kim }
80939a53e0cSJaegeuk Kim 
81039a53e0cSJaegeuk Kim static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
81139a53e0cSJaegeuk Kim {
81239a53e0cSJaegeuk Kim 	return test_bit(flag, &fi->flags);
81339a53e0cSJaegeuk Kim }
81439a53e0cSJaegeuk Kim 
81539a53e0cSJaegeuk Kim static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
81639a53e0cSJaegeuk Kim {
81739a53e0cSJaegeuk Kim 	clear_bit(flag, &fi->flags);
81839a53e0cSJaegeuk Kim }
81939a53e0cSJaegeuk Kim 
82039a53e0cSJaegeuk Kim static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
82139a53e0cSJaegeuk Kim {
82239a53e0cSJaegeuk Kim 	fi->i_acl_mode = mode;
82339a53e0cSJaegeuk Kim 	set_inode_flag(fi, FI_ACL_MODE);
82439a53e0cSJaegeuk Kim }
82539a53e0cSJaegeuk Kim 
82639a53e0cSJaegeuk Kim static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
82739a53e0cSJaegeuk Kim {
82839a53e0cSJaegeuk Kim 	if (is_inode_flag_set(fi, FI_ACL_MODE)) {
82939a53e0cSJaegeuk Kim 		clear_inode_flag(fi, FI_ACL_MODE);
83039a53e0cSJaegeuk Kim 		return 1;
83139a53e0cSJaegeuk Kim 	}
83239a53e0cSJaegeuk Kim 	return 0;
83339a53e0cSJaegeuk Kim }
83439a53e0cSJaegeuk Kim 
83539a53e0cSJaegeuk Kim /*
83639a53e0cSJaegeuk Kim  * file.c
83739a53e0cSJaegeuk Kim  */
83839a53e0cSJaegeuk Kim int f2fs_sync_file(struct file *, loff_t, loff_t, int);
83939a53e0cSJaegeuk Kim void truncate_data_blocks(struct dnode_of_data *);
84039a53e0cSJaegeuk Kim void f2fs_truncate(struct inode *);
84139a53e0cSJaegeuk Kim int f2fs_setattr(struct dentry *, struct iattr *);
84239a53e0cSJaegeuk Kim int truncate_hole(struct inode *, pgoff_t, pgoff_t);
84339a53e0cSJaegeuk Kim long f2fs_ioctl(struct file *, unsigned int, unsigned long);
84439a53e0cSJaegeuk Kim 
84539a53e0cSJaegeuk Kim /*
84639a53e0cSJaegeuk Kim  * inode.c
84739a53e0cSJaegeuk Kim  */
84839a53e0cSJaegeuk Kim void f2fs_set_inode_flags(struct inode *);
84939a53e0cSJaegeuk Kim struct inode *f2fs_iget_nowait(struct super_block *, unsigned long);
85039a53e0cSJaegeuk Kim struct inode *f2fs_iget(struct super_block *, unsigned long);
85139a53e0cSJaegeuk Kim void update_inode(struct inode *, struct page *);
85239a53e0cSJaegeuk Kim int f2fs_write_inode(struct inode *, struct writeback_control *);
85339a53e0cSJaegeuk Kim void f2fs_evict_inode(struct inode *);
85439a53e0cSJaegeuk Kim 
85539a53e0cSJaegeuk Kim /*
85639a53e0cSJaegeuk Kim  * namei.c
85739a53e0cSJaegeuk Kim  */
85839a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
85939a53e0cSJaegeuk Kim 
86039a53e0cSJaegeuk Kim /*
86139a53e0cSJaegeuk Kim  * dir.c
86239a53e0cSJaegeuk Kim  */
86339a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
86439a53e0cSJaegeuk Kim 							struct page **);
86539a53e0cSJaegeuk Kim struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
86639a53e0cSJaegeuk Kim ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
86739a53e0cSJaegeuk Kim void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
86839a53e0cSJaegeuk Kim 				struct page *, struct inode *);
86939a53e0cSJaegeuk Kim void init_dent_inode(struct dentry *, struct page *);
87039a53e0cSJaegeuk Kim int f2fs_add_link(struct dentry *, struct inode *);
87139a53e0cSJaegeuk Kim void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
87239a53e0cSJaegeuk Kim int f2fs_make_empty(struct inode *, struct inode *);
87339a53e0cSJaegeuk Kim bool f2fs_empty_dir(struct inode *);
87439a53e0cSJaegeuk Kim 
87539a53e0cSJaegeuk Kim /*
87639a53e0cSJaegeuk Kim  * super.c
87739a53e0cSJaegeuk Kim  */
87839a53e0cSJaegeuk Kim int f2fs_sync_fs(struct super_block *, int);
87939a53e0cSJaegeuk Kim 
88039a53e0cSJaegeuk Kim /*
88139a53e0cSJaegeuk Kim  * hash.c
88239a53e0cSJaegeuk Kim  */
88339a53e0cSJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const char *, int);
88439a53e0cSJaegeuk Kim 
88539a53e0cSJaegeuk Kim /*
88639a53e0cSJaegeuk Kim  * node.c
88739a53e0cSJaegeuk Kim  */
88839a53e0cSJaegeuk Kim struct dnode_of_data;
88939a53e0cSJaegeuk Kim struct node_info;
89039a53e0cSJaegeuk Kim 
89139a53e0cSJaegeuk Kim int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
89239a53e0cSJaegeuk Kim void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
89339a53e0cSJaegeuk Kim int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
89439a53e0cSJaegeuk Kim int truncate_inode_blocks(struct inode *, pgoff_t);
89539a53e0cSJaegeuk Kim int remove_inode_page(struct inode *);
89639a53e0cSJaegeuk Kim int new_inode_page(struct inode *, struct dentry *);
89739a53e0cSJaegeuk Kim struct page *new_node_page(struct dnode_of_data *, unsigned int);
89839a53e0cSJaegeuk Kim void ra_node_page(struct f2fs_sb_info *, nid_t);
89939a53e0cSJaegeuk Kim struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
90039a53e0cSJaegeuk Kim struct page *get_node_page_ra(struct page *, int);
90139a53e0cSJaegeuk Kim void sync_inode_page(struct dnode_of_data *);
90239a53e0cSJaegeuk Kim int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
90339a53e0cSJaegeuk Kim bool alloc_nid(struct f2fs_sb_info *, nid_t *);
90439a53e0cSJaegeuk Kim void alloc_nid_done(struct f2fs_sb_info *, nid_t);
90539a53e0cSJaegeuk Kim void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
90639a53e0cSJaegeuk Kim void recover_node_page(struct f2fs_sb_info *, struct page *,
90739a53e0cSJaegeuk Kim 		struct f2fs_summary *, struct node_info *, block_t);
90839a53e0cSJaegeuk Kim int recover_inode_page(struct f2fs_sb_info *, struct page *);
90939a53e0cSJaegeuk Kim int restore_node_summary(struct f2fs_sb_info *, unsigned int,
91039a53e0cSJaegeuk Kim 				struct f2fs_summary_block *);
91139a53e0cSJaegeuk Kim void flush_nat_entries(struct f2fs_sb_info *);
91239a53e0cSJaegeuk Kim int build_node_manager(struct f2fs_sb_info *);
91339a53e0cSJaegeuk Kim void destroy_node_manager(struct f2fs_sb_info *);
91439a53e0cSJaegeuk Kim int create_node_manager_caches(void);
91539a53e0cSJaegeuk Kim void destroy_node_manager_caches(void);
91639a53e0cSJaegeuk Kim 
91739a53e0cSJaegeuk Kim /*
91839a53e0cSJaegeuk Kim  * segment.c
91939a53e0cSJaegeuk Kim  */
92039a53e0cSJaegeuk Kim void f2fs_balance_fs(struct f2fs_sb_info *);
92139a53e0cSJaegeuk Kim void invalidate_blocks(struct f2fs_sb_info *, block_t);
92239a53e0cSJaegeuk Kim void locate_dirty_segment(struct f2fs_sb_info *, unsigned int);
92339a53e0cSJaegeuk Kim void clear_prefree_segments(struct f2fs_sb_info *);
92439a53e0cSJaegeuk Kim int npages_for_summary_flush(struct f2fs_sb_info *);
92539a53e0cSJaegeuk Kim void allocate_new_segments(struct f2fs_sb_info *);
92639a53e0cSJaegeuk Kim struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
92739a53e0cSJaegeuk Kim struct bio *f2fs_bio_alloc(struct block_device *, sector_t, int, gfp_t);
92839a53e0cSJaegeuk Kim void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool sync);
92939a53e0cSJaegeuk Kim int write_meta_page(struct f2fs_sb_info *, struct page *,
93039a53e0cSJaegeuk Kim 					struct writeback_control *);
93139a53e0cSJaegeuk Kim void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
93239a53e0cSJaegeuk Kim 					block_t, block_t *);
93339a53e0cSJaegeuk Kim void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
93439a53e0cSJaegeuk Kim 					block_t, block_t *);
93539a53e0cSJaegeuk Kim void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
93639a53e0cSJaegeuk Kim void recover_data_page(struct f2fs_sb_info *, struct page *,
93739a53e0cSJaegeuk Kim 				struct f2fs_summary *, block_t, block_t);
93839a53e0cSJaegeuk Kim void rewrite_node_page(struct f2fs_sb_info *, struct page *,
93939a53e0cSJaegeuk Kim 				struct f2fs_summary *, block_t, block_t);
94039a53e0cSJaegeuk Kim void write_data_summaries(struct f2fs_sb_info *, block_t);
94139a53e0cSJaegeuk Kim void write_node_summaries(struct f2fs_sb_info *, block_t);
94239a53e0cSJaegeuk Kim int lookup_journal_in_cursum(struct f2fs_summary_block *,
94339a53e0cSJaegeuk Kim 					int, unsigned int, int);
94439a53e0cSJaegeuk Kim void flush_sit_entries(struct f2fs_sb_info *);
94539a53e0cSJaegeuk Kim int build_segment_manager(struct f2fs_sb_info *);
94639a53e0cSJaegeuk Kim void reset_victim_segmap(struct f2fs_sb_info *);
94739a53e0cSJaegeuk Kim void destroy_segment_manager(struct f2fs_sb_info *);
94839a53e0cSJaegeuk Kim 
94939a53e0cSJaegeuk Kim /*
95039a53e0cSJaegeuk Kim  * checkpoint.c
95139a53e0cSJaegeuk Kim  */
95239a53e0cSJaegeuk Kim struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
95339a53e0cSJaegeuk Kim struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
95439a53e0cSJaegeuk Kim long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
95539a53e0cSJaegeuk Kim int check_orphan_space(struct f2fs_sb_info *);
95639a53e0cSJaegeuk Kim void add_orphan_inode(struct f2fs_sb_info *, nid_t);
95739a53e0cSJaegeuk Kim void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
95839a53e0cSJaegeuk Kim int recover_orphan_inodes(struct f2fs_sb_info *);
95939a53e0cSJaegeuk Kim int get_valid_checkpoint(struct f2fs_sb_info *);
96039a53e0cSJaegeuk Kim void set_dirty_dir_page(struct inode *, struct page *);
96139a53e0cSJaegeuk Kim void remove_dirty_dir_inode(struct inode *);
96239a53e0cSJaegeuk Kim void sync_dirty_dir_inodes(struct f2fs_sb_info *);
96339a53e0cSJaegeuk Kim void block_operations(struct f2fs_sb_info *);
96439a53e0cSJaegeuk Kim void write_checkpoint(struct f2fs_sb_info *, bool, bool);
96539a53e0cSJaegeuk Kim void init_orphan_info(struct f2fs_sb_info *);
96639a53e0cSJaegeuk Kim int create_checkpoint_caches(void);
96739a53e0cSJaegeuk Kim void destroy_checkpoint_caches(void);
96839a53e0cSJaegeuk Kim 
96939a53e0cSJaegeuk Kim /*
97039a53e0cSJaegeuk Kim  * data.c
97139a53e0cSJaegeuk Kim  */
97239a53e0cSJaegeuk Kim int reserve_new_block(struct dnode_of_data *);
97339a53e0cSJaegeuk Kim void update_extent_cache(block_t, struct dnode_of_data *);
97439a53e0cSJaegeuk Kim struct page *find_data_page(struct inode *, pgoff_t);
97539a53e0cSJaegeuk Kim struct page *get_lock_data_page(struct inode *, pgoff_t);
97639a53e0cSJaegeuk Kim struct page *get_new_data_page(struct inode *, pgoff_t, bool);
97739a53e0cSJaegeuk Kim int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
97839a53e0cSJaegeuk Kim int do_write_data_page(struct page *);
97939a53e0cSJaegeuk Kim 
98039a53e0cSJaegeuk Kim /*
98139a53e0cSJaegeuk Kim  * gc.c
98239a53e0cSJaegeuk Kim  */
98339a53e0cSJaegeuk Kim int start_gc_thread(struct f2fs_sb_info *);
98439a53e0cSJaegeuk Kim void stop_gc_thread(struct f2fs_sb_info *);
98539a53e0cSJaegeuk Kim block_t start_bidx_of_node(unsigned int);
98639a53e0cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *, int);
98739a53e0cSJaegeuk Kim void build_gc_manager(struct f2fs_sb_info *);
98839a53e0cSJaegeuk Kim int create_gc_caches(void);
98939a53e0cSJaegeuk Kim void destroy_gc_caches(void);
99039a53e0cSJaegeuk Kim 
99139a53e0cSJaegeuk Kim /*
99239a53e0cSJaegeuk Kim  * recovery.c
99339a53e0cSJaegeuk Kim  */
99439a53e0cSJaegeuk Kim void recover_fsync_data(struct f2fs_sb_info *);
99539a53e0cSJaegeuk Kim bool space_for_roll_forward(struct f2fs_sb_info *);
99639a53e0cSJaegeuk Kim 
99739a53e0cSJaegeuk Kim /*
99839a53e0cSJaegeuk Kim  * debug.c
99939a53e0cSJaegeuk Kim  */
100039a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
100139a53e0cSJaegeuk Kim struct f2fs_stat_info {
100239a53e0cSJaegeuk Kim 	struct list_head stat_list;
100339a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
100439a53e0cSJaegeuk Kim 	struct mutex stat_lock;
100539a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
100639a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
100739a53e0cSJaegeuk Kim 	int hit_ext, total_ext;
100839a53e0cSJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
100939a53e0cSJaegeuk Kim 	int nats, sits, fnids;
101039a53e0cSJaegeuk Kim 	int total_count, utilization;
101139a53e0cSJaegeuk Kim 	int bg_gc;
101239a53e0cSJaegeuk Kim 	unsigned int valid_count, valid_node_count, valid_inode_count;
101339a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
101439a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
101539a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
101639a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
101739a53e0cSJaegeuk Kim 	int prefree_count, call_count;
101839a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
101939a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
102039a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
102139a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
102239a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
102339a53e0cSJaegeuk Kim 
102439a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
102539a53e0cSJaegeuk Kim 	unsigned int block_count[2];
102639a53e0cSJaegeuk Kim 	unsigned base_mem, cache_mem;
102739a53e0cSJaegeuk Kim };
102839a53e0cSJaegeuk Kim 
102939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)	((si)->call_count++)
103039a53e0cSJaegeuk Kim 
103139a53e0cSJaegeuk Kim #define stat_inc_seg_count(sbi, type)					\
103239a53e0cSJaegeuk Kim 	do {								\
103339a53e0cSJaegeuk Kim 		struct f2fs_stat_info *si = sbi->stat_info;		\
103439a53e0cSJaegeuk Kim 		(si)->tot_segs++;					\
103539a53e0cSJaegeuk Kim 		if (type == SUM_TYPE_DATA)				\
103639a53e0cSJaegeuk Kim 			si->data_segs++;				\
103739a53e0cSJaegeuk Kim 		else							\
103839a53e0cSJaegeuk Kim 			si->node_segs++;				\
103939a53e0cSJaegeuk Kim 	} while (0)
104039a53e0cSJaegeuk Kim 
104139a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
104239a53e0cSJaegeuk Kim 	(si->tot_blks += (blks))
104339a53e0cSJaegeuk Kim 
104439a53e0cSJaegeuk Kim #define stat_inc_data_blk_count(sbi, blks)				\
104539a53e0cSJaegeuk Kim 	do {								\
104639a53e0cSJaegeuk Kim 		struct f2fs_stat_info *si = sbi->stat_info;		\
104739a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
104839a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
104939a53e0cSJaegeuk Kim 	} while (0)
105039a53e0cSJaegeuk Kim 
105139a53e0cSJaegeuk Kim #define stat_inc_node_blk_count(sbi, blks)				\
105239a53e0cSJaegeuk Kim 	do {								\
105339a53e0cSJaegeuk Kim 		struct f2fs_stat_info *si = sbi->stat_info;		\
105439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
105539a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
105639a53e0cSJaegeuk Kim 	} while (0)
105739a53e0cSJaegeuk Kim 
105839a53e0cSJaegeuk Kim int f2fs_build_stats(struct f2fs_sb_info *);
105939a53e0cSJaegeuk Kim void f2fs_destroy_stats(struct f2fs_sb_info *);
106039a53e0cSJaegeuk Kim void destroy_root_stats(void);
106139a53e0cSJaegeuk Kim #else
106239a53e0cSJaegeuk Kim #define stat_inc_call_count(si)
106339a53e0cSJaegeuk Kim #define stat_inc_seg_count(si, type)
106439a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)
106539a53e0cSJaegeuk Kim #define stat_inc_data_blk_count(si, blks)
106639a53e0cSJaegeuk Kim #define stat_inc_node_blk_count(sbi, blks)
106739a53e0cSJaegeuk Kim 
106839a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
106939a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
107039a53e0cSJaegeuk Kim static inline void destroy_root_stats(void) { }
107139a53e0cSJaegeuk Kim #endif
107239a53e0cSJaegeuk Kim 
107339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
107439a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
107539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
107639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
107739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
107839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
107939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
108039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
108139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
108239a53e0cSJaegeuk Kim #endif
1083