HelenOS sources
This source file includes following definitions.
- open_nodes_key_hash
- open_nodes_hash
- open_nodes_key_equal
- ext4_global_init
- ext4_global_fini
- ext4_instance_get
- ext4_root_get
- ext4_match
- ext4_node_get
- ext4_node_get_core
- ext4_node_put_core
- ext4_node_open
- ext4_node_put
- ext4_create_node
- ext4_destroy_node
- ext4_link
- ext4_unlink
- ext4_has_children
- ext4_index_get
- ext4_size_get
- ext4_lnkcnt_get
- ext4_is_directory
- ext4_is_file
- ext4_service_get
- ext4_size_block
- ext4_total_block_count
- ext4_free_block_count
- ext4_fsprobe
- ext4_mounted
- ext4_unmounted
- ext4_read
- ext4_is_dots
- ext4_read_directory
- ext4_read_file
- ext4_write
- ext4_truncate
- ext4_close
- ext4_destroy
- ext4_sync
#include <adt/hash_table.h>
#include <adt/hash.h>
#include <errno.h>
#include <fibril_synch.h>
#include <libfs.h>
#include <macros.h>
#include <mem.h>
#include <stdlib.h>
#include <str.h>
#include <ipc/loc.h>
#include "ext4/balloc.h"
#include "ext4/directory.h"
#include "ext4/directory_index.h"
#include "ext4/extent.h"
#include "ext4/inode.h"
#include "ext4/ops.h"
#include "ext4/filesystem.h"
#include "ext4/fstypes.h"
#include "ext4/superblock.h"
static errno_t ext4_read_directory(ipc_call_t *, aoff64_t, size_t,
ext4_instance_t *, ext4_inode_ref_t *, size_t *);
static errno_t ext4_read_file(ipc_call_t *, aoff64_t, size_t, ext4_instance_t *,
ext4_inode_ref_t *, size_t *);
static bool ext4_is_dots(const uint8_t *, size_t);
static errno_t ext4_instance_get(service_id_t, ext4_instance_t **);
static errno_t ext4_root_get(fs_node_t **, service_id_t);
static errno_t ext4_match(fs_node_t **, fs_node_t *, const char *);
static errno_t ext4_node_get(fs_node_t **, service_id_t, fs_index_t);
static errno_t ext4_node_open(fs_node_t *);
errno_t ext4_node_put(fs_node_t *);
static errno_t ext4_create_node(fs_node_t **, service_id_t, int);
static errno_t ext4_destroy_node(fs_node_t *);
static errno_t ext4_link(fs_node_t *, fs_node_t *, const char *);
static errno_t ext4_unlink(fs_node_t *, fs_node_t *, const char *);
static errno_t ext4_has_children(bool *, fs_node_t *);
static fs_index_t ext4_index_get(fs_node_t *);
static aoff64_t ext4_size_get(fs_node_t *);
static unsigned ext4_lnkcnt_get(fs_node_t *);
static bool ext4_is_directory(fs_node_t *);
static bool ext4_is_file(fs_node_t *node);
static service_id_t ext4_service_get(fs_node_t *node);
static errno_t ext4_size_block(service_id_t, uint32_t *);
static errno_t ext4_total_block_count(service_id_t, uint64_t *);
static errno_t ext4_free_block_count(service_id_t, uint64_t *);
static LIST_INITIALIZE(instance_list);
static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
static hash_table_t open_nodes;
static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
typedef struct {
service_id_t service_id;
fs_index_t index;
} node_key_t;
static size_t open_nodes_key_hash(const void *key_arg)
{
const node_key_t *key = key_arg;
return hash_combine(key->service_id, key->index);
}
static size_t open_nodes_hash(const ht_link_t *item)
{
ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
return hash_combine(enode->instance->service_id, enode->inode_ref->index);
}
static bool open_nodes_key_equal(const void *key_arg, const ht_link_t *item)
{
const node_key_t *key = key_arg;
ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
return key->service_id == enode->instance->service_id &&
key->index == enode->inode_ref->index;
}
static const hash_table_ops_t open_nodes_ops = {
.hash = open_nodes_hash,
.key_hash = open_nodes_key_hash,
.key_equal = open_nodes_key_equal,
.equal = NULL,
.remove_callback = NULL,
};
errno_t ext4_global_init(void)
{
if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops))
return ENOMEM;
return EOK;
}
errno_t ext4_global_fini(void)
{
hash_table_destroy(&open_nodes);
return EOK;
}
errno_t ext4_instance_get(service_id_t service_id, ext4_instance_t **inst)
{
fibril_mutex_lock(&instance_list_mutex);
if (list_empty(&instance_list)) {
fibril_mutex_unlock(&instance_list_mutex);
return EINVAL;
}
list_foreach(instance_list, link, ext4_instance_t, tmp) {
if (tmp->service_id == service_id) {
*inst = tmp;
fibril_mutex_unlock(&instance_list_mutex);
return EOK;
}
}
fibril_mutex_unlock(&instance_list_mutex);
return EINVAL;
}
errno_t ext4_root_get(fs_node_t **rfn, service_id_t service_id)
{
return ext4_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
}
errno_t ext4_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
{
ext4_node_t *eparent = EXT4_NODE(pfn);
ext4_filesystem_t *fs = eparent->instance->filesystem;
errno_t rc2;
if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY))
return ENOTDIR;
ext4_directory_search_result_t result;
errno_t rc = ext4_directory_find_entry(&result, eparent->inode_ref,
component);
if (rc != EOK) {
if (rc == ENOENT) {
*rfn = NULL;
return EOK;
}
return rc;
}
uint32_t inode = ext4_directory_entry_ll_get_inode(result.dentry);
rc = ext4_node_get_core(rfn, eparent->instance, inode);
if (rc != EOK)
goto exit;
exit:
rc2 = ext4_directory_destroy_result(&result);
return rc == EOK ? rc2 : rc;
}
errno_t ext4_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
{
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
return ext4_node_get_core(rfn, inst, index);
}
errno_t ext4_node_get_core(fs_node_t **rfn, ext4_instance_t *inst,
fs_index_t index)
{
fibril_mutex_lock(&open_nodes_lock);
node_key_t key = {
.service_id = inst->service_id,
.index = index
};
ht_link_t *already_open = hash_table_find(&open_nodes, &key);
ext4_node_t *enode = NULL;
if (already_open) {
enode = hash_table_get_inst(already_open, ext4_node_t, link);
*rfn = enode->fs_node;
enode->references++;
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
enode = malloc(sizeof(ext4_node_t));
if (enode == NULL) {
fibril_mutex_unlock(&open_nodes_lock);
return ENOMEM;
}
fs_node_t *fs_node = malloc(sizeof(fs_node_t));
if (fs_node == NULL) {
free(enode);
fibril_mutex_unlock(&open_nodes_lock);
return ENOMEM;
}
fs_node_initialize(fs_node);
ext4_inode_ref_t *inode_ref;
errno_t rc = ext4_filesystem_get_inode_ref(inst->filesystem, index,
&inode_ref);
if (rc != EOK) {
free(enode);
free(fs_node);
fibril_mutex_unlock(&open_nodes_lock);
return rc;
}
enode->inode_ref = inode_ref;
enode->instance = inst;
enode->references = 1;
enode->fs_node = fs_node;
fs_node->data = enode;
*rfn = fs_node;
hash_table_insert(&open_nodes, &enode->link);
inst->open_nodes_count++;
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
static errno_t ext4_node_put_core(ext4_node_t *enode)
{
hash_table_remove_item(&open_nodes, &enode->link);
assert(enode->instance->open_nodes_count > 0);
enode->instance->open_nodes_count--;
errno_t rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
if (rc != EOK)
return rc;
free(enode->fs_node);
free(enode);
return EOK;
}
errno_t ext4_node_open(fs_node_t *fn)
{
return EOK;
}
errno_t ext4_node_put(fs_node_t *fn)
{
fibril_mutex_lock(&open_nodes_lock);
ext4_node_t *enode = EXT4_NODE(fn);
assert(enode->references > 0);
enode->references--;
if (enode->references == 0) {
errno_t rc = ext4_node_put_core(enode);
if (rc != EOK) {
fibril_mutex_unlock(&open_nodes_lock);
return rc;
}
}
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
errno_t ext4_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
{
ext4_node_t *enode;
enode = malloc(sizeof(ext4_node_t));
if (enode == NULL)
return ENOMEM;
fs_node_t *fs_node;
fs_node = malloc(sizeof(fs_node_t));
if (fs_node == NULL) {
free(enode);
return ENOMEM;
}
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK) {
free(enode);
free(fs_node);
return rc;
}
ext4_inode_ref_t *inode_ref;
rc = ext4_filesystem_alloc_inode(inst->filesystem, &inode_ref, flags);
if (rc != EOK) {
free(enode);
free(fs_node);
return rc;
}
enode->inode_ref = inode_ref;
enode->instance = inst;
enode->references = 1;
fibril_mutex_lock(&open_nodes_lock);
hash_table_insert(&open_nodes, &enode->link);
fibril_mutex_unlock(&open_nodes_lock);
inst->open_nodes_count++;
enode->inode_ref->dirty = true;
fs_node_initialize(fs_node);
fs_node->data = enode;
enode->fs_node = fs_node;
*rfn = fs_node;
return EOK;
}
errno_t ext4_destroy_node(fs_node_t *fn)
{
bool has_children;
errno_t rc = ext4_has_children(&has_children, fn);
if (rc != EOK) {
ext4_node_put(fn);
return rc;
}
if (has_children) {
ext4_node_put(fn);
return EINVAL;
}
ext4_node_t *enode = EXT4_NODE(fn);
ext4_inode_ref_t *inode_ref = enode->inode_ref;
rc = ext4_filesystem_truncate_inode(inode_ref, 0);
if (rc != EOK) {
ext4_node_put(fn);
return rc;
}
ext4_inode_set_deletion_time(inode_ref->inode, 0xdeadbeef);
inode_ref->dirty = true;
rc = ext4_filesystem_free_inode(inode_ref);
if (rc != EOK) {
ext4_node_put(fn);
return rc;
}
return ext4_node_put(fn);
}
errno_t ext4_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
{
if (str_size(name) > EXT4_DIRECTORY_FILENAME_LEN)
return ENAMETOOLONG;
ext4_node_t *parent = EXT4_NODE(pfn);
ext4_node_t *child = EXT4_NODE(cfn);
ext4_filesystem_t *fs = parent->instance->filesystem;
errno_t rc = ext4_directory_add_entry(parent->inode_ref, name,
child->inode_ref);
if (rc != EOK)
return rc;
if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY)) {
rc = ext4_directory_add_entry(child->inode_ref, ".",
child->inode_ref);
if (rc != EOK) {
ext4_directory_remove_entry(parent->inode_ref, name);
return rc;
}
rc = ext4_directory_add_entry(child->inode_ref, "..",
parent->inode_ref);
if (rc != EOK) {
ext4_directory_remove_entry(parent->inode_ref, name);
ext4_directory_remove_entry(child->inode_ref, ".");
return rc;
}
if (ext4_superblock_has_feature_compatible(fs->superblock,
EXT4_FEATURE_COMPAT_DIR_INDEX)) {
rc = ext4_directory_dx_init(child->inode_ref);
if (rc != EOK)
return rc;
ext4_inode_set_flag(child->inode_ref->inode,
EXT4_INODE_FLAG_INDEX);
child->inode_ref->dirty = true;
}
uint16_t parent_links =
ext4_inode_get_links_count(parent->inode_ref->inode);
parent_links++;
ext4_inode_set_links_count(parent->inode_ref->inode, parent_links);
parent->inode_ref->dirty = true;
}
uint16_t child_links =
ext4_inode_get_links_count(child->inode_ref->inode);
child_links++;
ext4_inode_set_links_count(child->inode_ref->inode, child_links);
child->inode_ref->dirty = true;
return EOK;
}
errno_t ext4_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
{
bool has_children;
errno_t rc = ext4_has_children(&has_children, cfn);
if (rc != EOK)
return rc;
if (has_children)
return ENOTEMPTY;
ext4_inode_ref_t *parent = EXT4_NODE(pfn)->inode_ref;
rc = ext4_directory_remove_entry(parent, name);
if (rc != EOK)
return rc;
ext4_inode_ref_t *child_inode_ref = EXT4_NODE(cfn)->inode_ref;
uint32_t lnk_count =
ext4_inode_get_links_count(child_inode_ref->inode);
lnk_count--;
if ((lnk_count <= 1) && (ext4_is_directory(cfn))) {
assert(lnk_count == 1);
lnk_count--;
ext4_inode_ref_t *parent_inode_ref = EXT4_NODE(pfn)->inode_ref;
uint32_t parent_lnk_count = ext4_inode_get_links_count(
parent_inode_ref->inode);
parent_lnk_count--;
ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
parent->dirty = true;
}
ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
child_inode_ref->dirty = true;
return EOK;
}
errno_t ext4_has_children(bool *has_children, fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
ext4_filesystem_t *fs = enode->instance->filesystem;
if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY)) {
*has_children = false;
return EOK;
}
ext4_directory_iterator_t it;
errno_t rc = ext4_directory_iterator_init(&it, enode->inode_ref, 0);
if (rc != EOK)
return rc;
bool found = false;
while (it.current != NULL) {
if (it.current->inode != 0) {
uint16_t name_size =
ext4_directory_entry_ll_get_name_length(fs->superblock,
it.current);
if (!ext4_is_dots(it.current->name, name_size)) {
found = true;
break;
}
}
rc = ext4_directory_iterator_next(&it);
if (rc != EOK) {
ext4_directory_iterator_fini(&it);
return rc;
}
}
rc = ext4_directory_iterator_fini(&it);
if (rc != EOK)
return rc;
*has_children = found;
return EOK;
}
fs_index_t ext4_index_get(fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
return enode->inode_ref->index;
}
aoff64_t ext4_size_get(fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
ext4_superblock_t *sb = enode->instance->filesystem->superblock;
return ext4_inode_get_size(sb, enode->inode_ref->inode);
}
unsigned ext4_lnkcnt_get(fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
uint32_t lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
if (ext4_is_directory(fn)) {
if (lnkcnt > 1)
return 1;
else
return 0;
}
return lnkcnt;
}
bool ext4_is_directory(fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
ext4_superblock_t *sb = enode->instance->filesystem->superblock;
return ext4_inode_is_type(sb, enode->inode_ref->inode,
EXT4_INODE_MODE_DIRECTORY);
}
bool ext4_is_file(fs_node_t *fn)
{
ext4_node_t *enode = EXT4_NODE(fn);
ext4_superblock_t *sb = enode->instance->filesystem->superblock;
return ext4_inode_is_type(sb, enode->inode_ref->inode,
EXT4_INODE_MODE_FILE);
}
service_id_t ext4_service_get(fs_node_t *fn)
{
return 0;
}
errno_t ext4_size_block(service_id_t service_id, uint32_t *size)
{
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
if (NULL == inst)
return ENOENT;
ext4_superblock_t *sb = inst->filesystem->superblock;
*size = ext4_superblock_get_block_size(sb);
return EOK;
}
errno_t ext4_total_block_count(service_id_t service_id, uint64_t *count)
{
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
if (NULL == inst)
return ENOENT;
ext4_superblock_t *sb = inst->filesystem->superblock;
*count = ext4_superblock_get_blocks_count(sb);
return EOK;
}
errno_t ext4_free_block_count(service_id_t service_id, uint64_t *count)
{
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
ext4_superblock_t *sb = inst->filesystem->superblock;
*count = ext4_superblock_get_free_blocks_count(sb);
return EOK;
}
libfs_ops_t ext4_libfs_ops = {
.root_get = ext4_root_get,
.match = ext4_match,
.node_get = ext4_node_get,
.node_open = ext4_node_open,
.node_put = ext4_node_put,
.create = ext4_create_node,
.destroy = ext4_destroy_node,
.link = ext4_link,
.unlink = ext4_unlink,
.has_children = ext4_has_children,
.index_get = ext4_index_get,
.size_get = ext4_size_get,
.lnkcnt_get = ext4_lnkcnt_get,
.is_directory = ext4_is_directory,
.is_file = ext4_is_file,
.service_get = ext4_service_get,
.size_block = ext4_size_block,
.total_block_count = ext4_total_block_count,
.free_block_count = ext4_free_block_count
};
static errno_t ext4_fsprobe(service_id_t service_id, vfs_fs_probe_info_t *info)
{
ext4_fs_probe_info_t pinfo;
errno_t rc;
rc = ext4_filesystem_probe(service_id, &pinfo);
if (rc != EOK)
return rc;
memcpy(info->label, pinfo.vol_name, sizeof(pinfo.vol_name));
return EOK;
}
static errno_t ext4_mounted(service_id_t service_id, const char *opts,
fs_index_t *index, aoff64_t *size)
{
ext4_filesystem_t *fs;
ext4_instance_t *inst = (ext4_instance_t *)
malloc(sizeof(ext4_instance_t));
if (inst == NULL)
return ENOMEM;
enum cache_mode cmode;
if (str_cmp(opts, "wtcache") == 0)
cmode = CACHE_MODE_WT;
else
cmode = CACHE_MODE_WB;
link_initialize(&inst->link);
inst->service_id = service_id;
inst->open_nodes_count = 0;
aoff64_t rnsize;
errno_t rc = ext4_filesystem_open(inst, service_id, cmode, &rnsize, &fs);
if (rc != EOK) {
free(inst);
return rc;
}
fibril_mutex_lock(&instance_list_mutex);
list_append(&inst->link, &instance_list);
fibril_mutex_unlock(&instance_list_mutex);
*index = EXT4_INODE_ROOT_INDEX;
*size = rnsize;
return EOK;
}
static errno_t ext4_unmounted(service_id_t service_id)
{
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
fibril_mutex_lock(&open_nodes_lock);
if (inst->open_nodes_count != 0) {
fibril_mutex_unlock(&open_nodes_lock);
return EBUSY;
}
fibril_mutex_lock(&instance_list_mutex);
list_remove(&inst->link);
fibril_mutex_unlock(&instance_list_mutex);
fibril_mutex_unlock(&open_nodes_lock);
rc = ext4_filesystem_close(inst->filesystem);
if (rc != EOK) {
fibril_mutex_lock(&instance_list_mutex);
list_append(&inst->link, &instance_list);
fibril_mutex_unlock(&instance_list_mutex);
}
free(inst);
return EOK;
}
static errno_t ext4_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
size_t *rbytes)
{
ipc_call_t call;
size_t size;
if (!async_data_read_receive(&call, &size)) {
async_answer_0(&call, EINVAL);
return EINVAL;
}
ext4_instance_t *inst;
errno_t rc = ext4_instance_get(service_id, &inst);
if (rc != EOK) {
async_answer_0(&call, rc);
return rc;
}
ext4_inode_ref_t *inode_ref;
rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
if (rc != EOK) {
async_answer_0(&call, rc);
return rc;
}
if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
EXT4_INODE_MODE_FILE)) {
rc = ext4_read_file(&call, pos, size, inst, inode_ref,
rbytes);
} else if (ext4_inode_is_type(inst->filesystem->superblock,
inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
rc = ext4_read_directory(&call, pos, size, inst, inode_ref,
rbytes);
} else {
async_answer_0(&call, ENOTSUP);
rc = ENOTSUP;
}
errno_t const rc2 = ext4_filesystem_put_inode_ref(inode_ref);
return rc == EOK ? rc2 : rc;
}
bool ext4_is_dots(const uint8_t *name, size_t name_size)
{
if ((name_size == 1) && (name[0] == '.'))
return true;
if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
return true;
return false;
}
errno_t ext4_read_directory(ipc_call_t *call, aoff64_t pos, size_t size,
ext4_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
{
ext4_directory_iterator_t it;
errno_t rc = ext4_directory_iterator_init(&it, inode_ref, pos);
if (rc != EOK) {
async_answer_0(call, rc);
return rc;
}
bool found = false;
while (it.current != NULL) {
if (it.current->inode == 0)
goto skip;
uint16_t name_size = ext4_directory_entry_ll_get_name_length(
inst->filesystem->superblock, it.current);
if (ext4_is_dots(it.current->name, name_size))
goto skip;
uint8_t *buf = malloc(name_size + 1);
if (buf == NULL) {
ext4_directory_iterator_fini(&it);
async_answer_0(call, ENOMEM);
return ENOMEM;
}
memcpy(buf, &it.current->name, name_size);
*(buf + name_size) = 0;
found = true;
(void) async_data_read_finalize(call, buf, name_size + 1);
free(buf);
break;
skip:
rc = ext4_directory_iterator_next(&it);
if (rc != EOK) {
ext4_directory_iterator_fini(&it);
async_answer_0(call, rc);
return rc;
}
}
uint64_t next;
if (found) {
rc = ext4_directory_iterator_next(&it);
if (rc != EOK)
return rc;
next = it.current_offset;
}
rc = ext4_directory_iterator_fini(&it);
if (rc != EOK)
return rc;
if (found) {
*rbytes = next - pos;
return EOK;
} else {
async_answer_0(call, ENOENT);
return ENOENT;
}
}
errno_t ext4_read_file(ipc_call_t *call, aoff64_t pos, size_t size,
ext4_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
{
ext4_superblock_t *sb = inst->filesystem->superblock;
uint64_t file_size = ext4_inode_get_size(sb, inode_ref->inode);
if (pos >= file_size) {
async_data_read_finalize(call, NULL, 0);
*rbytes = 0;
return EOK;
}
uint32_t block_size = ext4_superblock_get_block_size(sb);
aoff64_t file_block = pos / block_size;
uint32_t offset_in_block = pos % block_size;
uint32_t bytes = min(block_size - offset_in_block, size);
if (pos + bytes > file_size)
bytes = file_size - pos;
uint32_t fs_block;
errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
file_block, &fs_block);
if (rc != EOK) {
async_answer_0(call, rc);
return rc;
}
uint8_t *buffer;
if (fs_block == 0) {
buffer = malloc(bytes);
if (buffer == NULL) {
async_answer_0(call, ENOMEM);
return ENOMEM;
}
memset(buffer, 0, bytes);
rc = async_data_read_finalize(call, buffer, bytes);
*rbytes = bytes;
free(buffer);
return rc;
}
block_t *block;
rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
if (rc != EOK) {
async_answer_0(call, rc);
return rc;
}
assert(offset_in_block + bytes <= block_size);
rc = async_data_read_finalize(call, block->data + offset_in_block, bytes);
if (rc != EOK) {
block_put(block);
return rc;
}
rc = block_put(block);
if (rc != EOK)
return rc;
*rbytes = bytes;
return EOK;
}
static errno_t ext4_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
size_t *wbytes, aoff64_t *nsize)
{
fs_node_t *fn;
errno_t rc2;
errno_t rc = ext4_node_get(&fn, service_id, index);
if (rc != EOK)
return rc;
ipc_call_t call;
size_t len;
if (!async_data_write_receive(&call, &len)) {
rc = EINVAL;
async_answer_0(&call, rc);
goto exit;
}
ext4_node_t *enode = EXT4_NODE(fn);
ext4_filesystem_t *fs = enode->instance->filesystem;
uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
uint32_t bytes = min(len, block_size - (pos % block_size));
int flags = BLOCK_FLAGS_NONE;
if (bytes == block_size)
flags = BLOCK_FLAGS_NOREAD;
uint32_t iblock = pos / block_size;
uint32_t fblock;
ext4_inode_ref_t *inode_ref = enode->inode_ref;
rc = ext4_filesystem_get_inode_data_block_index(inode_ref, iblock,
&fblock);
if (rc != EOK) {
async_answer_0(&call, rc);
goto exit;
}
if (fblock == 0) {
if ((ext4_superblock_has_feature_incompatible(fs->superblock,
EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
(ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
uint32_t last_iblock =
ext4_inode_get_size(fs->superblock, inode_ref->inode) /
block_size;
while (last_iblock < iblock) {
rc = ext4_extent_append_block(inode_ref, &last_iblock,
&fblock, true);
if (rc != EOK) {
async_answer_0(&call, rc);
goto exit;
}
}
rc = ext4_extent_append_block(inode_ref, &last_iblock,
&fblock, false);
if (rc != EOK) {
async_answer_0(&call, rc);
goto exit;
}
} else {
rc = ext4_balloc_alloc_block(inode_ref, &fblock);
if (rc != EOK) {
async_answer_0(&call, rc);
goto exit;
}
rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
iblock, fblock);
if (rc != EOK) {
ext4_balloc_free_block(inode_ref, fblock);
async_answer_0(&call, rc);
goto exit;
}
}
flags = BLOCK_FLAGS_NOREAD;
inode_ref->dirty = true;
}
block_t *write_block;
rc = block_get(&write_block, service_id, fblock, flags);
if (rc != EOK) {
async_answer_0(&call, rc);
goto exit;
}
if (flags == BLOCK_FLAGS_NOREAD)
memset(write_block->data, 0, block_size);
rc = async_data_write_finalize(&call, write_block->data +
(pos % block_size), bytes);
if (rc != EOK) {
block_put(write_block);
goto exit;
}
write_block->dirty = true;
rc = block_put(write_block);
if (rc != EOK)
goto exit;
uint32_t old_inode_size = ext4_inode_get_size(fs->superblock,
inode_ref->inode);
if (pos + bytes > old_inode_size) {
ext4_inode_set_size(inode_ref->inode, pos + bytes);
inode_ref->dirty = true;
}
*nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
*wbytes = bytes;
exit:
rc2 = ext4_node_put(fn);
return rc == EOK ? rc2 : rc;
}
static errno_t ext4_truncate(service_id_t service_id, fs_index_t index,
aoff64_t new_size)
{
fs_node_t *fn;
errno_t rc = ext4_node_get(&fn, service_id, index);
if (rc != EOK)
return rc;
ext4_node_t *enode = EXT4_NODE(fn);
ext4_inode_ref_t *inode_ref = enode->inode_ref;
rc = ext4_filesystem_truncate_inode(inode_ref, new_size);
errno_t const rc2 = ext4_node_put(fn);
return rc == EOK ? rc2 : rc;
}
static errno_t ext4_close(service_id_t service_id, fs_index_t index)
{
return EOK;
}
static errno_t ext4_destroy(service_id_t service_id, fs_index_t index)
{
fs_node_t *fn;
errno_t rc = ext4_node_get(&fn, service_id, index);
if (rc != EOK)
return rc;
return ext4_destroy_node(fn);
}
static errno_t ext4_sync(service_id_t service_id, fs_index_t index)
{
fs_node_t *fn;
errno_t rc = ext4_node_get(&fn, service_id, index);
if (rc != EOK)
return rc;
ext4_node_t *enode = EXT4_NODE(fn);
enode->inode_ref->dirty = true;
return ext4_node_put(fn);
}
vfs_out_ops_t ext4_ops = {
.fsprobe = ext4_fsprobe,
.mounted = ext4_mounted,
.unmounted = ext4_unmounted,
.read = ext4_read,
.write = ext4_write,
.truncate = ext4_truncate,
.close = ext4_close,
.destroy = ext4_destroy,
.sync = ext4_sync
};
HelenOS homepage, sources at GitHub