4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Dual licensed under the GPL or LGPL version 2 licenses.
9 #define _LARGEFILE64_SOURCE
19 #ifdef HAVE_SYS_STAT_H
22 #ifdef HAVE_SYS_MOUNT_H
23 #include <sys/mount.h>
27 #ifdef HAVE_UUID_UUID_H
28 #include <uuid/uuid.h>
31 #define uuid_parse(a, b) -1
32 #define uuid_generate(a)
33 #define uuid_unparse(a, b) -1
37 #include "f2fs_format_utils.h"
39 extern struct f2fs_configuration c;
40 struct f2fs_super_block raw_sb;
41 struct f2fs_super_block *sb = &raw_sb;
42 struct f2fs_checkpoint *cp;
44 /* Return first segment number of each area */
45 #define prev_zone(cur) (c.cur_seg[cur] - c.segs_per_zone)
46 #define next_zone(cur) (c.cur_seg[cur] + c.segs_per_zone)
47 #define last_zone(cur) ((cur - 1) * c.segs_per_zone)
48 #define last_section(cur) (cur + (c.secs_per_zone - 1) * c.segs_per_sec)
50 /* Return time fixed by the user or current time by default */
51 #define mkfs_time ((c.fixed_time == -1) ? time(NULL) : c.fixed_time)
53 const char *media_ext_lists[] = {
55 "mp", // Covers mp3, mp4, mpeg, mpg
56 "wm", // Covers wma, wmb, wmv
57 "og", // Covers oga, ogg, ogm, ogv
58 "jp", // Covers jpg, jpeg, jp2
91 "pyc", // Python bytecode
100 "odex", // Android RunTime
101 "vdex", // Android RunTime
107 const char *hot_ext_lists[] = {
111 /* Virtual machines */
112 "vmdk", // VMware or VirtualBox
119 const char **default_ext_list[] = {
124 static bool is_extension_exist(const char *name)
128 for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
129 char *ext = (char *)sb->extension_list[i];
130 if (!strcmp(ext, name))
137 static void cure_extension_list(void)
139 const char **extlist;
145 set_sb(extension_count, 0);
146 memset(sb->extension_list, 0, sizeof(sb->extension_list));
148 for (i = 0; i < 2; i++) {
149 ext_str = c.extension_list[i];
150 extlist = default_ext_list[i];
153 name_len = strlen(*extlist);
154 memcpy(sb->extension_list[pos++], *extlist, name_len);
158 set_sb(extension_count, pos);
160 sb->hot_ext_count = pos - get_sb(extension_count);;
165 /* add user ext list */
166 ue = strtok(ext_str, ", ");
168 name_len = strlen(ue);
169 if (name_len >= F2FS_EXTENSION_LEN) {
170 MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
173 if (!is_extension_exist(ue))
174 memcpy(sb->extension_list[pos++], ue, name_len);
176 ue = strtok(NULL, ", ");
177 if (pos >= F2FS_MAX_EXTENSION)
182 set_sb(extension_count, pos);
184 sb->hot_ext_count = pos - get_sb(extension_count);
186 free(c.extension_list[i]);
190 static void verify_cur_segs(void)
195 for (i = 0; i < NR_CURSEG_TYPE; i++) {
196 for (j = i + 1; j < NR_CURSEG_TYPE; j++) {
197 if (c.cur_seg[i] == c.cur_seg[j]) {
208 for (i = 1; i < NR_CURSEG_TYPE; i++)
209 c.cur_seg[i] = next_zone(i - 1);
212 static int f2fs_prepare_super_block(void)
214 uint32_t blk_size_bytes;
215 uint32_t log_sectorsize, log_sectors_per_block;
216 uint32_t log_blocksize, log_blks_per_seg;
217 uint32_t segment_size_bytes, zone_size_bytes;
218 uint32_t sit_segments, nat_segments;
219 uint32_t blocks_for_sit, blocks_for_nat, blocks_for_ssa;
220 uint32_t total_valid_blks_available;
221 uint64_t zone_align_start_offset, diff;
222 uint64_t total_meta_zones, total_meta_segments;
223 uint32_t sit_bitmap_size, max_sit_bitmap_size;
224 uint32_t max_nat_bitmap_size, max_nat_segments;
225 uint32_t total_zones, avail_zones;
226 enum quota_type qtype;
229 set_sb(magic, F2FS_SUPER_MAGIC);
230 set_sb(major_ver, F2FS_MAJOR_VERSION);
231 set_sb(minor_ver, F2FS_MINOR_VERSION);
233 log_sectorsize = log_base_2(c.sector_size);
234 log_sectors_per_block = log_base_2(c.sectors_per_blk);
235 log_blocksize = log_sectorsize + log_sectors_per_block;
236 log_blks_per_seg = log_base_2(c.blks_per_seg);
238 set_sb(log_sectorsize, log_sectorsize);
239 set_sb(log_sectors_per_block, log_sectors_per_block);
241 set_sb(log_blocksize, log_blocksize);
242 set_sb(log_blocks_per_seg, log_blks_per_seg);
244 set_sb(segs_per_sec, c.segs_per_sec);
245 set_sb(secs_per_zone, c.secs_per_zone);
247 blk_size_bytes = 1 << log_blocksize;
248 segment_size_bytes = blk_size_bytes * c.blks_per_seg;
250 blk_size_bytes * c.secs_per_zone *
251 c.segs_per_sec * c.blks_per_seg;
253 set_sb(checksum_offset, 0);
255 set_sb(block_count, c.total_sectors >> log_sectors_per_block);
257 zone_align_start_offset =
258 ((uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE +
259 2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
260 zone_size_bytes * zone_size_bytes -
261 (uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE;
263 if (c.feature & F2FS_FEATURE_RO)
264 zone_align_start_offset = 8192;
266 if (c.start_sector % DEFAULT_SECTORS_PER_BLOCK) {
267 MSG(1, "\t%s: Align start sector number to the page unit\n",
268 c.zoned_mode ? "FAIL" : "WARN");
269 MSG(1, "\ti.e., start sector: %d, ofs:%d (sects/page: %d)\n",
271 c.start_sector % DEFAULT_SECTORS_PER_BLOCK,
272 DEFAULT_SECTORS_PER_BLOCK);
277 if (c.zoned_mode && c.ndevs > 1)
278 zone_align_start_offset +=
279 (c.devices[0].total_sectors * c.sector_size) % zone_size_bytes;
281 set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
282 sb->cp_blkaddr = sb->segment0_blkaddr;
284 MSG(0, "Info: zone aligned segment0 blkaddr: %u\n",
285 get_sb(segment0_blkaddr));
289 (get_sb(segment0_blkaddr) + c.start_sector /
290 DEFAULT_SECTORS_PER_BLOCK) % c.zone_blocks) ||
292 c.devices[1].start_blkaddr % c.zone_blocks))) {
293 MSG(1, "\tError: Unaligned segment0 block address %u\n",
294 get_sb(segment0_blkaddr));
298 for (i = 0; i < c.ndevs; i++) {
300 c.devices[i].total_segments =
301 (c.devices[i].total_sectors *
302 c.sector_size - zone_align_start_offset) /
304 c.devices[i].start_blkaddr = 0;
305 c.devices[i].end_blkaddr = c.devices[i].total_segments *
307 sb->segment0_blkaddr;
309 c.devices[i].total_segments =
310 c.devices[i].total_sectors /
311 (c.sectors_per_blk * c.blks_per_seg);
312 c.devices[i].start_blkaddr =
313 c.devices[i - 1].end_blkaddr + 1;
314 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
315 c.devices[i].total_segments *
319 memcpy(sb->devs[i].path, c.devices[i].path, MAX_PATH_LEN);
320 sb->devs[i].total_segments =
321 cpu_to_le32(c.devices[i].total_segments);
324 c.total_segments += c.devices[i].total_segments;
326 set_sb(segment_count, (c.total_segments / c.segs_per_zone *
328 set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
330 set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
331 get_sb(segment_count_ckpt) * c.blks_per_seg);
333 blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
335 sit_segments = SEG_ALIGN(blocks_for_sit);
337 set_sb(segment_count_sit, sit_segments * 2);
339 set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
342 total_valid_blks_available = (get_sb(segment_count) -
343 (get_sb(segment_count_ckpt) +
344 get_sb(segment_count_sit))) * c.blks_per_seg;
346 blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
347 NAT_ENTRY_PER_BLOCK);
349 if (c.large_nat_bitmap) {
350 nat_segments = SEG_ALIGN(blocks_for_nat) *
351 DEFAULT_NAT_ENTRY_RATIO / 100;
352 set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
353 max_nat_bitmap_size = (get_sb(segment_count_nat) <<
354 log_blks_per_seg) / 8;
355 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
357 set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
358 max_nat_bitmap_size = 0;
362 * The number of node segments should not be exceeded a "Threshold".
363 * This number resizes NAT bitmap area in a CP page.
364 * So the threshold is determined not to overflow one CP page
366 sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
367 log_blks_per_seg) / 8;
369 if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
370 max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
372 max_sit_bitmap_size = sit_bitmap_size;
374 if (c.large_nat_bitmap) {
375 /* use cp_payload if free space of f2fs_checkpoint is not enough */
376 if (max_sit_bitmap_size + max_nat_bitmap_size >
377 MAX_BITMAP_SIZE_IN_CKPT) {
378 uint32_t diff = max_sit_bitmap_size +
379 max_nat_bitmap_size -
380 MAX_BITMAP_SIZE_IN_CKPT;
381 set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
383 set_sb(cp_payload, 0);
387 * It should be reserved minimum 1 segment for nat.
388 * When sit is too large, we should expand cp area.
389 * It requires more pages for cp.
391 if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
392 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
393 set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
395 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
397 set_sb(cp_payload, 0);
399 max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
401 if (get_sb(segment_count_nat) > max_nat_segments)
402 set_sb(segment_count_nat, max_nat_segments);
404 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
407 set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
410 total_valid_blks_available = (get_sb(segment_count) -
411 (get_sb(segment_count_ckpt) +
412 get_sb(segment_count_sit) +
413 get_sb(segment_count_nat))) *
416 if (c.feature & F2FS_FEATURE_RO)
419 blocks_for_ssa = total_valid_blks_available /
422 set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
424 total_meta_segments = get_sb(segment_count_ckpt) +
425 get_sb(segment_count_sit) +
426 get_sb(segment_count_nat) +
427 get_sb(segment_count_ssa);
428 diff = total_meta_segments % (c.segs_per_zone);
430 set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
431 (c.segs_per_zone - diff));
433 total_meta_zones = ZONE_ALIGN(total_meta_segments *
436 set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
437 c.segs_per_zone * c.blks_per_seg);
441 * Make sure there is enough randomly writeable
442 * space at the beginning of the disk.
444 unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
446 if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
447 c.devices[0].nr_rnd_zones < main_blkzone) {
448 MSG(0, "\tError: Device does not have enough random "
449 "write zones for F2FS volume (%lu needed)\n",
454 * Check if conventional device has enough space
455 * to accommodate all metadata, zoned device should
456 * not overlap to metadata area.
458 for (i = 1; i < c.ndevs; i++) {
459 if (c.devices[i].zoned_model != F2FS_ZONED_NONE &&
460 c.devices[i].start_blkaddr < get_sb(main_blkaddr)) {
461 MSG(0, "\tError: Conventional device %s is too small,"
462 " (%"PRIu64" MiB needed).\n", c.devices[0].path,
463 (get_sb(main_blkaddr) -
464 c.devices[i].start_blkaddr) >> 8);
470 total_zones = get_sb(segment_count) / (c.segs_per_zone) -
472 if (total_zones == 0)
474 set_sb(section_count, total_zones * c.secs_per_zone);
476 set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
479 * Let's determine the best reserved and overprovisioned space.
480 * For Zoned device, if zone capacity less than zone size, the segments
481 * starting after the zone capacity are unusable in each zone. So get
482 * overprovision ratio and reserved seg count based on avg usable
485 if (c.overprovision == 0)
486 c.overprovision = get_best_overprovision(sb);
488 c.reserved_segments = get_reserved(sb, c.overprovision);
490 if (c.feature & F2FS_FEATURE_RO) {
492 c.reserved_segments = 0;
494 if ((!(c.feature & F2FS_FEATURE_RO) &&
495 c.overprovision == 0) ||
496 c.total_segments < F2FS_MIN_SEGMENTS ||
497 (c.devices[0].total_sectors *
498 c.sector_size < zone_align_start_offset) ||
499 (get_sb(segment_count_main) - NR_CURSEG_TYPE) <
500 c.reserved_segments) {
505 if (uuid_parse(c.vol_uuid, sb->uuid)) {
506 MSG(0, "\tError: supplied string is not a valid UUID\n");
510 uuid_generate(sb->uuid);
513 /* precompute checksum seed for metadata */
514 if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
515 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
517 utf8_to_utf16((char *)sb->volume_name, (const char *)c.vol_label,
518 MAX_VOLUME_NAME, strlen(c.vol_label));
524 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
525 if (!((1 << qtype) & c.quota_bits))
527 sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
528 MSG(0, "Info: add quota type = %u => %u\n",
529 qtype, c.next_free_nid - 1);
532 if (c.feature & F2FS_FEATURE_LOST_FOUND)
533 c.lpf_ino = c.next_free_nid++;
535 if (c.feature & F2FS_FEATURE_RO)
540 if (total_zones <= avail_zones) {
541 MSG(1, "\tError: %d zones: Need more zones "
542 "by shrinking zone size\n", total_zones);
546 if (c.feature & F2FS_FEATURE_RO) {
547 c.cur_seg[CURSEG_HOT_NODE] = last_section(last_zone(total_zones));
548 c.cur_seg[CURSEG_WARM_NODE] = 0;
549 c.cur_seg[CURSEG_COLD_NODE] = 0;
550 c.cur_seg[CURSEG_HOT_DATA] = 0;
551 c.cur_seg[CURSEG_COLD_DATA] = 0;
552 c.cur_seg[CURSEG_WARM_DATA] = 0;
554 c.cur_seg[CURSEG_HOT_NODE] =
555 last_section(last_zone(total_zones));
556 c.cur_seg[CURSEG_WARM_NODE] = prev_zone(CURSEG_HOT_NODE);
557 c.cur_seg[CURSEG_COLD_NODE] = prev_zone(CURSEG_WARM_NODE);
558 c.cur_seg[CURSEG_HOT_DATA] = prev_zone(CURSEG_COLD_NODE);
559 c.cur_seg[CURSEG_COLD_DATA] = 0;
560 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_COLD_DATA);
561 } else if (c.zoned_mode) {
562 c.cur_seg[CURSEG_HOT_NODE] = 0;
563 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
564 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
565 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
566 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_HOT_DATA);
567 c.cur_seg[CURSEG_COLD_DATA] = next_zone(CURSEG_WARM_DATA);
569 c.cur_seg[CURSEG_HOT_NODE] = 0;
570 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
571 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
572 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
573 c.cur_seg[CURSEG_COLD_DATA] =
574 max(last_zone((total_zones >> 2)),
575 next_zone(CURSEG_HOT_DATA));
576 c.cur_seg[CURSEG_WARM_DATA] =
577 max(last_zone((total_zones >> 1)),
578 next_zone(CURSEG_COLD_DATA));
581 /* if there is redundancy, reassign it */
582 if (!(c.feature & F2FS_FEATURE_RO))
585 cure_extension_list();
587 /* get kernel version */
589 dev_read_version(c.version, 0, VERSION_LEN);
590 get_kernel_version(c.version);
592 get_kernel_uname_version(c.version);
594 MSG(0, "Info: format version with\n \"%s\"\n", c.version);
596 memcpy(sb->version, c.version, VERSION_LEN);
597 memcpy(sb->init_version, c.version, VERSION_LEN);
599 if (c.feature & F2FS_FEATURE_CASEFOLD) {
600 set_sb(s_encoding, c.s_encoding);
601 set_sb(s_encoding_flags, c.s_encoding_flags);
604 sb->feature = cpu_to_le32(c.feature);
606 if (c.feature & F2FS_FEATURE_SB_CHKSUM) {
607 set_sb(checksum_offset, SB_CHKSUM_OFFSET);
608 set_sb(crc, f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
610 MSG(1, "Info: SB CRC is set: offset (%d), crc (0x%x)\n",
611 get_sb(checksum_offset), get_sb(crc));
617 MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
621 static int f2fs_init_sit_area(void)
623 uint32_t blk_size, seg_size;
625 uint64_t sit_seg_addr = 0;
626 uint8_t *zero_buf = NULL;
628 blk_size = 1 << get_sb(log_blocksize);
629 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
631 zero_buf = calloc(sizeof(uint8_t), seg_size);
632 if(zero_buf == NULL) {
633 MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
637 sit_seg_addr = get_sb(sit_blkaddr);
638 sit_seg_addr *= blk_size;
640 DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
641 for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
642 if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
643 MSG(1, "\tError: While zeroing out the sit area "
648 sit_seg_addr += seg_size;
655 static int f2fs_init_nat_area(void)
657 uint32_t blk_size, seg_size;
659 uint64_t nat_seg_addr = 0;
660 uint8_t *nat_buf = NULL;
662 blk_size = 1 << get_sb(log_blocksize);
663 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
665 nat_buf = calloc(sizeof(uint8_t), seg_size);
666 if (nat_buf == NULL) {
667 MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
671 nat_seg_addr = get_sb(nat_blkaddr);
672 nat_seg_addr *= blk_size;
674 DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
675 for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
676 if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
677 MSG(1, "\tError: While zeroing out the nat area "
682 nat_seg_addr = nat_seg_addr + (2 * seg_size);
689 static int f2fs_write_check_point_pack(void)
691 struct f2fs_summary_block *sum;
692 struct f2fs_journal *journal;
693 uint32_t blk_size_bytes;
694 uint32_t nat_bits_bytes, nat_bits_blocks;
695 unsigned char *nat_bits = NULL, *empty_nat_bits;
696 uint64_t cp_seg_blk = 0;
697 uint32_t crc = 0, flags;
699 char *cp_payload = NULL;
700 char *sum_compact, *sum_compact_p;
701 struct f2fs_summary *sum_entry;
702 unsigned short vblocks;
705 cp = calloc(F2FS_BLKSIZE, 1);
707 MSG(1, "\tError: Calloc failed for f2fs_checkpoint!!!\n");
711 sum = calloc(F2FS_BLKSIZE, 1);
713 MSG(1, "\tError: Calloc failed for summary_node!!!\n");
717 sum_compact = calloc(F2FS_BLKSIZE, 1);
718 if (sum_compact == NULL) {
719 MSG(1, "\tError: Calloc failed for summary buffer!!!\n");
722 sum_compact_p = sum_compact;
724 nat_bits_bytes = get_sb(segment_count_nat) << 5;
725 nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
727 nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
728 if (nat_bits == NULL) {
729 MSG(1, "\tError: Calloc failed for nat bits buffer!!!\n");
730 goto free_sum_compact;
733 cp_payload = calloc(F2FS_BLKSIZE, 1);
734 if (cp_payload == NULL) {
735 MSG(1, "\tError: Calloc failed for cp_payload!!!\n");
739 /* 1. cp page 1 of checkpoint pack 1 */
740 srand((c.fake_seed) ? 0 : time(NULL));
741 cp->checkpoint_ver = cpu_to_le64(rand() | 0x1);
742 set_cp(cur_node_segno[0], c.cur_seg[CURSEG_HOT_NODE]);
743 set_cp(cur_node_segno[1], c.cur_seg[CURSEG_WARM_NODE]);
744 set_cp(cur_node_segno[2], c.cur_seg[CURSEG_COLD_NODE]);
745 set_cp(cur_data_segno[0], c.cur_seg[CURSEG_HOT_DATA]);
746 set_cp(cur_data_segno[1], c.cur_seg[CURSEG_WARM_DATA]);
747 set_cp(cur_data_segno[2], c.cur_seg[CURSEG_COLD_DATA]);
748 for (i = 3; i < MAX_ACTIVE_NODE_LOGS; i++) {
749 set_cp(cur_node_segno[i], 0xffffffff);
750 set_cp(cur_data_segno[i], 0xffffffff);
753 set_cp(cur_node_blkoff[0], c.curseg_offset[CURSEG_HOT_NODE]);
754 set_cp(cur_data_blkoff[0], c.curseg_offset[CURSEG_HOT_DATA]);
755 set_cp(valid_block_count, c.curseg_offset[CURSEG_HOT_NODE] +
756 c.curseg_offset[CURSEG_HOT_DATA]);
757 set_cp(rsvd_segment_count, c.reserved_segments);
760 * For zoned devices, if zone capacity less than zone size, get
761 * overprovision segment count based on usable segments in the device.
763 set_cp(overprov_segment_count, (f2fs_get_usable_segments(sb) -
764 get_cp(rsvd_segment_count)) *
765 c.overprovision / 100);
767 if (!(c.conf_reserved_sections) &&
768 get_cp(overprov_segment_count) < get_cp(rsvd_segment_count))
769 set_cp(overprov_segment_count, get_cp(rsvd_segment_count));
772 * If conf_reserved_sections has a non zero value, overprov_segment_count
773 * is set to overprov_segment_count + rsvd_segment_count.
775 if (c.conf_reserved_sections) {
777 * Overprovision segments must be bigger than two sections.
778 * In non configurable reserved section case, overprovision
779 * segments are always bigger than two sections.
781 if (get_cp(overprov_segment_count) < 2 * get_sb(segs_per_sec)) {
782 MSG(0, "\tError: Not enough overprovision segments (%u)\n",
783 get_cp(overprov_segment_count));
784 goto free_cp_payload;
786 set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
787 get_cp(rsvd_segment_count));
789 set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
790 2 * get_sb(segs_per_sec));
793 if (f2fs_get_usable_segments(sb) <= get_cp(overprov_segment_count)) {
794 MSG(0, "\tError: Not enough segments to create F2FS Volume\n");
795 goto free_cp_payload;
797 MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
798 MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
799 get_cp(overprov_segment_count),
800 c.reserved_segments);
802 /* main segments - reserved segments - (node + data segments) */
803 if (c.feature & F2FS_FEATURE_RO) {
804 set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
805 set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
806 get_cp(overprov_segment_count)) * c.blks_per_seg));
808 set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 6);
809 set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
810 get_cp(overprov_segment_count)) * c.blks_per_seg));
812 /* cp page (2), data summaries (1), node summaries (3) */
813 set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
814 flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
815 if (get_cp(cp_pack_total_block_count) <=
816 (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
817 flags |= CP_NAT_BITS_FLAG;
820 flags |= CP_TRIMMED_FLAG;
822 if (c.large_nat_bitmap)
823 flags |= CP_LARGE_NAT_BITMAP_FLAG;
825 set_cp(ckpt_flags, flags);
826 set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
827 set_cp(valid_node_count, c.curseg_offset[CURSEG_HOT_NODE]);
828 set_cp(valid_inode_count, c.curseg_offset[CURSEG_HOT_NODE]);
829 set_cp(next_free_nid, c.next_free_nid);
830 set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
831 get_sb(log_blocks_per_seg)) / 8);
833 set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
834 get_sb(log_blocks_per_seg)) / 8);
836 if (c.large_nat_bitmap)
837 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
839 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
841 crc = f2fs_checkpoint_chksum(cp);
842 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
845 blk_size_bytes = 1 << get_sb(log_blocksize);
847 if (blk_size_bytes != F2FS_BLKSIZE) {
848 MSG(1, "\tError: Wrong block size %d / %d!!!\n",
849 blk_size_bytes, F2FS_BLKSIZE);
850 goto free_cp_payload;
853 cp_seg_blk = get_sb(segment0_blkaddr);
855 DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
857 if (dev_write_block(cp, cp_seg_blk)) {
858 MSG(1, "\tError: While writing the cp to disk!!!\n");
859 goto free_cp_payload;
862 for (i = 0; i < get_sb(cp_payload); i++) {
864 if (dev_fill_block(cp_payload, cp_seg_blk)) {
865 MSG(1, "\tError: While zeroing out the sit bitmap area "
867 goto free_cp_payload;
871 /* Prepare and write Segment summary for HOT/WARM/COLD DATA
873 * The structure of compact summary
874 * +-------------------+
876 * +-------------------+
878 * +-------------------+
879 * | hot data summary |
880 * +-------------------+
881 * | warm data summary |
882 * +-------------------+
883 * | cold data summary |
884 * +-------------------+
888 journal = &c.nat_jnl;
889 memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
890 sum_compact_p += SUM_JOURNAL_SIZE;
893 journal = &c.sit_jnl;
895 if (c.feature & F2FS_FEATURE_RO) {
896 i = CURSEG_RO_HOT_DATA;
897 vblocks = le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
898 journal->sit_j.entries[i].segno = cp->cur_data_segno[0];
899 journal->sit_j.entries[i].se.vblocks =
900 cpu_to_le16(vblocks | (CURSEG_HOT_DATA << 10));
902 i = CURSEG_RO_HOT_NODE;
903 vblocks = le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
904 journal->sit_j.entries[i].segno = cp->cur_node_segno[0];
905 journal->sit_j.entries[i].se.vblocks |=
906 cpu_to_le16(vblocks | (CURSEG_HOT_NODE << 10));
908 journal->n_sits = cpu_to_le16(2);
910 for (i = CURSEG_HOT_DATA; i < NR_CURSEG_TYPE; i++) {
911 if (i < NR_CURSEG_DATA_TYPE)
912 journal->sit_j.entries[i].segno =
913 cp->cur_data_segno[i];
916 journal->sit_j.entries[i].segno =
917 cp->cur_node_segno[i - NR_CURSEG_DATA_TYPE];
920 le16_to_cpu(journal->sit_j.entries[i].se.vblocks);
921 journal->sit_j.entries[i].se.vblocks =
922 cpu_to_le16(vblocks | (i << 10));
925 journal->n_sits = cpu_to_le16(6);
928 memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
929 sum_compact_p += SUM_JOURNAL_SIZE;
931 /* hot data summary */
932 memset(sum, 0, sizeof(struct f2fs_summary_block));
933 SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
935 sum_entry = (struct f2fs_summary *)sum_compact_p;
936 memcpy(sum_entry, c.sum[CURSEG_HOT_DATA],
937 sizeof(struct f2fs_summary) * MAX_CACHE_SUMS);
939 /* warm data summary, nothing to do */
940 /* cold data summary, nothing to do */
943 DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
945 if (dev_write_block(sum_compact, cp_seg_blk)) {
946 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
947 goto free_cp_payload;
950 /* Prepare and write Segment summary for HOT_NODE */
951 memset(sum, 0, sizeof(struct f2fs_summary_block));
952 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
953 memcpy(sum->entries, c.sum[CURSEG_HOT_NODE],
954 sizeof(struct f2fs_summary) * MAX_CACHE_SUMS);
957 DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
959 if (dev_write_block(sum, cp_seg_blk)) {
960 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
961 goto free_cp_payload;
964 /* Fill segment summary for WARM_NODE to zero. */
965 memset(sum, 0, sizeof(struct f2fs_summary_block));
966 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
969 DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
971 if (dev_write_block(sum, cp_seg_blk)) {
972 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
973 goto free_cp_payload;
976 /* Fill segment summary for COLD_NODE to zero. */
977 memset(sum, 0, sizeof(struct f2fs_summary_block));
978 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
980 DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
982 if (dev_write_block(sum, cp_seg_blk)) {
983 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
984 goto free_cp_payload;
989 DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
990 if (dev_write_block(cp, cp_seg_blk)) {
991 MSG(1, "\tError: While writing the cp to disk!!!\n");
992 goto free_cp_payload;
995 /* write NAT bits, if possible */
996 if (flags & CP_NAT_BITS_FLAG) {
999 *(__le64 *)nat_bits = get_cp_crc(cp);
1000 empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1001 memset(empty_nat_bits, 0xff, nat_bits_bytes);
1002 test_and_clear_bit_le(0, empty_nat_bits);
1004 /* write the last blocks in cp pack */
1005 cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1006 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1008 DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1011 for (i = 0; i < nat_bits_blocks; i++) {
1012 if (dev_write_block(nat_bits + i *
1013 F2FS_BLKSIZE, cp_seg_blk + i)) {
1014 MSG(1, "\tError: write NAT bits to disk!!!\n");
1015 goto free_cp_payload;
1020 /* cp page 1 of check point pack 2
1021 * Initialize other checkpoint pack with version zero
1023 cp->checkpoint_ver = 0;
1025 crc = f2fs_checkpoint_chksum(cp);
1026 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1028 cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1029 DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1031 if (dev_write_block(cp, cp_seg_blk)) {
1032 MSG(1, "\tError: While writing the cp to disk!!!\n");
1033 goto free_cp_payload;
1036 for (i = 0; i < get_sb(cp_payload); i++) {
1038 if (dev_fill_block(cp_payload, cp_seg_blk)) {
1039 MSG(1, "\tError: While zeroing out the sit bitmap area "
1041 goto free_cp_payload;
1045 /* cp page 2 of check point pack 2 */
1046 cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1047 get_sb(cp_payload) - 1);
1048 DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1050 if (dev_write_block(cp, cp_seg_blk)) {
1051 MSG(1, "\tError: While writing the cp to disk!!!\n");
1052 goto free_cp_payload;
1070 static int f2fs_write_super_block(void)
1075 zero_buff = calloc(F2FS_BLKSIZE, 1);
1076 if (zero_buff == NULL) {
1077 MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1081 memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1082 DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1083 for (index = 0; index < 2; index++) {
1084 if (dev_write_block(zero_buff, index)) {
1085 MSG(1, "\tError: While while writing super_blk "
1086 "on disk!!! index : %d\n", index);
1096 #ifndef WITH_ANDROID
1097 static int f2fs_discard_obsolete_dnode(void)
1099 struct f2fs_node *raw_node;
1100 uint64_t next_blkaddr = 0, offset;
1101 u64 end_blkaddr = (get_sb(segment_count_main) <<
1102 get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1103 uint64_t start_inode_pos = get_sb(main_blkaddr);
1104 uint64_t last_inode_pos;
1106 if (c.zoned_mode || c.feature & F2FS_FEATURE_RO)
1109 raw_node = calloc(sizeof(struct f2fs_node), 1);
1110 if (raw_node == NULL) {
1111 MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1115 /* avoid power-off-recovery based on roll-forward policy */
1116 offset = get_sb(main_blkaddr);
1117 offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1119 last_inode_pos = start_inode_pos +
1120 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1121 c.curseg_offset[CURSEG_COLD_NODE] - 1;
1124 if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1127 if (dev_read_block(raw_node, offset)) {
1128 MSG(1, "\tError: While traversing direct node!!!\n");
1133 next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
1134 memset(raw_node, 0, F2FS_BLKSIZE);
1136 DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1137 if (dev_write_block(raw_node, offset)) {
1138 MSG(1, "\tError: While discarding direct node!!!\n");
1142 offset = next_blkaddr;
1143 /* should avoid recursive chain due to stale data */
1144 if (offset >= start_inode_pos || offset <= last_inode_pos)
1153 static block_t alloc_next_free_block(int curseg_type)
1157 blkaddr = get_sb(main_blkaddr) +
1158 c.cur_seg[curseg_type] * c.blks_per_seg +
1159 c.curseg_offset[curseg_type];
1161 c.curseg_offset[curseg_type]++;
1166 void update_sit_journal(int curseg_type)
1168 struct f2fs_journal *sit_jnl = &c.sit_jnl;
1169 unsigned short vblocks;
1170 int idx = curseg_type;
1172 if (c.feature & F2FS_FEATURE_RO) {
1173 if (curseg_type < NR_CURSEG_DATA_TYPE)
1174 idx = CURSEG_RO_HOT_DATA;
1176 idx = CURSEG_RO_HOT_NODE;
1179 f2fs_set_bit(c.curseg_offset[curseg_type] - 1,
1180 (char *)sit_jnl->sit_j.entries[idx].se.valid_map);
1182 vblocks = le16_to_cpu(sit_jnl->sit_j.entries[idx].se.vblocks);
1183 sit_jnl->sit_j.entries[idx].se.vblocks = cpu_to_le16(vblocks + 1);
1186 void update_nat_journal(nid_t nid, block_t blkaddr)
1188 struct f2fs_journal *nat_jnl = &c.nat_jnl;
1189 unsigned short n_nats = le16_to_cpu(nat_jnl->n_nats);
1191 nat_jnl->nat_j.entries[n_nats].nid = cpu_to_le32(nid);
1192 nat_jnl->nat_j.entries[n_nats].ne.version = 0;
1193 nat_jnl->nat_j.entries[n_nats].ne.ino = cpu_to_le32(nid);
1194 nat_jnl->nat_j.entries[n_nats].ne.block_addr = cpu_to_le32(blkaddr);
1195 nat_jnl->n_nats = cpu_to_le16(n_nats + 1);
1198 void update_summary_entry(int curseg_type, nid_t nid,
1199 unsigned short ofs_in_node)
1201 struct f2fs_summary *sum;
1202 unsigned int curofs = c.curseg_offset[curseg_type] - 1;
1204 assert(curofs < MAX_CACHE_SUMS);
1206 sum = c.sum[curseg_type] + curofs;
1207 sum->nid = cpu_to_le32(nid);
1208 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
1211 static block_t f2fs_add_default_dentry_root(void)
1213 struct f2fs_dentry_block *dent_blk = NULL;
1214 block_t data_blkaddr;
1216 dent_blk = calloc(F2FS_BLKSIZE, 1);
1217 if(dent_blk == NULL) {
1218 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1222 dent_blk->dentry[0].hash_code = 0;
1223 dent_blk->dentry[0].ino = sb->root_ino;
1224 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1225 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1226 memcpy(dent_blk->filename[0], ".", 1);
1228 dent_blk->dentry[1].hash_code = 0;
1229 dent_blk->dentry[1].ino = sb->root_ino;
1230 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1231 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1232 memcpy(dent_blk->filename[1], "..", 2);
1234 /* bitmap for . and .. */
1235 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1236 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1239 int len = strlen(LPF);
1240 f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1242 dent_blk->dentry[2].hash_code = cpu_to_le32(hash);
1243 dent_blk->dentry[2].ino = cpu_to_le32(c.lpf_ino);
1244 dent_blk->dentry[2].name_len = cpu_to_le16(len);
1245 dent_blk->dentry[2].file_type = F2FS_FT_DIR;
1246 memcpy(dent_blk->filename[2], LPF, F2FS_SLOT_LEN);
1248 memcpy(dent_blk->filename[3], &LPF[F2FS_SLOT_LEN],
1249 len - F2FS_SLOT_LEN);
1251 test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1252 test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1255 data_blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1257 DBG(1, "\tWriting default dentry root, at offset 0x%x\n", data_blkaddr);
1258 if (dev_write_block(dent_blk, data_blkaddr)) {
1259 MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1264 update_sit_journal(CURSEG_HOT_DATA);
1265 update_summary_entry(CURSEG_HOT_DATA, le32_to_cpu(sb->root_ino), 0);
1268 return data_blkaddr;
1271 static int f2fs_write_root_inode(void)
1273 struct f2fs_node *raw_node = NULL;
1274 block_t data_blkaddr;
1275 block_t node_blkaddr;
1277 raw_node = calloc(F2FS_BLKSIZE, 1);
1278 if (raw_node == NULL) {
1279 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1283 f2fs_init_inode(sb, raw_node, le32_to_cpu(sb->root_ino),
1287 raw_node->i.i_links = cpu_to_le32(3);
1289 data_blkaddr = f2fs_add_default_dentry_root();
1290 if (data_blkaddr == 0) {
1291 MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1296 raw_node->i.i_addr[get_extra_isize(raw_node)] =
1297 cpu_to_le32(data_blkaddr);
1299 node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1300 raw_node->footer.next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1302 DBG(1, "\tWriting root inode (hot node), offset 0x%x\n", node_blkaddr);
1303 if (write_inode(raw_node, node_blkaddr) < 0) {
1304 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1309 update_nat_journal(le32_to_cpu(sb->root_ino), node_blkaddr);
1310 update_sit_journal(CURSEG_HOT_NODE);
1311 update_summary_entry(CURSEG_HOT_NODE, le32_to_cpu(sb->root_ino), 0);
1317 static int f2fs_write_default_quota(int qtype, __le32 raw_id)
1319 char *filebuf = calloc(F2FS_BLKSIZE, 2);
1320 int file_magics[] = INITQMAGICS;
1321 struct v2_disk_dqheader ddqheader;
1322 struct v2_disk_dqinfo ddqinfo;
1323 struct v2r1_disk_dqblk dqblk;
1327 if (filebuf == NULL) {
1328 MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1332 /* Write basic quota header */
1333 ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1334 /* only support QF_VFSV1 */
1335 ddqheader.dqh_version = cpu_to_le32(1);
1337 memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1339 /* Fill Initial quota file content */
1340 ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1341 ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1342 ddqinfo.dqi_flags = cpu_to_le32(0);
1343 ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1344 ddqinfo.dqi_free_blk = cpu_to_le32(0);
1345 ddqinfo.dqi_free_entry = cpu_to_le32(5);
1347 memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1354 filebuf[5120 + 8] = 1;
1356 dqblk.dqb_id = raw_id;
1357 dqblk.dqb_pad = cpu_to_le32(0);
1358 dqblk.dqb_ihardlimit = cpu_to_le64(0);
1359 dqblk.dqb_isoftlimit = cpu_to_le64(0);
1361 dqblk.dqb_curinodes = cpu_to_le64(2);
1363 dqblk.dqb_curinodes = cpu_to_le64(1);
1364 dqblk.dqb_bhardlimit = cpu_to_le64(0);
1365 dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1367 dqblk.dqb_curspace = cpu_to_le64(8192);
1369 dqblk.dqb_curspace = cpu_to_le64(4096);
1370 dqblk.dqb_btime = cpu_to_le64(0);
1371 dqblk.dqb_itime = cpu_to_le64(0);
1373 memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1375 /* Write two blocks */
1376 for (i = 0; i < QUOTA_DATA; i++) {
1377 blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1379 if (dev_write_block(filebuf + i * F2FS_BLKSIZE, blkaddr)) {
1380 MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1385 update_sit_journal(CURSEG_HOT_DATA);
1386 update_summary_entry(CURSEG_HOT_DATA,
1387 le32_to_cpu(sb->qf_ino[qtype]), i);
1390 DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
1391 blkaddr - 1, blkaddr);
1397 static int f2fs_write_qf_inode(int qtype)
1399 struct f2fs_node *raw_node = NULL;
1400 block_t data_blkaddr;
1401 block_t node_blkaddr;
1405 raw_node = calloc(F2FS_BLKSIZE, 1);
1406 if (raw_node == NULL) {
1407 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1410 f2fs_init_inode(sb, raw_node,
1411 le32_to_cpu(sb->qf_ino[qtype]), mkfs_time, 0x8180);
1413 raw_node->i.i_size = cpu_to_le64(1024 * 6);
1414 raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA);
1415 raw_node->i.i_flags = F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
1417 node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1418 raw_node->footer.next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1421 raw_id = raw_node->i.i_uid;
1422 else if (qtype == 1)
1423 raw_id = raw_node->i.i_gid;
1424 else if (qtype == 2)
1425 raw_id = raw_node->i.i_projid;
1429 /* write two blocks */
1430 data_blkaddr = f2fs_write_default_quota(qtype, raw_id);
1431 if (data_blkaddr == 0) {
1436 for (i = 0; i < QUOTA_DATA; i++)
1437 raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1438 cpu_to_le32(data_blkaddr + i);
1440 DBG(1, "\tWriting quota inode (hot node), offset 0x%x\n", node_blkaddr);
1441 if (write_inode(raw_node, node_blkaddr) < 0) {
1442 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1447 update_nat_journal(le32_to_cpu(sb->qf_ino[qtype]), node_blkaddr);
1448 update_sit_journal(CURSEG_HOT_NODE);
1449 update_summary_entry(CURSEG_HOT_NODE, le32_to_cpu(sb->qf_ino[qtype]), 0);
1455 static int f2fs_update_nat_default(void)
1457 struct f2fs_nat_block *nat_blk = NULL;
1458 uint64_t nat_seg_blk_offset = 0;
1460 nat_blk = calloc(F2FS_BLKSIZE, 1);
1461 if(nat_blk == NULL) {
1462 MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1466 /* update node nat */
1467 nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
1468 nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
1470 /* update meta nat */
1471 nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
1472 nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
1474 nat_seg_blk_offset = get_sb(nat_blkaddr);
1476 DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
1477 nat_seg_blk_offset);
1478 if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
1479 MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
1488 static block_t f2fs_add_default_dentry_lpf(void)
1490 struct f2fs_dentry_block *dent_blk;
1491 block_t data_blkaddr;
1493 dent_blk = calloc(F2FS_BLKSIZE, 1);
1494 if (dent_blk == NULL) {
1495 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1499 dent_blk->dentry[0].hash_code = 0;
1500 dent_blk->dentry[0].ino = cpu_to_le32(c.lpf_ino);
1501 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1502 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1503 memcpy(dent_blk->filename[0], ".", 1);
1505 dent_blk->dentry[1].hash_code = 0;
1506 dent_blk->dentry[1].ino = sb->root_ino;
1507 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1508 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1509 memcpy(dent_blk->filename[1], "..", 2);
1511 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1512 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1514 data_blkaddr = alloc_next_free_block(CURSEG_HOT_DATA);
1516 DBG(1, "\tWriting default dentry lost+found, at offset 0x%x\n",
1518 if (dev_write_block(dent_blk, data_blkaddr)) {
1519 MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1524 update_sit_journal(CURSEG_HOT_DATA);
1525 update_summary_entry(CURSEG_HOT_DATA, c.lpf_ino, 0);
1528 return data_blkaddr;
1531 static int f2fs_write_lpf_inode(void)
1533 struct f2fs_node *raw_node;
1534 block_t data_blkaddr;
1535 block_t node_blkaddr;
1540 raw_node = calloc(F2FS_BLKSIZE, 1);
1541 if (raw_node == NULL) {
1542 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1546 f2fs_init_inode(sb, raw_node, c.lpf_ino, mkfs_time, 0x41c0);
1548 raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1549 raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1550 memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1552 node_blkaddr = alloc_next_free_block(CURSEG_HOT_NODE);
1553 raw_node->footer.next_blkaddr = cpu_to_le32(node_blkaddr + 1);
1555 data_blkaddr = f2fs_add_default_dentry_lpf();
1556 if (data_blkaddr == 0) {
1557 MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1561 raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blkaddr);
1563 DBG(1, "\tWriting lost+found inode (hot node), offset 0x%x\n",
1565 if (write_inode(raw_node, node_blkaddr) < 0) {
1566 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1571 update_nat_journal(c.lpf_ino, node_blkaddr);
1572 update_sit_journal(CURSEG_HOT_NODE);
1573 update_summary_entry(CURSEG_HOT_NODE, c.lpf_ino, 0);
1580 static int f2fs_create_root_dir(void)
1582 enum quota_type qtype;
1585 err = f2fs_write_root_inode();
1587 MSG(1, "\tError: Failed to write root inode!!!\n");
1591 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1592 if (!((1 << qtype) & c.quota_bits))
1594 err = f2fs_write_qf_inode(qtype);
1596 MSG(1, "\tError: Failed to write quota inode!!!\n");
1601 if (c.feature & F2FS_FEATURE_LOST_FOUND) {
1602 err = f2fs_write_lpf_inode();
1604 MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1609 #ifndef WITH_ANDROID
1610 err = f2fs_discard_obsolete_dnode();
1612 MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1617 err = f2fs_update_nat_default();
1619 MSG(1, "\tError: Failed to update NAT for root!!!\n");
1624 MSG(1, "\tError: Could not create the root directory!!!\n");
1629 int f2fs_format_device(void)
1633 err= f2fs_prepare_super_block();
1635 MSG(0, "\tError: Failed to prepare a super block!!!\n");
1640 err = f2fs_trim_devices();
1642 MSG(0, "\tError: Failed to trim whole device!!!\n");
1647 err = f2fs_init_sit_area();
1649 MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1653 err = f2fs_init_nat_area();
1655 MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1659 err = f2fs_create_root_dir();
1661 MSG(0, "\tError: Failed to create the root directory!!!\n");
1665 err = f2fs_write_check_point_pack();
1667 MSG(0, "\tError: Failed to write the check point pack!!!\n");
1671 err = f2fs_write_super_block();
1673 MSG(0, "\tError: Failed to write the super block!!!\n");
1678 MSG(0, "\tError: Could not format the device!!!\n");