• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * f2fs_format.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * Dual licensed under the GPL or LGPL version 2 licenses.
8  */
9 #ifndef _LARGEFILE64_SOURCE
10 #define _LARGEFILE64_SOURCE
11 #endif
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <f2fs_fs.h>
19 
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_SYS_MOUNT_H
24 #include <sys/mount.h>
25 #endif
26 #include <time.h>
27 
28 #ifdef HAVE_UUID_UUID_H
29 #include <uuid.h>
30 #endif
31 #ifndef HAVE_LIBUUID
32 #define uuid_parse(a, b) -1
33 #define uuid_generate(a)
34 #endif
35 
36 #include "quota.h"
37 #include "f2fs_format_utils.h"
38 
39 extern struct f2fs_configuration c;
40 struct f2fs_super_block raw_sb;
41 struct f2fs_super_block *sb = &raw_sb;
42 struct f2fs_checkpoint *cp;
43 
44 /* Return first segment number of each area */
45 #define prev_zone(cur)		(c.cur_seg[cur] - c.segs_per_zone)
46 #define next_zone(cur)		(c.cur_seg[cur] + c.segs_per_zone)
47 #define last_zone(cur)		((cur - 1) * c.segs_per_zone)
48 #define last_section(cur)	(cur + (c.secs_per_zone - 1) * c.segs_per_sec)
49 
50 /* Return time fixed by the user or current time by default */
51 #define mkfs_time ((c.fixed_time == -1) ? time(NULL) : c.fixed_time)
52 
53 const char *media_ext_lists[] = {
54 	/* common prefix */
55 	"mp", // Covers mp3, mp4, mpeg, mpg
56 	"wm", // Covers wma, wmb, wmv
57 	"og", // Covers oga, ogg, ogm, ogv
58 	"jp", // Covers jpg, jpeg, jp2
59 
60 	/* video */
61 	"avi",
62 	"m4v",
63 	"m4p",
64 	"mkv",
65 	"mov",
66 	"webm",
67 
68 	/* audio */
69 	"wav",
70 	"m4a",
71 	"3gp",
72 	"opus",
73 	"flac",
74 
75 	/* image */
76 	"gif",
77 	"png",
78 	"svg",
79 	"webp",
80 
81 	/* archives */
82 	"jar",
83 	"deb",
84 	"iso",
85 	"gz",
86 	"xz",
87 	"zst",
88 
89 	/* others */
90 	"pdf",
91 	"pyc", // Python bytecode
92 	"ttc",
93 	"ttf",
94 	"exe",
95 
96 	/* android */
97 	"apk",
98 	"cnt", // Image alias
99 	"exo", // YouTube
100 	"odex", // Android RunTime
101 	"vdex", // Android RunTime
102 	"so",
103 
104 	NULL
105 };
106 
107 const char *hot_ext_lists[] = {
108 	"db",
109 
110 #ifndef WITH_ANDROID
111 	/* Virtual machines */
112 	"vmdk", // VMware or VirtualBox
113 	"vdi", // VirtualBox
114 	"qcow2", // QEMU
115 #endif
116 	NULL
117 };
118 
119 const char **default_ext_list[] = {
120 	media_ext_lists,
121 	hot_ext_lists
122 };
123 
is_extension_exist(const char * name)124 static bool is_extension_exist(const char *name)
125 {
126 	int i;
127 
128 	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
129 		char *ext = (char *)sb->extension_list[i];
130 		if (!strcmp(ext, name))
131 			return 1;
132 	}
133 
134 	return 0;
135 }
136 
cure_extension_list(void)137 static void cure_extension_list(void)
138 {
139 	const char **extlist;
140 	char *ext_str;
141 	char *ue;
142 	int name_len;
143 	int i, pos = 0;
144 
145 	set_sb(extension_count, 0);
146 	memset(sb->extension_list, 0, sizeof(sb->extension_list));
147 
148 	for (i = 0; i < 2; i++) {
149 		ext_str = c.extension_list[i];
150 		extlist = default_ext_list[i];
151 
152 		while (*extlist) {
153 			name_len = strlen(*extlist);
154 			memcpy(sb->extension_list[pos++], *extlist, name_len);
155 			extlist++;
156 		}
157 		if (i == 0)
158 			set_sb(extension_count, pos);
159 		else
160 			sb->hot_ext_count = pos - get_sb(extension_count);;
161 
162 		if (!ext_str)
163 			continue;
164 
165 		/* add user ext list */
166 		ue = strtok(ext_str, ", ");
167 		while (ue != NULL) {
168 			name_len = strlen(ue);
169 			if (name_len >= F2FS_EXTENSION_LEN) {
170 				MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
171 				goto next;
172 			}
173 			if (!is_extension_exist(ue))
174 				memcpy(sb->extension_list[pos++], ue, name_len);
175 next:
176 			ue = strtok(NULL, ", ");
177 			if (pos >= F2FS_MAX_EXTENSION)
178 				break;
179 		}
180 
181 		if (i == 0)
182 			set_sb(extension_count, pos);
183 		else
184 			sb->hot_ext_count = pos - get_sb(extension_count);
185 
186 		free(c.extension_list[i]);
187 	}
188 }
189 
verify_cur_segs(void)190 static void verify_cur_segs(void)
191 {
192 	int i, j;
193 	int reorder = 0;
194 
195 	for (i = 0; i < NR_CURSEG_TYPE; i++) {
196 		for (j = i + 1; j < NR_CURSEG_TYPE; j++) {
197 			if (c.cur_seg[i] == c.cur_seg[j]) {
198 				reorder = 1;
199 				break;
200 			}
201 		}
202 	}
203 
204 	if (!reorder)
205 		return;
206 
207 	c.cur_seg[0] = 0;
208 	for (i = 1; i < NR_CURSEG_TYPE; i++)
209 		c.cur_seg[i] = next_zone(i - 1);
210 }
211 
f2fs_prepare_super_block(void)212 static int f2fs_prepare_super_block(void)
213 {
214 	uint32_t blk_size_bytes;
215 	uint32_t log_sectorsize, log_sectors_per_block;
216 	uint32_t log_blocksize, log_blks_per_seg;
217 	uint32_t segment_size_bytes, zone_size_bytes;
218 	uint32_t sit_segments, nat_segments;
219 	uint32_t blocks_for_sit, blocks_for_nat, blocks_for_ssa;
220 	uint32_t total_valid_blks_available;
221 	uint64_t zone_align_start_offset, diff;
222 	uint64_t total_meta_zones, total_meta_segments;
223 	uint32_t sit_bitmap_size, max_sit_bitmap_size;
224 	uint32_t max_nat_bitmap_size, max_nat_segments;
225 	uint32_t total_zones, avail_zones;
226 	enum quota_type qtype;
227 	int i;
228 
229 	set_sb(magic, F2FS_SUPER_MAGIC);
230 	set_sb(major_ver, F2FS_MAJOR_VERSION);
231 	set_sb(minor_ver, F2FS_MINOR_VERSION);
232 
233 	log_sectorsize = log_base_2(c.sector_size);
234 	log_sectors_per_block = log_base_2(c.sectors_per_blk);
235 	log_blocksize = log_sectorsize + log_sectors_per_block;
236 	log_blks_per_seg = log_base_2(c.blks_per_seg);
237 
238 	set_sb(log_sectorsize, log_sectorsize);
239 	set_sb(log_sectors_per_block, log_sectors_per_block);
240 
241 	set_sb(log_blocksize, log_blocksize);
242 	set_sb(log_blocks_per_seg, log_blks_per_seg);
243 
244 	set_sb(segs_per_sec, c.segs_per_sec);
245 	set_sb(secs_per_zone, c.secs_per_zone);
246 
247 	blk_size_bytes = 1 << log_blocksize;
248 	segment_size_bytes = blk_size_bytes * c.blks_per_seg;
249 	zone_size_bytes =
250 		blk_size_bytes * c.secs_per_zone *
251 		c.segs_per_sec * c.blks_per_seg;
252 
253 	set_sb(checksum_offset, 0);
254 
255 	set_sb(block_count, c.total_sectors >> log_sectors_per_block);
256 
257 	zone_align_start_offset =
258 		((uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE +
259 		2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
260 		zone_size_bytes * zone_size_bytes -
261 		(uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE;
262 
263 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
264 		zone_align_start_offset = 8192;
265 
266 	if (c.start_sector % DEFAULT_SECTORS_PER_BLOCK) {
267 		MSG(1, "\t%s: Align start sector number to the page unit\n",
268 				c.zoned_mode ? "FAIL" : "WARN");
269 		MSG(1, "\ti.e., start sector: %d, ofs:%d (sects/page: %d)\n",
270 				c.start_sector,
271 				c.start_sector % DEFAULT_SECTORS_PER_BLOCK,
272 				DEFAULT_SECTORS_PER_BLOCK);
273 		if (c.zoned_mode)
274 			return -1;
275 	}
276 
277 	if (c.zoned_mode && c.ndevs > 1)
278 		zone_align_start_offset +=
279 			(c.devices[0].total_sectors * c.sector_size) % zone_size_bytes;
280 
281 	set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
282 	sb->cp_blkaddr = sb->segment0_blkaddr;
283 
284 	MSG(0, "Info: zone aligned segment0 blkaddr: %u\n",
285 					get_sb(segment0_blkaddr));
286 
287 	if (c.zoned_mode &&
288 		((c.ndevs == 1 &&
289 			(get_sb(segment0_blkaddr) + c.start_sector /
290 			DEFAULT_SECTORS_PER_BLOCK) % c.zone_blocks) ||
291 		(c.ndevs > 1 &&
292 			c.devices[1].start_blkaddr % c.zone_blocks))) {
293 		MSG(1, "\tError: Unaligned segment0 block address %u\n",
294 				get_sb(segment0_blkaddr));
295 		return -1;
296 	}
297 
298 	for (i = 0; i < c.ndevs; i++) {
299 		if (i == 0) {
300 			c.devices[i].total_segments =
301 				(c.devices[i].total_sectors *
302 				c.sector_size - zone_align_start_offset) /
303 				segment_size_bytes;
304 			c.devices[i].start_blkaddr = 0;
305 			c.devices[i].end_blkaddr = c.devices[i].total_segments *
306 						c.blks_per_seg - 1 +
307 						sb->segment0_blkaddr;
308 		} else {
309 			c.devices[i].total_segments =
310 				c.devices[i].total_sectors /
311 				(c.sectors_per_blk * c.blks_per_seg);
312 			c.devices[i].start_blkaddr =
313 					c.devices[i - 1].end_blkaddr + 1;
314 			c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
315 					c.devices[i].total_segments *
316 					c.blks_per_seg - 1;
317 		}
318 		if (c.ndevs > 1) {
319 			memcpy(sb->devs[i].path, c.devices[i].path, MAX_PATH_LEN);
320 			sb->devs[i].total_segments =
321 					cpu_to_le32(c.devices[i].total_segments);
322 		}
323 
324 		c.total_segments += c.devices[i].total_segments;
325 	}
326 	set_sb(segment_count, (c.total_segments / c.segs_per_zone *
327 						c.segs_per_zone));
328 	set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
329 
330 	set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
331 			get_sb(segment_count_ckpt) * c.blks_per_seg);
332 
333 	blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
334 
335 	sit_segments = SEG_ALIGN(blocks_for_sit);
336 
337 	set_sb(segment_count_sit, sit_segments * 2);
338 
339 	set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
340 			c.blks_per_seg);
341 
342 	total_valid_blks_available = (get_sb(segment_count) -
343 			(get_sb(segment_count_ckpt) +
344 			get_sb(segment_count_sit))) * c.blks_per_seg;
345 
346 	blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
347 			NAT_ENTRY_PER_BLOCK);
348 
349 	if (c.large_nat_bitmap) {
350 		nat_segments = SEG_ALIGN(blocks_for_nat) *
351 						DEFAULT_NAT_ENTRY_RATIO / 100;
352 		set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
353 		max_nat_bitmap_size = (get_sb(segment_count_nat) <<
354 						log_blks_per_seg) / 8;
355 		set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
356 	} else {
357 		set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
358 		max_nat_bitmap_size = 0;
359 	}
360 
361 	/*
362 	 * The number of node segments should not be exceeded a "Threshold".
363 	 * This number resizes NAT bitmap area in a CP page.
364 	 * So the threshold is determined not to overflow one CP page
365 	 */
366 	sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
367 				log_blks_per_seg) / 8;
368 
369 	if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
370 		max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
371 	else
372 		max_sit_bitmap_size = sit_bitmap_size;
373 
374 	if (c.large_nat_bitmap) {
375 		/* use cp_payload if free space of f2fs_checkpoint is not enough */
376 		if (max_sit_bitmap_size + max_nat_bitmap_size >
377 						MAX_BITMAP_SIZE_IN_CKPT) {
378 			uint32_t diff =  max_sit_bitmap_size +
379 						max_nat_bitmap_size -
380 						MAX_BITMAP_SIZE_IN_CKPT;
381 			set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
382 		} else {
383 			set_sb(cp_payload, 0);
384 		}
385 	} else {
386 		/*
387 		 * It should be reserved minimum 1 segment for nat.
388 		 * When sit is too large, we should expand cp area.
389 		 * It requires more pages for cp.
390 		 */
391 		if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
392 			max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
393 			set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
394 	        } else {
395 			max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
396 							max_sit_bitmap_size;
397 			set_sb(cp_payload, 0);
398 		}
399 		max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
400 
401 		if (get_sb(segment_count_nat) > max_nat_segments)
402 			set_sb(segment_count_nat, max_nat_segments);
403 
404 		set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
405 	}
406 
407 	set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
408 			c.blks_per_seg);
409 
410 	total_valid_blks_available = (get_sb(segment_count) -
411 			(get_sb(segment_count_ckpt) +
412 			get_sb(segment_count_sit) +
413 			get_sb(segment_count_nat))) *
414 			c.blks_per_seg;
415 
416 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
417 		blocks_for_ssa = 0;
418 	else
419 		blocks_for_ssa = total_valid_blks_available /
420 				c.blks_per_seg + 1;
421 
422 	set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
423 
424 	total_meta_segments = get_sb(segment_count_ckpt) +
425 		get_sb(segment_count_sit) +
426 		get_sb(segment_count_nat) +
427 		get_sb(segment_count_ssa);
428 	diff = total_meta_segments % (c.segs_per_zone);
429 	if (diff)
430 		set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
431 			(c.segs_per_zone - diff));
432 
433 	total_meta_zones = ZONE_ALIGN(total_meta_segments *
434 						c.blks_per_seg);
435 
436 	set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
437 				c.segs_per_zone * c.blks_per_seg);
438 
439 	if (c.zoned_mode) {
440 		/*
441 		 * Make sure there is enough randomly writeable
442 		 * space at the beginning of the disk.
443 		 */
444 		unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
445 
446 		if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
447 				c.devices[0].nr_rnd_zones < main_blkzone) {
448 			MSG(0, "\tError: Device does not have enough random "
449 					"write zones for F2FS volume (%lu needed)\n",
450 					main_blkzone);
451 			return -1;
452 		}
453 		/*
454 		 * Check if conventional device has enough space
455 		 * to accommodate all metadata, zoned device should
456 		 * not overlap to metadata area.
457 		 */
458 		for (i = 1; i < c.ndevs; i++) {
459 			if (c.devices[i].zoned_model != F2FS_ZONED_NONE &&
460 				c.devices[i].start_blkaddr < get_sb(main_blkaddr)) {
461 				MSG(0, "\tError: Conventional device %s is too small,"
462 					" (%"PRIu64" MiB needed).\n", c.devices[0].path,
463 					(get_sb(main_blkaddr) -
464 					c.devices[i].start_blkaddr) >> 8);
465 				return -1;
466 			}
467 		}
468 	}
469 
470 	total_zones = get_sb(segment_count) / (c.segs_per_zone) -
471 							total_meta_zones;
472 	if (total_zones == 0)
473 		goto too_small;
474 	set_sb(section_count, total_zones * c.secs_per_zone);
475 
476 	set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
477 
478 	/*
479 	 * Let's determine the best reserved and overprovisioned space.
480 	 * For Zoned device, if zone capacity less than zone size, the segments
481 	 * starting after the zone capacity are unusable in each zone. So get
482 	 * overprovision ratio and reserved seg count based on avg usable
483 	 * segs_per_sec.
484 	 */
485 	if (c.overprovision == 0)
486 		c.overprovision = get_best_overprovision(sb);
487 
488 	c.reserved_segments =
489 			(2 * (100 / c.overprovision + 1) + NR_CURSEG_TYPE) *
490 			round_up(f2fs_get_usable_segments(sb), get_sb(section_count));
491 
492 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
493 		c.overprovision = 0;
494 		c.reserved_segments = 0;
495 	}
496 	if ((!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)) &&
497 		c.overprovision == 0) ||
498 		c.total_segments < F2FS_MIN_SEGMENTS ||
499 		(c.devices[0].total_sectors *
500 			c.sector_size < zone_align_start_offset) ||
501 		(get_sb(segment_count_main) - NR_CURSEG_TYPE) <
502 						c.reserved_segments) {
503 		goto too_small;
504 	}
505 
506 	if (c.vol_uuid) {
507 		if (uuid_parse(c.vol_uuid, sb->uuid)) {
508 			MSG(0, "\tError: supplied string is not a valid UUID\n");
509 			return -1;
510 		}
511 	} else {
512 		uuid_generate(sb->uuid);
513 	}
514 
515 	/* precompute checksum seed for metadata */
516 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
517 		c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
518 
519 	utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
520 				MAX_VOLUME_NAME, strlen(c.vol_label));
521 	set_sb(node_ino, 1);
522 	set_sb(meta_ino, 2);
523 	set_sb(root_ino, 3);
524 	c.next_free_nid = 4;
525 
526 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
527 		if (!((1 << qtype) & c.quota_bits))
528 			continue;
529 		sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
530 		MSG(0, "Info: add quota type = %u => %u\n",
531 					qtype, c.next_free_nid - 1);
532 	}
533 
534 	if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND))
535 		c.lpf_ino = c.next_free_nid++;
536 
537 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
538 		avail_zones = 2;
539 	else
540 		avail_zones = 6;
541 
542 	if (total_zones <= avail_zones) {
543 		MSG(1, "\tError: %d zones: Need more zones "
544 			"by shrinking zone size\n", total_zones);
545 		return -1;
546 	}
547 
548 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
549 		c.cur_seg[CURSEG_HOT_NODE] = last_section(last_zone(total_zones));
550 		c.cur_seg[CURSEG_WARM_NODE] = 0;
551 		c.cur_seg[CURSEG_COLD_NODE] = 0;
552 		c.cur_seg[CURSEG_HOT_DATA] = 0;
553 		c.cur_seg[CURSEG_COLD_DATA] = 0;
554 		c.cur_seg[CURSEG_WARM_DATA] = 0;
555 	} else if (c.heap) {
556 		c.cur_seg[CURSEG_HOT_NODE] =
557 				last_section(last_zone(total_zones));
558 		c.cur_seg[CURSEG_WARM_NODE] = prev_zone(CURSEG_HOT_NODE);
559 		c.cur_seg[CURSEG_COLD_NODE] = prev_zone(CURSEG_WARM_NODE);
560 		c.cur_seg[CURSEG_HOT_DATA] = prev_zone(CURSEG_COLD_NODE);
561 		c.cur_seg[CURSEG_COLD_DATA] = 0;
562 		c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_COLD_DATA);
563 	} else if (c.zoned_mode) {
564 		c.cur_seg[CURSEG_HOT_NODE] = 0;
565 		c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
566 		c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
567 		c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
568 		c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_HOT_DATA);
569 		c.cur_seg[CURSEG_COLD_DATA] = next_zone(CURSEG_WARM_DATA);
570 	} else {
571 		c.cur_seg[CURSEG_HOT_NODE] = 0;
572 		c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
573 		c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
574 		c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
575 		c.cur_seg[CURSEG_COLD_DATA] =
576 				max(last_zone((total_zones >> 2)),
577 					next_zone(CURSEG_HOT_DATA));
578 		c.cur_seg[CURSEG_WARM_DATA] =
579 				max(last_zone((total_zones >> 1)),
580 					next_zone(CURSEG_COLD_DATA));
581 	}
582 
583 	/* if there is redundancy, reassign it */
584 	if (!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)))
585 		verify_cur_segs();
586 
587 	cure_extension_list();
588 
589 	/* get kernel version */
590 	if (c.kd >= 0) {
591 		dev_read_version(c.version, 0, VERSION_LEN);
592 		get_kernel_version(c.version);
593 	} else {
594 		get_kernel_uname_version(c.version);
595 	}
596 	MSG(0, "Info: format version with\n  \"%s\"\n", c.version);
597 
598 	memcpy(sb->version, c.version, VERSION_LEN);
599 	memcpy(sb->init_version, c.version, VERSION_LEN);
600 
601 	if (c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) {
602 		set_sb(s_encoding, c.s_encoding);
603 		set_sb(s_encoding_flags, c.s_encoding_flags);
604 	}
605 
606 	sb->feature = c.feature;
607 
608 	if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
609 		set_sb(checksum_offset, SB_CHKSUM_OFFSET);
610 		set_sb(crc, f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
611 						SB_CHKSUM_OFFSET));
612 		MSG(1, "Info: SB CRC is set: offset (%d), crc (0x%x)\n",
613 					get_sb(checksum_offset), get_sb(crc));
614 	}
615 
616 	return 0;
617 
618 too_small:
619 	MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
620 	return -1;
621 }
622 
f2fs_init_sit_area(void)623 static int f2fs_init_sit_area(void)
624 {
625 	uint32_t blk_size, seg_size;
626 	uint32_t index = 0;
627 	uint64_t sit_seg_addr = 0;
628 	uint8_t *zero_buf = NULL;
629 
630 	blk_size = 1 << get_sb(log_blocksize);
631 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
632 
633 	zero_buf = calloc(sizeof(uint8_t), seg_size);
634 	if(zero_buf == NULL) {
635 		MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
636 		return -1;
637 	}
638 
639 	sit_seg_addr = get_sb(sit_blkaddr);
640 	sit_seg_addr *= blk_size;
641 
642 	DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
643 	for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
644 		if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
645 			MSG(1, "\tError: While zeroing out the sit area "
646 					"on disk!!!\n");
647 			free(zero_buf);
648 			return -1;
649 		}
650 		sit_seg_addr += seg_size;
651 	}
652 
653 	free(zero_buf);
654 	return 0 ;
655 }
656 
f2fs_init_nat_area(void)657 static int f2fs_init_nat_area(void)
658 {
659 	uint32_t blk_size, seg_size;
660 	uint32_t index = 0;
661 	uint64_t nat_seg_addr = 0;
662 	uint8_t *nat_buf = NULL;
663 
664 	blk_size = 1 << get_sb(log_blocksize);
665 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
666 
667 	nat_buf = calloc(sizeof(uint8_t), seg_size);
668 	if (nat_buf == NULL) {
669 		MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
670 		return -1;
671 	}
672 
673 	nat_seg_addr = get_sb(nat_blkaddr);
674 	nat_seg_addr *= blk_size;
675 
676 	DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
677 	for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
678 		if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
679 			MSG(1, "\tError: While zeroing out the nat area "
680 					"on disk!!!\n");
681 			free(nat_buf);
682 			return -1;
683 		}
684 		nat_seg_addr = nat_seg_addr + (2 * seg_size);
685 	}
686 
687 	free(nat_buf);
688 	return 0 ;
689 }
690 
f2fs_write_check_point_pack(void)691 static int f2fs_write_check_point_pack(void)
692 {
693 	struct f2fs_summary_block *sum = NULL;
694 	struct f2fs_journal *journal;
695 	uint32_t blk_size_bytes;
696 	uint32_t nat_bits_bytes, nat_bits_blocks;
697 	unsigned char *nat_bits = NULL, *empty_nat_bits;
698 	uint64_t cp_seg_blk = 0;
699 	uint32_t crc = 0, flags;
700 	unsigned int i;
701 	char *cp_payload = NULL;
702 	char *sum_compact, *sum_compact_p;
703 	struct f2fs_summary *sum_entry;
704 	enum quota_type qtype;
705 	int off;
706 	int ret = -1;
707 
708 	cp = calloc(F2FS_BLKSIZE, 1);
709 	if (cp == NULL) {
710 		MSG(1, "\tError: Calloc failed for f2fs_checkpoint!!!\n");
711 		return ret;
712 	}
713 
714 	sum = calloc(F2FS_BLKSIZE, 1);
715 	if (sum == NULL) {
716 		MSG(1, "\tError: Calloc failed for summary_node!!!\n");
717 		goto free_cp;
718 	}
719 
720 	sum_compact = calloc(F2FS_BLKSIZE, 1);
721 	if (sum_compact == NULL) {
722 		MSG(1, "\tError: Calloc failed for summary buffer!!!\n");
723 		goto free_sum;
724 	}
725 	sum_compact_p = sum_compact;
726 
727 	nat_bits_bytes = get_sb(segment_count_nat) << 5;
728 	nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
729 						F2FS_BLKSIZE - 1);
730 	nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
731 	if (nat_bits == NULL) {
732 		MSG(1, "\tError: Calloc failed for nat bits buffer!!!\n");
733 		goto free_sum_compact;
734 	}
735 
736 	cp_payload = calloc(F2FS_BLKSIZE, 1);
737 	if (cp_payload == NULL) {
738 		MSG(1, "\tError: Calloc failed for cp_payload!!!\n");
739 		goto free_nat_bits;
740 	}
741 
742 	/* 1. cp page 1 of checkpoint pack 1 */
743 	srand((c.fake_seed) ? 0 : time(NULL));
744 	cp->checkpoint_ver = cpu_to_le64(rand() | 0x1);
745 	set_cp(cur_node_segno[0], c.cur_seg[CURSEG_HOT_NODE]);
746 	set_cp(cur_node_segno[1], c.cur_seg[CURSEG_WARM_NODE]);
747 	set_cp(cur_node_segno[2], c.cur_seg[CURSEG_COLD_NODE]);
748 	set_cp(cur_data_segno[0], c.cur_seg[CURSEG_HOT_DATA]);
749 	set_cp(cur_data_segno[1], c.cur_seg[CURSEG_WARM_DATA]);
750 	set_cp(cur_data_segno[2], c.cur_seg[CURSEG_COLD_DATA]);
751 	for (i = 3; i < MAX_ACTIVE_NODE_LOGS; i++) {
752 		set_cp(cur_node_segno[i], 0xffffffff);
753 		set_cp(cur_data_segno[i], 0xffffffff);
754 	}
755 
756 	set_cp(cur_node_blkoff[0], 1 + c.quota_inum + c.lpf_inum);
757 	set_cp(cur_data_blkoff[0], 1 + c.quota_dnum + c.lpf_dnum);
758 	set_cp(valid_block_count, 2 + c.quota_inum + c.quota_dnum +
759 			c.lpf_inum + c.lpf_dnum);
760 	set_cp(rsvd_segment_count, c.reserved_segments);
761 
762 	/*
763 	 * For zoned devices, if zone capacity less than zone size, get
764 	 * overprovision segment count based on usable segments in the device.
765 	 */
766 	set_cp(overprov_segment_count, (f2fs_get_usable_segments(sb) -
767 			get_cp(rsvd_segment_count)) *
768 			c.overprovision / 100);
769 	set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
770 			get_cp(rsvd_segment_count));
771 
772 	if (f2fs_get_usable_segments(sb) <= get_cp(overprov_segment_count)) {
773 		MSG(0, "\tError: Not enough segments to create F2FS Volume\n");
774 		goto free_cp_payload;
775 	}
776 	MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
777 	MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
778 					get_cp(overprov_segment_count),
779 					c.reserved_segments);
780 
781 	/* main segments - reserved segments - (node + data segments) */
782 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
783 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
784 		set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
785 			get_cp(overprov_segment_count)) * c.blks_per_seg));
786 	} else {
787 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 6);
788 		set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
789 			get_cp(overprov_segment_count)) * c.blks_per_seg));
790 	}
791 	/* cp page (2), data summaries (1), node summaries (3) */
792 	set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
793 	flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
794 	if (get_cp(cp_pack_total_block_count) <=
795 			(1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
796 		flags |= CP_NAT_BITS_FLAG;
797 
798 	if (c.trimmed)
799 		flags |= CP_TRIMMED_FLAG;
800 
801 	if (c.large_nat_bitmap)
802 		flags |= CP_LARGE_NAT_BITMAP_FLAG;
803 
804 	set_cp(ckpt_flags, flags);
805 	set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
806 	set_cp(valid_node_count, 1 + c.quota_inum + c.lpf_inum);
807 	set_cp(valid_inode_count, 1 + c.quota_inum + c.lpf_inum);
808 	set_cp(next_free_nid, c.next_free_nid);
809 	set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
810 			get_sb(log_blocks_per_seg)) / 8);
811 
812 	set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
813 			 get_sb(log_blocks_per_seg)) / 8);
814 
815 	if (c.large_nat_bitmap)
816 		set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
817 	else
818 		set_cp(checksum_offset, CP_CHKSUM_OFFSET);
819 
820 	crc = f2fs_checkpoint_chksum(cp);
821 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
822 							cpu_to_le32(crc);
823 
824 	blk_size_bytes = 1 << get_sb(log_blocksize);
825 
826 	if (blk_size_bytes != F2FS_BLKSIZE) {
827 		MSG(1, "\tError: Wrong block size %d / %d!!!\n",
828 					blk_size_bytes, F2FS_BLKSIZE);
829 		goto free_cp_payload;
830 	}
831 
832 	cp_seg_blk = get_sb(segment0_blkaddr);
833 
834 	DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
835 						cp_seg_blk);
836 	if (dev_write_block(cp, cp_seg_blk)) {
837 		MSG(1, "\tError: While writing the cp to disk!!!\n");
838 		goto free_cp_payload;
839 	}
840 
841 	for (i = 0; i < get_sb(cp_payload); i++) {
842 		cp_seg_blk++;
843 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
844 			MSG(1, "\tError: While zeroing out the sit bitmap area "
845 					"on disk!!!\n");
846 			goto free_cp_payload;
847 		}
848 	}
849 
850 	/* Prepare and write Segment summary for HOT/WARM/COLD DATA
851 	 *
852 	 * The structure of compact summary
853 	 * +-------------------+
854 	 * | nat_journal       |
855 	 * +-------------------+
856 	 * | sit_journal       |
857 	 * +-------------------+
858 	 * | hot data summary  |
859 	 * +-------------------+
860 	 * | warm data summary |
861 	 * +-------------------+
862 	 * | cold data summary |
863 	 * +-------------------+
864 	*/
865 	memset(sum, 0, sizeof(struct f2fs_summary_block));
866 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
867 
868 	journal = &sum->journal;
869 	journal->n_nats = cpu_to_le16(1 + c.quota_inum + c.lpf_inum);
870 	journal->nat_j.entries[0].nid = sb->root_ino;
871 	journal->nat_j.entries[0].ne.version = 0;
872 	journal->nat_j.entries[0].ne.ino = sb->root_ino;
873 	journal->nat_j.entries[0].ne.block_addr = cpu_to_le32(
874 			get_sb(main_blkaddr) +
875 			get_cp(cur_node_segno[0]) * c.blks_per_seg);
876 
877 	for (qtype = 0, i = 1; qtype < F2FS_MAX_QUOTAS; qtype++) {
878 		if (!((1 << qtype) & c.quota_bits))
879 			continue;
880 		journal->nat_j.entries[i].nid = sb->qf_ino[qtype];
881 		journal->nat_j.entries[i].ne.version = 0;
882 		journal->nat_j.entries[i].ne.ino = sb->qf_ino[qtype];
883 		journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
884 				get_sb(main_blkaddr) +
885 				get_cp(cur_node_segno[0]) *
886 				c.blks_per_seg + i);
887 		i++;
888 	}
889 
890 	if (c.lpf_inum) {
891 		journal->nat_j.entries[i].nid = cpu_to_le32(c.lpf_ino);
892 		journal->nat_j.entries[i].ne.version = 0;
893 		journal->nat_j.entries[i].ne.ino = cpu_to_le32(c.lpf_ino);
894 		journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
895 				get_sb(main_blkaddr) +
896 				get_cp(cur_node_segno[0]) *
897 				c.blks_per_seg + i);
898 	}
899 
900 	memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
901 	sum_compact_p += SUM_JOURNAL_SIZE;
902 
903 	memset(sum, 0, sizeof(struct f2fs_summary_block));
904 
905 	/* inode sit for root */
906 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
907 		journal->n_sits = cpu_to_le16(2);
908 	else
909 		journal->n_sits = cpu_to_le16(6);
910 
911 	journal->sit_j.entries[0].segno = cp->cur_node_segno[0];
912 	journal->sit_j.entries[0].se.vblocks =
913 				cpu_to_le16((CURSEG_HOT_NODE << 10) |
914 						(1 + c.quota_inum + c.lpf_inum));
915 	f2fs_set_bit(0, (char *)journal->sit_j.entries[0].se.valid_map);
916 	for (i = 1; i <= c.quota_inum; i++)
917 		f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
918 	if (c.lpf_inum)
919 		f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
920 
921 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
922 		/* data sit for root */
923 		journal->sit_j.entries[1].segno = cp->cur_data_segno[0];
924 		journal->sit_j.entries[1].se.vblocks =
925 					cpu_to_le16((CURSEG_HOT_DATA << 10) |
926 							(1 + c.quota_dnum + c.lpf_dnum));
927 		f2fs_set_bit(0, (char *)journal->sit_j.entries[1].se.valid_map);
928 		for (i = 1; i <= c.quota_dnum; i++)
929 			f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
930 		if (c.lpf_dnum)
931 			f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
932 	} else {
933 		journal->sit_j.entries[1].segno = cp->cur_node_segno[1];
934 		journal->sit_j.entries[1].se.vblocks =
935 					cpu_to_le16((CURSEG_WARM_NODE << 10));
936 		journal->sit_j.entries[2].segno = cp->cur_node_segno[2];
937 		journal->sit_j.entries[2].se.vblocks =
938 					cpu_to_le16((CURSEG_COLD_NODE << 10));
939 
940 		/* data sit for root */
941 		journal->sit_j.entries[3].segno = cp->cur_data_segno[0];
942 		journal->sit_j.entries[3].se.vblocks =
943 					cpu_to_le16((CURSEG_HOT_DATA << 10) |
944 							(1 + c.quota_dnum + c.lpf_dnum));
945 		f2fs_set_bit(0, (char *)journal->sit_j.entries[3].se.valid_map);
946 		for (i = 1; i <= c.quota_dnum; i++)
947 			f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
948 		if (c.lpf_dnum)
949 			f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
950 
951 		journal->sit_j.entries[4].segno = cp->cur_data_segno[1];
952 		journal->sit_j.entries[4].se.vblocks =
953 					cpu_to_le16((CURSEG_WARM_DATA << 10));
954 		journal->sit_j.entries[5].segno = cp->cur_data_segno[2];
955 		journal->sit_j.entries[5].se.vblocks =
956 					cpu_to_le16((CURSEG_COLD_DATA << 10));
957 	}
958 
959 	memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
960 	sum_compact_p += SUM_JOURNAL_SIZE;
961 
962 	/* hot data summary */
963 	sum_entry = (struct f2fs_summary *)sum_compact_p;
964 	sum_entry->nid = sb->root_ino;
965 	sum_entry->ofs_in_node = 0;
966 
967 	off = 1;
968 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
969 		int j;
970 
971 		if (!((1 << qtype) & c.quota_bits))
972 			continue;
973 
974 		for (j = 0; j < QUOTA_DATA(qtype); j++) {
975 			(sum_entry + off + j)->nid = sb->qf_ino[qtype];
976 			(sum_entry + off + j)->ofs_in_node = cpu_to_le16(j);
977 		}
978 		off += QUOTA_DATA(qtype);
979 	}
980 
981 	if (c.lpf_dnum) {
982 		(sum_entry + off)->nid = cpu_to_le32(c.lpf_ino);
983 		(sum_entry + off)->ofs_in_node = 0;
984 	}
985 
986 	/* warm data summary, nothing to do */
987 	/* cold data summary, nothing to do */
988 
989 	cp_seg_blk++;
990 	DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
991 			cp_seg_blk);
992 	if (dev_write_block(sum_compact, cp_seg_blk)) {
993 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
994 		goto free_cp_payload;
995 	}
996 
997 	/* Prepare and write Segment summary for HOT_NODE */
998 	memset(sum, 0, sizeof(struct f2fs_summary_block));
999 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1000 
1001 	sum->entries[0].nid = sb->root_ino;
1002 	sum->entries[0].ofs_in_node = 0;
1003 	for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1004 		if (!((1 << qtype) & c.quota_bits))
1005 			continue;
1006 		sum->entries[1 + i].nid = sb->qf_ino[qtype];
1007 		sum->entries[1 + i].ofs_in_node = 0;
1008 		i++;
1009 	}
1010 	if (c.lpf_inum) {
1011 		i++;
1012 		sum->entries[i].nid = cpu_to_le32(c.lpf_ino);
1013 		sum->entries[i].ofs_in_node = 0;
1014 	}
1015 
1016 	cp_seg_blk++;
1017 	DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
1018 			cp_seg_blk);
1019 	if (dev_write_block(sum, cp_seg_blk)) {
1020 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1021 		goto free_cp_payload;
1022 	}
1023 
1024 	/* Fill segment summary for WARM_NODE to zero. */
1025 	memset(sum, 0, sizeof(struct f2fs_summary_block));
1026 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1027 
1028 	cp_seg_blk++;
1029 	DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
1030 			cp_seg_blk);
1031 	if (dev_write_block(sum, cp_seg_blk)) {
1032 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1033 		goto free_cp_payload;
1034 	}
1035 
1036 	/* Fill segment summary for COLD_NODE to zero. */
1037 	memset(sum, 0, sizeof(struct f2fs_summary_block));
1038 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1039 	cp_seg_blk++;
1040 	DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
1041 			cp_seg_blk);
1042 	if (dev_write_block(sum, cp_seg_blk)) {
1043 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1044 		goto free_cp_payload;
1045 	}
1046 
1047 	/* cp page2 */
1048 	cp_seg_blk++;
1049 	DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
1050 	if (dev_write_block(cp, cp_seg_blk)) {
1051 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1052 		goto free_cp_payload;
1053 	}
1054 
1055 	/* write NAT bits, if possible */
1056 	if (flags & CP_NAT_BITS_FLAG) {
1057 		uint32_t i;
1058 
1059 		*(__le64 *)nat_bits = get_cp_crc(cp);
1060 		empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1061 		memset(empty_nat_bits, 0xff, nat_bits_bytes);
1062 		test_and_clear_bit_le(0, empty_nat_bits);
1063 
1064 		/* write the last blocks in cp pack */
1065 		cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1066 				get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1067 
1068 		DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1069 					cp_seg_blk);
1070 
1071 		for (i = 0; i < nat_bits_blocks; i++) {
1072 			if (dev_write_block(nat_bits + i *
1073 						F2FS_BLKSIZE, cp_seg_blk + i)) {
1074 				MSG(1, "\tError: write NAT bits to disk!!!\n");
1075 				goto free_cp_payload;
1076 			}
1077 		}
1078 	}
1079 
1080 	/* cp page 1 of check point pack 2
1081 	 * Initialize other checkpoint pack with version zero
1082 	 */
1083 	cp->checkpoint_ver = 0;
1084 
1085 	crc = f2fs_checkpoint_chksum(cp);
1086 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1087 							cpu_to_le32(crc);
1088 	cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1089 	DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1090 				cp_seg_blk);
1091 	if (dev_write_block(cp, cp_seg_blk)) {
1092 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1093 		goto free_cp_payload;
1094 	}
1095 
1096 	for (i = 0; i < get_sb(cp_payload); i++) {
1097 		cp_seg_blk++;
1098 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
1099 			MSG(1, "\tError: While zeroing out the sit bitmap area "
1100 					"on disk!!!\n");
1101 			goto free_cp_payload;
1102 		}
1103 	}
1104 
1105 	/* cp page 2 of check point pack 2 */
1106 	cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1107 					get_sb(cp_payload) - 1);
1108 	DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1109 				cp_seg_blk);
1110 	if (dev_write_block(cp, cp_seg_blk)) {
1111 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1112 		goto free_cp_payload;
1113 	}
1114 
1115 	ret = 0;
1116 
1117 free_cp_payload:
1118 	free(cp_payload);
1119 free_nat_bits:
1120 	free(nat_bits);
1121 free_sum_compact:
1122 	free(sum_compact);
1123 free_sum:
1124 	free(sum);
1125 free_cp:
1126 	free(cp);
1127 	return ret;
1128 }
1129 
f2fs_write_super_block(void)1130 static int f2fs_write_super_block(void)
1131 {
1132 	int index;
1133 	uint8_t *zero_buff;
1134 
1135 	zero_buff = calloc(F2FS_BLKSIZE, 1);
1136 	if (zero_buff == NULL) {
1137 		MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1138 		return -1;
1139 	}
1140 
1141 	memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1142 	DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1143 	for (index = 0; index < 2; index++) {
1144 		if (dev_write_block(zero_buff, index)) {
1145 			MSG(1, "\tError: While while writing super_blk "
1146 					"on disk!!! index : %d\n", index);
1147 			free(zero_buff);
1148 			return -1;
1149 		}
1150 	}
1151 
1152 	free(zero_buff);
1153 	return 0;
1154 }
1155 
1156 #ifndef WITH_ANDROID
f2fs_discard_obsolete_dnode(void)1157 static int f2fs_discard_obsolete_dnode(void)
1158 {
1159 	struct f2fs_node *raw_node;
1160 	uint64_t next_blkaddr = 0, offset;
1161 	u64 end_blkaddr = (get_sb(segment_count_main) <<
1162 			get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1163 	uint64_t start_inode_pos = get_sb(main_blkaddr);
1164 	uint64_t last_inode_pos;
1165 
1166 	if (c.zoned_mode || c.feature & cpu_to_le32(F2FS_FEATURE_RO))
1167 		return 0;
1168 
1169 	raw_node = calloc(sizeof(struct f2fs_node), 1);
1170 	if (raw_node == NULL) {
1171 		MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1172 		return -1;
1173 	}
1174 
1175 	/* avoid power-off-recovery based on roll-forward policy */
1176 	offset = get_sb(main_blkaddr);
1177 	offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1178 
1179 	last_inode_pos = start_inode_pos +
1180 		c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg + c.quota_inum + c.lpf_inum;
1181 
1182 	do {
1183 		if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1184 			break;
1185 
1186 		if (dev_read_block(raw_node, offset)) {
1187 			MSG(1, "\tError: While traversing direct node!!!\n");
1188 			free(raw_node);
1189 			return -1;
1190 		}
1191 
1192 		next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
1193 		memset(raw_node, 0, F2FS_BLKSIZE);
1194 
1195 		DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1196 		if (dev_write_block(raw_node, offset)) {
1197 			MSG(1, "\tError: While discarding direct node!!!\n");
1198 			free(raw_node);
1199 			return -1;
1200 		}
1201 		offset = next_blkaddr;
1202 		/* should avoid recursive chain due to stale data */
1203 		if (offset >= start_inode_pos || offset <= last_inode_pos)
1204 			break;
1205 	} while (1);
1206 
1207 	free(raw_node);
1208 	return 0;
1209 }
1210 #endif
1211 
f2fs_write_root_inode(void)1212 static int f2fs_write_root_inode(void)
1213 {
1214 	struct f2fs_node *raw_node = NULL;
1215 	uint64_t blk_size_bytes, data_blk_nor;
1216 	uint64_t main_area_node_seg_blk_offset = 0;
1217 
1218 	raw_node = calloc(F2FS_BLKSIZE, 1);
1219 	if (raw_node == NULL) {
1220 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1221 		return -1;
1222 	}
1223 
1224 	raw_node->footer.nid = sb->root_ino;
1225 	raw_node->footer.ino = sb->root_ino;
1226 	raw_node->footer.cp_ver = cpu_to_le64(1);
1227 	raw_node->footer.next_blkaddr = cpu_to_le32(
1228 			get_sb(main_blkaddr) +
1229 			c.cur_seg[CURSEG_HOT_NODE] *
1230 			c.blks_per_seg + 1);
1231 
1232 	raw_node->i.i_mode = cpu_to_le16(0x41ed);
1233 	if (c.lpf_ino)
1234 		raw_node->i.i_links = cpu_to_le32(3);
1235 	else
1236 		raw_node->i.i_links = cpu_to_le32(2);
1237 	raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1238 	raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1239 
1240 	blk_size_bytes = 1 << get_sb(log_blocksize);
1241 	raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
1242 	raw_node->i.i_blocks = cpu_to_le64(2);
1243 
1244 	raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1245 	raw_node->i.i_atime_nsec = 0;
1246 	raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1247 	raw_node->i.i_ctime_nsec = 0;
1248 	raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1249 	raw_node->i.i_mtime_nsec = 0;
1250 	raw_node->i.i_generation = 0;
1251 	raw_node->i.i_xattr_nid = 0;
1252 	raw_node->i.i_flags = 0;
1253 	raw_node->i.i_current_depth = cpu_to_le32(1);
1254 	raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1255 
1256 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1257 		raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1258 		raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1259 	}
1260 
1261 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1262 		raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1263 
1264 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1265 		raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1266 		raw_node->i.i_crtime_nsec = 0;
1267 	}
1268 
1269 	if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1270 		raw_node->i.i_compress_algrithm = 0;
1271 		raw_node->i.i_log_cluster_size = 0;
1272 		raw_node->i.i_padding = 0;
1273 	}
1274 
1275 	data_blk_nor = get_sb(main_blkaddr) +
1276 		c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg;
1277 	raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1278 
1279 	raw_node->i.i_ext.fofs = 0;
1280 	raw_node->i.i_ext.blk_addr = 0;
1281 	raw_node->i.i_ext.len = 0;
1282 
1283 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1284 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1285 					c.blks_per_seg;
1286 
1287 	DBG(1, "\tWriting root inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1288 			get_sb(main_blkaddr),
1289 			c.cur_seg[CURSEG_HOT_NODE],
1290 			c.blks_per_seg, main_area_node_seg_blk_offset);
1291 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1292 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1293 		free(raw_node);
1294 		return -1;
1295 	}
1296 
1297 	free(raw_node);
1298 	return 0;
1299 }
1300 
f2fs_write_default_quota(int qtype,unsigned int blkaddr,__le32 raw_id)1301 static int f2fs_write_default_quota(int qtype, unsigned int blkaddr,
1302 						__le32 raw_id)
1303 {
1304 	char *filebuf = calloc(F2FS_BLKSIZE, 2);
1305 	int file_magics[] = INITQMAGICS;
1306 	struct v2_disk_dqheader ddqheader;
1307 	struct v2_disk_dqinfo ddqinfo;
1308 	struct v2r1_disk_dqblk dqblk;
1309 
1310 	if (filebuf == NULL) {
1311 		MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1312 		return -1;
1313 	}
1314 
1315 	/* Write basic quota header */
1316 	ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1317 	/* only support QF_VFSV1 */
1318 	ddqheader.dqh_version = cpu_to_le32(1);
1319 
1320 	memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1321 
1322 	/* Fill Initial quota file content */
1323 	ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1324 	ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1325 	ddqinfo.dqi_flags = cpu_to_le32(0);
1326 	ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1327 	ddqinfo.dqi_free_blk = cpu_to_le32(0);
1328 	ddqinfo.dqi_free_entry = cpu_to_le32(5);
1329 
1330 	memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1331 
1332 	filebuf[1024] = 2;
1333 	filebuf[2048] = 3;
1334 	filebuf[3072] = 4;
1335 	filebuf[4096] = 5;
1336 
1337 	filebuf[5120 + 8] = 1;
1338 
1339 	dqblk.dqb_id = raw_id;
1340 	dqblk.dqb_pad = cpu_to_le32(0);
1341 	dqblk.dqb_ihardlimit = cpu_to_le64(0);
1342 	dqblk.dqb_isoftlimit = cpu_to_le64(0);
1343 	if (c.lpf_ino)
1344 		dqblk.dqb_curinodes = cpu_to_le64(2);
1345 	else
1346 		dqblk.dqb_curinodes = cpu_to_le64(1);
1347 	dqblk.dqb_bhardlimit = cpu_to_le64(0);
1348 	dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1349 	if (c.lpf_ino)
1350 		dqblk.dqb_curspace = cpu_to_le64(8192);
1351 	else
1352 		dqblk.dqb_curspace = cpu_to_le64(4096);
1353 	dqblk.dqb_btime = cpu_to_le64(0);
1354 	dqblk.dqb_itime = cpu_to_le64(0);
1355 
1356 	memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1357 
1358 	/* Write two blocks */
1359 	if (dev_write_block(filebuf, blkaddr) ||
1360 	    dev_write_block(filebuf + F2FS_BLKSIZE, blkaddr + 1)) {
1361 		MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1362 		free(filebuf);
1363 		return -1;
1364 	}
1365 	DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
1366 					blkaddr, blkaddr + 1);
1367 	free(filebuf);
1368 	c.quota_dnum += QUOTA_DATA(qtype);
1369 	return 0;
1370 }
1371 
f2fs_write_qf_inode(int qtype,int offset)1372 static int f2fs_write_qf_inode(int qtype, int offset)
1373 {
1374 	struct f2fs_node *raw_node = NULL;
1375 	uint64_t data_blk_nor;
1376 	uint64_t main_area_node_seg_blk_offset = 0;
1377 	__le32 raw_id;
1378 	int i;
1379 
1380 	raw_node = calloc(F2FS_BLKSIZE, 1);
1381 	if (raw_node == NULL) {
1382 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1383 		return -1;
1384 	}
1385 	f2fs_init_qf_inode(sb, raw_node, qtype, mkfs_time);
1386 
1387 	raw_node->footer.next_blkaddr = cpu_to_le32(
1388 			get_sb(main_blkaddr) +
1389 			c.cur_seg[CURSEG_HOT_NODE] *
1390 			c.blks_per_seg + 1 + qtype + 1);
1391 	raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
1392 
1393 	data_blk_nor = get_sb(main_blkaddr) +
1394 		c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg + 1
1395 		+ offset * QUOTA_DATA(i);
1396 
1397 	if (qtype == 0)
1398 		raw_id = raw_node->i.i_uid;
1399 	else if (qtype == 1)
1400 		raw_id = raw_node->i.i_gid;
1401 	else if (qtype == 2)
1402 		raw_id = raw_node->i.i_projid;
1403 	else
1404 		ASSERT(0);
1405 
1406 	/* write two blocks */
1407 	if (f2fs_write_default_quota(qtype, data_blk_nor, raw_id)) {
1408 		free(raw_node);
1409 		return -1;
1410 	}
1411 
1412 	for (i = 0; i < QUOTA_DATA(qtype); i++)
1413 		raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1414 					cpu_to_le32(data_blk_nor + i);
1415 
1416 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1417 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1418 					c.blks_per_seg + offset + 1;
1419 
1420 	DBG(1, "\tWriting quota inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1421 			get_sb(main_blkaddr),
1422 			c.cur_seg[CURSEG_HOT_NODE],
1423 			c.blks_per_seg, main_area_node_seg_blk_offset);
1424 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1425 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1426 		free(raw_node);
1427 		return -1;
1428 	}
1429 
1430 	free(raw_node);
1431 	c.quota_inum++;
1432 	return 0;
1433 }
1434 
f2fs_update_nat_root(void)1435 static int f2fs_update_nat_root(void)
1436 {
1437 	struct f2fs_nat_block *nat_blk = NULL;
1438 	uint64_t nat_seg_blk_offset = 0;
1439 	enum quota_type qtype;
1440 	int i;
1441 
1442 	nat_blk = calloc(F2FS_BLKSIZE, 1);
1443 	if(nat_blk == NULL) {
1444 		MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1445 		return -1;
1446 	}
1447 
1448 	/* update quota */
1449 	for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1450 		if (!((1 << qtype) & c.quota_bits))
1451 			continue;
1452 		nat_blk->entries[sb->qf_ino[qtype]].block_addr =
1453 				cpu_to_le32(get_sb(main_blkaddr) +
1454 				c.cur_seg[CURSEG_HOT_NODE] *
1455 				c.blks_per_seg + i + 1);
1456 		nat_blk->entries[sb->qf_ino[qtype]].ino = sb->qf_ino[qtype];
1457 		i++;
1458 	}
1459 
1460 	/* update root */
1461 	nat_blk->entries[get_sb(root_ino)].block_addr = cpu_to_le32(
1462 		get_sb(main_blkaddr) +
1463 		c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg);
1464 	nat_blk->entries[get_sb(root_ino)].ino = sb->root_ino;
1465 
1466 	/* update node nat */
1467 	nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
1468 	nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
1469 
1470 	/* update meta nat */
1471 	nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
1472 	nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
1473 
1474 	nat_seg_blk_offset = get_sb(nat_blkaddr);
1475 
1476 	DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
1477 					nat_seg_blk_offset);
1478 	if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
1479 		MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
1480 		free(nat_blk);
1481 		return -1;
1482 	}
1483 
1484 	free(nat_blk);
1485 	return 0;
1486 }
1487 
f2fs_add_default_dentry_lpf(void)1488 static block_t f2fs_add_default_dentry_lpf(void)
1489 {
1490 	struct f2fs_dentry_block *dent_blk;
1491 	uint64_t data_blk_offset;
1492 
1493 	dent_blk = calloc(F2FS_BLKSIZE, 1);
1494 	if (dent_blk == NULL) {
1495 		MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1496 		return 0;
1497 	}
1498 
1499 	dent_blk->dentry[0].hash_code = 0;
1500 	dent_blk->dentry[0].ino = cpu_to_le32(c.lpf_ino);
1501 	dent_blk->dentry[0].name_len = cpu_to_le16(1);
1502 	dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1503 	memcpy(dent_blk->filename[0], ".", 1);
1504 
1505 	dent_blk->dentry[1].hash_code = 0;
1506 	dent_blk->dentry[1].ino = sb->root_ino;
1507 	dent_blk->dentry[1].name_len = cpu_to_le16(2);
1508 	dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1509 	memcpy(dent_blk->filename[1], "..", 2);
1510 
1511 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1512 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1513 
1514 	data_blk_offset = get_sb(main_blkaddr);
1515 	data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg +
1516 		1 + c.quota_dnum;
1517 
1518 	DBG(1, "\tWriting default dentry lost+found, at offset 0x%08"PRIx64"\n",
1519 			data_blk_offset);
1520 	if (dev_write_block(dent_blk, data_blk_offset)) {
1521 		MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1522 		free(dent_blk);
1523 		return 0;
1524 	}
1525 
1526 	free(dent_blk);
1527 	c.lpf_dnum++;
1528 	return data_blk_offset;
1529 }
1530 
f2fs_write_lpf_inode(void)1531 static int f2fs_write_lpf_inode(void)
1532 {
1533 	struct f2fs_node *raw_node;
1534 	uint64_t blk_size_bytes, main_area_node_seg_blk_offset;
1535 	block_t data_blk_nor;
1536 	int err = 0;
1537 
1538 	ASSERT(c.lpf_ino);
1539 
1540 	raw_node = calloc(F2FS_BLKSIZE, 1);
1541 	if (raw_node == NULL) {
1542 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1543 		return -1;
1544 	}
1545 
1546 	raw_node->footer.nid = cpu_to_le32(c.lpf_ino);
1547 	raw_node->footer.ino = raw_node->footer.nid;
1548 	raw_node->footer.cp_ver = cpu_to_le64(1);
1549 	raw_node->footer.next_blkaddr = cpu_to_le32(
1550 			get_sb(main_blkaddr) +
1551 			c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1552 			1 + c.quota_inum + 1);
1553 
1554 	raw_node->i.i_mode = cpu_to_le16(0x41c0); /* 0700 */
1555 	raw_node->i.i_links = cpu_to_le32(2);
1556 	raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1557 	raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1558 
1559 	blk_size_bytes = 1 << get_sb(log_blocksize);
1560 	raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes);
1561 	raw_node->i.i_blocks = cpu_to_le64(2);
1562 
1563 	raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1564 	raw_node->i.i_atime_nsec = 0;
1565 	raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1566 	raw_node->i.i_ctime_nsec = 0;
1567 	raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1568 	raw_node->i.i_mtime_nsec = 0;
1569 	raw_node->i.i_generation = 0;
1570 	raw_node->i.i_xattr_nid = 0;
1571 	raw_node->i.i_flags = 0;
1572 	raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1573 	raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1574 	memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1575 	raw_node->i.i_current_depth = cpu_to_le32(1);
1576 	raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1577 
1578 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1579 		raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1580 		raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1581 	}
1582 
1583 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1584 		raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1585 
1586 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1587 		raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1588 		raw_node->i.i_crtime_nsec = 0;
1589 	}
1590 
1591 	if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1592 		raw_node->i.i_compress_algrithm = 0;
1593 		raw_node->i.i_log_cluster_size = 0;
1594 		raw_node->i.i_padding = 0;
1595 	}
1596 
1597 	data_blk_nor = f2fs_add_default_dentry_lpf();
1598 	if (data_blk_nor == 0) {
1599 		MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1600 		err = -1;
1601 		goto exit;
1602 	}
1603 	raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1604 
1605 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1606 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1607 		c.blks_per_seg + c.quota_inum + 1;
1608 
1609 	DBG(1, "\tWriting lost+found inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1610 			get_sb(main_blkaddr),
1611 			c.cur_seg[CURSEG_HOT_NODE],
1612 			c.blks_per_seg, main_area_node_seg_blk_offset);
1613 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1614 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1615 		err = -1;
1616 		goto exit;
1617 	}
1618 
1619 	c.lpf_inum++;
1620 exit:
1621 	free(raw_node);
1622 	return err;
1623 }
1624 
f2fs_add_default_dentry_root(void)1625 static int f2fs_add_default_dentry_root(void)
1626 {
1627 	struct f2fs_dentry_block *dent_blk = NULL;
1628 	uint64_t data_blk_offset = 0;
1629 
1630 	dent_blk = calloc(F2FS_BLKSIZE, 1);
1631 	if(dent_blk == NULL) {
1632 		MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1633 		return -1;
1634 	}
1635 
1636 	dent_blk->dentry[0].hash_code = 0;
1637 	dent_blk->dentry[0].ino = sb->root_ino;
1638 	dent_blk->dentry[0].name_len = cpu_to_le16(1);
1639 	dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1640 	memcpy(dent_blk->filename[0], ".", 1);
1641 
1642 	dent_blk->dentry[1].hash_code = 0;
1643 	dent_blk->dentry[1].ino = sb->root_ino;
1644 	dent_blk->dentry[1].name_len = cpu_to_le16(2);
1645 	dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1646 	memcpy(dent_blk->filename[1], "..", 2);
1647 
1648 	/* bitmap for . and .. */
1649 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1650 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1651 
1652 	if (c.lpf_ino) {
1653 		int len = strlen(LPF);
1654 		f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1655 
1656 		dent_blk->dentry[2].hash_code = cpu_to_le32(hash);
1657 		dent_blk->dentry[2].ino = cpu_to_le32(c.lpf_ino);
1658 		dent_blk->dentry[2].name_len = cpu_to_le16(len);
1659 		dent_blk->dentry[2].file_type = F2FS_FT_DIR;
1660 		memcpy(dent_blk->filename[2], LPF, F2FS_SLOT_LEN);
1661 
1662 		memcpy(dent_blk->filename[3], &LPF[F2FS_SLOT_LEN],
1663 				len - F2FS_SLOT_LEN);
1664 
1665 		test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1666 		test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1667 	}
1668 
1669 	data_blk_offset = get_sb(main_blkaddr);
1670 	data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] *
1671 				c.blks_per_seg;
1672 
1673 	DBG(1, "\tWriting default dentry root, at offset 0x%08"PRIx64"\n",
1674 				data_blk_offset);
1675 	if (dev_write_block(dent_blk, data_blk_offset)) {
1676 		MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1677 		free(dent_blk);
1678 		return -1;
1679 	}
1680 
1681 	free(dent_blk);
1682 	return 0;
1683 }
1684 
f2fs_create_root_dir(void)1685 static int f2fs_create_root_dir(void)
1686 {
1687 	enum quota_type qtype;
1688 	int err = 0, i = 0;
1689 
1690 	err = f2fs_write_root_inode();
1691 	if (err < 0) {
1692 		MSG(1, "\tError: Failed to write root inode!!!\n");
1693 		goto exit;
1694 	}
1695 
1696 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++)  {
1697 		if (!((1 << qtype) & c.quota_bits))
1698 			continue;
1699 		err = f2fs_write_qf_inode(qtype, i++);
1700 		if (err < 0) {
1701 			MSG(1, "\tError: Failed to write quota inode!!!\n");
1702 			goto exit;
1703 		}
1704 	}
1705 
1706 	if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
1707 		err = f2fs_write_lpf_inode();
1708 		if (err < 0) {
1709 			MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1710 			goto exit;
1711 		}
1712 	}
1713 
1714 #ifndef WITH_ANDROID
1715 	err = f2fs_discard_obsolete_dnode();
1716 	if (err < 0) {
1717 		MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1718 		goto exit;
1719 	}
1720 #endif
1721 
1722 	err = f2fs_update_nat_root();
1723 	if (err < 0) {
1724 		MSG(1, "\tError: Failed to update NAT for root!!!\n");
1725 		goto exit;
1726 	}
1727 
1728 	err = f2fs_add_default_dentry_root();
1729 	if (err < 0) {
1730 		MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1731 		goto exit;
1732 	}
1733 exit:
1734 	if (err)
1735 		MSG(1, "\tError: Could not create the root directory!!!\n");
1736 
1737 	return err;
1738 }
1739 
f2fs_format_device(void)1740 int f2fs_format_device(void)
1741 {
1742 	int err = 0;
1743 
1744 	err= f2fs_prepare_super_block();
1745 	if (err < 0) {
1746 		MSG(0, "\tError: Failed to prepare a super block!!!\n");
1747 		goto exit;
1748 	}
1749 
1750 	if (c.trim) {
1751 		err = f2fs_trim_devices();
1752 		if (err < 0) {
1753 			MSG(0, "\tError: Failed to trim whole device!!!\n");
1754 			goto exit;
1755 		}
1756 	}
1757 
1758 	err = f2fs_init_sit_area();
1759 	if (err < 0) {
1760 		MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1761 		goto exit;
1762 	}
1763 
1764 	err = f2fs_init_nat_area();
1765 	if (err < 0) {
1766 		MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1767 		goto exit;
1768 	}
1769 
1770 	err = f2fs_create_root_dir();
1771 	if (err < 0) {
1772 		MSG(0, "\tError: Failed to create the root directory!!!\n");
1773 		goto exit;
1774 	}
1775 
1776 	err = f2fs_write_check_point_pack();
1777 	if (err < 0) {
1778 		MSG(0, "\tError: Failed to write the check point pack!!!\n");
1779 		goto exit;
1780 	}
1781 
1782 	err = f2fs_write_super_block();
1783 	if (err < 0) {
1784 		MSG(0, "\tError: Failed to write the super block!!!\n");
1785 		goto exit;
1786 	}
1787 exit:
1788 	if (err)
1789 		MSG(0, "\tError: Could not format the device!!!\n");
1790 
1791 	return err;
1792 }
1793