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