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