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