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