• 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 #ifdef HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
21 #ifdef HAVE_SYS_MOUNT_H
22 #include <sys/mount.h>
23 #endif
24 #include <time.h>
25 
26 #include "config.h"
27 #ifdef HAVE_UUID_UUID_H
28 #include <uuid/uuid.h>
29 #endif
30 #ifndef HAVE_LIBUUID
31 #define uuid_parse(a, b) -1
32 #define uuid_generate(a)
33 #endif
34 
35 #include "f2fs_fs.h"
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_HM &&
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 
473 	set_sb(section_count, total_zones * c.secs_per_zone);
474 
475 	set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
476 
477 	/*
478 	 * Let's determine the best reserved and overprovisioned space.
479 	 * For Zoned device, if zone capacity less than zone size, the segments
480 	 * starting after the zone capacity are unusable in each zone. So get
481 	 * overprovision ratio and reserved seg count based on avg usable
482 	 * segs_per_sec.
483 	 */
484 	if (c.overprovision == 0)
485 		c.overprovision = get_best_overprovision(sb);
486 
487 	c.reserved_segments =
488 			(2 * (100 / c.overprovision + 1) + NR_CURSEG_TYPE) *
489 			round_up(f2fs_get_usable_segments(sb), get_sb(section_count));
490 
491 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
492 		c.overprovision = 0;
493 		c.reserved_segments = 0;
494 	}
495 	if ((!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)) &&
496 		c.overprovision == 0) ||
497 		c.total_segments < F2FS_MIN_SEGMENTS ||
498 		(c.devices[0].total_sectors *
499 			c.sector_size < zone_align_start_offset) ||
500 		(get_sb(segment_count_main) - NR_CURSEG_TYPE) <
501 						c.reserved_segments) {
502 		MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
503 		return -1;
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] = 0;
550 		c.cur_seg[CURSEG_WARM_NODE] = 0;
551 		c.cur_seg[CURSEG_COLD_NODE] = 0;
552 		c.cur_seg[CURSEG_HOT_DATA] = 1;
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 
f2fs_init_sit_area(void)619 static int f2fs_init_sit_area(void)
620 {
621 	uint32_t blk_size, seg_size;
622 	uint32_t index = 0;
623 	uint64_t sit_seg_addr = 0;
624 	uint8_t *zero_buf = NULL;
625 
626 	blk_size = 1 << get_sb(log_blocksize);
627 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
628 
629 	zero_buf = calloc(sizeof(uint8_t), seg_size);
630 	if(zero_buf == NULL) {
631 		MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
632 		return -1;
633 	}
634 
635 	sit_seg_addr = get_sb(sit_blkaddr);
636 	sit_seg_addr *= blk_size;
637 
638 	DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
639 	for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
640 		if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
641 			MSG(1, "\tError: While zeroing out the sit area "
642 					"on disk!!!\n");
643 			free(zero_buf);
644 			return -1;
645 		}
646 		sit_seg_addr += seg_size;
647 	}
648 
649 	free(zero_buf);
650 	return 0 ;
651 }
652 
f2fs_init_nat_area(void)653 static int f2fs_init_nat_area(void)
654 {
655 	uint32_t blk_size, seg_size;
656 	uint32_t index = 0;
657 	uint64_t nat_seg_addr = 0;
658 	uint8_t *nat_buf = NULL;
659 
660 	blk_size = 1 << get_sb(log_blocksize);
661 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
662 
663 	nat_buf = calloc(sizeof(uint8_t), seg_size);
664 	if (nat_buf == NULL) {
665 		MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
666 		return -1;
667 	}
668 
669 	nat_seg_addr = get_sb(nat_blkaddr);
670 	nat_seg_addr *= blk_size;
671 
672 	DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
673 	for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
674 		if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
675 			MSG(1, "\tError: While zeroing out the nat area "
676 					"on disk!!!\n");
677 			free(nat_buf);
678 			return -1;
679 		}
680 		nat_seg_addr = nat_seg_addr + (2 * seg_size);
681 	}
682 
683 	free(nat_buf);
684 	return 0 ;
685 }
686 
f2fs_write_check_point_pack(void)687 static int f2fs_write_check_point_pack(void)
688 {
689 	struct f2fs_summary_block *sum = NULL;
690 	struct f2fs_journal *journal;
691 	uint32_t blk_size_bytes;
692 	uint32_t nat_bits_bytes, nat_bits_blocks;
693 	unsigned char *nat_bits = NULL, *empty_nat_bits;
694 	uint64_t cp_seg_blk = 0;
695 	uint32_t crc = 0, flags;
696 	unsigned int i;
697 	char *cp_payload = NULL;
698 	char *sum_compact, *sum_compact_p;
699 	struct f2fs_summary *sum_entry;
700 	enum quota_type qtype;
701 	int off;
702 	int ret = -1;
703 
704 	cp = calloc(F2FS_BLKSIZE, 1);
705 	if (cp == NULL) {
706 		MSG(1, "\tError: Calloc failed for f2fs_checkpoint!!!\n");
707 		return ret;
708 	}
709 
710 	sum = calloc(F2FS_BLKSIZE, 1);
711 	if (sum == NULL) {
712 		MSG(1, "\tError: Calloc failed for summary_node!!!\n");
713 		goto free_cp;
714 	}
715 
716 	sum_compact = calloc(F2FS_BLKSIZE, 1);
717 	if (sum_compact == NULL) {
718 		MSG(1, "\tError: Calloc failed for summary buffer!!!\n");
719 		goto free_sum;
720 	}
721 	sum_compact_p = sum_compact;
722 
723 	nat_bits_bytes = get_sb(segment_count_nat) << 5;
724 	nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
725 						F2FS_BLKSIZE - 1);
726 	nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
727 	if (nat_bits == NULL) {
728 		MSG(1, "\tError: Calloc failed for nat bits buffer!!!\n");
729 		goto free_sum_compact;
730 	}
731 
732 	cp_payload = calloc(F2FS_BLKSIZE, 1);
733 	if (cp_payload == NULL) {
734 		MSG(1, "\tError: Calloc failed for cp_payload!!!\n");
735 		goto free_nat_bits;
736 	}
737 
738 	/* 1. cp page 1 of checkpoint pack 1 */
739 	srand((c.fake_seed) ? 0 : time(NULL));
740 	cp->checkpoint_ver = cpu_to_le64(rand() | 0x1);
741 	set_cp(cur_node_segno[0], c.cur_seg[CURSEG_HOT_NODE]);
742 	set_cp(cur_node_segno[1], c.cur_seg[CURSEG_WARM_NODE]);
743 	set_cp(cur_node_segno[2], c.cur_seg[CURSEG_COLD_NODE]);
744 	set_cp(cur_data_segno[0], c.cur_seg[CURSEG_HOT_DATA]);
745 	set_cp(cur_data_segno[1], c.cur_seg[CURSEG_WARM_DATA]);
746 	set_cp(cur_data_segno[2], c.cur_seg[CURSEG_COLD_DATA]);
747 	for (i = 3; i < MAX_ACTIVE_NODE_LOGS; i++) {
748 		set_cp(cur_node_segno[i], 0xffffffff);
749 		set_cp(cur_data_segno[i], 0xffffffff);
750 	}
751 
752 	set_cp(cur_node_blkoff[0], 1 + c.quota_inum + c.lpf_inum);
753 	set_cp(cur_data_blkoff[0], 1 + c.quota_dnum + c.lpf_dnum);
754 	set_cp(valid_block_count, 2 + c.quota_inum + c.quota_dnum +
755 			c.lpf_inum + c.lpf_dnum);
756 	set_cp(rsvd_segment_count, c.reserved_segments);
757 
758 	/*
759 	 * For zoned devices, if zone capacity less than zone size, get
760 	 * overprovision segment count based on usable segments in the device.
761 	 */
762 	set_cp(overprov_segment_count, (f2fs_get_usable_segments(sb) -
763 			get_cp(rsvd_segment_count)) *
764 			c.overprovision / 100);
765 	set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
766 			get_cp(rsvd_segment_count));
767 
768 	if (f2fs_get_usable_segments(sb) <= get_cp(overprov_segment_count)) {
769 		MSG(0, "\tError: Not enough segments to create F2FS Volume\n");
770 		goto free_cp_payload;
771 	}
772 	MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
773 	MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
774 					get_cp(overprov_segment_count),
775 					c.reserved_segments);
776 
777 	/* main segments - reserved segments - (node + data segments) */
778 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
779 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
780 		set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
781 			get_cp(overprov_segment_count)) * c.blks_per_seg));
782 	} else {
783 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 6);
784 		set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
785 			get_cp(overprov_segment_count)) * c.blks_per_seg));
786 	}
787 	/* cp page (2), data summaries (1), node summaries (3) */
788 	set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
789 	flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
790 	if (get_cp(cp_pack_total_block_count) <=
791 			(1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
792 		flags |= CP_NAT_BITS_FLAG;
793 
794 	if (c.trimmed)
795 		flags |= CP_TRIMMED_FLAG;
796 
797 	if (c.large_nat_bitmap)
798 		flags |= CP_LARGE_NAT_BITMAP_FLAG;
799 
800 	set_cp(ckpt_flags, flags);
801 	set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
802 	set_cp(valid_node_count, 1 + c.quota_inum + c.lpf_inum);
803 	set_cp(valid_inode_count, 1 + c.quota_inum + c.lpf_inum);
804 	set_cp(next_free_nid, c.next_free_nid);
805 	set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
806 			get_sb(log_blocks_per_seg)) / 8);
807 
808 	set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
809 			 get_sb(log_blocks_per_seg)) / 8);
810 
811 	if (c.large_nat_bitmap)
812 		set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
813 	else
814 		set_cp(checksum_offset, CP_CHKSUM_OFFSET);
815 
816 	crc = f2fs_checkpoint_chksum(cp);
817 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
818 							cpu_to_le32(crc);
819 
820 	blk_size_bytes = 1 << get_sb(log_blocksize);
821 
822 	if (blk_size_bytes != F2FS_BLKSIZE) {
823 		MSG(1, "\tError: Wrong block size %d / %d!!!\n",
824 					blk_size_bytes, F2FS_BLKSIZE);
825 		goto free_cp_payload;
826 	}
827 
828 	cp_seg_blk = get_sb(segment0_blkaddr);
829 
830 	DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
831 						cp_seg_blk);
832 	if (dev_write_block(cp, cp_seg_blk)) {
833 		MSG(1, "\tError: While writing the cp to disk!!!\n");
834 		goto free_cp_payload;
835 	}
836 
837 	for (i = 0; i < get_sb(cp_payload); i++) {
838 		cp_seg_blk++;
839 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
840 			MSG(1, "\tError: While zeroing out the sit bitmap area "
841 					"on disk!!!\n");
842 			goto free_cp_payload;
843 		}
844 	}
845 
846 	/* Prepare and write Segment summary for HOT/WARM/COLD DATA
847 	 *
848 	 * The structure of compact summary
849 	 * +-------------------+
850 	 * | nat_journal       |
851 	 * +-------------------+
852 	 * | sit_journal       |
853 	 * +-------------------+
854 	 * | hot data summary  |
855 	 * +-------------------+
856 	 * | warm data summary |
857 	 * +-------------------+
858 	 * | cold data summary |
859 	 * +-------------------+
860 	*/
861 	memset(sum, 0, sizeof(struct f2fs_summary_block));
862 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
863 
864 	journal = &sum->journal;
865 	journal->n_nats = cpu_to_le16(1 + c.quota_inum + c.lpf_inum);
866 	journal->nat_j.entries[0].nid = sb->root_ino;
867 	journal->nat_j.entries[0].ne.version = 0;
868 	journal->nat_j.entries[0].ne.ino = sb->root_ino;
869 	journal->nat_j.entries[0].ne.block_addr = cpu_to_le32(
870 			get_sb(main_blkaddr) +
871 			get_cp(cur_node_segno[0]) * c.blks_per_seg);
872 
873 	for (qtype = 0, i = 1; qtype < F2FS_MAX_QUOTAS; qtype++) {
874 		if (!((1 << qtype) & c.quota_bits))
875 			continue;
876 		journal->nat_j.entries[i].nid = sb->qf_ino[qtype];
877 		journal->nat_j.entries[i].ne.version = 0;
878 		journal->nat_j.entries[i].ne.ino = sb->qf_ino[qtype];
879 		journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
880 				get_sb(main_blkaddr) +
881 				get_cp(cur_node_segno[0]) *
882 				c.blks_per_seg + i);
883 		i++;
884 	}
885 
886 	if (c.lpf_inum) {
887 		journal->nat_j.entries[i].nid = cpu_to_le32(c.lpf_ino);
888 		journal->nat_j.entries[i].ne.version = 0;
889 		journal->nat_j.entries[i].ne.ino = cpu_to_le32(c.lpf_ino);
890 		journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
891 				get_sb(main_blkaddr) +
892 				get_cp(cur_node_segno[0]) *
893 				c.blks_per_seg + i);
894 	}
895 
896 	memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
897 	sum_compact_p += SUM_JOURNAL_SIZE;
898 
899 	memset(sum, 0, sizeof(struct f2fs_summary_block));
900 
901 	/* inode sit for root */
902 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
903 		journal->n_sits = cpu_to_le16(2);
904 	else
905 		journal->n_sits = cpu_to_le16(6);
906 
907 	journal->sit_j.entries[0].segno = cp->cur_node_segno[0];
908 	journal->sit_j.entries[0].se.vblocks =
909 				cpu_to_le16((CURSEG_HOT_NODE << 10) |
910 						(1 + c.quota_inum + c.lpf_inum));
911 	f2fs_set_bit(0, (char *)journal->sit_j.entries[0].se.valid_map);
912 	for (i = 1; i <= c.quota_inum; i++)
913 		f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
914 	if (c.lpf_inum)
915 		f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
916 
917 	if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
918 		/* data sit for root */
919 		journal->sit_j.entries[1].segno = cp->cur_data_segno[0];
920 		journal->sit_j.entries[1].se.vblocks =
921 					cpu_to_le16((CURSEG_HOT_DATA << 10) |
922 							(1 + c.quota_dnum + c.lpf_dnum));
923 		f2fs_set_bit(0, (char *)journal->sit_j.entries[1].se.valid_map);
924 		for (i = 1; i <= c.quota_dnum; i++)
925 			f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
926 		if (c.lpf_dnum)
927 			f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
928 	} else {
929 		journal->sit_j.entries[1].segno = cp->cur_node_segno[1];
930 		journal->sit_j.entries[1].se.vblocks =
931 					cpu_to_le16((CURSEG_WARM_NODE << 10));
932 		journal->sit_j.entries[2].segno = cp->cur_node_segno[2];
933 		journal->sit_j.entries[2].se.vblocks =
934 					cpu_to_le16((CURSEG_COLD_NODE << 10));
935 
936 		/* data sit for root */
937 		journal->sit_j.entries[3].segno = cp->cur_data_segno[0];
938 		journal->sit_j.entries[3].se.vblocks =
939 					cpu_to_le16((CURSEG_HOT_DATA << 10) |
940 							(1 + c.quota_dnum + c.lpf_dnum));
941 		f2fs_set_bit(0, (char *)journal->sit_j.entries[3].se.valid_map);
942 		for (i = 1; i <= c.quota_dnum; i++)
943 			f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
944 		if (c.lpf_dnum)
945 			f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
946 
947 		journal->sit_j.entries[4].segno = cp->cur_data_segno[1];
948 		journal->sit_j.entries[4].se.vblocks =
949 					cpu_to_le16((CURSEG_WARM_DATA << 10));
950 		journal->sit_j.entries[5].segno = cp->cur_data_segno[2];
951 		journal->sit_j.entries[5].se.vblocks =
952 					cpu_to_le16((CURSEG_COLD_DATA << 10));
953 	}
954 
955 	memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
956 	sum_compact_p += SUM_JOURNAL_SIZE;
957 
958 	/* hot data summary */
959 	sum_entry = (struct f2fs_summary *)sum_compact_p;
960 	sum_entry->nid = sb->root_ino;
961 	sum_entry->ofs_in_node = 0;
962 
963 	off = 1;
964 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
965 		int j;
966 
967 		if (!((1 << qtype) & c.quota_bits))
968 			continue;
969 
970 		for (j = 0; j < QUOTA_DATA(qtype); j++) {
971 			(sum_entry + off + j)->nid = sb->qf_ino[qtype];
972 			(sum_entry + off + j)->ofs_in_node = cpu_to_le16(j);
973 		}
974 		off += QUOTA_DATA(qtype);
975 	}
976 
977 	if (c.lpf_dnum) {
978 		(sum_entry + off)->nid = cpu_to_le32(c.lpf_ino);
979 		(sum_entry + off)->ofs_in_node = 0;
980 	}
981 
982 	/* warm data summary, nothing to do */
983 	/* cold data summary, nothing to do */
984 
985 	cp_seg_blk++;
986 	DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
987 			cp_seg_blk);
988 	if (dev_write_block(sum_compact, cp_seg_blk)) {
989 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
990 		goto free_cp_payload;
991 	}
992 
993 	/* Prepare and write Segment summary for HOT_NODE */
994 	memset(sum, 0, sizeof(struct f2fs_summary_block));
995 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
996 
997 	sum->entries[0].nid = sb->root_ino;
998 	sum->entries[0].ofs_in_node = 0;
999 	for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1000 		if (!((1 << qtype) & c.quota_bits))
1001 			continue;
1002 		sum->entries[1 + i].nid = sb->qf_ino[qtype];
1003 		sum->entries[1 + i].ofs_in_node = 0;
1004 		i++;
1005 	}
1006 	if (c.lpf_inum) {
1007 		i++;
1008 		sum->entries[i].nid = cpu_to_le32(c.lpf_ino);
1009 		sum->entries[i].ofs_in_node = 0;
1010 	}
1011 
1012 	cp_seg_blk++;
1013 	DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
1014 			cp_seg_blk);
1015 	if (dev_write_block(sum, cp_seg_blk)) {
1016 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1017 		goto free_cp_payload;
1018 	}
1019 
1020 	/* Fill segment summary for WARM_NODE to zero. */
1021 	memset(sum, 0, sizeof(struct f2fs_summary_block));
1022 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1023 
1024 	cp_seg_blk++;
1025 	DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
1026 			cp_seg_blk);
1027 	if (dev_write_block(sum, cp_seg_blk)) {
1028 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1029 		goto free_cp_payload;
1030 	}
1031 
1032 	/* Fill segment summary for COLD_NODE to zero. */
1033 	memset(sum, 0, sizeof(struct f2fs_summary_block));
1034 	SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1035 	cp_seg_blk++;
1036 	DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
1037 			cp_seg_blk);
1038 	if (dev_write_block(sum, cp_seg_blk)) {
1039 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1040 		goto free_cp_payload;
1041 	}
1042 
1043 	/* cp page2 */
1044 	cp_seg_blk++;
1045 	DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
1046 	if (dev_write_block(cp, cp_seg_blk)) {
1047 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1048 		goto free_cp_payload;
1049 	}
1050 
1051 	/* write NAT bits, if possible */
1052 	if (flags & CP_NAT_BITS_FLAG) {
1053 		uint32_t i;
1054 
1055 		*(__le64 *)nat_bits = get_cp_crc(cp);
1056 		empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1057 		memset(empty_nat_bits, 0xff, nat_bits_bytes);
1058 		test_and_clear_bit_le(0, empty_nat_bits);
1059 
1060 		/* write the last blocks in cp pack */
1061 		cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1062 				get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1063 
1064 		DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1065 					cp_seg_blk);
1066 
1067 		for (i = 0; i < nat_bits_blocks; i++) {
1068 			if (dev_write_block(nat_bits + i *
1069 						F2FS_BLKSIZE, cp_seg_blk + i)) {
1070 				MSG(1, "\tError: write NAT bits to disk!!!\n");
1071 				goto free_cp_payload;
1072 			}
1073 		}
1074 	}
1075 
1076 	/* cp page 1 of check point pack 2
1077 	 * Initialize other checkpoint pack with version zero
1078 	 */
1079 	cp->checkpoint_ver = 0;
1080 
1081 	crc = f2fs_checkpoint_chksum(cp);
1082 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1083 							cpu_to_le32(crc);
1084 	cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1085 	DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1086 				cp_seg_blk);
1087 	if (dev_write_block(cp, cp_seg_blk)) {
1088 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1089 		goto free_cp_payload;
1090 	}
1091 
1092 	for (i = 0; i < get_sb(cp_payload); i++) {
1093 		cp_seg_blk++;
1094 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
1095 			MSG(1, "\tError: While zeroing out the sit bitmap area "
1096 					"on disk!!!\n");
1097 			goto free_cp_payload;
1098 		}
1099 	}
1100 
1101 	/* cp page 2 of check point pack 2 */
1102 	cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1103 					get_sb(cp_payload) - 1);
1104 	DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1105 				cp_seg_blk);
1106 	if (dev_write_block(cp, cp_seg_blk)) {
1107 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1108 		goto free_cp_payload;
1109 	}
1110 
1111 	ret = 0;
1112 
1113 free_cp_payload:
1114 	free(cp_payload);
1115 free_nat_bits:
1116 	free(nat_bits);
1117 free_sum_compact:
1118 	free(sum_compact);
1119 free_sum:
1120 	free(sum);
1121 free_cp:
1122 	free(cp);
1123 	return ret;
1124 }
1125 
f2fs_write_super_block(void)1126 static int f2fs_write_super_block(void)
1127 {
1128 	int index;
1129 	uint8_t *zero_buff;
1130 
1131 	zero_buff = calloc(F2FS_BLKSIZE, 1);
1132 	if (zero_buff == NULL) {
1133 		MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1134 		return -1;
1135 	}
1136 
1137 	memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1138 	DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1139 	for (index = 0; index < 2; index++) {
1140 		if (dev_write_block(zero_buff, index)) {
1141 			MSG(1, "\tError: While while writing super_blk "
1142 					"on disk!!! index : %d\n", index);
1143 			free(zero_buff);
1144 			return -1;
1145 		}
1146 	}
1147 
1148 	free(zero_buff);
1149 	return 0;
1150 }
1151 
1152 #ifndef WITH_ANDROID
f2fs_discard_obsolete_dnode(void)1153 static int f2fs_discard_obsolete_dnode(void)
1154 {
1155 	struct f2fs_node *raw_node;
1156 	uint64_t next_blkaddr = 0, offset;
1157 	u64 end_blkaddr = (get_sb(segment_count_main) <<
1158 			get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1159 	uint64_t start_inode_pos = get_sb(main_blkaddr);
1160 	uint64_t last_inode_pos;
1161 
1162 	if (c.zoned_mode || c.feature & cpu_to_le32(F2FS_FEATURE_RO))
1163 		return 0;
1164 
1165 	raw_node = calloc(sizeof(struct f2fs_node), 1);
1166 	if (raw_node == NULL) {
1167 		MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1168 		return -1;
1169 	}
1170 
1171 	/* avoid power-off-recovery based on roll-forward policy */
1172 	offset = get_sb(main_blkaddr);
1173 	offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1174 
1175 	last_inode_pos = start_inode_pos +
1176 		c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg + c.quota_inum + c.lpf_inum;
1177 
1178 	do {
1179 		if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1180 			break;
1181 
1182 		if (dev_read_block(raw_node, offset)) {
1183 			MSG(1, "\tError: While traversing direct node!!!\n");
1184 			free(raw_node);
1185 			return -1;
1186 		}
1187 
1188 		next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
1189 		memset(raw_node, 0, F2FS_BLKSIZE);
1190 
1191 		DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1192 		if (dev_write_block(raw_node, offset)) {
1193 			MSG(1, "\tError: While discarding direct node!!!\n");
1194 			free(raw_node);
1195 			return -1;
1196 		}
1197 		offset = next_blkaddr;
1198 		/* should avoid recursive chain due to stale data */
1199 		if (offset >= start_inode_pos || offset <= last_inode_pos)
1200 			break;
1201 	} while (1);
1202 
1203 	free(raw_node);
1204 	return 0;
1205 }
1206 #endif
1207 
f2fs_write_root_inode(void)1208 static int f2fs_write_root_inode(void)
1209 {
1210 	struct f2fs_node *raw_node = NULL;
1211 	uint64_t blk_size_bytes, data_blk_nor;
1212 	uint64_t main_area_node_seg_blk_offset = 0;
1213 
1214 	raw_node = calloc(F2FS_BLKSIZE, 1);
1215 	if (raw_node == NULL) {
1216 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1217 		return -1;
1218 	}
1219 
1220 	raw_node->footer.nid = sb->root_ino;
1221 	raw_node->footer.ino = sb->root_ino;
1222 	raw_node->footer.cp_ver = cpu_to_le64(1);
1223 	raw_node->footer.next_blkaddr = cpu_to_le32(
1224 			get_sb(main_blkaddr) +
1225 			c.cur_seg[CURSEG_HOT_NODE] *
1226 			c.blks_per_seg + 1);
1227 
1228 	raw_node->i.i_mode = cpu_to_le16(0x41ed);
1229 	if (c.lpf_ino)
1230 		raw_node->i.i_links = cpu_to_le32(3);
1231 	else
1232 		raw_node->i.i_links = cpu_to_le32(2);
1233 	raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1234 	raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1235 
1236 	blk_size_bytes = 1 << get_sb(log_blocksize);
1237 	raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
1238 	raw_node->i.i_blocks = cpu_to_le64(2);
1239 
1240 	raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1241 	raw_node->i.i_atime_nsec = 0;
1242 	raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1243 	raw_node->i.i_ctime_nsec = 0;
1244 	raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1245 	raw_node->i.i_mtime_nsec = 0;
1246 	raw_node->i.i_generation = 0;
1247 	raw_node->i.i_xattr_nid = 0;
1248 	raw_node->i.i_flags = 0;
1249 	raw_node->i.i_current_depth = cpu_to_le32(1);
1250 	raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1251 
1252 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1253 		raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1254 		raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1255 	}
1256 
1257 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1258 		raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1259 
1260 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1261 		raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1262 		raw_node->i.i_crtime_nsec = 0;
1263 	}
1264 
1265 	if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1266 		raw_node->i.i_compress_algrithm = 0;
1267 		raw_node->i.i_log_cluster_size = 0;
1268 		raw_node->i.i_padding = 0;
1269 	}
1270 
1271 	data_blk_nor = get_sb(main_blkaddr) +
1272 		c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg;
1273 	raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1274 
1275 	raw_node->i.i_ext.fofs = 0;
1276 	raw_node->i.i_ext.blk_addr = 0;
1277 	raw_node->i.i_ext.len = 0;
1278 
1279 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1280 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1281 					c.blks_per_seg;
1282 
1283 	DBG(1, "\tWriting root inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1284 			get_sb(main_blkaddr),
1285 			c.cur_seg[CURSEG_HOT_NODE],
1286 			c.blks_per_seg, main_area_node_seg_blk_offset);
1287 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1288 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1289 		free(raw_node);
1290 		return -1;
1291 	}
1292 
1293 	free(raw_node);
1294 	return 0;
1295 }
1296 
f2fs_write_default_quota(int qtype,unsigned int blkaddr,__le32 raw_id)1297 static int f2fs_write_default_quota(int qtype, unsigned int blkaddr,
1298 						__le32 raw_id)
1299 {
1300 	char *filebuf = calloc(F2FS_BLKSIZE, 2);
1301 	int file_magics[] = INITQMAGICS;
1302 	struct v2_disk_dqheader ddqheader;
1303 	struct v2_disk_dqinfo ddqinfo;
1304 	struct v2r1_disk_dqblk dqblk;
1305 
1306 	if (filebuf == NULL) {
1307 		MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1308 		return -1;
1309 	}
1310 
1311 	/* Write basic quota header */
1312 	ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1313 	/* only support QF_VFSV1 */
1314 	ddqheader.dqh_version = cpu_to_le32(1);
1315 
1316 	memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1317 
1318 	/* Fill Initial quota file content */
1319 	ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1320 	ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1321 	ddqinfo.dqi_flags = cpu_to_le32(0);
1322 	ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1323 	ddqinfo.dqi_free_blk = cpu_to_le32(0);
1324 	ddqinfo.dqi_free_entry = cpu_to_le32(5);
1325 
1326 	memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1327 
1328 	filebuf[1024] = 2;
1329 	filebuf[2048] = 3;
1330 	filebuf[3072] = 4;
1331 	filebuf[4096] = 5;
1332 
1333 	filebuf[5120 + 8] = 1;
1334 
1335 	dqblk.dqb_id = raw_id;
1336 	dqblk.dqb_pad = cpu_to_le32(0);
1337 	dqblk.dqb_ihardlimit = cpu_to_le64(0);
1338 	dqblk.dqb_isoftlimit = cpu_to_le64(0);
1339 	if (c.lpf_ino)
1340 		dqblk.dqb_curinodes = cpu_to_le64(2);
1341 	else
1342 		dqblk.dqb_curinodes = cpu_to_le64(1);
1343 	dqblk.dqb_bhardlimit = cpu_to_le64(0);
1344 	dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1345 	if (c.lpf_ino)
1346 		dqblk.dqb_curspace = cpu_to_le64(8192);
1347 	else
1348 		dqblk.dqb_curspace = cpu_to_le64(4096);
1349 	dqblk.dqb_btime = cpu_to_le64(0);
1350 	dqblk.dqb_itime = cpu_to_le64(0);
1351 
1352 	memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1353 
1354 	/* Write two blocks */
1355 	if (dev_write_block(filebuf, blkaddr) ||
1356 	    dev_write_block(filebuf + F2FS_BLKSIZE, blkaddr + 1)) {
1357 		MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1358 		free(filebuf);
1359 		return -1;
1360 	}
1361 	DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
1362 					blkaddr, blkaddr + 1);
1363 	free(filebuf);
1364 	c.quota_dnum += QUOTA_DATA(qtype);
1365 	return 0;
1366 }
1367 
f2fs_write_qf_inode(int qtype,int offset)1368 static int f2fs_write_qf_inode(int qtype, int offset)
1369 {
1370 	struct f2fs_node *raw_node = NULL;
1371 	uint64_t data_blk_nor;
1372 	uint64_t main_area_node_seg_blk_offset = 0;
1373 	__le32 raw_id;
1374 	int i;
1375 
1376 	raw_node = calloc(F2FS_BLKSIZE, 1);
1377 	if (raw_node == NULL) {
1378 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1379 		return -1;
1380 	}
1381 	f2fs_init_qf_inode(sb, raw_node, qtype, mkfs_time);
1382 
1383 	raw_node->footer.next_blkaddr = cpu_to_le32(
1384 			get_sb(main_blkaddr) +
1385 			c.cur_seg[CURSEG_HOT_NODE] *
1386 			c.blks_per_seg + 1 + qtype + 1);
1387 	raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
1388 
1389 	data_blk_nor = get_sb(main_blkaddr) +
1390 		c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg + 1
1391 		+ offset * QUOTA_DATA(i);
1392 
1393 	if (qtype == 0)
1394 		raw_id = raw_node->i.i_uid;
1395 	else if (qtype == 1)
1396 		raw_id = raw_node->i.i_gid;
1397 	else if (qtype == 2)
1398 		raw_id = raw_node->i.i_projid;
1399 	else
1400 		ASSERT(0);
1401 
1402 	/* write two blocks */
1403 	if (f2fs_write_default_quota(qtype, data_blk_nor, raw_id)) {
1404 		free(raw_node);
1405 		return -1;
1406 	}
1407 
1408 	for (i = 0; i < QUOTA_DATA(qtype); i++)
1409 		raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1410 					cpu_to_le32(data_blk_nor + i);
1411 
1412 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1413 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1414 					c.blks_per_seg + offset + 1;
1415 
1416 	DBG(1, "\tWriting quota inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1417 			get_sb(main_blkaddr),
1418 			c.cur_seg[CURSEG_HOT_NODE],
1419 			c.blks_per_seg, main_area_node_seg_blk_offset);
1420 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1421 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1422 		free(raw_node);
1423 		return -1;
1424 	}
1425 
1426 	free(raw_node);
1427 	c.quota_inum++;
1428 	return 0;
1429 }
1430 
f2fs_update_nat_root(void)1431 static int f2fs_update_nat_root(void)
1432 {
1433 	struct f2fs_nat_block *nat_blk = NULL;
1434 	uint64_t nat_seg_blk_offset = 0;
1435 	enum quota_type qtype;
1436 	int i;
1437 
1438 	nat_blk = calloc(F2FS_BLKSIZE, 1);
1439 	if(nat_blk == NULL) {
1440 		MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1441 		return -1;
1442 	}
1443 
1444 	/* update quota */
1445 	for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1446 		if (!((1 << qtype) & c.quota_bits))
1447 			continue;
1448 		nat_blk->entries[sb->qf_ino[qtype]].block_addr =
1449 				cpu_to_le32(get_sb(main_blkaddr) +
1450 				c.cur_seg[CURSEG_HOT_NODE] *
1451 				c.blks_per_seg + i + 1);
1452 		nat_blk->entries[sb->qf_ino[qtype]].ino = sb->qf_ino[qtype];
1453 		i++;
1454 	}
1455 
1456 	/* update root */
1457 	nat_blk->entries[get_sb(root_ino)].block_addr = cpu_to_le32(
1458 		get_sb(main_blkaddr) +
1459 		c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg);
1460 	nat_blk->entries[get_sb(root_ino)].ino = sb->root_ino;
1461 
1462 	/* update node nat */
1463 	nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
1464 	nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
1465 
1466 	/* update meta nat */
1467 	nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
1468 	nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
1469 
1470 	nat_seg_blk_offset = get_sb(nat_blkaddr);
1471 
1472 	DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
1473 					nat_seg_blk_offset);
1474 	if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
1475 		MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
1476 		free(nat_blk);
1477 		return -1;
1478 	}
1479 
1480 	free(nat_blk);
1481 	return 0;
1482 }
1483 
f2fs_add_default_dentry_lpf(void)1484 static block_t f2fs_add_default_dentry_lpf(void)
1485 {
1486 	struct f2fs_dentry_block *dent_blk;
1487 	uint64_t data_blk_offset;
1488 
1489 	dent_blk = calloc(F2FS_BLKSIZE, 1);
1490 	if (dent_blk == NULL) {
1491 		MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1492 		return 0;
1493 	}
1494 
1495 	dent_blk->dentry[0].hash_code = 0;
1496 	dent_blk->dentry[0].ino = cpu_to_le32(c.lpf_ino);
1497 	dent_blk->dentry[0].name_len = cpu_to_le16(1);
1498 	dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1499 	memcpy(dent_blk->filename[0], ".", 1);
1500 
1501 	dent_blk->dentry[1].hash_code = 0;
1502 	dent_blk->dentry[1].ino = sb->root_ino;
1503 	dent_blk->dentry[1].name_len = cpu_to_le16(2);
1504 	dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1505 	memcpy(dent_blk->filename[1], "..", 2);
1506 
1507 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1508 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1509 
1510 	data_blk_offset = get_sb(main_blkaddr);
1511 	data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg +
1512 		1 + c.quota_dnum;
1513 
1514 	DBG(1, "\tWriting default dentry lost+found, at offset 0x%08"PRIx64"\n",
1515 			data_blk_offset);
1516 	if (dev_write_block(dent_blk, data_blk_offset)) {
1517 		MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1518 		free(dent_blk);
1519 		return 0;
1520 	}
1521 
1522 	free(dent_blk);
1523 	c.lpf_dnum++;
1524 	return data_blk_offset;
1525 }
1526 
f2fs_write_lpf_inode(void)1527 static int f2fs_write_lpf_inode(void)
1528 {
1529 	struct f2fs_node *raw_node;
1530 	uint64_t blk_size_bytes, main_area_node_seg_blk_offset;
1531 	block_t data_blk_nor;
1532 	int err = 0;
1533 
1534 	ASSERT(c.lpf_ino);
1535 
1536 	raw_node = calloc(F2FS_BLKSIZE, 1);
1537 	if (raw_node == NULL) {
1538 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1539 		return -1;
1540 	}
1541 
1542 	raw_node->footer.nid = cpu_to_le32(c.lpf_ino);
1543 	raw_node->footer.ino = raw_node->footer.nid;
1544 	raw_node->footer.cp_ver = cpu_to_le64(1);
1545 	raw_node->footer.next_blkaddr = cpu_to_le32(
1546 			get_sb(main_blkaddr) +
1547 			c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1548 			1 + c.quota_inum + 1);
1549 
1550 	raw_node->i.i_mode = cpu_to_le16(0x41c0); /* 0700 */
1551 	raw_node->i.i_links = cpu_to_le32(2);
1552 	raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1553 	raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1554 
1555 	blk_size_bytes = 1 << get_sb(log_blocksize);
1556 	raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes);
1557 	raw_node->i.i_blocks = cpu_to_le64(2);
1558 
1559 	raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1560 	raw_node->i.i_atime_nsec = 0;
1561 	raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1562 	raw_node->i.i_ctime_nsec = 0;
1563 	raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1564 	raw_node->i.i_mtime_nsec = 0;
1565 	raw_node->i.i_generation = 0;
1566 	raw_node->i.i_xattr_nid = 0;
1567 	raw_node->i.i_flags = 0;
1568 	raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1569 	raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1570 	memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1571 	raw_node->i.i_current_depth = cpu_to_le32(1);
1572 	raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1573 
1574 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1575 		raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1576 		raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1577 	}
1578 
1579 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1580 		raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1581 
1582 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1583 		raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1584 		raw_node->i.i_crtime_nsec = 0;
1585 	}
1586 
1587 	if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1588 		raw_node->i.i_compress_algrithm = 0;
1589 		raw_node->i.i_log_cluster_size = 0;
1590 		raw_node->i.i_padding = 0;
1591 	}
1592 
1593 	data_blk_nor = f2fs_add_default_dentry_lpf();
1594 	if (data_blk_nor == 0) {
1595 		MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1596 		err = -1;
1597 		goto exit;
1598 	}
1599 	raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1600 
1601 	main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1602 	main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1603 		c.blks_per_seg + c.quota_inum + 1;
1604 
1605 	DBG(1, "\tWriting lost+found inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1606 			get_sb(main_blkaddr),
1607 			c.cur_seg[CURSEG_HOT_NODE],
1608 			c.blks_per_seg, main_area_node_seg_blk_offset);
1609 	if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1610 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1611 		err = -1;
1612 		goto exit;
1613 	}
1614 
1615 	c.lpf_inum++;
1616 exit:
1617 	free(raw_node);
1618 	return err;
1619 }
1620 
f2fs_add_default_dentry_root(void)1621 static int f2fs_add_default_dentry_root(void)
1622 {
1623 	struct f2fs_dentry_block *dent_blk = NULL;
1624 	uint64_t data_blk_offset = 0;
1625 
1626 	dent_blk = calloc(F2FS_BLKSIZE, 1);
1627 	if(dent_blk == NULL) {
1628 		MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1629 		return -1;
1630 	}
1631 
1632 	dent_blk->dentry[0].hash_code = 0;
1633 	dent_blk->dentry[0].ino = sb->root_ino;
1634 	dent_blk->dentry[0].name_len = cpu_to_le16(1);
1635 	dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1636 	memcpy(dent_blk->filename[0], ".", 1);
1637 
1638 	dent_blk->dentry[1].hash_code = 0;
1639 	dent_blk->dentry[1].ino = sb->root_ino;
1640 	dent_blk->dentry[1].name_len = cpu_to_le16(2);
1641 	dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1642 	memcpy(dent_blk->filename[1], "..", 2);
1643 
1644 	/* bitmap for . and .. */
1645 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1646 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1647 
1648 	if (c.lpf_ino) {
1649 		int len = strlen(LPF);
1650 		f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1651 
1652 		dent_blk->dentry[2].hash_code = cpu_to_le32(hash);
1653 		dent_blk->dentry[2].ino = cpu_to_le32(c.lpf_ino);
1654 		dent_blk->dentry[2].name_len = cpu_to_le16(len);
1655 		dent_blk->dentry[2].file_type = F2FS_FT_DIR;
1656 		memcpy(dent_blk->filename[2], LPF, F2FS_SLOT_LEN);
1657 
1658 		memcpy(dent_blk->filename[3], &LPF[F2FS_SLOT_LEN],
1659 				len - F2FS_SLOT_LEN);
1660 
1661 		test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1662 		test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1663 	}
1664 
1665 	data_blk_offset = get_sb(main_blkaddr);
1666 	data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] *
1667 				c.blks_per_seg;
1668 
1669 	DBG(1, "\tWriting default dentry root, at offset 0x%08"PRIx64"\n",
1670 				data_blk_offset);
1671 	if (dev_write_block(dent_blk, data_blk_offset)) {
1672 		MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1673 		free(dent_blk);
1674 		return -1;
1675 	}
1676 
1677 	free(dent_blk);
1678 	return 0;
1679 }
1680 
f2fs_create_root_dir(void)1681 static int f2fs_create_root_dir(void)
1682 {
1683 	enum quota_type qtype;
1684 	int err = 0, i = 0;
1685 
1686 	err = f2fs_write_root_inode();
1687 	if (err < 0) {
1688 		MSG(1, "\tError: Failed to write root inode!!!\n");
1689 		goto exit;
1690 	}
1691 
1692 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++)  {
1693 		if (!((1 << qtype) & c.quota_bits))
1694 			continue;
1695 		err = f2fs_write_qf_inode(qtype, i++);
1696 		if (err < 0) {
1697 			MSG(1, "\tError: Failed to write quota inode!!!\n");
1698 			goto exit;
1699 		}
1700 	}
1701 
1702 	if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
1703 		err = f2fs_write_lpf_inode();
1704 		if (err < 0) {
1705 			MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1706 			goto exit;
1707 		}
1708 	}
1709 
1710 #ifndef WITH_ANDROID
1711 	err = f2fs_discard_obsolete_dnode();
1712 	if (err < 0) {
1713 		MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1714 		goto exit;
1715 	}
1716 #endif
1717 
1718 	err = f2fs_update_nat_root();
1719 	if (err < 0) {
1720 		MSG(1, "\tError: Failed to update NAT for root!!!\n");
1721 		goto exit;
1722 	}
1723 
1724 	err = f2fs_add_default_dentry_root();
1725 	if (err < 0) {
1726 		MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1727 		goto exit;
1728 	}
1729 exit:
1730 	if (err)
1731 		MSG(1, "\tError: Could not create the root directory!!!\n");
1732 
1733 	return err;
1734 }
1735 
f2fs_format_device(void)1736 int f2fs_format_device(void)
1737 {
1738 	int err = 0;
1739 
1740 	err= f2fs_prepare_super_block();
1741 	if (err < 0) {
1742 		MSG(0, "\tError: Failed to prepare a super block!!!\n");
1743 		goto exit;
1744 	}
1745 
1746 	if (c.trim) {
1747 		err = f2fs_trim_devices();
1748 		if (err < 0) {
1749 			MSG(0, "\tError: Failed to trim whole device!!!\n");
1750 			goto exit;
1751 		}
1752 	}
1753 
1754 	err = f2fs_init_sit_area();
1755 	if (err < 0) {
1756 		MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1757 		goto exit;
1758 	}
1759 
1760 	err = f2fs_init_nat_area();
1761 	if (err < 0) {
1762 		MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1763 		goto exit;
1764 	}
1765 
1766 	err = f2fs_create_root_dir();
1767 	if (err < 0) {
1768 		MSG(0, "\tError: Failed to create the root directory!!!\n");
1769 		goto exit;
1770 	}
1771 
1772 	err = f2fs_write_check_point_pack();
1773 	if (err < 0) {
1774 		MSG(0, "\tError: Failed to write the check point pack!!!\n");
1775 		goto exit;
1776 	}
1777 
1778 	err = f2fs_write_super_block();
1779 	if (err < 0) {
1780 		MSG(0, "\tError: Failed to write the super block!!!\n");
1781 		goto exit;
1782 	}
1783 exit:
1784 	if (err)
1785 		MSG(0, "\tError: Could not format the device!!!\n");
1786 
1787 	return err;
1788 }
1789