HelenOS sources
This source file includes following definitions.
- mfs_read_map
- mfs_write_map
- rw_map_ondisk
- mfs_prune_ind_zones
- reset_zone_content
- alloc_zone_and_clear
- read_ind_zone
- write_ind_zone
#include <align.h>
#include "mfs.h"
static errno_t
rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
bool write_mode, uint32_t w_block);
static errno_t
reset_zone_content(struct mfs_instance *inst, uint32_t zone);
static errno_t
alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone);
static errno_t
read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone);
static errno_t
write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone);
errno_t
mfs_read_map(uint32_t *b, const struct mfs_node *mnode, uint32_t pos)
{
errno_t r;
const struct mfs_sb_info *sbi = mnode->instance->sbi;
const int block_size = sbi->block_size;
int rblock = pos / block_size;
if (ROUND_UP(mnode->ino_i->i_size, sbi->block_size) < pos) {
r = EOK;
*b = 0;
goto out;
}
r = rw_map_ondisk(b, mnode, rblock, false, 0);
out:
return r;
}
errno_t
mfs_write_map(struct mfs_node *mnode, const uint32_t pos, uint32_t new_zone,
uint32_t *old_zone)
{
const struct mfs_sb_info *sbi = mnode->instance->sbi;
if (pos >= sbi->max_file_size) {
return EINVAL;
}
int rblock = pos / sbi->block_size;
return rw_map_ondisk(old_zone, mnode, rblock, true, new_zone);
}
static errno_t
rw_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock,
bool write_mode, uint32_t w_block)
{
int nr_direct;
int ptrs_per_block;
uint32_t *ind_zone = NULL, *ind2_zone = NULL;
errno_t r = EOK;
struct mfs_ino_info *ino_i = mnode->ino_i;
struct mfs_instance *inst = mnode->instance;
struct mfs_sb_info *sbi = inst->sbi;
const mfs_version_t fs_version = sbi->fs_version;
const bool deleting = write_mode && (w_block == 0);
if (fs_version == MFS_VERSION_V1) {
nr_direct = V1_NR_DIRECT_ZONES;
ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
} else {
nr_direct = V2_NR_DIRECT_ZONES;
ptrs_per_block = sbi->block_size / sizeof(uint32_t);
}
if (rblock < nr_direct) {
*b = ino_i->i_dzone[rblock];
if (write_mode) {
ino_i->i_dzone[rblock] = w_block;
ino_i->dirty = true;
}
goto out;
}
rblock -= nr_direct;
if (rblock < ptrs_per_block) {
if (ino_i->i_izone[0] == 0) {
if (write_mode && !deleting) {
uint32_t zone;
r = alloc_zone_and_clear(inst, &zone);
if (r != EOK)
goto out;
ino_i->i_izone[0] = zone;
ino_i->dirty = true;
} else {
*b = 0;
goto out;
}
}
r = read_ind_zone(inst, ino_i->i_izone[0], &ind_zone);
if (r != EOK)
goto out;
*b = ind_zone[rblock];
if (write_mode) {
ind_zone[rblock] = w_block;
write_ind_zone(inst, ino_i->i_izone[0], ind_zone);
}
goto out;
}
rblock -= ptrs_per_block;
if (ino_i->i_izone[1] == 0) {
if (write_mode && !deleting) {
uint32_t zone;
r = alloc_zone_and_clear(inst, &zone);
if (r != EOK)
goto out;
ino_i->i_izone[1] = zone;
ino_i->dirty = true;
} else {
*b = 0;
goto out;
}
}
r = read_ind_zone(inst, ino_i->i_izone[1], &ind_zone);
if (r != EOK)
goto out;
uint32_t ind2_off = rblock / ptrs_per_block;
if (ind_zone[ind2_off] == 0) {
if (write_mode && !deleting) {
uint32_t zone;
r = alloc_zone_and_clear(inst, &zone);
if (r != EOK)
goto out;
ind_zone[ind2_off] = zone;
write_ind_zone(inst, ino_i->i_izone[1], ind_zone);
} else {
*b = 0;
goto out;
}
}
r = read_ind_zone(inst, ind_zone[ind2_off], &ind2_zone);
if (r != EOK)
goto out;
*b = ind2_zone[rblock - (ind2_off * ptrs_per_block)];
if (write_mode) {
ind2_zone[rblock - (ind2_off * ptrs_per_block)] = w_block;
write_ind_zone(inst, ind_zone[ind2_off], ind2_zone);
}
out:
free(ind2_zone);
free(ind_zone);
return r;
}
errno_t
mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size)
{
struct mfs_instance *inst = mnode->instance;
struct mfs_sb_info *sbi = inst->sbi;
struct mfs_ino_info *ino_i = mnode->ino_i;
int nr_direct, ptrs_per_block, rblock;
errno_t r;
int i;
mfs_version_t fs_version = sbi->fs_version;
assert(new_size <= ino_i->i_size);
if (fs_version == MFS_VERSION_V1) {
nr_direct = V1_NR_DIRECT_ZONES;
ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
} else {
nr_direct = V2_NR_DIRECT_ZONES;
ptrs_per_block = sbi->block_size / sizeof(uint32_t);
}
rblock = new_size / sbi->block_size;
if (rblock < nr_direct) {
if (ino_i->i_izone[0]) {
r = mfs_free_zone(inst, ino_i->i_izone[0]);
if (r != EOK)
return r;
ino_i->i_izone[0] = 0;
ino_i->dirty = true;
}
}
rblock -= nr_direct + ptrs_per_block;
int fzone_to_free = (rblock < 0 ? 0 : rblock) / ptrs_per_block;
if ((fzone_to_free % ptrs_per_block) != 0)
++fzone_to_free;
uint32_t *dbl_zone;
if (ino_i->i_izone[1] == 0) {
return EOK;
}
r = read_ind_zone(inst, ino_i->i_izone[1], &dbl_zone);
if (r != EOK)
return r;
for (i = fzone_to_free; i < ptrs_per_block; ++i) {
if (dbl_zone[i] == 0)
continue;
r = mfs_free_zone(inst, dbl_zone[i]);
if (r != EOK)
goto out;
}
if (fzone_to_free == 0) {
r = mfs_free_zone(inst, ino_i->i_izone[1]);
ino_i->i_izone[1] = 0;
ino_i->dirty = true;
}
out:
free(dbl_zone);
return r;
}
static errno_t
reset_zone_content(struct mfs_instance *inst, uint32_t zone)
{
block_t *b;
errno_t r;
r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
if (r != EOK)
return r;
memset(b->data, 0, b->size);
b->dirty = true;
return block_put(b);
}
static errno_t
alloc_zone_and_clear(struct mfs_instance *inst, uint32_t *zone)
{
errno_t r;
r = mfs_alloc_zone(inst, zone);
if (r != EOK)
return r;
r = reset_zone_content(inst, *zone);
return r;
}
static errno_t
read_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t **ind_zone)
{
struct mfs_sb_info *sbi = inst->sbi;
errno_t r;
unsigned i;
block_t *b;
const int max_ind_zone_ptrs = (MFS_MAX_BLOCKSIZE / sizeof(uint16_t)) *
sizeof(uint32_t);
*ind_zone = malloc(max_ind_zone_ptrs);
if (*ind_zone == NULL)
return ENOMEM;
r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NONE);
if (r != EOK) {
free(*ind_zone);
return r;
}
if (sbi->fs_version == MFS_VERSION_V1) {
uint16_t *src_ptr = b->data;
for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
(*ind_zone)[i] = conv16(sbi->native, src_ptr[i]);
} else {
uint32_t *src_ptr = b->data;
for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
(*ind_zone)[i] = conv32(sbi->native, src_ptr[i]);
}
return block_put(b);
}
static errno_t
write_ind_zone(struct mfs_instance *inst, uint32_t zone, uint32_t *ind_zone)
{
struct mfs_sb_info *sbi = inst->sbi;
errno_t r;
unsigned i;
block_t *b;
r = block_get(&b, inst->service_id, zone, BLOCK_FLAGS_NOREAD);
if (r != EOK)
return r;
if (sbi->fs_version == MFS_VERSION_V1) {
uint16_t *dest_ptr = b->data;
for (i = 0; i < sbi->block_size / sizeof(uint16_t); ++i)
dest_ptr[i] = conv16(sbi->native, ind_zone[i]);
} else {
uint32_t *dest_ptr = b->data;
for (i = 0; i < sbi->block_size / sizeof(uint32_t); ++i)
dest_ptr[i] = conv32(sbi->native, ind_zone[i]);
}
b->dirty = true;
return block_put(b);
}
HelenOS homepage, sources at GitHub