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