HelenOS sources
This source file includes following definitions.
- unused_initialize
- unused_find
- pos_key_hash
- pos_hash
- pos_key_equal
- idx_key_hash
- idx_hash
- idx_key_equal
- idx_remove_callback
- exfat_index_alloc
- try_coalesce_intervals
- exfat_index_free
- exfat_idx_create
- exfat_idx_get_new
- exfat_idx_get_by_pos
- exfat_idx_hashin
- exfat_idx_hashout
- exfat_idx_get_by_index
- exfat_idx_destroy
- exfat_idx_init
- exfat_idx_fini
- exfat_idx_init_by_service_id
- rm_pos_service_id
- rm_idx_service_id
- exfat_idx_fini_by_service_id
#include "exfat.h"
#include "../../vfs/vfs.h"
#include <errno.h>
#include <str.h>
#include <adt/hash_table.h>
#include <adt/hash.h>
#include <adt/list.h>
#include <assert.h>
#include <fibril_synch.h>
#include <stdlib.h>
typedef struct {
link_t link;
fs_index_t first;
fs_index_t last;
} freed_t;
typedef struct {
link_t link;
service_id_t service_id;
fs_index_t next;
uint64_t remaining;
list_t freed_list;
} unused_t;
static FIBRIL_MUTEX_INITIALIZE(unused_lock);
static LIST_INITIALIZE(unused_list);
static void unused_initialize(unused_t *u, service_id_t service_id)
{
link_initialize(&u->link);
u->service_id = service_id;
u->next = 0;
u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
list_initialize(&u->freed_list);
}
static unused_t *unused_find(service_id_t service_id, bool lock)
{
if (lock)
fibril_mutex_lock(&unused_lock);
list_foreach(unused_list, link, unused_t, u) {
if (u->service_id == service_id)
return u;
}
if (lock)
fibril_mutex_unlock(&unused_lock);
return NULL;
}
static FIBRIL_MUTEX_INITIALIZE(used_lock);
static hash_table_t up_hash;
typedef struct {
service_id_t service_id;
exfat_cluster_t pfc;
unsigned pdi;
} pos_key_t;
static inline size_t pos_key_hash(const void *key)
{
const pos_key_t *pos = key;
size_t hash = 0;
hash = hash_combine(pos->pfc, pos->pdi);
return hash_combine(hash, pos->service_id);
}
static size_t pos_hash(const ht_link_t *item)
{
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
pos_key_t pkey = {
.service_id = fidx->service_id,
.pfc = fidx->pfc,
.pdi = fidx->pdi,
};
return pos_key_hash(&pkey);
}
static bool pos_key_equal(const void *key, const ht_link_t *item)
{
const pos_key_t *pos = key;
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
return pos->service_id == fidx->service_id &&
pos->pdi == fidx->pdi &&
pos->pfc == fidx->pfc;
}
static const hash_table_ops_t uph_ops = {
.hash = pos_hash,
.key_hash = pos_key_hash,
.key_equal = pos_key_equal,
.equal = NULL,
.remove_callback = NULL,
};
static hash_table_t ui_hash;
typedef struct {
service_id_t service_id;
fs_index_t index;
} idx_key_t;
static size_t idx_key_hash(const void *key_arg)
{
const idx_key_t *key = key_arg;
return hash_combine(key->service_id, key->index);
}
static size_t idx_hash(const ht_link_t *item)
{
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
return hash_combine(fidx->service_id, fidx->index);
}
static bool idx_key_equal(const void *key_arg, const ht_link_t *item)
{
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
const idx_key_t *key = key_arg;
return key->index == fidx->index && key->service_id == fidx->service_id;
}
static void idx_remove_callback(ht_link_t *item)
{
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
free(fidx);
}
static const hash_table_ops_t uih_ops = {
.hash = idx_hash,
.key_hash = idx_key_hash,
.key_equal = idx_key_equal,
.equal = NULL,
.remove_callback = idx_remove_callback,
};
static bool exfat_index_alloc(service_id_t service_id, fs_index_t *index)
{
unused_t *u;
assert(index);
u = unused_find(service_id, true);
if (!u)
return false;
if (list_empty(&u->freed_list)) {
if (u->remaining) {
*index = u->next++;
--u->remaining;
fibril_mutex_unlock(&unused_lock);
return true;
}
} else {
freed_t *f = list_get_instance(list_first(&u->freed_list),
freed_t, link);
*index = f->first;
if (f->first++ == f->last) {
list_remove(&f->link);
free(f);
}
fibril_mutex_unlock(&unused_lock);
return true;
}
fibril_mutex_unlock(&unused_lock);
return false;
}
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
{
freed_t *fl = list_get_instance(l, freed_t, link);
freed_t *fr = list_get_instance(r, freed_t, link);
if (fl->last + 1 == fr->first) {
if (cur == l) {
fl->last = fr->last;
list_remove(r);
free(r);
} else {
fr->first = fl->first;
list_remove(l);
free(l);
}
}
}
static void exfat_index_free(service_id_t service_id, fs_index_t index)
{
unused_t *u;
u = unused_find(service_id, true);
assert(u);
if (u->next == index + 1) {
u->next--;
u->remaining++;
} else {
link_t *lnk;
freed_t *n;
for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
lnk = lnk->next) {
freed_t *f = list_get_instance(lnk, freed_t, link);
if (f->first == index + 1) {
f->first--;
if (lnk->prev != &u->freed_list.head)
try_coalesce_intervals(lnk->prev, lnk,
lnk);
fibril_mutex_unlock(&unused_lock);
return;
}
if (f->last == index - 1) {
f->last++;
if (lnk->next != &u->freed_list.head)
try_coalesce_intervals(lnk, lnk->next,
lnk);
fibril_mutex_unlock(&unused_lock);
return;
}
if (index > f->first) {
n = malloc(sizeof(freed_t));
assert(n);
link_initialize(&n->link);
n->first = index;
n->last = index;
list_insert_before(&n->link, lnk);
fibril_mutex_unlock(&unused_lock);
return;
}
}
n = malloc(sizeof(freed_t));
assert(n);
link_initialize(&n->link);
n->first = index;
n->last = index;
list_append(&n->link, &u->freed_list);
}
fibril_mutex_unlock(&unused_lock);
}
static errno_t exfat_idx_create(exfat_idx_t **fidxp, service_id_t service_id)
{
exfat_idx_t *fidx;
fidx = (exfat_idx_t *) malloc(sizeof(exfat_idx_t));
if (!fidx)
return ENOMEM;
if (!exfat_index_alloc(service_id, &fidx->index)) {
free(fidx);
return ENOSPC;
}
fibril_mutex_initialize(&fidx->lock);
fidx->service_id = service_id;
fidx->pfc = 0;
fidx->pdi = 0;
fidx->nodep = NULL;
*fidxp = fidx;
return EOK;
}
errno_t exfat_idx_get_new(exfat_idx_t **fidxp, service_id_t service_id)
{
exfat_idx_t *fidx;
errno_t rc;
fibril_mutex_lock(&used_lock);
rc = exfat_idx_create(&fidx, service_id);
if (rc != EOK) {
fibril_mutex_unlock(&used_lock);
return rc;
}
hash_table_insert(&ui_hash, &fidx->uih_link);
fibril_mutex_lock(&fidx->lock);
fibril_mutex_unlock(&used_lock);
*fidxp = fidx;
return EOK;
}
exfat_idx_t *
exfat_idx_get_by_pos(service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
{
exfat_idx_t *fidx;
pos_key_t pos_key = {
.service_id = service_id,
.pfc = pfc,
.pdi = pdi,
};
fibril_mutex_lock(&used_lock);
ht_link_t *l = hash_table_find(&up_hash, &pos_key);
if (l) {
fidx = hash_table_get_inst(l, exfat_idx_t, uph_link);
} else {
errno_t rc;
rc = exfat_idx_create(&fidx, service_id);
if (rc != EOK) {
fibril_mutex_unlock(&used_lock);
return NULL;
}
fidx->pfc = pfc;
fidx->pdi = pdi;
hash_table_insert(&up_hash, &fidx->uph_link);
hash_table_insert(&ui_hash, &fidx->uih_link);
}
fibril_mutex_lock(&fidx->lock);
fibril_mutex_unlock(&used_lock);
return fidx;
}
void exfat_idx_hashin(exfat_idx_t *idx)
{
fibril_mutex_lock(&used_lock);
hash_table_insert(&up_hash, &idx->uph_link);
fibril_mutex_unlock(&used_lock);
}
void exfat_idx_hashout(exfat_idx_t *idx)
{
fibril_mutex_lock(&used_lock);
hash_table_remove_item(&up_hash, &idx->uph_link);
fibril_mutex_unlock(&used_lock);
}
exfat_idx_t *
exfat_idx_get_by_index(service_id_t service_id, fs_index_t index)
{
exfat_idx_t *fidx = NULL;
idx_key_t idx_key = {
.service_id = service_id,
.index = index,
};
fibril_mutex_lock(&used_lock);
ht_link_t *l = hash_table_find(&ui_hash, &idx_key);
if (l) {
fidx = hash_table_get_inst(l, exfat_idx_t, uih_link);
fibril_mutex_lock(&fidx->lock);
}
fibril_mutex_unlock(&used_lock);
return fidx;
}
void exfat_idx_destroy(exfat_idx_t *idx)
{
idx_key_t idx_key = {
.service_id = idx->service_id,
.index = idx->index,
};
assert(idx->pfc == 0);
fibril_mutex_lock(&used_lock);
hash_table_remove(&ui_hash, &idx_key);
fibril_mutex_unlock(&used_lock);
exfat_index_free(idx_key.service_id, idx_key.index);
}
errno_t exfat_idx_init(void)
{
if (!hash_table_create(&up_hash, 0, 0, &uph_ops))
return ENOMEM;
if (!hash_table_create(&ui_hash, 0, 0, &uih_ops)) {
hash_table_destroy(&up_hash);
return ENOMEM;
}
return EOK;
}
void exfat_idx_fini(void)
{
assert(hash_table_empty(&up_hash) && hash_table_empty(&ui_hash));
hash_table_destroy(&up_hash);
hash_table_destroy(&ui_hash);
}
errno_t exfat_idx_init_by_service_id(service_id_t service_id)
{
unused_t *u;
errno_t rc = EOK;
u = (unused_t *) malloc(sizeof(unused_t));
if (!u)
return ENOMEM;
unused_initialize(u, service_id);
fibril_mutex_lock(&unused_lock);
if (!unused_find(service_id, false)) {
list_append(&u->link, &unused_list);
} else {
free(u);
rc = EEXIST;
}
fibril_mutex_unlock(&unused_lock);
return rc;
}
static bool rm_pos_service_id(ht_link_t *item, void *arg)
{
service_id_t service_id = *(service_id_t *)arg;
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
if (fidx->service_id == service_id) {
hash_table_remove_item(&up_hash, item);
}
return true;
}
static bool rm_idx_service_id(ht_link_t *item, void *arg)
{
service_id_t service_id = *(service_id_t *)arg;
exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
if (fidx->service_id == service_id) {
hash_table_remove_item(&ui_hash, item);
}
return true;
}
void exfat_idx_fini_by_service_id(service_id_t service_id)
{
fibril_mutex_lock(&used_lock);
hash_table_apply(&up_hash, rm_pos_service_id, &service_id);
hash_table_apply(&ui_hash, rm_idx_service_id, &service_id);
fibril_mutex_unlock(&used_lock);
unused_t *u = unused_find(service_id, true);
assert(u);
list_remove(&u->link);
fibril_mutex_unlock(&unused_lock);
while (!list_empty(&u->freed_list)) {
freed_t *f;
f = list_get_instance(list_first(&u->freed_list), freed_t, link);
list_remove(&f->link);
free(f);
}
free(u);
}
HelenOS homepage, sources at GitHub