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