2012-10-26 12:56:34 +00:00
|
|
|
/**
|
|
|
|
* f2fs_format.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
|
|
|
* http://www.samsung.com/
|
|
|
|
*
|
2014-04-07 03:10:59 +00:00
|
|
|
* Dual licensed under the GPL or LGPL version 2 licenses.
|
2012-10-26 12:56:34 +00:00
|
|
|
*/
|
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2017-12-01 23:03:30 +00:00
|
|
|
#ifndef ANDROID_WINDOWS_HOST
|
2012-10-26 12:56:34 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mount.h>
|
2017-12-01 23:03:30 +00:00
|
|
|
#endif
|
2012-10-26 12:56:34 +00:00
|
|
|
#include <time.h>
|
2012-10-29 22:35:00 +00:00
|
|
|
#include <uuid/uuid.h>
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2013-01-25 04:23:23 +00:00
|
|
|
#include "f2fs_fs.h"
|
2017-10-30 23:10:27 +00:00
|
|
|
#include "quota.h"
|
2014-01-11 05:16:10 +00:00
|
|
|
#include "f2fs_format_utils.h"
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
extern struct f2fs_configuration c;
|
2015-11-22 03:44:29 +00:00
|
|
|
struct f2fs_super_block raw_sb;
|
|
|
|
struct f2fs_super_block *sb = &raw_sb;
|
2014-11-17 05:03:41 +00:00
|
|
|
struct f2fs_checkpoint *cp;
|
|
|
|
|
|
|
|
/* Return first segment number of each area */
|
2016-09-17 01:41:00 +00:00
|
|
|
#define prev_zone(cur) (c.cur_seg[cur] - c.segs_per_zone)
|
|
|
|
#define next_zone(cur) (c.cur_seg[cur] + c.segs_per_zone)
|
|
|
|
#define last_zone(cur) ((cur - 1) * c.segs_per_zone)
|
|
|
|
#define last_section(cur) (cur + (c.secs_per_zone - 1) * c.segs_per_sec)
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2017-10-30 23:10:27 +00:00
|
|
|
static unsigned int quotatype_bits = 0;
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
const char *media_ext_lists[] = {
|
|
|
|
"jpg",
|
|
|
|
"gif",
|
|
|
|
"png",
|
|
|
|
"avi",
|
|
|
|
"divx",
|
|
|
|
"mp4",
|
|
|
|
"mp3",
|
|
|
|
"3gp",
|
|
|
|
"wmv",
|
|
|
|
"wma",
|
|
|
|
"mpeg",
|
|
|
|
"mkv",
|
|
|
|
"mov",
|
|
|
|
"asx",
|
|
|
|
"asf",
|
|
|
|
"wmx",
|
|
|
|
"svi",
|
|
|
|
"wvx",
|
|
|
|
"wm",
|
|
|
|
"mpg",
|
|
|
|
"mpe",
|
|
|
|
"rm",
|
|
|
|
"ogg",
|
2013-10-17 08:28:15 +00:00
|
|
|
"jpeg",
|
|
|
|
"video",
|
2013-07-16 05:20:41 +00:00
|
|
|
"apk", /* for android system */
|
2017-11-03 03:12:30 +00:00
|
|
|
"so", /* for android system */
|
2012-10-26 12:56:34 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-01 02:48:47 +00:00
|
|
|
const char *hot_ext_lists[] = {
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char **default_ext_list[] = {
|
|
|
|
media_ext_lists,
|
|
|
|
hot_ext_lists
|
|
|
|
};
|
|
|
|
|
2015-11-30 10:41:06 +00:00
|
|
|
static bool is_extension_exist(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
|
2015-11-22 03:44:29 +00:00
|
|
|
char *ext = (char *)sb->extension_list[i];
|
2015-11-30 10:41:06 +00:00
|
|
|
if (!strcmp(ext, name))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
static void cure_extension_list(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2018-03-01 02:48:47 +00:00
|
|
|
const char **extlist;
|
|
|
|
char *ext_str;
|
2012-10-26 12:56:34 +00:00
|
|
|
char *ue;
|
|
|
|
int name_len;
|
2018-03-01 02:48:47 +00:00
|
|
|
int i, pos = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
set_sb(extension_count, 0);
|
|
|
|
memset(sb->extension_list, 0, sizeof(sb->extension_list));
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-03-01 02:48:47 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
ext_str = c.extension_list[i];
|
|
|
|
extlist = default_ext_list[i];
|
|
|
|
|
|
|
|
while (*extlist) {
|
|
|
|
name_len = strlen(*extlist);
|
|
|
|
memcpy(sb->extension_list[pos++], *extlist, name_len);
|
|
|
|
extlist++;
|
2015-11-30 10:41:07 +00:00
|
|
|
}
|
2018-03-01 02:48:47 +00:00
|
|
|
if (i == 0)
|
|
|
|
set_sb(extension_count, pos);
|
|
|
|
else
|
|
|
|
sb->hot_ext_count = pos - get_sb(extension_count);;
|
|
|
|
|
|
|
|
if (!ext_str)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* add user ext list */
|
|
|
|
ue = strtok(ext_str, ", ");
|
|
|
|
while (ue != NULL) {
|
|
|
|
name_len = strlen(ue);
|
|
|
|
if (name_len >= 8) {
|
|
|
|
MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (!is_extension_exist(ue))
|
|
|
|
memcpy(sb->extension_list[pos++], ue, name_len);
|
2015-11-30 10:41:07 +00:00
|
|
|
next:
|
2018-03-01 02:48:47 +00:00
|
|
|
ue = strtok(NULL, ", ");
|
|
|
|
if (pos >= F2FS_MAX_EXTENSION)
|
|
|
|
break;
|
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-03-01 02:48:47 +00:00
|
|
|
if (i == 0)
|
|
|
|
set_sb(extension_count, pos);
|
|
|
|
else
|
|
|
|
sb->hot_ext_count = pos - get_sb(extension_count);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-03-01 02:48:47 +00:00
|
|
|
free(c.extension_list[i]);
|
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 00:49:17 +00:00
|
|
|
static void verify_cur_segs(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
2017-08-25 02:33:10 +00:00
|
|
|
int reorder = 0;
|
2017-04-26 00:49:17 +00:00
|
|
|
|
|
|
|
for (i = 0; i < NR_CURSEG_TYPE; i++) {
|
2017-08-25 02:33:10 +00:00
|
|
|
for (j = i + 1; j < NR_CURSEG_TYPE; j++) {
|
|
|
|
if (c.cur_seg[i] == c.cur_seg[j]) {
|
|
|
|
reorder = 1;
|
2017-04-26 00:49:17 +00:00
|
|
|
break;
|
2017-08-25 02:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 00:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 02:33:10 +00:00
|
|
|
if (!reorder)
|
2017-04-26 00:49:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
c.cur_seg[0] = 0;
|
|
|
|
for (i = 1; i < NR_CURSEG_TYPE; i++)
|
|
|
|
c.cur_seg[i] = next_zone(i - 1);
|
|
|
|
}
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
static int f2fs_prepare_super_block(void)
|
|
|
|
{
|
|
|
|
u_int32_t blk_size_bytes;
|
|
|
|
u_int32_t log_sectorsize, log_sectors_per_block;
|
|
|
|
u_int32_t log_blocksize, log_blks_per_seg;
|
|
|
|
u_int32_t segment_size_bytes, zone_size_bytes;
|
2018-01-25 11:45:13 +00:00
|
|
|
u_int32_t sit_segments, nat_segments;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int32_t blocks_for_sit, blocks_for_nat, blocks_for_ssa;
|
|
|
|
u_int32_t total_valid_blks_available;
|
2016-01-08 02:25:29 +00:00
|
|
|
u_int64_t zone_align_start_offset, diff;
|
|
|
|
u_int64_t total_meta_zones, total_meta_segments;
|
2014-07-10 06:26:04 +00:00
|
|
|
u_int32_t sit_bitmap_size, max_sit_bitmap_size;
|
|
|
|
u_int32_t max_nat_bitmap_size, max_nat_segments;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int32_t total_zones;
|
2017-10-30 23:10:27 +00:00
|
|
|
enum quota_type qtype;
|
2016-11-02 00:23:40 +00:00
|
|
|
int i;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(magic, F2FS_SUPER_MAGIC);
|
|
|
|
set_sb(major_ver, F2FS_MAJOR_VERSION);
|
|
|
|
set_sb(minor_ver, F2FS_MINOR_VERSION);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
log_sectorsize = log_base_2(c.sector_size);
|
|
|
|
log_sectors_per_block = log_base_2(c.sectors_per_blk);
|
2012-10-26 12:56:34 +00:00
|
|
|
log_blocksize = log_sectorsize + log_sectors_per_block;
|
2016-09-17 01:41:00 +00:00
|
|
|
log_blks_per_seg = log_base_2(c.blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(log_sectorsize, log_sectorsize);
|
|
|
|
set_sb(log_sectors_per_block, log_sectors_per_block);
|
|
|
|
|
|
|
|
set_sb(log_blocksize, log_blocksize);
|
|
|
|
set_sb(log_blocks_per_seg, log_blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
set_sb(segs_per_sec, c.segs_per_sec);
|
|
|
|
set_sb(secs_per_zone, c.secs_per_zone);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
blk_size_bytes = 1 << log_blocksize;
|
2016-09-17 01:41:00 +00:00
|
|
|
segment_size_bytes = blk_size_bytes * c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
zone_size_bytes =
|
2016-09-17 01:41:00 +00:00
|
|
|
blk_size_bytes * c.secs_per_zone *
|
|
|
|
c.segs_per_sec * c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
set_sb(checksum_offset, 0);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
set_sb(block_count, c.total_sectors >> log_sectors_per_block);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
zone_align_start_offset =
|
2016-09-17 01:41:00 +00:00
|
|
|
(c.start_sector * c.sector_size +
|
2012-10-29 23:43:28 +00:00
|
|
|
2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
|
|
|
|
zone_size_bytes * zone_size_bytes -
|
2016-09-17 01:41:00 +00:00
|
|
|
c.start_sector * c.sector_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.start_sector % c.sectors_per_blk) {
|
2016-11-02 00:23:40 +00:00
|
|
|
MSG(1, "\t%s: Align start sector number to the page unit\n",
|
|
|
|
c.zoned_mode ? "FAIL" : "WARN");
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\ti.e., start sector: %d, ofs:%d (sects/page: %d)\n",
|
2016-09-17 01:41:00 +00:00
|
|
|
c.start_sector,
|
|
|
|
c.start_sector % c.sectors_per_blk,
|
|
|
|
c.sectors_per_blk);
|
2016-11-02 00:23:40 +00:00
|
|
|
if (c.zoned_mode)
|
|
|
|
return -1;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
|
2015-11-22 03:44:29 +00:00
|
|
|
sb->cp_blkaddr = sb->segment0_blkaddr;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
MSG(0, "Info: zone aligned segment0 blkaddr: %u\n",
|
|
|
|
get_sb(segment0_blkaddr));
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
if (c.zoned_mode && (get_sb(segment0_blkaddr) + c.start_sector /
|
|
|
|
c.sectors_per_blk) % c.zone_blocks) {
|
2016-10-28 07:57:03 +00:00
|
|
|
MSG(1, "\tError: Unaligned segment0 block address %u\n",
|
|
|
|
get_sb(segment0_blkaddr));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
for (i = 0; i < c.ndevs; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
c.devices[i].total_segments =
|
|
|
|
(c.devices[i].total_sectors *
|
|
|
|
c.sector_size - zone_align_start_offset) /
|
|
|
|
segment_size_bytes;
|
|
|
|
c.devices[i].start_blkaddr = 0;
|
|
|
|
c.devices[i].end_blkaddr = c.devices[i].total_segments *
|
|
|
|
c.blks_per_seg - 1 +
|
|
|
|
sb->segment0_blkaddr;
|
|
|
|
} else {
|
|
|
|
c.devices[i].total_segments =
|
|
|
|
c.devices[i].total_sectors /
|
|
|
|
(c.sectors_per_blk * c.blks_per_seg);
|
|
|
|
c.devices[i].start_blkaddr =
|
|
|
|
c.devices[i - 1].end_blkaddr + 1;
|
|
|
|
c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
|
|
|
|
c.devices[i].total_segments *
|
|
|
|
c.blks_per_seg - 1;
|
|
|
|
}
|
|
|
|
if (c.ndevs > 1) {
|
|
|
|
memcpy(sb->devs[i].path, c.devices[i].path, MAX_PATH_LEN);
|
|
|
|
sb->devs[i].total_segments =
|
|
|
|
cpu_to_le32(c.devices[i].total_segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
c.total_segments += c.devices[i].total_segments;
|
|
|
|
}
|
|
|
|
set_sb(segment_count, (c.total_segments / c.segs_per_zone *
|
|
|
|
c.segs_per_zone));
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(segment_count_ckpt, F2FS_NUMBER_OF_CHECKPOINT_PACK);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
set_sb(sit_blkaddr, get_sb(segment0_blkaddr) +
|
|
|
|
get_sb(segment_count_ckpt) * c.blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-11-14 18:53:32 +00:00
|
|
|
blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
sit_segments = SEG_ALIGN(blocks_for_sit);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(segment_count_sit, sit_segments * 2);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(nat_blkaddr, get_sb(sit_blkaddr) + get_sb(segment_count_sit) *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
total_valid_blks_available = (get_sb(segment_count) -
|
2016-09-17 01:41:00 +00:00
|
|
|
(get_sb(segment_count_ckpt) +
|
|
|
|
get_sb(segment_count_sit))) * c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-11-14 18:53:32 +00:00
|
|
|
blocks_for_nat = SIZE_ALIGN(total_valid_blks_available,
|
2016-09-17 01:41:00 +00:00
|
|
|
NAT_ENTRY_PER_BLOCK);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-01-25 11:45:13 +00:00
|
|
|
if (c.large_nat_bitmap) {
|
|
|
|
nat_segments = SEG_ALIGN(blocks_for_nat) *
|
|
|
|
DEFAULT_NAT_ENTRY_RATIO / 100;
|
|
|
|
set_sb(segment_count_nat, nat_segments ? nat_segments : 1);
|
|
|
|
max_nat_bitmap_size = (get_sb(segment_count_nat) <<
|
|
|
|
log_blks_per_seg) / 8;
|
|
|
|
set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
|
|
|
|
} else {
|
|
|
|
set_sb(segment_count_nat, SEG_ALIGN(blocks_for_nat));
|
|
|
|
max_nat_bitmap_size = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
/*
|
|
|
|
* The number of node segments should not be exceeded a "Threshold".
|
|
|
|
* This number resizes NAT bitmap area in a CP page.
|
|
|
|
* So the threshold is determined not to overflow one CP page
|
|
|
|
*/
|
2014-11-17 05:03:41 +00:00
|
|
|
sit_bitmap_size = ((get_sb(segment_count_sit) / 2) <<
|
2012-10-26 12:56:34 +00:00
|
|
|
log_blks_per_seg) / 8;
|
2014-07-10 06:26:04 +00:00
|
|
|
|
|
|
|
if (sit_bitmap_size > MAX_SIT_BITMAP_SIZE)
|
|
|
|
max_sit_bitmap_size = MAX_SIT_BITMAP_SIZE;
|
|
|
|
else
|
|
|
|
max_sit_bitmap_size = sit_bitmap_size;
|
|
|
|
|
2018-01-25 11:45:13 +00:00
|
|
|
if (c.large_nat_bitmap) {
|
|
|
|
/* use cp_payload if free space of f2fs_checkpoint is not enough */
|
|
|
|
if (max_sit_bitmap_size + max_nat_bitmap_size >
|
|
|
|
MAX_BITMAP_SIZE_IN_CKPT) {
|
|
|
|
u_int32_t diff = max_sit_bitmap_size +
|
|
|
|
max_nat_bitmap_size -
|
|
|
|
MAX_BITMAP_SIZE_IN_CKPT;
|
|
|
|
set_sb(cp_payload, F2FS_BLK_ALIGN(diff));
|
|
|
|
} else {
|
|
|
|
set_sb(cp_payload, 0);
|
|
|
|
}
|
2014-07-10 06:26:04 +00:00
|
|
|
} else {
|
2018-01-25 11:45:13 +00:00
|
|
|
/*
|
|
|
|
* It should be reserved minimum 1 segment for nat.
|
|
|
|
* When sit is too large, we should expand cp area.
|
|
|
|
* It requires more pages for cp.
|
|
|
|
*/
|
|
|
|
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
|
|
|
|
max_nat_bitmap_size = CHECKSUM_OFFSET -
|
|
|
|
sizeof(struct f2fs_checkpoint) + 1;
|
|
|
|
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
|
|
|
|
} else {
|
|
|
|
max_nat_bitmap_size =
|
|
|
|
CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1
|
|
|
|
- max_sit_bitmap_size;
|
|
|
|
set_sb(cp_payload, 0);
|
|
|
|
}
|
|
|
|
max_nat_segments = (max_nat_bitmap_size * 8) >> log_blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-01-25 11:45:13 +00:00
|
|
|
if (get_sb(segment_count_nat) > max_nat_segments)
|
|
|
|
set_sb(segment_count_nat, max_nat_segments);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2018-01-25 11:45:13 +00:00
|
|
|
set_sb(segment_count_nat, get_sb(segment_count_nat) * 2);
|
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(ssa_blkaddr, get_sb(nat_blkaddr) + get_sb(segment_count_nat) *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
total_valid_blks_available = (get_sb(segment_count) -
|
|
|
|
(get_sb(segment_count_ckpt) +
|
|
|
|
get_sb(segment_count_sit) +
|
|
|
|
get_sb(segment_count_nat))) *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
blocks_for_ssa = total_valid_blks_available /
|
2016-09-17 01:41:00 +00:00
|
|
|
c.blks_per_seg + 1;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(segment_count_ssa, SEG_ALIGN(blocks_for_ssa));
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
total_meta_segments = get_sb(segment_count_ckpt) +
|
|
|
|
get_sb(segment_count_sit) +
|
|
|
|
get_sb(segment_count_nat) +
|
|
|
|
get_sb(segment_count_ssa);
|
2016-09-17 01:41:00 +00:00
|
|
|
diff = total_meta_segments % (c.segs_per_zone);
|
2012-10-26 12:56:34 +00:00
|
|
|
if (diff)
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(segment_count_ssa, get_sb(segment_count_ssa) +
|
2016-09-17 01:41:00 +00:00
|
|
|
(c.segs_per_zone - diff));
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2016-01-08 02:25:29 +00:00
|
|
|
total_meta_zones = ZONE_ALIGN(total_meta_segments *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.blks_per_seg);
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2016-01-08 02:25:29 +00:00
|
|
|
set_sb(main_blkaddr, get_sb(segment0_blkaddr) + total_meta_zones *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.segs_per_zone * c.blks_per_seg);
|
2016-01-08 02:25:29 +00:00
|
|
|
|
2016-10-28 07:57:03 +00:00
|
|
|
if (c.zoned_mode) {
|
|
|
|
/*
|
|
|
|
* Make sure there is enough randomly writeable
|
|
|
|
* space at the beginning of the disk.
|
|
|
|
*/
|
|
|
|
unsigned long main_blkzone = get_sb(main_blkaddr) / c.zone_blocks;
|
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
if (c.devices[0].zoned_model == F2FS_ZONED_HM &&
|
|
|
|
c.devices[0].nr_rnd_zones < main_blkzone) {
|
|
|
|
MSG(0, "\tError: Device does not have enough random "
|
|
|
|
"write zones for F2FS volume (%lu needed)\n",
|
2016-10-28 07:57:03 +00:00
|
|
|
main_blkzone);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
total_zones = get_sb(segment_count) / (c.segs_per_zone) -
|
2016-01-08 02:25:29 +00:00
|
|
|
total_meta_zones;
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
set_sb(section_count, total_zones * c.secs_per_zone);
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
set_sb(segment_count_main, get_sb(section_count) * c.segs_per_sec);
|
2014-11-17 05:03:41 +00:00
|
|
|
|
2015-08-10 22:45:10 +00:00
|
|
|
/* Let's determine the best reserved and overprovisioned space */
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.overprovision == 0)
|
|
|
|
c.overprovision = get_best_overprovision(sb);
|
2015-08-10 22:45:10 +00:00
|
|
|
|
2016-11-28 22:07:18 +00:00
|
|
|
if (c.overprovision == 0 || c.total_segments < F2FS_MIN_SEGMENTS ||
|
|
|
|
(c.devices[0].total_sectors *
|
|
|
|
c.sector_size < zone_align_start_offset) ||
|
|
|
|
(get_sb(segment_count_main) - 2) < c.reserved_segments) {
|
|
|
|
MSG(0, "\tError: Device size is not sufficient for F2FS volume\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-06 09:58:43 +00:00
|
|
|
c.reserved_segments =
|
|
|
|
(2 * (100 / c.overprovision + 1) + 6)
|
|
|
|
* c.segs_per_sec;
|
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
uuid_generate(sb->uuid);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-07-26 15:01:35 +00:00
|
|
|
/* precompute checksum seed for metadata */
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
|
|
|
|
c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
|
|
|
|
MAX_VOLUME_NAME, strlen(c.vol_label));
|
2014-11-17 05:03:41 +00:00
|
|
|
set_sb(node_ino, 1);
|
|
|
|
set_sb(meta_ino, 2);
|
|
|
|
set_sb(root_ino, 3);
|
2018-02-23 03:17:56 +00:00
|
|
|
c.next_free_nid = 4;
|
2017-10-30 23:10:27 +00:00
|
|
|
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
|
|
|
|
quotatype_bits = QUOTA_USR_BIT | QUOTA_GRP_BIT;
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
|
|
|
|
quotatype_bits |= QUOTA_PRJ_BIT;
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:21:09 +00:00
|
|
|
for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (!((1 << qtype) & quotatype_bits))
|
|
|
|
continue;
|
2018-02-23 03:17:56 +00:00
|
|
|
sb->qf_ino[qtype] = cpu_to_le32(c.next_free_nid++);
|
2017-10-30 23:21:09 +00:00
|
|
|
MSG(0, "Info: add quota type = %u => %u\n",
|
2018-02-23 03:17:56 +00:00
|
|
|
qtype, c.next_free_nid - 1);
|
2017-10-30 23:21:09 +00:00
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
if (total_zones <= 6) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: %d zones: Need more zones "
|
|
|
|
"by shrinking zone size\n", total_zones);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.heap) {
|
|
|
|
c.cur_seg[CURSEG_HOT_NODE] =
|
|
|
|
last_section(last_zone(total_zones));
|
|
|
|
c.cur_seg[CURSEG_WARM_NODE] = prev_zone(CURSEG_HOT_NODE);
|
|
|
|
c.cur_seg[CURSEG_COLD_NODE] = prev_zone(CURSEG_WARM_NODE);
|
|
|
|
c.cur_seg[CURSEG_HOT_DATA] = prev_zone(CURSEG_COLD_NODE);
|
|
|
|
c.cur_seg[CURSEG_COLD_DATA] = 0;
|
|
|
|
c.cur_seg[CURSEG_WARM_DATA] = next_zone(CURSEG_COLD_DATA);
|
2012-10-26 12:56:34 +00:00
|
|
|
} else {
|
2016-09-17 01:41:00 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_NODE] = 0;
|
|
|
|
c.cur_seg[CURSEG_WARM_NODE] = next_zone(CURSEG_HOT_NODE);
|
|
|
|
c.cur_seg[CURSEG_COLD_NODE] = next_zone(CURSEG_WARM_NODE);
|
|
|
|
c.cur_seg[CURSEG_HOT_DATA] = next_zone(CURSEG_COLD_NODE);
|
2017-03-25 00:56:24 +00:00
|
|
|
c.cur_seg[CURSEG_COLD_DATA] =
|
|
|
|
max(last_zone((total_zones >> 2)),
|
|
|
|
next_zone(CURSEG_COLD_NODE));
|
|
|
|
c.cur_seg[CURSEG_WARM_DATA] =
|
|
|
|
max(last_zone((total_zones >> 1)),
|
|
|
|
next_zone(CURSEG_COLD_DATA));
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 00:49:17 +00:00
|
|
|
/* if there is redundancy, reassign it */
|
|
|
|
verify_cur_segs();
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
cure_extension_list();
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-07 04:44:57 +00:00
|
|
|
/* get kernel version */
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.kd >= 0) {
|
|
|
|
dev_read_version(c.version, 0, VERSION_LEN);
|
|
|
|
get_kernel_version(c.version);
|
|
|
|
MSG(0, "Info: format version with\n \"%s\"\n", c.version);
|
2014-11-07 04:44:57 +00:00
|
|
|
} else {
|
2018-05-01 20:54:25 +00:00
|
|
|
get_kernel_uname_version(c.version);
|
2014-11-07 04:44:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
memcpy(sb->version, c.version, VERSION_LEN);
|
|
|
|
memcpy(sb->init_version, c.version, VERSION_LEN);
|
2014-11-07 04:44:57 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
sb->feature = c.feature;
|
2015-04-22 03:03:40 +00:00
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_init_sit_area(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2013-01-25 08:20:16 +00:00
|
|
|
u_int32_t blk_size, seg_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int32_t index = 0;
|
2013-01-25 08:20:16 +00:00
|
|
|
u_int64_t sit_seg_addr = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int8_t *zero_buf = NULL;
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
blk_size = 1 << get_sb(log_blocksize);
|
|
|
|
seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
zero_buf = calloc(sizeof(u_int8_t), seg_size);
|
2012-10-26 12:56:34 +00:00
|
|
|
if(zero_buf == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for sit_zero_buf!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
sit_seg_addr = get_sb(sit_blkaddr);
|
2013-01-25 08:20:16 +00:00
|
|
|
sit_seg_addr *= blk_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-03-31 22:33:01 +00:00
|
|
|
DBG(1, "\tFilling sit area at offset 0x%08"PRIx64"\n", sit_seg_addr);
|
2014-11-17 05:03:41 +00:00
|
|
|
for (index = 0; index < (get_sb(segment_count_sit) / 2); index++) {
|
2014-03-31 22:33:01 +00:00
|
|
|
if (dev_fill(zero_buf, sit_seg_addr, seg_size)) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: While zeroing out the sit area "
|
|
|
|
"on disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
free(zero_buf);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-25 08:20:16 +00:00
|
|
|
sit_seg_addr += seg_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(zero_buf);
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_init_nat_area(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2013-01-25 08:20:16 +00:00
|
|
|
u_int32_t blk_size, seg_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int32_t index = 0;
|
2013-01-25 08:20:16 +00:00
|
|
|
u_int64_t nat_seg_addr = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int8_t *nat_buf = NULL;
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
blk_size = 1 << get_sb(log_blocksize);
|
|
|
|
seg_size = (1 << get_sb(log_blocks_per_seg)) * blk_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
nat_buf = calloc(sizeof(u_int8_t), seg_size);
|
2012-10-26 12:56:34 +00:00
|
|
|
if (nat_buf == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for nat_zero_blk!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
nat_seg_addr = get_sb(nat_blkaddr);
|
2013-01-25 08:20:16 +00:00
|
|
|
nat_seg_addr *= blk_size;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-03-31 22:33:01 +00:00
|
|
|
DBG(1, "\tFilling nat area at offset 0x%08"PRIx64"\n", nat_seg_addr);
|
2014-11-17 05:03:41 +00:00
|
|
|
for (index = 0; index < get_sb(segment_count_nat) / 2; index++) {
|
2014-03-31 22:33:01 +00:00
|
|
|
if (dev_fill(nat_buf, nat_seg_addr, seg_size)) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: While zeroing out the nat area "
|
|
|
|
"on disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
free(nat_buf);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-25 08:20:16 +00:00
|
|
|
nat_seg_addr = nat_seg_addr + (2 * seg_size);
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(nat_buf);
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_write_check_point_pack(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
|
|
|
struct f2fs_summary_block *sum = NULL;
|
2016-03-11 08:43:54 +00:00
|
|
|
struct f2fs_journal *journal;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int32_t blk_size_bytes;
|
2017-02-09 02:55:43 +00:00
|
|
|
u_int32_t nat_bits_bytes, nat_bits_blocks;
|
|
|
|
unsigned char *nat_bits = NULL, *empty_nat_bits;
|
2016-09-16 01:44:41 +00:00
|
|
|
u_int64_t cp_seg_blk = 0;
|
2017-02-09 02:55:43 +00:00
|
|
|
u_int32_t crc = 0, flags;
|
2014-07-29 23:24:52 +00:00
|
|
|
unsigned int i;
|
2014-07-10 06:26:04 +00:00
|
|
|
char *cp_payload = NULL;
|
2014-11-17 05:03:41 +00:00
|
|
|
char *sum_compact, *sum_compact_p;
|
2014-11-13 11:15:04 +00:00
|
|
|
struct f2fs_summary *sum_entry;
|
2017-10-30 23:10:27 +00:00
|
|
|
enum quota_type qtype;
|
|
|
|
int off;
|
2015-02-06 08:11:41 +00:00
|
|
|
int ret = -1;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
cp = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
if (cp == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for f2fs_checkpoint!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
return ret;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
sum = calloc(F2FS_BLKSIZE, 1);
|
2012-10-26 12:56:34 +00:00
|
|
|
if (sum == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for summay_node!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
sum_compact = calloc(F2FS_BLKSIZE, 1);
|
2015-12-21 07:44:41 +00:00
|
|
|
if (sum_compact == NULL) {
|
2014-11-13 11:15:04 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for summay buffer!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_sum;
|
2014-11-13 11:15:04 +00:00
|
|
|
}
|
2014-11-17 05:03:41 +00:00
|
|
|
sum_compact_p = sum_compact;
|
2014-11-13 11:15:04 +00:00
|
|
|
|
2017-02-09 02:55:43 +00:00
|
|
|
nat_bits_bytes = get_sb(segment_count_nat) << 5;
|
|
|
|
nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
|
|
|
|
F2FS_BLKSIZE - 1);
|
|
|
|
nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
|
|
|
|
if (nat_bits == NULL) {
|
|
|
|
MSG(1, "\tError: Calloc Failed for nat bits buffer!!!\n");
|
|
|
|
goto free_sum_compact;
|
|
|
|
}
|
|
|
|
|
2014-07-10 06:26:04 +00:00
|
|
|
cp_payload = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
if (cp_payload == NULL) {
|
|
|
|
MSG(1, "\tError: Calloc Failed for cp_payload!!!\n");
|
2017-02-09 02:55:43 +00:00
|
|
|
goto free_nat_bits;
|
2014-07-10 06:26:04 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
/* 1. cp page 1 of checkpoint pack 1 */
|
2018-01-11 02:17:20 +00:00
|
|
|
srand(time(NULL));
|
2017-08-23 20:33:00 +00:00
|
|
|
cp->checkpoint_ver = cpu_to_le64(rand() | 0x1);
|
2016-09-17 01:41:00 +00:00
|
|
|
set_cp(cur_node_segno[0], c.cur_seg[CURSEG_HOT_NODE]);
|
|
|
|
set_cp(cur_node_segno[1], c.cur_seg[CURSEG_WARM_NODE]);
|
|
|
|
set_cp(cur_node_segno[2], c.cur_seg[CURSEG_COLD_NODE]);
|
|
|
|
set_cp(cur_data_segno[0], c.cur_seg[CURSEG_HOT_DATA]);
|
|
|
|
set_cp(cur_data_segno[1], c.cur_seg[CURSEG_WARM_DATA]);
|
|
|
|
set_cp(cur_data_segno[2], c.cur_seg[CURSEG_COLD_DATA]);
|
2012-10-26 12:56:34 +00:00
|
|
|
for (i = 3; i < MAX_ACTIVE_NODE_LOGS; i++) {
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(cur_node_segno[i], 0xffffffff);
|
|
|
|
set_cp(cur_data_segno[i], 0xffffffff);
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 03:17:56 +00:00
|
|
|
set_cp(cur_node_blkoff[0], 1 + c.quota_inum);
|
|
|
|
set_cp(cur_data_blkoff[0], 1 + c.quota_dnum);
|
|
|
|
set_cp(valid_block_count, 2 + c.quota_inum + c.quota_dnum);
|
2016-09-17 01:41:00 +00:00
|
|
|
set_cp(rsvd_segment_count, c.reserved_segments);
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(overprov_segment_count, (get_sb(segment_count_main) -
|
|
|
|
get_cp(rsvd_segment_count)) *
|
2016-09-17 01:41:00 +00:00
|
|
|
c.overprovision / 100);
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
|
|
|
|
get_cp(rsvd_segment_count));
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
MSG(0, "Info: Overprovision ratio = %.3lf%%\n", c.overprovision);
|
2015-08-10 22:45:10 +00:00
|
|
|
MSG(0, "Info: Overprovision segments = %u (GC reserved = %u)\n",
|
|
|
|
get_cp(overprov_segment_count),
|
2016-09-17 01:41:00 +00:00
|
|
|
c.reserved_segments);
|
2015-08-10 22:45:10 +00:00
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
/* main segments - reserved segments - (node + data segments) */
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(free_segment_count, get_sb(segment_count_main) - 6);
|
|
|
|
set_cp(user_block_count, ((get_cp(free_segment_count) + 6 -
|
2016-09-17 01:41:00 +00:00
|
|
|
get_cp(overprov_segment_count)) * c.blks_per_seg));
|
2014-11-13 11:15:04 +00:00
|
|
|
/* cp page (2), data summaries (1), node summaries (3) */
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(cp_pack_total_block_count, 6 + get_sb(cp_payload));
|
2017-02-09 02:55:43 +00:00
|
|
|
flags = CP_UMOUNT_FLAG | CP_COMPACT_SUM_FLAG;
|
|
|
|
if (get_cp(cp_pack_total_block_count) <=
|
|
|
|
(1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
|
|
|
|
flags |= CP_NAT_BITS_FLAG;
|
2017-04-28 09:16:31 +00:00
|
|
|
|
|
|
|
if (c.trimmed)
|
|
|
|
flags |= CP_TRIMMED_FLAG;
|
|
|
|
|
2018-01-25 11:45:13 +00:00
|
|
|
if (c.large_nat_bitmap)
|
|
|
|
flags |= CP_LARGE_NAT_BITMAP_FLAG;
|
|
|
|
|
2017-02-09 02:55:43 +00:00
|
|
|
set_cp(ckpt_flags, flags);
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(cp_pack_start_sum, 1 + get_sb(cp_payload));
|
2018-02-23 03:17:56 +00:00
|
|
|
set_cp(valid_node_count, 1 + c.quota_inum);
|
|
|
|
set_cp(valid_inode_count, 1 + c.quota_inum);
|
|
|
|
set_cp(next_free_nid, c.next_free_nid);
|
2014-11-17 05:03:41 +00:00
|
|
|
set_cp(sit_ver_bitmap_bytesize, ((get_sb(segment_count_sit) / 2) <<
|
|
|
|
get_sb(log_blocks_per_seg)) / 8);
|
|
|
|
|
|
|
|
set_cp(nat_ver_bitmap_bytesize, ((get_sb(segment_count_nat) / 2) <<
|
|
|
|
get_sb(log_blocks_per_seg)) / 8);
|
|
|
|
|
|
|
|
set_cp(checksum_offset, CHECKSUM_OFFSET);
|
|
|
|
|
|
|
|
crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET);
|
|
|
|
*((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) =
|
2013-06-19 11:49:47 +00:00
|
|
|
cpu_to_le32(crc);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
blk_size_bytes = 1 << get_sb(log_blocksize);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
if (blk_size_bytes != F2FS_BLKSIZE) {
|
|
|
|
MSG(1, "\tError: Wrong block size %d / %d!!!\n",
|
|
|
|
blk_size_bytes, F2FS_BLKSIZE);
|
|
|
|
goto free_cp_payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp_seg_blk = get_sb(segment0_blkaddr);
|
|
|
|
|
|
|
|
DBG(1, "\tWriting main segments, cp at offset 0x%08"PRIx64"\n",
|
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(cp, cp_seg_blk)) {
|
2014-11-17 05:03:41 +00:00
|
|
|
MSG(1, "\tError: While writing the cp to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
for (i = 0; i < get_sb(cp_payload); i++) {
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
|
|
|
if (dev_fill_block(cp_payload, cp_seg_blk)) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: While zeroing out the sit bitmap area "
|
|
|
|
"on disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2014-07-10 06:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* Prepare and write Segment summary for HOT/WARM/COLD DATA
|
|
|
|
*
|
|
|
|
* The structure of compact summary
|
|
|
|
* +-------------------+
|
|
|
|
* | nat_journal |
|
|
|
|
* +-------------------+
|
|
|
|
* | sit_journal |
|
|
|
|
* +-------------------+
|
|
|
|
* | hot data summary |
|
|
|
|
* +-------------------+
|
|
|
|
* | warm data summary |
|
|
|
|
* +-------------------+
|
|
|
|
* | cold data summary |
|
|
|
|
* +-------------------+
|
|
|
|
*/
|
2012-10-29 22:35:21 +00:00
|
|
|
memset(sum, 0, sizeof(struct f2fs_summary_block));
|
2012-10-26 12:56:34 +00:00
|
|
|
SET_SUM_TYPE((&sum->footer), SUM_TYPE_DATA);
|
|
|
|
|
2016-03-11 08:43:54 +00:00
|
|
|
journal = &sum->journal;
|
2018-02-23 03:17:56 +00:00
|
|
|
journal->n_nats = cpu_to_le16(1 + c.quota_inum);
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->nat_j.entries[0].nid = sb->root_ino;
|
|
|
|
journal->nat_j.entries[0].ne.version = 0;
|
|
|
|
journal->nat_j.entries[0].ne.ino = sb->root_ino;
|
|
|
|
journal->nat_j.entries[0].ne.block_addr = cpu_to_le32(
|
2014-11-17 05:03:41 +00:00
|
|
|
get_sb(main_blkaddr) +
|
2016-09-17 01:41:00 +00:00
|
|
|
get_cp(cur_node_segno[0]) * c.blks_per_seg);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-10-30 23:10:27 +00:00
|
|
|
for (qtype = 0, i = 1; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (sb->qf_ino[qtype] == 0)
|
|
|
|
continue;
|
|
|
|
journal->nat_j.entries[i].nid = sb->qf_ino[qtype];
|
|
|
|
journal->nat_j.entries[i].ne.version = 0;
|
|
|
|
journal->nat_j.entries[i].ne.ino = sb->qf_ino[qtype];
|
|
|
|
journal->nat_j.entries[i].ne.block_addr = cpu_to_le32(
|
|
|
|
get_sb(main_blkaddr) +
|
|
|
|
get_cp(cur_node_segno[0]) *
|
|
|
|
c.blks_per_seg + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:43:54 +00:00
|
|
|
memcpy(sum_compact_p, &journal->n_nats, SUM_JOURNAL_SIZE);
|
2014-11-17 05:03:41 +00:00
|
|
|
sum_compact_p += SUM_JOURNAL_SIZE;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
memset(sum, 0, sizeof(struct f2fs_summary_block));
|
|
|
|
/* inode sit for root */
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->n_sits = cpu_to_le16(6);
|
|
|
|
journal->sit_j.entries[0].segno = cp->cur_node_segno[0];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[0].se.vblocks =
|
2017-10-30 23:10:27 +00:00
|
|
|
cpu_to_le16((CURSEG_HOT_NODE << 10) |
|
2018-02-23 03:17:56 +00:00
|
|
|
(1 + c.quota_inum));
|
2016-03-11 08:43:54 +00:00
|
|
|
f2fs_set_bit(0, (char *)journal->sit_j.entries[0].se.valid_map);
|
2018-02-23 03:17:56 +00:00
|
|
|
for (i = 1; i <= c.quota_inum; i++)
|
2017-10-30 23:10:27 +00:00
|
|
|
f2fs_set_bit(i, (char *)journal->sit_j.entries[0].se.valid_map);
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->sit_j.entries[1].segno = cp->cur_node_segno[1];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[1].se.vblocks =
|
|
|
|
cpu_to_le16((CURSEG_WARM_NODE << 10));
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->sit_j.entries[2].segno = cp->cur_node_segno[2];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[2].se.vblocks =
|
|
|
|
cpu_to_le16((CURSEG_COLD_NODE << 10));
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
/* data sit for root */
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->sit_j.entries[3].segno = cp->cur_data_segno[0];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[3].se.vblocks =
|
2017-10-30 23:10:27 +00:00
|
|
|
cpu_to_le16((CURSEG_HOT_DATA << 10) |
|
2018-02-23 03:17:56 +00:00
|
|
|
(1 + c.quota_dnum));
|
2016-03-11 08:43:54 +00:00
|
|
|
f2fs_set_bit(0, (char *)journal->sit_j.entries[3].se.valid_map);
|
2018-02-23 03:17:56 +00:00
|
|
|
for (i = 1; i <= c.quota_dnum; i++)
|
2017-10-30 23:10:27 +00:00
|
|
|
f2fs_set_bit(i, (char *)journal->sit_j.entries[3].se.valid_map);
|
|
|
|
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->sit_j.entries[4].segno = cp->cur_data_segno[1];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[4].se.vblocks =
|
|
|
|
cpu_to_le16((CURSEG_WARM_DATA << 10));
|
2016-03-11 08:43:54 +00:00
|
|
|
journal->sit_j.entries[5].segno = cp->cur_data_segno[2];
|
2016-09-17 01:41:00 +00:00
|
|
|
journal->sit_j.entries[5].se.vblocks =
|
|
|
|
cpu_to_le16((CURSEG_COLD_DATA << 10));
|
2016-03-11 08:43:54 +00:00
|
|
|
|
|
|
|
memcpy(sum_compact_p, &journal->n_sits, SUM_JOURNAL_SIZE);
|
2014-11-17 05:03:41 +00:00
|
|
|
sum_compact_p += SUM_JOURNAL_SIZE;
|
2014-11-13 11:15:04 +00:00
|
|
|
|
|
|
|
/* hot data summary */
|
2014-11-17 05:03:41 +00:00
|
|
|
sum_entry = (struct f2fs_summary *)sum_compact_p;
|
2015-11-22 03:44:29 +00:00
|
|
|
sum_entry->nid = sb->root_ino;
|
2014-11-13 11:15:04 +00:00
|
|
|
sum_entry->ofs_in_node = 0;
|
2017-10-30 23:10:27 +00:00
|
|
|
|
|
|
|
off = 1;
|
|
|
|
for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (sb->qf_ino[qtype] == 0)
|
|
|
|
continue;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < QUOTA_DATA(qtype); j++) {
|
|
|
|
(sum_entry + off + j)->nid = sb->qf_ino[qtype];
|
2018-02-08 15:17:11 +00:00
|
|
|
(sum_entry + off + j)->ofs_in_node = cpu_to_le16(j);
|
2017-10-30 23:10:27 +00:00
|
|
|
}
|
|
|
|
off += QUOTA_DATA(qtype);
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* warm data summary, nothing to do */
|
|
|
|
/* cold data summary, nothing to do */
|
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
2014-11-13 11:15:04 +00:00
|
|
|
DBG(1, "\tWriting Segment summary for HOT/WARM/COLD_DATA, at offset 0x%08"PRIx64"\n",
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(sum_compact, cp_seg_blk)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* Prepare and write Segment summary for HOT_NODE */
|
2012-10-29 22:35:21 +00:00
|
|
|
memset(sum, 0, sizeof(struct f2fs_summary_block));
|
|
|
|
SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
sum->entries[0].nid = sb->root_ino;
|
2012-10-29 22:35:21 +00:00
|
|
|
sum->entries[0].ofs_in_node = 0;
|
2017-10-30 23:10:27 +00:00
|
|
|
for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (sb->qf_ino[qtype] == 0)
|
|
|
|
continue;
|
|
|
|
sum->entries[1 + i].nid = sb->qf_ino[qtype];
|
|
|
|
sum->entries[1 + i].ofs_in_node = 0;
|
|
|
|
i++;
|
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
2014-11-13 11:15:04 +00:00
|
|
|
DBG(1, "\tWriting Segment summary for HOT_NODE, at offset 0x%08"PRIx64"\n",
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(sum, cp_seg_blk)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* Fill segment summary for WARM_NODE to zero. */
|
2012-10-26 12:56:34 +00:00
|
|
|
memset(sum, 0, sizeof(struct f2fs_summary_block));
|
2012-10-29 22:35:21 +00:00
|
|
|
SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
|
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
2014-11-13 11:15:04 +00:00
|
|
|
DBG(1, "\tWriting Segment summary for WARM_NODE, at offset 0x%08"PRIx64"\n",
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(sum, cp_seg_blk)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* Fill segment summary for COLD_NODE to zero. */
|
2012-10-26 12:56:34 +00:00
|
|
|
memset(sum, 0, sizeof(struct f2fs_summary_block));
|
2012-10-29 22:35:21 +00:00
|
|
|
SET_SUM_TYPE((&sum->footer), SUM_TYPE_NODE);
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
2014-11-13 11:15:04 +00:00
|
|
|
DBG(1, "\tWriting Segment summary for COLD_NODE, at offset 0x%08"PRIx64"\n",
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(sum, cp_seg_blk)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the sum_blk to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* cp page2 */
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
|
|
|
DBG(1, "\tWriting cp page2, at offset 0x%08"PRIx64"\n", cp_seg_blk);
|
|
|
|
if (dev_write_block(cp, cp_seg_blk)) {
|
2014-11-17 05:03:41 +00:00
|
|
|
MSG(1, "\tError: While writing the cp to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:55:43 +00:00
|
|
|
/* write NAT bits, if possible */
|
|
|
|
if (flags & CP_NAT_BITS_FLAG) {
|
2017-06-05 20:44:50 +00:00
|
|
|
uint32_t i;
|
2017-02-09 02:55:43 +00:00
|
|
|
|
|
|
|
*(__le64 *)nat_bits = get_cp_crc(cp);
|
|
|
|
empty_nat_bits = nat_bits + 8 + nat_bits_bytes;
|
|
|
|
memset(empty_nat_bits, 0xff, nat_bits_bytes);
|
|
|
|
test_and_clear_bit_le(0, empty_nat_bits);
|
|
|
|
|
|
|
|
/* write the last blocks in cp pack */
|
|
|
|
cp_seg_blk = get_sb(segment0_blkaddr) + (1 <<
|
|
|
|
get_sb(log_blocks_per_seg)) - nat_bits_blocks;
|
|
|
|
|
|
|
|
DBG(1, "\tWriting NAT bits pages, at offset 0x%08"PRIx64"\n",
|
|
|
|
cp_seg_blk);
|
|
|
|
|
|
|
|
for (i = 0; i < nat_bits_blocks; i++) {
|
|
|
|
if (dev_write_block(nat_bits + i *
|
|
|
|
F2FS_BLKSIZE, cp_seg_blk + i)) {
|
|
|
|
MSG(1, "\tError: write NAT bits to disk!!!\n");
|
|
|
|
goto free_cp_payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* cp page 1 of check point pack 2
|
2012-10-29 22:35:21 +00:00
|
|
|
* Initiatialize other checkpoint pack with version zero
|
|
|
|
*/
|
2014-11-17 05:03:41 +00:00
|
|
|
cp->checkpoint_ver = 0;
|
2012-10-29 22:35:21 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET);
|
|
|
|
*((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) =
|
2013-06-19 11:49:47 +00:00
|
|
|
cpu_to_le32(crc);
|
2016-09-17 01:41:00 +00:00
|
|
|
cp_seg_blk = get_sb(segment0_blkaddr) + c.blks_per_seg;
|
2016-09-16 01:44:41 +00:00
|
|
|
DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
|
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(cp, cp_seg_blk)) {
|
2014-11-17 05:03:41 +00:00
|
|
|
MSG(1, "\tError: While writing the cp to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2012-10-29 22:35:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
for (i = 0; i < get_sb(cp_payload); i++) {
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk++;
|
|
|
|
if (dev_fill_block(cp_payload, cp_seg_blk)) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: While zeroing out the sit bitmap area "
|
|
|
|
"on disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2014-07-10 06:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:04 +00:00
|
|
|
/* cp page 2 of check point pack 2 */
|
2016-09-16 01:44:41 +00:00
|
|
|
cp_seg_blk += (le32_to_cpu(cp->cp_pack_total_block_count) -
|
|
|
|
get_sb(cp_payload) - 1);
|
|
|
|
DBG(1, "\tWriting cp page 2 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
|
|
|
|
cp_seg_blk);
|
|
|
|
if (dev_write_block(cp, cp_seg_blk)) {
|
2014-11-17 05:03:41 +00:00
|
|
|
MSG(1, "\tError: While writing the cp to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
goto free_cp_payload;
|
2014-01-10 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 08:11:41 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
free_cp_payload:
|
|
|
|
free(cp_payload);
|
2017-02-09 02:55:43 +00:00
|
|
|
free_nat_bits:
|
|
|
|
free(nat_bits);
|
2015-02-06 08:11:41 +00:00
|
|
|
free_sum_compact:
|
2014-11-17 05:03:41 +00:00
|
|
|
free(sum_compact);
|
2015-02-06 08:11:41 +00:00
|
|
|
free_sum:
|
2014-11-13 11:15:04 +00:00
|
|
|
free(sum);
|
2015-02-06 08:11:41 +00:00
|
|
|
free_cp:
|
2014-11-17 05:03:41 +00:00
|
|
|
free(cp);
|
2015-02-06 08:11:41 +00:00
|
|
|
return ret;
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_write_super_block(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2013-01-25 08:20:16 +00:00
|
|
|
int index;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int8_t *zero_buff;
|
|
|
|
|
2012-10-29 23:43:28 +00:00
|
|
|
zero_buff = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
|
2014-03-31 22:33:01 +00:00
|
|
|
DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
|
2012-10-26 12:56:34 +00:00
|
|
|
for (index = 0; index < 2; index++) {
|
2016-09-16 01:44:41 +00:00
|
|
|
if (dev_write_block(zero_buff, index)) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(1, "\tError: While while writing supe_blk "
|
|
|
|
"on disk!!! index : %d\n", index);
|
2015-02-06 08:11:41 +00:00
|
|
|
free(zero_buff);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(zero_buff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 19:51:33 +00:00
|
|
|
#ifndef WITH_ANDROID
|
2018-02-23 03:17:56 +00:00
|
|
|
static int f2fs_discard_obsolete_dnode(void)
|
2015-12-03 05:33:21 +00:00
|
|
|
{
|
2018-02-23 03:17:56 +00:00
|
|
|
struct f2fs_node *raw_node;
|
|
|
|
u_int64_t next_blkaddr = 0, offset;
|
2017-10-10 12:49:35 +00:00
|
|
|
u64 end_blkaddr = (get_sb(segment_count_main) <<
|
|
|
|
get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
|
2017-10-30 23:10:27 +00:00
|
|
|
u_int64_t start_inode_pos = get_sb(main_blkaddr);
|
|
|
|
u_int64_t last_inode_pos;
|
|
|
|
|
2018-02-23 03:17:56 +00:00
|
|
|
if (c.zoned_mode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
raw_node = calloc(sizeof(struct f2fs_node), 1);
|
|
|
|
if (!raw_node)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* avoid power-off-recovery based on roll-forward policy */
|
|
|
|
offset = get_sb(main_blkaddr);
|
|
|
|
offset += c.cur_seg[CURSEG_WARM_NODE] * c.blks_per_seg;
|
2017-05-26 02:17:44 +00:00
|
|
|
|
2017-10-30 23:10:27 +00:00
|
|
|
last_inode_pos = start_inode_pos +
|
2018-02-23 03:17:56 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg + c.quota_inum;
|
2017-02-07 02:43:09 +00:00
|
|
|
|
2015-12-03 05:33:21 +00:00
|
|
|
do {
|
2017-10-10 12:49:35 +00:00
|
|
|
if (offset < get_sb(main_blkaddr) || offset >= end_blkaddr)
|
2015-12-03 05:33:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (dev_read_block(raw_node, offset)) {
|
|
|
|
MSG(1, "\tError: While traversing direct node!!!\n");
|
2018-02-23 03:17:56 +00:00
|
|
|
free(raw_node);
|
2015-12-03 05:33:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:43:09 +00:00
|
|
|
next_blkaddr = le32_to_cpu(raw_node->footer.next_blkaddr);
|
2016-01-23 14:29:36 +00:00
|
|
|
memset(raw_node, 0, F2FS_BLKSIZE);
|
2015-12-03 05:33:21 +00:00
|
|
|
|
|
|
|
DBG(1, "\tDiscard dnode, at offset 0x%08"PRIx64"\n", offset);
|
|
|
|
if (dev_write_block(raw_node, offset)) {
|
|
|
|
MSG(1, "\tError: While discarding direct node!!!\n");
|
2018-02-23 03:17:56 +00:00
|
|
|
free(raw_node);
|
2015-12-03 05:33:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-02-07 02:43:09 +00:00
|
|
|
offset = next_blkaddr;
|
2017-05-26 02:17:44 +00:00
|
|
|
/* should avoid recursive chain due to stale data */
|
2017-10-30 23:10:27 +00:00
|
|
|
if (offset >= start_inode_pos || offset <= last_inode_pos)
|
2017-05-26 02:17:44 +00:00
|
|
|
break;
|
2015-12-03 05:33:21 +00:00
|
|
|
} while (1);
|
|
|
|
|
2018-02-23 03:17:56 +00:00
|
|
|
free(raw_node);
|
2015-12-03 05:33:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-13 19:51:33 +00:00
|
|
|
#endif
|
2015-12-03 05:33:21 +00:00
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_write_root_inode(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
|
|
|
struct f2fs_node *raw_node = NULL;
|
2012-11-13 07:32:44 +00:00
|
|
|
u_int64_t blk_size_bytes, data_blk_nor;
|
2012-10-26 12:56:34 +00:00
|
|
|
u_int64_t main_area_node_seg_blk_offset = 0;
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
raw_node = calloc(F2FS_BLKSIZE, 1);
|
2012-10-26 12:56:34 +00:00
|
|
|
if (raw_node == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-22 03:44:29 +00:00
|
|
|
raw_node->footer.nid = sb->root_ino;
|
|
|
|
raw_node->footer.ino = sb->root_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
raw_node->footer.cp_ver = cpu_to_le64(1);
|
|
|
|
raw_node->footer.next_blkaddr = cpu_to_le32(
|
2014-11-17 05:03:41 +00:00
|
|
|
get_sb(main_blkaddr) +
|
2016-09-17 01:41:00 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_NODE] *
|
|
|
|
c.blks_per_seg + 1);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
raw_node->i.i_mode = cpu_to_le16(0x41ed);
|
|
|
|
raw_node->i.i_links = cpu_to_le32(2);
|
|
|
|
raw_node->i.i_uid = cpu_to_le32(getuid());
|
|
|
|
raw_node->i.i_gid = cpu_to_le32(getgid());
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
blk_size_bytes = 1 << get_sb(log_blocksize);
|
2012-10-26 12:56:34 +00:00
|
|
|
raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
|
|
|
|
raw_node->i.i_blocks = cpu_to_le64(2);
|
|
|
|
|
2012-10-29 22:34:35 +00:00
|
|
|
raw_node->i.i_atime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_atime_nsec = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
raw_node->i.i_ctime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_ctime_nsec = 0;
|
|
|
|
raw_node->i.i_mtime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_mtime_nsec = 0;
|
2012-10-29 22:34:35 +00:00
|
|
|
raw_node->i.i_generation = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
raw_node->i.i_xattr_nid = 0;
|
|
|
|
raw_node->i.i_flags = 0;
|
2012-10-29 22:34:35 +00:00
|
|
|
raw_node->i.i_current_depth = cpu_to_le32(1);
|
2014-02-27 11:12:35 +00:00
|
|
|
raw_node->i.i_dir_level = DEF_DIR_LEVEL;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-07-26 14:49:57 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
|
|
|
|
raw_node->i.i_inline = F2FS_EXTRA_ATTR;
|
|
|
|
raw_node->i.i_extra_isize =
|
|
|
|
cpu_to_le16(F2FS_TOTAL_EXTRA_ATTR_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-07-26 14:54:18 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
|
|
|
|
raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
|
|
|
|
|
2018-01-25 06:58:45 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
|
|
|
|
raw_node->i.i_crtime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_crtime_nsec = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
data_blk_nor = get_sb(main_blkaddr) +
|
2016-09-17 01:41:00 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg;
|
2017-07-26 14:49:57 +00:00
|
|
|
raw_node->i.i_addr[get_extra_isize(raw_node)] = cpu_to_le32(data_blk_nor);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
raw_node->i.i_ext.fofs = 0;
|
2015-12-15 02:39:00 +00:00
|
|
|
raw_node->i.i_ext.blk_addr = 0;
|
|
|
|
raw_node->i.i_ext.len = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2017-07-26 15:01:35 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
|
|
|
|
raw_node->i.i_inode_checksum =
|
|
|
|
cpu_to_le32(f2fs_inode_chksum(raw_node));
|
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
main_area_node_seg_blk_offset = get_sb(main_blkaddr);
|
2016-09-17 01:41:00 +00:00
|
|
|
main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
|
|
|
|
c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
DBG(1, "\tWriting root inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
|
|
|
|
get_sb(main_blkaddr),
|
2016-09-17 01:41:00 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_NODE],
|
|
|
|
c.blks_per_seg, main_area_node_seg_blk_offset);
|
2016-09-16 01:44:41 +00:00
|
|
|
if (dev_write_block(raw_node, main_area_node_seg_blk_offset)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the raw_node to disk!!!\n");
|
2017-11-16 11:50:57 +00:00
|
|
|
free(raw_node);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-16 11:50:57 +00:00
|
|
|
free(raw_node);
|
2017-10-30 23:10:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-30 19:35:48 +00:00
|
|
|
static int f2fs_write_default_quota(int qtype, unsigned int blkaddr,
|
|
|
|
__le32 raw_id)
|
2017-10-30 23:10:27 +00:00
|
|
|
{
|
|
|
|
char *filebuf = calloc(F2FS_BLKSIZE, 2);
|
|
|
|
int file_magics[] = INITQMAGICS;
|
|
|
|
struct v2_disk_dqheader ddqheader;
|
|
|
|
struct v2_disk_dqinfo ddqinfo;
|
|
|
|
struct v2r1_disk_dqblk dqblk;
|
|
|
|
|
|
|
|
if (filebuf == NULL) {
|
|
|
|
MSG(1, "\tError: Calloc Failed for filebuf!!!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write basic quota header */
|
|
|
|
ddqheader.dqh_magic = cpu_to_le32(file_magics[qtype]);
|
|
|
|
/* only support QF_VFSV1 */
|
|
|
|
ddqheader.dqh_version = cpu_to_le32(1);
|
|
|
|
|
|
|
|
memcpy(filebuf, &ddqheader, sizeof(ddqheader));
|
|
|
|
|
|
|
|
/* Fill Initial quota file content */
|
|
|
|
ddqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
|
|
|
|
ddqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
|
|
|
|
ddqinfo.dqi_flags = cpu_to_le32(0);
|
|
|
|
ddqinfo.dqi_blocks = cpu_to_le32(QT_TREEOFF + 5);
|
|
|
|
ddqinfo.dqi_free_blk = cpu_to_le32(0);
|
|
|
|
ddqinfo.dqi_free_entry = cpu_to_le32(5);
|
|
|
|
|
|
|
|
memcpy(filebuf + V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo));
|
|
|
|
|
|
|
|
filebuf[1024] = 2;
|
|
|
|
filebuf[2048] = 3;
|
|
|
|
filebuf[3072] = 4;
|
|
|
|
filebuf[4096] = 5;
|
|
|
|
|
|
|
|
filebuf[5120 + 8] = 1;
|
|
|
|
|
2017-11-30 19:35:48 +00:00
|
|
|
dqblk.dqb_id = raw_id;
|
2017-10-30 23:10:27 +00:00
|
|
|
dqblk.dqb_pad = cpu_to_le32(0);
|
|
|
|
dqblk.dqb_ihardlimit = cpu_to_le64(0);
|
|
|
|
dqblk.dqb_isoftlimit = cpu_to_le64(0);
|
|
|
|
dqblk.dqb_curinodes = cpu_to_le64(1);
|
|
|
|
dqblk.dqb_bhardlimit = cpu_to_le64(0);
|
|
|
|
dqblk.dqb_bsoftlimit = cpu_to_le64(0);
|
|
|
|
dqblk.dqb_curspace = cpu_to_le64(4096);
|
|
|
|
dqblk.dqb_btime = cpu_to_le64(0);
|
|
|
|
dqblk.dqb_itime = cpu_to_le64(0);
|
|
|
|
|
|
|
|
memcpy(filebuf + 5136, &dqblk, sizeof(struct v2r1_disk_dqblk));
|
|
|
|
|
|
|
|
/* Write two blocks */
|
|
|
|
if (dev_write_block(filebuf, blkaddr) ||
|
|
|
|
dev_write_block(filebuf + F2FS_BLKSIZE, blkaddr + 1)) {
|
|
|
|
MSG(1, "\tError: While writing the quota_blk to disk!!!\n");
|
|
|
|
free(filebuf);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-30 18:40:12 +00:00
|
|
|
DBG(1, "\tWriting quota data, at offset %08x, %08x\n",
|
|
|
|
blkaddr, blkaddr + 1);
|
2017-10-30 23:10:27 +00:00
|
|
|
free(filebuf);
|
2018-02-23 03:17:56 +00:00
|
|
|
c.quota_dnum += QUOTA_DATA(qtype);
|
2017-10-30 23:10:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int f2fs_write_qf_inode(int qtype)
|
|
|
|
{
|
|
|
|
struct f2fs_node *raw_node = NULL;
|
|
|
|
u_int64_t data_blk_nor;
|
|
|
|
u_int64_t main_area_node_seg_blk_offset = 0;
|
2017-11-30 19:35:48 +00:00
|
|
|
__le32 raw_id;
|
2017-10-30 23:10:27 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
raw_node = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
if (raw_node == NULL) {
|
|
|
|
MSG(1, "\tError: Calloc Failed for raw_node!!!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_node->footer.nid = sb->qf_ino[qtype];
|
|
|
|
raw_node->footer.ino = sb->qf_ino[qtype];
|
|
|
|
raw_node->footer.cp_ver = cpu_to_le64(1);
|
|
|
|
raw_node->footer.next_blkaddr = cpu_to_le32(
|
|
|
|
get_sb(main_blkaddr) +
|
|
|
|
c.cur_seg[CURSEG_HOT_NODE] *
|
|
|
|
c.blks_per_seg + 1 + qtype + 1);
|
|
|
|
|
|
|
|
raw_node->i.i_mode = cpu_to_le16(0x8180);
|
|
|
|
raw_node->i.i_links = cpu_to_le32(1);
|
|
|
|
raw_node->i.i_uid = cpu_to_le32(getuid());
|
|
|
|
raw_node->i.i_gid = cpu_to_le32(getgid());
|
|
|
|
|
|
|
|
raw_node->i.i_size = cpu_to_le64(1024 * 6); /* Hard coded */
|
|
|
|
raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
|
|
|
|
|
|
|
|
raw_node->i.i_atime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_atime_nsec = 0;
|
|
|
|
raw_node->i.i_ctime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_ctime_nsec = 0;
|
|
|
|
raw_node->i.i_mtime = cpu_to_le32(time(NULL));
|
|
|
|
raw_node->i.i_mtime_nsec = 0;
|
|
|
|
raw_node->i.i_generation = 0;
|
|
|
|
raw_node->i.i_xattr_nid = 0;
|
|
|
|
raw_node->i.i_flags = FS_IMMUTABLE_FL;
|
|
|
|
raw_node->i.i_current_depth = cpu_to_le32(1);
|
|
|
|
raw_node->i.i_dir_level = DEF_DIR_LEVEL;
|
|
|
|
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
|
|
|
|
raw_node->i.i_inline = F2FS_EXTRA_ATTR;
|
|
|
|
raw_node->i.i_extra_isize =
|
|
|
|
cpu_to_le16(F2FS_TOTAL_EXTRA_ATTR_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
|
|
|
|
raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
|
|
|
|
|
|
|
|
data_blk_nor = get_sb(main_blkaddr) +
|
|
|
|
c.cur_seg[CURSEG_HOT_DATA] * c.blks_per_seg + 1;
|
|
|
|
|
|
|
|
for (i = 0; i < qtype; i++)
|
|
|
|
if (sb->qf_ino[i])
|
|
|
|
data_blk_nor += QUOTA_DATA(i);
|
2017-11-30 19:35:48 +00:00
|
|
|
if (qtype == 0)
|
|
|
|
raw_id = raw_node->i.i_uid;
|
|
|
|
else if (qtype == 1)
|
|
|
|
raw_id = raw_node->i.i_gid;
|
|
|
|
else if (qtype == 2)
|
|
|
|
raw_id = raw_node->i.i_projid;
|
|
|
|
else
|
|
|
|
ASSERT(0);
|
2017-10-30 23:10:27 +00:00
|
|
|
|
|
|
|
/* write two blocks */
|
2017-11-30 19:35:48 +00:00
|
|
|
if (f2fs_write_default_quota(qtype, data_blk_nor, raw_id)) {
|
2017-10-30 23:10:27 +00:00
|
|
|
free(raw_node);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < QUOTA_DATA(qtype); i++)
|
|
|
|
raw_node->i.i_addr[get_extra_isize(raw_node) + i] =
|
|
|
|
cpu_to_le32(data_blk_nor + i);
|
|
|
|
raw_node->i.i_ext.fofs = 0;
|
|
|
|
raw_node->i.i_ext.blk_addr = 0;
|
|
|
|
raw_node->i.i_ext.len = 0;
|
|
|
|
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
|
|
|
|
raw_node->i.i_inode_checksum =
|
|
|
|
cpu_to_le32(f2fs_inode_chksum(raw_node));
|
|
|
|
|
|
|
|
main_area_node_seg_blk_offset = get_sb(main_blkaddr);
|
|
|
|
main_area_node_seg_blk_offset += c.cur_seg[CURSEG_HOT_NODE] *
|
|
|
|
c.blks_per_seg + qtype + 1;
|
|
|
|
|
|
|
|
DBG(1, "\tWriting quota inode (hot node), %x %x %x at offset 0x%08"PRIu64"\n",
|
|
|
|
get_sb(main_blkaddr),
|
|
|
|
c.cur_seg[CURSEG_HOT_NODE],
|
|
|
|
c.blks_per_seg, main_area_node_seg_blk_offset);
|
|
|
|
if (dev_write_block(raw_node, main_area_node_seg_blk_offset)) {
|
|
|
|
MSG(1, "\tError: While writing the raw_node to disk!!!\n");
|
|
|
|
free(raw_node);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-03 05:33:21 +00:00
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
free(raw_node);
|
2018-02-23 03:17:56 +00:00
|
|
|
c.quota_inum++;
|
2012-10-26 12:56:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_update_nat_root(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
|
|
|
struct f2fs_nat_block *nat_blk = NULL;
|
2016-09-16 01:44:41 +00:00
|
|
|
u_int64_t nat_seg_blk_offset = 0;
|
2017-10-30 23:10:27 +00:00
|
|
|
enum quota_type qtype;
|
|
|
|
int i;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
nat_blk = calloc(F2FS_BLKSIZE, 1);
|
2012-10-26 12:56:34 +00:00
|
|
|
if(nat_blk == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for nat_blk!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:10:27 +00:00
|
|
|
/* update quota */
|
|
|
|
for (qtype = i = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (sb->qf_ino[qtype] == 0)
|
|
|
|
continue;
|
|
|
|
nat_blk->entries[sb->qf_ino[qtype]].block_addr =
|
|
|
|
cpu_to_le32(get_sb(main_blkaddr) +
|
|
|
|
c.cur_seg[CURSEG_HOT_NODE] *
|
|
|
|
c.blks_per_seg + i + 1);
|
|
|
|
nat_blk->entries[sb->qf_ino[qtype]].ino = sb->qf_ino[qtype];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
/* update root */
|
2014-11-17 05:03:41 +00:00
|
|
|
nat_blk->entries[get_sb(root_ino)].block_addr = cpu_to_le32(
|
|
|
|
get_sb(main_blkaddr) +
|
2016-09-17 01:41:00 +00:00
|
|
|
c.cur_seg[CURSEG_HOT_NODE] * c.blks_per_seg);
|
2015-11-22 03:44:29 +00:00
|
|
|
nat_blk->entries[get_sb(root_ino)].ino = sb->root_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
/* update node nat */
|
2014-11-17 05:03:41 +00:00
|
|
|
nat_blk->entries[get_sb(node_ino)].block_addr = cpu_to_le32(1);
|
2015-11-22 03:44:29 +00:00
|
|
|
nat_blk->entries[get_sb(node_ino)].ino = sb->node_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
/* update meta nat */
|
2014-11-17 05:03:41 +00:00
|
|
|
nat_blk->entries[get_sb(meta_ino)].block_addr = cpu_to_le32(1);
|
2015-11-22 03:44:29 +00:00
|
|
|
nat_blk->entries[get_sb(meta_ino)].ino = sb->meta_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
nat_seg_blk_offset = get_sb(nat_blkaddr);
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
DBG(1, "\tWriting nat root, at offset 0x%08"PRIx64"\n",
|
|
|
|
nat_seg_blk_offset);
|
|
|
|
if (dev_write_block(nat_blk, nat_seg_blk_offset)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the nat_blk set0 to disk!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
free(nat_blk);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(nat_blk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_add_default_dentry_root(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
|
|
|
struct f2fs_dentry_block *dent_blk = NULL;
|
2016-09-16 01:44:41 +00:00
|
|
|
u_int64_t data_blk_offset = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
dent_blk = calloc(F2FS_BLKSIZE, 1);
|
2012-10-26 12:56:34 +00:00
|
|
|
if(dent_blk == NULL) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Calloc Failed for dent_blk!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dent_blk->dentry[0].hash_code = 0;
|
2015-11-22 03:44:29 +00:00
|
|
|
dent_blk->dentry[0].ino = sb->root_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
dent_blk->dentry[0].name_len = cpu_to_le16(1);
|
|
|
|
dent_blk->dentry[0].file_type = F2FS_FT_DIR;
|
|
|
|
memcpy(dent_blk->filename[0], ".", 1);
|
|
|
|
|
|
|
|
dent_blk->dentry[1].hash_code = 0;
|
2015-11-22 03:44:29 +00:00
|
|
|
dent_blk->dentry[1].ino = sb->root_ino;
|
2012-10-26 12:56:34 +00:00
|
|
|
dent_blk->dentry[1].name_len = cpu_to_le16(2);
|
|
|
|
dent_blk->dentry[1].file_type = F2FS_FT_DIR;
|
|
|
|
memcpy(dent_blk->filename[1], "..", 2);
|
|
|
|
|
|
|
|
/* bitmap for . and .. */
|
2015-12-15 18:10:15 +00:00
|
|
|
test_and_set_bit_le(0, dent_blk->dentry_bitmap);
|
|
|
|
test_and_set_bit_le(1, dent_blk->dentry_bitmap);
|
2017-10-30 23:10:27 +00:00
|
|
|
|
2014-11-17 05:03:41 +00:00
|
|
|
data_blk_offset = get_sb(main_blkaddr);
|
2016-09-17 01:41:00 +00:00
|
|
|
data_blk_offset += c.cur_seg[CURSEG_HOT_DATA] *
|
|
|
|
c.blks_per_seg;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-16 01:44:41 +00:00
|
|
|
DBG(1, "\tWriting default dentry root, at offset 0x%08"PRIx64"\n",
|
|
|
|
data_blk_offset);
|
|
|
|
if (dev_write_block(dent_blk, data_blk_offset)) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: While writing the dentry_blk to disk!!!\n");
|
2015-02-06 08:11:41 +00:00
|
|
|
free(dent_blk);
|
2012-10-26 12:56:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dent_blk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 08:20:16 +00:00
|
|
|
static int f2fs_create_root_dir(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2017-10-30 23:10:27 +00:00
|
|
|
enum quota_type qtype;
|
2013-01-25 08:20:16 +00:00
|
|
|
int err = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
err = f2fs_write_root_inode();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Failed to write root inode!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:10:27 +00:00
|
|
|
for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
|
|
|
|
if (sb->qf_ino[qtype] == 0)
|
|
|
|
continue;
|
|
|
|
err = f2fs_write_qf_inode(qtype);
|
|
|
|
if (err < 0) {
|
|
|
|
MSG(1, "\tError: Failed to write quota inode!!!\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 03:17:56 +00:00
|
|
|
#ifndef WITH_ANDROID
|
|
|
|
err = f2fs_discard_obsolete_dnode();
|
|
|
|
if (err < 0) {
|
|
|
|
MSG(1, "\tError: Failed to discard obsolete dnode!!!\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-26 12:56:34 +00:00
|
|
|
err = f2fs_update_nat_root();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Failed to update NAT for root!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_add_default_dentry_root();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Failed to add default dentries for root!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
if (err)
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(1, "\tError: Could not create the root directory!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
int f2fs_format_device(void)
|
2012-10-26 12:56:34 +00:00
|
|
|
{
|
2013-01-25 08:20:16 +00:00
|
|
|
int err = 0;
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
err= f2fs_prepare_super_block();
|
2013-01-25 08:20:16 +00:00
|
|
|
if (err < 0) {
|
|
|
|
MSG(0, "\tError: Failed to prepare a super block!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
2013-01-25 08:20:16 +00:00
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.trim) {
|
2016-11-02 00:23:40 +00:00
|
|
|
err = f2fs_trim_devices();
|
2016-09-02 17:33:44 +00:00
|
|
|
if (err < 0) {
|
|
|
|
MSG(0, "\tError: Failed to trim whole device!!!\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-10-26 12:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_init_sit_area();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Failed to Initialise the SIT AREA!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_init_nat_area();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Failed to Initialise the NAT AREA!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_create_root_dir();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Failed to create the root directory!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_write_check_point_pack();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Failed to write the check point pack!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f2fs_write_super_block();
|
|
|
|
if (err < 0) {
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Failed to write the Super Block!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
if (err)
|
2013-01-25 08:20:16 +00:00
|
|
|
MSG(0, "\tError: Could not format the device!!!\n");
|
2012-10-26 12:56:34 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|