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