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