• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
225 
226 /* different from dnode_of_data in kernel */
227 struct dnode_of_data {
228 	struct f2fs_node *inode_blk;	/* inode page */
229 	struct f2fs_node *node_blk;	/* cached direct node page */
230 	nid_t nid;
231 	unsigned int ofs_in_node;
232 	block_t data_blkaddr;
233 	block_t node_blkaddr;
234 	int idirty, ndirty;
235 };
236 
237 struct f2fs_sb_info {
238 	struct f2fs_fsck *fsck;
239 
240 	struct f2fs_super_block *raw_super;
241 	struct f2fs_nm_info *nm_info;
242 	struct f2fs_sm_info *sm_info;
243 	struct f2fs_checkpoint *ckpt;
244 	int cur_cp;
245 
246 	struct list_head orphan_inode_list;
247 	unsigned int n_orphans;
248 
249 	/* basic file system units */
250 	unsigned int log_sectors_per_block;     /* log2 sectors per block */
251 	unsigned int log_blocksize;             /* log2 block size */
252 	unsigned int blocksize;                 /* block size */
253 	unsigned int root_ino_num;              /* root inode number*/
254 	unsigned int node_ino_num;              /* node inode number*/
255 	unsigned int meta_ino_num;              /* meta inode number*/
256 	unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
257 	unsigned int blocks_per_seg;            /* blocks per segment */
258 	unsigned int segs_per_sec;              /* segments per section */
259 	unsigned int secs_per_zone;             /* sections per zone */
260 	unsigned int total_sections;            /* total section count */
261 	unsigned int total_node_count;          /* total node block count */
262 	unsigned int total_valid_node_count;    /* valid node block count */
263 	unsigned int total_valid_inode_count;   /* valid inode count */
264 	int active_logs;                        /* # of active logs */
265 
266 	block_t user_block_count;               /* # of user blocks */
267 	block_t total_valid_block_count;        /* # of valid blocks */
268 	block_t alloc_valid_block_count;        /* # of allocated blocks */
269 	block_t last_valid_block_count;         /* for recovery */
270 	u32 s_next_generation;                  /* for NFS support */
271 
272 	unsigned int cur_victim_sec;            /* current victim section num */
273 	u32 free_segments;
274 
275 	int cp_backuped;			/* backup valid checkpoint */
276 
277 	/* true if late_build_segment_manger() is called */
278 	bool seg_manager_done;
279 };
280 
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)281 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
282 {
283 	return (struct f2fs_super_block *)(sbi->raw_super);
284 }
285 
F2FS_CKPT(struct f2fs_sb_info * sbi)286 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
287 {
288 	return (struct f2fs_checkpoint *)(sbi->ckpt);
289 }
290 
F2FS_FSCK(struct f2fs_sb_info * sbi)291 static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
292 {
293 	return (struct f2fs_fsck *)(sbi->fsck);
294 }
295 
NM_I(struct f2fs_sb_info * sbi)296 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
297 {
298 	return (struct f2fs_nm_info *)(sbi->nm_info);
299 }
300 
SM_I(struct f2fs_sb_info * sbi)301 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
302 {
303 	return (struct f2fs_sm_info *)(sbi->sm_info);
304 }
305 
SIT_I(struct f2fs_sb_info * sbi)306 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
307 {
308 	return (struct sit_info *)(SM_I(sbi)->sit_info);
309 }
310 
inline_data_addr(struct f2fs_node * node_blk)311 static inline void *inline_data_addr(struct f2fs_node *node_blk)
312 {
313 	int ofs = get_extra_isize(node_blk) + DEF_INLINE_RESERVED_SIZE;
314 
315 	return (void *)&(node_blk->i.i_addr[ofs]);
316 }
317 
ofs_of_node(struct f2fs_node * node_blk)318 static inline unsigned int ofs_of_node(struct f2fs_node *node_blk)
319 {
320 	unsigned flag = le32_to_cpu(node_blk->footer.flag);
321 	return flag >> OFFSET_BIT_SHIFT;
322 }
323 
cur_cp_version(struct f2fs_checkpoint * cp)324 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
325 {
326 	return le64_to_cpu(cp->checkpoint_ver);
327 }
328 
cur_cp_crc(struct f2fs_checkpoint * cp)329 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
330 {
331 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
332 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
333 }
334 
is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)335 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
336 {
337 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
338 	return ckpt_flags & f ? 1 : 0;
339 }
340 
__bitmap_size(struct f2fs_sb_info * sbi,int flag)341 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
342 {
343 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
344 
345 	/* return NAT or SIT bitmap */
346 	if (flag == NAT_BITMAP)
347 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
348 	else if (flag == SIT_BITMAP)
349 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
350 
351 	return 0;
352 }
353 
__cp_payload(struct f2fs_sb_info * sbi)354 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
355 {
356 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
357 }
358 
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)359 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
360 {
361 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
362 	int offset;
363 
364 	if (is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG)) {
365 		unsigned int chksum_size = 0;
366 
367 		offset = (flag == SIT_BITMAP) ?
368 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
369 
370 		if (le32_to_cpu(ckpt->checksum_offset) ==
371 					CP_MIN_CHKSUM_OFFSET)
372 			chksum_size = sizeof(__le32);
373 
374 		return &ckpt->sit_nat_version_bitmap + offset + chksum_size;
375 	}
376 
377 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
378 		if (flag == NAT_BITMAP)
379 			return &ckpt->sit_nat_version_bitmap;
380 		else
381 			return ((char *)ckpt + F2FS_BLKSIZE);
382 	} else {
383 		offset = (flag == NAT_BITMAP) ?
384 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
385 		return &ckpt->sit_nat_version_bitmap + offset;
386 	}
387 }
388 
__start_cp_addr(struct f2fs_sb_info * sbi)389 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
390 {
391 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
392 
393 	if (sbi->cur_cp == 2)
394 		start_addr += sbi->blocks_per_seg;
395 	return start_addr;
396 }
397 
__start_sum_addr(struct f2fs_sb_info * sbi)398 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
399 {
400 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
401 }
402 
__end_block_addr(struct f2fs_sb_info * sbi)403 static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
404 {
405 	block_t end = SM_I(sbi)->main_blkaddr;
406 	return end + le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
407 }
408 
409 #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
410 	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
411 
412 #define IS_DATASEG(t)                                                   \
413 	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
414 	 (t == CURSEG_WARM_DATA))
415 
416 #define IS_NODESEG(t)                                                   \
417 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
418 	 (t == CURSEG_WARM_NODE))
419 
420 #define MAIN_BLKADDR(sbi)						\
421 	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr :				\
422 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
423 #define SEG0_BLKADDR(sbi)						\
424 	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr :				\
425 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
426 
427 #define GET_SUM_BLKADDR(sbi, segno)					\
428 	((sbi->sm_info->ssa_blkaddr) + segno)
429 
430 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
431 	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
432 
433 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
434 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
435 
436 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
437 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
438 
439 #define GET_SEC_FROM_SEG(sbi, segno)					\
440 	((segno) / (sbi)->segs_per_sec)
441 #define GET_SEG_FROM_SEC(sbi, secno)					\
442 	((secno) * (sbi)->segs_per_sec)
443 
444 #define FREE_I_START_SEGNO(sbi)						\
445 	GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
446 #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
447 
448 #define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
449 #define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
450 #define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
451 
452 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
453 	((segno) << sbi->log_blocks_per_seg))
454 
455 #define NEXT_FREE_BLKADDR(sbi, curseg)					\
456 	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
457 
458 #define SIT_BLK_CNT(sbi)						\
459 	((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
460 
CURSEG_I(struct f2fs_sb_info * sbi,int type)461 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
462 {
463 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
464 }
465 
start_sum_block(struct f2fs_sb_info * sbi)466 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
467 {
468 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
469 }
470 
sum_blk_addr(struct f2fs_sb_info * sbi,int base,int type)471 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
472 {
473 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
474 		- (base + 1) + type;
475 }
476 
477 /* for the list of fsync inodes, used only during recovery */
478 struct fsync_inode_entry {
479 	struct list_head list;	/* list head */
480 	nid_t ino;		/* inode number */
481 	block_t blkaddr;	/* block address locating the last fsync */
482 	block_t last_dentry;	/* block address locating the last dentry */
483 };
484 
485 #define nats_in_cursum(jnl)             (le16_to_cpu(jnl->n_nats))
486 #define sits_in_cursum(jnl)             (le16_to_cpu(jnl->n_sits))
487 
488 #define nat_in_journal(jnl, i)          (jnl->nat_j.entries[i].ne)
489 #define nid_in_journal(jnl, i)          (jnl->nat_j.entries[i].nid)
490 #define sit_in_journal(jnl, i)          (jnl->sit_j.entries[i].se)
491 #define segno_in_journal(jnl, i)        (jnl->sit_j.entries[i].segno)
492 
493 #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
494 	((segno) % sit_i->sents_per_block)
495 #define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
496 	((segno) / SIT_ENTRY_PER_BLOCK)
497 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
498 
IS_VALID_NID(struct f2fs_sb_info * sbi,u32 nid)499 static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
500 {
501 	return (nid < (NAT_ENTRY_PER_BLOCK *
502 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)
503 			<< (sbi->log_blocks_per_seg - 1)));
504 }
505 
IS_VALID_BLK_ADDR(struct f2fs_sb_info * sbi,u32 addr)506 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
507 {
508 	if (addr == NULL_ADDR || addr == NEW_ADDR)
509 		return 1;
510 
511 	if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
512 				addr < SM_I(sbi)->main_blkaddr) {
513 		DBG(1, "block addr [0x%x]\n", addr);
514 		return 0;
515 	}
516 	/* next block offset will be checked at the end of fsck. */
517 	return 1;
518 }
519 
IS_CUR_SEGNO(struct f2fs_sb_info * sbi,u32 segno)520 static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno)
521 {
522 	int i;
523 
524 	for (i = 0; i < NO_CHECK_TYPE; i++) {
525 		struct curseg_info *curseg = CURSEG_I(sbi, i);
526 
527 		if (segno == curseg->segno)
528 			return 1;
529 	}
530 	return 0;
531 }
532 
BLKOFF_FROM_MAIN(struct f2fs_sb_info * sbi,u64 blk_addr)533 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
534 {
535 	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
536 	return blk_addr - SM_I(sbi)->main_blkaddr;
537 }
538 
GET_SEGNO(struct f2fs_sb_info * sbi,u64 blk_addr)539 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
540 {
541 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
542 			>> sbi->log_blocks_per_seg);
543 }
544 
OFFSET_IN_SEG(struct f2fs_sb_info * sbi,u64 blk_addr)545 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
546 {
547 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
548 			% (1 << sbi->log_blocks_per_seg));
549 }
550 
node_info_from_raw_nat(struct node_info * ni,struct f2fs_nat_entry * raw_nat)551 static inline void node_info_from_raw_nat(struct node_info *ni,
552 		struct f2fs_nat_entry *raw_nat)
553 {
554 	ni->ino = le32_to_cpu(raw_nat->ino);
555 	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
556 	ni->version = raw_nat->version;
557 }
558 
set_summary(struct f2fs_summary * sum,nid_t nid,unsigned int ofs_in_node,unsigned char version)559 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
560 			unsigned int ofs_in_node, unsigned char version)
561 {
562 	sum->nid = cpu_to_le32(nid);
563 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
564 	sum->version = version;
565 }
566 
567 #define S_SHIFT 12
568 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
569 	[S_IFREG >> S_SHIFT]    = F2FS_FT_REG_FILE,
570 	[S_IFDIR >> S_SHIFT]    = F2FS_FT_DIR,
571 	[S_IFCHR >> S_SHIFT]    = F2FS_FT_CHRDEV,
572 	[S_IFBLK >> S_SHIFT]    = F2FS_FT_BLKDEV,
573 	[S_IFIFO >> S_SHIFT]    = F2FS_FT_FIFO,
574 	[S_IFSOCK >> S_SHIFT]   = F2FS_FT_SOCK,
575 	[S_IFLNK >> S_SHIFT]    = F2FS_FT_SYMLINK,
576 };
577 
map_de_type(umode_t mode)578 static inline int map_de_type(umode_t mode)
579 {
580        return f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
581 }
582 
inline_xattr_addr(struct f2fs_inode * inode)583 static inline void *inline_xattr_addr(struct f2fs_inode *inode)
584 {
585 	return (void *)&(inode->i_addr[DEF_ADDRS_PER_INODE -
586 				get_inline_xattr_addrs(inode)]);
587 }
588 
inline_xattr_size(struct f2fs_inode * inode)589 static inline int inline_xattr_size(struct f2fs_inode *inode)
590 {
591 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
592 }
593 
594 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
595 #define IS_SUM_NODE_SEG(footer)		(footer.entry_type == SUM_TYPE_NODE)
596 #define IS_SUM_DATA_SEG(footer)		(footer.entry_type == SUM_TYPE_DATA)
597 
dir_buckets(unsigned int level,int dir_level)598 static inline unsigned int dir_buckets(unsigned int level, int dir_level)
599 {
600 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
601 		return 1 << (level + dir_level);
602 	else
603 		return MAX_DIR_BUCKETS;
604 }
605 
bucket_blocks(unsigned int level)606 static inline unsigned int bucket_blocks(unsigned int level)
607 {
608 	if (level < MAX_DIR_HASH_DEPTH / 2)
609 		return 2;
610 	else
611 		return 4;
612 }
613 
dir_block_index(unsigned int level,int dir_level,unsigned int idx)614 static inline unsigned long dir_block_index(unsigned int level,
615 				int dir_level, unsigned int idx)
616 {
617 	unsigned long i;
618 	unsigned long bidx = 0;
619 
620 	for (i = 0; i < level; i++)
621 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
622 	bidx += idx * bucket_blocks(level);
623 	return bidx;
624 }
625 
is_dot_dotdot(const unsigned char * name,const int len)626 static inline int is_dot_dotdot(const unsigned char *name, const int len)
627 {
628 	if (len == 1 && name[0] == '.')
629 		return 1;
630 	if (len == 2 && name[0] == '.' && name[1] == '.')
631 		return 1;
632 	return 0;
633 }
634 
get_encoding(struct f2fs_sb_info * sbi)635 static inline int get_encoding(struct f2fs_sb_info *sbi)
636 {
637 	return le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding);
638 }
639 
640 #endif /* _F2FS_H_ */
641