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