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