f2fs-tools: fix to le32 type variable correctly
[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 "node.h"
13 #include "xattr.h"
14 #include "quota.h"
15 #include <locale.h>
16 #include <stdbool.h>
17 #include <time.h>
18 #ifdef HAVE_LINUX_POSIX_ACL_H
19 #include <linux/posix_acl.h>
20 #endif
21 #ifdef HAVE_SYS_ACL_H
22 #include <sys/acl.h>
23 #endif
24 #ifdef HAVE_UUID_UUID_H
25 #include <uuid/uuid.h>
26 #endif
27
28 #ifndef ACL_UNDEFINED_TAG
29 #define ACL_UNDEFINED_TAG       (0x00)
30 #define ACL_USER_OBJ            (0x01)
31 #define ACL_USER                (0x02)
32 #define ACL_GROUP_OBJ           (0x04)
33 #define ACL_GROUP               (0x08)
34 #define ACL_MASK                (0x10)
35 #define ACL_OTHER               (0x20)
36 #endif
37
38 #ifdef HAVE_LINUX_BLKZONED_H
39
40 static int get_device_idx(struct f2fs_sb_info *sbi, uint32_t segno)
41 {
42         block_t seg_start_blkaddr;
43         int i;
44
45         seg_start_blkaddr = SM_I(sbi)->main_blkaddr +
46                                 segno * DEFAULT_BLOCKS_PER_SEGMENT;
47         for (i = 0; i < c.ndevs; i++)
48                 if (c.devices[i].start_blkaddr <= seg_start_blkaddr &&
49                         c.devices[i].end_blkaddr > seg_start_blkaddr)
50                         return i;
51         return 0;
52 }
53
54 static int get_zone_idx_from_dev(struct f2fs_sb_info *sbi,
55                                         uint32_t segno, uint32_t dev_idx)
56 {
57         block_t seg_start_blkaddr = START_BLOCK(sbi, segno);
58
59         return (seg_start_blkaddr - c.devices[dev_idx].start_blkaddr) >>
60                         log_base_2(sbi->segs_per_sec * sbi->blocks_per_seg);
61 }
62
63 bool is_usable_seg(struct f2fs_sb_info *sbi, unsigned int segno)
64 {
65         unsigned int secno = segno / sbi->segs_per_sec;
66         block_t seg_start = START_BLOCK(sbi, segno);
67         block_t blocks_per_sec = sbi->blocks_per_seg * sbi->segs_per_sec;
68         unsigned int dev_idx = get_device_idx(sbi, segno);
69         unsigned int zone_idx = get_zone_idx_from_dev(sbi, segno, dev_idx);
70         unsigned int sec_off = SM_I(sbi)->main_blkaddr >>
71                                                 log_base_2(blocks_per_sec);
72
73         if (zone_idx < c.devices[dev_idx].nr_rnd_zones)
74                 return true;
75
76         if (c.devices[dev_idx].zoned_model != F2FS_ZONED_HM)
77                 return true;
78
79         return seg_start < ((sec_off + secno) * blocks_per_sec) +
80                                 c.devices[dev_idx].zone_cap_blocks[zone_idx];
81 }
82
83 unsigned int get_usable_seg_count(struct f2fs_sb_info *sbi)
84 {
85         unsigned int i, usable_seg_count = 0;
86
87         for (i = 0; i < MAIN_SEGS(sbi); i++)
88                 if (is_usable_seg(sbi, i))
89                         usable_seg_count++;
90
91         return usable_seg_count;
92 }
93
94 #else
95
96 bool is_usable_seg(struct f2fs_sb_info *UNUSED(sbi), unsigned int UNUSED(segno))
97 {
98         return true;
99 }
100
101 unsigned int get_usable_seg_count(struct f2fs_sb_info *sbi)
102 {
103         return MAIN_SEGS(sbi);
104 }
105
106 #endif
107
108 u32 get_free_segments(struct f2fs_sb_info *sbi)
109 {
110         u32 i, free_segs = 0;
111
112         for (i = 0; i < MAIN_SEGS(sbi); i++) {
113                 struct seg_entry *se = get_seg_entry(sbi, i);
114
115                 if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, i) &&
116                                                         is_usable_seg(sbi, i))
117                         free_segs++;
118         }
119         return free_segs;
120 }
121
122 void update_free_segments(struct f2fs_sb_info *sbi)
123 {
124         char *progress = "-*|*-";
125         static int i = 0;
126
127         if (c.dbg_lv)
128                 return;
129
130         MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], get_free_segments(sbi));
131         fflush(stdout);
132         i++;
133 }
134
135 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
136 static void print_acl(const u8 *value, int size)
137 {
138         const struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
139         const struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
140         const u8 *end = value + size;
141         int i, count;
142
143         if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) {
144                 MSG(0, "Invalid ACL version [0x%x : 0x%x]\n",
145                                 le32_to_cpu(hdr->a_version), F2FS_ACL_VERSION);
146                 return;
147         }
148
149         count = f2fs_acl_count(size);
150         if (count <= 0) {
151                 MSG(0, "Invalid ACL value size %d\n", size);
152                 return;
153         }
154
155         for (i = 0; i < count; i++) {
156                 if ((u8 *)entry > end) {
157                         MSG(0, "Invalid ACL entries count %d\n", count);
158                         return;
159                 }
160
161                 switch (le16_to_cpu(entry->e_tag)) {
162                 case ACL_USER_OBJ:
163                 case ACL_GROUP_OBJ:
164                 case ACL_MASK:
165                 case ACL_OTHER:
166                         MSG(0, "tag:0x%x perm:0x%x\n",
167                                         le16_to_cpu(entry->e_tag),
168                                         le16_to_cpu(entry->e_perm));
169                         entry = (struct f2fs_acl_entry *)((char *)entry +
170                                         sizeof(struct f2fs_acl_entry_short));
171                         break;
172                 case ACL_USER:
173                         MSG(0, "tag:0x%x perm:0x%x uid:%u\n",
174                                         le16_to_cpu(entry->e_tag),
175                                         le16_to_cpu(entry->e_perm),
176                                         le32_to_cpu(entry->e_id));
177                         entry = (struct f2fs_acl_entry *)((char *)entry +
178                                         sizeof(struct f2fs_acl_entry));
179                         break;
180                 case ACL_GROUP:
181                         MSG(0, "tag:0x%x perm:0x%x gid:%u\n",
182                                         le16_to_cpu(entry->e_tag),
183                                         le16_to_cpu(entry->e_perm),
184                                         le32_to_cpu(entry->e_id));
185                         entry = (struct f2fs_acl_entry *)((char *)entry +
186                                         sizeof(struct f2fs_acl_entry));
187                         break;
188                 default:
189                         MSG(0, "Unknown ACL tag 0x%x\n",
190                                         le16_to_cpu(entry->e_tag));
191                         return;
192                 }
193         }
194 }
195 #endif /* HAVE_LINUX_POSIX_ACL_H || HAVE_SYS_ACL_H */
196
197 static void print_xattr_entry(const struct f2fs_xattr_entry *ent)
198 {
199         const u8 *value = (const u8 *)&ent->e_name[ent->e_name_len];
200         const int size = le16_to_cpu(ent->e_value_size);
201         const struct fscrypt_context *ctx;
202         int i;
203
204         MSG(0, "\nxattr: e_name_index:%d e_name:", ent->e_name_index);
205         for (i = 0; i < ent->e_name_len; i++)
206                 MSG(0, "%c", ent->e_name[i]);
207         MSG(0, " e_name_len:%d e_value_size:%d e_value:\n",
208                         ent->e_name_len, size);
209
210         switch (ent->e_name_index) {
211 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
212         case F2FS_XATTR_INDEX_POSIX_ACL_ACCESS:
213         case F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT:
214                 print_acl(value, size);
215                 return;
216 #endif
217         case F2FS_XATTR_INDEX_ENCRYPTION:
218                 ctx = (const struct fscrypt_context *)value;
219                 if (size != sizeof(*ctx) ||
220                     ctx->format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
221                         break;
222                 MSG(0, "format: %d\n", ctx->format);
223                 MSG(0, "contents_encryption_mode: 0x%x\n", ctx->contents_encryption_mode);
224                 MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->filenames_encryption_mode);
225                 MSG(0, "flags: 0x%x\n", ctx->flags);
226                 MSG(0, "master_key_descriptor: ");
227                 for (i = 0; i < FS_KEY_DESCRIPTOR_SIZE; i++)
228                         MSG(0, "%02X", ctx->master_key_descriptor[i]);
229                 MSG(0, "\nnonce: ");
230                 for (i = 0; i < FS_KEY_DERIVATION_NONCE_SIZE; i++)
231                         MSG(0, "%02X", ctx->nonce[i]);
232                 MSG(0, "\n");
233                 return;
234         }
235         for (i = 0; i < size; i++)
236                 MSG(0, "%02X", value[i]);
237         MSG(0, "\n");
238 }
239
240 void print_inode_info(struct f2fs_sb_info *sbi,
241                         struct f2fs_node *node, int name)
242 {
243         struct f2fs_inode *inode = &node->i;
244         void *xattr_addr;
245         struct f2fs_xattr_entry *ent;
246         char en[F2FS_PRINT_NAMELEN];
247         unsigned int i = 0;
248         u32 namelen = le32_to_cpu(inode->i_namelen);
249         int enc_name = file_enc_name(inode);
250         int ofs = get_extra_isize(node);
251
252         pretty_print_filename(inode->i_name, namelen, en, enc_name);
253         if (name && en[0]) {
254                 MSG(0, " - File name         : %s%s\n", en,
255                                 enc_name ? " <encrypted>" : "");
256                 setlocale(LC_ALL, "");
257                 MSG(0, " - File size         : %'" PRIu64 " (bytes)\n",
258                                 le64_to_cpu(inode->i_size));
259                 return;
260         }
261
262         DISP_u32(inode, i_mode);
263         DISP_u32(inode, i_advise);
264         DISP_u32(inode, i_uid);
265         DISP_u32(inode, i_gid);
266         DISP_u32(inode, i_links);
267         DISP_u64(inode, i_size);
268         DISP_u64(inode, i_blocks);
269
270         DISP_u64(inode, i_atime);
271         DISP_u32(inode, i_atime_nsec);
272         DISP_u64(inode, i_ctime);
273         DISP_u32(inode, i_ctime_nsec);
274         DISP_u64(inode, i_mtime);
275         DISP_u32(inode, i_mtime_nsec);
276
277         DISP_u32(inode, i_generation);
278         DISP_u32(inode, i_current_depth);
279         DISP_u32(inode, i_xattr_nid);
280         DISP_u32(inode, i_flags);
281         DISP_u32(inode, i_inline);
282         DISP_u32(inode, i_pino);
283         DISP_u32(inode, i_dir_level);
284
285         if (en[0]) {
286                 DISP_u32(inode, i_namelen);
287                 printf("%-30s\t\t[%s]\n", "i_name", en);
288         }
289
290         printf("i_ext: fofs:%x blkaddr:%x len:%x\n",
291                         le32_to_cpu(inode->i_ext.fofs),
292                         le32_to_cpu(inode->i_ext.blk_addr),
293                         le32_to_cpu(inode->i_ext.len));
294
295         if (c.feature & F2FS_FEATURE_EXTRA_ATTR) {
296                 DISP_u16(inode, i_extra_isize);
297                 if (c.feature & F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)
298                         DISP_u16(inode, i_inline_xattr_size);
299                 if (c.feature & F2FS_FEATURE_PRJQUOTA)
300                         DISP_u32(inode, i_projid);
301                 if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
302                         DISP_u32(inode, i_inode_checksum);
303                 if (c.feature & F2FS_FEATURE_INODE_CRTIME) {
304                         DISP_u64(inode, i_crtime);
305                         DISP_u32(inode, i_crtime_nsec);
306                 }
307                 if (c.feature & F2FS_FEATURE_COMPRESSION) {
308                         DISP_u64(inode, i_compr_blocks);
309                         DISP_u8(inode, i_compress_algorithm);
310                         DISP_u8(inode, i_log_cluster_size);
311                         DISP_u16(inode, i_compress_flag);
312                 }
313         }
314
315         for (i = 0; i < ADDRS_PER_INODE(inode); i++) {
316                 block_t blkaddr;
317                 char *flag = "";
318
319                 if (i + ofs >= DEF_ADDRS_PER_INODE)
320                         break;
321
322                 blkaddr = le32_to_cpu(inode->i_addr[i + ofs]);
323
324                 if (blkaddr == 0x0)
325                         continue;
326                 if (blkaddr == COMPRESS_ADDR)
327                         flag = "cluster flag";
328                 else if (blkaddr == NEW_ADDR)
329                         flag = "reserved flag";
330                 printf("i_addr[0x%x] %-16s\t\t[0x%8x : %u]\n", i + ofs, flag,
331                                 blkaddr, blkaddr);
332         }
333
334         DISP_u32(inode, i_nid[0]);      /* direct */
335         DISP_u32(inode, i_nid[1]);      /* direct */
336         DISP_u32(inode, i_nid[2]);      /* indirect */
337         DISP_u32(inode, i_nid[3]);      /* indirect */
338         DISP_u32(inode, i_nid[4]);      /* double indirect */
339
340         xattr_addr = read_all_xattrs(sbi, node);
341         if (xattr_addr) {
342                 list_for_each_xattr(ent, xattr_addr) {
343                         print_xattr_entry(ent);
344                 }
345                 free(xattr_addr);
346         }
347
348         printf("\n");
349 }
350
351 void print_node_info(struct f2fs_sb_info *sbi,
352                         struct f2fs_node *node_block, int verbose)
353 {
354         nid_t ino = le32_to_cpu(node_block->footer.ino);
355         nid_t nid = le32_to_cpu(node_block->footer.nid);
356         /* Is this inode? */
357         if (ino == nid) {
358                 DBG(verbose, "Node ID [0x%x:%u] is inode\n", nid, nid);
359                 print_inode_info(sbi, node_block, verbose);
360         } else {
361                 int i;
362                 u32 *dump_blk = (u32 *)node_block;
363                 DBG(verbose,
364                         "Node ID [0x%x:%u] is direct node or indirect node.\n",
365                                                                 nid, nid);
366                 for (i = 0; i < DEF_ADDRS_PER_BLOCK; i++)
367                         MSG(verbose, "[%d]\t\t\t[0x%8x : %d]\n",
368                                                 i, dump_blk[i], dump_blk[i]);
369         }
370 }
371
372 void print_extention_list(struct f2fs_super_block *sb, int cold)
373 {
374         int start, end, i;
375
376         if (cold) {
377                 DISP_u32(sb, extension_count);
378
379                 start = 0;
380                 end = le32_to_cpu(sb->extension_count);
381         } else {
382                 DISP_u8(sb, hot_ext_count);
383
384                 start = le32_to_cpu(sb->extension_count);
385                 end = start + sb->hot_ext_count;
386         }
387
388         printf("%s file extentsions\n", cold ? "cold" : "hot");
389
390         for (i = start; i < end; i++) {
391                 if (c.layout) {
392                         printf("%-30s %-8.8s\n", "extension_list",
393                                                 sb->extension_list[i]);
394                 } else {
395                         if (i % 4 == 0)
396                                 printf("%-30s\t\t[", "");
397
398                         printf("%-8.8s", sb->extension_list[i]);
399
400                         if (i % 4 == 4 - 1 || i == end - start - 1)
401                                 printf("]\n");
402                 }
403         }
404 }
405
406 static void DISP_label(const char *name)
407 {
408         char buffer[MAX_VOLUME_NAME];
409
410         utf16_to_utf8(buffer, name, MAX_VOLUME_NAME, MAX_VOLUME_NAME);
411         if (c.layout)
412                 printf("%-30s %s\n", "Filesystem volume name:", buffer);
413         else
414                 printf("%-30s" "\t\t[%s]\n", "volum_name", buffer);
415 }
416
417 void print_sb_debug_info(struct f2fs_super_block *sb);
418 void print_raw_sb_info(struct f2fs_super_block *sb)
419 {
420 #ifdef HAVE_LIBUUID
421         char uuid[40];
422         char encrypt_pw_salt[40];
423 #endif
424
425         if (c.layout)
426                 goto printout;
427         if (!c.dbg_lv)
428                 return;
429
430         printf("\n");
431         printf("+--------------------------------------------------------+\n");
432         printf("| Super block                                            |\n");
433         printf("+--------------------------------------------------------+\n");
434 printout:
435         DISP_u32(sb, magic);
436         DISP_u32(sb, major_ver);
437
438         DISP_u32(sb, minor_ver);
439         DISP_u32(sb, log_sectorsize);
440         DISP_u32(sb, log_sectors_per_block);
441
442         DISP_u32(sb, log_blocksize);
443         DISP_u32(sb, log_blocks_per_seg);
444         DISP_u32(sb, segs_per_sec);
445         DISP_u32(sb, secs_per_zone);
446         DISP_u32(sb, checksum_offset);
447         DISP_u64(sb, block_count);
448
449         DISP_u32(sb, section_count);
450         DISP_u32(sb, segment_count);
451         DISP_u32(sb, segment_count_ckpt);
452         DISP_u32(sb, segment_count_sit);
453         DISP_u32(sb, segment_count_nat);
454
455         DISP_u32(sb, segment_count_ssa);
456         DISP_u32(sb, segment_count_main);
457         DISP_u32(sb, segment0_blkaddr);
458
459         DISP_u32(sb, cp_blkaddr);
460         DISP_u32(sb, sit_blkaddr);
461         DISP_u32(sb, nat_blkaddr);
462         DISP_u32(sb, ssa_blkaddr);
463         DISP_u32(sb, main_blkaddr);
464
465         DISP_u32(sb, root_ino);
466         DISP_u32(sb, node_ino);
467         DISP_u32(sb, meta_ino);
468
469 #ifdef HAVE_LIBUUID
470         uuid_unparse(sb->uuid, uuid);
471         DISP_raw_str("%-.36s", uuid);
472 #endif
473
474         DISP_label((const char *)sb->volume_name);
475
476         print_extention_list(sb, 1);
477         print_extention_list(sb, 0);
478
479         DISP_u32(sb, cp_payload);
480
481         DISP_str("%-.252s", sb, version);
482         DISP_str("%-.252s", sb, init_version);
483
484         DISP_u32(sb, feature);
485         DISP_u8(sb, encryption_level);
486
487 #ifdef HAVE_LIBUUID
488         uuid_unparse(sb->encrypt_pw_salt, encrypt_pw_salt);
489         DISP_raw_str("%-.36s", encrypt_pw_salt);
490 #endif
491
492         DISP_u32(sb, qf_ino[USRQUOTA]);
493         DISP_u32(sb, qf_ino[GRPQUOTA]);
494         DISP_u32(sb, qf_ino[PRJQUOTA]);
495
496         DISP_u16(sb, s_encoding);
497         DISP_u32(sb, crc);
498
499         print_sb_debug_info(sb);
500
501         printf("\n");
502 }
503
504 void print_ckpt_info(struct f2fs_sb_info *sbi)
505 {
506         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
507
508         if (c.layout)
509                 goto printout;
510         if (!c.dbg_lv)
511                 return;
512
513         printf("\n");
514         printf("+--------------------------------------------------------+\n");
515         printf("| Checkpoint                                             |\n");
516         printf("+--------------------------------------------------------+\n");
517 printout:
518         DISP_u64(cp, checkpoint_ver);
519         DISP_u64(cp, user_block_count);
520         DISP_u64(cp, valid_block_count);
521         DISP_u32(cp, rsvd_segment_count);
522         DISP_u32(cp, overprov_segment_count);
523         DISP_u32(cp, free_segment_count);
524
525         DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]);
526         DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]);
527         DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]);
528         DISP_u32(cp, cur_node_segno[0]);
529         DISP_u32(cp, cur_node_segno[1]);
530         DISP_u32(cp, cur_node_segno[2]);
531
532         DISP_u32(cp, cur_node_blkoff[0]);
533         DISP_u32(cp, cur_node_blkoff[1]);
534         DISP_u32(cp, cur_node_blkoff[2]);
535
536
537         DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]);
538         DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]);
539         DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]);
540         DISP_u32(cp, cur_data_segno[0]);
541         DISP_u32(cp, cur_data_segno[1]);
542         DISP_u32(cp, cur_data_segno[2]);
543
544         DISP_u32(cp, cur_data_blkoff[0]);
545         DISP_u32(cp, cur_data_blkoff[1]);
546         DISP_u32(cp, cur_data_blkoff[2]);
547
548         DISP_u32(cp, ckpt_flags);
549         DISP_u32(cp, cp_pack_total_block_count);
550         DISP_u32(cp, cp_pack_start_sum);
551         DISP_u32(cp, valid_node_count);
552         DISP_u32(cp, valid_inode_count);
553         DISP_u32(cp, next_free_nid);
554         DISP_u32(cp, sit_ver_bitmap_bytesize);
555         DISP_u32(cp, nat_ver_bitmap_bytesize);
556         DISP_u32(cp, checksum_offset);
557         DISP_u64(cp, elapsed_time);
558
559         DISP_u32(cp, sit_nat_version_bitmap[0]);
560         printf("\n\n");
561 }
562
563 void print_cp_state(u32 flag)
564 {
565         if (c.show_file_map)
566                 return;
567
568         MSG(0, "Info: checkpoint state = %x : ", flag);
569         if (flag & CP_QUOTA_NEED_FSCK_FLAG)
570                 MSG(0, "%s", " quota_need_fsck");
571         if (flag & CP_LARGE_NAT_BITMAP_FLAG)
572                 MSG(0, "%s", " large_nat_bitmap");
573         if (flag & CP_NOCRC_RECOVERY_FLAG)
574                 MSG(0, "%s", " allow_nocrc");
575         if (flag & CP_TRIMMED_FLAG)
576                 MSG(0, "%s", " trimmed");
577         if (flag & CP_NAT_BITS_FLAG)
578                 MSG(0, "%s", " nat_bits");
579         if (flag & CP_CRC_RECOVERY_FLAG)
580                 MSG(0, "%s", " crc");
581         if (flag & CP_FASTBOOT_FLAG)
582                 MSG(0, "%s", " fastboot");
583         if (flag & CP_FSCK_FLAG)
584                 MSG(0, "%s", " fsck");
585         if (flag & CP_ERROR_FLAG)
586                 MSG(0, "%s", " error");
587         if (flag & CP_COMPACT_SUM_FLAG)
588                 MSG(0, "%s", " compacted_summary");
589         if (flag & CP_ORPHAN_PRESENT_FLAG)
590                 MSG(0, "%s", " orphan_inodes");
591         if (flag & CP_DISABLED_FLAG)
592                 MSG(0, "%s", " disabled");
593         if (flag & CP_RESIZEFS_FLAG)
594                 MSG(0, "%s", " resizefs");
595         if (flag & CP_UMOUNT_FLAG)
596                 MSG(0, "%s", " unmount");
597         else
598                 MSG(0, "%s", " sudden-power-off");
599         MSG(0, "\n");
600 }
601
602 void print_sb_state(struct f2fs_super_block *sb)
603 {
604         unsigned int f = get_sb(feature);
605         int i;
606
607         MSG(0, "Info: superblock features = %x : ", f);
608         if (f & F2FS_FEATURE_ENCRYPT) {
609                 MSG(0, "%s", " encrypt");
610         }
611         if (f & F2FS_FEATURE_VERITY) {
612                 MSG(0, "%s", " verity");
613         }
614         if (f & F2FS_FEATURE_BLKZONED) {
615                 MSG(0, "%s", " blkzoned");
616         }
617         if (f & F2FS_FEATURE_EXTRA_ATTR) {
618                 MSG(0, "%s", " extra_attr");
619         }
620         if (f & F2FS_FEATURE_PRJQUOTA) {
621                 MSG(0, "%s", " project_quota");
622         }
623         if (f & F2FS_FEATURE_INODE_CHKSUM) {
624                 MSG(0, "%s", " inode_checksum");
625         }
626         if (f & F2FS_FEATURE_FLEXIBLE_INLINE_XATTR) {
627                 MSG(0, "%s", " flexible_inline_xattr");
628         }
629         if (f & F2FS_FEATURE_QUOTA_INO) {
630                 MSG(0, "%s", " quota_ino");
631         }
632         if (f & F2FS_FEATURE_INODE_CRTIME) {
633                 MSG(0, "%s", " inode_crtime");
634         }
635         if (f & F2FS_FEATURE_LOST_FOUND) {
636                 MSG(0, "%s", " lost_found");
637         }
638         if (f & F2FS_FEATURE_SB_CHKSUM) {
639                 MSG(0, "%s", " sb_checksum");
640         }
641         if (f & F2FS_FEATURE_CASEFOLD) {
642                 MSG(0, "%s", " casefold");
643         }
644         if (f & F2FS_FEATURE_COMPRESSION) {
645                 MSG(0, "%s", " compression");
646         }
647         if (f & F2FS_FEATURE_RO) {
648                 MSG(0, "%s", " ro");
649         }
650         MSG(0, "\n");
651         MSG(0, "Info: superblock encrypt level = %d, salt = ",
652                                         sb->encryption_level);
653         for (i = 0; i < 16; i++)
654                 MSG(0, "%02x", sb->encrypt_pw_salt[i]);
655         MSG(0, "\n");
656 }
657
658 static char *stop_reason_str[] = {
659         [STOP_CP_REASON_SHUTDOWN]               = "shutdown",
660         [STOP_CP_REASON_FAULT_INJECT]           = "fault_inject",
661         [STOP_CP_REASON_META_PAGE]              = "meta_page",
662         [STOP_CP_REASON_WRITE_FAIL]             = "write_fail",
663         [STOP_CP_REASON_CORRUPTED_SUMMARY]      = "corrupted_summary",
664         [STOP_CP_REASON_UPDATE_INODE]           = "update_inode",
665         [STOP_CP_REASON_FLUSH_FAIL]             = "flush_fail",
666 };
667
668 void print_sb_stop_reason(struct f2fs_super_block *sb)
669 {
670         u8 *reason = sb->s_stop_reason;
671         int i;
672
673         if (!c.force_stop)
674                 return;
675
676         MSG(0, "Info: checkpoint stop reason: ");
677
678         for (i = 0; i < STOP_CP_REASON_MAX; i++) {
679                 if (reason[i])
680                         MSG(0, "%s(%d) ", stop_reason_str[i], reason[i]);
681         }
682
683         MSG(0, "\n");
684 }
685
686 static char *errors_str[] = {
687         [ERROR_CORRUPTED_CLUSTER]               = "corrupted_cluster",
688         [ERROR_FAIL_DECOMPRESSION]              = "fail_decompression",
689         [ERROR_INVALID_BLKADDR]                 = "invalid_blkaddr",
690         [ERROR_CORRUPTED_DIRENT]                = "corrupted_dirent",
691         [ERROR_CORRUPTED_INODE]                 = "corrupted_inode",
692         [ERROR_INCONSISTENT_SUMMARY]            = "inconsistent_summary",
693         [ERROR_INCONSISTENT_FOOTER]             = "inconsistent_footer",
694         [ERROR_INCONSISTENT_SUM_TYPE]           = "inconsistent_sum_type",
695         [ERROR_CORRUPTED_JOURNAL]               = "corrupted_journal",
696         [ERROR_INCONSISTENT_NODE_COUNT]         = "inconsistent_node_count",
697         [ERROR_INCONSISTENT_BLOCK_COUNT]        = "inconsistent_block_count",
698         [ERROR_INVALID_CURSEG]                  = "invalid_curseg",
699         [ERROR_INCONSISTENT_SIT]                = "inconsistent_sit",
700         [ERROR_CORRUPTED_VERITY_XATTR]          = "corrupted_verity_xattr",
701         [ERROR_CORRUPTED_XATTR]                 = "corrupted_xattr",
702 };
703
704 void print_sb_errors(struct f2fs_super_block *sb)
705 {
706         u8 *errors = sb->s_errors;
707         int i;
708
709         if (!c.fs_errors)
710                 return;
711
712         MSG(0, "Info: fs errors: ");
713
714         for (i = 0; i < ERROR_MAX; i++) {
715                 if (test_bit_le(i, errors))
716                         MSG(0, "%s ",  errors_str[i]);
717         }
718
719         MSG(0, "\n");
720 }
721
722 void print_sb_debug_info(struct f2fs_super_block *sb)
723 {
724         u8 *reason = sb->s_stop_reason;
725         u8 *errors = sb->s_errors;
726         int i;
727
728         for (i = 0; i < STOP_CP_REASON_MAX; i++) {
729                 if (!reason[i])
730                         continue;
731                 if (c.layout)
732                         printf("%-30s %s(%s, %d)\n", "", "stop_reason",
733                                 stop_reason_str[i], reason[i]);
734                 else
735                         printf("%-30s\t\t[%-20s : %u]\n", "",
736                                 stop_reason_str[i], reason[i]);
737         }
738
739         for (i = 0; i < ERROR_MAX; i++) {
740                 if (!test_bit_le(i, errors))
741                         continue;
742                 if (c.layout)
743                         printf("%-30s %s(%s)\n", "", "errors", errors_str[i]);
744                 else
745                         printf("%-30s\t\t[%-20s]\n", "", errors_str[i]);
746         }
747 }
748
749 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
750                                         block_t blkaddr, int type)
751 {
752         switch (type) {
753         case META_NAT:
754                 break;
755         case META_SIT:
756                 if (blkaddr >= SIT_BLK_CNT(sbi))
757                         return 0;
758                 break;
759         case META_SSA:
760                 if (blkaddr >= MAIN_BLKADDR(sbi) ||
761                         blkaddr < SM_I(sbi)->ssa_blkaddr)
762                         return 0;
763                 break;
764         case META_CP:
765                 if (blkaddr >= SIT_I(sbi)->sit_base_addr ||
766                         blkaddr < __start_cp_addr(sbi))
767                         return 0;
768                 break;
769         case META_POR:
770         case DATA_GENERIC:
771                 if (blkaddr >= MAX_BLKADDR(sbi) ||
772                         blkaddr < MAIN_BLKADDR(sbi))
773                         return 0;
774                 break;
775         default:
776                 ASSERT(0);
777         }
778
779         return 1;
780 }
781
782 static inline block_t current_sit_addr(struct f2fs_sb_info *sbi,
783                                                 unsigned int start);
784
785 /*
786  * Readahead CP/NAT/SIT/SSA pages
787  */
788 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
789                                                         int type)
790 {
791         block_t blkno = start;
792         block_t blkaddr, start_blk = 0, len = 0;
793
794         for (; nrpages-- > 0; blkno++) {
795
796                 if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
797                         goto out;
798
799                 switch (type) {
800                 case META_NAT:
801                         if (blkno >= NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))
802                                 blkno = 0;
803                         /* get nat block addr */
804                         blkaddr = current_nat_addr(sbi,
805                                         blkno * NAT_ENTRY_PER_BLOCK, NULL);
806                         break;
807                 case META_SIT:
808                         /* get sit block addr */
809                         blkaddr = current_sit_addr(sbi,
810                                         blkno * SIT_ENTRY_PER_BLOCK);
811                         break;
812                 case META_SSA:
813                 case META_CP:
814                 case META_POR:
815                         blkaddr = blkno;
816                         break;
817                 default:
818                         ASSERT(0);
819                 }
820
821                 if (!len) {
822                         start_blk = blkaddr;
823                         len = 1;
824                 } else if (start_blk + len == blkaddr) {
825                         len++;
826                 } else {
827                         dev_readahead(start_blk << F2FS_BLKSIZE_BITS,
828                                                 len << F2FS_BLKSIZE_BITS);
829                 }
830         }
831 out:
832         if (len)
833                 dev_readahead(start_blk << F2FS_BLKSIZE_BITS,
834                                         len << F2FS_BLKSIZE_BITS);
835         return blkno - start;
836 }
837
838 void update_superblock(struct f2fs_super_block *sb, int sb_mask)
839 {
840         int addr, ret;
841         uint8_t *buf;
842         u32 old_crc, new_crc;
843
844         buf = calloc(BLOCK_SZ, 1);
845         ASSERT(buf);
846
847         if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
848                 old_crc = get_sb(crc);
849                 new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
850                                                 SB_CHKSUM_OFFSET);
851                 set_sb(crc, new_crc);
852                 MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
853                                                         old_crc, new_crc);
854         }
855
856         memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
857         for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) {
858                 if (SB_MASK(addr) & sb_mask) {
859                         ret = dev_write_block(buf, addr);
860                         ASSERT(ret >= 0);
861                 }
862         }
863
864         free(buf);
865         DBG(0, "Info: Done to update superblock\n");
866 }
867
868 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
869                                                         enum SB_ADDR sb_addr)
870 {
871         u32 segment0_blkaddr = get_sb(segment0_blkaddr);
872         u32 cp_blkaddr = get_sb(cp_blkaddr);
873         u32 sit_blkaddr = get_sb(sit_blkaddr);
874         u32 nat_blkaddr = get_sb(nat_blkaddr);
875         u32 ssa_blkaddr = get_sb(ssa_blkaddr);
876         u32 main_blkaddr = get_sb(main_blkaddr);
877         u32 segment_count_ckpt = get_sb(segment_count_ckpt);
878         u32 segment_count_sit = get_sb(segment_count_sit);
879         u32 segment_count_nat = get_sb(segment_count_nat);
880         u32 segment_count_ssa = get_sb(segment_count_ssa);
881         u32 segment_count_main = get_sb(segment_count_main);
882         u32 segment_count = get_sb(segment_count);
883         u32 log_blocks_per_seg = get_sb(log_blocks_per_seg);
884         u64 main_end_blkaddr = main_blkaddr +
885                                 (segment_count_main << log_blocks_per_seg);
886         u64 seg_end_blkaddr = segment0_blkaddr +
887                                 (segment_count << log_blocks_per_seg);
888
889         if (segment0_blkaddr != cp_blkaddr) {
890                 MSG(0, "\tMismatch segment0(%u) cp_blkaddr(%u)\n",
891                                 segment0_blkaddr, cp_blkaddr);
892                 return -1;
893         }
894
895         if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
896                                                         sit_blkaddr) {
897                 MSG(0, "\tWrong CP boundary, start(%u) end(%u) blocks(%u)\n",
898                         cp_blkaddr, sit_blkaddr,
899                         segment_count_ckpt << log_blocks_per_seg);
900                 return -1;
901         }
902
903         if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
904                                                         nat_blkaddr) {
905                 MSG(0, "\tWrong SIT boundary, start(%u) end(%u) blocks(%u)\n",
906                         sit_blkaddr, nat_blkaddr,
907                         segment_count_sit << log_blocks_per_seg);
908                 return -1;
909         }
910
911         if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
912                                                         ssa_blkaddr) {
913                 MSG(0, "\tWrong NAT boundary, start(%u) end(%u) blocks(%u)\n",
914                         nat_blkaddr, ssa_blkaddr,
915                         segment_count_nat << log_blocks_per_seg);
916                 return -1;
917         }
918
919         if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
920                                                         main_blkaddr) {
921                 MSG(0, "\tWrong SSA boundary, start(%u) end(%u) blocks(%u)\n",
922                         ssa_blkaddr, main_blkaddr,
923                         segment_count_ssa << log_blocks_per_seg);
924                 return -1;
925         }
926
927         if (main_end_blkaddr > seg_end_blkaddr) {
928                 MSG(0, "\tWrong MAIN_AREA, start(%u) end(%u) block(%u)\n",
929                         main_blkaddr,
930                         segment0_blkaddr +
931                                 (segment_count << log_blocks_per_seg),
932                         segment_count_main << log_blocks_per_seg);
933                 return -1;
934         } else if (main_end_blkaddr < seg_end_blkaddr) {
935                 set_sb(segment_count, (main_end_blkaddr -
936                                 segment0_blkaddr) >> log_blocks_per_seg);
937
938                 update_superblock(sb, SB_MASK(sb_addr));
939                 MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
940                         main_blkaddr,
941                         segment0_blkaddr +
942                                 (segment_count << log_blocks_per_seg),
943                         segment_count_main << log_blocks_per_seg);
944         }
945         return 0;
946 }
947
948 static int verify_sb_chksum(struct f2fs_super_block *sb)
949 {
950         if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
951                 MSG(0, "\tInvalid SB CRC offset: %u\n",
952                                         get_sb(checksum_offset));
953                 return -1;
954         }
955         if (f2fs_crc_valid(get_sb(crc), sb,
956                         get_sb(checksum_offset))) {
957                 MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
958                 return -1;
959         }
960         return 0;
961 }
962
963 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
964 {
965         unsigned int blocksize;
966         unsigned int segment_count, segs_per_sec, secs_per_zone, segs_per_zone;
967         unsigned int total_sections, blocks_per_seg;
968
969         if (F2FS_SUPER_MAGIC != get_sb(magic)) {
970                 MSG(0, "Magic Mismatch, valid(0x%x) - read(0x%x)\n",
971                         F2FS_SUPER_MAGIC, get_sb(magic));
972                 return -1;
973         }
974
975         if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
976                                         verify_sb_chksum(sb))
977                 return -1;
978
979         blocksize = 1 << get_sb(log_blocksize);
980         if (F2FS_BLKSIZE != blocksize) {
981                 MSG(0, "Invalid blocksize (%u), supports only 4KB\n",
982                         blocksize);
983                 return -1;
984         }
985
986         /* check log blocks per segment */
987         if (get_sb(log_blocks_per_seg) != 9) {
988                 MSG(0, "Invalid log blocks per segment (%u)\n",
989                         get_sb(log_blocks_per_seg));
990                 return -1;
991         }
992
993         /* Currently, support 512/1024/2048/4096 bytes sector size */
994         if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE ||
995                         get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE) {
996                 MSG(0, "Invalid log sectorsize (%u)\n", get_sb(log_sectorsize));
997                 return -1;
998         }
999
1000         if (get_sb(log_sectors_per_block) + get_sb(log_sectorsize) !=
1001                                                 F2FS_MAX_LOG_SECTOR_SIZE) {
1002                 MSG(0, "Invalid log sectors per block(%u) log sectorsize(%u)\n",
1003                         get_sb(log_sectors_per_block),
1004                         get_sb(log_sectorsize));
1005                 return -1;
1006         }
1007
1008         segment_count = get_sb(segment_count);
1009         segs_per_sec = get_sb(segs_per_sec);
1010         secs_per_zone = get_sb(secs_per_zone);
1011         total_sections = get_sb(section_count);
1012         segs_per_zone = segs_per_sec * secs_per_zone;
1013
1014         /* blocks_per_seg should be 512, given the above check */
1015         blocks_per_seg = 1 << get_sb(log_blocks_per_seg);
1016
1017         if (segment_count > F2FS_MAX_SEGMENT ||
1018                         segment_count < F2FS_MIN_SEGMENTS) {
1019                 MSG(0, "\tInvalid segment count (%u)\n", segment_count);
1020                 return -1;
1021         }
1022
1023         if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
1024                         (total_sections > segment_count ||
1025                         total_sections < F2FS_MIN_SEGMENTS ||
1026                         segs_per_sec > segment_count || !segs_per_sec)) {
1027                 MSG(0, "\tInvalid segment/section count (%u, %u x %u)\n",
1028                         segment_count, total_sections, segs_per_sec);
1029                 return 1;
1030         }
1031
1032         if ((segment_count / segs_per_sec) < total_sections) {
1033                 MSG(0, "Small segment_count (%u < %u * %u)\n",
1034                         segment_count, segs_per_sec, total_sections);
1035                 return 1;
1036         }
1037
1038         if (segment_count > (get_sb(block_count) >> 9)) {
1039                 MSG(0, "Wrong segment_count / block_count (%u > %llu)\n",
1040                         segment_count, get_sb(block_count));
1041                 return 1;
1042         }
1043
1044         if (sb->devs[0].path[0]) {
1045                 unsigned int dev_segs = le32_to_cpu(sb->devs[0].total_segments);
1046                 int i = 1;
1047
1048                 while (i < MAX_DEVICES && sb->devs[i].path[0]) {
1049                         dev_segs += le32_to_cpu(sb->devs[i].total_segments);
1050                         i++;
1051                 }
1052                 if (segment_count != dev_segs / segs_per_zone * segs_per_zone) {
1053                         MSG(0, "Segment count (%u) mismatch with total segments from devices (%u)",
1054                                 segment_count, dev_segs);
1055                         return 1;
1056                 }
1057         }
1058
1059         if (secs_per_zone > total_sections || !secs_per_zone) {
1060                 MSG(0, "Wrong secs_per_zone / total_sections (%u, %u)\n",
1061                         secs_per_zone, total_sections);
1062                 return 1;
1063         }
1064         if (get_sb(extension_count) > F2FS_MAX_EXTENSION ||
1065                         sb->hot_ext_count > F2FS_MAX_EXTENSION ||
1066                         get_sb(extension_count) +
1067                         sb->hot_ext_count > F2FS_MAX_EXTENSION) {
1068                 MSG(0, "Corrupted extension count (%u + %u > %u)\n",
1069                         get_sb(extension_count),
1070                         sb->hot_ext_count,
1071                         F2FS_MAX_EXTENSION);
1072                 return 1;
1073         }
1074
1075         if (get_sb(cp_payload) > (blocks_per_seg - F2FS_CP_PACKS)) {
1076                 MSG(0, "Insane cp_payload (%u > %u)\n",
1077                         get_sb(cp_payload), blocks_per_seg - F2FS_CP_PACKS);
1078                 return 1;
1079         }
1080
1081         /* check reserved ino info */
1082         if (get_sb(node_ino) != 1 || get_sb(meta_ino) != 2 ||
1083                                                 get_sb(root_ino) != 3) {
1084                 MSG(0, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)\n",
1085                         get_sb(node_ino), get_sb(meta_ino), get_sb(root_ino));
1086                 return -1;
1087         }
1088
1089         /* Check zoned block device feature */
1090         if (c.devices[0].zoned_model != F2FS_ZONED_NONE &&
1091                         !(get_sb(feature) & F2FS_FEATURE_BLKZONED)) {
1092                 MSG(0, "\tMissing zoned block device feature\n");
1093                 return -1;
1094         }
1095
1096         if (sanity_check_area_boundary(sb, sb_addr))
1097                 return -1;
1098         return 0;
1099 }
1100
1101 #define CHECK_PERIOD (3600 * 24 * 30)   // one month by default
1102
1103 int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
1104 {
1105         char buf[F2FS_BLKSIZE];
1106
1107         sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
1108         if (!sbi->raw_super)
1109                 return -ENOMEM;
1110
1111         if (dev_read_block(buf, sb_addr))
1112                 return -1;
1113
1114         memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
1115                                         sizeof(struct f2fs_super_block));
1116
1117         if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
1118                 /* get kernel version */
1119                 if (c.kd >= 0) {
1120                         dev_read_version(c.version, 0, VERSION_NAME_LEN);
1121                         get_kernel_version(c.version);
1122                 } else {
1123                         get_kernel_uname_version(c.version);
1124                 }
1125
1126                 /* build sb version */
1127                 memcpy(c.sb_version, sbi->raw_super->version, VERSION_NAME_LEN);
1128                 get_kernel_version(c.sb_version);
1129                 memcpy(c.init_version, sbi->raw_super->init_version,
1130                                 VERSION_NAME_LEN);
1131                 get_kernel_version(c.init_version);
1132
1133                 c.force_stop = is_checkpoint_stop(sbi->raw_super, false);
1134                 c.abnormal_stop = is_checkpoint_stop(sbi->raw_super, true);
1135                 c.fs_errors = is_inconsistent_error(sbi->raw_super);
1136
1137                 MSG(0, "Info: MKFS version\n  \"%s\"\n", c.init_version);
1138                 MSG(0, "Info: FSCK version\n  from \"%s\"\n    to \"%s\"\n",
1139                                         c.sb_version, c.version);
1140                 print_sb_state(sbi->raw_super);
1141                 print_sb_stop_reason(sbi->raw_super);
1142                 print_sb_errors(sbi->raw_super);
1143                 return 0;
1144         }
1145
1146         free(sbi->raw_super);
1147         sbi->raw_super = NULL;
1148         MSG(0, "\tCan't find a valid F2FS superblock at 0x%x\n", sb_addr);
1149
1150         return -EINVAL;
1151 }
1152
1153 int init_sb_info(struct f2fs_sb_info *sbi)
1154 {
1155         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1156         u64 total_sectors;
1157         int i;
1158
1159         sbi->log_sectors_per_block = get_sb(log_sectors_per_block);
1160         sbi->log_blocksize = get_sb(log_blocksize);
1161         sbi->blocksize = 1 << sbi->log_blocksize;
1162         sbi->log_blocks_per_seg = get_sb(log_blocks_per_seg);
1163         sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1164         sbi->segs_per_sec = get_sb(segs_per_sec);
1165         sbi->secs_per_zone = get_sb(secs_per_zone);
1166         sbi->total_sections = get_sb(section_count);
1167         sbi->total_node_count = (get_sb(segment_count_nat) / 2) *
1168                                 sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1169         sbi->root_ino_num = get_sb(root_ino);
1170         sbi->node_ino_num = get_sb(node_ino);
1171         sbi->meta_ino_num = get_sb(meta_ino);
1172         sbi->cur_victim_sec = NULL_SEGNO;
1173
1174         for (i = 0; i < MAX_DEVICES; i++) {
1175                 if (!sb->devs[i].path[0])
1176                         break;
1177
1178                 if (i) {
1179                         c.devices[i].path = strdup((char *)sb->devs[i].path);
1180                         if (get_device_info(i))
1181                                 ASSERT(0);
1182                 } else {
1183                         ASSERT(!strcmp((char *)sb->devs[i].path,
1184                                                 (char *)c.devices[i].path));
1185                 }
1186
1187                 c.devices[i].total_segments =
1188                         le32_to_cpu(sb->devs[i].total_segments);
1189                 if (i)
1190                         c.devices[i].start_blkaddr =
1191                                 c.devices[i - 1].end_blkaddr + 1;
1192                 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
1193                         c.devices[i].total_segments *
1194                         c.blks_per_seg - 1;
1195                 if (i == 0)
1196                         c.devices[i].end_blkaddr += get_sb(segment0_blkaddr);
1197
1198                 if (c.zoned_model == F2FS_ZONED_NONE) {
1199                         if (c.devices[i].zoned_model == F2FS_ZONED_HM)
1200                                 c.zoned_model = F2FS_ZONED_HM;
1201                         else if (c.devices[i].zoned_model == F2FS_ZONED_HA &&
1202                                         c.zoned_model != F2FS_ZONED_HM)
1203                                 c.zoned_model = F2FS_ZONED_HA;
1204                 }
1205
1206                 c.ndevs = i + 1;
1207                 MSG(0, "Info: Device[%d] : %s blkaddr = %"PRIx64"--%"PRIx64"\n",
1208                                 i, c.devices[i].path,
1209                                 c.devices[i].start_blkaddr,
1210                                 c.devices[i].end_blkaddr);
1211         }
1212
1213         total_sectors = get_sb(block_count) << sbi->log_sectors_per_block;
1214         MSG(0, "Info: Segments per section = %d\n", sbi->segs_per_sec);
1215         MSG(0, "Info: Sections per zone = %d\n", sbi->secs_per_zone);
1216         MSG(0, "Info: total FS sectors = %"PRIu64" (%"PRIu64" MB)\n",
1217                                 total_sectors, total_sectors >>
1218                                                 (20 - get_sb(log_sectorsize)));
1219         return 0;
1220 }
1221
1222 static int verify_checksum_chksum(struct f2fs_checkpoint *cp)
1223 {
1224         unsigned int chksum_offset = get_cp(checksum_offset);
1225         unsigned int crc, cal_crc;
1226
1227         if (chksum_offset < CP_MIN_CHKSUM_OFFSET ||
1228                         chksum_offset > CP_CHKSUM_OFFSET) {
1229                 MSG(0, "\tInvalid CP CRC offset: %u\n", chksum_offset);
1230                 return -1;
1231         }
1232
1233         crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp + chksum_offset));
1234         cal_crc = f2fs_checkpoint_chksum(cp);
1235         if (cal_crc != crc) {
1236                 MSG(0, "\tInvalid CP CRC: offset:%u, crc:0x%x, calc:0x%x\n",
1237                         chksum_offset, crc, cal_crc);
1238                 return -1;
1239         }
1240         return 0;
1241 }
1242
1243 static void *get_checkpoint_version(block_t cp_addr)
1244 {
1245         void *cp_page;
1246
1247         cp_page = malloc(F2FS_BLKSIZE);
1248         ASSERT(cp_page);
1249
1250         if (dev_read_block(cp_page, cp_addr) < 0)
1251                 ASSERT(0);
1252
1253         if (verify_checksum_chksum((struct f2fs_checkpoint *)cp_page))
1254                 goto out;
1255         return cp_page;
1256 out:
1257         free(cp_page);
1258         return NULL;
1259 }
1260
1261 void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr,
1262                                 unsigned long long *version)
1263 {
1264         void *cp_page_1, *cp_page_2;
1265         struct f2fs_checkpoint *cp;
1266         unsigned long long cur_version = 0, pre_version = 0;
1267
1268         /* Read the 1st cp block in this CP pack */
1269         cp_page_1 = get_checkpoint_version(cp_addr);
1270         if (!cp_page_1)
1271                 return NULL;
1272
1273         cp = (struct f2fs_checkpoint *)cp_page_1;
1274         if (get_cp(cp_pack_total_block_count) > sbi->blocks_per_seg)
1275                 goto invalid_cp1;
1276
1277         pre_version = get_cp(checkpoint_ver);
1278
1279         /* Read the 2nd cp block in this CP pack */
1280         cp_addr += get_cp(cp_pack_total_block_count) - 1;
1281         cp_page_2 = get_checkpoint_version(cp_addr);
1282         if (!cp_page_2)
1283                 goto invalid_cp1;
1284
1285         cp = (struct f2fs_checkpoint *)cp_page_2;
1286         cur_version = get_cp(checkpoint_ver);
1287
1288         if (cur_version == pre_version) {
1289                 *version = cur_version;
1290                 free(cp_page_2);
1291                 return cp_page_1;
1292         }
1293
1294         free(cp_page_2);
1295 invalid_cp1:
1296         free(cp_page_1);
1297         return NULL;
1298 }
1299
1300 int get_valid_checkpoint(struct f2fs_sb_info *sbi)
1301 {
1302         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1303         void *cp1, *cp2, *cur_page;
1304         unsigned long blk_size = sbi->blocksize;
1305         unsigned long long cp1_version = 0, cp2_version = 0, version;
1306         unsigned long long cp_start_blk_no;
1307         unsigned int cp_payload, cp_blks;
1308         int ret;
1309
1310         cp_payload = get_sb(cp_payload);
1311         if (cp_payload > F2FS_BLK_ALIGN(MAX_CP_PAYLOAD))
1312                 return -EINVAL;
1313
1314         cp_blks = 1 + cp_payload;
1315         sbi->ckpt = malloc(cp_blks * blk_size);
1316         if (!sbi->ckpt)
1317                 return -ENOMEM;
1318         /*
1319          * Finding out valid cp block involves read both
1320          * sets( cp pack1 and cp pack 2)
1321          */
1322         cp_start_blk_no = get_sb(cp_blkaddr);
1323         cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
1324
1325         /* The second checkpoint pack should start at the next segment */
1326         cp_start_blk_no += 1 << get_sb(log_blocks_per_seg);
1327         cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
1328
1329         if (cp1 && cp2) {
1330                 if (ver_after(cp2_version, cp1_version)) {
1331                         cur_page = cp2;
1332                         sbi->cur_cp = 2;
1333                         version = cp2_version;
1334                 } else {
1335                         cur_page = cp1;
1336                         sbi->cur_cp = 1;
1337                         version = cp1_version;
1338                 }
1339         } else if (cp1) {
1340                 cur_page = cp1;
1341                 sbi->cur_cp = 1;
1342                 version = cp1_version;
1343         } else if (cp2) {
1344                 cur_page = cp2;
1345                 sbi->cur_cp = 2;
1346                 version = cp2_version;
1347         } else
1348                 goto fail_no_cp;
1349
1350         MSG(0, "Info: CKPT version = %llx\n", version);
1351
1352         memcpy(sbi->ckpt, cur_page, blk_size);
1353
1354         if (cp_blks > 1) {
1355                 unsigned int i;
1356                 unsigned long long cp_blk_no;
1357
1358                 cp_blk_no = get_sb(cp_blkaddr);
1359                 if (cur_page == cp2)
1360                         cp_blk_no += 1 << get_sb(log_blocks_per_seg);
1361
1362                 /* copy sit bitmap */
1363                 for (i = 1; i < cp_blks; i++) {
1364                         unsigned char *ckpt = (unsigned char *)sbi->ckpt;
1365                         ret = dev_read_block(cur_page, cp_blk_no + i);
1366                         ASSERT(ret >= 0);
1367                         memcpy(ckpt + i * blk_size, cur_page, blk_size);
1368                 }
1369         }
1370         if (cp1)
1371                 free(cp1);
1372         if (cp2)
1373                 free(cp2);
1374         return 0;
1375
1376 fail_no_cp:
1377         free(sbi->ckpt);
1378         sbi->ckpt = NULL;
1379         return -EINVAL;
1380 }
1381
1382 bool is_checkpoint_stop(struct f2fs_super_block *sb, bool abnormal)
1383 {
1384         int i;
1385
1386         for (i = 0; i < STOP_CP_REASON_MAX; i++) {
1387                 if (abnormal && i == STOP_CP_REASON_SHUTDOWN)
1388                         continue;
1389                 if (sb->s_stop_reason[i])
1390                         return true;
1391         }
1392
1393         return false;
1394 }
1395
1396 bool is_inconsistent_error(struct f2fs_super_block *sb)
1397 {
1398         int i;
1399
1400         for (i = 0; i < MAX_F2FS_ERRORS; i++) {
1401                 if (sb->s_errors[i])
1402                         return true;
1403         }
1404
1405         return false;
1406 }
1407
1408 /*
1409  * For a return value of 1, caller should further check for c.fix_on state
1410  * and take appropriate action.
1411  */
1412 static int f2fs_should_proceed(struct f2fs_super_block *sb, u32 flag)
1413 {
1414         if (!c.fix_on && (c.auto_fix || c.preen_mode)) {
1415                 if (flag & CP_FSCK_FLAG ||
1416                         flag & CP_QUOTA_NEED_FSCK_FLAG ||
1417                         c.abnormal_stop || c.fs_errors ||
1418                         (exist_qf_ino(sb) && (flag & CP_ERROR_FLAG))) {
1419                         c.fix_on = 1;
1420                 } else if (!c.preen_mode) {
1421                         print_cp_state(flag);
1422                         return 0;
1423                 }
1424         }
1425         return 1;
1426 }
1427
1428 int sanity_check_ckpt(struct f2fs_sb_info *sbi)
1429 {
1430         unsigned int total, fsmeta;
1431         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1432         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1433         unsigned int flag = get_cp(ckpt_flags);
1434         unsigned int ovp_segments, reserved_segments;
1435         unsigned int main_segs, blocks_per_seg;
1436         unsigned int sit_segs, nat_segs;
1437         unsigned int sit_bitmap_size, nat_bitmap_size;
1438         unsigned int log_blocks_per_seg;
1439         unsigned int segment_count_main;
1440         unsigned int cp_pack_start_sum, cp_payload;
1441         block_t user_block_count;
1442         int i;
1443
1444         total = get_sb(segment_count);
1445         fsmeta = get_sb(segment_count_ckpt);
1446         sit_segs = get_sb(segment_count_sit);
1447         fsmeta += sit_segs;
1448         nat_segs = get_sb(segment_count_nat);
1449         fsmeta += nat_segs;
1450         fsmeta += get_cp(rsvd_segment_count);
1451         fsmeta += get_sb(segment_count_ssa);
1452
1453         if (fsmeta >= total)
1454                 return 1;
1455
1456         ovp_segments = get_cp(overprov_segment_count);
1457         reserved_segments = get_cp(rsvd_segment_count);
1458
1459         if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
1460                 (fsmeta < F2FS_MIN_SEGMENT || ovp_segments == 0 ||
1461                                         reserved_segments == 0)) {
1462                 MSG(0, "\tWrong layout: check mkfs.f2fs version\n");
1463                 return 1;
1464         }
1465
1466         user_block_count = get_cp(user_block_count);
1467         segment_count_main = get_sb(segment_count_main) +
1468                                 ((get_sb(feature) & F2FS_FEATURE_RO) ? 1 : 0);
1469         log_blocks_per_seg = get_sb(log_blocks_per_seg);
1470         if (!user_block_count || user_block_count >=
1471                         segment_count_main << log_blocks_per_seg) {
1472                 ASSERT_MSG("\tWrong user_block_count(%u)\n", user_block_count);
1473
1474                 if (!f2fs_should_proceed(sb, flag))
1475                         return 1;
1476                 if (!c.fix_on)
1477                         return 1;
1478
1479                 if (flag & (CP_FSCK_FLAG | CP_RESIZEFS_FLAG)) {
1480                         u32 valid_user_block_cnt;
1481                         u32 seg_cnt_main = get_sb(segment_count) -
1482                                         (get_sb(segment_count_ckpt) +
1483                                          get_sb(segment_count_sit) +
1484                                          get_sb(segment_count_nat) +
1485                                          get_sb(segment_count_ssa));
1486
1487                         /* validate segment_count_main in sb first */
1488                         if (seg_cnt_main != get_sb(segment_count_main)) {
1489                                 MSG(0, "Inconsistent segment_cnt_main %u in sb\n",
1490                                                 segment_count_main << log_blocks_per_seg);
1491                                 return 1;
1492                         }
1493                         valid_user_block_cnt = ((get_sb(segment_count_main) -
1494                                                 get_cp(overprov_segment_count)) * c.blks_per_seg);
1495                         MSG(0, "Info: Fix wrong user_block_count in CP: (%u) -> (%u)\n",
1496                                         user_block_count, valid_user_block_cnt);
1497                         set_cp(user_block_count, valid_user_block_cnt);
1498                         c.bug_on = 1;
1499                 }
1500         }
1501
1502         main_segs = get_sb(segment_count_main);
1503         blocks_per_seg = sbi->blocks_per_seg;
1504
1505         for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1506                 if (get_cp(cur_node_segno[i]) >= main_segs ||
1507                         get_cp(cur_node_blkoff[i]) >= blocks_per_seg)
1508                         return 1;
1509         }
1510         for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1511                 if (get_cp(cur_data_segno[i]) >= main_segs ||
1512                         get_cp(cur_data_blkoff[i]) >= blocks_per_seg)
1513                         return 1;
1514         }
1515
1516         sit_bitmap_size = get_cp(sit_ver_bitmap_bytesize);
1517         nat_bitmap_size = get_cp(nat_ver_bitmap_bytesize);
1518
1519         if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
1520                 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
1521                 MSG(0, "\tWrong bitmap size: sit(%u), nat(%u)\n",
1522                                 sit_bitmap_size, nat_bitmap_size);
1523                 return 1;
1524         }
1525
1526         cp_pack_start_sum = __start_sum_addr(sbi);
1527         cp_payload = __cp_payload(sbi);
1528         if (cp_pack_start_sum < cp_payload + 1 ||
1529                 cp_pack_start_sum > blocks_per_seg - 1 -
1530                         NR_CURSEG_TYPE) {
1531                 MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
1532                         cp_pack_start_sum, cp_payload);
1533                 if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
1534                         return 1;
1535                 set_sb(cp_payload, cp_pack_start_sum - 1);
1536                 update_superblock(sb, SB_MASK_ALL);
1537         }
1538
1539         return 0;
1540 }
1541
1542 pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start, int *pack)
1543 {
1544         struct f2fs_nm_info *nm_i = NM_I(sbi);
1545         pgoff_t block_off;
1546         pgoff_t block_addr;
1547         int seg_off;
1548
1549         block_off = NAT_BLOCK_OFFSET(start);
1550         seg_off = block_off >> sbi->log_blocks_per_seg;
1551
1552         block_addr = (pgoff_t)(nm_i->nat_blkaddr +
1553                         (seg_off << sbi->log_blocks_per_seg << 1) +
1554                         (block_off & ((1 << sbi->log_blocks_per_seg) -1)));
1555         if (pack)
1556                 *pack = 1;
1557
1558         if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) {
1559                 block_addr += sbi->blocks_per_seg;
1560                 if (pack)
1561                         *pack = 2;
1562         }
1563
1564         return block_addr;
1565 }
1566
1567 /* will not init nid_bitmap from nat */
1568 static int f2fs_early_init_nid_bitmap(struct f2fs_sb_info *sbi)
1569 {
1570         struct f2fs_nm_info *nm_i = NM_I(sbi);
1571         int nid_bitmap_size = (nm_i->max_nid + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
1572         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1573         struct f2fs_summary_block *sum = curseg->sum_blk;
1574         struct f2fs_journal *journal = &sum->journal;
1575         nid_t nid;
1576         int i;
1577
1578         if (!(c.func == SLOAD || c.func == FSCK))
1579                 return 0;
1580
1581         nm_i->nid_bitmap = (char *)calloc(nid_bitmap_size, 1);
1582         if (!nm_i->nid_bitmap)
1583                 return -ENOMEM;
1584
1585         /* arbitrarily set 0 bit */
1586         f2fs_set_bit(0, nm_i->nid_bitmap);
1587
1588         if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
1589                 MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
1590                         "NAT_JOURNAL_ENTRIES(%zu)\n",
1591                         nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
1592                 journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
1593                 c.fix_on = 1;
1594         }
1595
1596         for (i = 0; i < nats_in_cursum(journal); i++) {
1597                 block_t addr;
1598
1599                 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
1600                 if (addr != NULL_ADDR &&
1601                         !f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC)) {
1602                         MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
1603                         journal->n_nats = cpu_to_le16(i);
1604                         c.fix_on = 1;
1605                         continue;
1606                 }
1607
1608                 nid = le32_to_cpu(nid_in_journal(journal, i));
1609                 if (!IS_VALID_NID(sbi, nid)) {
1610                         MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
1611                         journal->n_nats = cpu_to_le16(i);
1612                         c.fix_on = 1;
1613                         continue;
1614                 }
1615                 if (addr != NULL_ADDR)
1616                         f2fs_set_bit(nid, nm_i->nid_bitmap);
1617         }
1618         return 0;
1619 }
1620
1621 /* will init nid_bitmap from nat */
1622 static int f2fs_late_init_nid_bitmap(struct f2fs_sb_info *sbi)
1623 {
1624         struct f2fs_nm_info *nm_i = NM_I(sbi);
1625         struct f2fs_nat_block *nat_block;
1626         block_t start_blk;
1627         nid_t nid;
1628
1629         if (!(c.func == SLOAD || c.func == FSCK))
1630                 return 0;
1631
1632         nat_block = malloc(F2FS_BLKSIZE);
1633         if (!nat_block) {
1634                 free(nm_i->nid_bitmap);
1635                 return -ENOMEM;
1636         }
1637
1638         f2fs_ra_meta_pages(sbi, 0, NAT_BLOCK_OFFSET(nm_i->max_nid),
1639                                                         META_NAT);
1640         for (nid = 0; nid < nm_i->max_nid; nid++) {
1641                 if (!(nid % NAT_ENTRY_PER_BLOCK)) {
1642                         int ret;
1643
1644                         start_blk = current_nat_addr(sbi, nid, NULL);
1645                         ret = dev_read_block(nat_block, start_blk);
1646                         ASSERT(ret >= 0);
1647                 }
1648
1649                 if (nat_block->entries[nid % NAT_ENTRY_PER_BLOCK].block_addr)
1650                         f2fs_set_bit(nid, nm_i->nid_bitmap);
1651         }
1652
1653         free(nat_block);
1654         return 0;
1655 }
1656
1657 u32 update_nat_bits_flags(struct f2fs_super_block *sb,
1658                                 struct f2fs_checkpoint *cp, u32 flags)
1659 {
1660         uint32_t nat_bits_bytes, nat_bits_blocks;
1661
1662         nat_bits_bytes = get_sb(segment_count_nat) << 5;
1663         nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
1664                                                 F2FS_BLKSIZE - 1);
1665         if (get_cp(cp_pack_total_block_count) <=
1666                         (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
1667                 flags |= CP_NAT_BITS_FLAG;
1668         else
1669                 flags &= (~CP_NAT_BITS_FLAG);
1670
1671         return flags;
1672 }
1673
1674 /* should call flush_journal_entries() bfore this */
1675 void write_nat_bits(struct f2fs_sb_info *sbi,
1676         struct f2fs_super_block *sb, struct f2fs_checkpoint *cp, int set)
1677 {
1678         struct f2fs_nm_info *nm_i = NM_I(sbi);
1679         uint32_t nat_blocks = get_sb(segment_count_nat) <<
1680                                 (get_sb(log_blocks_per_seg) - 1);
1681         uint32_t nat_bits_bytes = nat_blocks >> 3;
1682         uint32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1683                                         8 + F2FS_BLKSIZE - 1);
1684         unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1685         struct f2fs_nat_block *nat_block;
1686         uint32_t i, j;
1687         block_t blkaddr;
1688         int ret;
1689
1690         nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1691         ASSERT(nat_bits);
1692
1693         nat_block = malloc(F2FS_BLKSIZE);
1694         ASSERT(nat_block);
1695
1696         full_nat_bits = nat_bits + 8;
1697         empty_nat_bits = full_nat_bits + nat_bits_bytes;
1698
1699         memset(full_nat_bits, 0, nat_bits_bytes);
1700         memset(empty_nat_bits, 0, nat_bits_bytes);
1701
1702         for (i = 0; i < nat_blocks; i++) {
1703                 int seg_off = i >> get_sb(log_blocks_per_seg);
1704                 int valid = 0;
1705
1706                 blkaddr = (pgoff_t)(get_sb(nat_blkaddr) +
1707                                 (seg_off << get_sb(log_blocks_per_seg) << 1) +
1708                                 (i & ((1 << get_sb(log_blocks_per_seg)) - 1)));
1709
1710                 /*
1711                  * Should consider new nat_blocks is larger than old
1712                  * nm_i->nat_blocks, since nm_i->nat_bitmap is based on
1713                  * old one.
1714                  */
1715                 if (i < nm_i->nat_blocks && f2fs_test_bit(i, nm_i->nat_bitmap))
1716                         blkaddr += (1 << get_sb(log_blocks_per_seg));
1717
1718                 ret = dev_read_block(nat_block, blkaddr);
1719                 ASSERT(ret >= 0);
1720
1721                 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1722                         if ((i == 0 && j == 0) ||
1723                                 nat_block->entries[j].block_addr != NULL_ADDR)
1724                                 valid++;
1725                 }
1726                 if (valid == 0)
1727                         test_and_set_bit_le(i, empty_nat_bits);
1728                 else if (valid == NAT_ENTRY_PER_BLOCK)
1729                         test_and_set_bit_le(i, full_nat_bits);
1730         }
1731         *(__le64 *)nat_bits = get_cp_crc(cp);
1732         free(nat_block);
1733
1734         blkaddr = get_sb(segment0_blkaddr) + (set <<
1735                                 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1736
1737         DBG(1, "\tWriting NAT bits pages, at offset 0x%08x\n", blkaddr);
1738
1739         for (i = 0; i < nat_bits_blocks; i++) {
1740                 if (dev_write_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i))
1741                         ASSERT_MSG("\tError: write NAT bits to disk!!!\n");
1742         }
1743         MSG(0, "Info: Write valid nat_bits in checkpoint\n");
1744
1745         free(nat_bits);
1746 }
1747
1748 static int check_nat_bits(struct f2fs_sb_info *sbi,
1749         struct f2fs_super_block *sb, struct f2fs_checkpoint *cp)
1750 {
1751         struct f2fs_nm_info *nm_i = NM_I(sbi);
1752         uint32_t nat_blocks = get_sb(segment_count_nat) <<
1753                                 (get_sb(log_blocks_per_seg) - 1);
1754         uint32_t nat_bits_bytes = nat_blocks >> 3;
1755         uint32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1756                                         8 + F2FS_BLKSIZE - 1);
1757         unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1758         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1759         struct f2fs_journal *journal = &curseg->sum_blk->journal;
1760         uint32_t i, j;
1761         block_t blkaddr;
1762         int err = 0;
1763
1764         nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1765         ASSERT(nat_bits);
1766
1767         full_nat_bits = nat_bits + 8;
1768         empty_nat_bits = full_nat_bits + nat_bits_bytes;
1769
1770         blkaddr = get_sb(segment0_blkaddr) + (sbi->cur_cp <<
1771                                 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1772
1773         for (i = 0; i < nat_bits_blocks; i++) {
1774                 if (dev_read_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i))
1775                         ASSERT_MSG("\tError: read NAT bits to disk!!!\n");
1776         }
1777
1778         if (*(__le64 *)nat_bits != get_cp_crc(cp) || nats_in_cursum(journal)) {
1779                 /*
1780                  * if there is a journal, f2fs was not shutdown cleanly. Let's
1781                  * flush them with nat_bits.
1782                  */
1783                 if (c.fix_on)
1784                         err = -1;
1785                 /* Otherwise, kernel will disable nat_bits */
1786                 goto out;
1787         }
1788
1789         for (i = 0; i < nat_blocks; i++) {
1790                 uint32_t start_nid = i * NAT_ENTRY_PER_BLOCK;
1791                 uint32_t valid = 0;
1792                 int empty = test_bit_le(i, empty_nat_bits);
1793                 int full = test_bit_le(i, full_nat_bits);
1794
1795                 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1796                         if (f2fs_test_bit(start_nid + j, nm_i->nid_bitmap))
1797                                 valid++;
1798                 }
1799                 if (valid == 0) {
1800                         if (!empty || full) {
1801                                 err = -1;
1802                                 goto out;
1803                         }
1804                 } else if (valid == NAT_ENTRY_PER_BLOCK) {
1805                         if (empty || !full) {
1806                                 err = -1;
1807                                 goto out;
1808                         }
1809                 } else {
1810                         if (empty || full) {
1811                                 err = -1;
1812                                 goto out;
1813                         }
1814                 }
1815         }
1816 out:
1817         free(nat_bits);
1818         if (!err) {
1819                 MSG(0, "Info: Checked valid nat_bits in checkpoint\n");
1820         } else {
1821                 c.bug_nat_bits = 1;
1822                 MSG(0, "Info: Corrupted valid nat_bits in checkpoint\n");
1823         }
1824         return err;
1825 }
1826
1827 int init_node_manager(struct f2fs_sb_info *sbi)
1828 {
1829         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1830         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1831         struct f2fs_nm_info *nm_i = NM_I(sbi);
1832         unsigned char *version_bitmap;
1833         unsigned int nat_segs;
1834
1835         nm_i->nat_blkaddr = get_sb(nat_blkaddr);
1836
1837         /* segment_count_nat includes pair segment so divide to 2. */
1838         nat_segs = get_sb(segment_count_nat) >> 1;
1839         nm_i->nat_blocks = nat_segs << get_sb(log_blocks_per_seg);
1840         nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
1841         nm_i->fcnt = 0;
1842         nm_i->nat_cnt = 0;
1843         nm_i->init_scan_nid = get_cp(next_free_nid);
1844         nm_i->next_scan_nid = get_cp(next_free_nid);
1845
1846         nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1847
1848         nm_i->nat_bitmap = malloc(nm_i->bitmap_size);
1849         if (!nm_i->nat_bitmap)
1850                 return -ENOMEM;
1851         version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1852         if (!version_bitmap)
1853                 return -EFAULT;
1854
1855         /* copy version bitmap */
1856         memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size);
1857         return f2fs_early_init_nid_bitmap(sbi);
1858 }
1859
1860 int build_node_manager(struct f2fs_sb_info *sbi)
1861 {
1862         int err;
1863         sbi->nm_info = malloc(sizeof(struct f2fs_nm_info));
1864         if (!sbi->nm_info)
1865                 return -ENOMEM;
1866
1867         err = init_node_manager(sbi);
1868         if (err)
1869                 return err;
1870
1871         return 0;
1872 }
1873
1874 int build_sit_info(struct f2fs_sb_info *sbi)
1875 {
1876         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1877         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1878         struct sit_info *sit_i;
1879         unsigned int sit_segs;
1880         int start;
1881         char *src_bitmap, *dst_bitmap;
1882         unsigned char *bitmap;
1883         unsigned int bitmap_size;
1884
1885         sit_i = malloc(sizeof(struct sit_info));
1886         if (!sit_i) {
1887                 MSG(1, "\tError: Malloc failed for build_sit_info!\n");
1888                 return -ENOMEM;
1889         }
1890
1891         SM_I(sbi)->sit_info = sit_i;
1892
1893         sit_i->sentries = calloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry), 1);
1894         if (!sit_i->sentries) {
1895                 MSG(1, "\tError: Calloc failed for build_sit_info!\n");
1896                 goto free_sit_info;
1897         }
1898
1899         bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE;
1900
1901         if (need_fsync_data_record(sbi))
1902                 bitmap_size += bitmap_size;
1903
1904         sit_i->bitmap = calloc(bitmap_size, 1);
1905         if (!sit_i->bitmap) {
1906                 MSG(1, "\tError: Calloc failed for build_sit_info!!\n");
1907                 goto free_sentries;
1908         }
1909
1910         bitmap = sit_i->bitmap;
1911
1912         for (start = 0; start < MAIN_SEGS(sbi); start++) {
1913                 sit_i->sentries[start].cur_valid_map = bitmap;
1914                 bitmap += SIT_VBLOCK_MAP_SIZE;
1915
1916                 if (need_fsync_data_record(sbi)) {
1917                         sit_i->sentries[start].ckpt_valid_map = bitmap;
1918                         bitmap += SIT_VBLOCK_MAP_SIZE;
1919                 }
1920         }
1921
1922         sit_segs = get_sb(segment_count_sit) >> 1;
1923         bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1924         src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1925
1926         dst_bitmap = malloc(bitmap_size);
1927         if (!dst_bitmap) {
1928                 MSG(1, "\tError: Malloc failed for build_sit_info!!\n");
1929                 goto free_validity_maps;
1930         }
1931
1932         memcpy(dst_bitmap, src_bitmap, bitmap_size);
1933
1934         sit_i->sit_base_addr = get_sb(sit_blkaddr);
1935         sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1936         sit_i->written_valid_blocks = get_cp(valid_block_count);
1937         sit_i->sit_bitmap = dst_bitmap;
1938         sit_i->bitmap_size = bitmap_size;
1939         sit_i->dirty_sentries = 0;
1940         sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1941         sit_i->elapsed_time = get_cp(elapsed_time);
1942         return 0;
1943
1944 free_validity_maps:
1945         free(sit_i->bitmap);
1946 free_sentries:
1947         free(sit_i->sentries);
1948 free_sit_info:
1949         free(sit_i);
1950
1951         return -ENOMEM;
1952 }
1953
1954 void reset_curseg(struct f2fs_sb_info *sbi, int type)
1955 {
1956         struct curseg_info *curseg = CURSEG_I(sbi, type);
1957         struct summary_footer *sum_footer;
1958         struct seg_entry *se;
1959
1960         sum_footer = &(curseg->sum_blk->footer);
1961         memset(sum_footer, 0, sizeof(struct summary_footer));
1962         if (IS_DATASEG(type))
1963                 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1964         if (IS_NODESEG(type))
1965                 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1966         se = get_seg_entry(sbi, curseg->segno);
1967         se->type = type;
1968         se->dirty = 1;
1969 }
1970
1971 static void read_compacted_summaries(struct f2fs_sb_info *sbi)
1972 {
1973         struct curseg_info *curseg;
1974         unsigned int i, j, offset;
1975         block_t start;
1976         char *kaddr;
1977         int ret;
1978
1979         start = start_sum_block(sbi);
1980
1981         kaddr = malloc(F2FS_BLKSIZE);
1982         ASSERT(kaddr);
1983
1984         ret = dev_read_block(kaddr, start++);
1985         ASSERT(ret >= 0);
1986
1987         curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1988         memcpy(&curseg->sum_blk->journal.n_nats, kaddr, SUM_JOURNAL_SIZE);
1989
1990         curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1991         memcpy(&curseg->sum_blk->journal.n_sits, kaddr + SUM_JOURNAL_SIZE,
1992                                                 SUM_JOURNAL_SIZE);
1993
1994         offset = 2 * SUM_JOURNAL_SIZE;
1995         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1996                 unsigned short blk_off;
1997                 struct curseg_info *curseg = CURSEG_I(sbi, i);
1998
1999                 reset_curseg(sbi, i);
2000
2001                 if (curseg->alloc_type == SSR)
2002                         blk_off = sbi->blocks_per_seg;
2003                 else
2004                         blk_off = curseg->next_blkoff;
2005
2006                 ASSERT(blk_off <= ENTRIES_IN_SUM);
2007
2008                 for (j = 0; j < blk_off; j++) {
2009                         struct f2fs_summary *s;
2010                         s = (struct f2fs_summary *)(kaddr + offset);
2011                         curseg->sum_blk->entries[j] = *s;
2012                         offset += SUMMARY_SIZE;
2013                         if (offset + SUMMARY_SIZE <=
2014                                         F2FS_BLKSIZE - SUM_FOOTER_SIZE)
2015                                 continue;
2016                         memset(kaddr, 0, F2FS_BLKSIZE);
2017                         ret = dev_read_block(kaddr, start++);
2018                         ASSERT(ret >= 0);
2019                         offset = 0;
2020                 }
2021         }
2022         free(kaddr);
2023 }
2024
2025 static void restore_node_summary(struct f2fs_sb_info *sbi,
2026                 unsigned int segno, struct f2fs_summary_block *sum_blk)
2027 {
2028         struct f2fs_node *node_blk;
2029         struct f2fs_summary *sum_entry;
2030         block_t addr;
2031         unsigned int i;
2032         int ret;
2033
2034         node_blk = malloc(F2FS_BLKSIZE);
2035         ASSERT(node_blk);
2036
2037         /* scan the node segment */
2038         addr = START_BLOCK(sbi, segno);
2039         sum_entry = &sum_blk->entries[0];
2040
2041         for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) {
2042                 ret = dev_read_block(node_blk, addr);
2043                 ASSERT(ret >= 0);
2044                 sum_entry->nid = node_blk->footer.nid;
2045                 addr++;
2046         }
2047         free(node_blk);
2048 }
2049
2050 static void read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2051 {
2052         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2053         struct f2fs_summary_block *sum_blk;
2054         struct curseg_info *curseg;
2055         unsigned int segno = 0;
2056         block_t blk_addr = 0;
2057         int ret;
2058
2059         if (IS_DATASEG(type)) {
2060                 segno = get_cp(cur_data_segno[type]);
2061                 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2062                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2063                 else
2064                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2065         } else {
2066                 segno = get_cp(cur_node_segno[type - CURSEG_HOT_NODE]);
2067                 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2068                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2069                                                         type - CURSEG_HOT_NODE);
2070                 else
2071                         blk_addr = GET_SUM_BLKADDR(sbi, segno);
2072         }
2073
2074         sum_blk = malloc(sizeof(*sum_blk));
2075         ASSERT(sum_blk);
2076
2077         ret = dev_read_block(sum_blk, blk_addr);
2078         ASSERT(ret >= 0);
2079
2080         if (IS_NODESEG(type) && !is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2081                 restore_node_summary(sbi, segno, sum_blk);
2082
2083         curseg = CURSEG_I(sbi, type);
2084         memcpy(curseg->sum_blk, sum_blk, sizeof(*sum_blk));
2085         reset_curseg(sbi, type);
2086         free(sum_blk);
2087 }
2088
2089 void update_sum_entry(struct f2fs_sb_info *sbi, block_t blk_addr,
2090                                         struct f2fs_summary *sum)
2091 {
2092         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2093         struct f2fs_summary_block *sum_blk;
2094         u32 segno, offset;
2095         int type, ret;
2096         struct seg_entry *se;
2097
2098         if (get_sb(feature) & F2FS_FEATURE_RO)
2099                 return;
2100
2101         segno = GET_SEGNO(sbi, blk_addr);
2102         offset = OFFSET_IN_SEG(sbi, blk_addr);
2103
2104         se = get_seg_entry(sbi, segno);
2105
2106         sum_blk = get_sum_block(sbi, segno, &type);
2107         memcpy(&sum_blk->entries[offset], sum, sizeof(*sum));
2108         sum_blk->footer.entry_type = IS_NODESEG(se->type) ? SUM_TYPE_NODE :
2109                                                         SUM_TYPE_DATA;
2110
2111         /* write SSA all the time */
2112         ret = dev_write_block(sum_blk, GET_SUM_BLKADDR(sbi, segno));
2113         ASSERT(ret >= 0);
2114
2115         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
2116                                         type == SEG_TYPE_MAX)
2117                 free(sum_blk);
2118 }
2119
2120 static void restore_curseg_summaries(struct f2fs_sb_info *sbi)
2121 {
2122         int type = CURSEG_HOT_DATA;
2123
2124         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
2125                 read_compacted_summaries(sbi);
2126                 type = CURSEG_HOT_NODE;
2127         }
2128
2129         for (; type <= CURSEG_COLD_NODE; type++)
2130                 read_normal_summaries(sbi, type);
2131 }
2132
2133 static int build_curseg(struct f2fs_sb_info *sbi)
2134 {
2135         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2136         struct curseg_info *array;
2137         unsigned short blk_off;
2138         unsigned int segno;
2139         int i;
2140
2141         array = malloc(sizeof(*array) * NR_CURSEG_TYPE);
2142         if (!array) {
2143                 MSG(1, "\tError: Malloc failed for build_curseg!\n");
2144                 return -ENOMEM;
2145         }
2146
2147         SM_I(sbi)->curseg_array = array;
2148
2149         for (i = 0; i < NR_CURSEG_TYPE; i++) {
2150                 array[i].sum_blk = calloc(sizeof(*(array[i].sum_blk)), 1);
2151                 if (!array[i].sum_blk) {
2152                         MSG(1, "\tError: Calloc failed for build_curseg!!\n");
2153                         goto seg_cleanup;
2154                 }
2155
2156                 if (i <= CURSEG_COLD_DATA) {
2157                         blk_off = get_cp(cur_data_blkoff[i]);
2158                         segno = get_cp(cur_data_segno[i]);
2159                 }
2160                 if (i > CURSEG_COLD_DATA) {
2161                         blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]);
2162                         segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]);
2163                 }
2164                 ASSERT(segno < MAIN_SEGS(sbi));
2165                 ASSERT(blk_off < DEFAULT_BLOCKS_PER_SEGMENT);
2166
2167                 array[i].segno = segno;
2168                 array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno);
2169                 array[i].next_segno = NULL_SEGNO;
2170                 array[i].next_blkoff = blk_off;
2171                 array[i].alloc_type = cp->alloc_type[i];
2172         }
2173         restore_curseg_summaries(sbi);
2174         return 0;
2175
2176 seg_cleanup:
2177         for(--i ; i >=0; --i)
2178                 free(array[i].sum_blk);
2179         free(array);
2180
2181         return -ENOMEM;
2182 }
2183
2184 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
2185 {
2186         unsigned int end_segno = SM_I(sbi)->segment_count - 1;
2187         ASSERT(segno <= end_segno);
2188 }
2189
2190 static inline block_t current_sit_addr(struct f2fs_sb_info *sbi,
2191                                                 unsigned int segno)
2192 {
2193         struct sit_info *sit_i = SIT_I(sbi);
2194         unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
2195         block_t blk_addr = sit_i->sit_base_addr + offset;
2196
2197         check_seg_range(sbi, segno);
2198
2199         /* calculate sit block address */
2200         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
2201                 blk_addr += sit_i->sit_blocks;
2202
2203         return blk_addr;
2204 }
2205
2206 void get_current_sit_page(struct f2fs_sb_info *sbi,
2207                         unsigned int segno, struct f2fs_sit_block *sit_blk)
2208 {
2209         block_t blk_addr = current_sit_addr(sbi, segno);
2210
2211         ASSERT(dev_read_block(sit_blk, blk_addr) >= 0);
2212 }
2213
2214 void rewrite_current_sit_page(struct f2fs_sb_info *sbi,
2215                         unsigned int segno, struct f2fs_sit_block *sit_blk)
2216 {
2217         block_t blk_addr = current_sit_addr(sbi, segno);
2218
2219         ASSERT(dev_write_block(sit_blk, blk_addr) >= 0);
2220 }
2221
2222 void check_block_count(struct f2fs_sb_info *sbi,
2223                 unsigned int segno, struct f2fs_sit_entry *raw_sit)
2224 {
2225         struct f2fs_sm_info *sm_info = SM_I(sbi);
2226         unsigned int end_segno = sm_info->segment_count - 1;
2227         int valid_blocks = 0;
2228         unsigned int i;
2229
2230         /* check segment usage */
2231         if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg)
2232                 ASSERT_MSG("Invalid SIT vblocks: segno=0x%x, %u",
2233                                 segno, GET_SIT_VBLOCKS(raw_sit));
2234
2235         /* check boundary of a given segment number */
2236         if (segno > end_segno)
2237                 ASSERT_MSG("Invalid SEGNO: 0x%x", segno);
2238
2239         /* check bitmap with valid block count */
2240         for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
2241                 valid_blocks += get_bits_in_byte(raw_sit->valid_map[i]);
2242
2243         if (GET_SIT_VBLOCKS(raw_sit) != valid_blocks)
2244                 ASSERT_MSG("Wrong SIT valid blocks: segno=0x%x, %u vs. %u",
2245                                 segno, GET_SIT_VBLOCKS(raw_sit), valid_blocks);
2246
2247         if (GET_SIT_TYPE(raw_sit) >= NO_CHECK_TYPE)
2248                 ASSERT_MSG("Wrong SIT type: segno=0x%x, %u",
2249                                 segno, GET_SIT_TYPE(raw_sit));
2250 }
2251
2252 void __seg_info_from_raw_sit(struct seg_entry *se,
2253                 struct f2fs_sit_entry *raw_sit)
2254 {
2255         se->valid_blocks = GET_SIT_VBLOCKS(raw_sit);
2256         memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE);
2257         se->type = GET_SIT_TYPE(raw_sit);
2258         se->orig_type = GET_SIT_TYPE(raw_sit);
2259         se->mtime = le64_to_cpu(raw_sit->mtime);
2260 }
2261
2262 void seg_info_from_raw_sit(struct f2fs_sb_info *sbi, struct seg_entry *se,
2263                                                 struct f2fs_sit_entry *raw_sit)
2264 {
2265         __seg_info_from_raw_sit(se, raw_sit);
2266
2267         if (!need_fsync_data_record(sbi))
2268                 return;
2269         se->ckpt_valid_blocks = se->valid_blocks;
2270         memcpy(se->ckpt_valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2271         se->ckpt_type = se->type;
2272 }
2273
2274 struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
2275                 unsigned int segno)
2276 {
2277         struct sit_info *sit_i = SIT_I(sbi);
2278         return &sit_i->sentries[segno];
2279 }
2280
2281 unsigned short get_seg_vblocks(struct f2fs_sb_info *sbi, struct seg_entry *se)
2282 {
2283         if (!need_fsync_data_record(sbi))
2284                 return se->valid_blocks;
2285         else
2286                 return se->ckpt_valid_blocks;
2287 }
2288
2289 unsigned char *get_seg_bitmap(struct f2fs_sb_info *sbi, struct seg_entry *se)
2290 {
2291         if (!need_fsync_data_record(sbi))
2292                 return se->cur_valid_map;
2293         else
2294                 return se->ckpt_valid_map;
2295 }
2296
2297 unsigned char get_seg_type(struct f2fs_sb_info *sbi, struct seg_entry *se)
2298 {
2299         if (!need_fsync_data_record(sbi))
2300                 return se->type;
2301         else
2302                 return se->ckpt_type;
2303 }
2304
2305 struct f2fs_summary_block *get_sum_block(struct f2fs_sb_info *sbi,
2306                                 unsigned int segno, int *ret_type)
2307 {
2308         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2309         struct f2fs_summary_block *sum_blk;
2310         struct curseg_info *curseg;
2311         int type, ret;
2312         u64 ssa_blk;
2313
2314         *ret_type= SEG_TYPE_MAX;
2315
2316         ssa_blk = GET_SUM_BLKADDR(sbi, segno);
2317         for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) {
2318                 if (segno == get_cp(cur_node_segno[type])) {
2319                         curseg = CURSEG_I(sbi, CURSEG_HOT_NODE + type);
2320                         if (!IS_SUM_NODE_SEG(curseg->sum_blk->footer)) {
2321                                 ASSERT_MSG("segno [0x%x] indicates a data "
2322                                                 "segment, but should be node",
2323                                                 segno);
2324                                 *ret_type = -SEG_TYPE_CUR_NODE;
2325                         } else {
2326                                 *ret_type = SEG_TYPE_CUR_NODE;
2327                         }
2328                         return curseg->sum_blk;
2329                 }
2330         }
2331
2332         for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) {
2333                 if (segno == get_cp(cur_data_segno[type])) {
2334                         curseg = CURSEG_I(sbi, type);
2335                         if (IS_SUM_NODE_SEG(curseg->sum_blk->footer)) {
2336                                 ASSERT_MSG("segno [0x%x] indicates a node "
2337                                                 "segment, but should be data",
2338                                                 segno);
2339                                 *ret_type = -SEG_TYPE_CUR_DATA;
2340                         } else {
2341                                 *ret_type = SEG_TYPE_CUR_DATA;
2342                         }
2343                         return curseg->sum_blk;
2344                 }
2345         }
2346
2347         sum_blk = calloc(BLOCK_SZ, 1);
2348         ASSERT(sum_blk);
2349
2350         ret = dev_read_block(sum_blk, ssa_blk);
2351         ASSERT(ret >= 0);
2352
2353         if (IS_SUM_NODE_SEG(sum_blk->footer))
2354                 *ret_type = SEG_TYPE_NODE;
2355         else if (IS_SUM_DATA_SEG(sum_blk->footer))
2356                 *ret_type = SEG_TYPE_DATA;
2357
2358         return sum_blk;
2359 }
2360
2361 int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr,
2362                                 struct f2fs_summary *sum_entry)
2363 {
2364         struct f2fs_summary_block *sum_blk;
2365         u32 segno, offset;
2366         int type;
2367
2368         segno = GET_SEGNO(sbi, blk_addr);
2369         offset = OFFSET_IN_SEG(sbi, blk_addr);
2370
2371         sum_blk = get_sum_block(sbi, segno, &type);
2372         memcpy(sum_entry, &(sum_blk->entries[offset]),
2373                                 sizeof(struct f2fs_summary));
2374         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
2375                                         type == SEG_TYPE_MAX)
2376                 free(sum_blk);
2377         return type;
2378 }
2379
2380 static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
2381                                 struct f2fs_nat_entry *raw_nat)
2382 {
2383         struct f2fs_nat_block *nat_block;
2384         pgoff_t block_addr;
2385         int entry_off;
2386         int ret;
2387
2388         if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0)
2389                 return;
2390
2391         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2392         ASSERT(nat_block);
2393
2394         entry_off = nid % NAT_ENTRY_PER_BLOCK;
2395         block_addr = current_nat_addr(sbi, nid, NULL);
2396
2397         ret = dev_read_block(nat_block, block_addr);
2398         ASSERT(ret >= 0);
2399
2400         memcpy(raw_nat, &nat_block->entries[entry_off],
2401                                         sizeof(struct f2fs_nat_entry));
2402         free(nat_block);
2403 }
2404
2405 void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid,
2406                                 u16 ofs_in_node, block_t newaddr)
2407 {
2408         struct f2fs_node *node_blk = NULL;
2409         struct node_info ni;
2410         block_t oldaddr, startaddr, endaddr;
2411         int ret;
2412
2413         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
2414         ASSERT(node_blk);
2415
2416         get_node_info(sbi, nid, &ni);
2417
2418         /* read node_block */
2419         ret = dev_read_block(node_blk, ni.blk_addr);
2420         ASSERT(ret >= 0);
2421
2422         /* check its block address */
2423         if (node_blk->footer.nid == node_blk->footer.ino) {
2424                 int ofs = get_extra_isize(node_blk);
2425
2426                 oldaddr = le32_to_cpu(node_blk->i.i_addr[ofs + ofs_in_node]);
2427                 node_blk->i.i_addr[ofs + ofs_in_node] = cpu_to_le32(newaddr);
2428                 ret = write_inode(node_blk, ni.blk_addr);
2429                 ASSERT(ret >= 0);
2430         } else {
2431                 oldaddr = le32_to_cpu(node_blk->dn.addr[ofs_in_node]);
2432                 node_blk->dn.addr[ofs_in_node] = cpu_to_le32(newaddr);
2433                 ret = dev_write_block(node_blk, ni.blk_addr);
2434                 ASSERT(ret >= 0);
2435         }
2436
2437         /* check extent cache entry */
2438         if (node_blk->footer.nid != node_blk->footer.ino) {
2439                 get_node_info(sbi, le32_to_cpu(node_blk->footer.ino), &ni);
2440
2441                 /* read inode block */
2442                 ret = dev_read_block(node_blk, ni.blk_addr);
2443                 ASSERT(ret >= 0);
2444         }
2445
2446         startaddr = le32_to_cpu(node_blk->i.i_ext.blk_addr);
2447         endaddr = startaddr + le32_to_cpu(node_blk->i.i_ext.len);
2448         if (oldaddr >= startaddr && oldaddr < endaddr) {
2449                 node_blk->i.i_ext.len = 0;
2450
2451                 /* update inode block */
2452                 ASSERT(write_inode(node_blk, ni.blk_addr) >= 0);
2453         }
2454         free(node_blk);
2455 }
2456
2457 void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,
2458                                         nid_t nid, block_t newaddr)
2459 {
2460         struct f2fs_nat_block *nat_block;
2461         pgoff_t block_addr;
2462         int entry_off;
2463         int ret;
2464
2465         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2466         ASSERT(nat_block);
2467
2468         entry_off = nid % NAT_ENTRY_PER_BLOCK;
2469         block_addr = current_nat_addr(sbi, nid, NULL);
2470
2471         ret = dev_read_block(nat_block, block_addr);
2472         ASSERT(ret >= 0);
2473
2474         if (ino)
2475                 nat_block->entries[entry_off].ino = cpu_to_le32(ino);
2476         nat_block->entries[entry_off].block_addr = cpu_to_le32(newaddr);
2477         if (c.func == FSCK)
2478                 F2FS_FSCK(sbi)->entries[nid] = nat_block->entries[entry_off];
2479
2480         ret = dev_write_block(nat_block, block_addr);
2481         ASSERT(ret >= 0);
2482         free(nat_block);
2483 }
2484
2485 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
2486 {
2487         struct f2fs_nat_entry raw_nat;
2488
2489         ni->nid = nid;
2490         if (c.func == FSCK && F2FS_FSCK(sbi)->nr_nat_entries) {
2491                 node_info_from_raw_nat(ni, &(F2FS_FSCK(sbi)->entries[nid]));
2492                 if (ni->blk_addr)
2493                         return;
2494                 /* nat entry is not cached, read it */
2495         }
2496
2497         get_nat_entry(sbi, nid, &raw_nat);
2498         node_info_from_raw_nat(ni, &raw_nat);
2499 }
2500
2501 static int build_sit_entries(struct f2fs_sb_info *sbi)
2502 {
2503         struct sit_info *sit_i = SIT_I(sbi);
2504         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2505         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2506         struct f2fs_sit_block *sit_blk;
2507         struct seg_entry *se;
2508         struct f2fs_sit_entry sit;
2509         int sit_blk_cnt = SIT_BLK_CNT(sbi);
2510         unsigned int i, segno, end;
2511         unsigned int readed, start_blk = 0;
2512
2513         sit_blk = calloc(BLOCK_SZ, 1);
2514         if (!sit_blk) {
2515                 MSG(1, "\tError: Calloc failed for build_sit_entries!\n");
2516                 return -ENOMEM;
2517         }
2518
2519         do {
2520                 readed = f2fs_ra_meta_pages(sbi, start_blk, MAX_RA_BLOCKS,
2521                                                                 META_SIT);
2522
2523                 segno = start_blk * sit_i->sents_per_block;
2524                 end = (start_blk + readed) * sit_i->sents_per_block;
2525
2526                 for (; segno < end && segno < MAIN_SEGS(sbi); segno++) {
2527                         se = &sit_i->sentries[segno];
2528
2529                         get_current_sit_page(sbi, segno, sit_blk);
2530                         sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2531
2532                         check_block_count(sbi, segno, &sit);
2533                         seg_info_from_raw_sit(sbi, se, &sit);
2534                 }
2535                 start_blk += readed;
2536         } while (start_blk < sit_blk_cnt);
2537
2538
2539         free(sit_blk);
2540
2541         if (sits_in_cursum(journal) > SIT_JOURNAL_ENTRIES) {
2542                 MSG(0, "\tError: build_sit_entries truncate n_sits(%u) to "
2543                         "SIT_JOURNAL_ENTRIES(%zu)\n",
2544                         sits_in_cursum(journal), SIT_JOURNAL_ENTRIES);
2545                 journal->n_sits = cpu_to_le16(SIT_JOURNAL_ENTRIES);
2546                 c.fix_on = 1;
2547         }
2548
2549         for (i = 0; i < sits_in_cursum(journal); i++) {
2550                 segno = le32_to_cpu(segno_in_journal(journal, i));
2551
2552                 if (segno >= MAIN_SEGS(sbi)) {
2553                         MSG(0, "\tError: build_sit_entries: segno(%u) is invalid!!!\n", segno);
2554                         journal->n_sits = cpu_to_le16(i);
2555                         c.fix_on = 1;
2556                         continue;
2557                 }
2558
2559                 se = &sit_i->sentries[segno];
2560                 sit = sit_in_journal(journal, i);
2561
2562                 check_block_count(sbi, segno, &sit);
2563                 seg_info_from_raw_sit(sbi, se, &sit);
2564         }
2565         return 0;
2566 }
2567
2568 static int early_build_segment_manager(struct f2fs_sb_info *sbi)
2569 {
2570         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2571         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2572         struct f2fs_sm_info *sm_info;
2573
2574         sm_info = malloc(sizeof(struct f2fs_sm_info));
2575         if (!sm_info) {
2576                 MSG(1, "\tError: Malloc failed for build_segment_manager!\n");
2577                 return -ENOMEM;
2578         }
2579
2580         /* init sm info */
2581         sbi->sm_info = sm_info;
2582         sm_info->seg0_blkaddr = get_sb(segment0_blkaddr);
2583         sm_info->main_blkaddr = get_sb(main_blkaddr);
2584         sm_info->segment_count = get_sb(segment_count);
2585         sm_info->reserved_segments = get_cp(rsvd_segment_count);
2586         sm_info->ovp_segments = get_cp(overprov_segment_count);
2587         sm_info->main_segments = get_sb(segment_count_main);
2588         sm_info->ssa_blkaddr = get_sb(ssa_blkaddr);
2589
2590         if (build_sit_info(sbi) || build_curseg(sbi)) {
2591                 free(sm_info);
2592                 return -ENOMEM;
2593         }
2594
2595         return 0;
2596 }
2597
2598 static int late_build_segment_manager(struct f2fs_sb_info *sbi)
2599 {
2600         if (sbi->seg_manager_done)
2601                 return 1; /* this function was already called */
2602
2603         sbi->seg_manager_done = true;
2604         if (build_sit_entries(sbi)) {
2605                 free (sbi->sm_info);
2606                 return -ENOMEM;
2607         }
2608
2609         return 0;
2610 }
2611
2612 void build_sit_area_bitmap(struct f2fs_sb_info *sbi)
2613 {
2614         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2615         struct f2fs_sm_info *sm_i = SM_I(sbi);
2616         unsigned int segno = 0;
2617         char *ptr = NULL;
2618         u32 sum_vblocks = 0;
2619         u32 free_segs = 0;
2620         struct seg_entry *se;
2621
2622         fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE;
2623         fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz);
2624         ASSERT(fsck->sit_area_bitmap);
2625         ptr = fsck->sit_area_bitmap;
2626
2627         ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz);
2628
2629         for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2630                 se = get_seg_entry(sbi, segno);
2631
2632                 memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2633                 ptr += SIT_VBLOCK_MAP_SIZE;
2634
2635                 if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno)) {
2636                         if (le32_to_cpu(sbi->ckpt->cur_node_segno[0]) == segno ||
2637                                 le32_to_cpu(sbi->ckpt->cur_data_segno[0]) == segno ||
2638                                 le32_to_cpu(sbi->ckpt->cur_node_segno[1]) == segno ||
2639                                 le32_to_cpu(sbi->ckpt->cur_data_segno[1]) == segno ||
2640                                 le32_to_cpu(sbi->ckpt->cur_node_segno[2]) == segno ||
2641                                 le32_to_cpu(sbi->ckpt->cur_data_segno[2]) == segno) {
2642                                 continue;
2643                         } else {
2644                                 free_segs++;
2645                         }
2646                 } else {
2647                         sum_vblocks += se->valid_blocks;
2648                 }
2649         }
2650         fsck->chk.sit_valid_blocks = sum_vblocks;
2651         fsck->chk.sit_free_segs = free_segs;
2652
2653         DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n",
2654                         sum_vblocks, sum_vblocks,
2655                         free_segs, free_segs);
2656 }
2657
2658 void rewrite_sit_area_bitmap(struct f2fs_sb_info *sbi)
2659 {
2660         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2661         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2662         struct sit_info *sit_i = SIT_I(sbi);
2663         struct f2fs_sit_block *sit_blk;
2664         unsigned int segno = 0;
2665         struct f2fs_summary_block *sum = curseg->sum_blk;
2666         char *ptr = NULL;
2667
2668         sit_blk = calloc(BLOCK_SZ, 1);
2669         ASSERT(sit_blk);
2670         /* remove sit journal */
2671         sum->journal.n_sits = 0;
2672
2673         ptr = fsck->main_area_bitmap;
2674
2675         for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2676                 struct f2fs_sit_entry *sit;
2677                 struct seg_entry *se;
2678                 u16 valid_blocks = 0;
2679                 u16 type;
2680                 int i;
2681
2682                 get_current_sit_page(sbi, segno, sit_blk);
2683                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2684                 memcpy(sit->valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2685
2686                 /* update valid block count */
2687                 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
2688                         valid_blocks += get_bits_in_byte(sit->valid_map[i]);
2689
2690                 se = get_seg_entry(sbi, segno);
2691                 memcpy(se->cur_valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2692                 se->valid_blocks = valid_blocks;
2693                 type = se->type;
2694                 if (type >= NO_CHECK_TYPE) {
2695                         ASSERT_MSG("Invalid type and valid blocks=%x,%x",
2696                                         segno, valid_blocks);
2697                         type = 0;
2698                 }
2699                 sit->vblocks = cpu_to_le16((type << SIT_VBLOCKS_SHIFT) |
2700                                                                 valid_blocks);
2701                 rewrite_current_sit_page(sbi, segno, sit_blk);
2702
2703                 ptr += SIT_VBLOCK_MAP_SIZE;
2704         }
2705
2706         free(sit_blk);
2707 }
2708
2709 static int flush_sit_journal_entries(struct f2fs_sb_info *sbi)
2710 {
2711         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2712         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2713         struct sit_info *sit_i = SIT_I(sbi);
2714         struct f2fs_sit_block *sit_blk;
2715         unsigned int segno;
2716         int i;
2717
2718         sit_blk = calloc(BLOCK_SZ, 1);
2719         ASSERT(sit_blk);
2720         for (i = 0; i < sits_in_cursum(journal); i++) {
2721                 struct f2fs_sit_entry *sit;
2722                 struct seg_entry *se;
2723
2724                 segno = segno_in_journal(journal, i);
2725                 se = get_seg_entry(sbi, segno);
2726
2727                 get_current_sit_page(sbi, segno, sit_blk);
2728                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2729
2730                 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2731                 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2732                                                         se->valid_blocks);
2733                 sit->mtime = cpu_to_le64(se->mtime);
2734
2735                 rewrite_current_sit_page(sbi, segno, sit_blk);
2736         }
2737
2738         free(sit_blk);
2739         journal->n_sits = 0;
2740         return i;
2741 }
2742
2743 static int flush_nat_journal_entries(struct f2fs_sb_info *sbi)
2744 {
2745         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2746         struct f2fs_journal *journal = &curseg->sum_blk->journal;
2747         struct f2fs_nat_block *nat_block;
2748         pgoff_t block_addr;
2749         int entry_off;
2750         nid_t nid;
2751         int ret;
2752         int i = 0;
2753
2754         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
2755         ASSERT(nat_block);
2756 next:
2757         if (i >= nats_in_cursum(journal)) {
2758                 free(nat_block);
2759                 journal->n_nats = 0;
2760                 return i;
2761         }
2762
2763         nid = le32_to_cpu(nid_in_journal(journal, i));
2764
2765         entry_off = nid % NAT_ENTRY_PER_BLOCK;
2766         block_addr = current_nat_addr(sbi, nid, NULL);
2767
2768         ret = dev_read_block(nat_block, block_addr);
2769         ASSERT(ret >= 0);
2770
2771         memcpy(&nat_block->entries[entry_off], &nat_in_journal(journal, i),
2772                                         sizeof(struct f2fs_nat_entry));
2773
2774         ret = dev_write_block(nat_block, block_addr);
2775         ASSERT(ret >= 0);
2776         i++;
2777         goto next;
2778 }
2779
2780 void flush_journal_entries(struct f2fs_sb_info *sbi)
2781 {
2782         int n_nats = flush_nat_journal_entries(sbi);
2783         int n_sits = flush_sit_journal_entries(sbi);
2784
2785         if (n_nats || n_sits) {
2786                 MSG(0, "Info: flush_journal_entries() n_nats: %d, n_sits: %d\n",
2787                                                         n_nats, n_sits);
2788                 write_checkpoints(sbi);
2789         }
2790 }
2791
2792 void flush_sit_entries(struct f2fs_sb_info *sbi)
2793 {
2794         struct sit_info *sit_i = SIT_I(sbi);
2795         struct f2fs_sit_block *sit_blk;
2796         unsigned int segno = 0;
2797
2798         sit_blk = calloc(BLOCK_SZ, 1);
2799         ASSERT(sit_blk);
2800         /* update free segments */
2801         for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2802                 struct f2fs_sit_entry *sit;
2803                 struct seg_entry *se;
2804
2805                 se = get_seg_entry(sbi, segno);
2806
2807                 if (!se->dirty)
2808                         continue;
2809
2810                 get_current_sit_page(sbi, segno, sit_blk);
2811                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2812                 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2813                 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2814                                                         se->valid_blocks);
2815                 rewrite_current_sit_page(sbi, segno, sit_blk);
2816         }
2817
2818         free(sit_blk);
2819 }
2820
2821 int relocate_curseg_offset(struct f2fs_sb_info *sbi, int type)
2822 {
2823         struct curseg_info *curseg = CURSEG_I(sbi, type);
2824         struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
2825         unsigned int i;
2826
2827         if (c.zoned_model == F2FS_ZONED_HM)
2828                 return -EINVAL;
2829
2830         for (i = 0; i < sbi->blocks_per_seg; i++) {
2831                 if (!f2fs_test_bit(i, (const char *)se->cur_valid_map))
2832                         break;
2833         }
2834
2835         if (i == sbi->blocks_per_seg)
2836                 return -EINVAL;
2837
2838         DBG(1, "Update curseg[%d].next_blkoff %u -> %u, alloc_type %s -> SSR\n",
2839                         type, curseg->next_blkoff, i,
2840                         curseg->alloc_type == LFS ? "LFS" : "SSR");
2841
2842         curseg->next_blkoff = i;
2843         curseg->alloc_type = SSR;
2844
2845         return 0;
2846 }
2847
2848 void set_section_type(struct f2fs_sb_info *sbi, unsigned int segno, int type)
2849 {
2850         unsigned int i;
2851
2852         if (sbi->segs_per_sec == 1)
2853                 return;
2854
2855         for (i = 0; i < sbi->segs_per_sec; i++) {
2856                 struct seg_entry *se = get_seg_entry(sbi, segno + i);
2857
2858                 se->type = type;
2859         }
2860 }
2861
2862 #ifdef HAVE_LINUX_BLKZONED_H
2863
2864 static bool write_pointer_at_zone_start(struct f2fs_sb_info *sbi,
2865                                         unsigned int zone_segno)
2866 {
2867         uint64_t sector;
2868         struct blk_zone blkz;
2869         block_t block = START_BLOCK(sbi, zone_segno);
2870         int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2871         int ret, j;
2872
2873         if (c.zoned_model != F2FS_ZONED_HM)
2874                 return true;
2875
2876         for (j = 0; j < MAX_DEVICES; j++) {
2877                 if (!c.devices[j].path)
2878                         break;
2879                 if (c.devices[j].start_blkaddr <= block &&
2880                     block <= c.devices[j].end_blkaddr)
2881                         break;
2882         }
2883
2884         if (j >= MAX_DEVICES)
2885                 return false;
2886
2887         sector = (block - c.devices[j].start_blkaddr) << log_sectors_per_block;
2888         ret = f2fs_report_zone(j, sector, &blkz);
2889         if (ret)
2890                 return false;
2891
2892         if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2893                 return true;
2894
2895         return blk_zone_sector(&blkz) == blk_zone_wp_sector(&blkz);
2896 }
2897
2898 #else
2899
2900 static bool write_pointer_at_zone_start(struct f2fs_sb_info *UNUSED(sbi),
2901                                         unsigned int UNUSED(zone_segno))
2902 {
2903         return true;
2904 }
2905
2906 #endif
2907
2908 int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left,
2909                                                 int want_type, bool new_sec)
2910 {
2911         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2912         struct seg_entry *se;
2913         u32 segno;
2914         u32 offset;
2915         int not_enough = 0;
2916         u64 end_blkaddr = (get_sb(segment_count_main) <<
2917                         get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
2918
2919         if (*to > 0)
2920                 *to -= left;
2921         if (get_free_segments(sbi) <= SM_I(sbi)->reserved_segments + 1)
2922                 not_enough = 1;
2923
2924         while (*to >= SM_I(sbi)->main_blkaddr && *to < end_blkaddr) {
2925                 unsigned short vblocks;
2926                 unsigned char *bitmap;
2927                 unsigned char type;
2928
2929                 segno = GET_SEGNO(sbi, *to);
2930                 offset = OFFSET_IN_SEG(sbi, *to);
2931
2932                 se = get_seg_entry(sbi, segno);
2933
2934                 vblocks = get_seg_vblocks(sbi, se);
2935                 bitmap = get_seg_bitmap(sbi, se);
2936                 type = get_seg_type(sbi, se);
2937
2938                 if (vblocks == sbi->blocks_per_seg) {
2939 next_segment:
2940                         *to = left ? START_BLOCK(sbi, segno) - 1:
2941                                                 START_BLOCK(sbi, segno + 1);
2942                         continue;
2943                 }
2944                 if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
2945                                                 IS_CUR_SEGNO(sbi, segno))
2946                         goto next_segment;
2947                 if (vblocks == 0 && not_enough)
2948                         goto next_segment;
2949
2950                 if (vblocks == 0 && !(segno % sbi->segs_per_sec)) {
2951                         struct seg_entry *se2;
2952                         unsigned int i;
2953
2954                         for (i = 1; i < sbi->segs_per_sec; i++) {
2955                                 se2 = get_seg_entry(sbi, segno + i);
2956                                 if (get_seg_vblocks(sbi, se2))
2957                                         break;
2958                         }
2959
2960                         if (i == sbi->segs_per_sec &&
2961                             write_pointer_at_zone_start(sbi, segno)) {
2962                                 set_section_type(sbi, segno, want_type);
2963                                 return 0;
2964                         }
2965                 }
2966
2967                 if (type == want_type && !new_sec &&
2968                         !f2fs_test_bit(offset, (const char *)bitmap))
2969                         return 0;
2970
2971                 *to = left ? *to - 1: *to + 1;
2972         }
2973         return -1;
2974 }
2975
2976 static void move_one_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left,
2977                                  int i)
2978 {
2979         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2980         struct curseg_info *curseg = CURSEG_I(sbi, i);
2981         struct f2fs_summary_block buf;
2982         u32 old_segno;
2983         u64 ssa_blk, to;
2984         int ret;
2985
2986         if ((get_sb(feature) & F2FS_FEATURE_RO)) {
2987                 if (i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
2988                         return;
2989
2990                 if (i == CURSEG_HOT_DATA) {
2991                         left = 0;
2992                         from = SM_I(sbi)->main_blkaddr;
2993                 } else {
2994                         left = 1;
2995                         from = __end_block_addr(sbi);
2996                 }
2997                 goto bypass_ssa;
2998         }
2999
3000         /* update original SSA too */
3001         ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3002         ret = dev_write_block(curseg->sum_blk, ssa_blk);
3003         ASSERT(ret >= 0);
3004 bypass_ssa:
3005         to = from;
3006         ret = find_next_free_block(sbi, &to, left, i,
3007                                    c.zoned_model == F2FS_ZONED_HM);
3008         ASSERT(ret == 0);
3009
3010         old_segno = curseg->segno;
3011         curseg->segno = GET_SEGNO(sbi, to);
3012         curseg->next_blkoff = OFFSET_IN_SEG(sbi, to);
3013         curseg->alloc_type = c.zoned_model == F2FS_ZONED_HM ? LFS : SSR;
3014
3015         /* update new segno */
3016         ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3017         ret = dev_read_block(&buf, ssa_blk);
3018         ASSERT(ret >= 0);
3019
3020         memcpy(curseg->sum_blk, &buf, SUM_ENTRIES_SIZE);
3021
3022         /* update se->types */
3023         reset_curseg(sbi, i);
3024
3025         FIX_MSG("Move curseg[%d] %x -> %x after %"PRIx64"\n",
3026                 i, old_segno, curseg->segno, from);
3027 }
3028
3029 void move_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left)
3030 {
3031         int i;
3032
3033         /* update summary blocks having nullified journal entries */
3034         for (i = 0; i < NO_CHECK_TYPE; i++)
3035                 move_one_curseg_info(sbi, from, left, i);
3036 }
3037
3038 void update_curseg_info(struct f2fs_sb_info *sbi, int type)
3039 {
3040         if (!relocate_curseg_offset(sbi, type))
3041                 return;
3042         move_one_curseg_info(sbi, SM_I(sbi)->main_blkaddr, 0, type);
3043 }
3044
3045 void zero_journal_entries(struct f2fs_sb_info *sbi)
3046 {
3047         int i;
3048
3049         for (i = 0; i < NO_CHECK_TYPE; i++)
3050                 CURSEG_I(sbi, i)->sum_blk->journal.n_nats = 0;
3051 }
3052
3053 void write_curseg_info(struct f2fs_sb_info *sbi)
3054 {
3055         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3056         int i;
3057
3058         for (i = 0; i < NO_CHECK_TYPE; i++) {
3059                 cp->alloc_type[i] = CURSEG_I(sbi, i)->alloc_type;
3060                 if (i < CURSEG_HOT_NODE) {
3061                         set_cp(cur_data_segno[i], CURSEG_I(sbi, i)->segno);
3062                         set_cp(cur_data_blkoff[i],
3063                                         CURSEG_I(sbi, i)->next_blkoff);
3064                 } else {
3065                         int n = i - CURSEG_HOT_NODE;
3066
3067                         set_cp(cur_node_segno[n], CURSEG_I(sbi, i)->segno);
3068                         set_cp(cur_node_blkoff[n],
3069                                         CURSEG_I(sbi, i)->next_blkoff);
3070                 }
3071         }
3072 }
3073
3074 int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid,
3075                                         struct f2fs_nat_entry *raw_nat)
3076 {
3077         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3078         struct f2fs_journal *journal = &curseg->sum_blk->journal;
3079         int i = 0;
3080
3081         for (i = 0; i < nats_in_cursum(journal); i++) {
3082                 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
3083                         memcpy(raw_nat, &nat_in_journal(journal, i),
3084                                                 sizeof(struct f2fs_nat_entry));
3085                         DBG(3, "==> Found nid [0x%x] in nat cache\n", nid);
3086                         return i;
3087                 }
3088         }
3089         return -1;
3090 }
3091
3092 void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid)
3093 {
3094         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3095         struct f2fs_journal *journal = &curseg->sum_blk->journal;
3096         struct f2fs_nat_block *nat_block;
3097         pgoff_t block_addr;
3098         int entry_off;
3099         int ret;
3100         int i = 0;
3101
3102         /* check in journal */
3103         for (i = 0; i < nats_in_cursum(journal); i++) {
3104                 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
3105                         memset(&nat_in_journal(journal, i), 0,
3106                                         sizeof(struct f2fs_nat_entry));
3107                         FIX_MSG("Remove nid [0x%x] in nat journal", nid);
3108                         return;
3109                 }
3110         }
3111         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
3112         ASSERT(nat_block);
3113
3114         entry_off = nid % NAT_ENTRY_PER_BLOCK;
3115         block_addr = current_nat_addr(sbi, nid, NULL);
3116
3117         ret = dev_read_block(nat_block, block_addr);
3118         ASSERT(ret >= 0);
3119
3120         if (nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi)) {
3121                 FIX_MSG("nid [0x%x] block_addr= 0x%x -> 0x1", nid,
3122                         le32_to_cpu(nat_block->entries[entry_off].block_addr));
3123                 nat_block->entries[entry_off].block_addr = cpu_to_le32(0x1);
3124         } else {
3125                 memset(&nat_block->entries[entry_off], 0,
3126                                         sizeof(struct f2fs_nat_entry));
3127                 FIX_MSG("Remove nid [0x%x] in NAT", nid);
3128         }
3129
3130         ret = dev_write_block(nat_block, block_addr);
3131         ASSERT(ret >= 0);
3132         free(nat_block);
3133 }
3134
3135 void update_nat_journal_blkaddr(struct f2fs_sb_info *sbi, u32 nid,
3136                                         block_t blkaddr)
3137 {
3138         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3139         struct f2fs_journal *journal = &curseg->sum_blk->journal;
3140         int i;
3141
3142         for (i = 0; i < nats_in_cursum(journal); i++) {
3143                 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
3144                         nat_in_journal(journal, i).block_addr =
3145                                                 cpu_to_le32(blkaddr);
3146                         MSG(0, "update nat(nid:%d) blkaddr [0x%x] in journal\n",
3147                                                         nid, blkaddr);
3148                         return;
3149                 }
3150         }
3151 }
3152
3153 void duplicate_checkpoint(struct f2fs_sb_info *sbi)
3154 {
3155         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3156         unsigned long long dst, src;
3157         void *buf;
3158         unsigned int seg_size = 1 << get_sb(log_blocks_per_seg);
3159         int ret;
3160
3161         if (sbi->cp_backuped)
3162                 return;
3163
3164         buf = malloc(F2FS_BLKSIZE * seg_size);
3165         ASSERT(buf);
3166
3167         if (sbi->cur_cp == 1) {
3168                 src = get_sb(cp_blkaddr);
3169                 dst = src + seg_size;
3170         } else {
3171                 dst = get_sb(cp_blkaddr);
3172                 src = dst + seg_size;
3173         }
3174
3175         ret = dev_read(buf, src << F2FS_BLKSIZE_BITS,
3176                                 seg_size << F2FS_BLKSIZE_BITS);
3177         ASSERT(ret >= 0);
3178
3179         ret = dev_write(buf, dst << F2FS_BLKSIZE_BITS,
3180                                 seg_size << F2FS_BLKSIZE_BITS);
3181         ASSERT(ret >= 0);
3182
3183         free(buf);
3184
3185         ret = f2fs_fsync_device();
3186         ASSERT(ret >= 0);
3187
3188         sbi->cp_backuped = 1;
3189
3190         MSG(0, "Info: Duplicate valid checkpoint to mirror position "
3191                 "%llu -> %llu\n", src, dst);
3192 }
3193
3194 void write_checkpoint(struct f2fs_sb_info *sbi)
3195 {
3196         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3197         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3198         block_t orphan_blks = 0;
3199         unsigned long long cp_blk_no;
3200         u32 flags = CP_UMOUNT_FLAG;
3201         int i, ret;
3202         uint32_t crc = 0;
3203
3204         if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
3205                 orphan_blks = __start_sum_addr(sbi) - 1;
3206                 flags |= CP_ORPHAN_PRESENT_FLAG;
3207         }
3208         if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
3209                 flags |= CP_TRIMMED_FLAG;
3210         if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
3211                 flags |= CP_DISABLED_FLAG;
3212         if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
3213                 flags |= CP_LARGE_NAT_BITMAP_FLAG;
3214                 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
3215         } else {
3216                 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
3217         }
3218
3219         set_cp(free_segment_count, get_free_segments(sbi));
3220         if (c.func == FSCK) {
3221                 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3222
3223                 set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
3224                 set_cp(valid_node_count, fsck->chk.valid_node_cnt);
3225                 set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
3226         } else {
3227                 set_cp(valid_block_count, sbi->total_valid_block_count);
3228                 set_cp(valid_node_count, sbi->total_valid_node_count);
3229                 set_cp(valid_inode_count, sbi->total_valid_inode_count);
3230         }
3231         set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload));
3232
3233         flags = update_nat_bits_flags(sb, cp, flags);
3234         set_cp(ckpt_flags, flags);
3235
3236         crc = f2fs_checkpoint_chksum(cp);
3237         *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
3238                                                         cpu_to_le32(crc);
3239
3240         cp_blk_no = get_sb(cp_blkaddr);
3241         if (sbi->cur_cp == 2)
3242                 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
3243
3244         /* write the first cp */
3245         ret = dev_write_block(cp, cp_blk_no++);
3246         ASSERT(ret >= 0);
3247
3248         /* skip payload */
3249         cp_blk_no += get_sb(cp_payload);
3250         /* skip orphan blocks */
3251         cp_blk_no += orphan_blks;
3252
3253         /* update summary blocks having nullified journal entries */
3254         for (i = 0; i < NO_CHECK_TYPE; i++) {
3255                 struct curseg_info *curseg = CURSEG_I(sbi, i);
3256                 u64 ssa_blk;
3257
3258                 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
3259                 ASSERT(ret >= 0);
3260
3261                 if (!(get_sb(feature) & F2FS_FEATURE_RO)) {
3262                         /* update original SSA too */
3263                         ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3264                         ret = dev_write_block(curseg->sum_blk, ssa_blk);
3265                         ASSERT(ret >= 0);
3266                 }
3267         }
3268
3269         /* Write nat bits */
3270         if (flags & CP_NAT_BITS_FLAG)
3271                 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
3272
3273         /* in case of sudden power off */
3274         ret = f2fs_fsync_device();
3275         ASSERT(ret >= 0);
3276
3277         /* write the last cp */
3278         ret = dev_write_block(cp, cp_blk_no++);
3279         ASSERT(ret >= 0);
3280
3281         ret = f2fs_fsync_device();
3282         ASSERT(ret >= 0);
3283
3284         MSG(0, "Info: write_checkpoint() cur_cp:%d\n", sbi->cur_cp);
3285 }
3286
3287 void write_checkpoints(struct f2fs_sb_info *sbi)
3288 {
3289         /* copy valid checkpoint to its mirror position */
3290         duplicate_checkpoint(sbi);
3291
3292         /* repair checkpoint at CP #0 position */
3293         sbi->cur_cp = 1;
3294         write_checkpoint(sbi);
3295 }
3296
3297 void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
3298 {
3299         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3300         struct f2fs_journal *journal = &curseg->sum_blk->journal;
3301         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3302         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3303         struct f2fs_nm_info *nm_i = NM_I(sbi);
3304         struct f2fs_nat_block *nat_block;
3305         struct node_info ni;
3306         u32 nid, nr_nat_blks;
3307         pgoff_t block_off;
3308         pgoff_t block_addr;
3309         int seg_off;
3310         int ret;
3311         unsigned int i;
3312
3313         nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
3314         ASSERT(nat_block);
3315
3316         /* Alloc & build nat entry bitmap */
3317         nr_nat_blks = (get_sb(segment_count_nat) / 2) <<
3318                                         sbi->log_blocks_per_seg;
3319
3320         fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK;
3321         fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8;
3322         fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
3323         ASSERT(fsck->nat_area_bitmap);
3324
3325         fsck->entries = calloc(sizeof(struct f2fs_nat_entry),
3326                                         fsck->nr_nat_entries);
3327         ASSERT(fsck->entries);
3328
3329         for (block_off = 0; block_off < nr_nat_blks; block_off++) {
3330
3331                 seg_off = block_off >> sbi->log_blocks_per_seg;
3332                 block_addr = (pgoff_t)(nm_i->nat_blkaddr +
3333                         (seg_off << sbi->log_blocks_per_seg << 1) +
3334                         (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
3335
3336                 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
3337                         block_addr += sbi->blocks_per_seg;
3338
3339                 ret = dev_read_block(nat_block, block_addr);
3340                 ASSERT(ret >= 0);
3341
3342                 nid = block_off * NAT_ENTRY_PER_BLOCK;
3343                 for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
3344                         ni.nid = nid + i;
3345
3346                         if ((nid + i) == F2FS_NODE_INO(sbi) ||
3347                                         (nid + i) == F2FS_META_INO(sbi)) {
3348                                 /*
3349                                  * block_addr of node/meta inode should be 0x1.
3350                                  * Set this bit, and fsck_verify will fix it.
3351                                  */
3352                                 if (le32_to_cpu(nat_block->entries[i].block_addr) != 0x1) {
3353                                         ASSERT_MSG("\tError: ino[0x%x] block_addr[0x%x] is invalid\n",
3354                                                         nid + i, le32_to_cpu(nat_block->entries[i].block_addr));
3355                                         f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
3356                                 }
3357                                 continue;
3358                         }
3359
3360                         node_info_from_raw_nat(&ni, &nat_block->entries[i]);
3361                         if (ni.blk_addr == 0x0)
3362                                 continue;
3363                         if (ni.ino == 0x0) {
3364                                 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
3365                                         " is invalid\n", ni.ino, ni.blk_addr);
3366                         }
3367                         if (ni.ino == (nid + i)) {
3368                                 fsck->nat_valid_inode_cnt++;
3369                                 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
3370                         }
3371                         if (nid + i == 0) {
3372                                 /*
3373                                  * nat entry [0] must be null.  If
3374                                  * it is corrupted, set its bit in
3375                                  * nat_area_bitmap, fsck_verify will
3376                                  * nullify it
3377                                  */
3378                                 ASSERT_MSG("Invalid nat entry[0]: "
3379                                         "blk_addr[0x%x]\n", ni.blk_addr);
3380                                 fsck->chk.valid_nat_entry_cnt--;
3381                         }
3382
3383                         DBG(3, "nid[0x%8x] addr[0x%16x] ino[0x%8x]\n",
3384                                 nid + i, ni.blk_addr, ni.ino);
3385                         f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
3386                         fsck->chk.valid_nat_entry_cnt++;
3387
3388                         fsck->entries[nid + i] = nat_block->entries[i];
3389                 }
3390         }
3391
3392         /* Traverse nat journal, update the corresponding entries */
3393         for (i = 0; i < nats_in_cursum(journal); i++) {
3394                 struct f2fs_nat_entry raw_nat;
3395                 nid = le32_to_cpu(nid_in_journal(journal, i));
3396                 ni.nid = nid;
3397
3398                 DBG(3, "==> Found nid [0x%x] in nat cache, update it\n", nid);
3399
3400                 /* Clear the original bit and count */
3401                 if (fsck->entries[nid].block_addr != 0x0) {
3402                         fsck->chk.valid_nat_entry_cnt--;
3403                         f2fs_clear_bit(nid, fsck->nat_area_bitmap);
3404                         if (fsck->entries[nid].ino == nid)
3405                                 fsck->nat_valid_inode_cnt--;
3406                 }
3407
3408                 /* Use nat entries in journal */
3409                 memcpy(&raw_nat, &nat_in_journal(journal, i),
3410                                         sizeof(struct f2fs_nat_entry));
3411                 node_info_from_raw_nat(&ni, &raw_nat);
3412                 if (ni.blk_addr != 0x0) {
3413                         if (ni.ino == 0x0)
3414                                 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
3415                                         " is invalid\n", ni.ino, ni.blk_addr);
3416                         if (ni.ino == nid) {
3417                                 fsck->nat_valid_inode_cnt++;
3418                                 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
3419                         }
3420                         f2fs_set_bit(nid, fsck->nat_area_bitmap);
3421                         fsck->chk.valid_nat_entry_cnt++;
3422                         DBG(3, "nid[0x%x] in nat cache\n", nid);
3423                 }
3424                 fsck->entries[nid] = raw_nat;
3425         }
3426         free(nat_block);
3427
3428         DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n",
3429                         fsck->chk.valid_nat_entry_cnt,
3430                         fsck->chk.valid_nat_entry_cnt);
3431 }
3432
3433 static int check_sector_size(struct f2fs_super_block *sb)
3434 {
3435         uint32_t log_sectorsize, log_sectors_per_block;
3436
3437         log_sectorsize = log_base_2(c.sector_size);
3438         log_sectors_per_block = log_base_2(c.sectors_per_blk);
3439
3440         if (log_sectorsize == get_sb(log_sectorsize) &&
3441                         log_sectors_per_block == get_sb(log_sectors_per_block))
3442                 return 0;
3443
3444         set_sb(log_sectorsize, log_sectorsize);
3445         set_sb(log_sectors_per_block, log_sectors_per_block);
3446
3447         update_superblock(sb, SB_MASK_ALL);
3448         return 0;
3449 }
3450
3451 static int tune_sb_features(struct f2fs_sb_info *sbi)
3452 {
3453         int sb_changed = 0;
3454         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3455
3456         if (!(get_sb(feature) & F2FS_FEATURE_ENCRYPT) &&
3457                         c.feature & F2FS_FEATURE_ENCRYPT) {
3458                 sb->feature = cpu_to_le32(get_sb(feature) |
3459                                         F2FS_FEATURE_ENCRYPT);
3460                 MSG(0, "Info: Set Encryption feature\n");
3461                 sb_changed = 1;
3462         }
3463         if (!(get_sb(feature) & F2FS_FEATURE_CASEFOLD) &&
3464                 c.feature & F2FS_FEATURE_CASEFOLD) {
3465                 if (!c.s_encoding) {
3466                         ERR_MSG("ERROR: Must specify encoding to enable casefolding.\n");
3467                         return -1;
3468                 }
3469                 sb->feature = cpu_to_le32(get_sb(feature) |
3470                                         F2FS_FEATURE_CASEFOLD);
3471                 MSG(0, "Info: Set Casefold feature\n");
3472                 sb_changed = 1;
3473         }
3474         /* TODO: quota needs to allocate inode numbers */
3475
3476         c.feature = get_sb(feature);
3477         if (!sb_changed)
3478                 return 0;
3479
3480         update_superblock(sb, SB_MASK_ALL);
3481         return 0;
3482 }
3483
3484 static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
3485                                                                 nid_t ino)
3486 {
3487         struct fsync_inode_entry *entry;
3488
3489         list_for_each_entry(entry, head, list)
3490                 if (entry->ino == ino)
3491                         return entry;
3492
3493         return NULL;
3494 }
3495
3496 static struct fsync_inode_entry *add_fsync_inode(struct list_head *head,
3497                                                                 nid_t ino)
3498 {
3499         struct fsync_inode_entry *entry;
3500
3501         entry = calloc(sizeof(struct fsync_inode_entry), 1);
3502         if (!entry)
3503                 return NULL;
3504         entry->ino = ino;
3505         list_add_tail(&entry->list, head);
3506         return entry;
3507 }
3508
3509 static void del_fsync_inode(struct fsync_inode_entry *entry)
3510 {
3511         list_del(&entry->list);
3512         free(entry);
3513 }
3514
3515 static void destroy_fsync_dnodes(struct list_head *head)
3516 {
3517         struct fsync_inode_entry *entry, *tmp;
3518
3519         list_for_each_entry_safe(entry, tmp, head, list)
3520                 del_fsync_inode(entry);
3521 }
3522
3523 static int find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
3524 {
3525         struct curseg_info *curseg;
3526         struct f2fs_node *node_blk;
3527         block_t blkaddr;
3528         unsigned int loop_cnt = 0;
3529         unsigned int free_blocks = MAIN_SEGS(sbi) * sbi->blocks_per_seg -
3530                                                 sbi->total_valid_block_count;
3531         int err = 0;
3532
3533         /* get node pages in the current segment */
3534         curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3535         blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3536
3537         node_blk = calloc(F2FS_BLKSIZE, 1);
3538         ASSERT(node_blk);
3539
3540         while (1) {
3541                 struct fsync_inode_entry *entry;
3542
3543                 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
3544                         break;
3545
3546                 err = dev_read_block(node_blk, blkaddr);
3547                 if (err)
3548                         break;
3549
3550                 if (!is_recoverable_dnode(sbi, node_blk))
3551                         break;
3552
3553                 if (!is_fsync_dnode(node_blk))
3554                         goto next;
3555
3556                 entry = get_fsync_inode(head, ino_of_node(node_blk));
3557                 if (!entry) {
3558                         entry = add_fsync_inode(head, ino_of_node(node_blk));
3559                         if (!entry) {
3560                                 err = -1;
3561                                 break;
3562                         }
3563                 }
3564                 entry->blkaddr = blkaddr;
3565
3566                 if (IS_INODE(node_blk) && is_dent_dnode(node_blk))
3567                         entry->last_dentry = blkaddr;
3568 next:
3569                 /* sanity check in order to detect looped node chain */
3570                 if (++loop_cnt >= free_blocks ||
3571                         blkaddr == next_blkaddr_of_node(node_blk)) {
3572                         MSG(0, "\tdetect looped node chain, blkaddr:%u, next:%u\n",
3573                                     blkaddr,
3574                                     next_blkaddr_of_node(node_blk));
3575                         err = -1;
3576                         break;
3577                 }
3578
3579                 blkaddr = next_blkaddr_of_node(node_blk);
3580         }
3581
3582         free(node_blk);
3583         return err;
3584 }
3585
3586 static int do_record_fsync_data(struct f2fs_sb_info *sbi,
3587                                         struct f2fs_node *node_blk,
3588                                         block_t blkaddr)
3589 {
3590         unsigned int segno, offset;
3591         struct seg_entry *se;
3592         unsigned int ofs_in_node = 0;
3593         unsigned int start, end;
3594         int err = 0, recorded = 0;
3595
3596         segno = GET_SEGNO(sbi, blkaddr);
3597         se = get_seg_entry(sbi, segno);
3598         offset = OFFSET_IN_SEG(sbi, blkaddr);
3599
3600         if (f2fs_test_bit(offset, (char *)se->cur_valid_map)) {
3601                 ASSERT(0);
3602                 return -1;
3603         }
3604         if (f2fs_test_bit(offset, (char *)se->ckpt_valid_map)) {
3605                 ASSERT(0);
3606                 return -1;
3607         }
3608
3609         if (!se->ckpt_valid_blocks)
3610                 se->ckpt_type = CURSEG_WARM_NODE;
3611
3612         se->ckpt_valid_blocks++;
3613         f2fs_set_bit(offset, (char *)se->ckpt_valid_map);
3614
3615         MSG(1, "do_record_fsync_data: [node] ino = %u, nid = %u, blkaddr = %u\n",
3616             ino_of_node(node_blk), ofs_of_node(node_blk), blkaddr);
3617
3618         /* inline data */
3619         if (IS_INODE(node_blk) && (node_blk->i.i_inline & F2FS_INLINE_DATA))
3620                 return 0;
3621         /* xattr node */
3622         if (ofs_of_node(node_blk) == XATTR_NODE_OFFSET)
3623                 return 0;
3624
3625         /* step 3: recover data indices */
3626         start = start_bidx_of_node(ofs_of_node(node_blk), node_blk);
3627         end = start + ADDRS_PER_PAGE(sbi, node_blk, NULL);
3628
3629         for (; start < end; start++, ofs_in_node++) {
3630                 blkaddr = datablock_addr(node_blk, ofs_in_node);
3631
3632                 if (!is_valid_data_blkaddr(blkaddr))
3633                         continue;
3634
3635                 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR)) {
3636                         err = -1;
3637                         goto out;
3638                 }
3639
3640                 segno = GET_SEGNO(sbi, blkaddr);
3641                 se = get_seg_entry(sbi, segno);
3642                 offset = OFFSET_IN_SEG(sbi, blkaddr);
3643
3644                 if (f2fs_test_bit(offset, (char *)se->cur_valid_map))
3645                         continue;
3646                 if (f2fs_test_bit(offset, (char *)se->ckpt_valid_map))
3647                         continue;
3648
3649                 if (!se->ckpt_valid_blocks)
3650                         se->ckpt_type = CURSEG_WARM_DATA;
3651
3652                 se->ckpt_valid_blocks++;
3653                 f2fs_set_bit(offset, (char *)se->ckpt_valid_map);
3654
3655                 MSG(1, "do_record_fsync_data: [data] ino = %u, nid = %u, blkaddr = %u\n",
3656                     ino_of_node(node_blk), ofs_of_node(node_blk), blkaddr);
3657
3658                 recorded++;
3659         }
3660 out:
3661         MSG(1, "recover_data: ino = %u, nid = %u, recorded = %d, err = %d\n",
3662                     ino_of_node(node_blk), ofs_of_node(node_blk),
3663                     recorded, err);
3664         return err;
3665 }
3666
3667 static int traverse_dnodes(struct f2fs_sb_info *sbi,
3668                                 struct list_head *inode_list)
3669 {
3670         struct curseg_info *curseg;
3671         struct f2fs_node *node_blk;
3672         block_t blkaddr;
3673         int err = 0;
3674
3675         /* get node pages in the current segment */
3676         curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3677         blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3678
3679         node_blk = calloc(F2FS_BLKSIZE, 1);
3680         ASSERT(node_blk);
3681
3682         while (1) {
3683                 struct fsync_inode_entry *entry;
3684
3685                 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
3686                         break;
3687
3688                 err = dev_read_block(node_blk, blkaddr);
3689                 if (err)
3690                         break;
3691
3692                 if (!is_recoverable_dnode(sbi, node_blk))
3693                         break;
3694
3695                 entry = get_fsync_inode(inode_list,
3696                                         ino_of_node(node_blk));
3697                 if (!entry)
3698                         goto next;
3699
3700                 err = do_record_fsync_data(sbi, node_blk, blkaddr);
3701                 if (err)
3702                         break;
3703
3704                 if (entry->blkaddr == blkaddr)
3705                         del_fsync_inode(entry);
3706 next:
3707                 blkaddr = next_blkaddr_of_node(node_blk);
3708         }
3709
3710         free(node_blk);
3711         return err;
3712 }
3713
3714 static int record_fsync_data(struct f2fs_sb_info *sbi)
3715 {
3716         struct list_head inode_list = LIST_HEAD_INIT(inode_list);
3717         int ret;
3718
3719         if (!need_fsync_data_record(sbi))
3720                 return 0;
3721
3722         ret = find_fsync_inode(sbi, &inode_list);
3723         if (ret)
3724                 goto out;
3725
3726         ret = late_build_segment_manager(sbi);
3727         if (ret < 0) {
3728                 ERR_MSG("late_build_segment_manager failed\n");
3729                 goto out;
3730         }
3731
3732         ret = traverse_dnodes(sbi, &inode_list);
3733 out:
3734         destroy_fsync_dnodes(&inode_list);
3735         return ret;
3736 }
3737
3738 int f2fs_do_mount(struct f2fs_sb_info *sbi)
3739 {
3740         struct f2fs_checkpoint *cp = NULL;
3741         struct f2fs_super_block *sb = NULL;
3742         int ret;
3743
3744         sbi->active_logs = NR_CURSEG_TYPE;
3745         ret = validate_super_block(sbi, SB0_ADDR);
3746         if (ret) {
3747                 ret = validate_super_block(sbi, SB1_ADDR);
3748                 if (ret)
3749                         return -1;
3750         }
3751         sb = F2FS_RAW_SUPER(sbi);
3752
3753         ret = check_sector_size(sb);
3754         if (ret)
3755                 return -1;
3756
3757         print_raw_sb_info(sb);
3758
3759         init_sb_info(sbi);
3760
3761         ret = get_valid_checkpoint(sbi);
3762         if (ret) {
3763                 ERR_MSG("Can't find valid checkpoint\n");
3764                 return -1;
3765         }
3766
3767         c.bug_on = 0;
3768
3769         if (sanity_check_ckpt(sbi)) {
3770                 ERR_MSG("Checkpoint is polluted\n");
3771                 return -1;
3772         }
3773         cp = F2FS_CKPT(sbi);
3774
3775         if (c.func != FSCK && c.func != DUMP &&
3776                 !is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
3777                 ERR_MSG("Mount unclean image to replay log first\n");
3778                 return -1;
3779         }
3780
3781         if (c.func == FSCK) {
3782 #if defined(__APPLE__)
3783                 if (!c.no_kernel_check &&
3784                         memcmp(c.sb_version, c.version, VERSION_NAME_LEN)) {
3785                         c.auto_fix = 0;
3786                         c.fix_on = 1;
3787                         memcpy(sbi->raw_super->version,
3788                                         c.version, VERSION_NAME_LEN);
3789                         update_superblock(sbi->raw_super, SB_MASK_ALL);
3790                 }
3791 #else
3792                 if (!c.no_kernel_check) {
3793                         u32 prev_time, cur_time, time_diff;
3794                         __le32 *ver_ts_ptr = (__le32 *)(sbi->raw_super->version
3795                                                 + VERSION_NAME_LEN);
3796
3797                         cur_time = (u32)get_cp(elapsed_time);
3798                         prev_time = le32_to_cpu(*ver_ts_ptr);
3799
3800                         MSG(0, "Info: version timestamp cur: %u, prev: %u\n",
3801                                         cur_time, prev_time);
3802                         if (!memcmp(c.sb_version, c.version,
3803                                                 VERSION_NAME_LEN)) {
3804                                 /* valid prev_time */
3805                                 if (prev_time != 0 && cur_time > prev_time) {
3806                                         time_diff = cur_time - prev_time;
3807                                         if (time_diff < CHECK_PERIOD)
3808                                                 goto out;
3809                                         c.auto_fix = 0;
3810                                         c.fix_on = 1;
3811                                 }
3812                         } else {
3813                                 memcpy(sbi->raw_super->version,
3814                                                 c.version, VERSION_NAME_LEN);
3815                         }
3816
3817                         *ver_ts_ptr = cpu_to_le32(cur_time);
3818                         update_superblock(sbi->raw_super, SB_MASK_ALL);
3819                 }
3820 #endif
3821         }
3822 out:
3823         print_ckpt_info(sbi);
3824
3825         if (c.quota_fix) {
3826                 if (get_cp(ckpt_flags) & CP_QUOTA_NEED_FSCK_FLAG)
3827                         c.fix_on = 1;
3828         }
3829         if (c.layout)
3830                 return 1;
3831
3832         if (tune_sb_features(sbi))
3833                 return -1;
3834
3835         /* precompute checksum seed for metadata */
3836         if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
3837                 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
3838
3839         sbi->total_valid_node_count = get_cp(valid_node_count);
3840         sbi->total_valid_inode_count = get_cp(valid_inode_count);
3841         sbi->user_block_count = get_cp(user_block_count);
3842         sbi->total_valid_block_count = get_cp(valid_block_count);
3843         sbi->last_valid_block_count = sbi->total_valid_block_count;
3844         sbi->alloc_valid_block_count = 0;
3845
3846         if (early_build_segment_manager(sbi)) {
3847                 ERR_MSG("early_build_segment_manager failed\n");
3848                 return -1;
3849         }
3850
3851         if (build_node_manager(sbi)) {
3852                 ERR_MSG("build_node_manager failed\n");
3853                 return -1;
3854         }
3855
3856         if (record_fsync_data(sbi)) {
3857                 ERR_MSG("record_fsync_data failed\n");
3858                 return -1;
3859         }
3860
3861         if (!f2fs_should_proceed(sb, get_cp(ckpt_flags)))
3862                 return 1;
3863
3864         if (late_build_segment_manager(sbi) < 0) {
3865                 ERR_MSG("late_build_segment_manager failed\n");
3866                 return -1;
3867         }
3868
3869         if (f2fs_late_init_nid_bitmap(sbi)) {
3870                 ERR_MSG("f2fs_late_init_nid_bitmap failed\n");
3871                 return -1;
3872         }
3873
3874         /* Check nat_bits */
3875         if (c.func == FSCK && is_set_ckpt_flags(cp, CP_NAT_BITS_FLAG)) {
3876                 if (check_nat_bits(sbi, sb, cp) && c.fix_on)
3877                         write_nat_bits(sbi, sb, cp, sbi->cur_cp);
3878         }
3879         return 0;
3880 }
3881
3882 void f2fs_do_umount(struct f2fs_sb_info *sbi)
3883 {
3884         struct sit_info *sit_i = SIT_I(sbi);
3885         struct f2fs_sm_info *sm_i = SM_I(sbi);
3886         struct f2fs_nm_info *nm_i = NM_I(sbi);
3887         unsigned int i;
3888
3889         /* free nm_info */
3890         if (c.func == SLOAD || c.func == FSCK)
3891                 free(nm_i->nid_bitmap);
3892         free(nm_i->nat_bitmap);
3893         free(sbi->nm_info);
3894
3895         /* free sit_info */
3896         free(sit_i->bitmap);
3897         free(sit_i->sit_bitmap);
3898         free(sit_i->sentries);
3899         free(sm_i->sit_info);
3900
3901         /* free sm_info */
3902         for (i = 0; i < NR_CURSEG_TYPE; i++)
3903                 free(sm_i->curseg_array[i].sum_blk);
3904
3905         free(sm_i->curseg_array);
3906         free(sbi->sm_info);
3907
3908         free(sbi->ckpt);
3909         free(sbi->raw_super);
3910 }
3911
3912 #ifdef WITH_ANDROID
3913 int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi)
3914 {
3915         struct f2fs_super_block *sb = sbi->raw_super;
3916         uint32_t sit_seg_count, sit_size;
3917         uint32_t nat_seg_count, nat_size;
3918         uint64_t sit_seg_addr, nat_seg_addr, payload_addr;
3919         uint32_t seg_size = 1 << get_sb(log_blocks_per_seg);
3920         int ret;
3921
3922         if (!c.sparse_mode)
3923                 return 0;
3924
3925         sit_seg_addr = get_sb(sit_blkaddr);
3926         sit_seg_count = get_sb(segment_count_sit);
3927         sit_size = sit_seg_count * seg_size;
3928
3929         DBG(1, "\tSparse: filling sit area at block offset: 0x%08"PRIx64" len: %u\n",
3930                                                         sit_seg_addr, sit_size);
3931         ret = dev_fill(NULL, sit_seg_addr * F2FS_BLKSIZE,
3932                                         sit_size * F2FS_BLKSIZE);
3933         if (ret) {
3934                 MSG(1, "\tError: While zeroing out the sit area "
3935                                 "on disk!!!\n");
3936                 return -1;
3937         }
3938
3939         nat_seg_addr = get_sb(nat_blkaddr);
3940         nat_seg_count = get_sb(segment_count_nat);
3941         nat_size = nat_seg_count * seg_size;
3942
3943         DBG(1, "\tSparse: filling nat area at block offset 0x%08"PRIx64" len: %u\n",
3944                                                         nat_seg_addr, nat_size);
3945         ret = dev_fill(NULL, nat_seg_addr * F2FS_BLKSIZE,
3946                                         nat_size * F2FS_BLKSIZE);
3947         if (ret) {
3948                 MSG(1, "\tError: While zeroing out the nat area "
3949                                 "on disk!!!\n");
3950                 return -1;
3951         }
3952
3953         payload_addr = get_sb(segment0_blkaddr) + 1;
3954
3955         DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
3956                                         payload_addr, get_sb(cp_payload));
3957         ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
3958                                         get_sb(cp_payload) * F2FS_BLKSIZE);
3959         if (ret) {
3960                 MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
3961                                 "on disk!!!\n");
3962                 return -1;
3963         }
3964
3965         payload_addr += seg_size;
3966
3967         DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
3968                                         payload_addr, get_sb(cp_payload));
3969         ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
3970                                         get_sb(cp_payload) * F2FS_BLKSIZE);
3971         if (ret) {
3972                 MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
3973                                 "on disk!!!\n");
3974                 return -1;
3975         }
3976         return 0;
3977 }
3978 #else
3979 int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi) { return 0; }
3980 #endif