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