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