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