HelenOS sources
This source file includes following definitions.
- vfs_files_init
- vfs_files_done
- vfs_client_data_create
- vfs_client_data_destroy
- vfs_file_close_remote
- vfs_file_addref
- vfs_file_delref
- _vfs_fd_alloc
- vfs_fd_alloc
- _vfs_fd_free_locked
- _vfs_fd_free
- vfs_fd_free
- vfs_fd_assign
- _vfs_file_put
- _vfs_file_get
- vfs_file_get
- vfs_file_put
- vfs_op_pass_handle
- vfs_wait_handle_internal
#include <errno.h>
#include <stdlib.h>
#include <str.h>
#include <assert.h>
#include <stdbool.h>
#include <fibril.h>
#include <fibril_synch.h>
#include <adt/list.h>
#include <task.h>
#include <vfs/vfs.h>
#include "vfs.h"
#define VFS_DATA ((vfs_client_data_t *) async_get_client_data())
#define FILES (VFS_DATA->files)
typedef struct {
fibril_mutex_t lock;
fibril_condvar_t cv;
list_t passed_handles;
vfs_file_t **files;
} vfs_client_data_t;
typedef struct {
link_t link;
vfs_node_t *node;
int permissions;
} vfs_boxed_handle_t;
static errno_t _vfs_fd_free(vfs_client_data_t *, int);
static bool vfs_files_init(vfs_client_data_t *vfs_data)
{
fibril_mutex_lock(&vfs_data->lock);
if (!vfs_data->files) {
vfs_data->files = malloc(VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
if (!vfs_data->files) {
fibril_mutex_unlock(&vfs_data->lock);
return false;
}
memset(vfs_data->files, 0, VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
}
fibril_mutex_unlock(&vfs_data->lock);
return true;
}
static void vfs_files_done(vfs_client_data_t *vfs_data)
{
int i;
if (!vfs_data->files)
return;
for (i = 0; i < VFS_MAX_OPEN_FILES; i++) {
if (vfs_data->files[i])
(void) _vfs_fd_free(vfs_data, i);
}
free(vfs_data->files);
while (!list_empty(&vfs_data->passed_handles)) {
link_t *lnk;
vfs_boxed_handle_t *bh;
lnk = list_first(&vfs_data->passed_handles);
list_remove(lnk);
bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
free(bh);
}
}
void *vfs_client_data_create(void)
{
vfs_client_data_t *vfs_data;
vfs_data = malloc(sizeof(vfs_client_data_t));
if (vfs_data) {
fibril_mutex_initialize(&vfs_data->lock);
fibril_condvar_initialize(&vfs_data->cv);
list_initialize(&vfs_data->passed_handles);
vfs_data->files = NULL;
}
return vfs_data;
}
void vfs_client_data_destroy(void *data)
{
vfs_client_data_t *vfs_data = (vfs_client_data_t *) data;
vfs_files_done(vfs_data);
free(vfs_data);
}
static errno_t vfs_file_close_remote(vfs_file_t *file)
{
assert(!file->refcnt);
async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
ipc_call_t answer;
aid_t msg = async_send_2(exch, VFS_OUT_CLOSE, file->node->service_id,
file->node->index, &answer);
vfs_exchange_release(exch);
errno_t rc;
async_wait_for(msg, &rc);
return ipc_get_retval(&answer);
}
static void vfs_file_addref(vfs_client_data_t *vfs_data, vfs_file_t *file)
{
assert(fibril_mutex_is_locked(&vfs_data->lock));
file->refcnt++;
}
static errno_t vfs_file_delref(vfs_client_data_t *vfs_data, vfs_file_t *file)
{
errno_t rc = EOK;
assert(fibril_mutex_is_locked(&vfs_data->lock));
if (file->refcnt-- == 1) {
if (file->node != NULL) {
if (file->open_read || file->open_write) {
rc = vfs_file_close_remote(file);
}
vfs_node_delref(file->node);
}
free(file);
}
return rc;
}
static errno_t _vfs_fd_alloc(vfs_client_data_t *vfs_data, vfs_file_t **file, bool desc, int *out_fd)
{
if (!vfs_files_init(vfs_data))
return ENOMEM;
unsigned int i;
if (desc)
i = VFS_MAX_OPEN_FILES - 1;
else
i = 0;
fibril_mutex_lock(&vfs_data->lock);
while (true) {
if (!vfs_data->files[i]) {
vfs_data->files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
if (!vfs_data->files[i]) {
fibril_mutex_unlock(&vfs_data->lock);
return ENOMEM;
}
memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
fibril_mutex_initialize(&vfs_data->files[i]->_lock);
fibril_mutex_lock(&vfs_data->files[i]->_lock);
vfs_file_addref(vfs_data, vfs_data->files[i]);
*file = vfs_data->files[i];
vfs_file_addref(vfs_data, *file);
fibril_mutex_unlock(&vfs_data->lock);
*out_fd = (int) i;
return EOK;
}
if (desc) {
if (i == 0)
break;
i--;
} else {
if (i == VFS_MAX_OPEN_FILES - 1)
break;
i++;
}
}
fibril_mutex_unlock(&vfs_data->lock);
return EMFILE;
}
errno_t vfs_fd_alloc(vfs_file_t **file, bool desc, int *out_fd)
{
return _vfs_fd_alloc(VFS_DATA, file, desc, out_fd);
}
static errno_t _vfs_fd_free_locked(vfs_client_data_t *vfs_data, int fd)
{
if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES) || !vfs_data->files[fd]) {
return EBADF;
}
errno_t rc = vfs_file_delref(vfs_data, vfs_data->files[fd]);
vfs_data->files[fd] = NULL;
return rc;
}
static errno_t _vfs_fd_free(vfs_client_data_t *vfs_data, int fd)
{
errno_t rc;
if (!vfs_files_init(vfs_data))
return ENOMEM;
fibril_mutex_lock(&vfs_data->lock);
rc = _vfs_fd_free_locked(vfs_data, fd);
fibril_mutex_unlock(&vfs_data->lock);
return rc;
}
errno_t vfs_fd_free(int fd)
{
return _vfs_fd_free(VFS_DATA, fd);
}
errno_t vfs_fd_assign(vfs_file_t *file, int fd)
{
if (!vfs_files_init(VFS_DATA))
return ENOMEM;
fibril_mutex_lock(&VFS_DATA->lock);
if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES)) {
fibril_mutex_unlock(&VFS_DATA->lock);
return EBADF;
}
(void) _vfs_fd_free_locked(VFS_DATA, fd);
assert(FILES[fd] == NULL);
FILES[fd] = file;
vfs_file_addref(VFS_DATA, FILES[fd]);
fibril_mutex_unlock(&VFS_DATA->lock);
return EOK;
}
static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
{
fibril_mutex_unlock(&file->_lock);
fibril_mutex_lock(&vfs_data->lock);
vfs_file_delref(vfs_data, file);
fibril_mutex_unlock(&vfs_data->lock);
}
static vfs_file_t *_vfs_file_get(vfs_client_data_t *vfs_data, int fd)
{
if (!vfs_files_init(vfs_data))
return NULL;
fibril_mutex_lock(&vfs_data->lock);
if ((fd >= 0) && (fd < VFS_MAX_OPEN_FILES)) {
vfs_file_t *file = vfs_data->files[fd];
if (file != NULL) {
vfs_file_addref(vfs_data, file);
fibril_mutex_unlock(&vfs_data->lock);
fibril_mutex_lock(&file->_lock);
if (file->node == NULL) {
_vfs_file_put(vfs_data, file);
return NULL;
}
assert(file != NULL);
assert(file->node != NULL);
return file;
}
}
fibril_mutex_unlock(&vfs_data->lock);
return NULL;
}
vfs_file_t *vfs_file_get(int fd)
{
return _vfs_file_get(VFS_DATA, fd);
}
void vfs_file_put(vfs_file_t *file)
{
_vfs_file_put(VFS_DATA, file);
}
void vfs_op_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
{
vfs_client_data_t *donor_data = NULL;
vfs_client_data_t *acceptor_data = NULL;
vfs_file_t *donor_file = NULL;
vfs_boxed_handle_t *bh;
acceptor_data = async_get_client_data_by_id(acceptor_id);
if (!acceptor_data)
return;
bh = malloc(sizeof(vfs_boxed_handle_t));
assert(bh);
link_initialize(&bh->link);
bh->node = NULL;
donor_data = async_get_client_data_by_id(donor_id);
if (!donor_data)
goto out;
donor_file = _vfs_file_get(donor_data, donor_fd);
if (!donor_file)
goto out;
vfs_node_addref(donor_file->node);
bh->node = donor_file->node;
bh->permissions = donor_file->permissions;
out:
fibril_mutex_lock(&acceptor_data->lock);
list_append(&bh->link, &acceptor_data->passed_handles);
fibril_condvar_broadcast(&acceptor_data->cv);
fibril_mutex_unlock(&acceptor_data->lock);
if (donor_data)
async_put_client_data_by_id(donor_id);
if (acceptor_data)
async_put_client_data_by_id(acceptor_id);
if (donor_file)
_vfs_file_put(donor_data, donor_file);
}
errno_t vfs_wait_handle_internal(bool high_fd, int *out_fd)
{
vfs_client_data_t *vfs_data = VFS_DATA;
fibril_mutex_lock(&vfs_data->lock);
while (list_empty(&vfs_data->passed_handles))
fibril_condvar_wait(&vfs_data->cv, &vfs_data->lock);
link_t *lnk = list_first(&vfs_data->passed_handles);
list_remove(lnk);
fibril_mutex_unlock(&vfs_data->lock);
vfs_boxed_handle_t *bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
vfs_file_t *file;
errno_t rc = _vfs_fd_alloc(vfs_data, &file, high_fd, out_fd);
if (rc != EOK) {
vfs_node_delref(bh->node);
free(bh);
return rc;
}
file->node = bh->node;
file->permissions = bh->permissions;
vfs_file_put(file);
free(bh);
return EOK;
}
HelenOS homepage, sources at GitHub