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)
35 #include "f2fs_format_utils.h"
37 extern struct f2fs_configuration c;
38 struct f2fs_super_block raw_sb;
39 struct f2fs_super_block *sb = &raw_sb;
40 struct f2fs_checkpoint *cp;
42 /* Return first segment number of each area */
43 #define prev_zone(cur) (c.cur_seg[cur] - c.segs_per_zone)
44 #define next_zone(cur) (c.cur_seg[cur] + c.segs_per_zone)
45 #define last_zone(cur) ((cur - 1) * c.segs_per_zone)
46 #define last_section(cur) (cur + (c.secs_per_zone - 1) * c.segs_per_sec)
48 /* Return time fixed by the user or current time by default */
49 #define mkfs_time ((c.fixed_time == -1) ? time(NULL) : c.fixed_time)
51 const char *media_ext_lists[] = {
53 "mp", // Covers mp3, mp4, mpeg, mpg
54 "wm", // Covers wma, wmb, wmv
55 "og", // Covers oga, ogg, ogm, ogv
56 "jp", // Covers jpg, jpeg, jp2
89 "pyc", // Python bytecode
98 "odex", // Android RunTime
99 "vdex", // Android RunTime
105 const char *hot_ext_lists[] = {
109 /* Virtual machines */
110 "vmdk", // VMware or VirtualBox
117 const char **default_ext_list[] = {
122 static bool is_extension_exist(const char *name)
126 for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
127 char *ext = (char *)sb->extension_list[i];
128 if (!strcmp(ext, name))
135 static void cure_extension_list(void)
137 const char **extlist;
143 set_sb(extension_count, 0);
144 memset(sb->extension_list, 0, sizeof(sb->extension_list));
146 for (i = 0; i < 2; i++) {
147 ext_str = c.extension_list[i];
148 extlist = default_ext_list[i];
151 name_len = strlen(*extlist);
152 memcpy(sb->extension_list[pos++], *extlist, name_len);
156 set_sb(extension_count, pos);
158 sb->hot_ext_count = pos - get_sb(extension_count);;
163 /* add user ext list */
164 ue = strtok(ext_str, ", ");
166 name_len = strlen(ue);
167 if (name_len >= F2FS_EXTENSION_LEN) {
168 MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
171 if (!is_extension_exist(ue))
172 memcpy(sb->extension_list[pos++], ue, name_len);
174 ue = strtok(NULL, ", ");
175 if (pos >= F2FS_MAX_EXTENSION)
180 set_sb(extension_count, pos);
182 sb->hot_ext_count = pos - get_sb(extension_count);
184 free(c.extension_list[i]);
188 static void verify_cur_segs(void)
193 for (i = 0; i < NR_CURSEG_TYPE; i++) {
194 for (j = i + 1; j < NR_CURSEG_TYPE; j++) {
195 if (c.cur_seg[i] == c.cur_seg[j]) {
206 for (i = 1; i < NR_CURSEG_TYPE; i++)
207 c.cur_seg[i] = next_zone(i - 1);
210 static int f2fs_prepare_super_block(void)
212 uint32_t blk_size_bytes;
213 uint32_t log_sectorsize, log_sectors_per_block;
214 uint32_t log_blocksize, log_blks_per_seg;
215 uint32_t segment_size_bytes, zone_size_bytes;
216 uint32_t sit_segments, nat_segments;
217 uint32_t blocks_for_sit, blocks_for_nat, blocks_for_ssa;
218 uint32_t total_valid_blks_available;
219 uint64_t zone_align_start_offset, diff;
220 uint64_t total_meta_zones, total_meta_segments;
221 uint32_t sit_bitmap_size, max_sit_bitmap_size;
222 uint32_t max_nat_bitmap_size, max_nat_segments;
223 uint32_t total_zones, avail_zones;
224 enum quota_type qtype;
227 set_sb(magic, F2FS_SUPER_MAGIC);
228 set_sb(major_ver, F2FS_MAJOR_VERSION);
229 set_sb(minor_ver, F2FS_MINOR_VERSION);
231 log_sectorsize = log_base_2(c.sector_size);
232 log_sectors_per_block = log_base_2(c.sectors_per_blk);
233 log_blocksize = log_sectorsize + log_sectors_per_block;
234 log_blks_per_seg = log_base_2(c.blks_per_seg);
236 set_sb(log_sectorsize, log_sectorsize);
237 set_sb(log_sectors_per_block, log_sectors_per_block);
239 set_sb(log_blocksize, log_blocksize);
240 set_sb(log_blocks_per_seg, log_blks_per_seg);
242 set_sb(segs_per_sec, c.segs_per_sec);
243 set_sb(secs_per_zone, c.secs_per_zone);
245 blk_size_bytes = 1 << log_blocksize;
246 segment_size_bytes = blk_size_bytes * c.blks_per_seg;
248 blk_size_bytes * c.secs_per_zone *
249 c.segs_per_sec * c.blks_per_seg;
251 set_sb(checksum_offset, 0);
253 set_sb(block_count, c.total_sectors >> log_sectors_per_block);
255 zone_align_start_offset =
256 ((uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE +
257 2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
258 zone_size_bytes * zone_size_bytes -
259 (uint64_t) c.start_sector * DEFAULT_SECTOR_SIZE;
261 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
262 zone_align_start_offset = 8192;
264 if (c.start_sector % DEFAULT_SECTORS_PER_BLOCK) {
265 MSG(1, "\t%s: Align start sector number to the page unit\n",
266 c.zoned_mode ? "FAIL" : "WARN");
267 MSG(1, "\ti.e., start sector: %d, ofs:%d (sects/page: %d)\n",
269 c.start_sector % DEFAULT_SECTORS_PER_BLOCK,
270 DEFAULT_SECTORS_PER_BLOCK);
275 if (c.zoned_mode && c.ndevs > 1)
276 zone_align_start_offset +=
277 (c.devices[0].total_sectors * c.sector_size) % zone_size_bytes;
279 set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
280 sb->cp_blkaddr = sb->segment0_blkaddr;
282 MSG(0, "Info: zone aligned segment0 blkaddr: %u\n",
283 get_sb(segment0_blkaddr));
287 (get_sb(segment0_blkaddr) + c.start_sector /
288 DEFAULT_SECTORS_PER_BLOCK) % c.zone_blocks) ||
290 c.devices[1].start_blkaddr % c.zone_blocks))) {
291 MSG(1, "\tError: Unaligned segment0 block address %u\n",
292 get_sb(segment0_blkaddr));
296 for (i = 0; i < c.ndevs; i++) {
298 c.devices[i].total_segments =
299 (c.devices[i].total_sectors *
300 c.sector_size - zone_align_start_offset) /
302 c.devices[i].start_blkaddr = 0;
303 c.devices[i].end_blkaddr = c.devices[i].total_segments *
305 sb->segment0_blkaddr;
307 c.devices[i].total_segments =
308 c.devices[i].total_sectors /
309 (c.sectors_per_blk * c.blks_per_seg);
310 c.devices[i].start_blkaddr =
311 c.devices[i - 1].end_blkaddr + 1;
312 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
313 c.devices[i].total_segments *
317 memcpy(sb->devs[i].path, c.devices[i].path, MAX_PATH_LEN);
318 sb->devs[i].total_segments =
319 cpu_to_le32(c.devices[i].total_segments);
322 c.total_segments += c.devices[i].total_segments;
324 set_sb(segment_count, (c.total_segments / c.segs_per_zone *
326 set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
328 set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
329 get_sb(segment_count_ckpt) * c.blks_per_seg);
331 blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
333 sit_segments = SEG_ALIGN(blocks_for_sit);
335 set_sb(segment_count_sit, sit_segments * 2);
337 set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
340 total_valid_blks_available = (get_sb(segment_count) -
341 (get_sb(segment_count_ckpt) +
342 get_sb(segment_count_sit))) * c.blks_per_seg;
344 blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
345 NAT_ENTRY_PER_BLOCK);
347 if (c.large_nat_bitmap) {
348 nat_segments = SEG_ALIGN(blocks_for_nat) *
349 DEFAULT_NAT_ENTRY_RATIO / 100;
350 set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
351 max_nat_bitmap_size = (get_sb(segment_count_nat) <<
352 log_blks_per_seg) / 8;
353 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
355 set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
356 max_nat_bitmap_size = 0;
360 * The number of node segments should not be exceeded a "Threshold".
361 * This number resizes NAT bitmap area in a CP page.
362 * So the threshold is determined not to overflow one CP page
364 sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
365 log_blks_per_seg) / 8;
367 if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
368 max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
370 max_sit_bitmap_size = sit_bitmap_size;
372 if (c.large_nat_bitmap) {
373 /* use cp_payload if free space of f2fs_checkpoint is not enough */
374 if (max_sit_bitmap_size + max_nat_bitmap_size >
375 MAX_BITMAP_SIZE_IN_CKPT) {
376 uint32_t diff = max_sit_bitmap_size +
377 max_nat_bitmap_size -
378 MAX_BITMAP_SIZE_IN_CKPT;
379 set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
381 set_sb(cp_payload, 0);
385 * It should be reserved minimum 1 segment for nat.
386 * When sit is too large, we should expand cp area.
387 * It requires more pages for cp.
389 if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
390 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
391 set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
393 max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
395 set_sb(cp_payload, 0);
397 max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
399 if (get_sb(segment_count_nat) > max_nat_segments)
400 set_sb(segment_count_nat, max_nat_segments);
402 set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
405 set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
408 total_valid_blks_available = (get_sb(segment_count) -
409 (get_sb(segment_count_ckpt) +
410 get_sb(segment_count_sit) +
411 get_sb(segment_count_nat))) *
414 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
417 blocks_for_ssa = total_valid_blks_available /
420 set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
422 total_meta_segments = get_sb(segment_count_ckpt) +
423 get_sb(segment_count_sit) +
424 get_sb(segment_count_nat) +
425 get_sb(segment_count_ssa);
426 diff = total_meta_segments % (c.segs_per_zone);
428 set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
429 (c.segs_per_zone - diff));
431 total_meta_zones = ZONE_ALIGN(total_meta_segments *
434 set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
435 c.segs_per_zone * c.blks_per_seg);
439 * Make sure there is enough randomly writeable
440 * space at the beginning of the disk.
442 unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
444 if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
445 c.devices[0].nr_rnd_zones < main_blkzone) {
446 MSG(0, "\tError: Device does not have enough random "
447 "write zones for F2FS volume (%lu needed)\n",
452 * Check if conventional device has enough space
453 * to accommodate all metadata, zoned device should
454 * not overlap to metadata area.
456 for (i = 1; i < c.ndevs; i++) {
457 if (c.devices[i].zoned_model != F2FS_ZONED_NONE &&
458 c.devices[i].start_blkaddr < get_sb(main_blkaddr)) {
459 MSG(0, "\tError: Conventional device %s is too small,"
460 " (%"PRIu64" MiB needed).\n", c.devices[0].path,
461 (get_sb(main_blkaddr) -
462 c.devices[i].start_blkaddr) >> 8);
468 total_zones = get_sb(segment_count) / (c.segs_per_zone) -
470 if (total_zones == 0)
472 set_sb(section_count, total_zones * c.secs_per_zone);
474 set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
477 * Let's determine the best reserved and overprovisioned space.
478 * For Zoned device, if zone capacity less than zone size, the segments
479 * starting after the zone capacity are unusable in each zone. So get
480 * overprovision ratio and reserved seg count based on avg usable
483 if (c.overprovision == 0)
484 c.overprovision = get_best_overprovision(sb);
486 c.reserved_segments = get_reserved(sb, c.overprovision);
488 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
490 c.reserved_segments = 0;
492 if ((!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)) &&
493 c.overprovision == 0) ||
494 c.total_segments < F2FS_MIN_SEGMENTS ||
495 (c.devices[0].total_sectors *
496 c.sector_size < zone_align_start_offset) ||
497 (get_sb(segment_count_main) - NR_CURSEG_TYPE) <
498 c.reserved_segments) {
503 if (uuid_parse(c.vol_uuid, sb->uuid)) {
504 MSG(0, "\tError: supplied string is not a valid UUID\n");
508 uuid_generate(sb->uuid);
511 /* precompute checksum seed for metadata */
512 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
513 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
515 utf8_to_utf16((char *)sb->volume_name, (const char *)c.vol_label,
516 MAX_VOLUME_NAME, strlen(c.vol_label));
522 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
523 if (!((1 << qtype) & c.quota_bits))
525 sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
526 MSG(0, "Info: add quota type = %u => %u\n",
527 qtype, c.next_free_nid - 1);
530 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND))
531 c.lpf_ino = c.next_free_nid++;
533 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
538 if (total_zones <= avail_zones) {
539 MSG(1, "\tError: %d zones: Need more zones "
540 "by shrinking zone size\n", total_zones);
544 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
545 c.cur_seg[CURSEG_HOT_NODE] = last_section(last_zone(total_zones));
546 c.cur_seg[CURSEG_WARM_NODE] = 0;
547 c.cur_seg[CURSEG_COLD_NODE] = 0;
548 c.cur_seg[CURSEG_HOT_DATA] = 0;
549 c.cur_seg[CURSEG_COLD_DATA] = 0;
550 c.cur_seg[CURSEG_WARM_DATA] = 0;
552 c.cur_seg[CURSEG_HOT_NODE] =
553 last_section(last_zone(total_zones));
554 c.cur_seg[CURSEG_WARM_NODE] = prev_zone(CURSEG_HOT_NODE);
555 c.cur_seg[CURSEG_COLD_NODE] = prev_zone(CURSEG_WARM_NODE);
556 c.cur_seg[CURSEG_HOT_DATA] = prev_zone(CURSEG_COLD_NODE);
557 c.cur_seg[CURSEG_COLD_DATA] = 0;
558 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_COLD_DATA);
559 } else if (c.zoned_mode) {
560 c.cur_seg[CURSEG_HOT_NODE] = 0;
561 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
562 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
563 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
564 c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_HOT_DATA);
565 c.cur_seg[CURSEG_COLD_DATA] = next_zone(CURSEG_WARM_DATA);
567 c.cur_seg[CURSEG_HOT_NODE] = 0;
568 c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
569 c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
570 c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
571 c.cur_seg[CURSEG_COLD_DATA] =
572 max(last_zone((total_zones >> 2)),
573 next_zone(CURSEG_HOT_DATA));
574 c.cur_seg[CURSEG_WARM_DATA] =
575 max(last_zone((total_zones >> 1)),
576 next_zone(CURSEG_COLD_DATA));
579 /* if there is redundancy, reassign it */
580 if (!(c.feature & cpu_to_le32(F2FS_FEATURE_RO)))
583 cure_extension_list();
585 /* get kernel version */
587 dev_read_version(c.version, 0, VERSION_LEN);
588 get_kernel_version(c.version);
590 get_kernel_uname_version(c.version);
592 MSG(0, "Info: format version with\n \"%s\"\n", c.version);
594 memcpy(sb->version, c.version, VERSION_LEN);
595 memcpy(sb->init_version, c.version, VERSION_LEN);
597 if (c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) {
598 set_sb(s_encoding, c.s_encoding);
599 set_sb(s_encoding_flags, c.s_encoding_flags);
602 sb->feature = c.feature;
604 if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
605 set_sb(checksum_offset, SB_CHKSUM_OFFSET);
606 set_sb(crc, f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
608 MSG(1, "Info: SB CRC is set: offset (%d), crc (0x%x)\n",
609 get_sb(checksum_offset), get_sb(crc));
615 MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
619 static int f2fs_init_sit_area(void)
621 uint32_t blk_size, seg_size;
623 uint64_t sit_seg_addr = 0;
624 uint8_t *zero_buf = NULL;
626 blk_size = 1 << get_sb(log_blocksize);
627 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
629 zero_buf = calloc(sizeof(uint8_t), seg_size);
630 if(zero_buf == NULL) {
631 MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
635 sit_seg_addr = get_sb(sit_blkaddr);
636 sit_seg_addr *= blk_size;
638 DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
639 for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
640 if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
641 MSG(1, "\tError: While zeroing out the sit area "
646 sit_seg_addr += seg_size;
653 static int f2fs_init_nat_area(void)
655 uint32_t blk_size, seg_size;
657 uint64_t nat_seg_addr = 0;
658 uint8_t *nat_buf = NULL;
660 blk_size = 1 << get_sb(log_blocksize);
661 seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
663 nat_buf = calloc(sizeof(uint8_t), seg_size);
664 if (nat_buf == NULL) {
665 MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
669 nat_seg_addr = get_sb(nat_blkaddr);
670 nat_seg_addr *= blk_size;
672 DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
673 for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
674 if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
675 MSG(1, "\tError: While zeroing out the nat area "
680 nat_seg_addr = nat_seg_addr + (2 * seg_size);
687 static int f2fs_write_check_point_pack(void)
689 struct f2fs_summary_block *sum = NULL;
690 struct f2fs_journal *journal;
691 uint32_t blk_size_bytes;
692 uint32_t nat_bits_bytes, nat_bits_blocks;
693 unsigned char *nat_bits = NULL, *empty_nat_bits;
694 uint64_t cp_seg_blk = 0;
695 uint32_t crc = 0, flags;
697 char *cp_payload = NULL;
698 char *sum_compact, *sum_compact_p;
699 struct f2fs_summary *sum_entry;
700 enum quota_type qtype;
704 cp = calloc(F2FS_BLKSIZE, 1);
706 MSG(1, "\tError: Calloc failed for f2fs_checkpoint!!!\n");
710 sum = calloc(F2FS_BLKSIZE, 1);
712 MSG(1, "\tError: Calloc failed for summary_node!!!\n");
716 sum_compact = calloc(F2FS_BLKSIZE, 1);
717 if (sum_compact == NULL) {
718 MSG(1, "\tError: Calloc failed for summary buffer!!!\n");
721 sum_compact_p = sum_compact;
723 nat_bits_bytes = get_sb(segment_count_nat) << 5;
724 nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
726 nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
727 if (nat_bits == NULL) {
728 MSG(1, "\tError: Calloc failed for nat bits buffer!!!\n");
729 goto free_sum_compact;
732 cp_payload = calloc(F2FS_BLKSIZE, 1);
733 if (cp_payload == NULL) {
734 MSG(1, "\tError: Calloc failed for cp_payload!!!\n");
738 /* 1. cp page 1 of checkpoint pack 1 */
739 srand((c.fake_seed) ? 0 : time(NULL));
740 cp->checkpoint_ver = cpu_to_le64(rand() | 0x1);
741 set_cp(cur_node_segno[0], c.cur_seg[CURSEG_HOT_NODE]);
742 set_cp(cur_node_segno[1], c.cur_seg[CURSEG_WARM_NODE]);
743 set_cp(cur_node_segno[2], c.cur_seg[CURSEG_COLD_NODE]);
744 set_cp(cur_data_segno[0], c.cur_seg[CURSEG_HOT_DATA]);
745 set_cp(cur_data_segno[1], c.cur_seg[CURSEG_WARM_DATA]);
746 set_cp(cur_data_segno[2], c.cur_seg[CURSEG_COLD_DATA]);
747 for (i = 3; i < MAX_ACTIVE_NODE_LOGS; i++) {
748 set_cp(cur_node_segno[i], 0xffffffff);
749 set_cp(cur_data_segno[i], 0xffffffff);
752 set_cp(cur_node_blkoff[0], 1 + c.quota_inum + c.lpf_inum);
753 set_cp(cur_data_blkoff[0], 1 + c.quota_dnum + c.lpf_dnum);
754 set_cp(valid_block_count, 2 + c.quota_inum + c.quota_dnum +
755 c.lpf_inum + c.lpf_dnum);
756 set_cp(rsvd_segment_count, c.reserved_segments);
759 * For zoned devices, if zone capacity less than zone size, get
760 * overprovision segment count based on usable segments in the device.
762 set_cp(overprov_segment_count, (f2fs_get_usable_segments(sb) -
763 get_cp(rsvd_segment_count)) *
764 c.overprovision / 100);
766 if (!(c.conf_reserved_sections) &&
767 get_cp(overprov_segment_count) < get_cp(rsvd_segment_count))
768 set_cp(overprov_segment_count, get_cp(rsvd_segment_count));
771 * If conf_reserved_sections has a non zero value, overprov_segment_count
772 * is set to overprov_segment_count + rsvd_segment_count.
774 if (c.conf_reserved_sections) {
776 * Overprovision segments must be bigger than two sections.
777 * In non configurable reserved section case, overprovision
778 * segments are always bigger than two sections.
780 if (get_cp(overprov_segment_count) < 2 * get_sb(segs_per_sec)) {
781 MSG(0, "\tError: Not enough overprovision segments (%u)\n",
782 get_cp(overprov_segment_count));
783 goto free_cp_payload;
785 set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
786 get_cp(rsvd_segment_count));
788 set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
789 2 * get_sb(segs_per_sec));
792 if (f2fs_get_usable_segments(sb) <= get_cp(overprov_segment_count)) {
793 MSG(0, "\tError: Not enough segments to create F2FS Volume\n");
794 goto free_cp_payload;
796 MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
797 MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
798 get_cp(overprov_segment_count),
799 c.reserved_segments);
801 /* main segments - reserved segments - (node + data segments) */
802 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
803 set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 2);
804 set_cp(user_block_count, ((get_cp(free_segment_count) + 2 -
805 get_cp(overprov_segment_count)) * c.blks_per_seg));
807 set_cp(free_segment_count, f2fs_get_usable_segments(sb) - 6);
808 set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
809 get_cp(overprov_segment_count)) * c.blks_per_seg));
811 /* cp page (2), data summaries (1), node summaries (3) */
812 set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
813 flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
814 if (get_cp(cp_pack_total_block_count) <=
815 (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
816 flags |= CP_NAT_BITS_FLAG;
819 flags |= CP_TRIMMED_FLAG;
821 if (c.large_nat_bitmap)
822 flags |= CP_LARGE_NAT_BITMAP_FLAG;
824 set_cp(ckpt_flags, flags);
825 set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
826 set_cp(valid_node_count, 1 + c.quota_inum + c.lpf_inum);
827 set_cp(valid_inode_count, 1 + c.quota_inum + c.lpf_inum);
828 set_cp(next_free_nid, c.next_free_nid);
829 set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
830 get_sb(log_blocks_per_seg)) / 8);
832 set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
833 get_sb(log_blocks_per_seg)) / 8);
835 if (c.large_nat_bitmap)
836 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
838 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
840 crc = f2fs_checkpoint_chksum(cp);
841 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
844 blk_size_bytes = 1 << get_sb(log_blocksize);
846 if (blk_size_bytes != F2FS_BLKSIZE) {
847 MSG(1, "\tError: Wrong block size %d / %d!!!\n",
848 blk_size_bytes, F2FS_BLKSIZE);
849 goto free_cp_payload;
852 cp_seg_blk = get_sb(segment0_blkaddr);
854 DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
856 if (dev_write_block(cp, cp_seg_blk)) {
857 MSG(1, "\tError: While writing the cp to disk!!!\n");
858 goto free_cp_payload;
861 for (i = 0; i < get_sb(cp_payload); i++) {
863 if (dev_fill_block(cp_payload, cp_seg_blk)) {
864 MSG(1, "\tError: While zeroing out the sit bitmap area "
866 goto free_cp_payload;
870 /* Prepare and write Segment summary for HOT/WARM/COLD DATA
872 * The structure of compact summary
873 * +-------------------+
875 * +-------------------+
877 * +-------------------+
878 * | hot data summary |
879 * +-------------------+
880 * | warm data summary |
881 * +-------------------+
882 * | cold data summary |
883 * +-------------------+
885 memset(sum, 0, sizeof(struct f2fs_summary_block));
886 SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
888 journal = &sum->journal;
889 journal->n_nats = cpu_to_le16(1 + c.quota_inum + c.lpf_inum);
890 journal->nat_j.entries[0].nid = sb->root_ino;
891 journal->nat_j.entries[0].ne.version = 0;
892 journal->nat_j.entries[0].ne.ino = sb->root_ino;
893 journal->nat_j.entries[0].ne.block_addr = cpu_to_le32(
894 get_sb(main_blkaddr) +
895 get_cp(cur_node_segno[0]) * c.blks_per_seg);
897 for (qtype = 0, i = 1; qtype < F2FS_MAX_QUOTAS; qtype++) {
898 if (!((1 << qtype) & c.quota_bits))
900 journal->nat_j.entries[i].nid = sb->qf_ino[qtype];
901 journal->nat_j.entries[i].ne.version = 0;
902 journal->nat_j.entries[i].ne.ino = sb->qf_ino[qtype];
903 journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
904 get_sb(main_blkaddr) +
905 get_cp(cur_node_segno[0]) *
911 journal->nat_j.entries[i].nid = cpu_to_le32(c.lpf_ino);
912 journal->nat_j.entries[i].ne.version = 0;
913 journal->nat_j.entries[i].ne.ino = cpu_to_le32(c.lpf_ino);
914 journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
915 get_sb(main_blkaddr) +
916 get_cp(cur_node_segno[0]) *
920 memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
921 sum_compact_p += SUM_JOURNAL_SIZE;
923 memset(sum, 0, sizeof(struct f2fs_summary_block));
925 /* inode sit for root */
926 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
927 journal->n_sits = cpu_to_le16(2);
929 journal->n_sits = cpu_to_le16(6);
931 journal->sit_j.entries[0].segno = cp->cur_node_segno[0];
932 journal->sit_j.entries[0].se.vblocks =
933 cpu_to_le16((CURSEG_HOT_NODE << 10) |
934 (1 + c.quota_inum + c.lpf_inum));
935 f2fs_set_bit(0, (char *)journal->sit_j.entries[0].se.valid_map);
936 for (i = 1; i <= c.quota_inum; i++)
937 f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
939 f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
941 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO)) {
942 /* data sit for root */
943 journal->sit_j.entries[1].segno = cp->cur_data_segno[0];
944 journal->sit_j.entries[1].se.vblocks =
945 cpu_to_le16((CURSEG_HOT_DATA << 10) |
946 (1 + c.quota_dnum + c.lpf_dnum));
947 f2fs_set_bit(0, (char *)journal->sit_j.entries[1].se.valid_map);
948 for (i = 1; i <= c.quota_dnum; i++)
949 f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
951 f2fs_set_bit(i, (char *)journal->sit_j.entries[1].se.valid_map);
953 journal->sit_j.entries[1].segno = cp->cur_node_segno[1];
954 journal->sit_j.entries[1].se.vblocks =
955 cpu_to_le16((CURSEG_WARM_NODE << 10));
956 journal->sit_j.entries[2].segno = cp->cur_node_segno[2];
957 journal->sit_j.entries[2].se.vblocks =
958 cpu_to_le16((CURSEG_COLD_NODE << 10));
960 /* data sit for root */
961 journal->sit_j.entries[3].segno = cp->cur_data_segno[0];
962 journal->sit_j.entries[3].se.vblocks =
963 cpu_to_le16((CURSEG_HOT_DATA << 10) |
964 (1 + c.quota_dnum + c.lpf_dnum));
965 f2fs_set_bit(0, (char *)journal->sit_j.entries[3].se.valid_map);
966 for (i = 1; i <= c.quota_dnum; i++)
967 f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
969 f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
971 journal->sit_j.entries[4].segno = cp->cur_data_segno[1];
972 journal->sit_j.entries[4].se.vblocks =
973 cpu_to_le16((CURSEG_WARM_DATA << 10));
974 journal->sit_j.entries[5].segno = cp->cur_data_segno[2];
975 journal->sit_j.entries[5].se.vblocks =
976 cpu_to_le16((CURSEG_COLD_DATA << 10));
979 memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
980 sum_compact_p += SUM_JOURNAL_SIZE;
982 /* hot data summary */
983 sum_entry = (struct f2fs_summary *)sum_compact_p;
984 sum_entry->nid = sb->root_ino;
985 sum_entry->ofs_in_node = 0;
988 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
991 if (!((1 << qtype) & c.quota_bits))
994 for (j = 0; j < QUOTA_DATA(qtype); j++) {
995 (sum_entry + off + j)->nid = sb->qf_ino[qtype];
996 (sum_entry + off + j)->ofs_in_node = cpu_to_le16(j);
998 off += QUOTA_DATA(qtype);
1002 (sum_entry + off)->nid = cpu_to_le32(c.lpf_ino);
1003 (sum_entry + off)->ofs_in_node = 0;
1006 /* warm data summary, nothing to do */
1007 /* cold data summary, nothing to do */
1010 DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
1012 if (dev_write_block(sum_compact, cp_seg_blk)) {
1013 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1014 goto free_cp_payload;
1017 /* Prepare and write Segment summary for HOT_NODE */
1018 memset(sum, 0, sizeof(struct f2fs_summary_block));
1019 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1021 sum->entries[0].nid = sb->root_ino;
1022 sum->entries[0].ofs_in_node = 0;
1023 for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1024 if (!((1 << qtype) & c.quota_bits))
1026 sum->entries[1 + i].nid = sb->qf_ino[qtype];
1027 sum->entries[1 + i].ofs_in_node = 0;
1032 sum->entries[i].nid = cpu_to_le32(c.lpf_ino);
1033 sum->entries[i].ofs_in_node = 0;
1037 DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
1039 if (dev_write_block(sum, cp_seg_blk)) {
1040 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1041 goto free_cp_payload;
1044 /* Fill segment summary for WARM_NODE to zero. */
1045 memset(sum, 0, sizeof(struct f2fs_summary_block));
1046 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1049 DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
1051 if (dev_write_block(sum, cp_seg_blk)) {
1052 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1053 goto free_cp_payload;
1056 /* Fill segment summary for COLD_NODE to zero. */
1057 memset(sum, 0, sizeof(struct f2fs_summary_block));
1058 SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
1060 DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
1062 if (dev_write_block(sum, cp_seg_blk)) {
1063 MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
1064 goto free_cp_payload;
1069 DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
1070 if (dev_write_block(cp, cp_seg_blk)) {
1071 MSG(1, "\tError: While writing the cp to disk!!!\n");
1072 goto free_cp_payload;
1075 /* write NAT bits, if possible */
1076 if (flags & CP_NAT_BITS_FLAG) {
1079 *(__le64 *)nat_bits = get_cp_crc(cp);
1080 empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
1081 memset(empty_nat_bits, 0xff, nat_bits_bytes);
1082 test_and_clear_bit_le(0, empty_nat_bits);
1084 /* write the last blocks in cp pack */
1085 cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
1086 get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1088 DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
1091 for (i = 0; i < nat_bits_blocks; i++) {
1092 if (dev_write_block(nat_bits + i *
1093 F2FS_BLKSIZE, cp_seg_blk + i)) {
1094 MSG(1, "\tError: write NAT bits to disk!!!\n");
1095 goto free_cp_payload;
1100 /* cp page 1 of check point pack 2
1101 * Initialize other checkpoint pack with version zero
1103 cp->checkpoint_ver = 0;
1105 crc = f2fs_checkpoint_chksum(cp);
1106 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
1108 cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
1109 DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1111 if (dev_write_block(cp, cp_seg_blk)) {
1112 MSG(1, "\tError: While writing the cp to disk!!!\n");
1113 goto free_cp_payload;
1116 for (i = 0; i < get_sb(cp_payload); i++) {
1118 if (dev_fill_block(cp_payload, cp_seg_blk)) {
1119 MSG(1, "\tError: While zeroing out the sit bitmap area "
1121 goto free_cp_payload;
1125 /* cp page 2 of check point pack 2 */
1126 cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
1127 get_sb(cp_payload) - 1);
1128 DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
1130 if (dev_write_block(cp, cp_seg_blk)) {
1131 MSG(1, "\tError: While writing the cp to disk!!!\n");
1132 goto free_cp_payload;
1150 static int f2fs_write_super_block(void)
1155 zero_buff = calloc(F2FS_BLKSIZE, 1);
1156 if (zero_buff == NULL) {
1157 MSG(1, "\tError: Calloc Failed for super_blk_zero_buf!!!\n");
1161 memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
1162 DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
1163 for (index = 0; index < 2; index++) {
1164 if (dev_write_block(zero_buff, index)) {
1165 MSG(1, "\tError: While while writing super_blk "
1166 "on disk!!! index : %d\n", index);
1176 #ifndef WITH_ANDROID
1177 static int f2fs_discard_obsolete_dnode(void)
1179 struct f2fs_node *raw_node;
1180 uint64_t next_blkaddr = 0, offset;
1181 u64 end_blkaddr = (get_sb(segment_count_main) <<
1182 get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
1183 uint64_t start_inode_pos = get_sb(main_blkaddr);
1184 uint64_t last_inode_pos;
1186 if (c.zoned_mode || c.feature & cpu_to_le32(F2FS_FEATURE_RO))
1189 raw_node = calloc(sizeof(struct f2fs_node), 1);
1190 if (raw_node == NULL) {
1191 MSG(1, "\tError: Calloc Failed for discard_raw_node!!!\n");
1195 /* avoid power-off-recovery based on roll-forward policy */
1196 offset = get_sb(main_blkaddr);
1197 offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
1199 last_inode_pos = start_inode_pos +
1200 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg + c.quota_inum + c.lpf_inum;
1203 if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
1206 if (dev_read_block(raw_node, offset)) {
1207 MSG(1, "\tError: While traversing direct node!!!\n");
1212 next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
1213 memset(raw_node, 0, F2FS_BLKSIZE);
1215 DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
1216 if (dev_write_block(raw_node, offset)) {
1217 MSG(1, "\tError: While discarding direct node!!!\n");
1221 offset = next_blkaddr;
1222 /* should avoid recursive chain due to stale data */
1223 if (offset >= start_inode_pos || offset <= last_inode_pos)
1232 static int f2fs_write_root_inode(void)
1234 struct f2fs_node *raw_node = NULL;
1235 uint64_t blk_size_bytes, data_blk_nor;
1236 uint64_t main_area_node_seg_blk_offset = 0;
1238 raw_node = calloc(F2FS_BLKSIZE, 1);
1239 if (raw_node == NULL) {
1240 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1244 raw_node->footer.nid = sb->root_ino;
1245 raw_node->footer.ino = sb->root_ino;
1246 raw_node->footer.cp_ver = cpu_to_le64(1);
1247 raw_node->footer.next_blkaddr = cpu_to_le32(
1248 get_sb(main_blkaddr) +
1249 c.cur_seg[CURSEG_HOT_NODE] *
1250 c.blks_per_seg + 1);
1252 raw_node->i.i_mode = cpu_to_le16(0x41ed);
1254 raw_node->i.i_links = cpu_to_le32(3);
1256 raw_node->i.i_links = cpu_to_le32(2);
1257 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1258 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1260 blk_size_bytes = 1 << get_sb(log_blocksize);
1261 raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
1262 raw_node->i.i_blocks = cpu_to_le64(2);
1264 raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1265 raw_node->i.i_atime_nsec = 0;
1266 raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1267 raw_node->i.i_ctime_nsec = 0;
1268 raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1269 raw_node->i.i_mtime_nsec = 0;
1270 raw_node->i.i_generation = 0;
1271 raw_node->i.i_xattr_nid = 0;
1272 raw_node->i.i_flags = 0;
1273 raw_node->i.i_current_depth = cpu_to_le32(1);
1274 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1276 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1277 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1278 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1281 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1282 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1284 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1285 raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1286 raw_node->i.i_crtime_nsec = 0;
1289 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1290 raw_node->i.i_compress_algrithm = 0;
1291 raw_node->i.i_log_cluster_size = 0;
1292 raw_node->i.i_compress_flag = 0;
1295 data_blk_nor = get_sb(main_blkaddr) +
1296 c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg;
1297 raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1299 raw_node->i.i_ext.fofs = 0;
1300 raw_node->i.i_ext.blk_addr = 0;
1301 raw_node->i.i_ext.len = 0;
1303 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1304 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1307 DBG(1, "\tWriting root inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1308 get_sb(main_blkaddr),
1309 c.cur_seg[CURSEG_HOT_NODE],
1310 c.blks_per_seg, main_area_node_seg_blk_offset);
1311 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1312 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1321 static int f2fs_write_default_quota(int qtype, unsigned int blkaddr,
1324 char *filebuf = calloc(F2FS_BLKSIZE, 2);
1325 int file_magics[] = INITQMAGICS;
1326 struct v2_disk_dqheader ddqheader;
1327 struct v2_disk_dqinfo ddqinfo;
1328 struct v2r1_disk_dqblk dqblk;
1330 if (filebuf == NULL) {
1331 MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
1335 /* Write basic quota header */
1336 ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
1337 /* only support QF_VFSV1 */
1338 ddqheader.dqh_version = cpu_to_le32(1);
1340 memcpy(filebuf, &ddqheader, sizeof(ddqheader));
1342 /* Fill Initial quota file content */
1343 ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1344 ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1345 ddqinfo.dqi_flags = cpu_to_le32(0);
1346 ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
1347 ddqinfo.dqi_free_blk = cpu_to_le32(0);
1348 ddqinfo.dqi_free_entry = cpu_to_le32(5);
1350 memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
1357 filebuf[5120 + 8] = 1;
1359 dqblk.dqb_id = raw_id;
1360 dqblk.dqb_pad = cpu_to_le32(0);
1361 dqblk.dqb_ihardlimit = cpu_to_le64(0);
1362 dqblk.dqb_isoftlimit = cpu_to_le64(0);
1364 dqblk.dqb_curinodes = cpu_to_le64(2);
1366 dqblk.dqb_curinodes = cpu_to_le64(1);
1367 dqblk.dqb_bhardlimit = cpu_to_le64(0);
1368 dqblk.dqb_bsoftlimit = cpu_to_le64(0);
1370 dqblk.dqb_curspace = cpu_to_le64(8192);
1372 dqblk.dqb_curspace = cpu_to_le64(4096);
1373 dqblk.dqb_btime = cpu_to_le64(0);
1374 dqblk.dqb_itime = cpu_to_le64(0);
1376 memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
1378 /* Write two blocks */
1379 if (dev_write_block(filebuf, blkaddr) ||
1380 dev_write_block(filebuf + F2FS_BLKSIZE, blkaddr + 1)) {
1381 MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
1385 DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
1386 blkaddr, blkaddr + 1);
1388 c.quota_dnum += QUOTA_DATA(qtype);
1392 static int f2fs_write_qf_inode(int qtype, int offset)
1394 struct f2fs_node *raw_node = NULL;
1395 uint64_t data_blk_nor;
1396 uint64_t main_area_node_seg_blk_offset = 0;
1400 raw_node = calloc(F2FS_BLKSIZE, 1);
1401 if (raw_node == NULL) {
1402 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1405 f2fs_init_qf_inode(sb, raw_node, qtype, mkfs_time);
1407 raw_node->footer.next_blkaddr = cpu_to_le32(
1408 get_sb(main_blkaddr) +
1409 c.cur_seg[CURSEG_HOT_NODE] *
1410 c.blks_per_seg + 1 + qtype + 1);
1411 raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
1413 data_blk_nor = get_sb(main_blkaddr) +
1414 c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg + 1
1415 + offset * QUOTA_DATA(i);
1418 raw_id = raw_node->i.i_uid;
1419 else if (qtype == 1)
1420 raw_id = raw_node->i.i_gid;
1421 else if (qtype == 2)
1422 raw_id = raw_node->i.i_projid;
1426 /* write two blocks */
1427 if (f2fs_write_default_quota(qtype, data_blk_nor, raw_id)) {
1432 for (i = 0; i < QUOTA_DATA(qtype); i++)
1433 raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
1434 cpu_to_le32(data_blk_nor + i);
1436 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1437 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1438 c.blks_per_seg + offset + 1;
1440 DBG(1, "\tWriting quota inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1441 get_sb(main_blkaddr),
1442 c.cur_seg[CURSEG_HOT_NODE],
1443 c.blks_per_seg, main_area_node_seg_blk_offset);
1444 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1445 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1455 static int f2fs_update_nat_root(void)
1457 struct f2fs_nat_block *nat_blk = NULL;
1458 uint64_t nat_seg_blk_offset = 0;
1459 enum quota_type qtype;
1462 nat_blk = calloc(F2FS_BLKSIZE, 1);
1463 if(nat_blk == NULL) {
1464 MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
1469 for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1470 if (!((1 << qtype) & c.quota_bits))
1472 nat_blk->entries[sb->qf_ino[qtype]].block_addr =
1473 cpu_to_le32(get_sb(main_blkaddr) +
1474 c.cur_seg[CURSEG_HOT_NODE] *
1475 c.blks_per_seg + i + 1);
1476 nat_blk->entries[sb->qf_ino[qtype]].ino = sb->qf_ino[qtype];
1481 nat_blk->entries[get_sb(root_ino)].block_addr = cpu_to_le32(
1482 get_sb(main_blkaddr) +
1483 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg);
1484 nat_blk->entries[get_sb(root_ino)].ino = sb->root_ino;
1486 /* update node nat */
1487 nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
1488 nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
1490 /* update meta nat */
1491 nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
1492 nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
1494 nat_seg_blk_offset = get_sb(nat_blkaddr);
1496 DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
1497 nat_seg_blk_offset);
1498 if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
1499 MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
1508 static block_t f2fs_add_default_dentry_lpf(void)
1510 struct f2fs_dentry_block *dent_blk;
1511 uint64_t data_blk_offset;
1513 dent_blk = calloc(F2FS_BLKSIZE, 1);
1514 if (dent_blk == NULL) {
1515 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1519 dent_blk->dentry[0].hash_code = 0;
1520 dent_blk->dentry[0].ino = cpu_to_le32(c.lpf_ino);
1521 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1522 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1523 memcpy(dent_blk->filename[0], ".", 1);
1525 dent_blk->dentry[1].hash_code = 0;
1526 dent_blk->dentry[1].ino = sb->root_ino;
1527 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1528 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1529 memcpy(dent_blk->filename[1], "..", 2);
1531 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1532 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1534 data_blk_offset = get_sb(main_blkaddr);
1535 data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg +
1538 DBG(1, "\tWriting default dentry lost+found, at offset 0x%08"PRIx64"\n",
1540 if (dev_write_block(dent_blk, data_blk_offset)) {
1541 MSG(1, "\tError While writing the dentry_blk to disk!!!\n");
1548 return data_blk_offset;
1551 static int f2fs_write_lpf_inode(void)
1553 struct f2fs_node *raw_node;
1554 uint64_t blk_size_bytes, main_area_node_seg_blk_offset;
1555 block_t data_blk_nor;
1560 raw_node = calloc(F2FS_BLKSIZE, 1);
1561 if (raw_node == NULL) {
1562 MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
1566 raw_node->footer.nid = cpu_to_le32(c.lpf_ino);
1567 raw_node->footer.ino = raw_node->footer.nid;
1568 raw_node->footer.cp_ver = cpu_to_le64(1);
1569 raw_node->footer.next_blkaddr = cpu_to_le32(
1570 get_sb(main_blkaddr) +
1571 c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg +
1572 1 + c.quota_inum + 1);
1574 raw_node->i.i_mode = cpu_to_le16(0x41c0); /* 0700 */
1575 raw_node->i.i_links = cpu_to_le32(2);
1576 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1577 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1579 blk_size_bytes = 1 << get_sb(log_blocksize);
1580 raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes);
1581 raw_node->i.i_blocks = cpu_to_le64(2);
1583 raw_node->i.i_atime = cpu_to_le32(mkfs_time);
1584 raw_node->i.i_atime_nsec = 0;
1585 raw_node->i.i_ctime = cpu_to_le32(mkfs_time);
1586 raw_node->i.i_ctime_nsec = 0;
1587 raw_node->i.i_mtime = cpu_to_le32(mkfs_time);
1588 raw_node->i.i_mtime_nsec = 0;
1589 raw_node->i.i_generation = 0;
1590 raw_node->i.i_xattr_nid = 0;
1591 raw_node->i.i_flags = 0;
1592 raw_node->i.i_pino = le32_to_cpu(sb->root_ino);
1593 raw_node->i.i_namelen = le32_to_cpu(strlen(LPF));
1594 memcpy(raw_node->i.i_name, LPF, strlen(LPF));
1595 raw_node->i.i_current_depth = cpu_to_le32(1);
1596 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1598 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1599 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1600 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1603 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1604 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1606 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1607 raw_node->i.i_crtime = cpu_to_le32(mkfs_time);
1608 raw_node->i.i_crtime_nsec = 0;
1611 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1612 raw_node->i.i_compress_algrithm = 0;
1613 raw_node->i.i_log_cluster_size = 0;
1614 raw_node->i.i_compress_flag = 0;
1617 data_blk_nor = f2fs_add_default_dentry_lpf();
1618 if (data_blk_nor == 0) {
1619 MSG(1, "\tError: Failed to add default dentries for lost+found!!!\n");
1623 raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
1625 main_area_node_seg_blk_offset = get_sb(main_blkaddr);
1626 main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
1627 c.blks_per_seg + c.quota_inum + 1;
1629 DBG(1, "\tWriting lost+found inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
1630 get_sb(main_blkaddr),
1631 c.cur_seg[CURSEG_HOT_NODE],
1632 c.blks_per_seg, main_area_node_seg_blk_offset);
1633 if (write_inode(raw_node, main_area_node_seg_blk_offset) < 0) {
1634 MSG(1, "\tError: While writing the raw_node to disk!!!\n");
1645 static int f2fs_add_default_dentry_root(void)
1647 struct f2fs_dentry_block *dent_blk = NULL;
1648 uint64_t data_blk_offset = 0;
1650 dent_blk = calloc(F2FS_BLKSIZE, 1);
1651 if(dent_blk == NULL) {
1652 MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
1656 dent_blk->dentry[0].hash_code = 0;
1657 dent_blk->dentry[0].ino = sb->root_ino;
1658 dent_blk->dentry[0].name_len = cpu_to_le16(1);
1659 dent_blk->dentry[0].file_type = F2FS_FT_DIR;
1660 memcpy(dent_blk->filename[0], ".", 1);
1662 dent_blk->dentry[1].hash_code = 0;
1663 dent_blk->dentry[1].ino = sb->root_ino;
1664 dent_blk->dentry[1].name_len = cpu_to_le16(2);
1665 dent_blk->dentry[1].file_type = F2FS_FT_DIR;
1666 memcpy(dent_blk->filename[1], "..", 2);
1668 /* bitmap for . and .. */
1669 test_and_set_bit_le(0, dent_blk->dentry_bitmap);
1670 test_and_set_bit_le(1, dent_blk->dentry_bitmap);
1673 int len = strlen(LPF);
1674 f2fs_hash_t hash = f2fs_dentry_hash(0, 0, (unsigned char *)LPF, len);
1676 dent_blk->dentry[2].hash_code = cpu_to_le32(hash);
1677 dent_blk->dentry[2].ino = cpu_to_le32(c.lpf_ino);
1678 dent_blk->dentry[2].name_len = cpu_to_le16(len);
1679 dent_blk->dentry[2].file_type = F2FS_FT_DIR;
1680 memcpy(dent_blk->filename[2], LPF, F2FS_SLOT_LEN);
1682 memcpy(dent_blk->filename[3], &LPF[F2FS_SLOT_LEN],
1683 len - F2FS_SLOT_LEN);
1685 test_and_set_bit_le(2, dent_blk->dentry_bitmap);
1686 test_and_set_bit_le(3, dent_blk->dentry_bitmap);
1689 data_blk_offset = get_sb(main_blkaddr);
1690 data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] *
1693 DBG(1, "\tWriting default dentry root, at offset 0x%08"PRIx64"\n",
1695 if (dev_write_block(dent_blk, data_blk_offset)) {
1696 MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
1705 static int f2fs_create_root_dir(void)
1707 enum quota_type qtype;
1710 err = f2fs_write_root_inode();
1712 MSG(1, "\tError: Failed to write root inode!!!\n");
1716 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1717 if (!((1 << qtype) & c.quota_bits))
1719 err = f2fs_write_qf_inode(qtype, i++);
1721 MSG(1, "\tError: Failed to write quota inode!!!\n");
1726 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
1727 err = f2fs_write_lpf_inode();
1729 MSG(1, "\tError: Failed to write lost+found inode!!!\n");
1734 #ifndef WITH_ANDROID
1735 err = f2fs_discard_obsolete_dnode();
1737 MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
1742 err = f2fs_update_nat_root();
1744 MSG(1, "\tError: Failed to update NAT for root!!!\n");
1748 err = f2fs_add_default_dentry_root();
1750 MSG(1, "\tError: Failed to add default dentries for root!!!\n");
1755 MSG(1, "\tError: Could not create the root directory!!!\n");
1760 int f2fs_format_device(void)
1764 err= f2fs_prepare_super_block();
1766 MSG(0, "\tError: Failed to prepare a super block!!!\n");
1771 err = f2fs_trim_devices();
1773 MSG(0, "\tError: Failed to trim whole device!!!\n");
1778 err = f2fs_init_sit_area();
1780 MSG(0, "\tError: Failed to initialise the SIT AREA!!!\n");
1784 err = f2fs_init_nat_area();
1786 MSG(0, "\tError: Failed to initialise the NAT AREA!!!\n");
1790 err = f2fs_create_root_dir();
1792 MSG(0, "\tError: Failed to create the root directory!!!\n");
1796 err = f2fs_write_check_point_pack();
1798 MSG(0, "\tError: Failed to write the check point pack!!!\n");
1802 err = f2fs_write_super_block();
1804 MSG(0, "\tError: Failed to write the super block!!!\n");
1809 MSG(0, "\tError: Could not format the device!!!\n");