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
18 #ifdef HAVE_SYS_STAT_H
21 #ifdef HAVE_SYS_MOUNT_H
22 #include <sys/mount.h>
26 #ifdef HAVE_UUID_UUID_H
27 #include <uuid/uuid.h>
30 #define uuid_parse(a, b) -1
31 #define uuid_generate(a)
32 #define uuid_unparse(a, b) -1
36 #include "f2fs_format_utils.h"
38 extern struct f2fs_configuration c;
39 struct f2fs_super_block raw_sb;
40 struct f2fs_super_block *sb = &raw_sb;
41 struct f2fs_checkpoint *cp;
43 /* Return first segment number of each area */
44 #define prev_zone(cur) (c.cur_seg[cur] - c.segs_per_zone)
45 #define next_zone(cur) (c.cur_seg[cur] + c.segs_per_zone)
46 #define last_zone(cur) ((cur - 1) * c.segs_per_zone)
47 #define last_section(cur) (cur + (c.secs_per_zone - 1) * c.segs_per_sec)
49 /* Return time fixed by the user or current time by default */
50 #define mkfs_time ((c.fixed_time == -1) ? time(NULL) : c.fixed_time)
52 const char *media_ext_lists[] = {
54 "mp", // Covers mp3, mp4, mpeg, mpg
55 "wm", // Covers wma, wmb, wmv
56 "og", // Covers oga, ogg, ogm, ogv
57 "jp", // Covers jpg, jpeg, jp2
90 "pyc", // Python bytecode
99 "odex", // Android RunTime
100 "vdex", // Android RunTime
106 const char *hot_ext_lists[] = {
110 /* Virtual machines */
111 "vmdk", // VMware or VirtualBox
118 const char **default_ext_list[] = {
123 static bool is_extension_exist(const char *name)
127 for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
128 char *ext = (char *)sb->extension_list[i];
129 if (!strcmp(ext, name))
136 static void cure_extension_list(void)
138 const char **extlist;
144 set_sb(extension_count, 0);
145 memset(sb->extension_list, 0, sizeof(sb->extension_list));
147 for (i = 0; i < 2; i++) {
148 ext_str = c.extension_list[i];
149 extlist = default_ext_list[i];
152 name_len = strlen(*extlist);
153 memcpy(sb->extension_list[pos++], *extlist, name_len);
157 set_sb(extension_count, pos);
159 sb->hot_ext_count = pos - get_sb(extension_count);;
164 /* add user ext list */
165 ue = strtok(ext_str, ", ");
167 name_len = strlen(ue);
168 if (name_len >= F2FS_EXTENSION_LEN) {
169 MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
172 if (!is_extension_exist(ue))
173 memcpy(sb->extension_list[pos++], ue, name_len);
175 ue = strtok(NULL, ", ");
176 if (pos >= F2FS_MAX_EXTENSION)
181 set_sb(extension_count, pos);
183 sb->hot_ext_count = pos - get_sb(extension_count);
185 free(c.extension_list[i]);
189 static void verify_cur_segs(void)
194 for (i = 0; i < NR_CURSEG_TYPE; i++) {
195 for (j = i + 1; j < NR_CURSEG_TYPE; j++) {
196 if (c.cur_seg[i] == c.cur_seg[j]) {
207 for (i = 1; i < NR_CURSEG_TYPE; i++)
208 c.cur_seg[i] = next_zone(i - 1);
211 static int f2fs_prepare_super_block(void)
213 uint32_t blk_size_bytes;
214 uint32_t log_sectorsize, log_sectors_per_block;
215 uint32_t log_blocksize, log_blks_per_seg;
216 uint32_t segment_size_bytes, zone_size_bytes;
217 uint32_t sit_segments, nat_segments;
218 uint32_t blocks_for_sit, blocks_for_nat, blocks_for_ssa;
219 uint32_t total_valid_blks_available;
220 uint64_t zone_align_start_offset, diff;
221 uint64_t total_meta_zones, total_meta_segments;
222 uint32_t sit_bitmap_size, max_sit_bitmap_size;
223 uint32_t max_nat_bitmap_size, max_nat_segments;
224 uint32_t total_zones, avail_zones;
225 enum quota_type qtype;
228 set_sb(magic, F2FS_SUPER_MAGIC);
229 set_sb(major_ver, F2FS_MAJOR_VERSION);
230 set_sb(minor_ver, F2FS_MINOR_VERSION);
232 log_sectorsize = log_base_2(c.sector_size);
233 log_sectors_per_block = log_base_2(c.sectors_per_blk);
234 log_blocksize = log_sectorsize + log_sectors_per_block;
235 log_blks_per_seg = log_base_2(c.blks_per_seg);
237 set_sb(log_sectorsize, log_sectorsize);
238 set_sb(log_sectors_per_block, log_sectors_per_block);
240 set_sb(log_blocksize, log_blocksize);
241 set_sb(log_blocks_per_seg, log_blks_per_seg);
243 set_sb(segs_per_sec, c.segs_per_sec);
244 set_sb(secs_per_zone, c.secs_per_zone);
246 blk_size_bytes = 1 << log_blocksize;
247 segment_size_bytes = blk_size_bytes * c.blks_per_seg;
249 blk_size_bytes * c.secs_per_zone *
250 c.segs_per_sec * c.blks_per_seg;
252 set_sb(checksum_offset, 0);
254 set_sb(block_count, c.total_sectors >> log_sectors_per_block);
256 zone_align_start_offset =
257 ((uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE +
258 2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
259 zone_size_bytes * zone_size_bytes -
260 (uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE;
262 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
263 zone_align_start_offset = 8192;
265 if (c.start_sector % DEFAULT_SECTORS_PER_BLOCK) {
266 MSG(1, "\t%s: Align start sector number to the page unit\n",
267 c.zoned_mode ? "FAIL" : "WARN");
268 MSG(1, "\ti.e., start sector: %d, ofs:%d (sects/page: %d)\n",
270 c.start_sector % DEFAULT_SECTORS_PER_BLOCK,
271 DEFAULT_SECTORS_PER_BLOCK);
276 if (c.zoned_mode && c.ndevs > 1)
277 zone_align_start_offset +=
278 (c.devices[0].total_sectors * c.sector_size) % zone_size_bytes;
280 set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
281 sb->cp_blkaddr = sb->segment0_blkaddr;
283 MSG(0, "Info: zone aligned segment0 blkaddr: %u\n",
284 get_sb(segment0_blkaddr));
288 (get_sb(segment0_blkaddr) + c.start_sector /
289 DEFAULT_SECTORS_PER_BLOCK) % c.zone_blocks) ||
291 c.devices[1].start_blkaddr % c.zone_blocks))) {
292 MSG(1, "\tError: Unaligned segment0 block address %u\n",
293 get_sb(segment0_blkaddr));
297 for (i = 0; i < c.ndevs; i++) {
299 c.devices[i].total_segments =
300 (c.devices[i].total_sectors *
301 c.sector_size - zone_align_start_offset) /
303 c.devices[i].start_blkaddr = 0;
304 c.devices[i].end_blkaddr = c.devices[i].total_segments *
306 sb->segment0_blkaddr;
308 c.devices[i].total_segments =
309 c.devices[i].total_sectors /
310 (c.sectors_per_blk * c.blks_per_seg);
311 c.devices[i].start_blkaddr =
312 c.devices[i - 1].end_blkaddr + 1;
313 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
314 c.devices[i].total_segments *
318 memcpy(sb->devs[i].path, c.devices[i].path, MAX_PATH_LEN);
319 sb->devs[i].total_segments =
320 cpu_to_le32(c.devices[i].total_segments);
323 c.total_segments += c.devices[i].total_segments;
325 set_sb(segment_count, (c.total_segments / c.segs_per_zone *
327 set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
329 set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
330 get_sb(segment_count_ckpt) * c.blks_per_seg);
332 blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
334 sit_segments = SEG_ALIGN(blocks_for_sit);
336 set_sb(segment_count_sit, sit_segments * 2);
338 set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
341 total_valid_blks_available = (get_sb(segment_count) -
342 (get_sb(segment_count_ckpt) +
343 get_sb(segment_count_sit))) * c.blks_per_seg;
345 blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
346 NAT_ENTRY_PER_BLOCK);
348 if (c.large_nat_bitmap) {
349 nat_segments = SEG_ALIGN(blocks_for_nat) *
350 DEFAULT_NAT_ENTRY_RATIO / 100;
351 set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
352 max_nat_bitmap_size = (get_sb(segment_count_nat) <<
353 log_blks_per_seg) / 8;
354 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
356 set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
357 max_nat_bitmap_size = 0;
361 * The number of node segments should not be exceeded a "Threshold".
362 * This number resizes NAT bitmap area in a CP page.
363 * So the threshold is determined not to overflow one CP page
365 sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
366 log_blks_per_seg) / 8;
368 if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
369 max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
371 max_sit_bitmap_size = sit_bitmap_size;
373 if (c.large_nat_bitmap) {
374 /* use cp_payload if free space of f2fs_checkpoint is not enough */
375 if (max_sit_bitmap_size + max_nat_bitmap_size >
376 MAX_BITMAP_SIZE_IN_CKPT) {
377 uint32_t diff = max_sit_bitmap_size +
378 max_nat_bitmap_size -
379 MAX_BITMAP_SIZE_IN_CKPT;
380 set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
382 set_sb(cp_payload, 0);
386 * It should be reserved minimum 1 segment for nat.
387 * When sit is too large, we should expand cp area.
388 * It requires more pages for cp.
390 if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
391 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
392 set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
394 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
396 set_sb(cp_payload, 0);
398 max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
400 if (get_sb(segment_count_nat) > max_nat_segments)
401 set_sb(segment_count_nat, max_nat_segments);
403 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
406 set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
409 total_valid_blks_available = (get_sb(segment_count) -
410 (get_sb(segment_count_ckpt) +
411 get_sb(segment_count_sit) +
412 get_sb(segment_count_nat))) *
415 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
418 blocks_for_ssa = total_valid_blks_available /
421 set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
423 total_meta_segments = get_sb(segment_count_ckpt) +
424 get_sb(segment_count_sit) +
425 get_sb(segment_count_nat) +
426 get_sb(segment_count_ssa);
427 diff = total_meta_segments % (c.segs_per_zone);
429 set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
430 (c.segs_per_zone - diff));
432 total_meta_zones = ZONE_ALIGN(total_meta_segments *
435 set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
436 c.segs_per_zone * c.blks_per_seg);
440 * Make sure there is enough randomly writeable
441 * space at the beginning of the disk.
443 unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
445 if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
446 c.devices[0].nr_rnd_zones < main_blkzone) {
447 MSG(0, "\tError: Device does not have enough random "
448 "write zones for F2FS volume (%lu needed)\n",
453 * Check if conventional device has enough space
454 * to accommodate all metadata, zoned device should
455 * not overlap to metadata area.
457 for (i = 1; i < c.ndevs; i++) {
458 if (c.devices[i].zoned_model != F2FS_ZONED_NONE &&
459 c.devices[i].start_blkaddr < get_sb(main_blkaddr)) {
460 MSG(0, "\tError: Conventional device %s is too small,"
461 " (%"PRIu64" MiB needed).\n", c.devices[0].path,
462 (get_sb(main_blkaddr) -
463 c.devices[i].start_blkaddr) >> 8);
469 total_zones = get_sb(segment_count) / (c.segs_per_zone) -
471 if (total_zones == 0)
473 set_sb(section_count, total_zones * c.secs_per_zone);
475 set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
478 * Let's determine the best reserved and overprovisioned space.
479 * For Zoned device, if zone capacity less than zone size, the segments
480 * starting after the zone capacity are unusable in each zone. So get
481 * overprovision ratio and reserved seg count based on avg usable
484 if (c.overprovision == 0)
485 c.overprovision = get_best_overprovision(sb);
487 c.reserved_segments = get_reserved(sb, c.overprovision);
489 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
491 c.reserved_segments = 0;
493 if ((!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)) &&
494 c.overprovision == 0) ||
495 c.total_segments < F2FS_MIN_SEGMENTS ||
496 (c.devices[0].total_sectors *
497 c.sector_size < zone_align_start_offset) ||
498 (get_sb(segment_count_main) - NR_CURSEG_TYPE) <
499 c.reserved_segments) {
504 if (uuid_parse(c.vol_uuid, sb->uuid)) {
505 MSG(0, "\tError: supplied string is not a valid UUID\n");
509 uuid_generate(sb->uuid);
512 /* precompute checksum seed for metadata */
513 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
514 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
516 utf8_to_utf16((char *)sb->volume_name, (const char *)c.vol_label,
517 MAX_VOLUME_NAME, strlen(c.vol_label));
523 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
524 if (!((1 << qtype) & c.quota_bits))
526 sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
527 MSG(0, "Info: add quota type = %u => %u\n",
528 qtype, c.next_free_nid - 1);
531 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND))
532 c.lpf_ino = c.next_free_nid++;
534 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
539 if (total_zones <= avail_zones) {
540 MSG(1, "\tError: %d zones: Need more zones "
541 "by shrinking zone size\n", total_zones);
545 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
546 c.cur_seg[CURSEG_HOT_NODE] = last_section(last_zone(total_zones));
547 c.cur_seg[CURSEG_WARM_NODE] = 0;
548 c.cur_seg[CURSEG_COLD_NODE] = 0;
549 c.cur_seg[CURSEG_HOT_DATA] = 0;
550 c.cur_seg[CURSEG_COLD_DATA] = 0;
551 c.cur_seg[CURSEG_WARM_DATA] = 0;
553 c.cur_seg[CURSEG_HOT_NODE] =
554 last_section(last_zone(total_zones));
555 c.cur_seg[CURSEG_WARM_NODE] = prev_zone(CURSEG_HOT_NODE);
556 c.cur_seg[CURSEG_COLD_NODE] = prev_zone(CURSEG_WARM_NODE);
557 c.cur_seg[CURSEG_HOT_DATA] = prev_zone(CURSEG_COLD_NODE);
558 c.cur_seg[CURSEG_COLD_DATA] = 0;
559 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_COLD_DATA);
560 } else if (c.zoned_mode) {
561 c.cur_seg[CURSEG_HOT_NODE] = 0;
562 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
563 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
564 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
565 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_HOT_DATA);
566 c.cur_seg[CURSEG_COLD_DATA] = next_zone(CURSEG_WARM_DATA);
568 c.cur_seg[CURSEG_HOT_NODE] = 0;
569 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
570 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
571 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
572 c.cur_seg[CURSEG_COLD_DATA] =
573 max(last_zone((total_zones >> 2)),
574 next_zone(CURSEG_HOT_DATA));
575 c.cur_seg[CURSEG_WARM_DATA] =
576 max(last_zone((total_zones >> 1)),
577 next_zone(CURSEG_COLD_DATA));
580 /* if there is redundancy, reassign it */
581 if (!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)))
584 cure_extension_list();
586 /* get kernel version */
588 dev_read_version(c.version, 0, VERSION_LEN);
589 get_kernel_version(c.version);
591 get_kernel_uname_version(c.version);
593 MSG(0, "Info: format version with\n \"%s\"\n", c.version);
595 memcpy(sb->version, c.version, VERSION_LEN);
596 memcpy(sb->init_version, c.version, VERSION_LEN);
598 if (c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) {
599 set_sb(s_encoding, c.s_encoding);
600 set_sb(s_encoding_flags, c.s_encoding_flags);
603 sb->feature = c.feature;
605 if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
606 set_sb(checksum_offset, SB_CHKSUM_OFFSET);
607 set_sb(crc, f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
609 MSG(1, "Info: SB CRC is set: offset (%d), crc (0x%x)\n",
610 get_sb(checksum_offset), get_sb(crc));
616 MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
620 static int f2fs_init_sit_area(void)
622 uint32_t blk_size, seg_size;
624 uint64_t sit_seg_addr = 0;
625 uint8_t *zero_buf = NULL;
627 blk_size = 1 << get_sb(log_blocksize);
628 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
630 zero_buf = calloc(sizeof(uint8_t), seg_size);
631 if(zero_buf == NULL) {
632 MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
636 sit_seg_addr = get_sb(sit_blkaddr);
637 sit_seg_addr *= blk_size;
639 DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
640 for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
641 if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
642 MSG(1, "\tError: While zeroing out the sit area "
647 sit_seg_addr += seg_size;
654 static int f2fs_init_nat_area(void)
656 uint32_t blk_size, seg_size;
658 uint64_t nat_seg_addr = 0;
659 uint8_t *nat_buf = NULL;
661 blk_size = 1 << get_sb(log_blocksize);
662 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
664 nat_buf = calloc(sizeof(uint8_t), seg_size);
665 if (nat_buf == NULL) {
666 MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
670 nat_seg_addr = get_sb(nat_blkaddr);
671 nat_seg_addr *= blk_size;
673 DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
674 for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
675 if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
676 MSG(1, "\tError: While zeroing out the nat area "
681 nat_seg_addr = nat_seg_addr + (2 * seg_size);
688 static int f2fs_write_check_point_pack(void)
690 struct f2fs_summary_block *sum = NULL;
691 struct f2fs_journal *journal;
692 uint32_t blk_size_bytes;
693 uint32_t nat_bits_bytes, nat_bits_blocks;
694 unsigned char *nat_bits = NULL, *empty_nat_bits;
695 uint64_t cp_seg_blk = 0;
696 uint32_t crc = 0, flags;
698 char *cp_payload = NULL;
699 char *sum_compact, *sum_compact_p;
700 struct f2fs_summary *sum_entry;
701 enum quota_type qtype;
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], 1 + c.quota_inum + c.lpf_inum);
754 set_cp(cur_data_blkoff[0], 1 + c.quota_dnum + c.lpf_dnum);
755 set_cp(valid_block_count, 2 + c.quota_inum + c.quota_dnum +
756 c.lpf_inum + c.lpf_dnum);
757 set_cp(rsvd_segment_count, c.reserved_segments);
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 & cpu_to_le32(F2FS_FEATURE_RO)) {
804 set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
805 set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
806 get_cp(overprov_segment_count)) * c.blks_per_seg));
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, 1 + c.quota_inum + c.lpf_inum);
828 set_cp(valid_inode_count, 1 + c.quota_inum + c.lpf_inum);
829 set_cp(next_free_nid, c.next_free_nid);
830 set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
831 get_sb(log_blocks_per_seg)) / 8);
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 * +-------------------+
886 memset(sum, 0, sizeof(struct f2fs_summary_block));
887 SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
889 journal = &sum->journal;
890 journal->n_nats = cpu_to_le16(1 + c.quota_inum + c.lpf_inum);
891 journal->nat_j.entries[0].nid = sb->root_ino;
892 journal->nat_j.entries[0].ne.version = 0;
893 journal->nat_j.entries[0].ne.ino = sb->root_ino;
894 journal->nat_j.entries[0].ne.block_addr = cpu_to_le32(
895 get_sb(main_blkaddr) +
896 get_cp(cur_node_segno[0]) * c.blks_per_seg);
898 for (qtype = 0, i = 1; qtype < F2FS_MAX_QUOTAS; qtype++) {
899 if (!((1 << qtype) & c.quota_bits))
901 journal->nat_j.entries[i].nid = sb->qf_ino[qtype];
902 journal->nat_j.entries[i].ne.version = 0;
903 journal->nat_j.entries[i].ne.ino = sb->qf_ino[qtype];
904 journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
905 get_sb(main_blkaddr) +
906 get_cp(cur_node_segno[0]) *
912 journal->nat_j.entries[i].nid = cpu_to_le32(c.lpf_ino);
913 journal->nat_j.entries[i].ne.version = 0;
914 journal->nat_j.entries[i].ne.ino = cpu_to_le32(c.lpf_ino);
915 journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
916 get_sb(main_blkaddr) +
917 get_cp(cur_node_segno[0]) *
921 memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
922 sum_compact_p += SUM_JOURNAL_SIZE;
924 memset(sum, 0, sizeof(struct f2fs_summary_block));
926 /* inode sit for root */
927 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
928 journal->n_sits = cpu_to_le16(2);
930 journal->n_sits = cpu_to_le16(6);
932 journal->sit_j.entries[0].segno = cp->cur_node_segno[0];
933 journal->sit_j.entries[0].se.vblocks =
934 cpu_to_le16((CURSEG_HOT_NODE << 10) |
935 (1 + c.quota_inum + c.lpf_inum));
936 f2fs_set_bit(0, (char *)journal->sit_j.entries[0].se.valid_map);
937 for (i = 1; i <= c.quota_inum; i++)
938 f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
940 f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
942 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
943 /* data sit for root */
944 journal->sit_j.entries[1].segno = cp->cur_data_segno[0];
945 journal->sit_j.entries[1].se.vblocks =
946 cpu_to_le16((CURSEG_HOT_DATA << 10) |
947 (1 + c.quota_dnum + c.lpf_dnum));
948 f2fs_set_bit(0, (char *)journal->sit_j.entries[1].se.valid_map);
949 for (i = 1; i <= c.quota_dnum; i++)
950 f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
952 f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
954 journal->sit_j.entries[1].segno = cp->cur_node_segno[1];
955 journal->sit_j.entries[1].se.vblocks =
956 cpu_to_le16((CURSEG_WARM_NODE << 10));
957 journal->sit_j.entries[2].segno = cp->cur_node_segno[2];
958 journal->sit_j.entries[2].se.vblocks =
959 cpu_to_le16((CURSEG_COLD_NODE << 10));
961 /* data sit for root */
962 journal->sit_j.entries[3].segno = cp->cur_data_segno[0];
963 journal->sit_j.entries[3].se.vblocks =
964 cpu_to_le16((CURSEG_HOT_DATA << 10) |
965 (1 + c.quota_dnum + c.lpf_dnum));
966 f2fs_set_bit(0, (char *)journal->sit_j.entries[3].se.valid_map);
967 for (i = 1; i <= c.quota_dnum; i++)
968 f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
970 f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
972 journal->sit_j.entries[4].segno = cp->cur_data_segno[1];
973 journal->sit_j.entries[4].se.vblocks =
974 cpu_to_le16((CURSEG_WARM_DATA << 10));
975 journal->sit_j.entries[5].segno = cp->cur_data_segno[2];
976 journal->sit_j.entries[5].se.vblocks =
977 cpu_to_le16((CURSEG_COLD_DATA << 10));
980 memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
981 sum_compact_p += SUM_JOURNAL_SIZE;
983 /* hot data summary */
984 sum_entry = (struct f2fs_summary *)sum_compact_p;
985 sum_entry->nid = sb->root_ino;
986 sum_entry->ofs_in_node = 0;
989 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
992 if (!((1 << qtype) & c.quota_bits))
995 for (j = 0; j < QUOTA_DATA(qtype); j++) {
996 (sum_entry + off + j)->nid = sb->qf_ino[qtype];
997 (sum_entry + off + j)->ofs_in_node = cpu_to_le16(j);
999 off += QUOTA_DATA(qtype);
1003 (sum_entry + off)->nid = cpu_to_le32(c.lpf_ino);
1004 (sum_entry + off)->ofs_in_node = 0;
1007 /* warm data summary, nothing to do */
1008 /* cold data summary, nothing to do */
1011 DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
1013 if (dev_write_block(sum_compact, cp_seg_blk)) {
1014 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1015 goto free_cp_payload;
1018 /* Prepare and write Segment summary for HOT_NODE */
1019 memset(sum, 0, sizeof(struct f2fs_summary_block));
1020 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1022 sum->entries[0].nid = sb->root_ino;
1023 sum->entries[0].ofs_in_node = 0;
1024 for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1025 if (!((1 << qtype) & c.quota_bits))
1027 sum->entries[1 + i].nid = sb->qf_ino[qtype];
1028 sum->entries[1 + i].ofs_in_node = 0;
1033 sum->entries[i].nid = cpu_to_le32(c.lpf_ino);
1034 sum->entries[i].ofs_in_node = 0;
1038 DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
1040 if (dev_write_block(sum, cp_seg_blk)) {
1041 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1042 goto free_cp_payload;
1045 /* Fill segment summary for WARM_NODE to zero. */
1046 memset(sum, 0, sizeof(struct f2fs_summary_block));
1047 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1050 DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
1052 if (dev_write_block(sum, cp_seg_blk)) {
1053 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1054 goto free_cp_payload;
1057 /* Fill segment summary for COLD_NODE to zero. */
1058 memset(sum, 0, sizeof(struct f2fs_summary_block));
1059 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1061 DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
1063 if (dev_write_block(sum, cp_seg_blk)) {
1064 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1065 goto free_cp_payload;
1070 DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
1071 if (dev_write_block(cp, cp_seg_blk)) {
1072 MSG(1, "\tError: While writing the cp to disk!!!\n");
1073 goto free_cp_payload;
1076 /* write NAT bits, if possible */
1077 if (flags & CP_NAT_BITS_FLAG) {
1080 *(__le64 *)nat_bits = get_cp_crc(cp);
1081 empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1082 memset(empty_nat_bits, 0xff, nat_bits_bytes);
1083 test_and_clear_bit_le(0, empty_nat_bits);
1085 /* write the last blocks in cp pack */
1086 cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1087 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1089 DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1092 for (i = 0; i < nat_bits_blocks; i++) {
1093 if (dev_write_block(nat_bits + i *
1094 F2FS_BLKSIZE, cp_seg_blk + i)) {
1095 MSG(1, "\tError: write NAT bits to disk!!!\n");
1096 goto free_cp_payload;
1101 /* cp page 1 of check point pack 2
1102 * Initialize other checkpoint pack with version zero
1104 cp->checkpoint_ver = 0;
1106 crc = f2fs_checkpoint_chksum(cp);
1107 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1109 cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1110 DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1112 if (dev_write_block(cp, cp_seg_blk)) {
1113 MSG(1, "\tError: While writing the cp to disk!!!\n");
1114 goto free_cp_payload;
1117 for (i = 0; i < get_sb(cp_payload); i++) {
1119 if (dev_fill_block(cp_payload, cp_seg_blk)) {
1120 MSG(1, "\tError: While zeroing out the sit bitmap area "
1122 goto free_cp_payload;
1126 /* cp page 2 of check point pack 2 */
1127 cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1128 get_sb(cp_payload) - 1);
1129 DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1131 if (dev_write_block(cp, cp_seg_blk)) {
1132 MSG(1, "\tError: While writing the cp to disk!!!\n");
1133 goto free_cp_payload;
1151 static int f2fs_write_super_block(void)
1156 zero_buff = calloc(F2FS_BLKSIZE, 1);
1157 if (zero_buff == NULL) {
1158 MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1162 memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1163 DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1164 for (index = 0; index < 2; index++) {
1165 if (dev_write_block(zero_buff, index)) {
1166 MSG(1, "\tError: While while writing super_blk "
1167 "on disk!!! index : %d\n", index);
1177 #ifndef WITH_ANDROID
1178 static int f2fs_discard_obsolete_dnode(void)
1180 struct f2fs_node *raw_node;
1181 uint64_t next_blkaddr = 0, offset;
1182 u64 end_blkaddr = (get_sb(segment_count_main) <<
1183 get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1184 uint64_t start_inode_pos = get_sb(main_blkaddr);
1185 uint64_t last_inode_pos;
1187 if (c.zoned_mode || c.feature & cpu_to_le32(F2FS_FEATURE_RO))
1190 raw_node = calloc(sizeof(struct f2fs_node), 1);
1191 if (raw_node == NULL) {
1192 MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1196 /* avoid power-off-recovery based on roll-forward policy */
1197 offset = get_sb(main_blkaddr);
1198 offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1200 last_inode_pos = start_inode_pos +
1201 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg + c.quota_inum + c.lpf_inum;
1204 if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1207 if (dev_read_block(raw_node, offset)) {
1208 MSG(1, "\tError: While traversing direct node!!!\n");
1213 next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
1214 memset(raw_node, 0, F2FS_BLKSIZE);
1216 DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1217 if (dev_write_block(raw_node, offset)) {
1218 MSG(1, "\tError: While discarding direct node!!!\n");
1222 offset = next_blkaddr;
1223 /* should avoid recursive chain due to stale data */
1224 if (offset >= start_inode_pos || offset <= last_inode_pos)
1233 static int f2fs_write_root_inode(void)
1235 struct f2fs_node *raw_node = NULL;
1236 uint64_t blk_size_bytes, data_blk_nor;
1237 uint64_t main_area_node_seg_blk_offset = 0;
1239 raw_node = calloc(F2FS_BLKSIZE, 1);
1240 if (raw_node == NULL) {
1241 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1245 raw_node->footer.nid = sb->root_ino;
1246 raw_node->footer.ino = sb->root_ino;
1247 raw_node->footer.cp_ver = cpu_to_le64(1);
1248 raw_node->footer.next_blkaddr = cpu_to_le32(
1249 get_sb(main_blkaddr) +
1250 c.cur_seg[CURSEG_HOT_NODE] *
1251 c.blks_per_seg + 1);
1253 raw_node->i.i_mode = cpu_to_le16(0x41ed);
1255 raw_node->i.i_links = cpu_to_le32(3);
1257 raw_node->i.i_links = cpu_to_le32(2);
1258 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1259 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1261 blk_size_bytes = 1 << get_sb(log_blocksize);
1262 raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
1263 raw_node->i.i_blocks = cpu_to_le64(2);
1265 raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1266 raw_node->i.i_atime_nsec = 0;
1267 raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1268 raw_node->i.i_ctime_nsec = 0;
1269 raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1270 raw_node->i.i_mtime_nsec = 0;
1271 raw_node->i.i_generation = 0;
1272 raw_node->i.i_xattr_nid = 0;
1273 raw_node->i.i_flags = 0;
1274 raw_node->i.i_current_depth = cpu_to_le32(1);
1275 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1277 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1278 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1279 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1282 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1283 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1285 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1286 raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1287 raw_node->i.i_crtime_nsec = 0;
1290 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1291 raw_node->i.i_compress_algorithm = 0;
1292 raw_node->i.i_log_cluster_size = 0;
1293 raw_node->i.i_compress_flag = 0;
1296 data_blk_nor = get_sb(main_blkaddr) +
1297 c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg;
1298 raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1300 raw_node->i.i_ext.fofs = 0;
1301 raw_node->i.i_ext.blk_addr = 0;
1302 raw_node->i.i_ext.len = 0;
1304 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1305 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1308 DBG(1, "\tWriting root inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1309 get_sb(main_blkaddr),
1310 c.cur_seg[CURSEG_HOT_NODE],
1311 c.blks_per_seg, main_area_node_seg_blk_offset);
1312 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1313 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1322 static int f2fs_write_default_quota(int qtype, unsigned int blkaddr,
1325 char *filebuf = calloc(F2FS_BLKSIZE, 2);
1326 int file_magics[] = INITQMAGICS;
1327 struct v2_disk_dqheader ddqheader;
1328 struct v2_disk_dqinfo ddqinfo;
1329 struct v2r1_disk_dqblk dqblk;
1331 if (filebuf == NULL) {
1332 MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1336 /* Write basic quota header */
1337 ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1338 /* only support QF_VFSV1 */
1339 ddqheader.dqh_version = cpu_to_le32(1);
1341 memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1343 /* Fill Initial quota file content */
1344 ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1345 ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1346 ddqinfo.dqi_flags = cpu_to_le32(0);
1347 ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1348 ddqinfo.dqi_free_blk = cpu_to_le32(0);
1349 ddqinfo.dqi_free_entry = cpu_to_le32(5);
1351 memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1358 filebuf[5120 + 8] = 1;
1360 dqblk.dqb_id = raw_id;
1361 dqblk.dqb_pad = cpu_to_le32(0);
1362 dqblk.dqb_ihardlimit = cpu_to_le64(0);
1363 dqblk.dqb_isoftlimit = cpu_to_le64(0);
1365 dqblk.dqb_curinodes = cpu_to_le64(2);
1367 dqblk.dqb_curinodes = cpu_to_le64(1);
1368 dqblk.dqb_bhardlimit = cpu_to_le64(0);
1369 dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1371 dqblk.dqb_curspace = cpu_to_le64(8192);
1373 dqblk.dqb_curspace = cpu_to_le64(4096);
1374 dqblk.dqb_btime = cpu_to_le64(0);
1375 dqblk.dqb_itime = cpu_to_le64(0);
1377 memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1379 /* Write two blocks */
1380 if (dev_write_block(filebuf, blkaddr) ||
1381 dev_write_block(filebuf + F2FS_BLKSIZE, blkaddr + 1)) {
1382 MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1386 DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
1387 blkaddr, blkaddr + 1);
1389 c.quota_dnum += QUOTA_DATA(qtype);
1393 static int f2fs_write_qf_inode(int qtype, int offset)
1395 struct f2fs_node *raw_node = NULL;
1396 uint64_t data_blk_nor;
1397 uint64_t main_area_node_seg_blk_offset = 0;
1401 raw_node = calloc(F2FS_BLKSIZE, 1);
1402 if (raw_node == NULL) {
1403 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1406 f2fs_init_qf_inode(sb, raw_node, qtype, mkfs_time);
1408 raw_node->footer.next_blkaddr = cpu_to_le32(
1409 get_sb(main_blkaddr) +
1410 c.cur_seg[CURSEG_HOT_NODE] *
1411 c.blks_per_seg + 1 + qtype + 1);
1412 raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
1414 data_blk_nor = get_sb(main_blkaddr) +
1415 c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg + 1
1416 + offset * QUOTA_DATA(i);
1419 raw_id = raw_node->i.i_uid;
1420 else if (qtype == 1)
1421 raw_id = raw_node->i.i_gid;
1422 else if (qtype == 2)
1423 raw_id = raw_node->i.i_projid;
1427 /* write two blocks */
1428 if (f2fs_write_default_quota(qtype, data_blk_nor, raw_id)) {
1433 for (i = 0; i < QUOTA_DATA(qtype); i++)
1434 raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1435 cpu_to_le32(data_blk_nor + i);
1437 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1438 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1439 c.blks_per_seg + offset + 1;
1441 DBG(1, "\tWriting quota inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1442 get_sb(main_blkaddr),
1443 c.cur_seg[CURSEG_HOT_NODE],
1444 c.blks_per_seg, main_area_node_seg_blk_offset);
1445 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1446 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1456 static int f2fs_update_nat_root(void)
1458 struct f2fs_nat_block *nat_blk = NULL;
1459 uint64_t nat_seg_blk_offset = 0;
1460 enum quota_type qtype;
1463 nat_blk = calloc(F2FS_BLKSIZE, 1);
1464 if(nat_blk == NULL) {
1465 MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1470 for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1471 if (!((1 << qtype) & c.quota_bits))
1473 nat_blk->entries[sb->qf_ino[qtype]].block_addr =
1474 cpu_to_le32(get_sb(main_blkaddr) +
1475 c.cur_seg[CURSEG_HOT_NODE] *
1476 c.blks_per_seg + i + 1);
1477 nat_blk->entries[sb->qf_ino[qtype]].ino = sb->qf_ino[qtype];
1482 nat_blk->entries[get_sb(root_ino)].block_addr = cpu_to_le32(
1483 get_sb(main_blkaddr) +
1484 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg);
1485 nat_blk->entries[get_sb(root_ino)].ino = sb->root_ino;
1487 /* update node nat */
1488 nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
1489 nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
1491 /* update meta nat */
1492 nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
1493 nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
1495 nat_seg_blk_offset = get_sb(nat_blkaddr);
1497 DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
1498 nat_seg_blk_offset);
1499 if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
1500 MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
1509 static block_t f2fs_add_default_dentry_lpf(void)
1511 struct f2fs_dentry_block *dent_blk;
1512 uint64_t data_blk_offset;
1514 dent_blk = calloc(F2FS_BLKSIZE, 1);
1515 if (dent_blk == NULL) {
1516 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1520 dent_blk->dentry[0].hash_code = 0;
1521 dent_blk->dentry[0].ino = cpu_to_le32(c.lpf_ino);
1522 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1523 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1524 memcpy(dent_blk->filename[0], ".", 1);
1526 dent_blk->dentry[1].hash_code = 0;
1527 dent_blk->dentry[1].ino = sb->root_ino;
1528 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1529 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1530 memcpy(dent_blk->filename[1], "..", 2);
1532 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1533 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1535 data_blk_offset = get_sb(main_blkaddr);
1536 data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg +
1539 DBG(1, "\tWriting default dentry lost+found, at offset 0x%08"PRIx64"\n",
1541 if (dev_write_block(dent_blk, data_blk_offset)) {
1542 MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1549 return data_blk_offset;
1552 static int f2fs_write_lpf_inode(void)
1554 struct f2fs_node *raw_node;
1555 uint64_t blk_size_bytes, main_area_node_seg_blk_offset;
1556 block_t data_blk_nor;
1561 raw_node = calloc(F2FS_BLKSIZE, 1);
1562 if (raw_node == NULL) {
1563 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1567 raw_node->footer.nid = cpu_to_le32(c.lpf_ino);
1568 raw_node->footer.ino = raw_node->footer.nid;
1569 raw_node->footer.cp_ver = cpu_to_le64(1);
1570 raw_node->footer.next_blkaddr = cpu_to_le32(
1571 get_sb(main_blkaddr) +
1572 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1573 1 + c.quota_inum + 1);
1575 raw_node->i.i_mode = cpu_to_le16(0x41c0); /* 0700 */
1576 raw_node->i.i_links = cpu_to_le32(2);
1577 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1578 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1580 blk_size_bytes = 1 << get_sb(log_blocksize);
1581 raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes);
1582 raw_node->i.i_blocks = cpu_to_le64(2);
1584 raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1585 raw_node->i.i_atime_nsec = 0;
1586 raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1587 raw_node->i.i_ctime_nsec = 0;
1588 raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1589 raw_node->i.i_mtime_nsec = 0;
1590 raw_node->i.i_generation = 0;
1591 raw_node->i.i_xattr_nid = 0;
1592 raw_node->i.i_flags = 0;
1593 raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1594 raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1595 memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1596 raw_node->i.i_current_depth = cpu_to_le32(1);
1597 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1599 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1600 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1601 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1604 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1605 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1607 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1608 raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1609 raw_node->i.i_crtime_nsec = 0;
1612 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1613 raw_node->i.i_compress_algorithm = 0;
1614 raw_node->i.i_log_cluster_size = 0;
1615 raw_node->i.i_compress_flag = 0;
1618 data_blk_nor = f2fs_add_default_dentry_lpf();
1619 if (data_blk_nor == 0) {
1620 MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1624 raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1626 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1627 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1628 c.blks_per_seg + c.quota_inum + 1;
1630 DBG(1, "\tWriting lost+found inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1631 get_sb(main_blkaddr),
1632 c.cur_seg[CURSEG_HOT_NODE],
1633 c.blks_per_seg, main_area_node_seg_blk_offset);
1634 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1635 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1646 static int f2fs_add_default_dentry_root(void)
1648 struct f2fs_dentry_block *dent_blk = NULL;
1649 uint64_t data_blk_offset = 0;
1651 dent_blk = calloc(F2FS_BLKSIZE, 1);
1652 if(dent_blk == NULL) {
1653 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1657 dent_blk->dentry[0].hash_code = 0;
1658 dent_blk->dentry[0].ino = sb->root_ino;
1659 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1660 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1661 memcpy(dent_blk->filename[0], ".", 1);
1663 dent_blk->dentry[1].hash_code = 0;
1664 dent_blk->dentry[1].ino = sb->root_ino;
1665 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1666 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1667 memcpy(dent_blk->filename[1], "..", 2);
1669 /* bitmap for . and .. */
1670 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1671 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1674 int len = strlen(LPF);
1675 f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1677 dent_blk->dentry[2].hash_code = cpu_to_le32(hash);
1678 dent_blk->dentry[2].ino = cpu_to_le32(c.lpf_ino);
1679 dent_blk->dentry[2].name_len = cpu_to_le16(len);
1680 dent_blk->dentry[2].file_type = F2FS_FT_DIR;
1681 memcpy(dent_blk->filename[2], LPF, F2FS_SLOT_LEN);
1683 memcpy(dent_blk->filename[3], &LPF[F2FS_SLOT_LEN],
1684 len - F2FS_SLOT_LEN);
1686 test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1687 test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1690 data_blk_offset = get_sb(main_blkaddr);
1691 data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] *
1694 DBG(1, "\tWriting default dentry root, at offset 0x%08"PRIx64"\n",
1696 if (dev_write_block(dent_blk, data_blk_offset)) {
1697 MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1706 static int f2fs_create_root_dir(void)
1708 enum quota_type qtype;
1711 err = f2fs_write_root_inode();
1713 MSG(1, "\tError: Failed to write root inode!!!\n");
1717 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1718 if (!((1 << qtype) & c.quota_bits))
1720 err = f2fs_write_qf_inode(qtype, i++);
1722 MSG(1, "\tError: Failed to write quota inode!!!\n");
1727 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
1728 err = f2fs_write_lpf_inode();
1730 MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1735 #ifndef WITH_ANDROID
1736 err = f2fs_discard_obsolete_dnode();
1738 MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1743 err = f2fs_update_nat_root();
1745 MSG(1, "\tError: Failed to update NAT for root!!!\n");
1749 err = f2fs_add_default_dentry_root();
1751 MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1756 MSG(1, "\tError: Could not create the root directory!!!\n");
1761 int f2fs_format_device(void)
1765 err= f2fs_prepare_super_block();
1767 MSG(0, "\tError: Failed to prepare a super block!!!\n");
1772 err = f2fs_trim_devices();
1774 MSG(0, "\tError: Failed to trim whole device!!!\n");
1779 err = f2fs_init_sit_area();
1781 MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1785 err = f2fs_init_nat_area();
1787 MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1791 err = f2fs_create_root_dir();
1793 MSG(0, "\tError: Failed to create the root directory!!!\n");
1797 err = f2fs_write_check_point_pack();
1799 MSG(0, "\tError: Failed to write the check point pack!!!\n");
1803 err = f2fs_write_super_block();
1805 MSG(0, "\tError: Failed to write the super block!!!\n");
1810 MSG(0, "\tError: Could not format the device!!!\n");