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