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