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 #ifdef HAVE_MNTENT_H
24 #include <mntent.h>
25 #endif
26 #ifdef HAVE_MACH_TIME_H
27 #include <mach/mach_time.h>
28 #endif
29 #include <sys/stat.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_SYS_MOUNT_H
34 #include <sys/mount.h>
35 #endif
36 #include <assert.h>
37
38 #include "f2fs_fs.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 block_t end = SM_I(sbi)->main_blkaddr;
420 return end + le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
421 }
422
423 #define GET_ZONENO_FROM_SEGNO(sbi, segno) \
424 ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
425
426 #define IS_DATASEG(t) \
427 ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || \
428 (t == CURSEG_WARM_DATA))
429
430 #define IS_NODESEG(t) \
431 ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || \
432 (t == CURSEG_WARM_NODE))
433
434 #define MAIN_BLKADDR(sbi) \
435 (SM_I(sbi) ? SM_I(sbi)->main_blkaddr : \
436 le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
437 #define SEG0_BLKADDR(sbi) \
438 (SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : \
439 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
440
441 #define GET_SUM_BLKADDR(sbi, segno) \
442 ((sbi->sm_info->ssa_blkaddr) + segno)
443
444 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) \
445 ((blk_addr) - SM_I(sbi)->seg0_blkaddr)
446
447 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
448 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
449
450 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
451 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
452
453 #define GET_SEC_FROM_SEG(sbi, segno) \
454 ((segno) / (sbi)->segs_per_sec)
455 #define GET_SEG_FROM_SEC(sbi, secno) \
456 ((secno) * (sbi)->segs_per_sec)
457
458 #define FREE_I_START_SEGNO(sbi) \
459 GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
460 #define GET_R2L_SEGNO(sbi, segno) (segno + FREE_I_START_SEGNO(sbi))
461
462 #define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
463 #define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
464 #define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
465
466 #define START_BLOCK(sbi, segno) (SM_I(sbi)->main_blkaddr + \
467 ((segno) << sbi->log_blocks_per_seg))
468
469 #define NEXT_FREE_BLKADDR(sbi, curseg) \
470 (START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
471
472 #define SIT_BLK_CNT(sbi) \
473 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
474
CURSEG_I(struct f2fs_sb_info * sbi,int type)475 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
476 {
477 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
478 }
479
start_sum_block(struct f2fs_sb_info * sbi)480 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
481 {
482 return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
483 }
484
sum_blk_addr(struct f2fs_sb_info * sbi,int base,int type)485 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
486 {
487 return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
488 - (base + 1) + type;
489 }
490
491 /* for the list of fsync inodes, used only during recovery */
492 struct fsync_inode_entry {
493 struct list_head list; /* list head */
494 nid_t ino; /* inode number */
495 block_t blkaddr; /* block address locating the last fsync */
496 block_t last_dentry; /* block address locating the last dentry */
497 };
498
499 #define nats_in_cursum(jnl) (le16_to_cpu(jnl->n_nats))
500 #define sits_in_cursum(jnl) (le16_to_cpu(jnl->n_sits))
501
502 #define nat_in_journal(jnl, i) (jnl->nat_j.entries[i].ne)
503 #define nid_in_journal(jnl, i) (jnl->nat_j.entries[i].nid)
504 #define sit_in_journal(jnl, i) (jnl->sit_j.entries[i].se)
505 #define segno_in_journal(jnl, i) (jnl->sit_j.entries[i].segno)
506
507 #define SIT_ENTRY_OFFSET(sit_i, segno) \
508 ((segno) % sit_i->sents_per_block)
509 #define SIT_BLOCK_OFFSET(sit_i, segno) \
510 ((segno) / SIT_ENTRY_PER_BLOCK)
511 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
512
IS_VALID_NID(struct f2fs_sb_info * sbi,u32 nid)513 static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
514 {
515 return (nid < (NAT_ENTRY_PER_BLOCK *
516 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)
517 << (sbi->log_blocks_per_seg - 1)));
518 }
519
IS_VALID_BLK_ADDR(struct f2fs_sb_info * sbi,u32 addr)520 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
521 {
522 if (addr == NULL_ADDR || addr == NEW_ADDR)
523 return 1;
524
525 if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
526 addr < SM_I(sbi)->main_blkaddr) {
527 DBG(1, "block addr [0x%x]\n", addr);
528 return 0;
529 }
530 /* next block offset will be checked at the end of fsck. */
531 return 1;
532 }
533
is_valid_data_blkaddr(block_t blkaddr)534 static inline bool is_valid_data_blkaddr(block_t blkaddr)
535 {
536 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
537 blkaddr == COMPRESS_ADDR)
538 return 0;
539 return 1;
540 }
541
IS_CUR_SEGNO(struct f2fs_sb_info * sbi,u32 segno)542 static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno)
543 {
544 int i;
545
546 for (i = 0; i < NO_CHECK_TYPE; i++) {
547 struct curseg_info *curseg = CURSEG_I(sbi, i);
548
549 if (segno == curseg->segno)
550 return 1;
551 }
552 return 0;
553 }
554
BLKOFF_FROM_MAIN(struct f2fs_sb_info * sbi,u64 blk_addr)555 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
556 {
557 ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
558 return blk_addr - SM_I(sbi)->main_blkaddr;
559 }
560
GET_SEGNO(struct f2fs_sb_info * sbi,u64 blk_addr)561 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
562 {
563 return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
564 >> sbi->log_blocks_per_seg);
565 }
566
OFFSET_IN_SEG(struct f2fs_sb_info * sbi,u64 blk_addr)567 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
568 {
569 return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
570 % (1 << sbi->log_blocks_per_seg));
571 }
572
node_info_from_raw_nat(struct node_info * ni,struct f2fs_nat_entry * raw_nat)573 static inline void node_info_from_raw_nat(struct node_info *ni,
574 struct f2fs_nat_entry *raw_nat)
575 {
576 ni->ino = le32_to_cpu(raw_nat->ino);
577 ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
578 ni->version = raw_nat->version;
579 }
580
set_summary(struct f2fs_summary * sum,nid_t nid,unsigned int ofs_in_node,unsigned char version)581 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
582 unsigned int ofs_in_node, unsigned char version)
583 {
584 sum->nid = cpu_to_le32(nid);
585 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
586 sum->version = version;
587 }
588
589 #define S_SHIFT 12
590 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
591 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
592 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
593 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
594 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
595 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
596 #ifdef S_IFSOCK
597 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
598 #endif
599 #ifdef S_IFLNK
600 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
601 #endif
602 };
603
map_de_type(umode_t mode)604 static inline int map_de_type(umode_t mode)
605 {
606 return f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
607 }
608
inline_xattr_addr(struct f2fs_inode * inode)609 static inline void *inline_xattr_addr(struct f2fs_inode *inode)
610 {
611 return (void *)&(inode->i_addr[DEF_ADDRS_PER_INODE -
612 get_inline_xattr_addrs(inode)]);
613 }
614
inline_xattr_size(struct f2fs_inode * inode)615 static inline int inline_xattr_size(struct f2fs_inode *inode)
616 {
617 return get_inline_xattr_addrs(inode) * sizeof(__le32);
618 }
619
620 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
621 #define IS_SUM_NODE_SEG(footer) (footer.entry_type == SUM_TYPE_NODE)
622 #define IS_SUM_DATA_SEG(footer) (footer.entry_type == SUM_TYPE_DATA)
623
dir_buckets(unsigned int level,int dir_level)624 static inline unsigned int dir_buckets(unsigned int level, int dir_level)
625 {
626 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
627 return 1 << (level + dir_level);
628 else
629 return MAX_DIR_BUCKETS;
630 }
631
bucket_blocks(unsigned int level)632 static inline unsigned int bucket_blocks(unsigned int level)
633 {
634 if (level < MAX_DIR_HASH_DEPTH / 2)
635 return 2;
636 else
637 return 4;
638 }
639
dir_block_index(unsigned int level,int dir_level,unsigned int idx)640 static inline unsigned long dir_block_index(unsigned int level,
641 int dir_level, unsigned int idx)
642 {
643 unsigned long i;
644 unsigned long bidx = 0;
645
646 for (i = 0; i < level; i++)
647 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
648 bidx += idx * bucket_blocks(level);
649 return bidx;
650 }
651
is_dot_dotdot(const unsigned char * name,const int len)652 static inline int is_dot_dotdot(const unsigned char *name, const int len)
653 {
654 if (len == 1 && name[0] == '.')
655 return 1;
656 if (len == 2 && name[0] == '.' && name[1] == '.')
657 return 1;
658 return 0;
659 }
660
get_encoding(struct f2fs_sb_info * sbi)661 static inline int get_encoding(struct f2fs_sb_info *sbi)
662 {
663 return le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding);
664 }
665
666 #endif /* _F2FS_H_ */
667