HelenOS sources
This source file includes following definitions.
- ext4_filesystem_init
- ext4_filesystem_fini
- ext4_filesystem_create_lost_found
- ext4_filesystem_create_root_dir
- ext4_filesystem_create
- ext4_filesystem_probe
- ext4_filesystem_open
- ext4_filesystem_close
- ext4_filesystem_check_features
- ext4_filesystem_blockaddr2_index_in_group
- ext4_filesystem_index_in_group2blockaddr
- ext4_filesystem_blockaddr2group
- ext4_filesystem_init_block_groups
- ext4_filesystem_init_block_bitmap
- ext4_filesystem_init_inode_bitmap
- ext4_filesystem_init_inode_table
- ext4_filesystem_get_block_group_ref
- ext4_filesystem_bg_checksum
- ext4_filesystem_bg_get_itable_size
- ext4_filesystem_bg_get_backup_blocks
- ext4_filesystem_put_block_group_ref
- ext4_filesystem_get_inode_ref
- ext4_filesystem_put_inode_ref
- ext4_filesystem_init_inode
- ext4_filesystem_alloc_inode
- ext4_filesystem_alloc_this_inode
- ext4_filesystem_free_inode
- ext4_filesystem_truncate_inode
- ext4_filesystem_get_inode_data_block_index
- ext4_filesystem_set_inode_data_block_index
- ext4_filesystem_release_inode_block
- ext4_filesystem_append_inode_block
- ext4_filesystem_inodes_per_block
#include <byteorder.h>
#include <errno.h>
#include <mem.h>
#include <align.h>
#include <crypto.h>
#include <ipc/vfs.h>
#include <libfs.h>
#include <stdlib.h>
#include "ext4/balloc.h"
#include "ext4/bitmap.h"
#include "ext4/block_group.h"
#include "ext4/cfg.h"
#include "ext4/directory.h"
#include "ext4/extent.h"
#include "ext4/filesystem.h"
#include "ext4/ialloc.h"
#include "ext4/inode.h"
#include "ext4/ops.h"
#include "ext4/superblock.h"
static errno_t ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *);
static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *,
uint32_t, ext4_inode_ref_t **, int);
static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *);
static errno_t ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
enum cache_mode cmode)
{
errno_t rc;
ext4_superblock_t *temp_superblock = NULL;
fs->device = service_id;
rc = block_init(fs->device);
if (rc != EOK)
goto err;
rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
if (rc != EOK)
goto err_1;
uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
if (block_size > EXT4_MAX_BLOCK_SIZE) {
rc = ENOTSUP;
goto err_1;
}
rc = block_cache_init(service_id, block_size, 0, cmode);
if (rc != EOK)
goto err_1;
uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
fs->inode_blocks_per_level[0] = 1;
for (unsigned int i = 1; i < 4; i++) {
fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
block_ids_per_block;
fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
fs->inode_blocks_per_level[i];
}
fs->superblock = temp_superblock;
uint16_t state = ext4_superblock_get_state(fs->superblock);
if (((state & EXT4_SUPERBLOCK_STATE_VALID_FS) !=
EXT4_SUPERBLOCK_STATE_VALID_FS) ||
((state & EXT4_SUPERBLOCK_STATE_ERROR_FS) ==
EXT4_SUPERBLOCK_STATE_ERROR_FS)) {
rc = ENOTSUP;
goto err_2;
}
rc = ext4_superblock_check_sanity(fs->superblock);
if (rc != EOK)
goto err_2;
bool read_only;
rc = ext4_filesystem_check_features(fs, &read_only);
if (rc != EOK)
goto err_2;
return EOK;
err_2:
block_cache_fini(fs->device);
err_1:
block_fini(fs->device);
err:
if (temp_superblock)
ext4_superblock_release(temp_superblock);
return rc;
}
static void ext4_filesystem_fini(ext4_filesystem_t *fs)
{
free(fs->superblock);
block_cache_fini(fs->device);
block_fini(fs->device);
}
static errno_t ext4_filesystem_create_lost_found(ext4_filesystem_t *fs,
ext4_inode_ref_t *root_dir_ref)
{
errno_t rc;
ext4_inode_ref_t *inode_ref;
rc = ext4_filesystem_alloc_inode(fs, &inode_ref, L_DIRECTORY);
if (rc != EOK)
goto error;
rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
if (rc != EOK)
goto error;
rc = ext4_directory_add_entry(inode_ref, "..", root_dir_ref);
if (rc != EOK)
goto error;
rc = ext4_directory_add_entry(root_dir_ref, "lost+found", inode_ref);
if (rc != EOK)
goto error;
inode_ref->dirty = true;
uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
rc = ext4_filesystem_put_inode_ref(inode_ref);
if (rc != EOK)
goto error;
error:
return rc;
}
static errno_t ext4_filesystem_create_root_dir(ext4_filesystem_t *fs)
{
errno_t rc;
ext4_inode_ref_t *inode_ref;
rc = ext4_filesystem_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX,
&inode_ref);
if (rc != EOK)
goto error;
inode_ref->dirty = true;
rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
if (rc != EOK)
goto error;
rc = ext4_directory_add_entry(inode_ref, "..", inode_ref);
if (rc != EOK)
goto error;
uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
rc = ext4_filesystem_create_lost_found(fs, inode_ref);
if (rc != EOK)
goto error;
nlinks = ext4_inode_get_links_count(inode_ref->inode);
ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
rc = ext4_filesystem_put_inode_ref(inode_ref);
if (rc != EOK)
goto error;
error:
return rc;
}
errno_t ext4_filesystem_create(ext4_cfg_t *cfg, service_id_t service_id)
{
errno_t rc;
ext4_superblock_t *superblock = NULL;
ext4_filesystem_t *fs = NULL;
size_t dev_bsize;
aoff64_t dev_nblocks;
ext4_inode_ref_t *inode_ref = NULL;
bool block_inited = false;
bool fs_inited = false;
uint32_t idx;
rc = block_init(service_id);
if (rc != EOK)
goto err;
block_inited = true;
rc = block_get_bsize(service_id, &dev_bsize);
if (rc != EOK)
goto err;
rc = block_get_nblocks(service_id, &dev_nblocks);
if (rc != EOK)
goto err;
rc = ext4_superblock_create(dev_bsize, dev_nblocks, cfg, &superblock);
if (rc != EOK)
goto err;
rc = ext4_superblock_write_direct(service_id, superblock);
if (rc != EOK)
goto err;
block_fini(service_id);
block_inited = false;
ext4_superblock_release(superblock);
superblock = NULL;
fs = calloc(1, sizeof(ext4_filesystem_t));
if (fs == NULL)
goto err;
rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
if (rc != EOK)
goto err;
fs_inited = true;
rc = ext4_filesystem_init_block_groups(fs);
if (rc != EOK)
goto err;
for (idx = 1; idx < EXT4_REV0_FIRST_INO; idx++) {
if (idx == EXT4_INODE_ROOT_INDEX) {
rc = ext4_filesystem_alloc_this_inode(fs, idx,
&inode_ref, L_DIRECTORY);
if (rc != EOK)
goto error;
rc = ext4_filesystem_put_inode_ref(inode_ref);
if (rc != EOK)
goto error;
} else {
errno_t rc = ext4_ialloc_alloc_this_inode(fs, idx,
false);
if (rc != EOK)
return rc;
rc = ext4_filesystem_get_inode_ref(fs, idx,
&inode_ref);
if (rc != EOK)
goto error;
memset(inode_ref->inode, 0, ext4_superblock_get_inode_size(fs->superblock));
inode_ref->dirty = true;
rc = ext4_filesystem_put_inode_ref(inode_ref);
if (rc != EOK)
goto error;
}
}
rc = ext4_filesystem_create_root_dir(fs);
if (rc != EOK)
goto err;
rc = ext4_superblock_write_direct(service_id, fs->superblock);
if (rc != EOK)
goto err;
ext4_filesystem_fini(fs);
free(fs);
return EOK;
err:
if (fs_inited)
ext4_filesystem_fini(fs);
if (fs != NULL)
free(fs);
if (superblock != NULL)
ext4_superblock_release(superblock);
if (block_inited)
block_fini(service_id);
return rc;
error:
return rc;
}
errno_t ext4_filesystem_probe(service_id_t service_id,
ext4_fs_probe_info_t *info)
{
ext4_filesystem_t *fs = NULL;
errno_t rc;
fs = calloc(1, sizeof(ext4_filesystem_t));
if (fs == NULL)
return ENOMEM;
rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
if (rc != EOK) {
free(fs);
return rc;
}
rc = ext4_superblock_get_volume_name(fs->superblock, info->vol_name,
sizeof(info->vol_name));
if (rc != EOK) {
ext4_filesystem_fini(fs);
return rc;
}
ext4_filesystem_fini(fs);
return EOK;
}
errno_t ext4_filesystem_open(ext4_instance_t *inst, service_id_t service_id,
enum cache_mode cmode, aoff64_t *size, ext4_filesystem_t **rfs)
{
int fs_inited = 0;
ext4_filesystem_t *fs = NULL;
fs_node_t *root_node = NULL;
errno_t rc;
fs = calloc(1, sizeof(ext4_filesystem_t));
if (fs == NULL) {
rc = ENOMEM;
goto error;
}
inst->filesystem = fs;
rc = ext4_filesystem_init(fs, service_id, cmode);
if (rc != EOK)
goto error;
fs_inited = 1;
rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
if (rc != EOK)
goto error;
ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
rc = ext4_superblock_write_direct(fs->device, fs->superblock);
if (rc != EOK)
goto error;
uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
ext4_node_t *enode = EXT4_NODE(root_node);
*size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
ext4_node_put(root_node);
*rfs = fs;
return EOK;
error:
if (root_node != NULL)
ext4_node_put(root_node);
if (fs_inited)
ext4_filesystem_fini(fs);
free(fs);
return rc;
}
errno_t ext4_filesystem_close(ext4_filesystem_t *fs)
{
ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
errno_t rc = ext4_superblock_write_direct(fs->device, fs->superblock);
if (rc != EOK)
return rc;
ext4_filesystem_fini(fs);
return EOK;
}
static errno_t ext4_filesystem_check_features(ext4_filesystem_t *fs,
bool *read_only)
{
if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
*read_only = false;
return EOK;
}
uint32_t incompatible_features;
incompatible_features =
ext4_superblock_get_features_incompatible(fs->superblock);
incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
if (incompatible_features > 0)
return ENOTSUP;
uint32_t compatible_read_only;
compatible_read_only =
ext4_superblock_get_features_read_only(fs->superblock);
compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
if (compatible_read_only > 0) {
*read_only = true;
return EOK;
}
return EOK;
}
uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
uint32_t block_addr)
{
uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
uint32_t first_block = ext4_superblock_get_first_data_block(sb);
if (first_block == 0)
return block_addr % blocks_per_group;
else
return (block_addr - 1) % blocks_per_group;
}
uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
uint32_t index, uint32_t bgid)
{
uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
if (ext4_superblock_get_first_data_block(sb) == 0)
return bgid * blocks_per_group + index;
else
return bgid * blocks_per_group + index + 1;
}
uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
{
uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
uint32_t first_block = ext4_superblock_get_first_data_block(sb);
return (b - first_block) / blocks_per_group;
}
static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *fs)
{
errno_t rc;
block_t *block;
aoff64_t b;
ext4_block_group_t *bg;
ext4_superblock_t *sb = fs->superblock;
ext4_block_group_ref_t *bg_ref;
uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
uint32_t block_size = ext4_superblock_get_block_size(sb);
uint32_t desc_size = ext4_superblock_get_desc_size(fs->superblock);
uint32_t descriptors_per_block =
ext4_superblock_get_block_size(fs->superblock) / desc_size;
aoff64_t block_id =
ext4_superblock_get_first_data_block(fs->superblock) + 1;
uint32_t dtable_blocks =
(block_group_count + descriptors_per_block - 1) /
descriptors_per_block;
uint32_t bg_index;
aoff64_t bg_block0;
uint32_t dcnt;
uint32_t i;
uint32_t now;
aoff64_t block_bitmap;
aoff64_t inode_bitmap;
aoff64_t inode_table;
uint32_t free_blocks;
uint32_t free_inodes;
uint32_t used_dirs;
uint32_t reserved;
uint32_t inode_table_blocks;
dcnt = block_group_count;
b = block_id;
bg_index = 0;
bg_block0 = block_id;
while (dcnt > 0) {
rc = block_get(&block, fs->device, b, BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
if (dcnt > descriptors_per_block)
now = descriptors_per_block;
else
now = dcnt;
memset(block->data, 0, block_size);
for (i = 0; i < now; i++) {
bg = (ext4_block_group_t *) (block->data + i *
desc_size);
block_bitmap = bg_block0 + dtable_blocks;
inode_bitmap = block_bitmap + 1;
inode_table = inode_bitmap + 1;
free_blocks = ext4_superblock_get_blocks_in_group(sb,
bg_index);
free_inodes =
ext4_filesystem_bg_get_itable_size(sb, bg_index) *
ext4_filesystem_inodes_per_block(sb);
used_dirs = 0;
ext4_block_group_set_block_bitmap(bg, sb, block_bitmap);
ext4_block_group_set_inode_bitmap(bg, sb, inode_bitmap);
ext4_block_group_set_inode_table_first_block(bg, sb,
inode_table);
ext4_block_group_set_free_blocks_count(bg, sb,
free_blocks);
ext4_block_group_set_free_inodes_count(bg, sb,
free_inodes);
ext4_block_group_set_used_dirs_count(bg, sb,
used_dirs);
ext4_block_group_set_flag(bg,
EXT4_BLOCK_GROUP_BLOCK_UNINIT);
ext4_block_group_set_flag(bg,
EXT4_BLOCK_GROUP_INODE_UNINIT);
bg_index++;
bg_block0 += ext4_superblock_get_blocks_per_group(sb);
}
block->dirty = true;
rc = block_put(block);
if (rc != EOK)
return rc;
++b;
dcnt -= now;
}
for (bg_index = 0; bg_index < block_group_count; bg_index++) {
rc = ext4_filesystem_get_block_group_ref(fs, bg_index, &bg_ref);
if (rc != EOK)
return rc;
free_blocks = ext4_superblock_get_blocks_in_group(sb, bg_index);
reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
inode_table_blocks = ext4_filesystem_bg_get_itable_size(sb,
bg_ref->index);
free_blocks = free_blocks - reserved - 2 - inode_table_blocks;
ext4_block_group_set_free_blocks_count(bg_ref->block_group,
sb, free_blocks);
bg_ref->dirty = true;
rc = ext4_filesystem_put_block_group_ref(bg_ref);
if (rc != EOK)
return rc;
}
return EOK;
}
static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
{
uint64_t itb;
uint32_t sz;
uint32_t i;
ext4_superblock_t *sb = bg_ref->fs->superblock;
uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
bg_ref->block_group, bg_ref->fs->superblock);
uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
bg_ref->block_group, bg_ref->fs->superblock);
uint32_t blocks_group = ext4_superblock_get_blocks_per_group(sb);
uint32_t bg_blocks = ext4_superblock_get_blocks_in_group(sb,
bg_ref->index);
block_t *bitmap_block;
errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
bitmap_block_addr, BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
uint8_t *bitmap = bitmap_block->data;
uint32_t block_size = ext4_superblock_get_block_size(sb);
memset(bitmap, 0, block_size);
uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
for (uint32_t block = 0; block < reserved_cnt; ++block)
ext4_bitmap_set_bit(bitmap, block);
uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
bitmap_block_addr);
if (bitmap_block_gid == bg_ref->index) {
ext4_bitmap_set_bit(bitmap,
ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
}
uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
bitmap_inode_addr);
if (bitmap_inode_gid == bg_ref->index) {
ext4_bitmap_set_bit(bitmap,
ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
}
itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
sb);
sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
for (i = 0; i < sz; ++i, ++itb) {
uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
if (gid == bg_ref->index) {
ext4_bitmap_set_bit(bitmap,
ext4_filesystem_blockaddr2_index_in_group(sb, itb));
}
}
for (uint32_t block = bg_blocks; block < blocks_group; block++) {
ext4_bitmap_set_bit(bitmap, block);
}
bitmap_block->dirty = true;
return block_put(bitmap_block);
}
static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
{
uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
bg_ref->block_group, bg_ref->fs->superblock);
block_t *bitmap_block;
errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
bitmap_block_addr, BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
uint8_t *bitmap = bitmap_block->data;
uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
uint32_t inodes_per_group =
ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
memset(bitmap, 0, (inodes_per_group + 7) / 8);
uint32_t start_bit = inodes_per_group;
uint32_t end_bit = block_size * 8;
uint32_t i;
for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
ext4_bitmap_set_bit(bitmap, i);
if (i < end_bit)
memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
bitmap_block->dirty = true;
return block_put(bitmap_block);
}
static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
{
ext4_superblock_t *sb = bg_ref->fs->superblock;
uint32_t block_size = ext4_superblock_get_block_size(sb);
uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
uint32_t inodes_in_group =
ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
uint32_t table_blocks = inodes_in_group / inodes_per_block;
if (inodes_in_group % inodes_per_block)
table_blocks++;
uint32_t first_block = ext4_block_group_get_inode_table_first_block(
bg_ref->block_group, sb);
uint32_t last_block = first_block + table_blocks - 1;
for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
block_t *block;
errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
BLOCK_FLAGS_NOREAD);
if (rc != EOK)
return rc;
memset(block->data, 0, block_size);
block->dirty = true;
rc = block_put(block);
if (rc != EOK)
return rc;
}
return EOK;
}
errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
ext4_block_group_ref_t **ref)
{
ext4_block_group_ref_t *newref =
malloc(sizeof(ext4_block_group_ref_t));
if (newref == NULL)
return ENOMEM;
uint32_t descriptors_per_block =
ext4_superblock_get_block_size(fs->superblock) /
ext4_superblock_get_desc_size(fs->superblock);
aoff64_t block_id =
ext4_superblock_get_first_data_block(fs->superblock) + 1;
block_id += bgid / descriptors_per_block;
uint32_t offset = (bgid % descriptors_per_block) *
ext4_superblock_get_desc_size(fs->superblock);
errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
if (rc != EOK) {
free(newref);
return rc;
}
newref->block_group = newref->block->data + offset;
newref->fs = fs;
newref->index = bgid;
newref->dirty = false;
*ref = newref;
if (ext4_block_group_has_flag(newref->block_group,
EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
rc = ext4_filesystem_init_block_bitmap(newref);
if (rc != EOK) {
block_put(newref->block);
free(newref);
return rc;
}
ext4_block_group_clear_flag(newref->block_group,
EXT4_BLOCK_GROUP_BLOCK_UNINIT);
newref->dirty = true;
}
if (ext4_block_group_has_flag(newref->block_group,
EXT4_BLOCK_GROUP_INODE_UNINIT)) {
rc = ext4_filesystem_init_inode_bitmap(newref);
if (rc != EOK) {
block_put(newref->block);
free(newref);
return rc;
}
ext4_block_group_clear_flag(newref->block_group,
EXT4_BLOCK_GROUP_INODE_UNINIT);
if (!ext4_block_group_has_flag(newref->block_group,
EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
rc = ext4_filesystem_init_inode_table(newref);
if (rc != EOK) {
block_put(newref->block);
free(newref);
return rc;
}
ext4_block_group_set_flag(newref->block_group,
EXT4_BLOCK_GROUP_ITABLE_ZEROED);
}
newref->dirty = true;
}
return EOK;
}
static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
ext4_block_group_t *bg)
{
uint16_t crc = 0;
if (ext4_superblock_has_feature_read_only(sb,
EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
void *base = bg;
void *checksum = &bg->checksum;
uint32_t offset = (uint32_t) (checksum - base);
uint32_t le_group = host2uint32_t_le(bgid);
crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
crc = crc16_ibm(crc, (uint8_t *) bg, offset);
offset += sizeof(bg->checksum);
if ((ext4_superblock_has_feature_incompatible(sb,
EXT4_FEATURE_INCOMPAT_64BIT)) &&
(offset < ext4_superblock_get_desc_size(sb)))
crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
ext4_superblock_get_desc_size(sb) - offset);
}
return crc;
}
uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
uint32_t bg_index)
{
uint32_t itable_size;
uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
uint32_t block_size = ext4_superblock_get_block_size(sb);
if (bg_index < block_group_count - 1) {
itable_size = inodes_per_group * inode_table_item_size;
} else {
uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
itable_size =
(inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
inode_table_item_size;
}
return ROUND_UP(itable_size, block_size) / block_size;
}
uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
{
return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
bg->index);
}
errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
{
if (ref->dirty) {
uint16_t checksum =
ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
ref->block_group);
ext4_block_group_set_checksum(ref->block_group, checksum);
ref->block->dirty = true;
}
errno_t rc = block_put(ref->block);
free(ref);
return rc;
}
errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
ext4_inode_ref_t **ref)
{
ext4_inode_ref_t *newref =
malloc(sizeof(ext4_inode_ref_t));
if (newref == NULL)
return ENOMEM;
uint32_t inodes_per_group =
ext4_superblock_get_inodes_per_group(fs->superblock);
index -= 1;
uint32_t block_group = index / inodes_per_group;
uint32_t offset_in_group = index % inodes_per_group;
ext4_block_group_ref_t *bg_ref;
errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
if (rc != EOK) {
free(newref);
return rc;
}
uint32_t inode_table_start =
ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
fs->superblock);
rc = ext4_filesystem_put_block_group_ref(bg_ref);
if (rc != EOK) {
free(newref);
return rc;
}
uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
uint32_t byte_offset_in_group = offset_in_group * inode_size;
aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
rc = block_get(&newref->block, fs->device, block_id, 0);
if (rc != EOK) {
free(newref);
return rc;
}
uint32_t offset_in_block = byte_offset_in_group % block_size;
newref->inode = newref->block->data + offset_in_block;
newref->index = index + 1;
newref->fs = fs;
newref->dirty = false;
*ref = newref;
return EOK;
}
errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
{
if (ref->dirty) {
ref->block->dirty = true;
}
errno_t rc = block_put(ref->block);
free(ref);
return rc;
}
static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
ext4_inode_ref_t **inode_ref, int flags)
{
bool is_dir = false;
if (flags & L_DIRECTORY)
is_dir = true;
errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
if (rc != EOK) {
ext4_ialloc_free_inode(fs, index, is_dir);
return rc;
}
ext4_inode_t *inode = (*inode_ref)->inode;
uint16_t mode;
if (is_dir) {
mode = 0777;
mode |= EXT4_INODE_MODE_DIRECTORY;
ext4_inode_set_mode(fs->superblock, inode, mode);
ext4_inode_set_links_count(inode, 1);
} else {
mode = 0666;
mode |= EXT4_INODE_MODE_FILE;
ext4_inode_set_mode(fs->superblock, inode, mode);
ext4_inode_set_links_count(inode, 0);
}
ext4_inode_set_uid(inode, 0);
ext4_inode_set_gid(inode, 0);
ext4_inode_set_size(inode, 0);
ext4_inode_set_access_time(inode, 0);
ext4_inode_set_change_inode_time(inode, 0);
ext4_inode_set_modification_time(inode, 0);
ext4_inode_set_deletion_time(inode, 0);
ext4_inode_set_blocks_count(fs->superblock, inode, 0);
ext4_inode_set_flags(inode, 0);
ext4_inode_set_generation(inode, 0);
for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
inode->blocks[i] = 0;
if (ext4_superblock_has_feature_incompatible(
fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
ext4_extent_header_set_depth(header, 0);
ext4_extent_header_set_entries_count(header, 0);
ext4_extent_header_set_generation(header, 0);
ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
ext4_extent_header_set_max_entries_count(header, max_entries);
}
(*inode_ref)->dirty = true;
return EOK;
}
errno_t ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
ext4_inode_ref_t **inode_ref, int flags)
{
bool is_dir = false;
if (flags & L_DIRECTORY)
is_dir = true;
uint32_t index;
errno_t rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
if (rc != EOK)
return rc;
rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
if (rc != EOK) {
ext4_ialloc_free_inode(fs, index, is_dir);
return rc;
}
return EOK;
}
static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *fs,
uint32_t index, ext4_inode_ref_t **inode_ref, int flags)
{
bool is_dir = false;
if (flags & L_DIRECTORY)
is_dir = true;
errno_t rc = ext4_ialloc_alloc_this_inode(fs, index, is_dir);
if (rc != EOK)
return rc;
rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
if (rc != EOK) {
ext4_ialloc_free_inode(fs, index, is_dir);
return rc;
}
return EOK;
}
errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
{
ext4_filesystem_t *fs = inode_ref->fs;
if ((ext4_superblock_has_feature_incompatible(fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
goto finish;
}
uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
if (fblock != 0) {
errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
}
block_t *block;
uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
uint32_t count = block_size / sizeof(uint32_t);
fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
if (fblock != 0) {
errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
if (rc != EOK)
return rc;
uint32_t ind_block;
for (uint32_t offset = 0; offset < count; ++offset) {
ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
if (ind_block != 0) {
rc = ext4_balloc_free_block(inode_ref, ind_block);
if (rc != EOK) {
block_put(block);
return rc;
}
}
}
rc = block_put(block);
if (rc != EOK)
return rc;
rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
}
block_t *subblock;
fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
if (fblock != 0) {
errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
if (rc != EOK)
return rc;
uint32_t ind_block;
for (uint32_t offset = 0; offset < count; ++offset) {
ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
if (ind_block != 0) {
rc = block_get(&subblock, fs->device, ind_block,
BLOCK_FLAGS_NONE);
if (rc != EOK) {
block_put(block);
return rc;
}
uint32_t ind_subblock;
for (uint32_t suboffset = 0; suboffset < count;
++suboffset) {
ind_subblock = uint32_t_le2host(((uint32_t *)
subblock->data)[suboffset]);
if (ind_subblock != 0) {
rc = ext4_balloc_free_block(inode_ref, ind_subblock);
if (rc != EOK) {
block_put(subblock);
block_put(block);
return rc;
}
}
}
rc = block_put(subblock);
if (rc != EOK) {
block_put(block);
return rc;
}
}
rc = ext4_balloc_free_block(inode_ref, ind_block);
if (rc != EOK) {
block_put(block);
return rc;
}
}
rc = block_put(block);
if (rc != EOK)
return rc;
rc = ext4_balloc_free_block(inode_ref, fblock);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
}
finish:
inode_ref->dirty = true;
uint32_t xattr_block = ext4_inode_get_file_acl(
inode_ref->inode, fs->superblock);
if (xattr_block) {
errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
if (rc != EOK)
return rc;
ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
}
errno_t rc;
if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY))
rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
else
rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
return rc;
}
errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
aoff64_t new_size)
{
ext4_superblock_t *sb = inode_ref->fs->superblock;
if (!ext4_inode_can_truncate(sb, inode_ref->inode))
return EINVAL;
aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
if (old_size == new_size)
return EOK;
if (old_size < new_size)
return EINVAL;
aoff64_t size_diff = old_size - new_size;
uint32_t block_size = ext4_superblock_get_block_size(sb);
uint32_t diff_blocks_count = size_diff / block_size;
if (size_diff % block_size != 0)
diff_blocks_count++;
uint32_t old_blocks_count = old_size / block_size;
if (old_size % block_size != 0)
old_blocks_count++;
if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
errno_t rc = ext4_extent_release_blocks_from(inode_ref,
old_blocks_count - diff_blocks_count);
if (rc != EOK)
return rc;
} else {
for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
old_blocks_count - i);
if (rc != EOK)
return rc;
}
}
ext4_inode_set_size(inode_ref->inode, new_size);
inode_ref->dirty = true;
return EOK;
}
errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
aoff64_t iblock, uint32_t *fblock)
{
ext4_filesystem_t *fs = inode_ref->fs;
if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
*fblock = 0;
return EOK;
}
uint32_t current_block;
if ((ext4_superblock_has_feature_incompatible(fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
errno_t rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
if (rc != EOK)
return rc;
*fblock = current_block;
return EOK;
}
ext4_inode_t *inode = inode_ref->inode;
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
*fblock = current_block;
return EOK;
}
unsigned int level = 0;
for (unsigned int i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
level = i;
break;
}
}
if (level == 0)
return EIO;
aoff64_t block_offset_in_level =
iblock - fs->inode_block_limits[level - 1];
current_block = ext4_inode_get_indirect_block(inode, level - 1);
uint32_t offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
if (current_block == 0) {
*fblock = 0;
return EOK;
}
block_t *block;
while (level > 0) {
errno_t rc = block_get(&block, fs->device, current_block, 0);
if (rc != EOK)
return rc;
current_block =
uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
rc = block_put(block);
if (rc != EOK)
return rc;
if (current_block == 0) {
*fblock = 0;
return EOK;
}
level--;
if (level == 0)
break;
block_offset_in_level %= fs->inode_blocks_per_level[level];
offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
}
*fblock = current_block;
return EOK;
}
errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
aoff64_t iblock, uint32_t fblock)
{
ext4_filesystem_t *fs = inode_ref->fs;
if ((ext4_superblock_has_feature_compatible(fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
return ENOTSUP;
}
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
inode_ref->dirty = true;
return EOK;
}
unsigned int level = 0;
for (unsigned int i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
level = i;
break;
}
}
if (level == 0)
return EIO;
uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
aoff64_t block_offset_in_level =
iblock - fs->inode_block_limits[level - 1];
uint32_t current_block =
ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
uint32_t offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
uint32_t new_block_addr;
block_t *block;
block_t *new_block;
if (current_block == 0) {
errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
if (rc != EOK)
return rc;
ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
new_block_addr);
inode_ref->dirty = true;
rc = block_get(&new_block, fs->device, new_block_addr,
BLOCK_FLAGS_NOREAD);
if (rc != EOK) {
ext4_balloc_free_block(inode_ref, new_block_addr);
return rc;
}
memset(new_block->data, 0, block_size);
new_block->dirty = true;
rc = block_put(new_block);
if (rc != EOK)
return rc;
current_block = new_block_addr;
}
while (level > 0) {
errno_t rc = block_get(&block, fs->device, current_block, 0);
if (rc != EOK)
return rc;
current_block =
uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
if ((level > 1) && (current_block == 0)) {
rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
if (rc != EOK) {
block_put(block);
return rc;
}
rc = block_get(&new_block, fs->device, new_block_addr,
BLOCK_FLAGS_NOREAD);
if (rc != EOK) {
block_put(block);
return rc;
}
memset(new_block->data, 0, block_size);
new_block->dirty = true;
rc = block_put(new_block);
if (rc != EOK) {
block_put(block);
return rc;
}
((uint32_t *) block->data)[offset_in_block] =
host2uint32_t_le(new_block_addr);
block->dirty = true;
current_block = new_block_addr;
}
if (level == 1) {
((uint32_t *) block->data)[offset_in_block] =
host2uint32_t_le(fblock);
block->dirty = true;
}
rc = block_put(block);
if (rc != EOK)
return rc;
level--;
if (level == 0)
break;
block_offset_in_level %= fs->inode_blocks_per_level[level];
offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
}
return EOK;
}
errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
uint32_t iblock)
{
uint32_t fblock;
ext4_filesystem_t *fs = inode_ref->fs;
assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
ext4_inode_t *inode = inode_ref->inode;
if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
fblock = ext4_inode_get_direct_block(inode, iblock);
if (fblock == 0)
return EOK;
ext4_inode_set_direct_block(inode, iblock, 0);
return ext4_balloc_free_block(inode_ref, fblock);
}
unsigned int level = 0;
for (unsigned int i = 1; i < 4; i++) {
if (iblock < fs->inode_block_limits[i]) {
level = i;
break;
}
}
if (level == 0)
return EIO;
aoff64_t block_offset_in_level =
iblock - fs->inode_block_limits[level - 1];
uint32_t current_block =
ext4_inode_get_indirect_block(inode, level - 1);
uint32_t offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
block_t *block;
while (level > 0) {
if (current_block == 0)
return EOK;
errno_t rc = block_get(&block, fs->device, current_block, 0);
if (rc != EOK)
return rc;
current_block =
uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
if (level == 1) {
((uint32_t *) block->data)[offset_in_block] =
host2uint32_t_le(0);
block->dirty = true;
}
rc = block_put(block);
if (rc != EOK)
return rc;
level--;
if (level == 0)
break;
block_offset_in_level %= fs->inode_blocks_per_level[level];
offset_in_block =
block_offset_in_level / fs->inode_blocks_per_level[level - 1];
}
fblock = current_block;
if (fblock == 0)
return EOK;
return ext4_balloc_free_block(inode_ref, fblock);
}
errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
uint32_t *fblock, uint32_t *iblock)
{
if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
return ext4_extent_append_block(inode_ref, iblock, fblock, true);
ext4_superblock_t *sb = inode_ref->fs->superblock;
uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
uint32_t block_size = ext4_superblock_get_block_size(sb);
if ((inode_size % block_size) != 0)
inode_size += block_size - (inode_size % block_size);
uint32_t new_block_idx = inode_size / block_size;
uint32_t phys_block;
errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
if (rc != EOK)
return rc;
rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
new_block_idx, phys_block);
if (rc != EOK) {
ext4_balloc_free_block(inode_ref, phys_block);
return rc;
}
ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
inode_ref->dirty = true;
*fblock = phys_block;
*iblock = new_block_idx;
return EOK;
}
static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *sb)
{
uint32_t inode_size = ext4_superblock_get_inode_size(sb);
uint32_t block_size = ext4_superblock_get_block_size(sb);
return block_size / inode_size;
}
HelenOS homepage, sources at GitHub