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