45,24 → 45,102 |
#include <stdlib.h> |
#include <string.h> |
#include <stdio.h> |
#include <dirent.h> |
#include <assert.h> |
#include <sys/types.h> |
#include <libadt/hash_table.h> |
#include <as.h> |
#include <libfs.h> |
|
#define min(a, b) ((a) < (b) ? (a) : (b)) |
#define max(a, b) ((a) > (b) ? (a) : (b)) |
|
#define PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE]) |
|
#define DENTRIES_BUCKETS 256 |
|
#define NAMES_BUCKETS 4 |
|
/* |
* Hash table of all directory entries. |
* For now, we don't distinguish between different dev_handles/instances. All |
* requests resolve to the only instance, rooted in the following variable. |
*/ |
static tmpfs_dentry_t *root; |
|
/* |
* Implementation of the libfs interface. |
*/ |
|
/* Forward declarations of static functions. */ |
static void *tmpfs_match(void *, const char *); |
static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
static void tmpfs_node_put(void *); |
static void *tmpfs_create_node(int); |
static bool tmpfs_link_node(void *, void *, const char *); |
static int tmpfs_unlink_node(void *, void *); |
static int tmpfs_destroy_node(void *); |
|
/* Implementation of helper functions. */ |
static fs_index_t tmpfs_index_get(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->index; |
} |
|
static size_t tmpfs_size_get(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->size; |
} |
|
static unsigned tmpfs_lnkcnt_get(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
} |
|
static bool tmpfs_has_children(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->child != NULL; |
} |
|
static void *tmpfs_root_get(dev_handle_t dev_handle) |
{ |
return root; |
} |
|
static char tmpfs_plb_get_char(unsigned pos) |
{ |
return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
} |
|
static bool tmpfs_is_directory(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
} |
|
static bool tmpfs_is_file(void *nodep) |
{ |
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
} |
|
/** libfs operations */ |
libfs_ops_t tmpfs_libfs_ops = { |
.match = tmpfs_match, |
.node_get = tmpfs_node_get, |
.node_put = tmpfs_node_put, |
.create = tmpfs_create_node, |
.destroy = tmpfs_destroy_node, |
.link = tmpfs_link_node, |
.unlink = tmpfs_unlink_node, |
.index_get = tmpfs_index_get, |
.size_get = tmpfs_size_get, |
.lnkcnt_get = tmpfs_lnkcnt_get, |
.has_children = tmpfs_has_children, |
.root_get = tmpfs_root_get, |
.plb_get_char = tmpfs_plb_get_char, |
.is_directory = tmpfs_is_directory, |
.is_file = tmpfs_is_file |
}; |
|
/** Hash table of all directory entries. */ |
hash_table_t dentries; |
|
/* Implementation of hash table interface for the dentries hash table. */ |
static hash_index_t dentries_hash(unsigned long *key) |
{ |
return *key % DENTRIES_BUCKETS; |
87,255 → 165,282 |
.remove_callback = dentries_remove_callback |
}; |
|
unsigned tmpfs_next_index = 1; |
fs_index_t tmpfs_next_index = 1; |
|
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
typedef struct { |
char *name; |
tmpfs_dentry_t *parent; |
link_t link; |
} tmpfs_name_t; |
|
/* Implementation of hash table interface for the names hash table. */ |
static hash_index_t names_hash(unsigned long *key) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
return dentry->index % NAMES_BUCKETS; |
} |
|
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
link); |
return dentry == namep->parent; |
} |
|
static void names_remove_callback(link_t *item) |
{ |
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
link); |
free(namep->name); |
free(namep); |
} |
|
/** TMPFS node names hash table operations. */ |
static hash_table_operations_t names_ops = { |
.hash = names_hash, |
.compare = names_compare, |
.remove_callback = names_remove_callback |
}; |
|
static void tmpfs_name_initialize(tmpfs_name_t *namep) |
{ |
namep->name = NULL; |
namep->parent = NULL; |
link_initialize(&namep->link); |
} |
|
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
{ |
dentry->index = 0; |
dentry->parent = NULL; |
dentry->sibling = NULL; |
dentry->child = NULL; |
dentry->name = NULL; |
dentry->type = TMPFS_NONE; |
dentry->lnkcnt = 0; |
dentry->size = 0; |
dentry->data = NULL; |
link_initialize(&dentry->dh_link); |
return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
&names_ops); |
} |
|
/* |
* For now, we don't distinguish between different dev_handles/instances. All |
* requests resolve to the only instance, rooted in the following variable. |
*/ |
static tmpfs_dentry_t *root; |
|
static bool tmpfs_init(void) |
{ |
if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
return false; |
|
root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
if (!root) |
root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
if (!root) { |
hash_table_destroy(&dentries); |
return false; |
tmpfs_dentry_initialize(root); |
root->index = tmpfs_next_index++; |
root->name = ""; |
root->type = TMPFS_DIRECTORY; |
hash_table_insert(&dentries, &root->index, &root->dh_link); |
|
} |
root->lnkcnt = 0; /* FS root is not linked */ |
return true; |
} |
|
/** Compare one component of path to a directory entry. |
* |
* @param dentry Directory entry to compare the path component with. |
* @param parentp Pointer to node from which we descended. |
* @param childp Pointer to node to compare the path component with. |
* @param component Array of characters holding component name. |
* |
* @return True on match, false otherwise. |
*/ |
static bool match_component(tmpfs_dentry_t *dentry, const char *component) |
static bool |
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
const char *component) |
{ |
return !strcmp(dentry->name, component); |
unsigned long key = (unsigned long) parentp; |
link_t *hlp = hash_table_find(&childp->names, &key); |
assert(hlp); |
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
return !strcmp(namep->name, component); |
} |
|
static unsigned long create_node(tmpfs_dentry_t *dentry, |
const char *component, int lflag) |
void *tmpfs_match(void *prnt, const char *component) |
{ |
assert(dentry->type == TMPFS_DIRECTORY); |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
tmpfs_dentry_t *childp = parentp->child; |
|
while (childp && !tmpfs_match_one(parentp, childp, component)) |
childp = childp->sibling; |
|
return (void *) childp; |
} |
|
void * |
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
unsigned long key = index; |
link_t *lnk = hash_table_find(&dentries, &key); |
if (!lnk) |
return NULL; |
return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
} |
|
void tmpfs_node_put(void *node) |
{ |
/* nothing to do */ |
} |
|
void *tmpfs_create_node(int lflag) |
{ |
assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
|
tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
if (!node) |
return 0; |
size_t len = strlen(component); |
char *name = malloc(len + 1); |
if (!name) { |
return NULL; |
|
if (!tmpfs_dentry_initialize(node)) { |
free(node); |
return 0; |
return NULL; |
} |
strcpy(name, component); |
|
tmpfs_dentry_initialize(node); |
node->index = tmpfs_next_index++; |
node->name = name; |
node->parent = dentry; |
if (lflag & L_DIRECTORY) |
node->type = TMPFS_DIRECTORY; |
else |
node->type = TMPFS_FILE; |
|
/* Insert the new node into the dentry hash table. */ |
unsigned long key = node->index; |
hash_table_insert(&dentries, &key, &node->dh_link); |
return (void *) node; |
} |
|
bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
{ |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
|
assert(parentp->type == TMPFS_DIRECTORY); |
|
tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
if (!namep) |
return false; |
tmpfs_name_initialize(namep); |
size_t len = strlen(nm); |
namep->name = malloc(len + 1); |
if (!namep->name) { |
free(namep); |
return false; |
} |
strcpy(namep->name, nm); |
namep->parent = parentp; |
|
childp->lnkcnt++; |
|
unsigned long key = (unsigned long) parentp; |
hash_table_insert(&childp->names, &key, &namep->link); |
|
/* Insert the new node into the namespace. */ |
if (dentry->child) { |
tmpfs_dentry_t *tmp = dentry->child; |
if (parentp->child) { |
tmpfs_dentry_t *tmp = parentp->child; |
while (tmp->sibling) |
tmp = tmp->sibling; |
tmp->sibling = node; |
tmp->sibling = childp; |
} else { |
dentry->child = node; |
parentp->child = childp; |
} |
|
/* Insert the new node into the dentry hash table. */ |
hash_table_insert(&dentries, &node->index, &node->dh_link); |
return node->index; |
return true; |
} |
|
static int destroy_component(tmpfs_dentry_t *dentry) |
int tmpfs_unlink_node(void *prnt, void *chld) |
{ |
return EPERM; |
} |
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
|
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
unsigned next = IPC_GET_ARG1(*request); |
unsigned last = IPC_GET_ARG2(*request); |
int dev_handle = IPC_GET_ARG3(*request); |
int lflag = IPC_GET_ARG4(*request); |
if (!parentp) |
return EBUSY; |
|
if (last < next) |
last += PLB_SIZE; |
if (childp->child) |
return ENOTEMPTY; |
|
/* |
* Initialize TMPFS. |
*/ |
if (!root && !tmpfs_init()) { |
ipc_answer_0(rid, ENOMEM); |
return; |
if (parentp->child == childp) { |
parentp->child = childp->sibling; |
} else { |
/* TODO: consider doubly linked list for organizing siblings. */ |
tmpfs_dentry_t *tmp = parentp->child; |
while (tmp->sibling != childp) |
tmp = tmp->sibling; |
tmp->sibling = childp->sibling; |
} |
childp->sibling = NULL; |
|
tmpfs_dentry_t *dtmp = root->child; |
tmpfs_dentry_t *dcur = root; |
unsigned long key = (unsigned long) parentp; |
hash_table_remove(&childp->names, &key, 1); |
|
if (PLB_GET_CHAR(next) == '/') |
next++; /* eat slash */ |
|
char component[NAME_MAX + 1]; |
int len = 0; |
while (dtmp && next <= last) { |
childp->lnkcnt--; |
|
/* collect the component */ |
if (PLB_GET_CHAR(next) != '/') { |
if (len + 1 == NAME_MAX) { |
/* comopnent length overflow */ |
ipc_answer_0(rid, ENAMETOOLONG); |
return; |
} |
component[len++] = PLB_GET_CHAR(next); |
next++; /* process next character */ |
if (next <= last) |
continue; |
} |
return EOK; |
} |
|
assert(len); |
component[len] = '\0'; |
next++; /* eat slash */ |
len = 0; |
int tmpfs_destroy_node(void *nodep) |
{ |
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
|
assert(!dentry->lnkcnt); |
assert(!dentry->child); |
assert(!dentry->sibling); |
|
/* match the component */ |
while (dtmp && !match_component(dtmp, component)) |
dtmp = dtmp->sibling; |
unsigned long key = dentry->index; |
hash_table_remove(&dentries, &key, 1); |
|
/* handle miss: match amongst siblings */ |
if (!dtmp) { |
if ((next > last) && (lflag & L_CREATE)) { |
/* no components left and L_CREATE specified */ |
if (dcur->type != TMPFS_DIRECTORY) { |
ipc_answer_0(rid, ENOTDIR); |
return; |
} |
unsigned long index = create_node(dcur, |
component, lflag); |
if (index > 0) { |
ipc_answer_4(rid, EOK, |
tmpfs_reg.fs_handle, dev_handle, |
index, 0); |
} else { |
ipc_answer_0(rid, ENOSPC); |
} |
return; |
} |
ipc_answer_0(rid, ENOENT); |
return; |
} |
hash_table_destroy(&dentry->names); |
|
/* descend one level */ |
dcur = dtmp; |
dtmp = dtmp->child; |
} |
if (dentry->type == TMPFS_FILE) |
free(dentry->data); |
free(dentry); |
return EOK; |
} |
|
/* handle miss: excessive components */ |
if (!dtmp && next <= last) { |
if (lflag & L_CREATE) { |
if (dcur->type != TMPFS_DIRECTORY) { |
ipc_answer_0(rid, ENOTDIR); |
return; |
} |
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
|
/* collect next component */ |
while (next <= last) { |
if (PLB_GET_CHAR(next) == '/') { |
/* more than one component */ |
ipc_answer_0(rid, ENOENT); |
return; |
} |
if (len + 1 == NAME_MAX) { |
/* component length overflow */ |
ipc_answer_0(rid, ENAMETOOLONG); |
return; |
} |
component[len++] = PLB_GET_CHAR(next); |
next++; /* process next character */ |
} |
assert(len); |
component[len] = '\0'; |
len = 0; |
|
unsigned long index; |
index = create_node(dcur, component, lflag); |
if (index) { |
ipc_answer_4(rid, EOK, tmpfs_reg.fs_handle, |
dev_handle, index, 0); |
} else { |
ipc_answer_0(rid, ENOSPC); |
} |
return; |
} |
ipc_answer_0(rid, ENOENT); |
/* Initialize TMPFS. */ |
if (!root && !tmpfs_init()) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* handle hit */ |
if (lflag & L_DESTROY) { |
int res = destroy_component(dcur); |
ipc_answer_0(rid, res); |
return; |
if (dev_handle >= 0) { |
if (tmpfs_restore(dev_handle)) |
ipc_answer_3(rid, EOK, root->index, root->size, |
root->lnkcnt); |
else |
ipc_answer_0(rid, ELIMIT); |
} else { |
ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt); |
} |
if ((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) { |
ipc_answer_0(rid, EEXIST); |
return; |
} |
if ((lflag & L_FILE) && (dcur->type != TMPFS_FILE)) { |
ipc_answer_0(rid, EISDIR); |
return; |
} |
if ((lflag & L_DIRECTORY) && (dcur->type != TMPFS_DIRECTORY)) { |
ipc_answer_0(rid, ENOTDIR); |
return; |
} |
} |
|
ipc_answer_4(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index, |
dcur->size); |
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request); |
fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
|
ipc_answer_0(rid, ENOTSUP); |
} |
|
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
|
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
{ |
int dev_handle = IPC_GET_ARG1(*request); |
unsigned long index = IPC_GET_ARG2(*request); |
off_t pos = IPC_GET_ARG3(*request); |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
|
/* |
* Lookup the respective dentry. |
*/ |
link_t *hlp; |
hlp = hash_table_find(&dentries, &index); |
unsigned long key = index; |
hlp = hash_table_find(&dentries, &key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
361,7 → 466,7 |
bytes); |
} else { |
int i; |
tmpfs_dentry_t *cur = dentry->child; |
tmpfs_dentry_t *cur; |
|
assert(dentry->type == TMPFS_DIRECTORY); |
|
380,8 → 485,14 |
return; |
} |
|
(void) ipc_data_read_finalize(callid, cur->name, |
strlen(cur->name) + 1); |
unsigned long key = (unsigned long) dentry; |
link_t *hlp = hash_table_find(&cur->names, &key); |
assert(hlp); |
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
link); |
|
(void) ipc_data_read_finalize(callid, namep->name, |
strlen(namep->name) + 1); |
bytes = 1; |
} |
|
393,15 → 504,16 |
|
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
{ |
int dev_handle = IPC_GET_ARG1(*request); |
unsigned long index = IPC_GET_ARG2(*request); |
off_t pos = IPC_GET_ARG3(*request); |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
|
/* |
* Lookup the respective dentry. |
*/ |
link_t *hlp; |
hlp = hash_table_find(&dentries, &index); |
unsigned long key = index; |
hlp = hash_table_find(&dentries, &key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
453,15 → 565,16 |
|
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
{ |
int dev_handle = IPC_GET_ARG1(*request); |
unsigned long index = IPC_GET_ARG2(*request); |
size_t size = IPC_GET_ARG3(*request); |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
size_t size = (off_t)IPC_GET_ARG3(*request); |
|
/* |
* Lookup the respective dentry. |
*/ |
link_t *hlp; |
hlp = hash_table_find(&dentries, &index); |
unsigned long key = index; |
hlp = hash_table_find(&dentries, &key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
488,6 → 601,25 |
ipc_answer_0(rid, EOK); |
} |
|
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
int rc; |
|
link_t *hlp; |
unsigned long key = index; |
hlp = hash_table_find(&dentries, &key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
dh_link); |
rc = tmpfs_destroy_node(dentry); |
ipc_answer_0(rid, rc); |
} |
|
/** |
* @} |
*/ |