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