f2fs-tools: introduce get_checkpoint_version() for cleanup
[platform/upstream/f2fs-tools.git] / fsck / mount.c
1 /**
2  * mount.c
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 #include "fsck.h"
12 #include "xattr.h"
13 #include <locale.h>
14 #ifdef HAVE_LINUX_POSIX_ACL_H
15 #include <linux/posix_acl.h>
16 #endif
17 #ifdef HAVE_SYS_ACL_H
18 #include <sys/acl.h>
19 #endif
20
21 #ifndef ACL_UNDEFINED_TAG
22 #define ACL_UNDEFINED_TAG       (0x00)
23 #define ACL_USER_OBJ            (0x01)
24 #define ACL_USER                (0x02)
25 #define ACL_GROUP_OBJ           (0x04)
26 #define ACL_GROUP               (0x08)
27 #define ACL_MASK                (0x10)
28 #define ACL_OTHER               (0x20)
29 #endif
30
31 u32 get_free_segments(struct f2fs_sb_info *sbi)
32 {
33         u32 i, free_segs = 0;
34
35         for (i = 0; i < TOTAL_SEGS(sbi); i++) {
36                 struct seg_entry *se = get_seg_entry(sbi, i);
37
38                 if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, i))
39                         free_segs++;
40         }
41         return free_segs;
42 }
43
44 void update_free_segments(struct f2fs_sb_info *sbi)
45 {
46         char *progress = "-*|*-";
47         static int i = 0;
48
49         if (c.dbg_lv)
50                 return;
51
52         MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], get_free_segments(sbi));
53         fflush(stdout);
54         i++;
55 }
56
57 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
58 static void print_acl(const u8 *value, int size)
59 {
60         const struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
61         const struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
62         const u8 *end = value + size;
63         int i, count;
64
65         if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) {
66                 MSG(0, "Invalid ACL version [0x%x : 0x%x]\n",
67                                 le32_to_cpu(hdr->a_version), F2FS_ACL_VERSION);
68                 return;
69         }
70
71         count = f2fs_acl_count(size);
72         if (count <= 0) {
73                 MSG(0, "Invalid ACL value size %d\n", size);
74                 return;
75         }
76
77         for (i = 0; i < count; i++) {
78                 if ((u8 *)entry > end) {
79                         MSG(0, "Invalid ACL entries count %d\n", count);
80                         return;
81                 }
82
83                 switch (le16_to_cpu(entry->e_tag)) {
84                 case ACL_USER_OBJ:
85                 case ACL_GROUP_OBJ:
86                 case ACL_MASK:
87                 case ACL_OTHER:
88                         MSG(0, "tag:0x%x perm:0x%x\n",
89                                         le16_to_cpu(entry->e_tag),
90                                         le16_to_cpu(entry->e_perm));
91                         entry = (struct f2fs_acl_entry *)((char *)entry +
92                                         sizeof(struct f2fs_acl_entry_short));
93                         break;
94                 case ACL_USER:
95                         MSG(0, "tag:0x%x perm:0x%x uid:%u\n",
96                                         le16_to_cpu(entry->e_tag),
97                                         le16_to_cpu(entry->e_perm),
98                                         le32_to_cpu(entry->e_id));
99                         entry = (struct f2fs_acl_entry *)((char *)entry +
100                                         sizeof(struct f2fs_acl_entry));
101                         break;
102                 case ACL_GROUP:
103                         MSG(0, "tag:0x%x perm:0x%x gid:%u\n",
104                                         le16_to_cpu(entry->e_tag),
105                                         le16_to_cpu(entry->e_perm),
106                                         le32_to_cpu(entry->e_id));
107                         entry = (struct f2fs_acl_entry *)((char *)entry +
108                                         sizeof(struct f2fs_acl_entry));
109                         break;
110                 default:
111                         MSG(0, "Unknown ACL tag 0x%x\n",
112                                         le16_to_cpu(entry->e_tag));
113                         return;
114                 }
115         }
116 }
117 #endif /* HAVE_LINUX_POSIX_ACL_H || HAVE_SYS_ACL_H */
118
119 static void print_xattr_entry(const struct f2fs_xattr_entry *ent)
120 {
121         const u8 *value = (const u8 *)&ent->e_name[ent->e_name_len];
122         const int size = le16_to_cpu(ent->e_value_size);
123         const struct fscrypt_context *ctx;
124         int i;
125
126         MSG(0, "\nxattr: e_name_index:%d e_name:", ent->e_name_index);
127         for (i = 0; i < ent->e_name_len; i++)
128                 MSG(0, "%c", ent->e_name[i]);
129         MSG(0, " e_name_len:%d e_value_size:%d e_value:\n",
130                         ent->e_name_len, size);
131
132         switch (ent->e_name_index) {
133 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
134         case F2FS_XATTR_INDEX_POSIX_ACL_ACCESS:
135         case F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT:
136                 print_acl(value, size);
137                 return;
138 #endif
139         case F2FS_XATTR_INDEX_ENCRYPTION:
140                 ctx = (const struct fscrypt_context *)value;
141                 if (size != sizeof(*ctx) ||
142                     ctx->format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
143                         break;
144                 MSG(0, "format: %d\n", ctx->format);
145                 MSG(0, "contents_encryption_mode: 0x%x\n", ctx->contents_encryption_mode);
146                 MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->filenames_encryption_mode);
147                 MSG(0, "flags: 0x%x\n", ctx->flags);
148                 MSG(0, "master_key_descriptor: ");
149                 for (i = 0; i < FS_KEY_DESCRIPTOR_SIZE; i++)
150                         MSG(0, "%02X", ctx->master_key_descriptor[i]);
151                 MSG(0, "\nnonce: ");
152                 for (i = 0; i < FS_KEY_DERIVATION_NONCE_SIZE; i++)
153                         MSG(0, "%02X", ctx->nonce[i]);
154                 MSG(0, "\n");
155                 return;
156         }
157         for (i = 0; i < size; i++)
158                 MSG(0, "%02X", value[i]);
159         MSG(0, "\n");
160 }
161
162 void print_inode_info(struct f2fs_sb_info *sbi,
163                         struct f2fs_node *node, int name)
164 {
165         struct f2fs_inode *inode = &node->i;
166         void *xattr_addr;
167         struct f2fs_xattr_entry *ent;
168         char en[F2FS_PRINT_NAMELEN];
169         unsigned int i = 0;
170         u32 namelen = le32_to_cpu(inode->i_namelen);
171         int enc_name = file_enc_name(inode);
172         int ofs = __get_extra_isize(inode);
173
174         pretty_print_filename(inode->i_name, namelen, en, enc_name);
175         if (name && en[0]) {
176                 MSG(0, " - File name         : %s%s\n", en,
177                                 enc_name ? " <encrypted>" : "");
178                 setlocale(LC_ALL, "");
179                 MSG(0, " - File size         : %'llu (bytes)\n",
180                                 le64_to_cpu(inode->i_size));
181                 return;
182         }
183
184         DISP_u32(inode, i_mode);
185         DISP_u32(inode, i_advise);
186         DISP_u32(inode, i_uid);
187         DISP_u32(inode, i_gid);
188         DISP_u32(inode, i_links);
189         DISP_u64(inode, i_size);
190         DISP_u64(inode, i_blocks);
191
192         DISP_u64(inode, i_atime);
193         DISP_u32(inode, i_atime_nsec);
194         DISP_u64(inode, i_ctime);
195         DISP_u32(inode, i_ctime_nsec);
196         DISP_u64(inode, i_mtime);
197         DISP_u32(inode, i_mtime_nsec);
198
199         DISP_u32(inode, i_generation);
200         DISP_u32(inode, i_current_depth);
201         DISP_u32(inode, i_xattr_nid);
202         DISP_u32(inode, i_flags);
203         DISP_u32(inode, i_inline);
204         DISP_u32(inode, i_pino);
205         DISP_u32(inode, i_dir_level);
206
207         if (en[0]) {
208                 DISP_u32(inode, i_namelen);
209                 printf("%-30s\t\t[%s]\n", "i_name", en);
210         }
211
212         printf("i_ext: fofs:%x blkaddr:%x len:%x\n",
213                         le32_to_cpu(inode->i_ext.fofs),
214                         le32_to_cpu(inode->i_ext.blk_addr),
215                         le32_to_cpu(inode->i_ext.len));
216
217         if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
218                 DISP_u16(inode, i_extra_isize);
219                 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
220                         DISP_u16(inode, i_inline_xattr_size);
221                 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
222                         DISP_u32(inode, i_projid);
223                 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
224                         DISP_u32(inode, i_inode_checksum);
225                 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
226                         DISP_u64(inode, i_crtime);
227                         DISP_u32(inode, i_crtime_nsec);
228                 }
229         }
230
231         DISP_u32(inode, i_addr[ofs]);           /* Pointers to data blocks */
232         DISP_u32(inode, i_addr[ofs + 1]);       /* Pointers to data blocks */
233         DISP_u32(inode, i_addr[ofs + 2]);       /* Pointers to data blocks */
234         DISP_u32(inode, i_addr[ofs + 3]);       /* Pointers to data blocks */
235
236         for (i = ofs + 3; i < ADDRS_PER_INODE(inode); i++) {
237                 if (inode->i_addr[i] == 0x0)
238                         break;
239                 printf("i_addr[0x%x] points data block\t\t[0x%4x]\n",
240                                 i, le32_to_cpu(inode->i_addr[i]));
241         }
242
243         DISP_u32(inode, i_nid[0]);      /* direct */
244         DISP_u32(inode, i_nid[1]);      /* direct */
245         DISP_u32(inode, i_nid[2]);      /* indirect */
246         DISP_u32(inode, i_nid[3]);      /* indirect */
247         DISP_u32(inode, i_nid[4]);      /* double indirect */
248
249         xattr_addr = read_all_xattrs(sbi, node);
250         list_for_each_xattr(ent, xattr_addr) {
251                 print_xattr_entry(ent);
252         }
253         free(xattr_addr);
254
255         printf("\n");
256 }
257
258 void print_node_info(struct f2fs_sb_info *sbi,
259                         struct f2fs_node *node_block, int verbose)
260 {
261         nid_t ino = le32_to_cpu(node_block->footer.ino);
262         nid_t nid = le32_to_cpu(node_block->footer.nid);
263         /* Is this inode? */
264         if (ino == nid) {
265                 DBG(verbose, "Node ID [0x%x:%u] is inode\n", nid, nid);
266                 print_inode_info(sbi, node_block, verbose);
267         } else {
268                 int i;
269                 u32 *dump_blk = (u32 *)node_block;
270                 DBG(verbose,
271                         "Node ID [0x%x:%u] is direct node or indirect node.\n",
272                                                                 nid, nid);
273                 for (i = 0; i <= 10; i++)
274                         MSG(verbose, "[%d]\t\t\t[0x%8x : %d]\n",
275                                                 i, dump_blk[i], dump_blk[i]);
276         }
277 }
278
279 static void DISP_label(u_int16_t *name)
280 {
281         char buffer[MAX_VOLUME_NAME];
282
283         utf16_to_utf8(buffer, name, MAX_VOLUME_NAME, MAX_VOLUME_NAME);
284         printf("%-30s" "\t\t[%s]\n", "volum_name", buffer);
285 }
286
287 void print_raw_sb_info(struct f2fs_super_block *sb)
288 {
289         if (!c.dbg_lv)
290                 return;
291
292         printf("\n");
293         printf("+--------------------------------------------------------+\n");
294         printf("| Super block                                            |\n");
295         printf("+--------------------------------------------------------+\n");
296
297         DISP_u32(sb, magic);
298         DISP_u32(sb, major_ver);
299
300         DISP_label(sb->volume_name);
301
302         DISP_u32(sb, minor_ver);
303         DISP_u32(sb, log_sectorsize);
304         DISP_u32(sb, log_sectors_per_block);
305
306         DISP_u32(sb, log_blocksize);
307         DISP_u32(sb, log_blocks_per_seg);
308         DISP_u32(sb, segs_per_sec);
309         DISP_u32(sb, secs_per_zone);
310         DISP_u32(sb, checksum_offset);
311         DISP_u64(sb, block_count);
312
313         DISP_u32(sb, section_count);
314         DISP_u32(sb, segment_count);
315         DISP_u32(sb, segment_count_ckpt);
316         DISP_u32(sb, segment_count_sit);
317         DISP_u32(sb, segment_count_nat);
318
319         DISP_u32(sb, segment_count_ssa);
320         DISP_u32(sb, segment_count_main);
321         DISP_u32(sb, segment0_blkaddr);
322
323         DISP_u32(sb, cp_blkaddr);
324         DISP_u32(sb, sit_blkaddr);
325         DISP_u32(sb, nat_blkaddr);
326         DISP_u32(sb, ssa_blkaddr);
327         DISP_u32(sb, main_blkaddr);
328
329         DISP_u32(sb, root_ino);
330         DISP_u32(sb, node_ino);
331         DISP_u32(sb, meta_ino);
332         DISP_u32(sb, cp_payload);
333         DISP_u32(sb, crc);
334         DISP("%-.256s", sb, version);
335         printf("\n");
336 }
337
338 void print_ckpt_info(struct f2fs_sb_info *sbi)
339 {
340         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
341
342         if (!c.dbg_lv)
343                 return;
344
345         printf("\n");
346         printf("+--------------------------------------------------------+\n");
347         printf("| Checkpoint                                             |\n");
348         printf("+--------------------------------------------------------+\n");
349
350         DISP_u64(cp, checkpoint_ver);
351         DISP_u64(cp, user_block_count);
352         DISP_u64(cp, valid_block_count);
353         DISP_u32(cp, rsvd_segment_count);
354         DISP_u32(cp, overprov_segment_count);
355         DISP_u32(cp, free_segment_count);
356
357         DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]);
358         DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]);
359         DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]);
360         DISP_u32(cp, cur_node_segno[0]);
361         DISP_u32(cp, cur_node_segno[1]);
362         DISP_u32(cp, cur_node_segno[2]);
363
364         DISP_u32(cp, cur_node_blkoff[0]);
365         DISP_u32(cp, cur_node_blkoff[1]);
366         DISP_u32(cp, cur_node_blkoff[2]);
367
368
369         DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]);
370         DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]);
371         DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]);
372         DISP_u32(cp, cur_data_segno[0]);
373         DISP_u32(cp, cur_data_segno[1]);
374         DISP_u32(cp, cur_data_segno[2]);
375
376         DISP_u32(cp, cur_data_blkoff[0]);
377         DISP_u32(cp, cur_data_blkoff[1]);
378         DISP_u32(cp, cur_data_blkoff[2]);
379
380         DISP_u32(cp, ckpt_flags);
381         DISP_u32(cp, cp_pack_total_block_count);
382         DISP_u32(cp, cp_pack_start_sum);
383         DISP_u32(cp, valid_node_count);
384         DISP_u32(cp, valid_inode_count);
385         DISP_u32(cp, next_free_nid);
386         DISP_u32(cp, sit_ver_bitmap_bytesize);
387         DISP_u32(cp, nat_ver_bitmap_bytesize);
388         DISP_u32(cp, checksum_offset);
389         DISP_u64(cp, elapsed_time);
390
391         DISP_u32(cp, sit_nat_version_bitmap[0]);
392         printf("\n\n");
393 }
394
395 void print_cp_state(u32 flag)
396 {
397         MSG(0, "Info: checkpoint state = %x : ", flag);
398         if (flag & CP_QUOTA_NEED_FSCK_FLAG)
399                 MSG(0, "%s", " quota_need_fsck");
400         if (flag & CP_LARGE_NAT_BITMAP_FLAG)
401                 MSG(0, "%s", " large_nat_bitmap");
402         if (flag & CP_NOCRC_RECOVERY_FLAG)
403                 MSG(0, "%s", " allow_nocrc");
404         if (flag & CP_TRIMMED_FLAG)
405                 MSG(0, "%s", " trimmed");
406         if (flag & CP_NAT_BITS_FLAG)
407                 MSG(0, "%s", " nat_bits");
408         if (flag & CP_CRC_RECOVERY_FLAG)
409                 MSG(0, "%s", " crc");
410         if (flag & CP_FASTBOOT_FLAG)
411                 MSG(0, "%s", " fastboot");
412         if (flag & CP_FSCK_FLAG)
413                 MSG(0, "%s", " fsck");
414         if (flag & CP_ERROR_FLAG)
415                 MSG(0, "%s", " error");
416         if (flag & CP_COMPACT_SUM_FLAG)
417                 MSG(0, "%s", " compacted_summary");
418         if (flag & CP_ORPHAN_PRESENT_FLAG)
419                 MSG(0, "%s", " orphan_inodes");
420         if (flag & CP_DISABLED_FLAG)
421                 MSG(0, "%s", " disabled");
422         if (flag & CP_UMOUNT_FLAG)
423                 MSG(0, "%s", " unmount");
424         else
425                 MSG(0, "%s", " sudden-power-off");
426         MSG(0, "\n");
427 }
428
429 void print_sb_state(struct f2fs_super_block *sb)
430 {
431         __le32 f = sb->feature;
432         int i;
433
434         MSG(0, "Info: superblock features = %x : ", f);
435         if (f & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) {
436                 MSG(0, "%s", " encrypt");
437         }
438         if (f & cpu_to_le32(F2FS_FEATURE_VERITY)) {
439                 MSG(0, "%s", " verity");
440         }
441         if (f & cpu_to_le32(F2FS_FEATURE_BLKZONED)) {
442                 MSG(0, "%s", " blkzoned");
443         }
444         if (f & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
445                 MSG(0, "%s", " extra_attr");
446         }
447         if (f & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
448                 MSG(0, "%s", " project_quota");
449         }
450         if (f & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) {
451                 MSG(0, "%s", " inode_checksum");
452         }
453         if (f & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
454                 MSG(0, "%s", " flexible_inline_xattr");
455         }
456         if (f & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
457                 MSG(0, "%s", " quota_ino");
458         }
459         if (f & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
460                 MSG(0, "%s", " inode_crtime");
461         }
462         if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
463                 MSG(0, "%s", " lost_found");
464         }
465         if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
466                 MSG(0, "%s", " sb_checksum");
467         }
468         MSG(0, "\n");
469         MSG(0, "Info: superblock encrypt level = %d, salt = ",
470                                         sb->encryption_level);
471         for (i = 0; i < 16; i++)
472                 MSG(0, "%02x", sb->encrypt_pw_salt[i]);
473         MSG(0, "\n");
474 }
475
476 void update_superblock(struct f2fs_super_block *sb, int sb_mask)
477 {
478         int addr, ret;
479         u_int8_t *buf;
480         u32 old_crc, new_crc;
481
482         buf = calloc(BLOCK_SZ, 1);
483         ASSERT(buf);
484
485         if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
486                 old_crc = get_sb(crc);
487                 new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
488                                                 SB_CHKSUM_OFFSET);
489                 set_sb(crc, new_crc);
490                 MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
491                                                         old_crc, new_crc);
492         }
493
494         memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
495         for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) {
496                 if (SB_MASK(addr) & sb_mask) {
497                         ret = dev_write_block(buf, addr);
498                         ASSERT(ret >= 0);
499                 }
500         }
501
502         free(buf);
503         DBG(0, "Info: Done to update superblock\n");
504 }
505
506 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
507                                                         enum SB_ADDR sb_addr)
508 {
509         u32 segment0_blkaddr = get_sb(segment0_blkaddr);
510         u32 cp_blkaddr = get_sb(cp_blkaddr);
511         u32 sit_blkaddr = get_sb(sit_blkaddr);
512         u32 nat_blkaddr = get_sb(nat_blkaddr);
513         u32 ssa_blkaddr = get_sb(ssa_blkaddr);
514         u32 main_blkaddr = get_sb(main_blkaddr);
515         u32 segment_count_ckpt = get_sb(segment_count_ckpt);
516         u32 segment_count_sit = get_sb(segment_count_sit);
517         u32 segment_count_nat = get_sb(segment_count_nat);
518         u32 segment_count_ssa = get_sb(segment_count_ssa);
519         u32 segment_count_main = get_sb(segment_count_main);
520         u32 segment_count = get_sb(segment_count);
521         u32 log_blocks_per_seg = get_sb(log_blocks_per_seg);
522         u64 main_end_blkaddr = main_blkaddr +
523                                 (segment_count_main << log_blocks_per_seg);
524         u64 seg_end_blkaddr = segment0_blkaddr +
525                                 (segment_count << log_blocks_per_seg);
526
527         if (segment0_blkaddr != cp_blkaddr) {
528                 MSG(0, "\tMismatch segment0(%u) cp_blkaddr(%u)\n",
529                                 segment0_blkaddr, cp_blkaddr);
530                 return -1;
531         }
532
533         if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
534                                                         sit_blkaddr) {
535                 MSG(0, "\tWrong CP boundary, start(%u) end(%u) blocks(%u)\n",
536                         cp_blkaddr, sit_blkaddr,
537                         segment_count_ckpt << log_blocks_per_seg);
538                 return -1;
539         }
540
541         if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
542                                                         nat_blkaddr) {
543                 MSG(0, "\tWrong SIT boundary, start(%u) end(%u) blocks(%u)\n",
544                         sit_blkaddr, nat_blkaddr,
545                         segment_count_sit << log_blocks_per_seg);
546                 return -1;
547         }
548
549         if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
550                                                         ssa_blkaddr) {
551                 MSG(0, "\tWrong NAT boundary, start(%u) end(%u) blocks(%u)\n",
552                         nat_blkaddr, ssa_blkaddr,
553                         segment_count_nat << log_blocks_per_seg);
554                 return -1;
555         }
556
557         if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
558                                                         main_blkaddr) {
559                 MSG(0, "\tWrong SSA boundary, start(%u) end(%u) blocks(%u)\n",
560                         ssa_blkaddr, main_blkaddr,
561                         segment_count_ssa << log_blocks_per_seg);
562                 return -1;
563         }
564
565         if (main_end_blkaddr > seg_end_blkaddr) {
566                 MSG(0, "\tWrong MAIN_AREA, start(%u) end(%u) block(%u)\n",
567                         main_blkaddr,
568                         segment0_blkaddr +
569                                 (segment_count << log_blocks_per_seg),
570                         segment_count_main << log_blocks_per_seg);
571                 return -1;
572         } else if (main_end_blkaddr < seg_end_blkaddr) {
573                 set_sb(segment_count, (main_end_blkaddr -
574                                 segment0_blkaddr) >> log_blocks_per_seg);
575
576                 update_superblock(sb, SB_MASK(sb_addr));
577                 MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
578                         main_blkaddr,
579                         segment0_blkaddr +
580                                 (segment_count << log_blocks_per_seg),
581                         segment_count_main << log_blocks_per_seg);
582         }
583         return 0;
584 }
585
586 static int verify_sb_chksum(struct f2fs_super_block *sb)
587 {
588         if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
589                 MSG(0, "\tInvalid SB CRC offset: %u\n",
590                                         get_sb(checksum_offset));
591                 return -1;
592         }
593         if (f2fs_crc_valid(get_sb(crc), sb,
594                         get_sb(checksum_offset))) {
595                 MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
596                 return -1;
597         }
598         return 0;
599 }
600
601 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
602 {
603         unsigned int blocksize;
604
605         if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
606                                         verify_sb_chksum(sb))
607                 return -1;
608
609         if (F2FS_SUPER_MAGIC != get_sb(magic))
610                 return -1;
611
612         if (F2FS_BLKSIZE != PAGE_CACHE_SIZE)
613                 return -1;
614
615         blocksize = 1 << get_sb(log_blocksize);
616         if (F2FS_BLKSIZE != blocksize)
617                 return -1;
618
619         /* check log blocks per segment */
620         if (get_sb(log_blocks_per_seg) != 9)
621                 return -1;
622
623         /* Currently, support 512/1024/2048/4096 bytes sector size */
624         if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE ||
625                         get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE)
626                 return -1;
627
628         if (get_sb(log_sectors_per_block) + get_sb(log_sectorsize) !=
629                                                 F2FS_MAX_LOG_SECTOR_SIZE)
630                 return -1;
631
632         /* check reserved ino info */
633         if (get_sb(node_ino) != 1 || get_sb(meta_ino) != 2 ||
634                                         get_sb(root_ino) != 3)
635                 return -1;
636
637         /* Check zoned block device feature */
638         if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
639                         !(sb->feature & cpu_to_le32(F2FS_FEATURE_BLKZONED))) {
640                 MSG(0, "\tMissing zoned block device feature\n");
641                 return -1;
642         }
643
644         if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
645                 return -1;
646
647         if (sanity_check_area_boundary(sb, sb_addr))
648                 return -1;
649         return 0;
650 }
651
652 int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
653 {
654         char buf[F2FS_BLKSIZE];
655
656         sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
657         if (!sbi->raw_super)
658                 return -ENOMEM;
659
660         if (dev_read_block(buf, sb_addr))
661                 return -1;
662
663         memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
664                                         sizeof(struct f2fs_super_block));
665
666         if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
667                 /* get kernel version */
668                 if (c.kd >= 0) {
669                         dev_read_version(c.version, 0, VERSION_LEN);
670                         get_kernel_version(c.version);
671                 } else {
672                         get_kernel_uname_version(c.version);
673                 }
674
675                 /* build sb version */
676                 memcpy(c.sb_version, sbi->raw_super->version, VERSION_LEN);
677                 get_kernel_version(c.sb_version);
678                 memcpy(c.init_version, sbi->raw_super->init_version, VERSION_LEN);
679                 get_kernel_version(c.init_version);
680
681                 MSG(0, "Info: MKFS version\n  \"%s\"\n", c.init_version);
682                 MSG(0, "Info: FSCK version\n  from \"%s\"\n    to \"%s\"\n",
683                                         c.sb_version, c.version);
684                 if (memcmp(c.sb_version, c.version, VERSION_LEN)) {
685                         memcpy(sbi->raw_super->version,
686                                                 c.version, VERSION_LEN);
687                         update_superblock(sbi->raw_super, SB_MASK(sb_addr));
688
689                         c.auto_fix = 0;
690                         c.fix_on = 1;
691                 }
692                 print_sb_state(sbi->raw_super);
693                 return 0;
694         }
695
696         free(sbi->raw_super);
697         sbi->raw_super = NULL;
698         MSG(0, "\tCan't find a valid F2FS superblock at 0x%x\n", sb_addr);
699
700         return -EINVAL;
701 }
702
703 int init_sb_info(struct f2fs_sb_info *sbi)
704 {
705         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
706         u64 total_sectors;
707         int i;
708
709         sbi->log_sectors_per_block = get_sb(log_sectors_per_block);
710         sbi->log_blocksize = get_sb(log_blocksize);
711         sbi->blocksize = 1 << sbi->log_blocksize;
712         sbi->log_blocks_per_seg = get_sb(log_blocks_per_seg);
713         sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
714         sbi->segs_per_sec = get_sb(segs_per_sec);
715         sbi->secs_per_zone = get_sb(secs_per_zone);
716         sbi->total_sections = get_sb(section_count);
717         sbi->total_node_count = (get_sb(segment_count_nat) / 2) *
718                                 sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
719         sbi->root_ino_num = get_sb(root_ino);
720         sbi->node_ino_num = get_sb(node_ino);
721         sbi->meta_ino_num = get_sb(meta_ino);
722         sbi->cur_victim_sec = NULL_SEGNO;
723
724         for (i = 0; i < MAX_DEVICES; i++) {
725                 if (!sb->devs[i].path[0])
726                         break;
727
728                 if (i) {
729                         c.devices[i].path = strdup((char *)sb->devs[i].path);
730                         if (get_device_info(i))
731                                 ASSERT(0);
732                 } else {
733                         ASSERT(!strcmp((char *)sb->devs[i].path,
734                                                 (char *)c.devices[i].path));
735                 }
736
737                 c.devices[i].total_segments =
738                         le32_to_cpu(sb->devs[i].total_segments);
739                 if (i)
740                         c.devices[i].start_blkaddr =
741                                 c.devices[i - 1].end_blkaddr + 1;
742                 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
743                         c.devices[i].total_segments *
744                         c.blks_per_seg - 1;
745                 if (i == 0)
746                         c.devices[i].end_blkaddr += get_sb(segment0_blkaddr);
747
748                 c.ndevs = i + 1;
749                 MSG(0, "Info: Device[%d] : %s blkaddr = %"PRIx64"--%"PRIx64"\n",
750                                 i, c.devices[i].path,
751                                 c.devices[i].start_blkaddr,
752                                 c.devices[i].end_blkaddr);
753         }
754
755         total_sectors = get_sb(block_count) << sbi->log_sectors_per_block;
756         MSG(0, "Info: total FS sectors = %"PRIu64" (%"PRIu64" MB)\n",
757                                 total_sectors, total_sectors >>
758                                                 (20 - get_sb(log_sectorsize)));
759         return 0;
760 }
761
762 static int verify_checksum_chksum(struct f2fs_checkpoint *cp)
763 {
764         unsigned int chksum_offset = get_cp(checksum_offset);
765         unsigned int crc, cal_crc;
766
767         if (chksum_offset < CP_MIN_CHKSUM_OFFSET ||
768                         chksum_offset > CP_CHKSUM_OFFSET) {
769                 MSG(0, "\tInvalid CP CRC offset: %u\n", chksum_offset);
770                 return -1;
771         }
772
773         crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp + chksum_offset));
774         cal_crc = f2fs_checkpoint_chksum(cp);
775         if (cal_crc != crc) {
776                 MSG(0, "\tInvalid CP CRC: offset:%u, crc:0x%x, calc:0x%x\n",
777                         chksum_offset, crc, cal_crc);
778                 return -1;
779         }
780         return 0;
781 }
782
783 static void *get_checkpoint_version(block_t cp_addr)
784 {
785         void *cp_page;
786
787         cp_page = malloc(PAGE_SIZE);
788         ASSERT(cp_page);
789
790         if (dev_read_block(cp_page, cp_addr) < 0)
791                 ASSERT(0);
792
793         if (verify_checksum_chksum((struct f2fs_checkpoint *)cp_page))
794                 goto out;
795         return cp_page;
796 out:
797         free(cp_page);
798         return NULL;
799 }
800
801 void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr,
802                                 unsigned long long *version)
803 {
804         void *cp_page_1, *cp_page_2;
805         struct f2fs_checkpoint *cp;
806         unsigned long long cur_version = 0, pre_version = 0;
807
808         /* Read the 1st cp block in this CP pack */
809         cp_page_1 = get_checkpoint_version(cp_addr);
810         if (!cp_page_1)
811                 return NULL;
812
813         cp = (struct f2fs_checkpoint *)cp_page_1;
814         if (get_cp(cp_pack_total_block_count) > sbi->blocks_per_seg)
815                 goto invalid_cp1;
816
817         pre_version = get_cp(checkpoint_ver);
818
819         /* Read the 2nd cp block in this CP pack */
820         cp_addr += get_cp(cp_pack_total_block_count) - 1;
821         cp_page_2 = get_checkpoint_version(cp_addr);
822         if (!cp_page_2)
823                 goto invalid_cp1;
824
825         cp = (struct f2fs_checkpoint *)cp_page_2;
826         cur_version = get_cp(checkpoint_ver);
827
828         if (cur_version == pre_version) {
829                 *version = cur_version;
830                 free(cp_page_2);
831                 return cp_page_1;
832         }
833
834         free(cp_page_2);
835 invalid_cp1:
836         free(cp_page_1);
837         return NULL;
838 }
839
840 int get_valid_checkpoint(struct f2fs_sb_info *sbi)
841 {
842         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
843         void *cp1, *cp2, *cur_page;
844         unsigned long blk_size = sbi->blocksize;
845         unsigned long long cp1_version = 0, cp2_version = 0, version;
846         unsigned long long cp_start_blk_no;
847         unsigned int cp_payload, cp_blks;
848         int ret;
849
850         cp_payload = get_sb(cp_payload);
851         if (cp_payload > F2FS_BLK_ALIGN(MAX_SIT_BITMAP_SIZE))
852                 return -EINVAL;
853
854         cp_blks = 1 + cp_payload;
855         sbi->ckpt = malloc(cp_blks * blk_size);
856         if (!sbi->ckpt)
857                 return -ENOMEM;
858         /*
859          * Finding out valid cp block involves read both
860          * sets( cp pack1 and cp pack 2)
861          */
862         cp_start_blk_no = get_sb(cp_blkaddr);
863         cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
864
865         /* The second checkpoint pack should start at the next segment */
866         cp_start_blk_no += 1 << get_sb(log_blocks_per_seg);
867         cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
868
869         if (cp1 && cp2) {
870                 if (ver_after(cp2_version, cp1_version)) {
871                         cur_page = cp2;
872                         sbi->cur_cp = 2;
873                         version = cp2_version;
874                 } else {
875                         cur_page = cp1;
876                         sbi->cur_cp = 1;
877                         version = cp1_version;
878                 }
879         } else if (cp1) {
880                 cur_page = cp1;
881                 sbi->cur_cp = 1;
882                 version = cp1_version;
883         } else if (cp2) {
884                 cur_page = cp2;
885                 sbi->cur_cp = 2;
886                 version = cp2_version;
887         } else
888                 goto fail_no_cp;
889
890         MSG(0, "Info: CKPT version = %llx\n", version);
891
892         memcpy(sbi->ckpt, cur_page, blk_size);
893
894         if (cp_blks > 1) {
895                 unsigned int i;
896                 unsigned long long cp_blk_no;
897
898                 cp_blk_no = get_sb(cp_blkaddr);
899                 if (cur_page == cp2)
900                         cp_blk_no += 1 << get_sb(log_blocks_per_seg);
901
902                 /* copy sit bitmap */
903                 for (i = 1; i < cp_blks; i++) {
904                         unsigned char *ckpt = (unsigned char *)sbi->ckpt;
905                         ret = dev_read_block(cur_page, cp_blk_no + i);
906                         ASSERT(ret >= 0);
907                         memcpy(ckpt + i * blk_size, cur_page, blk_size);
908                 }
909         }
910         if (cp1)
911                 free(cp1);
912         if (cp2)
913                 free(cp2);
914         return 0;
915
916 fail_no_cp:
917         free(sbi->ckpt);
918         sbi->ckpt = NULL;
919         return -EINVAL;
920 }
921
922 int sanity_check_ckpt(struct f2fs_sb_info *sbi)
923 {
924         unsigned int total, fsmeta;
925         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
926         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
927         unsigned int ovp_segments, reserved_segments;
928         unsigned int main_segs, blocks_per_seg;
929         unsigned int sit_segs, nat_segs;
930         unsigned int sit_bitmap_size, nat_bitmap_size;
931         unsigned int log_blocks_per_seg;
932         unsigned int segment_count_main;
933         unsigned int cp_pack_start_sum, cp_payload;
934         block_t user_block_count;
935         int i;
936
937         total = get_sb(segment_count);
938         fsmeta = get_sb(segment_count_ckpt);
939         sit_segs = get_sb(segment_count_sit);
940         fsmeta += sit_segs;
941         nat_segs = get_sb(segment_count_nat);
942         fsmeta += nat_segs;
943         fsmeta += get_cp(rsvd_segment_count);
944         fsmeta += get_sb(segment_count_ssa);
945
946         if (fsmeta >= total)
947                 return 1;
948
949         ovp_segments = get_cp(overprov_segment_count);
950         reserved_segments = get_cp(rsvd_segment_count);
951
952         if (fsmeta < F2FS_MIN_SEGMENT || ovp_segments == 0 ||
953                                         reserved_segments == 0) {
954                 MSG(0, "\tWrong layout: check mkfs.f2fs version\n");
955                 return 1;
956         }
957
958         user_block_count = get_cp(user_block_count);
959         segment_count_main = get_sb(segment_count_main);
960         log_blocks_per_seg = get_sb(log_blocks_per_seg);
961         if (!user_block_count || user_block_count >=
962                         segment_count_main << log_blocks_per_seg) {
963                 MSG(0, "\tWrong user_block_count(%u)\n", user_block_count);
964                 return 1;
965         }
966
967         main_segs = get_sb(segment_count_main);
968         blocks_per_seg = sbi->blocks_per_seg;
969
970         for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
971                 if (get_cp(cur_node_segno[i]) >= main_segs ||
972                         get_cp(cur_node_blkoff[i]) >= blocks_per_seg)
973                         return 1;
974         }
975         for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
976                 if (get_cp(cur_data_segno[i]) >= main_segs ||
977                         get_cp(cur_data_blkoff[i]) >= blocks_per_seg)
978                         return 1;
979         }
980
981         sit_bitmap_size = get_cp(sit_ver_bitmap_bytesize);
982         nat_bitmap_size = get_cp(nat_ver_bitmap_bytesize);
983
984         if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
985                 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
986                 MSG(0, "\tWrong bitmap size: sit(%u), nat(%u)\n",
987                                 sit_bitmap_size, nat_bitmap_size);
988                 return 1;
989         }
990
991         cp_pack_start_sum = __start_sum_addr(sbi);
992         cp_payload = __cp_payload(sbi);
993         if (cp_pack_start_sum < cp_payload + 1 ||
994                 cp_pack_start_sum > blocks_per_seg - 1 -
995                         NR_CURSEG_TYPE) {
996                 MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
997                         cp_pack_start_sum, cp_payload);
998                 if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
999                         return 1;
1000                 set_sb(cp_payload, cp_pack_start_sum - 1);
1001                 update_superblock(sb, SB_MASK_ALL);
1002         }
1003
1004         return 0;
1005 }
1006
1007 pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start, int *pack)
1008 {
1009         struct f2fs_nm_info *nm_i = NM_I(sbi);
1010         pgoff_t block_off;
1011         pgoff_t block_addr;
1012         int seg_off;
1013
1014         block_off = NAT_BLOCK_OFFSET(start);
1015         seg_off = block_off >> sbi->log_blocks_per_seg;
1016
1017         block_addr = (pgoff_t)(nm_i->nat_blkaddr +
1018                         (seg_off << sbi->log_blocks_per_seg << 1) +
1019                         (block_off & ((1 << sbi->log_blocks_per_seg) -1)));
1020         if (pack)
1021                 *pack = 1;
1022
1023         if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) {
1024                 block_addr += sbi->blocks_per_seg;
1025                 if (pack)
1026                         *pack = 2;
1027         }
1028
1029         return block_addr;
1030 }
1031
1032 static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi)
1033 {
1034         struct f2fs_nm_info *nm_i = NM_I(sbi);
1035         int nid_bitmap_size = (nm_i->max_nid + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
1036         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1037         struct f2fs_summary_block *sum = curseg->sum_blk;
1038         struct f2fs_journal *journal = &sum->journal;
1039         struct f2fs_nat_block *nat_block;
1040         block_t start_blk;
1041         nid_t nid;
1042         int i;
1043
1044         if (!(c.func == SLOAD || c.func == FSCK))
1045                 return 0;
1046
1047         nm_i->nid_bitmap = (char *)calloc(nid_bitmap_size, 1);
1048         if (!nm_i->nid_bitmap)
1049                 return -ENOMEM;
1050
1051         /* arbitrarily set 0 bit */
1052         f2fs_set_bit(0, nm_i->nid_bitmap);
1053
1054         nat_block = malloc(F2FS_BLKSIZE);
1055         if (!nat_block) {
1056                 free(nm_i->nid_bitmap);
1057                 return -ENOMEM;
1058         }
1059
1060         for (nid = 0; nid < nm_i->max_nid; nid++) {
1061                 if (!(nid % NAT_ENTRY_PER_BLOCK)) {
1062                         int ret;
1063
1064                         start_blk = current_nat_addr(sbi, nid, NULL);
1065                         ret = dev_read_block(nat_block, start_blk);
1066                         ASSERT(ret >= 0);
1067                 }
1068
1069                 if (nat_block->entries[nid % NAT_ENTRY_PER_BLOCK].block_addr)
1070                         f2fs_set_bit(nid, nm_i->nid_bitmap);
1071         }
1072
1073         if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
1074                 MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
1075                         "NAT_JOURNAL_ENTRIES(%lu)\n",
1076                         nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
1077                 journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
1078                 c.fix_on = 1;
1079         }
1080
1081         for (i = 0; i < nats_in_cursum(journal); i++) {
1082                 block_t addr;
1083
1084                 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
1085                 if (!IS_VALID_BLK_ADDR(sbi, addr)) {
1086                         MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
1087                         journal->n_nats = cpu_to_le16(i);
1088                         c.fix_on = 1;
1089                         continue;
1090                 }
1091
1092                 nid = le32_to_cpu(nid_in_journal(journal, i));
1093                 if (!IS_VALID_NID(sbi, nid)) {
1094                         MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
1095                         journal->n_nats = cpu_to_le16(i);
1096                         c.fix_on = 1;
1097                         continue;
1098                 }
1099                 if (addr != NULL_ADDR)
1100                         f2fs_set_bit(nid, nm_i->nid_bitmap);
1101         }
1102         free(nat_block);
1103         return 0;
1104 }
1105
1106 u32 update_nat_bits_flags(struct f2fs_super_block *sb,
1107                                 struct f2fs_checkpoint *cp, u32 flags)
1108 {
1109         u_int32_t nat_bits_bytes, nat_bits_blocks;
1110
1111         nat_bits_bytes = get_sb(segment_count_nat) << 5;
1112         nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
1113                                                 F2FS_BLKSIZE - 1);
1114         if (get_cp(cp_pack_total_block_count) <=
1115                         (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
1116                 flags |= CP_NAT_BITS_FLAG;
1117         else
1118                 flags &= (~CP_NAT_BITS_FLAG);
1119
1120         return flags;
1121 }
1122
1123 /* should call flush_journal_entries() bfore this */
1124 void write_nat_bits(struct f2fs_sb_info *sbi,
1125         struct f2fs_super_block *sb, struct f2fs_checkpoint *cp, int set)
1126 {
1127         struct f2fs_nm_info *nm_i = NM_I(sbi);
1128         u_int32_t nat_blocks = get_sb(segment_count_nat) <<
1129                                 (get_sb(log_blocks_per_seg) - 1);
1130         u_int32_t nat_bits_bytes = nat_blocks >> 3;
1131         u_int32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1132                                         8 + F2FS_BLKSIZE - 1);
1133         unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1134         struct f2fs_nat_block *nat_block;
1135         u_int32_t i, j;
1136         block_t blkaddr;
1137         int ret;
1138
1139         nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1140         ASSERT(nat_bits);
1141
1142         nat_block = malloc(F2FS_BLKSIZE);
1143         ASSERT(nat_block);
1144
1145         full_nat_bits = nat_bits + 8;
1146         empty_nat_bits = full_nat_bits + nat_bits_bytes;
1147
1148         memset(full_nat_bits, 0, nat_bits_bytes);
1149         memset(empty_nat_bits, 0, nat_bits_bytes);
1150
1151         for (i = 0; i < nat_blocks; i++) {
1152                 int seg_off = i >> get_sb(log_blocks_per_seg);
1153                 int valid = 0;
1154
1155                 blkaddr = (pgoff_t)(get_sb(nat_blkaddr) +
1156                                 (seg_off << get_sb(log_blocks_per_seg) << 1) +
1157                                 (i & ((1 << get_sb(log_blocks_per_seg)) - 1)));
1158
1159                 /*
1160                  * Should consider new nat_blocks is larger than old
1161                  * nm_i->nat_blocks, since nm_i->nat_bitmap is based on
1162                  * old one.
1163                  */
1164                 if (i < nm_i->nat_blocks && f2fs_test_bit(i, nm_i->nat_bitmap))
1165                         blkaddr += (1 << get_sb(log_blocks_per_seg));
1166
1167                 ret = dev_read_block(nat_block, blkaddr);
1168                 ASSERT(ret >= 0);
1169
1170                 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1171                         if ((i == 0 && j == 0) ||
1172                                 nat_block->entries[j].block_addr != NULL_ADDR)
1173                                 valid++;
1174                 }
1175                 if (valid == 0)
1176                         test_and_set_bit_le(i, empty_nat_bits);
1177                 else if (valid == NAT_ENTRY_PER_BLOCK)
1178                         test_and_set_bit_le(i, full_nat_bits);
1179         }
1180         *(__le64 *)nat_bits = get_cp_crc(cp);
1181         free(nat_block);
1182
1183         blkaddr = get_sb(segment0_blkaddr) + (set <<
1184                                 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1185
1186         DBG(1, "\tWriting NAT bits pages, at offset 0x%08x\n", blkaddr);
1187
1188         for (i = 0; i < nat_bits_blocks; i++) {
1189                 if (dev_write_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i))
1190                         ASSERT_MSG("\tError: write NAT bits to disk!!!\n");
1191         }
1192         MSG(0, "Info: Write valid nat_bits in checkpoint\n");
1193
1194         free(nat_bits);
1195 }
1196
1197 static int check_nat_bits(struct f2fs_sb_info *sbi,
1198         struct f2fs_super_block *sb, struct f2fs_checkpoint *cp)
1199 {
1200         struct f2fs_nm_info *nm_i = NM_I(sbi);
1201         u_int32_t nat_blocks = get_sb(segment_count_nat) <<
1202                                 (get_sb(log_blocks_per_seg) - 1);
1203         u_int32_t nat_bits_bytes = nat_blocks >> 3;
1204         u_int32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1205                                         8 + F2FS_BLKSIZE - 1);
1206         unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1207         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1208         struct f2fs_journal *journal = &curseg->sum_blk->journal;
1209         u_int32_t i, j;
1210         block_t blkaddr;
1211         int err = 0;
1212
1213         nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1214         ASSERT(nat_bits);
1215
1216         full_nat_bits = nat_bits + 8;
1217         empty_nat_bits = full_nat_bits + nat_bits_bytes;
1218
1219         blkaddr = get_sb(segment0_blkaddr) + (sbi->cur_cp <<
1220                                 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1221
1222         for (i = 0; i < nat_bits_blocks; i++) {
1223                 if (dev_read_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i))
1224                         ASSERT_MSG("\tError: read NAT bits to disk!!!\n");
1225         }
1226
1227         if (*(__le64 *)nat_bits != get_cp_crc(cp) || nats_in_cursum(journal)) {
1228                 /*
1229                  * if there is a journal, f2fs was not shutdown cleanly. Let's
1230                  * flush them with nat_bits.
1231                  */
1232                 if (c.fix_on)
1233                         err = -1;
1234                 /* Otherwise, kernel will disable nat_bits */
1235                 goto out;
1236         }
1237
1238         for (i = 0; i < nat_blocks; i++) {
1239                 u_int32_t start_nid = i * NAT_ENTRY_PER_BLOCK;
1240                 u_int32_t valid = 0;
1241                 int empty = test_bit_le(i, empty_nat_bits);
1242                 int full = test_bit_le(i, full_nat_bits);
1243
1244                 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1245                         if (f2fs_test_bit(start_nid + j, nm_i->nid_bitmap))
1246                                 valid++;
1247                 }
1248                 if (valid == 0) {
1249                         if (!empty || full) {
1250                                 err = -1;
1251                                 goto out;
1252                         }
1253                 } else if (valid == NAT_ENTRY_PER_BLOCK) {
1254                         if (empty || !full) {
1255                                 err = -1;
1256                                 goto out;
1257                         }
1258                 } else {
1259                         if (empty || full) {
1260                                 err = -1;
1261                                 goto out;
1262                         }
1263                 }
1264         }
1265 out:
1266         free(nat_bits);
1267         if (!err) {
1268                 MSG(0, "Info: Checked valid nat_bits in checkpoint\n");
1269         } else {
1270                 c.bug_nat_bits = 1;
1271                 MSG(0, "Info: Corrupted valid nat_bits in checkpoint\n");
1272         }
1273         return err;
1274 }
1275
1276 int init_node_manager(struct f2fs_sb_info *sbi)
1277 {
1278         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1279         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1280         struct f2fs_nm_info *nm_i = NM_I(sbi);
1281         unsigned char *version_bitmap;
1282         unsigned int nat_segs;
1283
1284         nm_i->nat_blkaddr = get_sb(nat_blkaddr);
1285
1286         /* segment_count_nat includes pair segment so divide to 2. */
1287         nat_segs = get_sb(segment_count_nat) >> 1;
1288         nm_i->nat_blocks = nat_segs << get_sb(log_blocks_per_seg);
1289         nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
1290         nm_i->fcnt = 0;
1291         nm_i->nat_cnt = 0;
1292         nm_i->init_scan_nid = get_cp(next_free_nid);
1293         nm_i->next_scan_nid = get_cp(next_free_nid);
1294
1295         nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1296
1297         nm_i->nat_bitmap = malloc(nm_i->bitmap_size);
1298         if (!nm_i->nat_bitmap)
1299                 return -ENOMEM;
1300         version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1301         if (!version_bitmap)
1302                 return -EFAULT;
1303
1304         /* copy version bitmap */
1305         memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size);
1306         return f2fs_init_nid_bitmap(sbi);
1307 }
1308
1309 int build_node_manager(struct f2fs_sb_info *sbi)
1310 {
1311         int err;
1312         sbi->nm_info = malloc(sizeof(struct f2fs_nm_info));
1313         if (!sbi->nm_info)
1314                 return -ENOMEM;
1315
1316         err = init_node_manager(sbi);
1317         if (err)
1318                 return err;
1319
1320         return 0;
1321 }
1322
1323 int build_sit_info(struct f2fs_sb_info *sbi)
1324 {
1325         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1326         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1327         struct sit_info *sit_i;
1328         unsigned int sit_segs, start;
1329         char *src_bitmap, *dst_bitmap;
1330         unsigned int bitmap_size;
1331
1332         sit_i = malloc(sizeof(struct sit_info));
1333         if (!sit_i) {
1334                 MSG(1, "\tError: Malloc failed for build_sit_info!\n");
1335                 return -ENOMEM;
1336         }
1337
1338         SM_I(sbi)->sit_info = sit_i;
1339
1340         sit_i->sentries = calloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry), 1);
1341         if (!sit_i->sentries) {
1342                 MSG(1, "\tError: Calloc failed for build_sit_info!\n");
1343                 goto free_sit_info;
1344         }
1345
1346         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1347                 sit_i->sentries[start].cur_valid_map
1348                         = calloc(SIT_VBLOCK_MAP_SIZE, 1);
1349                 if (!sit_i->sentries[start].cur_valid_map) {
1350                         MSG(1, "\tError: Calloc failed for build_sit_info!!\n");
1351                         goto free_validity_maps;
1352                 }
1353         }
1354
1355         sit_segs = get_sb(segment_count_sit) >> 1;
1356         bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1357         src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1358
1359         dst_bitmap = malloc(bitmap_size);
1360         if (!dst_bitmap) {
1361                 MSG(1, "\tError: Malloc failed for build_sit_info!!\n");
1362                 goto free_validity_maps;
1363         }
1364
1365         memcpy(dst_bitmap, src_bitmap, bitmap_size);
1366
1367         sit_i->sit_base_addr = get_sb(sit_blkaddr);
1368         sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1369         sit_i->written_valid_blocks = get_cp(valid_block_count);
1370         sit_i->sit_bitmap = dst_bitmap;
1371         sit_i->bitmap_size = bitmap_size;
1372         sit_i->dirty_sentries = 0;
1373         sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1374         sit_i->elapsed_time = get_cp(elapsed_time);
1375         return 0;
1376
1377 free_validity_maps:
1378         for (--start ; start >= 0; --start)
1379                 free(sit_i->sentries[start].cur_valid_map);
1380         free(sit_i->sentries);
1381
1382 free_sit_info:
1383         free(sit_i);
1384
1385         return -ENOMEM;
1386 }
1387
1388 void reset_curseg(struct f2fs_sb_info *sbi, int type)
1389 {
1390         struct curseg_info *curseg = CURSEG_I(sbi, type);
1391         struct summary_footer *sum_footer;
1392         struct seg_entry *se;
1393
1394         sum_footer = &(curseg->sum_blk->footer);
1395         memset(sum_footer, 0, sizeof(struct summary_footer));
1396         if (IS_DATASEG(type))
1397                 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1398         if (IS_NODESEG(type))
1399                 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1400         se = get_seg_entry(sbi, curseg->segno);
1401         se->type = type;
1402         se->dirty = 1;
1403 }
1404
1405 static void read_compacted_summaries(struct f2fs_sb_info *sbi)
1406 {
1407         struct curseg_info *curseg;
1408         unsigned int i, j, offset;
1409         block_t start;
1410         char *kaddr;
1411         int ret;
1412
1413         start = start_sum_block(sbi);
1414
1415         kaddr = (char *)malloc(PAGE_SIZE);
1416         ASSERT(kaddr);
1417
1418         ret = dev_read_block(kaddr, start++);
1419         ASSERT(ret >= 0);
1420
1421         curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1422         memcpy(&curseg->sum_blk->journal.n_nats, kaddr, SUM_JOURNAL_SIZE);
1423
1424         curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1425         memcpy(&curseg->sum_blk->journal.n_sits, kaddr + SUM_JOURNAL_SIZE,
1426                                                 SUM_JOURNAL_SIZE);
1427
1428         offset = 2 * SUM_JOURNAL_SIZE;
1429         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1430                 unsigned short blk_off;
1431                 struct curseg_info *curseg = CURSEG_I(sbi, i);
1432
1433                 reset_curseg(sbi, i);
1434
1435                 if (curseg->alloc_type == SSR)
1436                         blk_off = sbi->blocks_per_seg;
1437                 else
1438                         blk_off = curseg->next_blkoff;
1439
1440                 ASSERT(blk_off <= ENTRIES_IN_SUM);
1441
1442                 for (j = 0; j < blk_off; j++) {
1443                         struct f2fs_summary *s;
1444                         s = (struct f2fs_summary *)(kaddr + offset);
1445                         curseg->sum_blk->entries[j] = *s;
1446                         offset += SUMMARY_SIZE;
1447                         if (offset + SUMMARY_SIZE <=
1448                                         PAGE_CACHE_SIZE - SUM_FOOTER_SIZE)
1449                                 continue;
1450                         memset(kaddr, 0, PAGE_SIZE);
1451                         ret = dev_read_block(kaddr, start++);
1452                         ASSERT(ret >= 0);
1453                         offset = 0;
1454                 }
1455         }
1456         free(kaddr);
1457 }
1458
1459 static void restore_node_summary(struct f2fs_sb_info *sbi,
1460                 unsigned int segno, struct f2fs_summary_block *sum_blk)
1461 {
1462         struct f2fs_node *node_blk;
1463         struct f2fs_summary *sum_entry;
1464         block_t addr;
1465         unsigned int i;
1466         int ret;
1467
1468         node_blk = malloc(F2FS_BLKSIZE);
1469         ASSERT(node_blk);
1470
1471         /* scan the node segment */
1472         addr = START_BLOCK(sbi, segno);
1473         sum_entry = &sum_blk->entries[0];
1474
1475         for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) {
1476                 ret = dev_read_block(node_blk, addr);
1477                 ASSERT(ret >= 0);
1478                 sum_entry->nid = node_blk->footer.nid;
1479                 addr++;
1480         }
1481         free(node_blk);
1482 }
1483
1484 static void read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1485 {
1486         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1487         struct f2fs_summary_block *sum_blk;
1488         struct curseg_info *curseg;
1489         unsigned int segno = 0;
1490         block_t blk_addr = 0;
1491         int ret;
1492
1493         if (IS_DATASEG(type)) {
1494                 segno = get_cp(cur_data_segno[type]);
1495                 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
1496                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1497                 else
1498                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1499         } else {
1500                 segno = get_cp(cur_node_segno[type - CURSEG_HOT_NODE]);
1501                 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
1502                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1503                                                         type - CURSEG_HOT_NODE);
1504                 else
1505                         blk_addr = GET_SUM_BLKADDR(sbi, segno);
1506         }
1507
1508         sum_blk = (struct f2fs_summary_block *)malloc(PAGE_SIZE);
1509         ASSERT(sum_blk);
1510
1511         ret = dev_read_block(sum_blk, blk_addr);
1512         ASSERT(ret >= 0);
1513
1514         if (IS_NODESEG(type) && !is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
1515                 restore_node_summary(sbi, segno, sum_blk);
1516
1517         curseg = CURSEG_I(sbi, type);
1518         memcpy(curseg->sum_blk, sum_blk, PAGE_CACHE_SIZE);
1519         reset_curseg(sbi, type);
1520         free(sum_blk);
1521 }
1522
1523 void update_sum_entry(struct f2fs_sb_info *sbi, block_t blk_addr,
1524                                         struct f2fs_summary *sum)
1525 {
1526         struct f2fs_summary_block *sum_blk;
1527         u32 segno, offset;
1528         int type, ret;
1529         struct seg_entry *se;
1530
1531         segno = GET_SEGNO(sbi, blk_addr);
1532         offset = OFFSET_IN_SEG(sbi, blk_addr);
1533
1534         se = get_seg_entry(sbi, segno);
1535
1536         sum_blk = get_sum_block(sbi, segno, &type);
1537         memcpy(&sum_blk->entries[offset], sum, sizeof(*sum));
1538         sum_blk->footer.entry_type = IS_NODESEG(se->type) ? SUM_TYPE_NODE :
1539                                                         SUM_TYPE_DATA;
1540
1541         /* write SSA all the time */
1542         ret = dev_write_block(sum_blk, GET_SUM_BLKADDR(sbi, segno));
1543         ASSERT(ret >= 0);
1544
1545         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
1546                                         type == SEG_TYPE_MAX)
1547                 free(sum_blk);
1548 }
1549
1550 static void restore_curseg_summaries(struct f2fs_sb_info *sbi)
1551 {
1552         int type = CURSEG_HOT_DATA;
1553
1554         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1555                 read_compacted_summaries(sbi);
1556                 type = CURSEG_HOT_NODE;
1557         }
1558
1559         for (; type <= CURSEG_COLD_NODE; type++)
1560                 read_normal_summaries(sbi, type);
1561 }
1562
1563 static int build_curseg(struct f2fs_sb_info *sbi)
1564 {
1565         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1566         struct curseg_info *array;
1567         unsigned short blk_off;
1568         unsigned int segno;
1569         int i;
1570
1571         array = malloc(sizeof(*array) * NR_CURSEG_TYPE);
1572         if (!array) {
1573                 MSG(1, "\tError: Malloc failed for build_curseg!\n");
1574                 return -ENOMEM;
1575         }
1576
1577         SM_I(sbi)->curseg_array = array;
1578
1579         for (i = 0; i < NR_CURSEG_TYPE; i++) {
1580                 array[i].sum_blk = malloc(PAGE_CACHE_SIZE);
1581                 if (!array[i].sum_blk) {
1582                         MSG(1, "\tError: Malloc failed for build_curseg!!\n");
1583                         goto seg_cleanup;
1584                 }
1585
1586                 if (i <= CURSEG_COLD_DATA) {
1587                         blk_off = get_cp(cur_data_blkoff[i]);
1588                         segno = get_cp(cur_data_segno[i]);
1589                 }
1590                 if (i > CURSEG_COLD_DATA) {
1591                         blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]);
1592                         segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]);
1593                 }
1594                 ASSERT(segno < TOTAL_SEGS(sbi));
1595                 ASSERT(blk_off < DEFAULT_BLOCKS_PER_SEGMENT);
1596
1597                 array[i].segno = segno;
1598                 array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno);
1599                 array[i].next_segno = NULL_SEGNO;
1600                 array[i].next_blkoff = blk_off;
1601                 array[i].alloc_type = cp->alloc_type[i];
1602         }
1603         restore_curseg_summaries(sbi);
1604         return 0;
1605
1606 seg_cleanup:
1607         for(--i ; i >=0; --i)
1608                 free(array[i].sum_blk);
1609         free(array);
1610
1611         return -ENOMEM;
1612 }
1613
1614 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
1615 {
1616         unsigned int end_segno = SM_I(sbi)->segment_count - 1;
1617         ASSERT(segno <= end_segno);
1618 }
1619
1620 void get_current_sit_page(struct f2fs_sb_info *sbi,
1621                         unsigned int segno, struct f2fs_sit_block *sit_blk)
1622 {
1623         struct sit_info *sit_i = SIT_I(sbi);
1624         unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1625         block_t blk_addr = sit_i->sit_base_addr + offset;
1626         int ret;
1627
1628         check_seg_range(sbi, segno);
1629
1630         /* calculate sit block address */
1631         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1632                 blk_addr += sit_i->sit_blocks;
1633
1634         ret = dev_read_block(sit_blk, blk_addr);
1635         ASSERT(ret >= 0);
1636 }
1637
1638 void rewrite_current_sit_page(struct f2fs_sb_info *sbi,
1639                         unsigned int segno, struct f2fs_sit_block *sit_blk)
1640 {
1641         struct sit_info *sit_i = SIT_I(sbi);
1642         unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1643         block_t blk_addr = sit_i->sit_base_addr + offset;
1644         int ret;
1645
1646         /* calculate sit block address */
1647         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1648                 blk_addr += sit_i->sit_blocks;
1649
1650         ret = dev_write_block(sit_blk, blk_addr);
1651         ASSERT(ret >= 0);
1652 }
1653
1654 void check_block_count(struct f2fs_sb_info *sbi,
1655                 unsigned int segno, struct f2fs_sit_entry *raw_sit)
1656 {
1657         struct f2fs_sm_info *sm_info = SM_I(sbi);
1658         unsigned int end_segno = sm_info->segment_count - 1;
1659         int valid_blocks = 0;
1660         unsigned int i;
1661
1662         /* check segment usage */
1663         if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg)
1664                 ASSERT_MSG("Invalid SIT vblocks: segno=0x%x, %u",
1665                                 segno, GET_SIT_VBLOCKS(raw_sit));
1666
1667         /* check boundary of a given segment number */
1668         if (segno > end_segno)
1669                 ASSERT_MSG("Invalid SEGNO: 0x%x", segno);
1670
1671         /* check bitmap with valid block count */
1672         for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
1673                 valid_blocks += get_bits_in_byte(raw_sit->valid_map[i]);
1674
1675         if (GET_SIT_VBLOCKS(raw_sit) != valid_blocks)
1676                 ASSERT_MSG("Wrong SIT valid blocks: segno=0x%x, %u vs. %u",
1677                                 segno, GET_SIT_VBLOCKS(raw_sit), valid_blocks);
1678
1679         if (GET_SIT_TYPE(raw_sit) >= NO_CHECK_TYPE)
1680                 ASSERT_MSG("Wrong SIT type: segno=0x%x, %u",
1681                                 segno, GET_SIT_TYPE(raw_sit));
1682 }
1683
1684 void seg_info_from_raw_sit(struct seg_entry *se,
1685                 struct f2fs_sit_entry *raw_sit)
1686 {
1687         se->valid_blocks = GET_SIT_VBLOCKS(raw_sit);
1688         memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE);
1689         se->type = GET_SIT_TYPE(raw_sit);
1690         se->orig_type = GET_SIT_TYPE(raw_sit);
1691         se->mtime = le64_to_cpu(raw_sit->mtime);
1692 }
1693
1694 struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
1695                 unsigned int segno)
1696 {
1697         struct sit_info *sit_i = SIT_I(sbi);
1698         return &sit_i->sentries[segno];
1699 }
1700
1701 struct f2fs_summary_block *get_sum_block(struct f2fs_sb_info *sbi,
1702                                 unsigned int segno, int *ret_type)
1703 {
1704         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1705         struct f2fs_summary_block *sum_blk;
1706         struct curseg_info *curseg;
1707         int type, ret;
1708         u64 ssa_blk;
1709
1710         *ret_type= SEG_TYPE_MAX;
1711
1712         ssa_blk = GET_SUM_BLKADDR(sbi, segno);
1713         for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) {
1714                 if (segno == get_cp(cur_node_segno[type])) {
1715                         curseg = CURSEG_I(sbi, CURSEG_HOT_NODE + type);
1716                         if (!IS_SUM_NODE_SEG(curseg->sum_blk->footer)) {
1717                                 ASSERT_MSG("segno [0x%x] indicates a data "
1718                                                 "segment, but should be node",
1719                                                 segno);
1720                                 *ret_type = -SEG_TYPE_CUR_NODE;
1721                         } else {
1722                                 *ret_type = SEG_TYPE_CUR_NODE;
1723                         }
1724                         return curseg->sum_blk;
1725                 }
1726         }
1727
1728         for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) {
1729                 if (segno == get_cp(cur_data_segno[type])) {
1730                         curseg = CURSEG_I(sbi, type);
1731                         if (IS_SUM_NODE_SEG(curseg->sum_blk->footer)) {
1732                                 ASSERT_MSG("segno [0x%x] indicates a node "
1733                                                 "segment, but should be data",
1734                                                 segno);
1735                                 *ret_type = -SEG_TYPE_CUR_DATA;
1736                         } else {
1737                                 *ret_type = SEG_TYPE_CUR_DATA;
1738                         }
1739                         return curseg->sum_blk;
1740                 }
1741         }
1742
1743         sum_blk = calloc(BLOCK_SZ, 1);
1744         ASSERT(sum_blk);
1745
1746         ret = dev_read_block(sum_blk, ssa_blk);
1747         ASSERT(ret >= 0);
1748
1749         if (IS_SUM_NODE_SEG(sum_blk->footer))
1750                 *ret_type = SEG_TYPE_NODE;
1751         else if (IS_SUM_DATA_SEG(sum_blk->footer))
1752                 *ret_type = SEG_TYPE_DATA;
1753
1754         return sum_blk;
1755 }
1756
1757 int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr,
1758                                 struct f2fs_summary *sum_entry)
1759 {
1760         struct f2fs_summary_block *sum_blk;
1761         u32 segno, offset;
1762         int type;
1763
1764         segno = GET_SEGNO(sbi, blk_addr);
1765         offset = OFFSET_IN_SEG(sbi, blk_addr);
1766
1767         sum_blk = get_sum_block(sbi, segno, &type);
1768         memcpy(sum_entry, &(sum_blk->entries[offset]),
1769                                 sizeof(struct f2fs_summary));
1770         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
1771                                         type == SEG_TYPE_MAX)
1772                 free(sum_blk);
1773         return type;
1774 }
1775
1776 static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
1777                                 struct f2fs_nat_entry *raw_nat)
1778 {
1779         struct f2fs_nat_block *nat_block;
1780         pgoff_t block_addr;
1781         int entry_off;
1782         int ret;
1783
1784         if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0)
1785                 return;
1786
1787         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
1788         ASSERT(nat_block);
1789
1790         entry_off = nid % NAT_ENTRY_PER_BLOCK;
1791         block_addr = current_nat_addr(sbi, nid, NULL);
1792
1793         ret = dev_read_block(nat_block, block_addr);
1794         ASSERT(ret >= 0);
1795
1796         memcpy(raw_nat, &nat_block->entries[entry_off],
1797                                         sizeof(struct f2fs_nat_entry));
1798         free(nat_block);
1799 }
1800
1801 void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid,
1802                                 u16 ofs_in_node, block_t newaddr)
1803 {
1804         struct f2fs_node *node_blk = NULL;
1805         struct node_info ni;
1806         block_t oldaddr, startaddr, endaddr;
1807         int ret;
1808
1809         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
1810         ASSERT(node_blk);
1811
1812         get_node_info(sbi, nid, &ni);
1813
1814         /* read node_block */
1815         ret = dev_read_block(node_blk, ni.blk_addr);
1816         ASSERT(ret >= 0);
1817
1818         /* check its block address */
1819         if (node_blk->footer.nid == node_blk->footer.ino) {
1820                 int ofs = get_extra_isize(node_blk);
1821
1822                 oldaddr = le32_to_cpu(node_blk->i.i_addr[ofs + ofs_in_node]);
1823                 node_blk->i.i_addr[ofs + ofs_in_node] = cpu_to_le32(newaddr);
1824                 ret = write_inode(node_blk, ni.blk_addr);
1825                 ASSERT(ret >= 0);
1826         } else {
1827                 oldaddr = le32_to_cpu(node_blk->dn.addr[ofs_in_node]);
1828                 node_blk->dn.addr[ofs_in_node] = cpu_to_le32(newaddr);
1829                 ret = dev_write_block(node_blk, ni.blk_addr);
1830                 ASSERT(ret >= 0);
1831         }
1832
1833         /* check extent cache entry */
1834         if (node_blk->footer.nid != node_blk->footer.ino) {
1835                 get_node_info(sbi, le32_to_cpu(node_blk->footer.ino), &ni);
1836
1837                 /* read inode block */
1838                 ret = dev_read_block(node_blk, ni.blk_addr);
1839                 ASSERT(ret >= 0);
1840         }
1841
1842         startaddr = le32_to_cpu(node_blk->i.i_ext.blk_addr);
1843         endaddr = startaddr + le32_to_cpu(node_blk->i.i_ext.len);
1844         if (oldaddr >= startaddr && oldaddr < endaddr) {
1845                 node_blk->i.i_ext.len = 0;
1846
1847                 /* update inode block */
1848                 ASSERT(write_inode(node_blk, ni.blk_addr) >= 0);
1849         }
1850         free(node_blk);
1851 }
1852
1853 void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,
1854                                         nid_t nid, block_t newaddr)
1855 {
1856         struct f2fs_nat_block *nat_block;
1857         pgoff_t block_addr;
1858         int entry_off;
1859         int ret;
1860
1861         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
1862         ASSERT(nat_block);
1863
1864         entry_off = nid % NAT_ENTRY_PER_BLOCK;
1865         block_addr = current_nat_addr(sbi, nid, NULL);
1866
1867         ret = dev_read_block(nat_block, block_addr);
1868         ASSERT(ret >= 0);
1869
1870         if (ino)
1871                 nat_block->entries[entry_off].ino = cpu_to_le32(ino);
1872         nat_block->entries[entry_off].block_addr = cpu_to_le32(newaddr);
1873         if (c.func == FSCK)
1874                 F2FS_FSCK(sbi)->entries[nid] = nat_block->entries[entry_off];
1875
1876         ret = dev_write_block(nat_block, block_addr);
1877         ASSERT(ret >= 0);
1878         free(nat_block);
1879 }
1880
1881 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
1882 {
1883         struct f2fs_nat_entry raw_nat;
1884
1885         ni->nid = nid;
1886         if (c.func == FSCK) {
1887                 node_info_from_raw_nat(ni, &(F2FS_FSCK(sbi)->entries[nid]));
1888                 if (ni->blk_addr)
1889                         return;
1890                 /* nat entry is not cached, read it */
1891         }
1892
1893         get_nat_entry(sbi, nid, &raw_nat);
1894         node_info_from_raw_nat(ni, &raw_nat);
1895 }
1896
1897 static int build_sit_entries(struct f2fs_sb_info *sbi)
1898 {
1899         struct sit_info *sit_i = SIT_I(sbi);
1900         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1901         struct f2fs_journal *journal = &curseg->sum_blk->journal;
1902         struct f2fs_sit_block *sit_blk;
1903         struct seg_entry *se;
1904         struct f2fs_sit_entry sit;
1905         unsigned int i, segno;
1906
1907         sit_blk = calloc(BLOCK_SZ, 1);
1908         if (!sit_blk) {
1909                 MSG(1, "\tError: Calloc failed for build_sit_entries!\n");
1910                 return -ENOMEM;
1911         }
1912
1913         for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) {
1914                 se = &sit_i->sentries[segno];
1915
1916                 get_current_sit_page(sbi, segno, sit_blk);
1917                 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
1918
1919                 check_block_count(sbi, segno, &sit);
1920                 seg_info_from_raw_sit(se, &sit);
1921         }
1922
1923         free(sit_blk);
1924
1925         if (sits_in_cursum(journal) > SIT_JOURNAL_ENTRIES) {
1926                 MSG(0, "\tError: build_sit_entries truncate n_sits(%u) to "
1927                         "SIT_JOURNAL_ENTRIES(%lu)\n",
1928                         sits_in_cursum(journal), SIT_JOURNAL_ENTRIES);
1929                 journal->n_sits = cpu_to_le16(SIT_JOURNAL_ENTRIES);
1930                 c.fix_on = 1;
1931         }
1932
1933         for (i = 0; i < sits_in_cursum(journal); i++) {
1934                 segno = le32_to_cpu(segno_in_journal(journal, i));
1935
1936                 if (segno >= TOTAL_SEGS(sbi)) {
1937                         MSG(0, "\tError: build_sit_entries: segno(%u) is invalid!!!\n", segno);
1938                         journal->n_sits = cpu_to_le16(i);
1939                         c.fix_on = 1;
1940                         continue;
1941                 }
1942
1943                 se = &sit_i->sentries[segno];
1944                 sit = sit_in_journal(journal, i);
1945
1946                 check_block_count(sbi, segno, &sit);
1947                 seg_info_from_raw_sit(se, &sit);
1948         }
1949         return 0;
1950 }
1951
1952 static int build_segment_manager(struct f2fs_sb_info *sbi)
1953 {
1954         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1955         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1956         struct f2fs_sm_info *sm_info;
1957
1958         sm_info = malloc(sizeof(struct f2fs_sm_info));
1959         if (!sm_info) {
1960                 MSG(1, "\tError: Malloc failed for build_segment_manager!\n");
1961                 return -ENOMEM;
1962         }
1963
1964         /* init sm info */
1965         sbi->sm_info = sm_info;
1966         sm_info->seg0_blkaddr = get_sb(segment0_blkaddr);
1967         sm_info->main_blkaddr = get_sb(main_blkaddr);
1968         sm_info->segment_count = get_sb(segment_count);
1969         sm_info->reserved_segments = get_cp(rsvd_segment_count);
1970         sm_info->ovp_segments = get_cp(overprov_segment_count);
1971         sm_info->main_segments = get_sb(segment_count_main);
1972         sm_info->ssa_blkaddr = get_sb(ssa_blkaddr);
1973
1974         if (build_sit_info(sbi) || build_curseg(sbi) || build_sit_entries(sbi)) {
1975                 free(sm_info);
1976                 return -ENOMEM;
1977         }
1978
1979         return 0;
1980 }
1981
1982 void build_sit_area_bitmap(struct f2fs_sb_info *sbi)
1983 {
1984         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1985         struct f2fs_sm_info *sm_i = SM_I(sbi);
1986         unsigned int segno = 0;
1987         char *ptr = NULL;
1988         u32 sum_vblocks = 0;
1989         u32 free_segs = 0;
1990         struct seg_entry *se;
1991
1992         fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE;
1993         fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz);
1994         ASSERT(fsck->sit_area_bitmap);
1995         ptr = fsck->sit_area_bitmap;
1996
1997         ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz);
1998
1999         for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) {
2000                 se = get_seg_entry(sbi, segno);
2001
2002                 memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2003                 ptr += SIT_VBLOCK_MAP_SIZE;
2004
2005                 if (se->valid_blocks == 0x0) {
2006                         if (le32_to_cpu(sbi->ckpt->cur_node_segno[0]) == segno ||
2007                                 le32_to_cpu(sbi->ckpt->cur_data_segno[0]) == segno ||
2008                                 le32_to_cpu(sbi->ckpt->cur_node_segno[1]) == segno ||
2009                                 le32_to_cpu(sbi->ckpt->cur_data_segno[1]) == segno ||
2010                                 le32_to_cpu(sbi->ckpt->cur_node_segno[2]) == segno ||
2011                                 le32_to_cpu(sbi->ckpt->cur_data_segno[2]) == segno) {
2012                                 continue;
2013                         } else {
2014                                 free_segs++;
2015                         }
2016                 } else {
2017                         sum_vblocks += se->valid_blocks;
2018                 }
2019         }
2020         fsck->chk.sit_valid_blocks = sum_vblocks;
2021         fsck->chk.sit_free_segs = free_segs;
2022
2023         DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n",
2024                         sum_vblocks, sum_vblocks,
2025                         free_segs, free_segs);
2026 }
2027
2028 void rewrite_sit_area_bitmap(struct f2fs_sb_info *sbi)
2029 {
2030         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2031         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2032         struct sit_info *sit_i = SIT_I(sbi);
2033         struct f2fs_sit_block *sit_blk;
2034         unsigned int segno = 0;
2035         struct f2fs_summary_block *sum = curseg->sum_blk;
2036         char *ptr = NULL;
2037
2038         sit_blk = calloc(BLOCK_SZ, 1);
2039         ASSERT(sit_blk);
2040         /* remove sit journal */
2041         sum->journal.n_sits = 0;
2042
2043         ptr = fsck->main_area_bitmap;
2044
2045         for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) {
2046                 struct f2fs_sit_entry *sit;
2047                 struct seg_entry *se;
2048                 u16 valid_blocks = 0;
2049                 u16 type;
2050                 int i;
2051
2052                 get_current_sit_page(sbi, segno, sit_blk);
2053                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2054                 memcpy(sit->valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2055
2056                 /* update valid block count */
2057                 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
2058                         valid_blocks += get_bits_in_byte(sit->valid_map[i]);
2059
2060                 se = get_seg_entry(sbi, segno);
2061                 memcpy(se->cur_valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2062                 se->valid_blocks = valid_blocks;
2063                 type = se->type;
2064                 if (type >= NO_CHECK_TYPE) {
2065                         ASSERT_MSG("Invalide type and valid blocks=%x,%x",
2066                                         segno, valid_blocks);
2067                         type = 0;
2068                 }
2069                 sit->vblocks = cpu_to_le16((type << SIT_VBLOCKS_SHIFT) |
2070                                                                 valid_blocks);
2071                 rewrite_current_sit_page(sbi, segno, sit_blk);
2072
2073                 ptr += SIT_VBLOCK_MAP_SIZE;
2074         }
2075
2076         free(sit_blk);
2077 }
2078
2079 static int flush_sit_journal_entries(struct f2fs_sb_info *sbi)
2080 {
2081         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2082         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2083         struct sit_info *sit_i = SIT_I(sbi);
2084         struct f2fs_sit_block *sit_blk;
2085         unsigned int segno;
2086         int i;
2087
2088         sit_blk = calloc(BLOCK_SZ, 1);
2089         ASSERT(sit_blk);
2090         for (i = 0; i < sits_in_cursum(journal); i++) {
2091                 struct f2fs_sit_entry *sit;
2092                 struct seg_entry *se;
2093
2094                 segno = segno_in_journal(journal, i);
2095                 se = get_seg_entry(sbi, segno);
2096
2097                 get_current_sit_page(sbi, segno, sit_blk);
2098                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2099
2100                 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2101                 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2102                                                         se->valid_blocks);
2103                 sit->mtime = cpu_to_le64(se->mtime);
2104
2105                 rewrite_current_sit_page(sbi, segno, sit_blk);
2106         }
2107
2108         free(sit_blk);
2109         journal->n_sits = 0;
2110         return i;
2111 }
2112
2113 static int flush_nat_journal_entries(struct f2fs_sb_info *sbi)
2114 {
2115         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2116         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2117         struct f2fs_nat_block *nat_block;
2118         pgoff_t block_addr;
2119         int entry_off;
2120         nid_t nid;
2121         int ret;
2122         int i = 0;
2123
2124         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2125         ASSERT(nat_block);
2126 next:
2127         if (i >= nats_in_cursum(journal)) {
2128                 free(nat_block);
2129                 journal->n_nats = 0;
2130                 return i;
2131         }
2132
2133         nid = le32_to_cpu(nid_in_journal(journal, i));
2134
2135         entry_off = nid % NAT_ENTRY_PER_BLOCK;
2136         block_addr = current_nat_addr(sbi, nid, NULL);
2137
2138         ret = dev_read_block(nat_block, block_addr);
2139         ASSERT(ret >= 0);
2140
2141         memcpy(&nat_block->entries[entry_off], &nat_in_journal(journal, i),
2142                                         sizeof(struct f2fs_nat_entry));
2143
2144         ret = dev_write_block(nat_block, block_addr);
2145         ASSERT(ret >= 0);
2146         i++;
2147         goto next;
2148 }
2149
2150 void flush_journal_entries(struct f2fs_sb_info *sbi)
2151 {
2152         int n_nats = flush_nat_journal_entries(sbi);
2153         int n_sits = flush_sit_journal_entries(sbi);
2154
2155         if (n_nats || n_sits)
2156                 write_checkpoint(sbi);
2157 }
2158
2159 void flush_sit_entries(struct f2fs_sb_info *sbi)
2160 {
2161         struct sit_info *sit_i = SIT_I(sbi);
2162         struct f2fs_sit_block *sit_blk;
2163         unsigned int segno = 0;
2164
2165         sit_blk = calloc(BLOCK_SZ, 1);
2166         ASSERT(sit_blk);
2167         /* update free segments */
2168         for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) {
2169                 struct f2fs_sit_entry *sit;
2170                 struct seg_entry *se;
2171
2172                 se = get_seg_entry(sbi, segno);
2173
2174                 if (!se->dirty)
2175                         continue;
2176
2177                 get_current_sit_page(sbi, segno, sit_blk);
2178                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2179                 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2180                 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2181                                                         se->valid_blocks);
2182                 rewrite_current_sit_page(sbi, segno, sit_blk);
2183         }
2184
2185         free(sit_blk);
2186 }
2187
2188 int relocate_curseg_offset(struct f2fs_sb_info *sbi, int type)
2189 {
2190         struct curseg_info *curseg = CURSEG_I(sbi, type);
2191         struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
2192         unsigned int i;
2193
2194         for (i = 0; i < sbi->blocks_per_seg; i++) {
2195                 if (!f2fs_test_bit(i, (const char *)se->cur_valid_map))
2196                         break;
2197         }
2198
2199         if (i == sbi->blocks_per_seg)
2200                 return -EINVAL;
2201
2202         DBG(1, "Update curseg[%d].next_blkoff %u -> %u, alloc_type %s -> SSR\n",
2203                         type, curseg->next_blkoff, i,
2204                         curseg->alloc_type == LFS ? "LFS" : "SSR");
2205
2206         curseg->next_blkoff = i;
2207         curseg->alloc_type = SSR;
2208
2209         return 0;
2210 }
2211
2212 int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left, int type)
2213 {
2214         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2215         struct seg_entry *se;
2216         u32 segno;
2217         u32 offset;
2218         int not_enough = 0;
2219         u64 end_blkaddr = (get_sb(segment_count_main) <<
2220                         get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
2221
2222         if (*to > 0)
2223                 *to -= left;
2224         if (get_free_segments(sbi) <= SM_I(sbi)->reserved_segments + 1)
2225                 not_enough = 1;
2226
2227         while (*to >= SM_I(sbi)->main_blkaddr && *to < end_blkaddr) {
2228                 segno = GET_SEGNO(sbi, *to);
2229                 offset = OFFSET_IN_SEG(sbi, *to);
2230
2231                 se = get_seg_entry(sbi, segno);
2232
2233                 if (se->valid_blocks == sbi->blocks_per_seg ||
2234                                 IS_CUR_SEGNO(sbi, segno)) {
2235                         *to = left ? START_BLOCK(sbi, segno) - 1:
2236                                                 START_BLOCK(sbi, segno + 1);
2237                         continue;
2238                 }
2239
2240                 if (se->valid_blocks == 0 && not_enough) {
2241                         *to = left ? START_BLOCK(sbi, segno) - 1:
2242                                                 START_BLOCK(sbi, segno + 1);
2243                         continue;
2244                 }
2245
2246                 if (se->valid_blocks == 0 && !(segno % sbi->segs_per_sec)) {
2247                         struct seg_entry *se2;
2248                         unsigned int i;
2249
2250                         for (i = 1; i < sbi->segs_per_sec; i++) {
2251                                 se2 = get_seg_entry(sbi, segno + i);
2252                                 if (se2->valid_blocks)
2253                                         break;
2254                         }
2255                         if (i == sbi->segs_per_sec)
2256                                 return 0;
2257                 }
2258
2259                 if (se->type == type &&
2260                         !f2fs_test_bit(offset, (const char *)se->cur_valid_map))
2261                         return 0;
2262
2263                 *to = left ? *to - 1: *to + 1;
2264         }
2265         return -1;
2266 }
2267
2268 void move_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left)
2269 {
2270         int i, ret;
2271
2272         /* update summary blocks having nullified journal entries */
2273         for (i = 0; i < NO_CHECK_TYPE; i++) {
2274                 struct curseg_info *curseg = CURSEG_I(sbi, i);
2275                 struct f2fs_summary_block buf;
2276                 u32 old_segno;
2277                 u64 ssa_blk, to;
2278
2279                 /* update original SSA too */
2280                 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
2281                 ret = dev_write_block(curseg->sum_blk, ssa_blk);
2282                 ASSERT(ret >= 0);
2283
2284                 to = from;
2285                 ret = find_next_free_block(sbi, &to, left, i);
2286                 ASSERT(ret == 0);
2287
2288                 old_segno = curseg->segno;
2289                 curseg->segno = GET_SEGNO(sbi, to);
2290                 curseg->next_blkoff = OFFSET_IN_SEG(sbi, to);
2291                 curseg->alloc_type = SSR;
2292
2293                 /* update new segno */
2294                 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
2295                 ret = dev_read_block(&buf, ssa_blk);
2296                 ASSERT(ret >= 0);
2297
2298                 memcpy(curseg->sum_blk, &buf, SUM_ENTRIES_SIZE);
2299
2300                 /* update se->types */
2301                 reset_curseg(sbi, i);
2302
2303                 DBG(1, "Move curseg[%d] %x -> %x after %"PRIx64"\n",
2304                                 i, old_segno, curseg->segno, from);
2305         }
2306 }
2307
2308 void update_curseg_info(struct f2fs_sb_info *sbi, int type)
2309 {
2310         if (!relocate_curseg_offset(sbi, type))
2311                 return;
2312         move_curseg_info(sbi, SM_I(sbi)->main_blkaddr, 0);
2313 }
2314
2315 void zero_journal_entries(struct f2fs_sb_info *sbi)
2316 {
2317         int i;
2318
2319         for (i = 0; i < NO_CHECK_TYPE; i++)
2320                 CURSEG_I(sbi, i)->sum_blk->journal.n_nats = 0;
2321 }
2322
2323 void write_curseg_info(struct f2fs_sb_info *sbi)
2324 {
2325         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2326         int i;
2327
2328         for (i = 0; i < NO_CHECK_TYPE; i++) {
2329                 cp->alloc_type[i] = CURSEG_I(sbi, i)->alloc_type;
2330                 if (i < CURSEG_HOT_NODE) {
2331                         set_cp(cur_data_segno[i], CURSEG_I(sbi, i)->segno);
2332                         set_cp(cur_data_blkoff[i],
2333                                         CURSEG_I(sbi, i)->next_blkoff);
2334                 } else {
2335                         int n = i - CURSEG_HOT_NODE;
2336
2337                         set_cp(cur_node_segno[n], CURSEG_I(sbi, i)->segno);
2338                         set_cp(cur_node_blkoff[n],
2339                                         CURSEG_I(sbi, i)->next_blkoff);
2340                 }
2341         }
2342 }
2343
2344 int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid,
2345                                         struct f2fs_nat_entry *raw_nat)
2346 {
2347         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2348         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2349         int i = 0;
2350
2351         for (i = 0; i < nats_in_cursum(journal); i++) {
2352                 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
2353                         memcpy(raw_nat, &nat_in_journal(journal, i),
2354                                                 sizeof(struct f2fs_nat_entry));
2355                         DBG(3, "==> Found nid [0x%x] in nat cache\n", nid);
2356                         return i;
2357                 }
2358         }
2359         return -1;
2360 }
2361
2362 void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid)
2363 {
2364         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2365         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2366         struct f2fs_nat_block *nat_block;
2367         pgoff_t block_addr;
2368         int entry_off;
2369         int ret;
2370         int i = 0;
2371
2372         /* check in journal */
2373         for (i = 0; i < nats_in_cursum(journal); i++) {
2374                 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
2375                         memset(&nat_in_journal(journal, i), 0,
2376                                         sizeof(struct f2fs_nat_entry));
2377                         FIX_MSG("Remove nid [0x%x] in nat journal", nid);
2378                         return;
2379                 }
2380         }
2381         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2382         ASSERT(nat_block);
2383
2384         entry_off = nid % NAT_ENTRY_PER_BLOCK;
2385         block_addr = current_nat_addr(sbi, nid, NULL);
2386
2387         ret = dev_read_block(nat_block, block_addr);
2388         ASSERT(ret >= 0);
2389
2390         if (nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi)) {
2391                 FIX_MSG("nid [0x%x] block_addr= 0x%x -> 0x1", nid,
2392                         le32_to_cpu(nat_block->entries[entry_off].block_addr));
2393                 nat_block->entries[entry_off].block_addr = cpu_to_le32(0x1);
2394         } else {
2395                 memset(&nat_block->entries[entry_off], 0,
2396                                         sizeof(struct f2fs_nat_entry));
2397                 FIX_MSG("Remove nid [0x%x] in NAT", nid);
2398         }
2399
2400         ret = dev_write_block(nat_block, block_addr);
2401         ASSERT(ret >= 0);
2402         free(nat_block);
2403 }
2404
2405 void write_checkpoint(struct f2fs_sb_info *sbi)
2406 {
2407         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2408         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2409         block_t orphan_blks = 0;
2410         unsigned long long cp_blk_no;
2411         u32 flags = CP_UMOUNT_FLAG;
2412         int i, ret;
2413         u_int32_t crc = 0;
2414
2415         if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2416                 orphan_blks = __start_sum_addr(sbi) - 1;
2417                 flags |= CP_ORPHAN_PRESENT_FLAG;
2418         }
2419         if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2420                 flags |= CP_TRIMMED_FLAG;
2421         if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2422                 flags |= CP_DISABLED_FLAG;
2423         if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
2424                 flags |= CP_LARGE_NAT_BITMAP_FLAG;
2425                 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
2426         } else {
2427                 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
2428         }
2429
2430         set_cp(free_segment_count, get_free_segments(sbi));
2431         set_cp(valid_block_count, sbi->total_valid_block_count);
2432         set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload));
2433
2434         flags = update_nat_bits_flags(sb, cp, flags);
2435         set_cp(ckpt_flags, flags);
2436
2437         crc = f2fs_checkpoint_chksum(cp);
2438         *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
2439                                                         cpu_to_le32(crc);
2440
2441         cp_blk_no = get_sb(cp_blkaddr);
2442         if (sbi->cur_cp == 2)
2443                 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2444
2445         /* write the first cp */
2446         ret = dev_write_block(cp, cp_blk_no++);
2447         ASSERT(ret >= 0);
2448
2449         /* skip payload */
2450         cp_blk_no += get_sb(cp_payload);
2451         /* skip orphan blocks */
2452         cp_blk_no += orphan_blks;
2453
2454         /* update summary blocks having nullified journal entries */
2455         for (i = 0; i < NO_CHECK_TYPE; i++) {
2456                 struct curseg_info *curseg = CURSEG_I(sbi, i);
2457                 u64 ssa_blk;
2458
2459                 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2460                 ASSERT(ret >= 0);
2461
2462                 /* update original SSA too */
2463                 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
2464                 ret = dev_write_block(curseg->sum_blk, ssa_blk);
2465                 ASSERT(ret >= 0);
2466         }
2467
2468         /* Write nat bits */
2469         if (flags & CP_NAT_BITS_FLAG)
2470                 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2471
2472         /* in case of sudden power off */
2473         ret = f2fs_fsync_device();
2474         ASSERT(ret >= 0);
2475
2476         /* write the last cp */
2477         ret = dev_write_block(cp, cp_blk_no++);
2478         ASSERT(ret >= 0);
2479
2480         ret = f2fs_fsync_device();
2481         ASSERT(ret >= 0);
2482 }
2483
2484 void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
2485 {
2486         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2487         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2488         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2489         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2490         struct f2fs_nm_info *nm_i = NM_I(sbi);
2491         struct f2fs_nat_block *nat_block;
2492         struct node_info ni;
2493         u32 nid, nr_nat_blks;
2494         pgoff_t block_off;
2495         pgoff_t block_addr;
2496         int seg_off;
2497         int ret;
2498         unsigned int i;
2499
2500         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2501         ASSERT(nat_block);
2502
2503         /* Alloc & build nat entry bitmap */
2504         nr_nat_blks = (get_sb(segment_count_nat) / 2) <<
2505                                         sbi->log_blocks_per_seg;
2506
2507         fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK;
2508         fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8;
2509         fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2510         ASSERT(fsck->nat_area_bitmap);
2511
2512         fsck->entries = calloc(sizeof(struct f2fs_nat_entry),
2513                                         fsck->nr_nat_entries);
2514         ASSERT(fsck->entries);
2515
2516         for (block_off = 0; block_off < nr_nat_blks; block_off++) {
2517
2518                 seg_off = block_off >> sbi->log_blocks_per_seg;
2519                 block_addr = (pgoff_t)(nm_i->nat_blkaddr +
2520                         (seg_off << sbi->log_blocks_per_seg << 1) +
2521                         (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
2522
2523                 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
2524                         block_addr += sbi->blocks_per_seg;
2525
2526                 ret = dev_read_block(nat_block, block_addr);
2527                 ASSERT(ret >= 0);
2528
2529                 nid = block_off * NAT_ENTRY_PER_BLOCK;
2530                 for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
2531                         ni.nid = nid + i;
2532
2533                         if ((nid + i) == F2FS_NODE_INO(sbi) ||
2534                                         (nid + i) == F2FS_META_INO(sbi)) {
2535                                 /*
2536                                  * block_addr of node/meta inode should be 0x1.
2537                                  * Set this bit, and fsck_verify will fix it.
2538                                  */
2539                                 if (le32_to_cpu(nat_block->entries[i].block_addr) != 0x1) {
2540                                         ASSERT_MSG("\tError: ino[0x%x] block_addr[0x%x] is invalid\n",
2541                                                         nid + i, le32_to_cpu(nat_block->entries[i].block_addr));
2542                                         f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
2543                                 }
2544                                 continue;
2545                         }
2546
2547                         node_info_from_raw_nat(&ni, &nat_block->entries[i]);
2548                         if (ni.blk_addr == 0x0)
2549                                 continue;
2550                         if (ni.ino == 0x0) {
2551                                 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
2552                                         " is invalid\n", ni.ino, ni.blk_addr);
2553                         }
2554                         if (ni.ino == (nid + i)) {
2555                                 fsck->nat_valid_inode_cnt++;
2556                                 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
2557                         }
2558                         if (nid + i == 0) {
2559                                 /*
2560                                  * nat entry [0] must be null.  If
2561                                  * it is corrupted, set its bit in
2562                                  * nat_area_bitmap, fsck_verify will
2563                                  * nullify it
2564                                  */
2565                                 ASSERT_MSG("Invalid nat entry[0]: "
2566                                         "blk_addr[0x%x]\n", ni.blk_addr);
2567                                 fsck->chk.valid_nat_entry_cnt--;
2568                         }
2569
2570                         DBG(3, "nid[0x%8x] addr[0x%16x] ino[0x%8x]\n",
2571                                 nid + i, ni.blk_addr, ni.ino);
2572                         f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
2573                         fsck->chk.valid_nat_entry_cnt++;
2574
2575                         fsck->entries[nid + i] = nat_block->entries[i];
2576                 }
2577         }
2578
2579         /* Traverse nat journal, update the corresponding entries */
2580         for (i = 0; i < nats_in_cursum(journal); i++) {
2581                 struct f2fs_nat_entry raw_nat;
2582                 nid = le32_to_cpu(nid_in_journal(journal, i));
2583                 ni.nid = nid;
2584
2585                 DBG(3, "==> Found nid [0x%x] in nat cache, update it\n", nid);
2586
2587                 /* Clear the original bit and count */
2588                 if (fsck->entries[nid].block_addr != 0x0) {
2589                         fsck->chk.valid_nat_entry_cnt--;
2590                         f2fs_clear_bit(nid, fsck->nat_area_bitmap);
2591                         if (fsck->entries[nid].ino == nid)
2592                                 fsck->nat_valid_inode_cnt--;
2593                 }
2594
2595                 /* Use nat entries in journal */
2596                 memcpy(&raw_nat, &nat_in_journal(journal, i),
2597                                         sizeof(struct f2fs_nat_entry));
2598                 node_info_from_raw_nat(&ni, &raw_nat);
2599                 if (ni.blk_addr != 0x0) {
2600                         if (ni.ino == 0x0)
2601                                 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
2602                                         " is invalid\n", ni.ino, ni.blk_addr);
2603                         if (ni.ino == nid) {
2604                                 fsck->nat_valid_inode_cnt++;
2605                                 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
2606                         }
2607                         f2fs_set_bit(nid, fsck->nat_area_bitmap);
2608                         fsck->chk.valid_nat_entry_cnt++;
2609                         DBG(3, "nid[0x%x] in nat cache\n", nid);
2610                 }
2611                 fsck->entries[nid] = raw_nat;
2612         }
2613         free(nat_block);
2614
2615         DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n",
2616                         fsck->chk.valid_nat_entry_cnt,
2617                         fsck->chk.valid_nat_entry_cnt);
2618 }
2619
2620 static int check_sector_size(struct f2fs_super_block *sb)
2621 {
2622         u_int32_t log_sectorsize, log_sectors_per_block;
2623
2624         log_sectorsize = log_base_2(c.sector_size);
2625         log_sectors_per_block = log_base_2(c.sectors_per_blk);
2626
2627         if (log_sectorsize == get_sb(log_sectorsize) &&
2628                         log_sectors_per_block == get_sb(log_sectors_per_block))
2629                 return 0;
2630
2631         set_sb(log_sectorsize, log_sectorsize);
2632         set_sb(log_sectors_per_block, log_sectors_per_block);
2633
2634         update_superblock(sb, SB_MASK_ALL);
2635         return 0;
2636 }
2637
2638 static void tune_sb_features(struct f2fs_sb_info *sbi)
2639 {
2640         int sb_changed = 0;
2641         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2642
2643         if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
2644                         c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) {
2645                 sb->feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
2646                 MSG(0, "Info: Set Encryption feature\n");
2647                 sb_changed = 1;
2648         }
2649         /* TODO: quota needs to allocate inode numbers */
2650
2651         c.feature = sb->feature;
2652         if (!sb_changed)
2653                 return;
2654
2655         update_superblock(sb, SB_MASK_ALL);
2656 }
2657
2658 int f2fs_do_mount(struct f2fs_sb_info *sbi)
2659 {
2660         struct f2fs_checkpoint *cp = NULL;
2661         struct f2fs_super_block *sb = NULL;
2662         int ret;
2663
2664         sbi->active_logs = NR_CURSEG_TYPE;
2665         ret = validate_super_block(sbi, SB0_ADDR);
2666         if (ret) {
2667                 ret = validate_super_block(sbi, SB1_ADDR);
2668                 if (ret)
2669                         return -1;
2670         }
2671         sb = F2FS_RAW_SUPER(sbi);
2672
2673         ret = check_sector_size(sb);
2674         if (ret)
2675                 return -1;
2676
2677         print_raw_sb_info(sb);
2678
2679         init_sb_info(sbi);
2680
2681         ret = get_valid_checkpoint(sbi);
2682         if (ret) {
2683                 ERR_MSG("Can't find valid checkpoint\n");
2684                 return -1;
2685         }
2686
2687         if (sanity_check_ckpt(sbi)) {
2688                 ERR_MSG("Checkpoint is polluted\n");
2689                 return -1;
2690         }
2691         cp = F2FS_CKPT(sbi);
2692
2693         print_ckpt_info(sbi);
2694
2695         if (c.quota_fix) {
2696                 if (get_cp(ckpt_flags) & CP_QUOTA_NEED_FSCK_FLAG)
2697                         c.fix_on = 1;
2698         }
2699
2700         c.bug_on = 0;
2701
2702         tune_sb_features(sbi);
2703
2704         /* precompute checksum seed for metadata */
2705         if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
2706                 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
2707
2708         sbi->total_valid_node_count = get_cp(valid_node_count);
2709         sbi->total_valid_inode_count = get_cp(valid_inode_count);
2710         sbi->user_block_count = get_cp(user_block_count);
2711         sbi->total_valid_block_count = get_cp(valid_block_count);
2712         sbi->last_valid_block_count = sbi->total_valid_block_count;
2713         sbi->alloc_valid_block_count = 0;
2714
2715         if (build_segment_manager(sbi)) {
2716                 ERR_MSG("build_segment_manager failed\n");
2717                 return -1;
2718         }
2719
2720         if (build_node_manager(sbi)) {
2721                 ERR_MSG("build_node_manager failed\n");
2722                 return -1;
2723         }
2724
2725         if (!c.fix_on && (c.auto_fix || c.preen_mode)) {
2726                 u32 flag = get_cp(ckpt_flags);
2727
2728                 if (flag & CP_FSCK_FLAG ||
2729                         flag & CP_QUOTA_NEED_FSCK_FLAG ||
2730                         (exist_qf_ino(sb) && (flag & CP_ERROR_FLAG))) {
2731                         c.fix_on = 1;
2732                 } else if (!c.preen_mode) {
2733                         print_cp_state(flag);
2734                         return 1;
2735                 }
2736         }
2737
2738         /* Check nat_bits */
2739         if (c.func == FSCK && is_set_ckpt_flags(cp, CP_NAT_BITS_FLAG)) {
2740                 if (check_nat_bits(sbi, sb, cp) && c.fix_on)
2741                         write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2742         }
2743         return 0;
2744 }
2745
2746 void f2fs_do_umount(struct f2fs_sb_info *sbi)
2747 {
2748         struct sit_info *sit_i = SIT_I(sbi);
2749         struct f2fs_sm_info *sm_i = SM_I(sbi);
2750         struct f2fs_nm_info *nm_i = NM_I(sbi);
2751         unsigned int i;
2752
2753         /* free nm_info */
2754         if (c.func == SLOAD || c.func == FSCK)
2755                 free(nm_i->nid_bitmap);
2756         free(nm_i->nat_bitmap);
2757         free(sbi->nm_info);
2758
2759         /* free sit_info */
2760         for (i = 0; i < TOTAL_SEGS(sbi); i++)
2761                 free(sit_i->sentries[i].cur_valid_map);
2762
2763         free(sit_i->sit_bitmap);
2764         free(sm_i->sit_info);
2765
2766         /* free sm_info */
2767         for (i = 0; i < NR_CURSEG_TYPE; i++)
2768                 free(sm_i->curseg_array[i].sum_blk);
2769
2770         free(sm_i->curseg_array);
2771         free(sbi->sm_info);
2772
2773         free(sbi->ckpt);
2774         free(sbi->raw_super);
2775 }
2776
2777 #ifdef WITH_ANDROID
2778 int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi)
2779 {
2780         struct f2fs_super_block *sb = sbi->raw_super;
2781         u_int32_t sit_seg_count, sit_size;
2782         u_int32_t nat_seg_count, nat_size;
2783         u_int64_t sit_seg_addr, nat_seg_addr, payload_addr;
2784         u_int32_t seg_size = 1 << get_sb(log_blocks_per_seg);
2785         int ret;
2786
2787         if (!c.sparse_mode)
2788                 return 0;
2789
2790         sit_seg_addr = get_sb(sit_blkaddr);
2791         sit_seg_count = get_sb(segment_count_sit);
2792         sit_size = sit_seg_count * seg_size;
2793
2794         DBG(1, "\tSparse: filling sit area at block offset: 0x%08"PRIx64" len: %u\n",
2795                                                         sit_seg_addr, sit_size);
2796         ret = dev_fill(NULL, sit_seg_addr * F2FS_BLKSIZE,
2797                                         sit_size * F2FS_BLKSIZE);
2798         if (ret) {
2799                 MSG(1, "\tError: While zeroing out the sit area "
2800                                 "on disk!!!\n");
2801                 return -1;
2802         }
2803
2804         nat_seg_addr = get_sb(nat_blkaddr);
2805         nat_seg_count = get_sb(segment_count_nat);
2806         nat_size = nat_seg_count * seg_size;
2807
2808         DBG(1, "\tSparse: filling nat area at block offset 0x%08"PRIx64" len: %u\n",
2809                                                         nat_seg_addr, nat_size);
2810         ret = dev_fill(NULL, nat_seg_addr * F2FS_BLKSIZE,
2811                                         nat_size * F2FS_BLKSIZE);
2812         if (ret) {
2813                 MSG(1, "\tError: While zeroing out the nat area "
2814                                 "on disk!!!\n");
2815                 return -1;
2816         }
2817
2818         payload_addr = get_sb(segment0_blkaddr) + 1;
2819
2820         DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
2821                                         payload_addr, get_sb(cp_payload));
2822         ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
2823                                         get_sb(cp_payload) * F2FS_BLKSIZE);
2824         if (ret) {
2825                 MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
2826                                 "on disk!!!\n");
2827                 return -1;
2828         }
2829
2830         payload_addr += seg_size;
2831
2832         DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
2833                                         payload_addr, get_sb(cp_payload));
2834         ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
2835                                         get_sb(cp_payload) * F2FS_BLKSIZE);
2836         if (ret) {
2837                 MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
2838                                 "on disk!!!\n");
2839                 return -1;
2840         }
2841         return 0;
2842 }
2843 #else
2844 int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi) { return 0; }
2845 #endif