Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2595 → Rev 2596

/trunk/uspace/srv/vfs/vfs_node.c
36,7 → 36,50
*/
 
#include "vfs.h"
#include <stdlib.h>
#include <string.h>
#include <atomic.h>
#include <futex.h>
#include <libadt/hash_table.h>
 
/** Futex protecting the VFS node hash table. */
atomic_t nodes_futex = FUTEX_INITIALIZER;
 
#define NODES_BUCKETS_LOG 8
#define NODES_BUCKETS (1 << NODES_BUCKETS_LOG)
 
/** VFS node hash table containing all active, in-memory VFS nodes. */
hash_table_t nodes;
 
#define KEY_FS_HANDLE 0
#define KEY_DEV_HANDLE 1
#define KEY_INDEX 2
 
static hash_index_t nodes_hash(unsigned long []);
static int nodes_compare(unsigned long [], hash_count_t, link_t *);
static void nodes_remove_callback(link_t *);
 
/** VFS node hash table operations. */
hash_table_operations_t nodes_ops = {
.hash = nodes_hash,
.compare = nodes_compare,
.remove_callback = nodes_remove_callback
};
 
/** Initialize the VFS node hash table.
*
* @return Return true on success, false on failure.
*/
bool vfs_nodes_init(void)
{
return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops);
}
 
static inline void _vfs_node_addref(vfs_node_t *node)
{
node->refcnt++;
}
 
/** Increment reference count of a VFS node.
*
* @param node VFS node that will have its refcnt incremented.
43,7 → 86,9
*/
void vfs_node_addref(vfs_node_t *node)
{
/* TODO */
futex_down(&nodes_futex);
_vfs_node_addref(node);
futex_up(&nodes_futex);
}
 
/** Decrement reference count of a VFS node.
54,7 → 99,16
*/
void vfs_node_delref(vfs_node_t *node)
{
/* TODO */
futex_down(&nodes_futex);
if (node->refcnt-- == 1) {
unsigned long key[] = {
[KEY_FS_HANDLE] = node->fs_handle,
[KEY_DEV_HANDLE] = node->dev_handle,
[KEY_INDEX] = node->index
};
hash_table_remove(&nodes, key, 3);
}
futex_up(&nodes_futex);
}
 
/** Find VFS node.
71,8 → 125,35
*/
vfs_node_t *vfs_node_get(vfs_triplet_t *triplet)
{
/* TODO */
return NULL;
unsigned long key[] = {
[KEY_FS_HANDLE] = triplet->fs_handle,
[KEY_DEV_HANDLE] = triplet->dev_handle,
[KEY_INDEX] = triplet->index
};
link_t *tmp;
vfs_node_t *node;
 
futex_down(&nodes_futex);
tmp = hash_table_find(&nodes, key);
if (!tmp) {
node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
if (!node) {
futex_up(&nodes_futex);
return NULL;
}
memset(node, 0, sizeof(vfs_node_t));
node->fs_handle = triplet->fs_handle;
node->dev_handle = triplet->fs_handle;
node->index = triplet->index;
link_initialize(&node->nh_link);
hash_table_insert(&nodes, key, &node->nh_link);
} else {
node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
}
_vfs_node_addref(node);
futex_up(&nodes_futex);
 
return node;
}
 
/** Return VFS node when no longer needed by the caller.
88,6 → 169,28
vfs_node_delref(node);
}
 
hash_index_t nodes_hash(unsigned long key[])
{
hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4);
hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2);
return (b | key[KEY_INDEX]) & ~(NODES_BUCKETS - 1);
}
 
int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
{
vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
return (node->fs_handle == key[KEY_FS_HANDLE]) &&
(node->dev_handle == key[KEY_DEV_HANDLE]) &&
(node->index == key[KEY_INDEX]);
}
 
void nodes_remove_callback(link_t *item)
{
vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
free(node);
}
 
/**
* @}
*/