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