xref: /aosp_15_r20/external/f2fs-tools/fsck/f2fs.h (revision 59bfda1f02d633cd6b8b69f31eee485d40f6eef6)
1*59bfda1fSAndroid Build Coastguard Worker /**
2*59bfda1fSAndroid Build Coastguard Worker  * f2fs.h
3*59bfda1fSAndroid Build Coastguard Worker  *
4*59bfda1fSAndroid Build Coastguard Worker  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5*59bfda1fSAndroid Build Coastguard Worker  *             http://www.samsung.com/
6*59bfda1fSAndroid Build Coastguard Worker  *
7*59bfda1fSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify
8*59bfda1fSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License version 2 as
9*59bfda1fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
10*59bfda1fSAndroid Build Coastguard Worker  */
11*59bfda1fSAndroid Build Coastguard Worker #ifndef _F2FS_H_
12*59bfda1fSAndroid Build Coastguard Worker #define _F2FS_H_
13*59bfda1fSAndroid Build Coastguard Worker 
14*59bfda1fSAndroid Build Coastguard Worker #include <stdlib.h>
15*59bfda1fSAndroid Build Coastguard Worker #include <unistd.h>
16*59bfda1fSAndroid Build Coastguard Worker #include <stdio.h>
17*59bfda1fSAndroid Build Coastguard Worker #include <stdbool.h>
18*59bfda1fSAndroid Build Coastguard Worker #include <stddef.h>
19*59bfda1fSAndroid Build Coastguard Worker #include <errno.h>
20*59bfda1fSAndroid Build Coastguard Worker #include <fcntl.h>
21*59bfda1fSAndroid Build Coastguard Worker #include <string.h>
22*59bfda1fSAndroid Build Coastguard Worker #include <errno.h>
23*59bfda1fSAndroid Build Coastguard Worker #include <f2fs_fs.h>
24*59bfda1fSAndroid Build Coastguard Worker 
25*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_MNTENT_H
26*59bfda1fSAndroid Build Coastguard Worker #include <mntent.h>
27*59bfda1fSAndroid Build Coastguard Worker #endif
28*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_MACH_TIME_H
29*59bfda1fSAndroid Build Coastguard Worker #include <mach/mach_time.h>
30*59bfda1fSAndroid Build Coastguard Worker #endif
31*59bfda1fSAndroid Build Coastguard Worker #include <sys/stat.h>
32*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_IOCTL_H
33*59bfda1fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
34*59bfda1fSAndroid Build Coastguard Worker #endif
35*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_MOUNT_H
36*59bfda1fSAndroid Build Coastguard Worker #include <sys/mount.h>
37*59bfda1fSAndroid Build Coastguard Worker #endif
38*59bfda1fSAndroid Build Coastguard Worker #include <assert.h>
39*59bfda1fSAndroid Build Coastguard Worker 
40*59bfda1fSAndroid Build Coastguard Worker #define EXIT_ERR_CODE		(-1)
41*59bfda1fSAndroid Build Coastguard Worker #define ver_after(a, b) (typecheck(unsigned long long, a) &&            \
42*59bfda1fSAndroid Build Coastguard Worker 		typecheck(unsigned long long, b) &&                     \
43*59bfda1fSAndroid Build Coastguard Worker 		((long long)((a) - (b)) > 0))
44*59bfda1fSAndroid Build Coastguard Worker 
45*59bfda1fSAndroid Build Coastguard Worker #define container_of(ptr, type, member) ({			\
46*59bfda1fSAndroid Build Coastguard Worker 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
47*59bfda1fSAndroid Build Coastguard Worker 	(type *)((char *)__mptr - offsetof(type, member)); })
48*59bfda1fSAndroid Build Coastguard Worker 
49*59bfda1fSAndroid Build Coastguard Worker struct list_head {
50*59bfda1fSAndroid Build Coastguard Worker 	struct list_head *next, *prev;
51*59bfda1fSAndroid Build Coastguard Worker };
52*59bfda1fSAndroid Build Coastguard Worker 
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)53*59bfda1fSAndroid Build Coastguard Worker static inline void __list_add(struct list_head *new,
54*59bfda1fSAndroid Build Coastguard Worker 				struct list_head *prev,
55*59bfda1fSAndroid Build Coastguard Worker 				struct list_head *next)
56*59bfda1fSAndroid Build Coastguard Worker {
57*59bfda1fSAndroid Build Coastguard Worker 	next->prev = new;
58*59bfda1fSAndroid Build Coastguard Worker 	new->next = next;
59*59bfda1fSAndroid Build Coastguard Worker 	new->prev = prev;
60*59bfda1fSAndroid Build Coastguard Worker 	prev->next = new;
61*59bfda1fSAndroid Build Coastguard Worker }
62*59bfda1fSAndroid Build Coastguard Worker 
__list_del(struct list_head * prev,struct list_head * next)63*59bfda1fSAndroid Build Coastguard Worker static inline void __list_del(struct list_head * prev, struct list_head * next)
64*59bfda1fSAndroid Build Coastguard Worker {
65*59bfda1fSAndroid Build Coastguard Worker 	next->prev = prev;
66*59bfda1fSAndroid Build Coastguard Worker 	prev->next = next;
67*59bfda1fSAndroid Build Coastguard Worker }
68*59bfda1fSAndroid Build Coastguard Worker 
list_del(struct list_head * entry)69*59bfda1fSAndroid Build Coastguard Worker static inline void list_del(struct list_head *entry)
70*59bfda1fSAndroid Build Coastguard Worker {
71*59bfda1fSAndroid Build Coastguard Worker 	__list_del(entry->prev, entry->next);
72*59bfda1fSAndroid Build Coastguard Worker }
73*59bfda1fSAndroid Build Coastguard Worker 
list_add_tail(struct list_head * new,struct list_head * head)74*59bfda1fSAndroid Build Coastguard Worker static inline void list_add_tail(struct list_head *new, struct list_head *head)
75*59bfda1fSAndroid Build Coastguard Worker {
76*59bfda1fSAndroid Build Coastguard Worker 	__list_add(new, head->prev, head);
77*59bfda1fSAndroid Build Coastguard Worker }
78*59bfda1fSAndroid Build Coastguard Worker 
79*59bfda1fSAndroid Build Coastguard Worker #define LIST_HEAD_INIT(name) { &(name), &(name) }
80*59bfda1fSAndroid Build Coastguard Worker 
81*59bfda1fSAndroid Build Coastguard Worker #define list_entry(ptr, type, member)					\
82*59bfda1fSAndroid Build Coastguard Worker 		container_of(ptr, type, member)
83*59bfda1fSAndroid Build Coastguard Worker 
84*59bfda1fSAndroid Build Coastguard Worker #define list_first_entry(ptr, type, member)				\
85*59bfda1fSAndroid Build Coastguard Worker 		list_entry((ptr)->next, type, member)
86*59bfda1fSAndroid Build Coastguard Worker 
87*59bfda1fSAndroid Build Coastguard Worker #define list_next_entry(pos, member)					\
88*59bfda1fSAndroid Build Coastguard Worker 		list_entry((pos)->member.next, typeof(*(pos)), member)
89*59bfda1fSAndroid Build Coastguard Worker 
90*59bfda1fSAndroid Build Coastguard Worker #define list_for_each_entry(pos, head, member)				\
91*59bfda1fSAndroid Build Coastguard Worker 	for (pos = list_first_entry(head, typeof(*pos), member);	\
92*59bfda1fSAndroid Build Coastguard Worker 		&pos->member != (head);					\
93*59bfda1fSAndroid Build Coastguard Worker 		pos = list_next_entry(pos, member))
94*59bfda1fSAndroid Build Coastguard Worker 
95*59bfda1fSAndroid Build Coastguard Worker #define list_for_each_entry_safe(pos, n, head, member)			\
96*59bfda1fSAndroid Build Coastguard Worker 	for (pos = list_first_entry(head, typeof(*pos), member),	\
97*59bfda1fSAndroid Build Coastguard Worker 		n = list_next_entry(pos, member);			\
98*59bfda1fSAndroid Build Coastguard Worker 		&pos->member != (head);					\
99*59bfda1fSAndroid Build Coastguard Worker 		pos = n, n = list_next_entry(n, member))
100*59bfda1fSAndroid Build Coastguard Worker 
101*59bfda1fSAndroid Build Coastguard Worker /*
102*59bfda1fSAndroid Build Coastguard Worker  * indicate meta/data type
103*59bfda1fSAndroid Build Coastguard Worker  */
104*59bfda1fSAndroid Build Coastguard Worker enum {
105*59bfda1fSAndroid Build Coastguard Worker 	META_CP,
106*59bfda1fSAndroid Build Coastguard Worker 	META_NAT,
107*59bfda1fSAndroid Build Coastguard Worker 	META_SIT,
108*59bfda1fSAndroid Build Coastguard Worker 	META_SSA,
109*59bfda1fSAndroid Build Coastguard Worker 	META_MAX,
110*59bfda1fSAndroid Build Coastguard Worker 	META_POR,
111*59bfda1fSAndroid Build Coastguard Worker 	DATA_GENERIC,
112*59bfda1fSAndroid Build Coastguard Worker };
113*59bfda1fSAndroid Build Coastguard Worker 
114*59bfda1fSAndroid Build Coastguard Worker #define MAX_RA_BLOCKS	64
115*59bfda1fSAndroid Build Coastguard Worker 
116*59bfda1fSAndroid Build Coastguard Worker enum {
117*59bfda1fSAndroid Build Coastguard Worker 	NAT_BITMAP,
118*59bfda1fSAndroid Build Coastguard Worker 	SIT_BITMAP
119*59bfda1fSAndroid Build Coastguard Worker };
120*59bfda1fSAndroid Build Coastguard Worker 
121*59bfda1fSAndroid Build Coastguard Worker struct node_info {
122*59bfda1fSAndroid Build Coastguard Worker 	nid_t nid;
123*59bfda1fSAndroid Build Coastguard Worker 	nid_t ino;
124*59bfda1fSAndroid Build Coastguard Worker 	u32 blk_addr;
125*59bfda1fSAndroid Build Coastguard Worker 	unsigned char version;
126*59bfda1fSAndroid Build Coastguard Worker };
127*59bfda1fSAndroid Build Coastguard Worker 
128*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info {
129*59bfda1fSAndroid Build Coastguard Worker 	block_t nat_blkaddr;
130*59bfda1fSAndroid Build Coastguard Worker 	block_t nat_blocks;
131*59bfda1fSAndroid Build Coastguard Worker 	nid_t max_nid;
132*59bfda1fSAndroid Build Coastguard Worker 	nid_t init_scan_nid;
133*59bfda1fSAndroid Build Coastguard Worker 	nid_t next_scan_nid;
134*59bfda1fSAndroid Build Coastguard Worker 
135*59bfda1fSAndroid Build Coastguard Worker 	unsigned int nat_cnt;
136*59bfda1fSAndroid Build Coastguard Worker 	unsigned int fcnt;
137*59bfda1fSAndroid Build Coastguard Worker 
138*59bfda1fSAndroid Build Coastguard Worker 	char *nat_bitmap;
139*59bfda1fSAndroid Build Coastguard Worker 	int bitmap_size;
140*59bfda1fSAndroid Build Coastguard Worker 	char *nid_bitmap;
141*59bfda1fSAndroid Build Coastguard Worker };
142*59bfda1fSAndroid Build Coastguard Worker 
143*59bfda1fSAndroid Build Coastguard Worker struct seg_entry {
144*59bfda1fSAndroid Build Coastguard Worker 	unsigned short valid_blocks;    /* # of valid blocks */
145*59bfda1fSAndroid Build Coastguard Worker 	unsigned short ckpt_valid_blocks;	/* # of valid blocks last cp, for recovered data/node */
146*59bfda1fSAndroid Build Coastguard Worker 	unsigned char *cur_valid_map;   /* validity bitmap of blocks */
147*59bfda1fSAndroid Build Coastguard Worker 	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp, for recovered data/node */
148*59bfda1fSAndroid Build Coastguard Worker 	unsigned char type;             /* segment type like CURSEG_XXX_TYPE */
149*59bfda1fSAndroid Build Coastguard Worker 	unsigned char orig_type;        /* segment type like CURSEG_XXX_TYPE */
150*59bfda1fSAndroid Build Coastguard Worker 	unsigned char ckpt_type;        /* segment type like CURSEG_XXX_TYPE , for recovered data/node */
151*59bfda1fSAndroid Build Coastguard Worker 	unsigned long long mtime;       /* modification time of the segment */
152*59bfda1fSAndroid Build Coastguard Worker 	int dirty;
153*59bfda1fSAndroid Build Coastguard Worker };
154*59bfda1fSAndroid Build Coastguard Worker 
155*59bfda1fSAndroid Build Coastguard Worker struct sec_entry {
156*59bfda1fSAndroid Build Coastguard Worker 	unsigned int valid_blocks;      /* # of valid blocks in a section */
157*59bfda1fSAndroid Build Coastguard Worker };
158*59bfda1fSAndroid Build Coastguard Worker 
159*59bfda1fSAndroid Build Coastguard Worker struct sit_info {
160*59bfda1fSAndroid Build Coastguard Worker 
161*59bfda1fSAndroid Build Coastguard Worker 	block_t sit_base_addr;          /* start block address of SIT area */
162*59bfda1fSAndroid Build Coastguard Worker 	block_t sit_blocks;             /* # of blocks used by SIT area */
163*59bfda1fSAndroid Build Coastguard Worker 	block_t written_valid_blocks;   /* # of valid blocks in main area */
164*59bfda1fSAndroid Build Coastguard Worker 	unsigned char *bitmap;		/* all bitmaps pointer */
165*59bfda1fSAndroid Build Coastguard Worker 	char *sit_bitmap;               /* SIT bitmap pointer */
166*59bfda1fSAndroid Build Coastguard Worker 	unsigned int bitmap_size;       /* SIT bitmap size */
167*59bfda1fSAndroid Build Coastguard Worker 
168*59bfda1fSAndroid Build Coastguard Worker 	unsigned long *dirty_sentries_bitmap;   /* bitmap for dirty sentries */
169*59bfda1fSAndroid Build Coastguard Worker 	unsigned int dirty_sentries;            /* # of dirty sentries */
170*59bfda1fSAndroid Build Coastguard Worker 	unsigned int sents_per_block;           /* # of SIT entries per block */
171*59bfda1fSAndroid Build Coastguard Worker 	struct seg_entry *sentries;             /* SIT segment-level cache */
172*59bfda1fSAndroid Build Coastguard Worker 	struct sec_entry *sec_entries;          /* SIT section-level cache */
173*59bfda1fSAndroid Build Coastguard Worker 
174*59bfda1fSAndroid Build Coastguard Worker 	unsigned long long elapsed_time;        /* elapsed time after mount */
175*59bfda1fSAndroid Build Coastguard Worker 	unsigned long long mounted_time;        /* mount time */
176*59bfda1fSAndroid Build Coastguard Worker 	unsigned long long min_mtime;           /* min. modification time */
177*59bfda1fSAndroid Build Coastguard Worker 	unsigned long long max_mtime;           /* max. modification time */
178*59bfda1fSAndroid Build Coastguard Worker };
179*59bfda1fSAndroid Build Coastguard Worker 
180*59bfda1fSAndroid Build Coastguard Worker struct curseg_info {
181*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_summary_block *sum_blk;     /* cached summary block */
182*59bfda1fSAndroid Build Coastguard Worker 	unsigned char alloc_type;               /* current allocation type */
183*59bfda1fSAndroid Build Coastguard Worker 	unsigned int segno;                     /* current segment number */
184*59bfda1fSAndroid Build Coastguard Worker 	unsigned short next_blkoff;             /* next block offset to write */
185*59bfda1fSAndroid Build Coastguard Worker 	unsigned int zone;                      /* current zone number */
186*59bfda1fSAndroid Build Coastguard Worker 	unsigned int next_segno;                /* preallocated segment */
187*59bfda1fSAndroid Build Coastguard Worker };
188*59bfda1fSAndroid Build Coastguard Worker 
189*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sm_info {
190*59bfda1fSAndroid Build Coastguard Worker 	struct sit_info *sit_info;
191*59bfda1fSAndroid Build Coastguard Worker 	struct curseg_info *curseg_array;
192*59bfda1fSAndroid Build Coastguard Worker 	struct curseg_info saved_curseg_warm_node;
193*59bfda1fSAndroid Build Coastguard Worker 
194*59bfda1fSAndroid Build Coastguard Worker 	block_t seg0_blkaddr;
195*59bfda1fSAndroid Build Coastguard Worker 	block_t main_blkaddr;
196*59bfda1fSAndroid Build Coastguard Worker 	block_t ssa_blkaddr;
197*59bfda1fSAndroid Build Coastguard Worker 
198*59bfda1fSAndroid Build Coastguard Worker 	unsigned int segment_count;
199*59bfda1fSAndroid Build Coastguard Worker 	unsigned int main_segments;
200*59bfda1fSAndroid Build Coastguard Worker 	unsigned int reserved_segments;
201*59bfda1fSAndroid Build Coastguard Worker 	unsigned int ovp_segments;
202*59bfda1fSAndroid Build Coastguard Worker 	unsigned int free_segments;
203*59bfda1fSAndroid Build Coastguard Worker };
204*59bfda1fSAndroid Build Coastguard Worker 
205*59bfda1fSAndroid Build Coastguard Worker struct f2fs_dentry_ptr {
206*59bfda1fSAndroid Build Coastguard Worker 	struct inode *inode;
207*59bfda1fSAndroid Build Coastguard Worker 	u8 *bitmap;
208*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_dir_entry *dentry;
209*59bfda1fSAndroid Build Coastguard Worker 	__u8 (*filename)[F2FS_SLOT_LEN];
210*59bfda1fSAndroid Build Coastguard Worker 	int max;
211*59bfda1fSAndroid Build Coastguard Worker 	int nr_bitmap;
212*59bfda1fSAndroid Build Coastguard Worker };
213*59bfda1fSAndroid Build Coastguard Worker 
214*59bfda1fSAndroid Build Coastguard Worker struct dentry {
215*59bfda1fSAndroid Build Coastguard Worker 	char *path;
216*59bfda1fSAndroid Build Coastguard Worker 	char *full_path;
217*59bfda1fSAndroid Build Coastguard Worker 	const u8 *name;
218*59bfda1fSAndroid Build Coastguard Worker 	int len;
219*59bfda1fSAndroid Build Coastguard Worker 	char *link;
220*59bfda1fSAndroid Build Coastguard Worker 	unsigned long size;
221*59bfda1fSAndroid Build Coastguard Worker 	u8 file_type;
222*59bfda1fSAndroid Build Coastguard Worker 	u16 mode;
223*59bfda1fSAndroid Build Coastguard Worker 	u16 uid;
224*59bfda1fSAndroid Build Coastguard Worker 	u16 gid;
225*59bfda1fSAndroid Build Coastguard Worker 	u32 *inode;
226*59bfda1fSAndroid Build Coastguard Worker 	u32 mtime;
227*59bfda1fSAndroid Build Coastguard Worker 	char *secon;
228*59bfda1fSAndroid Build Coastguard Worker 	uint64_t capabilities;
229*59bfda1fSAndroid Build Coastguard Worker 	nid_t ino;
230*59bfda1fSAndroid Build Coastguard Worker 	nid_t pino;
231*59bfda1fSAndroid Build Coastguard Worker 	u64 from_devino;
232*59bfda1fSAndroid Build Coastguard Worker };
233*59bfda1fSAndroid Build Coastguard Worker 
234*59bfda1fSAndroid Build Coastguard Worker /* different from dnode_of_data in kernel */
235*59bfda1fSAndroid Build Coastguard Worker struct dnode_of_data {
236*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_node *inode_blk;	/* inode page */
237*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_node *node_blk;	/* cached direct node page */
238*59bfda1fSAndroid Build Coastguard Worker 	nid_t nid;
239*59bfda1fSAndroid Build Coastguard Worker 	unsigned int ofs_in_node;
240*59bfda1fSAndroid Build Coastguard Worker 	block_t data_blkaddr;
241*59bfda1fSAndroid Build Coastguard Worker 	block_t node_blkaddr;
242*59bfda1fSAndroid Build Coastguard Worker 	int idirty, ndirty, alloced;
243*59bfda1fSAndroid Build Coastguard Worker };
244*59bfda1fSAndroid Build Coastguard Worker 
245*59bfda1fSAndroid Build Coastguard Worker struct hardlink_cache_entry {
246*59bfda1fSAndroid Build Coastguard Worker 	u64 from_devino;
247*59bfda1fSAndroid Build Coastguard Worker 	nid_t to_ino;
248*59bfda1fSAndroid Build Coastguard Worker 	int nbuild;
249*59bfda1fSAndroid Build Coastguard Worker };
250*59bfda1fSAndroid Build Coastguard Worker 
251*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sb_info {
252*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_fsck *fsck;
253*59bfda1fSAndroid Build Coastguard Worker 
254*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_super_block *raw_super;
255*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_nm_info *nm_info;
256*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_sm_info *sm_info;
257*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_checkpoint *ckpt;
258*59bfda1fSAndroid Build Coastguard Worker 	int cur_cp;
259*59bfda1fSAndroid Build Coastguard Worker 
260*59bfda1fSAndroid Build Coastguard Worker 	/* basic file system units */
261*59bfda1fSAndroid Build Coastguard Worker 	unsigned int log_sectors_per_block;     /* log2 sectors per block */
262*59bfda1fSAndroid Build Coastguard Worker 	unsigned int log_blocksize;             /* log2 block size */
263*59bfda1fSAndroid Build Coastguard Worker 	unsigned int blocksize;                 /* block size */
264*59bfda1fSAndroid Build Coastguard Worker 	unsigned int root_ino_num;              /* root inode number*/
265*59bfda1fSAndroid Build Coastguard Worker 	unsigned int node_ino_num;              /* node inode number*/
266*59bfda1fSAndroid Build Coastguard Worker 	unsigned int meta_ino_num;              /* meta inode number*/
267*59bfda1fSAndroid Build Coastguard Worker 	unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
268*59bfda1fSAndroid Build Coastguard Worker 	unsigned int blocks_per_seg;            /* blocks per segment */
269*59bfda1fSAndroid Build Coastguard Worker 	unsigned int segs_per_sec;              /* segments per section */
270*59bfda1fSAndroid Build Coastguard Worker 	unsigned int secs_per_zone;             /* sections per zone */
271*59bfda1fSAndroid Build Coastguard Worker 	unsigned int total_sections;            /* total section count */
272*59bfda1fSAndroid Build Coastguard Worker 	unsigned int total_node_count;          /* total node block count */
273*59bfda1fSAndroid Build Coastguard Worker 	unsigned int total_valid_node_count;    /* valid node block count */
274*59bfda1fSAndroid Build Coastguard Worker 	unsigned int total_valid_inode_count;   /* valid inode count */
275*59bfda1fSAndroid Build Coastguard Worker 	int active_logs;                        /* # of active logs */
276*59bfda1fSAndroid Build Coastguard Worker 
277*59bfda1fSAndroid Build Coastguard Worker 	block_t user_block_count;               /* # of user blocks */
278*59bfda1fSAndroid Build Coastguard Worker 	block_t total_valid_block_count;        /* # of valid blocks */
279*59bfda1fSAndroid Build Coastguard Worker 	block_t alloc_valid_block_count;        /* # of allocated blocks */
280*59bfda1fSAndroid Build Coastguard Worker 	block_t last_valid_block_count;         /* for recovery */
281*59bfda1fSAndroid Build Coastguard Worker 	u32 s_next_generation;                  /* for NFS support */
282*59bfda1fSAndroid Build Coastguard Worker 
283*59bfda1fSAndroid Build Coastguard Worker 	unsigned int cur_victim_sec;            /* current victim section num */
284*59bfda1fSAndroid Build Coastguard Worker 	u32 free_segments;
285*59bfda1fSAndroid Build Coastguard Worker 
286*59bfda1fSAndroid Build Coastguard Worker 	int cp_backuped;			/* backup valid checkpoint */
287*59bfda1fSAndroid Build Coastguard Worker 
288*59bfda1fSAndroid Build Coastguard Worker 	/* true if late_build_segment_manger() is called */
289*59bfda1fSAndroid Build Coastguard Worker 	bool seg_manager_done;
290*59bfda1fSAndroid Build Coastguard Worker 
291*59bfda1fSAndroid Build Coastguard Worker 	/* keep track of hardlinks so we can recreate them */
292*59bfda1fSAndroid Build Coastguard Worker 	void *hardlink_cache;
293*59bfda1fSAndroid Build Coastguard Worker };
294*59bfda1fSAndroid Build Coastguard Worker 
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)295*59bfda1fSAndroid Build Coastguard Worker static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
296*59bfda1fSAndroid Build Coastguard Worker {
297*59bfda1fSAndroid Build Coastguard Worker 	return (struct f2fs_super_block *)(sbi->raw_super);
298*59bfda1fSAndroid Build Coastguard Worker }
299*59bfda1fSAndroid Build Coastguard Worker 
F2FS_CKPT(struct f2fs_sb_info * sbi)300*59bfda1fSAndroid Build Coastguard Worker static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
301*59bfda1fSAndroid Build Coastguard Worker {
302*59bfda1fSAndroid Build Coastguard Worker 	return (struct f2fs_checkpoint *)(sbi->ckpt);
303*59bfda1fSAndroid Build Coastguard Worker }
304*59bfda1fSAndroid Build Coastguard Worker 
F2FS_FSCK(struct f2fs_sb_info * sbi)305*59bfda1fSAndroid Build Coastguard Worker static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
306*59bfda1fSAndroid Build Coastguard Worker {
307*59bfda1fSAndroid Build Coastguard Worker 	return (struct f2fs_fsck *)(sbi->fsck);
308*59bfda1fSAndroid Build Coastguard Worker }
309*59bfda1fSAndroid Build Coastguard Worker 
NM_I(struct f2fs_sb_info * sbi)310*59bfda1fSAndroid Build Coastguard Worker static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
311*59bfda1fSAndroid Build Coastguard Worker {
312*59bfda1fSAndroid Build Coastguard Worker 	return (struct f2fs_nm_info *)(sbi->nm_info);
313*59bfda1fSAndroid Build Coastguard Worker }
314*59bfda1fSAndroid Build Coastguard Worker 
SM_I(struct f2fs_sb_info * sbi)315*59bfda1fSAndroid Build Coastguard Worker static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
316*59bfda1fSAndroid Build Coastguard Worker {
317*59bfda1fSAndroid Build Coastguard Worker 	return (struct f2fs_sm_info *)(sbi->sm_info);
318*59bfda1fSAndroid Build Coastguard Worker }
319*59bfda1fSAndroid Build Coastguard Worker 
SIT_I(struct f2fs_sb_info * sbi)320*59bfda1fSAndroid Build Coastguard Worker static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
321*59bfda1fSAndroid Build Coastguard Worker {
322*59bfda1fSAndroid Build Coastguard Worker 	return (struct sit_info *)(SM_I(sbi)->sit_info);
323*59bfda1fSAndroid Build Coastguard Worker }
324*59bfda1fSAndroid Build Coastguard Worker 
inline_data_addr(struct f2fs_node * node_blk)325*59bfda1fSAndroid Build Coastguard Worker static inline void *inline_data_addr(struct f2fs_node *node_blk)
326*59bfda1fSAndroid Build Coastguard Worker {
327*59bfda1fSAndroid Build Coastguard Worker 	int ofs = get_extra_isize(node_blk) + DEF_INLINE_RESERVED_SIZE;
328*59bfda1fSAndroid Build Coastguard Worker 
329*59bfda1fSAndroid Build Coastguard Worker 	return (void *)&(node_blk->i.i_addr[ofs]);
330*59bfda1fSAndroid Build Coastguard Worker }
331*59bfda1fSAndroid Build Coastguard Worker 
ofs_of_node(struct f2fs_node * node_blk)332*59bfda1fSAndroid Build Coastguard Worker static inline unsigned int ofs_of_node(struct f2fs_node *node_blk)
333*59bfda1fSAndroid Build Coastguard Worker {
334*59bfda1fSAndroid Build Coastguard Worker 	unsigned flag = le32_to_cpu(F2FS_NODE_FOOTER(node_blk)->flag);
335*59bfda1fSAndroid Build Coastguard Worker 	return flag >> OFFSET_BIT_SHIFT;
336*59bfda1fSAndroid Build Coastguard Worker }
337*59bfda1fSAndroid Build Coastguard Worker 
cur_cp_version(struct f2fs_checkpoint * cp)338*59bfda1fSAndroid Build Coastguard Worker static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
339*59bfda1fSAndroid Build Coastguard Worker {
340*59bfda1fSAndroid Build Coastguard Worker 	return le64_to_cpu(cp->checkpoint_ver);
341*59bfda1fSAndroid Build Coastguard Worker }
342*59bfda1fSAndroid Build Coastguard Worker 
cur_cp_crc(struct f2fs_checkpoint * cp)343*59bfda1fSAndroid Build Coastguard Worker static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
344*59bfda1fSAndroid Build Coastguard Worker {
345*59bfda1fSAndroid Build Coastguard Worker 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
346*59bfda1fSAndroid Build Coastguard Worker 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
347*59bfda1fSAndroid Build Coastguard Worker }
348*59bfda1fSAndroid Build Coastguard Worker 
is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)349*59bfda1fSAndroid Build Coastguard Worker static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
350*59bfda1fSAndroid Build Coastguard Worker {
351*59bfda1fSAndroid Build Coastguard Worker 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
352*59bfda1fSAndroid Build Coastguard Worker 	return ckpt_flags & f ? 1 : 0;
353*59bfda1fSAndroid Build Coastguard Worker }
354*59bfda1fSAndroid Build Coastguard Worker 
__bitmap_size(struct f2fs_sb_info * sbi,int flag)355*59bfda1fSAndroid Build Coastguard Worker static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
356*59bfda1fSAndroid Build Coastguard Worker {
357*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
358*59bfda1fSAndroid Build Coastguard Worker 
359*59bfda1fSAndroid Build Coastguard Worker 	/* return NAT or SIT bitmap */
360*59bfda1fSAndroid Build Coastguard Worker 	if (flag == NAT_BITMAP)
361*59bfda1fSAndroid Build Coastguard Worker 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
362*59bfda1fSAndroid Build Coastguard Worker 	else if (flag == SIT_BITMAP)
363*59bfda1fSAndroid Build Coastguard Worker 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
364*59bfda1fSAndroid Build Coastguard Worker 
365*59bfda1fSAndroid Build Coastguard Worker 	return 0;
366*59bfda1fSAndroid Build Coastguard Worker }
367*59bfda1fSAndroid Build Coastguard Worker 
__cp_payload(struct f2fs_sb_info * sbi)368*59bfda1fSAndroid Build Coastguard Worker static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
369*59bfda1fSAndroid Build Coastguard Worker {
370*59bfda1fSAndroid Build Coastguard Worker 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
371*59bfda1fSAndroid Build Coastguard Worker }
372*59bfda1fSAndroid Build Coastguard Worker 
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)373*59bfda1fSAndroid Build Coastguard Worker static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
374*59bfda1fSAndroid Build Coastguard Worker {
375*59bfda1fSAndroid Build Coastguard Worker 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
376*59bfda1fSAndroid Build Coastguard Worker 	int offset;
377*59bfda1fSAndroid Build Coastguard Worker 
378*59bfda1fSAndroid Build Coastguard Worker 	if (is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG)) {
379*59bfda1fSAndroid Build Coastguard Worker 		unsigned int chksum_size = 0;
380*59bfda1fSAndroid Build Coastguard Worker 
381*59bfda1fSAndroid Build Coastguard Worker 		offset = (flag == SIT_BITMAP) ?
382*59bfda1fSAndroid Build Coastguard Worker 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
383*59bfda1fSAndroid Build Coastguard Worker 
384*59bfda1fSAndroid Build Coastguard Worker 		if (le32_to_cpu(ckpt->checksum_offset) ==
385*59bfda1fSAndroid Build Coastguard Worker 					CP_MIN_CHKSUM_OFFSET)
386*59bfda1fSAndroid Build Coastguard Worker 			chksum_size = sizeof(__le32);
387*59bfda1fSAndroid Build Coastguard Worker 
388*59bfda1fSAndroid Build Coastguard Worker 		return &ckpt->sit_nat_version_bitmap[offset + chksum_size];
389*59bfda1fSAndroid Build Coastguard Worker 	}
390*59bfda1fSAndroid Build Coastguard Worker 
391*59bfda1fSAndroid Build Coastguard Worker 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
392*59bfda1fSAndroid Build Coastguard Worker 		if (flag == NAT_BITMAP)
393*59bfda1fSAndroid Build Coastguard Worker 			return &ckpt->sit_nat_version_bitmap;
394*59bfda1fSAndroid Build Coastguard Worker 		else
395*59bfda1fSAndroid Build Coastguard Worker 			return ((char *)ckpt + F2FS_BLKSIZE);
396*59bfda1fSAndroid Build Coastguard Worker 	} else {
397*59bfda1fSAndroid Build Coastguard Worker 		offset = (flag == NAT_BITMAP) ?
398*59bfda1fSAndroid Build Coastguard Worker 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
399*59bfda1fSAndroid Build Coastguard Worker 		return &ckpt->sit_nat_version_bitmap[offset];
400*59bfda1fSAndroid Build Coastguard Worker 	}
401*59bfda1fSAndroid Build Coastguard Worker }
402*59bfda1fSAndroid Build Coastguard Worker 
__start_cp_addr(struct f2fs_sb_info * sbi)403*59bfda1fSAndroid Build Coastguard Worker static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
404*59bfda1fSAndroid Build Coastguard Worker {
405*59bfda1fSAndroid Build Coastguard Worker 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
406*59bfda1fSAndroid Build Coastguard Worker 
407*59bfda1fSAndroid Build Coastguard Worker 	if (sbi->cur_cp == 2)
408*59bfda1fSAndroid Build Coastguard Worker 		start_addr += sbi->blocks_per_seg;
409*59bfda1fSAndroid Build Coastguard Worker 	return start_addr;
410*59bfda1fSAndroid Build Coastguard Worker }
411*59bfda1fSAndroid Build Coastguard Worker 
__start_sum_addr(struct f2fs_sb_info * sbi)412*59bfda1fSAndroid Build Coastguard Worker static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
413*59bfda1fSAndroid Build Coastguard Worker {
414*59bfda1fSAndroid Build Coastguard Worker 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
415*59bfda1fSAndroid Build Coastguard Worker }
416*59bfda1fSAndroid Build Coastguard Worker 
__end_block_addr(struct f2fs_sb_info * sbi)417*59bfda1fSAndroid Build Coastguard Worker static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
418*59bfda1fSAndroid Build Coastguard Worker {
419*59bfda1fSAndroid Build Coastguard Worker 	return SM_I(sbi)->main_blkaddr +
420*59bfda1fSAndroid Build Coastguard Worker 		(le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <<
421*59bfda1fSAndroid Build Coastguard Worker 		sbi->log_blocks_per_seg);
422*59bfda1fSAndroid Build Coastguard Worker }
423*59bfda1fSAndroid Build Coastguard Worker 
424*59bfda1fSAndroid Build Coastguard Worker #define BLKS_PER_SEC(sbi)						\
425*59bfda1fSAndroid Build Coastguard Worker 	((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
426*59bfda1fSAndroid Build Coastguard Worker #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
427*59bfda1fSAndroid Build Coastguard Worker 	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
428*59bfda1fSAndroid Build Coastguard Worker 
429*59bfda1fSAndroid Build Coastguard Worker #define IS_DATASEG(t)                                                   \
430*59bfda1fSAndroid Build Coastguard Worker 	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
431*59bfda1fSAndroid Build Coastguard Worker 	 (t == CURSEG_WARM_DATA))
432*59bfda1fSAndroid Build Coastguard Worker 
433*59bfda1fSAndroid Build Coastguard Worker #define IS_NODESEG(t)                                                   \
434*59bfda1fSAndroid Build Coastguard Worker 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
435*59bfda1fSAndroid Build Coastguard Worker 	 (t == CURSEG_WARM_NODE))
436*59bfda1fSAndroid Build Coastguard Worker 
437*59bfda1fSAndroid Build Coastguard Worker #define MAIN_BLKADDR(sbi)						\
438*59bfda1fSAndroid Build Coastguard Worker 	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr :				\
439*59bfda1fSAndroid Build Coastguard Worker 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
440*59bfda1fSAndroid Build Coastguard Worker #define SEG0_BLKADDR(sbi)						\
441*59bfda1fSAndroid Build Coastguard Worker 	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr :				\
442*59bfda1fSAndroid Build Coastguard Worker 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
443*59bfda1fSAndroid Build Coastguard Worker 
444*59bfda1fSAndroid Build Coastguard Worker #define GET_SUM_BLKADDR(sbi, segno)					\
445*59bfda1fSAndroid Build Coastguard Worker 	((sbi->sm_info->ssa_blkaddr) + segno)
446*59bfda1fSAndroid Build Coastguard Worker 
447*59bfda1fSAndroid Build Coastguard Worker #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
448*59bfda1fSAndroid Build Coastguard Worker 	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
449*59bfda1fSAndroid Build Coastguard Worker 
450*59bfda1fSAndroid Build Coastguard Worker #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
451*59bfda1fSAndroid Build Coastguard Worker 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
452*59bfda1fSAndroid Build Coastguard Worker 
453*59bfda1fSAndroid Build Coastguard Worker #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
454*59bfda1fSAndroid Build Coastguard Worker 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
455*59bfda1fSAndroid Build Coastguard Worker 
456*59bfda1fSAndroid Build Coastguard Worker #define GET_SEC_FROM_SEG(sbi, segno)					\
457*59bfda1fSAndroid Build Coastguard Worker 	((segno) / (sbi)->segs_per_sec)
458*59bfda1fSAndroid Build Coastguard Worker #define GET_SEG_FROM_SEC(sbi, secno)					\
459*59bfda1fSAndroid Build Coastguard Worker 	((secno) * (sbi)->segs_per_sec)
460*59bfda1fSAndroid Build Coastguard Worker 
461*59bfda1fSAndroid Build Coastguard Worker #define FREE_I_START_SEGNO(sbi)						\
462*59bfda1fSAndroid Build Coastguard Worker 	GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
463*59bfda1fSAndroid Build Coastguard Worker #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
464*59bfda1fSAndroid Build Coastguard Worker 
465*59bfda1fSAndroid Build Coastguard Worker #define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
466*59bfda1fSAndroid Build Coastguard Worker #define TOTAL_SEGS(sbi)	(SM_I(sbi)->segment_count)
467*59bfda1fSAndroid Build Coastguard Worker #define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
468*59bfda1fSAndroid Build Coastguard Worker #define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
469*59bfda1fSAndroid Build Coastguard Worker 
470*59bfda1fSAndroid Build Coastguard Worker #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
471*59bfda1fSAndroid Build Coastguard Worker 	((segno) << sbi->log_blocks_per_seg))
472*59bfda1fSAndroid Build Coastguard Worker 
473*59bfda1fSAndroid Build Coastguard Worker #define NEXT_FREE_BLKADDR(sbi, curseg)					\
474*59bfda1fSAndroid Build Coastguard Worker 	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
475*59bfda1fSAndroid Build Coastguard Worker 
476*59bfda1fSAndroid Build Coastguard Worker #define SIT_BLK_CNT(sbi)						\
477*59bfda1fSAndroid Build Coastguard Worker 	((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
478*59bfda1fSAndroid Build Coastguard Worker 
CURSEG_I(struct f2fs_sb_info * sbi,int type)479*59bfda1fSAndroid Build Coastguard Worker static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
480*59bfda1fSAndroid Build Coastguard Worker {
481*59bfda1fSAndroid Build Coastguard Worker 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
482*59bfda1fSAndroid Build Coastguard Worker }
483*59bfda1fSAndroid Build Coastguard Worker 
start_sum_block(struct f2fs_sb_info * sbi)484*59bfda1fSAndroid Build Coastguard Worker static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
485*59bfda1fSAndroid Build Coastguard Worker {
486*59bfda1fSAndroid Build Coastguard Worker 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
487*59bfda1fSAndroid Build Coastguard Worker }
488*59bfda1fSAndroid Build Coastguard Worker 
sum_blk_addr(struct f2fs_sb_info * sbi,int base,int type)489*59bfda1fSAndroid Build Coastguard Worker static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
490*59bfda1fSAndroid Build Coastguard Worker {
491*59bfda1fSAndroid Build Coastguard Worker 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
492*59bfda1fSAndroid Build Coastguard Worker 		- (base + 1) + type;
493*59bfda1fSAndroid Build Coastguard Worker }
494*59bfda1fSAndroid Build Coastguard Worker 
495*59bfda1fSAndroid Build Coastguard Worker /* for the list of fsync inodes, used only during recovery */
496*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry {
497*59bfda1fSAndroid Build Coastguard Worker 	struct list_head list;	/* list head */
498*59bfda1fSAndroid Build Coastguard Worker 	nid_t ino;		/* inode number */
499*59bfda1fSAndroid Build Coastguard Worker 	block_t blkaddr;	/* block address locating the last fsync */
500*59bfda1fSAndroid Build Coastguard Worker 	block_t last_dentry;	/* block address locating the last dentry */
501*59bfda1fSAndroid Build Coastguard Worker };
502*59bfda1fSAndroid Build Coastguard Worker 
503*59bfda1fSAndroid Build Coastguard Worker #define nats_in_cursum(jnl)             (le16_to_cpu(jnl->n_nats))
504*59bfda1fSAndroid Build Coastguard Worker #define sits_in_cursum(jnl)             (le16_to_cpu(jnl->n_sits))
505*59bfda1fSAndroid Build Coastguard Worker 
506*59bfda1fSAndroid Build Coastguard Worker #define nat_in_journal(jnl, i)          (jnl->nat_j.entries[i].ne)
507*59bfda1fSAndroid Build Coastguard Worker #define nid_in_journal(jnl, i)          (jnl->nat_j.entries[i].nid)
508*59bfda1fSAndroid Build Coastguard Worker #define sit_in_journal(jnl, i)          (jnl->sit_j.entries[i].se)
509*59bfda1fSAndroid Build Coastguard Worker #define segno_in_journal(jnl, i)        (jnl->sit_j.entries[i].segno)
510*59bfda1fSAndroid Build Coastguard Worker 
511*59bfda1fSAndroid Build Coastguard Worker #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
512*59bfda1fSAndroid Build Coastguard Worker 	((segno) % sit_i->sents_per_block)
513*59bfda1fSAndroid Build Coastguard Worker #define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
514*59bfda1fSAndroid Build Coastguard Worker 	((segno) / SIT_ENTRY_PER_BLOCK)
515*59bfda1fSAndroid Build Coastguard Worker 
IS_VALID_NID(struct f2fs_sb_info * sbi,u32 nid)516*59bfda1fSAndroid Build Coastguard Worker static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
517*59bfda1fSAndroid Build Coastguard Worker {
518*59bfda1fSAndroid Build Coastguard Worker 	return (nid < (NAT_ENTRY_PER_BLOCK *
519*59bfda1fSAndroid Build Coastguard Worker 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)
520*59bfda1fSAndroid Build Coastguard Worker 			<< (sbi->log_blocks_per_seg - 1)));
521*59bfda1fSAndroid Build Coastguard Worker }
522*59bfda1fSAndroid Build Coastguard Worker 
is_valid_data_blkaddr(block_t blkaddr)523*59bfda1fSAndroid Build Coastguard Worker static inline bool is_valid_data_blkaddr(block_t blkaddr)
524*59bfda1fSAndroid Build Coastguard Worker {
525*59bfda1fSAndroid Build Coastguard Worker 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
526*59bfda1fSAndroid Build Coastguard Worker 				blkaddr == COMPRESS_ADDR)
527*59bfda1fSAndroid Build Coastguard Worker 		return 0;
528*59bfda1fSAndroid Build Coastguard Worker 	return 1;
529*59bfda1fSAndroid Build Coastguard Worker }
530*59bfda1fSAndroid Build Coastguard Worker 
IS_CUR_SEGNO(struct f2fs_sb_info * sbi,u32 segno)531*59bfda1fSAndroid Build Coastguard Worker static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno)
532*59bfda1fSAndroid Build Coastguard Worker {
533*59bfda1fSAndroid Build Coastguard Worker 	int i;
534*59bfda1fSAndroid Build Coastguard Worker 
535*59bfda1fSAndroid Build Coastguard Worker 	for (i = 0; i < NO_CHECK_TYPE; i++) {
536*59bfda1fSAndroid Build Coastguard Worker 		struct curseg_info *curseg = CURSEG_I(sbi, i);
537*59bfda1fSAndroid Build Coastguard Worker 
538*59bfda1fSAndroid Build Coastguard Worker 		if (segno == curseg->segno)
539*59bfda1fSAndroid Build Coastguard Worker 			return 1;
540*59bfda1fSAndroid Build Coastguard Worker 	}
541*59bfda1fSAndroid Build Coastguard Worker 	return 0;
542*59bfda1fSAndroid Build Coastguard Worker }
543*59bfda1fSAndroid Build Coastguard Worker 
BLKOFF_FROM_MAIN(struct f2fs_sb_info * sbi,u64 blk_addr)544*59bfda1fSAndroid Build Coastguard Worker static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
545*59bfda1fSAndroid Build Coastguard Worker {
546*59bfda1fSAndroid Build Coastguard Worker 	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
547*59bfda1fSAndroid Build Coastguard Worker 	return blk_addr - SM_I(sbi)->main_blkaddr;
548*59bfda1fSAndroid Build Coastguard Worker }
549*59bfda1fSAndroid Build Coastguard Worker 
GET_SEGNO(struct f2fs_sb_info * sbi,u64 blk_addr)550*59bfda1fSAndroid Build Coastguard Worker static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
551*59bfda1fSAndroid Build Coastguard Worker {
552*59bfda1fSAndroid Build Coastguard Worker 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
553*59bfda1fSAndroid Build Coastguard Worker 			>> sbi->log_blocks_per_seg);
554*59bfda1fSAndroid Build Coastguard Worker }
555*59bfda1fSAndroid Build Coastguard Worker 
OFFSET_IN_SEG(struct f2fs_sb_info * sbi,u64 blk_addr)556*59bfda1fSAndroid Build Coastguard Worker static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
557*59bfda1fSAndroid Build Coastguard Worker {
558*59bfda1fSAndroid Build Coastguard Worker 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
559*59bfda1fSAndroid Build Coastguard Worker 			% (1 << sbi->log_blocks_per_seg));
560*59bfda1fSAndroid Build Coastguard Worker }
561*59bfda1fSAndroid Build Coastguard Worker 
node_info_from_raw_nat(struct node_info * ni,struct f2fs_nat_entry * raw_nat)562*59bfda1fSAndroid Build Coastguard Worker static inline void node_info_from_raw_nat(struct node_info *ni,
563*59bfda1fSAndroid Build Coastguard Worker 		struct f2fs_nat_entry *raw_nat)
564*59bfda1fSAndroid Build Coastguard Worker {
565*59bfda1fSAndroid Build Coastguard Worker 	ni->ino = le32_to_cpu(raw_nat->ino);
566*59bfda1fSAndroid Build Coastguard Worker 	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
567*59bfda1fSAndroid Build Coastguard Worker 	ni->version = raw_nat->version;
568*59bfda1fSAndroid Build Coastguard Worker }
569*59bfda1fSAndroid Build Coastguard Worker 
set_summary(struct f2fs_summary * sum,nid_t nid,unsigned int ofs_in_node,unsigned char version)570*59bfda1fSAndroid Build Coastguard Worker static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
571*59bfda1fSAndroid Build Coastguard Worker 			unsigned int ofs_in_node, unsigned char version)
572*59bfda1fSAndroid Build Coastguard Worker {
573*59bfda1fSAndroid Build Coastguard Worker 	sum->nid = cpu_to_le32(nid);
574*59bfda1fSAndroid Build Coastguard Worker 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
575*59bfda1fSAndroid Build Coastguard Worker 	sum->version = version;
576*59bfda1fSAndroid Build Coastguard Worker }
577*59bfda1fSAndroid Build Coastguard Worker 
578*59bfda1fSAndroid Build Coastguard Worker #define S_SHIFT 12
579*59bfda1fSAndroid Build Coastguard Worker static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
580*59bfda1fSAndroid Build Coastguard Worker 	[S_IFREG >> S_SHIFT]    = F2FS_FT_REG_FILE,
581*59bfda1fSAndroid Build Coastguard Worker 	[S_IFDIR >> S_SHIFT]    = F2FS_FT_DIR,
582*59bfda1fSAndroid Build Coastguard Worker 	[S_IFCHR >> S_SHIFT]    = F2FS_FT_CHRDEV,
583*59bfda1fSAndroid Build Coastguard Worker 	[S_IFBLK >> S_SHIFT]    = F2FS_FT_BLKDEV,
584*59bfda1fSAndroid Build Coastguard Worker 	[S_IFIFO >> S_SHIFT]    = F2FS_FT_FIFO,
585*59bfda1fSAndroid Build Coastguard Worker #ifdef S_IFSOCK
586*59bfda1fSAndroid Build Coastguard Worker 	[S_IFSOCK >> S_SHIFT]   = F2FS_FT_SOCK,
587*59bfda1fSAndroid Build Coastguard Worker #endif
588*59bfda1fSAndroid Build Coastguard Worker #ifdef S_IFLNK
589*59bfda1fSAndroid Build Coastguard Worker 	[S_IFLNK >> S_SHIFT]    = F2FS_FT_SYMLINK,
590*59bfda1fSAndroid Build Coastguard Worker #endif
591*59bfda1fSAndroid Build Coastguard Worker };
592*59bfda1fSAndroid Build Coastguard Worker 
map_de_type(umode_t mode)593*59bfda1fSAndroid Build Coastguard Worker static inline int map_de_type(umode_t mode)
594*59bfda1fSAndroid Build Coastguard Worker {
595*59bfda1fSAndroid Build Coastguard Worker        return f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
596*59bfda1fSAndroid Build Coastguard Worker }
597*59bfda1fSAndroid Build Coastguard Worker 
inline_xattr_addr(struct f2fs_inode * inode)598*59bfda1fSAndroid Build Coastguard Worker static inline void *inline_xattr_addr(struct f2fs_inode *inode)
599*59bfda1fSAndroid Build Coastguard Worker {
600*59bfda1fSAndroid Build Coastguard Worker 	return (void *)&(inode->i_addr[DEF_ADDRS_PER_INODE -
601*59bfda1fSAndroid Build Coastguard Worker 				get_inline_xattr_addrs(inode)]);
602*59bfda1fSAndroid Build Coastguard Worker }
603*59bfda1fSAndroid Build Coastguard Worker 
inline_xattr_size(struct f2fs_inode * inode)604*59bfda1fSAndroid Build Coastguard Worker static inline int inline_xattr_size(struct f2fs_inode *inode)
605*59bfda1fSAndroid Build Coastguard Worker {
606*59bfda1fSAndroid Build Coastguard Worker 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
607*59bfda1fSAndroid Build Coastguard Worker }
608*59bfda1fSAndroid Build Coastguard Worker 
609*59bfda1fSAndroid Build Coastguard Worker extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
610*59bfda1fSAndroid Build Coastguard Worker #define IS_SUM_NODE_SEG(sum)		(F2FS_SUMMARY_BLOCK_FOOTER(sum)->entry_type == SUM_TYPE_NODE)
611*59bfda1fSAndroid Build Coastguard Worker #define IS_SUM_DATA_SEG(sum)		(F2FS_SUMMARY_BLOCK_FOOTER(sum)->entry_type == SUM_TYPE_DATA)
612*59bfda1fSAndroid Build Coastguard Worker 
dir_buckets(unsigned int level,int dir_level)613*59bfda1fSAndroid Build Coastguard Worker static inline unsigned int dir_buckets(unsigned int level, int dir_level)
614*59bfda1fSAndroid Build Coastguard Worker {
615*59bfda1fSAndroid Build Coastguard Worker 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
616*59bfda1fSAndroid Build Coastguard Worker 		return 1 << (level + dir_level);
617*59bfda1fSAndroid Build Coastguard Worker 	else
618*59bfda1fSAndroid Build Coastguard Worker 		return MAX_DIR_BUCKETS;
619*59bfda1fSAndroid Build Coastguard Worker }
620*59bfda1fSAndroid Build Coastguard Worker 
bucket_blocks(unsigned int level)621*59bfda1fSAndroid Build Coastguard Worker static inline unsigned int bucket_blocks(unsigned int level)
622*59bfda1fSAndroid Build Coastguard Worker {
623*59bfda1fSAndroid Build Coastguard Worker 	if (level < MAX_DIR_HASH_DEPTH / 2)
624*59bfda1fSAndroid Build Coastguard Worker 		return 2;
625*59bfda1fSAndroid Build Coastguard Worker 	else
626*59bfda1fSAndroid Build Coastguard Worker 		return 4;
627*59bfda1fSAndroid Build Coastguard Worker }
628*59bfda1fSAndroid Build Coastguard Worker 
dir_block_index(unsigned int level,int dir_level,unsigned int idx)629*59bfda1fSAndroid Build Coastguard Worker static inline unsigned long dir_block_index(unsigned int level,
630*59bfda1fSAndroid Build Coastguard Worker 				int dir_level, unsigned int idx)
631*59bfda1fSAndroid Build Coastguard Worker {
632*59bfda1fSAndroid Build Coastguard Worker 	unsigned long i;
633*59bfda1fSAndroid Build Coastguard Worker 	unsigned long bidx = 0;
634*59bfda1fSAndroid Build Coastguard Worker 
635*59bfda1fSAndroid Build Coastguard Worker 	for (i = 0; i < level; i++)
636*59bfda1fSAndroid Build Coastguard Worker 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
637*59bfda1fSAndroid Build Coastguard Worker 	bidx += idx * bucket_blocks(level);
638*59bfda1fSAndroid Build Coastguard Worker 	return bidx;
639*59bfda1fSAndroid Build Coastguard Worker }
640*59bfda1fSAndroid Build Coastguard Worker 
is_dot_dotdot(const unsigned char * name,const int len)641*59bfda1fSAndroid Build Coastguard Worker static inline int is_dot_dotdot(const unsigned char *name, const int len)
642*59bfda1fSAndroid Build Coastguard Worker {
643*59bfda1fSAndroid Build Coastguard Worker 	if (len == 1 && name[0] == '.')
644*59bfda1fSAndroid Build Coastguard Worker 		return 1;
645*59bfda1fSAndroid Build Coastguard Worker 	if (len == 2 && name[0] == '.' && name[1] == '.')
646*59bfda1fSAndroid Build Coastguard Worker 		return 1;
647*59bfda1fSAndroid Build Coastguard Worker 	return 0;
648*59bfda1fSAndroid Build Coastguard Worker }
649*59bfda1fSAndroid Build Coastguard Worker 
get_encoding(struct f2fs_sb_info * sbi)650*59bfda1fSAndroid Build Coastguard Worker static inline int get_encoding(struct f2fs_sb_info *sbi)
651*59bfda1fSAndroid Build Coastguard Worker {
652*59bfda1fSAndroid Build Coastguard Worker 	return le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding);
653*59bfda1fSAndroid Build Coastguard Worker }
654*59bfda1fSAndroid Build Coastguard Worker 
655*59bfda1fSAndroid Build Coastguard Worker #endif /* _F2FS_H_ */
656