• 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 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <f2fs_fs.h>
15 #include <assert.h>
16 
17 #ifdef HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
20 #ifdef HAVE_SYS_MOUNT_H
21 #include <sys/mount.h>
22 #endif
23 #include <time.h>
24 
25 #ifdef HAVE_UUID_UUID_H
26 #include <uuid/uuid.h>
27 #endif
28 #ifndef HAVE_LIBUUID
29 #define uuid_parse(a, b) -1
30 #define uuid_generate(a)
31 #define uuid_unparse(a, b) -1
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 & 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) / c.segs_per_zone *
302 				c.segs_per_zone;
303 			c.devices[i].start_blkaddr = 0;
304 			c.devices[i].end_blkaddr = c.devices[i].total_segments *
305 						c.blks_per_seg - 1 +
306 						sb->segment0_blkaddr;
307 		} else {
308 			c.devices[i].total_segments =
309 				(c.devices[i].total_sectors /
310 				(c.sectors_per_blk * c.blks_per_seg)) /
311 				c.segs_per_zone * c.segs_per_zone;
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);
327 	set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
328 
329 	set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
330 			get_sb(segment_count_ckpt) * c.blks_per_seg);
331 
332 	blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
333 
334 	sit_segments = SEG_ALIGN(blocks_for_sit);
335 
336 	set_sb(segment_count_sit, sit_segments * 2);
337 
338 	set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
339 			c.blks_per_seg);
340 
341 	total_valid_blks_available = (get_sb(segment_count) -
342 			(get_sb(segment_count_ckpt) +
343 			get_sb(segment_count_sit))) * c.blks_per_seg;
344 
345 	blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
346 			NAT_ENTRY_PER_BLOCK);
347 
348 	if (c.large_nat_bitmap) {
349 		nat_segments = SEG_ALIGN(blocks_for_nat) *
350 						DEFAULT_NAT_ENTRY_RATIO / 100;
351 		set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
352 		max_nat_bitmap_size = (get_sb(segment_count_nat) <<
353 						log_blks_per_seg) / 8;
354 		set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
355 	} else {
356 		set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
357 		max_nat_bitmap_size = 0;
358 	}
359 
360 	/*
361 	 * The number of node segments should not be exceeded a "Threshold".
362 	 * This number resizes NAT bitmap area in a CP page.
363 	 * So the threshold is determined not to overflow one CP page
364 	 */
365 	sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
366 				log_blks_per_seg) / 8;
367 
368 	if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
369 		max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
370 	else
371 		max_sit_bitmap_size = sit_bitmap_size;
372 
373 	if (c.large_nat_bitmap) {
374 		/* use cp_payload if free space of f2fs_checkpoint is not enough */
375 		if (max_sit_bitmap_size + max_nat_bitmap_size >
376 						MAX_BITMAP_SIZE_IN_CKPT) {
377 			uint32_t diff =  max_sit_bitmap_size +
378 						max_nat_bitmap_size -
379 						MAX_BITMAP_SIZE_IN_CKPT;
380 			set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
381 		} else {
382 			set_sb(cp_payload, 0);
383 		}
384 	} else {
385 		/*
386 		 * It should be reserved minimum 1 segment for nat.
387 		 * When sit is too large, we should expand cp area.
388 		 * It requires more pages for cp.
389 		 */
390 		if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
391 			max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
392 			set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
393 	        } else {
394 			max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
395 							max_sit_bitmap_size;
396 			set_sb(cp_payload, 0);
397 		}
398 		max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
399 
400 		if (get_sb(segment_count_nat) > max_nat_segments)
401 			set_sb(segment_count_nat, max_nat_segments);
402 
403 		set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
404 	}
405 
406 	set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
407 			c.blks_per_seg);
408 
409 	total_valid_blks_available = (get_sb(segment_count) -
410 			(get_sb(segment_count_ckpt) +
411 			get_sb(segment_count_sit) +
412 			get_sb(segment_count_nat))) *
413 			c.blks_per_seg;
414 
415 	if (c.feature & F2FS_FEATURE_RO)
416 		blocks_for_ssa = 0;
417 	else
418 		blocks_for_ssa = total_valid_blks_available /
419 				c.blks_per_seg + 1;
420 
421 	set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
422 
423 	total_meta_segments = get_sb(segment_count_ckpt) +
424 		get_sb(segment_count_sit) +
425 		get_sb(segment_count_nat) +
426 		get_sb(segment_count_ssa);
427 	diff = total_meta_segments % (c.segs_per_zone);
428 	if (diff)
429 		set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
430 			(c.segs_per_zone - diff));
431 
432 	total_meta_zones = ZONE_ALIGN(total_meta_segments *
433 						c.blks_per_seg);
434 
435 	set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
436 				c.segs_per_zone * c.blks_per_seg);
437 
438 	if (c.zoned_mode) {
439 		/*
440 		 * Make sure there is enough randomly writeable
441 		 * space at the beginning of the disk.
442 		 */
443 		unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
444 
445 		if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
446 				c.devices[0].nr_rnd_zones < main_blkzone) {
447 			MSG(0, "\tError: Device does not have enough random "
448 					"write zones for F2FS volume (%lu needed)\n",
449 					main_blkzone);
450 			return -1;
451 		}
452 		/*
453 		 * Check if conventional device has enough space
454 		 * to accommodate all metadata, zoned device should
455 		 * not overlap to metadata area.
456 		 */
457 		for (i = 1; i < c.ndevs; i++) {
458 			if (c.devices[i].zoned_model != F2FS_ZONED_NONE &&
459 				c.devices[i].start_blkaddr < get_sb(main_blkaddr)) {
460 				MSG(0, "\tError: Conventional device %s is too small,"
461 					" (%"PRIu64" MiB needed).\n", c.devices[0].path,
462 					(get_sb(main_blkaddr) -
463 					c.devices[i].start_blkaddr) >> 8);
464 				return -1;
465 			}
466 		}
467 	}
468 
469 	total_zones = get_sb(segment_count) / (c.segs_per_zone) -
470 							total_meta_zones;
471 	if (total_zones == 0)
472 		goto too_small;
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 = get_reserved(sb, c.overprovision);
488 
489 	if (c.feature & F2FS_FEATURE_RO) {
490 		c.overprovision = 0;
491 		c.reserved_segments = 0;
492 	}
493 	if ((!(c.feature & F2FS_FEATURE_RO) &&
494 		c.overprovision == 0) ||
495 		c.total_segments < F2FS_MIN_SEGMENTS ||
496 		(c.devices[0].total_sectors *
497 			c.sector_size < zone_align_start_offset) ||
498 		(get_sb(segment_count_main) - NR_CURSEG_TYPE) <
499 						c.reserved_segments) {
500 		goto too_small;
501 	}
502 
503 	if (c.vol_uuid) {
504 		if (uuid_parse(c.vol_uuid, sb->uuid)) {
505 			MSG(0, "\tError: supplied string is not a valid UUID\n");
506 			return -1;
507 		}
508 	} else {
509 		uuid_generate(sb->uuid);
510 	}
511 
512 	/* precompute checksum seed for metadata */
513 	if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
514 		c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
515 
516 	utf8_to_utf16((char *)sb->volume_name, (const char *)c.vol_label,
517 				MAX_VOLUME_NAME, strlen(c.vol_label));
518 	set_sb(node_ino, 1);
519 	set_sb(meta_ino, 2);
520 	set_sb(root_ino, 3);
521 	c.next_free_nid = 4;
522 
523 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
524 		if (!((1 << qtype) & c.quota_bits))
525 			continue;
526 		sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
527 		MSG(0, "Info: add quota type = %u => %u\n",
528 					qtype, c.next_free_nid - 1);
529 	}
530 
531 	if (c.feature & F2FS_FEATURE_LOST_FOUND)
532 		c.lpf_ino = c.next_free_nid++;
533 
534 	if (c.feature & F2FS_FEATURE_RO)
535 		avail_zones = 2;
536 	else
537 		avail_zones = 6;
538 
539 	if (total_zones <= avail_zones) {
540 		MSG(1, "\tError: %d zones: Need more zones "
541 			"by shrinking zone size\n", total_zones);
542 		return -1;
543 	}
544 
545 	if (c.feature & F2FS_FEATURE_RO) {
546 		c.cur_seg[CURSEG_HOT_NODE] = last_section(last_zone(total_zones));
547 		c.cur_seg[CURSEG_WARM_NODE] = 0;
548 		c.cur_seg[CURSEG_COLD_NODE] = 0;
549 		c.cur_seg[CURSEG_HOT_DATA] = 0;
550 		c.cur_seg[CURSEG_COLD_DATA] = 0;
551 		c.cur_seg[CURSEG_WARM_DATA] = 0;
552 	} else if (c.zoned_mode) {
553 		c.cur_seg[CURSEG_HOT_NODE] = 0;
554 		if (c.zoned_model == F2FS_ZONED_HM) {
555 			uint32_t conv_zones =
556 				c.devices[0].total_segments / c.segs_per_zone
557 				- total_meta_zones;
558 
559 			if (total_zones - conv_zones >= avail_zones)
560 				c.cur_seg[CURSEG_HOT_NODE] =
561 					(c.devices[1].start_blkaddr -
562 					 get_sb(main_blkaddr)) / c.blks_per_seg;
563 		}
564 		c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
565 		c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
566 		c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
567 		c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_HOT_DATA);
568 		c.cur_seg[CURSEG_COLD_DATA] = next_zone(CURSEG_WARM_DATA);
569 	} else {
570 		c.cur_seg[CURSEG_HOT_NODE] = 0;
571 		c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
572 		c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
573 		c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
574 		c.cur_seg[CURSEG_COLD_DATA] =
575 				max(last_zone((total_zones >> 2)),
576 					next_zone(CURSEG_HOT_DATA));
577 		c.cur_seg[CURSEG_WARM_DATA] =
578 				max(last_zone((total_zones >> 1)),
579 					next_zone(CURSEG_COLD_DATA));
580 	}
581 
582 	/* if there is redundancy, reassign it */
583 	if (!(c.feature & F2FS_FEATURE_RO))
584 		verify_cur_segs();
585 
586 	cure_extension_list();
587 
588 	/* get kernel version */
589 	if (c.kd >= 0) {
590 		dev_read_version(c.version, 0, VERSION_LEN);
591 		get_kernel_version(c.version);
592 	} else {
593 		get_kernel_uname_version(c.version);
594 	}
595 	MSG(0, "Info: format version with\n  \"%s\"\n", c.version);
596 
597 	memcpy(sb->version, c.version, VERSION_LEN);
598 	memcpy(sb->init_version, c.version, VERSION_LEN);
599 
600 	if (c.feature & F2FS_FEATURE_CASEFOLD) {
601 		set_sb(s_encoding, c.s_encoding);
602 		set_sb(s_encoding_flags, c.s_encoding_flags);
603 	}
604 
605 	sb->feature = cpu_to_le32(c.feature);
606 
607 	if (c.feature & F2FS_FEATURE_SB_CHKSUM) {
608 		set_sb(checksum_offset, SB_CHKSUM_OFFSET);
609 		set_sb(crc, f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
610 						SB_CHKSUM_OFFSET));
611 		MSG(1, "Info: SB CRC is set: offset (%d), crc (0x%x)\n",
612 					get_sb(checksum_offset), get_sb(crc));
613 	}
614 
615 	return 0;
616 
617 too_small:
618 	MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
619 	return -1;
620 }
621 
f2fs_init_sit_area(void)622 static int f2fs_init_sit_area(void)
623 {
624 	uint32_t blk_size, seg_size;
625 	uint32_t index = 0;
626 	uint64_t sit_seg_addr = 0;
627 	uint8_t *zero_buf = NULL;
628 
629 	blk_size = 1 << get_sb(log_blocksize);
630 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
631 
632 	zero_buf = calloc(sizeof(uint8_t), seg_size);
633 	if(zero_buf == NULL) {
634 		MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
635 		return -1;
636 	}
637 
638 	sit_seg_addr = get_sb(sit_blkaddr);
639 	sit_seg_addr *= blk_size;
640 
641 	DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
642 	for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
643 		if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
644 			MSG(1, "\tError: While zeroing out the sit area "
645 					"on disk!!!\n");
646 			free(zero_buf);
647 			return -1;
648 		}
649 		sit_seg_addr += seg_size;
650 	}
651 
652 	free(zero_buf);
653 	return 0 ;
654 }
655 
f2fs_init_nat_area(void)656 static int f2fs_init_nat_area(void)
657 {
658 	uint32_t blk_size, seg_size;
659 	uint32_t index = 0;
660 	uint64_t nat_seg_addr = 0;
661 	uint8_t *nat_buf = NULL;
662 
663 	blk_size = 1 << get_sb(log_blocksize);
664 	seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
665 
666 	nat_buf = calloc(sizeof(uint8_t), seg_size);
667 	if (nat_buf == NULL) {
668 		MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
669 		return -1;
670 	}
671 
672 	nat_seg_addr = get_sb(nat_blkaddr);
673 	nat_seg_addr *= blk_size;
674 
675 	DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
676 	for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
677 		if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
678 			MSG(1, "\tError: While zeroing out the nat area "
679 					"on disk!!!\n");
680 			free(nat_buf);
681 			return -1;
682 		}
683 		nat_seg_addr = nat_seg_addr + (2 * seg_size);
684 	}
685 
686 	free(nat_buf);
687 	return 0 ;
688 }
689 
f2fs_write_check_point_pack(void)690 static int f2fs_write_check_point_pack(void)
691 {
692 	struct f2fs_summary_block *sum;
693 	struct f2fs_journal *journal;
694 	uint32_t blk_size_bytes;
695 	uint32_t nat_bits_bytes, nat_bits_blocks;
696 	unsigned char *nat_bits = NULL, *empty_nat_bits;
697 	uint64_t cp_seg_blk = 0;
698 	uint32_t crc = 0, flags;
699 	unsigned int i;
700 	char *cp_payload = NULL;
701 	char *sum_compact, *sum_compact_p;
702 	struct f2fs_summary *sum_entry;
703 	unsigned short vblocks;
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], c.curseg_offset[CURSEG_HOT_NODE]);
755 	set_cp(cur_data_blkoff[0], c.curseg_offset[CURSEG_HOT_DATA]);
756 	set_cp(valid_block_count, c.curseg_offset[CURSEG_HOT_NODE] +
757 					c.curseg_offset[CURSEG_HOT_DATA]);
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 (!(c.conf_reserved_sections) &&
769 	    get_cp(overprov_segment_count) < get_cp(rsvd_segment_count))
770 		set_cp(overprov_segment_count, get_cp(rsvd_segment_count));
771 
772 	/*
773 	 * If conf_reserved_sections has a non zero value, overprov_segment_count
774 	 * is set to overprov_segment_count + rsvd_segment_count.
775 	 */
776 	if (c.conf_reserved_sections) {
777 		/*
778 		 * Overprovision segments must be bigger than two sections.
779 		 * In non configurable reserved section case, overprovision
780 		 * segments are always bigger than two sections.
781 		 */
782 		if (get_cp(overprov_segment_count) <
783 					overprovision_segment_buffer(sb)) {
784 			MSG(0, "\tError: Not enough overprovision segments (%u)\n",
785 			    get_cp(overprov_segment_count));
786 			goto free_cp_payload;
787 		}
788 		set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
789 				get_cp(rsvd_segment_count));
790 	 } else {
791 		set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
792 				overprovision_segment_buffer(sb));
793 	 }
794 
795 	if (f2fs_get_usable_segments(sb) <= get_cp(overprov_segment_count)) {
796 		MSG(0, "\tError: Not enough segments to create F2FS Volume\n");
797 		goto free_cp_payload;
798 	}
799 	MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
800 	MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
801 					get_cp(overprov_segment_count),
802 					c.reserved_segments);
803 
804 	/* main segments - reserved segments - (node + data segments) */
805 	if (c.feature & F2FS_FEATURE_RO) {
806 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
807 		set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
808 			get_cp(overprov_segment_count)) * c.blks_per_seg));
809 	} else {
810 		set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 6);
811 		set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
812 			get_cp(overprov_segment_count)) * c.blks_per_seg));
813 	}
814 	/* cp page (2), data summaries (1), node summaries (3) */
815 	set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
816 	flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
817 	if (get_cp(cp_pack_total_block_count) <=
818 			(1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
819 		flags |= CP_NAT_BITS_FLAG;
820 
821 	if (c.trimmed)
822 		flags |= CP_TRIMMED_FLAG;
823 
824 	if (c.large_nat_bitmap)
825 		flags |= CP_LARGE_NAT_BITMAP_FLAG;
826 
827 	set_cp(ckpt_flags, flags);
828 	set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
829 	set_cp(valid_node_count, c.curseg_offset[CURSEG_HOT_NODE]);
830 	set_cp(valid_inode_count, c.curseg_offset[CURSEG_HOT_NODE]);
831 	set_cp(next_free_nid, c.next_free_nid);
832 	set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
833 			get_sb(log_blocks_per_seg)) / 8);
834 
835 	set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
836 			 get_sb(log_blocks_per_seg)) / 8);
837 
838 	if (c.large_nat_bitmap)
839 		set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
840 	else
841 		set_cp(checksum_offset, CP_CHKSUM_OFFSET);
842 
843 	crc = f2fs_checkpoint_chksum(cp);
844 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
845 							cpu_to_le32(crc);
846 
847 	blk_size_bytes = 1 << get_sb(log_blocksize);
848 
849 	if (blk_size_bytes != F2FS_BLKSIZE) {
850 		MSG(1, "\tError: Wrong block size %d / %d!!!\n",
851 					blk_size_bytes, F2FS_BLKSIZE);
852 		goto free_cp_payload;
853 	}
854 
855 	cp_seg_blk = get_sb(segment0_blkaddr);
856 
857 	DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
858 						cp_seg_blk);
859 	if (dev_write_block(cp, cp_seg_blk)) {
860 		MSG(1, "\tError: While writing the cp to disk!!!\n");
861 		goto free_cp_payload;
862 	}
863 
864 	for (i = 0; i < get_sb(cp_payload); i++) {
865 		cp_seg_blk++;
866 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
867 			MSG(1, "\tError: While zeroing out the sit bitmap area "
868 					"on disk!!!\n");
869 			goto free_cp_payload;
870 		}
871 	}
872 
873 	/* Prepare and write Segment summary for HOT/WARM/COLD DATA
874 	 *
875 	 * The structure of compact summary
876 	 * +-------------------+
877 	 * | nat_journal       |
878 	 * +-------------------+
879 	 * | sit_journal       |
880 	 * +-------------------+
881 	 * | hot data summary  |
882 	 * +-------------------+
883 	 * | warm data summary |
884 	 * +-------------------+
885 	 * | cold data summary |
886 	 * +-------------------+
887 	*/
888 
889 	/* nat_sjournal */
890 	journal = &c.nat_jnl;
891 	memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
892 	sum_compact_p += SUM_JOURNAL_SIZE;
893 
894 	/* sit_journal */
895 	journal = &c.sit_jnl;
896 
897 	if (c.feature & F2FS_FEATURE_RO) {
898 		i = CURSEG_RO_HOT_DATA;
899 		vblocks = le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
900 		journal->sit_j.entries[i].segno = cp->cur_data_segno[0];
901 		journal->sit_j.entries[i].se.vblocks =
902 				cpu_to_le16(vblocks | (CURSEG_HOT_DATA << 10));
903 
904 		i = CURSEG_RO_HOT_NODE;
905 		vblocks = le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
906 		journal->sit_j.entries[i].segno = cp->cur_node_segno[0];
907 		journal->sit_j.entries[i].se.vblocks |=
908 				cpu_to_le16(vblocks | (CURSEG_HOT_NODE << 10));
909 
910 		journal->n_sits = cpu_to_le16(2);
911 	} else {
912 		for (i = CURSEG_HOT_DATA; i < NR_CURSEG_TYPE; i++) {
913 			if (i < NR_CURSEG_DATA_TYPE)
914 				journal->sit_j.entries[i].segno =
915 					cp->cur_data_segno[i];
916 
917 			else
918 				journal->sit_j.entries[i].segno =
919 					cp->cur_node_segno[i - NR_CURSEG_DATA_TYPE];
920 
921 			vblocks =
922 				le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
923 			journal->sit_j.entries[i].se.vblocks =
924 						cpu_to_le16(vblocks | (i << 10));
925 		}
926 
927 		journal->n_sits = cpu_to_le16(6);
928 	}
929 
930 	memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
931 	sum_compact_p += SUM_JOURNAL_SIZE;
932 
933 	/* hot data summary */
934 	memset(sum, 0, F2FS_BLKSIZE);
935 	SET_SUM_TYPE(sum, SUM_TYPE_DATA);
936 
937 	sum_entry = (struct f2fs_summary *)sum_compact_p;
938 	memcpy(sum_entry, c.sum[CURSEG_HOT_DATA],
939 			sizeof(struct f2fs_summary) * MAX_CACHE_SUMS);
940 
941 	/* warm data summary, nothing to do */
942 	/* cold data summary, nothing to do */
943 
944 	cp_seg_blk++;
945 	DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
946 			cp_seg_blk);
947 	if (dev_write_block(sum_compact, cp_seg_blk)) {
948 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
949 		goto free_cp_payload;
950 	}
951 
952 	/* Prepare and write Segment summary for HOT_NODE */
953 	memset(sum, 0, F2FS_BLKSIZE);
954 	SET_SUM_TYPE(sum, SUM_TYPE_NODE);
955 	memcpy(sum->entries, c.sum[CURSEG_HOT_NODE],
956 			sizeof(struct f2fs_summary) * MAX_CACHE_SUMS);
957 
958 	cp_seg_blk++;
959 	DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
960 			cp_seg_blk);
961 	if (dev_write_block(sum, cp_seg_blk)) {
962 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
963 		goto free_cp_payload;
964 	}
965 
966 	/* Fill segment summary for WARM_NODE to zero. */
967 	memset(sum, 0, F2FS_BLKSIZE);
968 	SET_SUM_TYPE(sum, SUM_TYPE_NODE);
969 
970 	cp_seg_blk++;
971 	DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
972 			cp_seg_blk);
973 	if (dev_write_block(sum, cp_seg_blk)) {
974 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
975 		goto free_cp_payload;
976 	}
977 
978 	/* Fill segment summary for COLD_NODE to zero. */
979 	memset(sum, 0, F2FS_BLKSIZE);
980 	SET_SUM_TYPE(sum, SUM_TYPE_NODE);
981 	cp_seg_blk++;
982 	DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
983 			cp_seg_blk);
984 	if (dev_write_block(sum, cp_seg_blk)) {
985 		MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
986 		goto free_cp_payload;
987 	}
988 
989 	/* cp page2 */
990 	cp_seg_blk++;
991 	DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
992 	if (dev_write_block(cp, cp_seg_blk)) {
993 		MSG(1, "\tError: While writing the cp to disk!!!\n");
994 		goto free_cp_payload;
995 	}
996 
997 	/* write NAT bits, if possible */
998 	if (flags & CP_NAT_BITS_FLAG) {
999 		uint32_t i;
1000 
1001 		*(__le64 *)nat_bits = get_cp_crc(cp);
1002 		empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1003 		memset(empty_nat_bits, 0xff, nat_bits_bytes);
1004 		test_and_clear_bit_le(0, empty_nat_bits);
1005 
1006 		/* write the last blocks in cp pack */
1007 		cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1008 				get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1009 
1010 		DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1011 					cp_seg_blk);
1012 
1013 		for (i = 0; i < nat_bits_blocks; i++) {
1014 			if (dev_write_block(nat_bits + i *
1015 						F2FS_BLKSIZE, cp_seg_blk + i)) {
1016 				MSG(1, "\tError: write NAT bits to disk!!!\n");
1017 				goto free_cp_payload;
1018 			}
1019 		}
1020 	}
1021 
1022 	/* cp page 1 of check point pack 2
1023 	 * Initialize other checkpoint pack with version zero
1024 	 */
1025 	cp->checkpoint_ver = 0;
1026 
1027 	crc = f2fs_checkpoint_chksum(cp);
1028 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1029 							cpu_to_le32(crc);
1030 	cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1031 	DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1032 				cp_seg_blk);
1033 	if (dev_write_block(cp, cp_seg_blk)) {
1034 		MSG(1, "\tError: While writing the cp to disk!!!\n");
1035 		goto free_cp_payload;
1036 	}
1037 
1038 	for (i = 0; i < get_sb(cp_payload); i++) {
1039 		cp_seg_blk++;
1040 		if (dev_fill_block(cp_payload, cp_seg_blk)) {
1041 			MSG(1, "\tError: While zeroing out the sit bitmap area "
1042 					"on disk!!!\n");
1043 			goto free_cp_payload;
1044 		}
1045 	}
1046 
1047 	/* cp page 2 of check point pack 2 */
1048 	cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1049 					get_sb(cp_payload) - 1);
1050 	DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1051 				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 	ret = 0;
1058 
1059 free_cp_payload:
1060 	free(cp_payload);
1061 free_nat_bits:
1062 	free(nat_bits);
1063 free_sum_compact:
1064 	free(sum_compact);
1065 free_sum:
1066 	free(sum);
1067 free_cp:
1068 	free(cp);
1069 	return ret;
1070 }
1071 
f2fs_write_super_block(void)1072 static int f2fs_write_super_block(void)
1073 {
1074 	int index;
1075 	uint8_t *zero_buff;
1076 
1077 	zero_buff = calloc(F2FS_BLKSIZE, 1);
1078 	if (zero_buff == NULL) {
1079 		MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1080 		return -1;
1081 	}
1082 
1083 	memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1084 	DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1085 	for (index = 0; index < 2; index++) {
1086 		if (dev_write_block(zero_buff, index)) {
1087 			MSG(1, "\tError: While while writing super_blk "
1088 					"on disk!!! index : %d\n", index);
1089 			free(zero_buff);
1090 			return -1;
1091 		}
1092 	}
1093 
1094 	free(zero_buff);
1095 	return 0;
1096 }
1097 
1098 #ifndef WITH_ANDROID
f2fs_discard_obsolete_dnode(void)1099 static int f2fs_discard_obsolete_dnode(void)
1100 {
1101 	struct f2fs_node *raw_node;
1102 	uint64_t next_blkaddr = 0, offset;
1103 	u64 end_blkaddr = (get_sb(segment_count_main) <<
1104 			get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1105 	uint64_t start_inode_pos = get_sb(main_blkaddr);
1106 	uint64_t last_inode_pos;
1107 
1108 	if (c.zoned_mode || c.feature & F2FS_FEATURE_RO)
1109 		return 0;
1110 
1111 	raw_node = calloc(F2FS_BLKSIZE, 1);
1112 	if (raw_node == NULL) {
1113 		MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1114 		return -1;
1115 	}
1116 
1117 	/* avoid power-off-recovery based on roll-forward policy */
1118 	offset = get_sb(main_blkaddr);
1119 	offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1120 
1121 	last_inode_pos = start_inode_pos +
1122 		c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1123 		c.curseg_offset[CURSEG_COLD_NODE] - 1;
1124 
1125 	do {
1126 		if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1127 			break;
1128 
1129 		if (dev_read_block(raw_node, offset)) {
1130 			MSG(1, "\tError: While traversing direct node!!!\n");
1131 			free(raw_node);
1132 			return -1;
1133 		}
1134 
1135 		next_blkaddr = le32_to_cpu(F2FS_NODE_FOOTER(raw_node)->next_blkaddr);
1136 		memset(raw_node, 0, F2FS_BLKSIZE);
1137 
1138 		DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1139 		if (dev_write_block(raw_node, offset)) {
1140 			MSG(1, "\tError: While discarding direct node!!!\n");
1141 			free(raw_node);
1142 			return -1;
1143 		}
1144 		offset = next_blkaddr;
1145 		/* should avoid recursive chain due to stale data */
1146 		if (offset >= start_inode_pos || offset <= last_inode_pos)
1147 			break;
1148 	} while (1);
1149 
1150 	free(raw_node);
1151 	return 0;
1152 }
1153 #endif
1154 
alloc_next_free_block(int curseg_type)1155 static block_t alloc_next_free_block(int curseg_type)
1156 {
1157 	block_t blkaddr;
1158 
1159 	blkaddr = get_sb(main_blkaddr) +
1160 			c.cur_seg[curseg_type] * c.blks_per_seg +
1161 			c.curseg_offset[curseg_type];
1162 
1163 	c.curseg_offset[curseg_type]++;
1164 
1165 	return blkaddr;
1166 }
1167 
update_sit_journal(int curseg_type)1168 void update_sit_journal(int curseg_type)
1169 {
1170 	struct f2fs_journal *sit_jnl = &c.sit_jnl;
1171 	unsigned short vblocks;
1172 	int idx = curseg_type;
1173 
1174 	if (c.feature & F2FS_FEATURE_RO) {
1175 		if (curseg_type < NR_CURSEG_DATA_TYPE)
1176 			idx = CURSEG_RO_HOT_DATA;
1177 		else
1178 			idx = CURSEG_RO_HOT_NODE;
1179 	}
1180 
1181 	f2fs_set_bit(c.curseg_offset[curseg_type] - 1,
1182 		(char *)sit_jnl->sit_j.entries[idx].se.valid_map);
1183 
1184 	vblocks = le16_to_cpu(sit_jnl->sit_j.entries[idx].se.vblocks);
1185 	sit_jnl->sit_j.entries[idx].se.vblocks = cpu_to_le16(vblocks + 1);
1186 }
1187 
update_nat_journal(nid_t nid,block_t blkaddr)1188 void update_nat_journal(nid_t nid, block_t blkaddr)
1189 {
1190 	struct f2fs_journal *nat_jnl = &c.nat_jnl;
1191 	unsigned short n_nats = le16_to_cpu(nat_jnl->n_nats);
1192 
1193 	nat_jnl->nat_j.entries[n_nats].nid = cpu_to_le32(nid);
1194 	nat_jnl->nat_j.entries[n_nats].ne.version = 0;
1195 	nat_jnl->nat_j.entries[n_nats].ne.ino = cpu_to_le32(nid);
1196 	nat_jnl->nat_j.entries[n_nats].ne.block_addr = cpu_to_le32(blkaddr);
1197 	nat_jnl->n_nats = cpu_to_le16(n_nats + 1);
1198 }
1199 
update_summary_entry(int curseg_type,nid_t nid,unsigned short ofs_in_node)1200 void update_summary_entry(int curseg_type, nid_t nid,
1201 					unsigned short ofs_in_node)
1202 {
1203 	struct f2fs_summary *sum;
1204 	unsigned int curofs = c.curseg_offset[curseg_type] - 1;
1205 
1206 	assert(curofs < MAX_CACHE_SUMS);
1207 
1208 	sum = c.sum[curseg_type] + curofs;
1209 	sum->nid = cpu_to_le32(nid);
1210 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
1211 }
1212 
f2fs_add_default_dentry_root(void)1213 static block_t f2fs_add_default_dentry_root(void)
1214 {
1215 	struct f2fs_dentry_block *dent_blk = NULL;
1216 	block_t data_blkaddr;
1217 
1218 	dent_blk = calloc(F2FS_BLKSIZE, 1);
1219 	if(dent_blk == NULL) {
1220 		MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1221 		return 0;
1222 	}
1223 
1224 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).hash_code = 0;
1225 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).ino = sb->root_ino;
1226 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).name_len = cpu_to_le16(1);
1227 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).file_type = F2FS_FT_DIR;
1228 	memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 0), ".", 1);
1229 
1230 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).hash_code = 0;
1231 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).ino = sb->root_ino;
1232 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).name_len = cpu_to_le16(2);
1233 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).file_type = F2FS_FT_DIR;
1234 	memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 1), "..", 2);
1235 
1236 	/* bitmap for . and .. */
1237 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1238 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1239 
1240 	if (c.lpf_ino) {
1241 		int len = strlen(LPF);
1242 		f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1243 
1244 		F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 2).hash_code = cpu_to_le32(hash);
1245 		F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 2).ino = cpu_to_le32(c.lpf_ino);
1246 		F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 2).name_len = cpu_to_le16(len);
1247 		F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 2).file_type = F2FS_FT_DIR;
1248 		memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 2), LPF, F2FS_SLOT_LEN);
1249 
1250 		memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 3), &LPF[F2FS_SLOT_LEN],
1251 				len - F2FS_SLOT_LEN);
1252 
1253 		test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1254 		test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1255 	}
1256 
1257 	data_blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1258 
1259 	DBG(1, "\tWriting default dentry root, at offset 0x%x\n", data_blkaddr);
1260 	if (dev_write_block(dent_blk, data_blkaddr)) {
1261 		MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1262 		free(dent_blk);
1263 		return 0;
1264 	}
1265 
1266 	update_sit_journal(CURSEG_HOT_DATA);
1267 	update_summary_entry(CURSEG_HOT_DATA, le32_to_cpu(sb->root_ino), 0);
1268 
1269 	free(dent_blk);
1270 	return data_blkaddr;
1271 }
1272 
f2fs_write_root_inode(void)1273 static int f2fs_write_root_inode(void)
1274 {
1275 	struct f2fs_node *raw_node = NULL;
1276 	block_t data_blkaddr;
1277 	block_t node_blkaddr;
1278 
1279 	raw_node = calloc(F2FS_BLKSIZE, 1);
1280 	if (raw_node == NULL) {
1281 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1282 		return -1;
1283 	}
1284 
1285 	f2fs_init_inode(sb, raw_node, le32_to_cpu(sb->root_ino),
1286 						mkfs_time, 0x41ed);
1287 
1288 	if (c.lpf_ino)
1289 		raw_node->i.i_links = cpu_to_le32(3);
1290 
1291 	data_blkaddr = f2fs_add_default_dentry_root();
1292 	if (data_blkaddr == 0) {
1293 		MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1294 		free(raw_node);
1295 		return -1;
1296 	}
1297 
1298 	raw_node->i.i_addr[get_extra_isize(raw_node)] =
1299 				cpu_to_le32(data_blkaddr);
1300 
1301 	node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1302 	F2FS_NODE_FOOTER(raw_node)->next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1303 
1304 	DBG(1, "\tWriting root inode (hot node), offset 0x%x\n", node_blkaddr);
1305 	if (write_inode(raw_node, node_blkaddr) < 0) {
1306 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1307 		free(raw_node);
1308 		return -1;
1309 	}
1310 
1311 	update_nat_journal(le32_to_cpu(sb->root_ino), node_blkaddr);
1312 	update_sit_journal(CURSEG_HOT_NODE);
1313 	update_summary_entry(CURSEG_HOT_NODE, le32_to_cpu(sb->root_ino), 0);
1314 
1315 	free(raw_node);
1316 	return 0;
1317 }
1318 
f2fs_write_default_quota(int qtype,__le32 raw_id)1319 static int f2fs_write_default_quota(int qtype, __le32 raw_id)
1320 {
1321 	char *filebuf = calloc(F2FS_BLKSIZE, 2);
1322 	int file_magics[] = INITQMAGICS;
1323 	struct v2_disk_dqheader ddqheader;
1324 	struct v2_disk_dqinfo ddqinfo;
1325 	struct v2r1_disk_dqblk dqblk;
1326 	block_t blkaddr;
1327 	int i;
1328 
1329 	if (filebuf == NULL) {
1330 		MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1331 		return 0;
1332 	}
1333 
1334 	/* Write basic quota header */
1335 	ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1336 	/* only support QF_VFSV1 */
1337 	ddqheader.dqh_version = cpu_to_le32(1);
1338 
1339 	memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1340 
1341 	/* Fill Initial quota file content */
1342 	ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1343 	ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1344 	ddqinfo.dqi_flags = cpu_to_le32(0);
1345 	ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1346 	ddqinfo.dqi_free_blk = cpu_to_le32(0);
1347 	ddqinfo.dqi_free_entry = cpu_to_le32(5);
1348 
1349 	memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1350 
1351 	filebuf[1024] = 2;
1352 	filebuf[2048] = 3;
1353 	filebuf[3072] = 4;
1354 	filebuf[4096] = 5;
1355 
1356 	filebuf[5120 + 8] = 1;
1357 
1358 	dqblk.dqb_id = raw_id;
1359 	dqblk.dqb_pad = cpu_to_le32(0);
1360 	dqblk.dqb_ihardlimit = cpu_to_le64(0);
1361 	dqblk.dqb_isoftlimit = cpu_to_le64(0);
1362 	if (c.lpf_ino)
1363 		dqblk.dqb_curinodes = cpu_to_le64(2);
1364 	else
1365 		dqblk.dqb_curinodes = cpu_to_le64(1);
1366 	dqblk.dqb_bhardlimit = cpu_to_le64(0);
1367 	dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1368 	if (c.lpf_ino)
1369 		dqblk.dqb_curspace = cpu_to_le64(F2FS_BLKSIZE * 2);
1370 	else
1371 		dqblk.dqb_curspace = cpu_to_le64(F2FS_BLKSIZE);
1372 	dqblk.dqb_btime = cpu_to_le64(0);
1373 	dqblk.dqb_itime = cpu_to_le64(0);
1374 
1375 	memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1376 
1377 	/* Write quota blocks */
1378 	for (i = 0; i < QUOTA_DATA; i++) {
1379 		blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1380 
1381 		if (dev_write_block(filebuf + i * F2FS_BLKSIZE, blkaddr)) {
1382 			MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1383 			free(filebuf);
1384 			return 0;
1385 		}
1386 
1387 		update_sit_journal(CURSEG_HOT_DATA);
1388 		update_summary_entry(CURSEG_HOT_DATA,
1389 					le32_to_cpu(sb->qf_ino[qtype]), i);
1390 		DBG(1, "\tWriting quota data, at offset %08x (%d/%d)\n",
1391 						blkaddr, i + 1, QUOTA_DATA);
1392 
1393 	}
1394 
1395 	free(filebuf);
1396 	return blkaddr + 1 - QUOTA_DATA;
1397 }
1398 
f2fs_write_qf_inode(int qtype)1399 static int f2fs_write_qf_inode(int qtype)
1400 {
1401 	struct f2fs_node *raw_node = NULL;
1402 	block_t data_blkaddr;
1403 	block_t node_blkaddr;
1404 	__le32 raw_id;
1405 	int i;
1406 
1407 	raw_node = calloc(F2FS_BLKSIZE, 1);
1408 	if (raw_node == NULL) {
1409 		MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1410 		return -1;
1411 	}
1412 	f2fs_init_inode(sb, raw_node,
1413 			le32_to_cpu(sb->qf_ino[qtype]), mkfs_time, 0x8180);
1414 
1415 	raw_node->i.i_size = cpu_to_le64(1024 * 6);
1416 	raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA);
1417 	raw_node->i.i_flags = F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
1418 
1419 	node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1420 	F2FS_NODE_FOOTER(raw_node)->next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1421 
1422 	if (qtype == 0)
1423 		raw_id = raw_node->i.i_uid;
1424 	else if (qtype == 1)
1425 		raw_id = raw_node->i.i_gid;
1426 	else if (qtype == 2)
1427 		raw_id = raw_node->i.i_projid;
1428 	else
1429 		ASSERT(0);
1430 
1431 	/* write quota blocks */
1432 	data_blkaddr = f2fs_write_default_quota(qtype, raw_id);
1433 	if (data_blkaddr == 0) {
1434 		free(raw_node);
1435 		return -1;
1436 	}
1437 
1438 	for (i = 0; i < QUOTA_DATA; i++)
1439 		raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1440 					cpu_to_le32(data_blkaddr + i);
1441 
1442 	DBG(1, "\tWriting quota inode (hot node), offset 0x%x\n", node_blkaddr);
1443 	if (write_inode(raw_node, node_blkaddr) < 0) {
1444 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1445 		free(raw_node);
1446 		return -1;
1447 	}
1448 
1449 	update_nat_journal(le32_to_cpu(sb->qf_ino[qtype]), node_blkaddr);
1450 	update_sit_journal(CURSEG_HOT_NODE);
1451 	update_summary_entry(CURSEG_HOT_NODE, le32_to_cpu(sb->qf_ino[qtype]), 0);
1452 
1453 	free(raw_node);
1454 	return 0;
1455 }
1456 
f2fs_update_nat_default(void)1457 static int f2fs_update_nat_default(void)
1458 {
1459 	struct f2fs_nat_block *nat_blk = NULL;
1460 	uint64_t nat_seg_blk_offset = 0;
1461 
1462 	nat_blk = calloc(F2FS_BLKSIZE, 1);
1463 	if(nat_blk == NULL) {
1464 		MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1465 		return -1;
1466 	}
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 	block_t data_blkaddr;
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 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).hash_code = 0;
1502 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).ino = cpu_to_le32(c.lpf_ino);
1503 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).name_len = cpu_to_le16(1);
1504 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 0).file_type = F2FS_FT_DIR;
1505 	memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 0), ".", 1);
1506 
1507 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).hash_code = 0;
1508 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).ino = sb->root_ino;
1509 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).name_len = cpu_to_le16(2);
1510 	F2FS_DENTRY_BLOCK_DENTRY(dent_blk, 1).file_type = F2FS_FT_DIR;
1511 	memcpy(F2FS_DENTRY_BLOCK_FILENAME(dent_blk, 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_blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1517 
1518 	DBG(1, "\tWriting default dentry lost+found, at offset 0x%x\n",
1519 							data_blkaddr);
1520 	if (dev_write_block(dent_blk, data_blkaddr)) {
1521 		MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1522 		free(dent_blk);
1523 		return 0;
1524 	}
1525 
1526 	update_sit_journal(CURSEG_HOT_DATA);
1527 	update_summary_entry(CURSEG_HOT_DATA, c.lpf_ino, 0);
1528 
1529 	free(dent_blk);
1530 	return data_blkaddr;
1531 }
1532 
f2fs_write_lpf_inode(void)1533 static int f2fs_write_lpf_inode(void)
1534 {
1535 	struct f2fs_node *raw_node;
1536 	block_t data_blkaddr;
1537 	block_t node_blkaddr;
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 	f2fs_init_inode(sb, raw_node, c.lpf_ino, mkfs_time, 0x41c0);
1549 
1550 	raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1551 	raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1552 	memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1553 
1554 	node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1555 	F2FS_NODE_FOOTER(raw_node)->next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1556 
1557 	data_blkaddr = f2fs_add_default_dentry_lpf();
1558 	if (data_blkaddr == 0) {
1559 		MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1560 		err = -1;
1561 		goto exit;
1562 	}
1563 	raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blkaddr);
1564 
1565 	DBG(1, "\tWriting lost+found inode (hot node), offset 0x%x\n",
1566 								node_blkaddr);
1567 	if (write_inode(raw_node, node_blkaddr) < 0) {
1568 		MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1569 		err = -1;
1570 		goto exit;
1571 	}
1572 
1573 	update_nat_journal(c.lpf_ino, node_blkaddr);
1574 	update_sit_journal(CURSEG_HOT_NODE);
1575 	update_summary_entry(CURSEG_HOT_NODE, c.lpf_ino, 0);
1576 
1577 exit:
1578 	free(raw_node);
1579 	return err;
1580 }
1581 
f2fs_create_root_dir(void)1582 static int f2fs_create_root_dir(void)
1583 {
1584 	enum quota_type qtype;
1585 	int err = 0;
1586 
1587 	err = f2fs_write_root_inode();
1588 	if (err < 0) {
1589 		MSG(1, "\tError: Failed to write root inode!!!\n");
1590 		goto exit;
1591 	}
1592 
1593 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++)  {
1594 		if (!((1 << qtype) & c.quota_bits))
1595 			continue;
1596 		err = f2fs_write_qf_inode(qtype);
1597 		if (err < 0) {
1598 			MSG(1, "\tError: Failed to write quota inode!!!\n");
1599 			goto exit;
1600 		}
1601 	}
1602 
1603 	if (c.feature & F2FS_FEATURE_LOST_FOUND) {
1604 		err = f2fs_write_lpf_inode();
1605 		if (err < 0) {
1606 			MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1607 			goto exit;
1608 		}
1609 	}
1610 
1611 #ifndef WITH_ANDROID
1612 	err = f2fs_discard_obsolete_dnode();
1613 	if (err < 0) {
1614 		MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1615 		goto exit;
1616 	}
1617 #endif
1618 
1619 	err = f2fs_update_nat_default();
1620 	if (err < 0) {
1621 		MSG(1, "\tError: Failed to update NAT for root!!!\n");
1622 		goto exit;
1623 	}
1624 exit:
1625 	if (err)
1626 		MSG(1, "\tError: Could not create the root directory!!!\n");
1627 
1628 	return err;
1629 }
1630 
f2fs_format_device(void)1631 int f2fs_format_device(void)
1632 {
1633 	int err = 0;
1634 
1635 	err= f2fs_prepare_super_block();
1636 	if (err < 0) {
1637 		MSG(0, "\tError: Failed to prepare a super block!!!\n");
1638 		goto exit;
1639 	}
1640 
1641 	if (c.trim) {
1642 		err = f2fs_trim_devices();
1643 		if (err < 0) {
1644 			MSG(0, "\tError: Failed to trim whole device!!!\n");
1645 			goto exit;
1646 		}
1647 	}
1648 
1649 	err = f2fs_init_sit_area();
1650 	if (err < 0) {
1651 		MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1652 		goto exit;
1653 	}
1654 
1655 	err = f2fs_init_nat_area();
1656 	if (err < 0) {
1657 		MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1658 		goto exit;
1659 	}
1660 
1661 	err = f2fs_create_root_dir();
1662 	if (err < 0) {
1663 		MSG(0, "\tError: Failed to create the root directory!!!\n");
1664 		goto exit;
1665 	}
1666 
1667 	err = f2fs_write_check_point_pack();
1668 	if (err < 0) {
1669 		MSG(0, "\tError: Failed to write the check point pack!!!\n");
1670 		goto exit;
1671 	}
1672 
1673 	err = f2fs_write_super_block();
1674 	if (err < 0) {
1675 		MSG(0, "\tError: Failed to write the super block!!!\n");
1676 		goto exit;
1677 	}
1678 exit:
1679 	if (err)
1680 		MSG(0, "\tError: Could not format the device!!!\n");
1681 
1682 	return err;
1683 }
1684