Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3352 → Rev 3386

/branches/network/uspace/srv/vfs/vfs_ops.c
35,6 → 35,7
* @brief Operations that VFS offers to its clients.
*/
 
#include "vfs.h"
#include <ipc/ipc.h>
#include <async.h>
#include <errno.h>
49,9 → 50,11
#include <ctype.h>
#include <fcntl.h>
#include <assert.h>
#include <atomic.h>
#include "vfs.h"
#include <vfs/canonify.h>
 
/* Forward declarations of static functions. */
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
 
/**
* This rwlock prevents the race between a triplet-to-VFS-node resolution and a
* concurrent VFS operation which modifies the file system namespace.
58,28 → 61,21
*/
RWLOCK_INITIALIZE(namespace_rwlock);
 
atomic_t rootfs_futex = FUTEX_INITIALIZER;
vfs_triplet_t rootfs = {
futex_t rootfs_futex = FUTEX_INITIALIZER;
vfs_pair_t rootfs = {
.fs_handle = 0,
.dev_handle = 0,
.index = 0,
.dev_handle = 0
};
 
static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
{
vfs_pair_t altroot = {
.fs_handle = fs_handle,
.dev_handle = dev_handle,
};
 
return vfs_lookup_internal("/", strlen("/"), L_DIRECTORY, result,
&altroot);
}
 
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
{
int dev_handle;
dev_handle_t dev_handle;
vfs_node_t *mp_node = NULL;
ipc_callid_t callid;
ipc_call_t data;
int rc;
int phone;
size_t size;
 
/*
* We expect the library to do the device-name to device-handle
86,7 → 82,7
* translation for us, thus the device handle will arrive as ARG1
* in the request.
*/
dev_handle = IPC_GET_ARG1(*request);
dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
 
/*
* For now, don't make use of ARG2 and ARG3, but they can be used to
93,9 → 89,6
* carry mount options in the future.
*/
 
ipc_callid_t callid;
size_t size;
 
/*
* Now, we expect the client to send us data with the name of the file
* system.
122,15 → 115,30
fs_name[size] = '\0';
/*
* Wait for IPC_M_PING so that we can return an error if we don't know
* fs_name.
*/
callid = async_get_call(&data);
if (IPC_GET_METHOD(data) != IPC_M_PING) {
ipc_answer_0(callid, ENOTSUP);
ipc_answer_0(rid, ENOTSUP);
return;
}
 
/*
* Check if we know a file system with the same name as is in fs_name.
* This will also give us its file system handle.
*/
int fs_handle = fs_name_to_handle(fs_name, true);
fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
if (!fs_handle) {
ipc_answer_0(callid, ENOENT);
ipc_answer_0(rid, ENOENT);
return;
}
 
/* Acknowledge that we know fs_name. */
ipc_answer_0(callid, EOK);
 
/* Now, we want the client to send us the mount point. */
if (!ipc_data_write_receive(&callid, &size)) {
ipc_answer_0(callid, EINVAL);
146,7 → 154,7
}
/* Allocate buffer for the mount point data being received. */
uint8_t *buf;
buf = malloc(size);
buf = malloc(size + 1);
if (!buf) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
155,41 → 163,27
 
/* Deliver the mount point. */
(void) ipc_data_write_finalize(callid, buf, size);
buf[size] = '\0';
 
/*
* Lookup the root node of the filesystem being mounted.
* In this case, we don't need to take the namespace_futex as the root
* node cannot be removed. However, we do take a reference to it so
* that we can track how many times it has been mounted.
*/
int rc;
vfs_lookup_res_t mr_res;
rc = lookup_root(fs_handle, dev_handle, &mr_res);
if (rc != EOK) {
free(buf);
ipc_answer_0(rid, rc);
return;
}
vfs_node_t *mr_node = vfs_node_get(&mr_res);
if (!mr_node) {
free(buf);
ipc_answer_0(rid, ENOMEM);
return;
}
 
/* Finally, we need to resolve the path to the mountpoint. */
/* Resolve the path to the mountpoint. */
vfs_lookup_res_t mp_res;
futex_down(&rootfs_futex);
if (rootfs.fs_handle) {
/* We already have the root FS. */
rwlock_write_lock(&namespace_rwlock);
rc = vfs_lookup_internal(buf, size, L_DIRECTORY, &mp_res,
NULL);
if ((size == 1) && (buf[0] == '/')) {
/* Trying to mount root FS over root FS */
rwlock_write_unlock(&namespace_rwlock);
futex_up(&rootfs_futex);
free(buf);
ipc_answer_0(rid, EBUSY);
return;
}
rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL);
if (rc != EOK) {
/* The lookup failed for some reason. */
rwlock_write_unlock(&namespace_rwlock);
futex_up(&rootfs_futex);
vfs_node_put(mr_node); /* failed -> drop reference */
free(buf);
ipc_answer_0(rid, rc);
return;
198,7 → 192,6
if (!mp_node) {
rwlock_write_unlock(&namespace_rwlock);
futex_up(&rootfs_futex);
vfs_node_put(mr_node); /* failed -> drop reference */
free(buf);
ipc_answer_0(rid, ENOMEM);
return;
212,11 → 205,45
} else {
/* We still don't have the root file system mounted. */
if ((size == 1) && (buf[0] == '/')) {
/* For this simple, but important case, we are done. */
rootfs = mr_res.triplet;
vfs_lookup_res_t mr_res;
vfs_node_t *mr_node;
ipcarg_t rindex;
ipcarg_t rsize;
ipcarg_t rlnkcnt;
/*
* For this simple, but important case,
* we are almost done.
*/
free(buf);
/* Tell the mountee that it is being mounted. */
phone = vfs_grab_phone(fs_handle);
rc = async_req_1_3(phone, VFS_MOUNTED,
(ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
vfs_release_phone(phone);
if (rc != EOK) {
futex_up(&rootfs_futex);
ipc_answer_0(rid, rc);
return;
}
 
mr_res.triplet.fs_handle = fs_handle;
mr_res.triplet.dev_handle = dev_handle;
mr_res.triplet.index = (fs_index_t) rindex;
mr_res.size = (size_t) rsize;
mr_res.lnkcnt = (unsigned) rlnkcnt;
 
rootfs.fs_handle = fs_handle;
rootfs.dev_handle = dev_handle;
futex_up(&rootfs_futex);
free(buf);
ipc_answer_0(rid, EOK);
 
/* Add reference to the mounted root. */
mr_node = vfs_node_get(&mr_res);
assert(mr_node);
 
ipc_answer_0(rid, rc);
return;
} else {
/*
225,7 → 252,6
*/
futex_up(&rootfs_futex);
free(buf);
vfs_node_put(mr_node); /* failed -> drop reference */
ipc_answer_0(rid, ENOENT);
return;
}
236,40 → 262,24
/*
* At this point, we have all necessary pieces: file system and device
* handles, and we know the mount point VFS node and also the root node
* of the file system being mounted.
* handles, and we know the mount point VFS node.
*/
 
int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
/* Later we can use ARG3 to pass mode/flags. */
aid_t req1 = async_send_3(phone, VFS_MOUNT,
phone = vfs_grab_phone(mp_res.triplet.fs_handle);
rc = async_req_4_0(phone, VFS_MOUNT,
(ipcarg_t) mp_res.triplet.dev_handle,
(ipcarg_t) mp_res.triplet.index, 0, NULL);
/* The second call uses the same method. */
aid_t req2 = async_send_3(phone, VFS_MOUNT,
(ipcarg_t) mr_res.triplet.fs_handle,
(ipcarg_t) mr_res.triplet.dev_handle,
(ipcarg_t) mr_res.triplet.index, NULL);
(ipcarg_t) mp_res.triplet.index,
(ipcarg_t) fs_handle,
(ipcarg_t) dev_handle);
vfs_release_phone(phone);
 
ipcarg_t rc1;
ipcarg_t rc2;
async_wait_for(req1, &rc1);
async_wait_for(req2, &rc2);
 
if ((rc1 != EOK) || (rc2 != EOK)) {
/* Mount failed, drop references to mr_node and mp_node. */
vfs_node_put(mr_node);
if (rc != EOK) {
/* Mount failed, drop reference to mp_node. */
if (mp_node)
vfs_node_put(mp_node);
}
if (rc2 == EOK)
ipc_answer_0(rid, rc1);
else if (rc1 == EOK)
ipc_answer_0(rid, rc2);
else
ipc_answer_0(rid, rc1);
ipc_answer_0(rid, rc);
}
 
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
304,21 → 314,12
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Now we are on the verge of accepting the path.
*
* There is one optimization we could do in the future: copy the path
* directly into the PLB using some kind of a callback.
*/
char *path = malloc(len);
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
 
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
325,6 → 326,7
free(path);
return;
}
path[len] = '\0';
/*
* Avoid the race condition in which the file can be deleted before we
338,7 → 340,7
 
/* The path is now populated and we can call vfs_lookup_internal(). */
vfs_lookup_res_t lr;
rc = vfs_lookup_internal(path, len, lflag, &lr, NULL);
rc = vfs_lookup_internal(path, lflag, &lr, NULL);
if (rc) {
if (lflag & L_CREATE)
rwlock_write_unlock(&namespace_rwlock);
349,7 → 351,7
return;
}
 
/** Path is no longer needed. */
/* Path is no longer needed. */
free(path);
 
vfs_node_t *node = vfs_node_get(&lr);
358,6 → 360,23
else
rwlock_read_unlock(&namespace_rwlock);
 
/* Truncate the file if requested and if necessary. */
if (oflag & O_TRUNC) {
rwlock_write_lock(&node->contents_rwlock);
if (node->size) {
rc = vfs_truncate_internal(node->fs_handle,
node->dev_handle, node->index, 0);
if (rc) {
rwlock_write_unlock(&node->contents_rwlock);
vfs_node_put(node);
ipc_answer_0(rid, rc);
return;
}
node->size = 0;
}
rwlock_write_unlock(&node->contents_rwlock);
}
 
/*
* Get ourselves a file descriptor and the corresponding vfs_file_t
* structure.
387,6 → 406,13
ipc_answer_1(rid, EOK, fd);
}
 
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
{
int fd = IPC_GET_ARG1(*request);
int rc = vfs_fd_free(fd);
ipc_answer_0(rid, rc);
}
 
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
{
 
401,7 → 427,7
*/
 
int fd = IPC_GET_ARG1(*request);
 
/* Lookup the file structure corresponding to the file descriptor. */
vfs_file_t *file = vfs_file_get(fd);
if (!file) {
408,7 → 434,7
ipc_answer_0(rid, ENOENT);
return;
}
 
/*
* Now we need to receive a call with client's
* IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
424,13 → 450,13
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Lock the open file structure so that no other thread can manipulate
* the same open file at a time.
*/
futex_down(&file->lock);
 
/*
* Lock the file's node so that no other client can read/write to it at
* the same time.
439,7 → 465,7
rwlock_read_lock(&file->node->contents_rwlock);
else
rwlock_write_lock(&file->node->contents_rwlock);
 
int fs_phone = vfs_grab_phone(file->node->fs_handle);
/* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
457,14 → 483,14
* don't have to bother.
*/
ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
 
vfs_release_phone(fs_phone);
 
/* Wait for reply from the FS server. */
ipcarg_t rc;
async_wait_for(msg, &rc);
size_t bytes = IPC_GET_ARG1(answer);
 
/* Unlock the VFS node. */
if (read)
rwlock_read_unlock(&file->node->contents_rwlock);
474,12 → 500,12
file->node->size = IPC_GET_ARG2(answer);
rwlock_write_unlock(&file->node->contents_rwlock);
}
 
/* Update the position pointer and unlock the open file. */
if (rc == EOK)
file->pos += bytes;
futex_up(&file->lock);
 
/*
* FS server's reply is the final result of the whole operation we
* return to the client.
549,11 → 575,25
ipc_answer_0(rid, EINVAL);
}
 
int
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
fs_index_t index, size_t size)
{
ipcarg_t rc;
int fs_phone;
fs_phone = vfs_grab_phone(fs_handle);
rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
(ipcarg_t)index, (ipcarg_t)size);
vfs_release_phone(fs_phone);
return (int)rc;
}
 
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
{
int fd = IPC_GET_ARG1(*request);
size_t size = IPC_GET_ARG2(*request);
ipcarg_t rc;
int rc;
 
vfs_file_t *file = vfs_file_get(fd);
if (!file) {
563,24 → 603,21
futex_down(&file->lock);
 
rwlock_write_lock(&file->node->contents_rwlock);
int fs_phone = vfs_grab_phone(file->node->fs_handle);
rc = async_req_3_0(fs_phone, VFS_TRUNCATE,
(ipcarg_t)file->node->dev_handle, (ipcarg_t)file->node->index,
(ipcarg_t)size);
vfs_release_phone(fs_phone);
rc = vfs_truncate_internal(file->node->fs_handle,
file->node->dev_handle, file->node->index, size);
if (rc == EOK)
file->node->size = size;
rwlock_write_unlock(&file->node->contents_rwlock);
 
futex_up(&file->lock);
ipc_answer_0(rid, rc);
ipc_answer_0(rid, (ipcarg_t)rc);
}
 
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
{
int mode = IPC_GET_ARG1(*request);
 
size_t len;
 
ipc_callid_t callid;
 
if (!ipc_data_write_receive(&callid, &len)) {
588,21 → 625,12
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Now we are on the verge of accepting the path.
*
* There is one optimization we could do in the future: copy the path
* directly into the PLB using some kind of a callback.
*/
char *path = malloc(len);
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
 
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
609,15 → 637,234
free(path);
return;
}
path[len] = '\0';
rwlock_write_lock(&namespace_rwlock);
int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
rc = vfs_lookup_internal(path, len, lflag, NULL, NULL);
rc = vfs_lookup_internal(path, lflag, NULL, NULL);
rwlock_write_unlock(&namespace_rwlock);
free(path);
ipc_answer_0(rid, rc);
}
 
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
{
int lflag = IPC_GET_ARG1(*request);
 
size_t len;
ipc_callid_t callid;
 
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
}
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
free(path);
return;
}
path[len] = '\0';
rwlock_write_lock(&namespace_rwlock);
lflag &= L_DIRECTORY; /* sanitize lflag */
vfs_lookup_res_t lr;
rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
free(path);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, rc);
return;
}
 
/*
* The name has already been unlinked by vfs_lookup_internal().
* We have to get and put the VFS node to ensure that it is
* VFS_DESTROY'ed after the last reference to it is dropped.
*/
vfs_node_t *node = vfs_node_get(&lr);
futex_down(&nodes_futex);
node->lnkcnt--;
futex_up(&nodes_futex);
rwlock_write_unlock(&namespace_rwlock);
vfs_node_put(node);
ipc_answer_0(rid, EOK);
}
 
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
{
size_t len;
ipc_callid_t callid;
int rc;
 
/* Retrieve the old path. */
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
}
char *old = malloc(len + 1);
if (!old) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
if ((rc = ipc_data_write_finalize(callid, old, len))) {
ipc_answer_0(rid, rc);
free(old);
return;
}
old[len] = '\0';
/* Retrieve the new path. */
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
free(old);
return;
}
char *new = malloc(len + 1);
if (!new) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
free(old);
return;
}
if ((rc = ipc_data_write_finalize(callid, new, len))) {
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
new[len] = '\0';
 
char *oldc = canonify(old, &len);
char *newc = canonify(new, NULL);
if (!oldc || !newc) {
ipc_answer_0(rid, EINVAL);
free(old);
free(new);
return;
}
if (!strncmp(newc, oldc, len)) {
/* oldc is a prefix of newc */
ipc_answer_0(rid, EINVAL);
free(old);
free(new);
return;
}
vfs_lookup_res_t old_lr;
vfs_lookup_res_t new_lr;
vfs_lookup_res_t new_par_lr;
rwlock_write_lock(&namespace_rwlock);
/* Lookup the node belonging to the old file name. */
rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
vfs_node_t *old_node = vfs_node_get(&old_lr);
if (!old_node) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOMEM);
free(old);
free(new);
return;
}
/* Lookup parent of the new file name. */
rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
/* Check whether linking to the same file system instance. */
if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
(old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, EXDEV); /* different file systems */
free(old);
free(new);
return;
}
/* Destroy the old link for the new name. */
vfs_node_t *new_node = NULL;
rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
switch (rc) {
case ENOENT:
/* simply not in our way */
break;
case EOK:
new_node = vfs_node_get(&new_lr);
if (!new_node) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOMEM);
free(old);
free(new);
return;
}
futex_down(&nodes_futex);
new_node->lnkcnt--;
futex_up(&nodes_futex);
break;
default:
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOTEMPTY);
free(old);
free(new);
return;
}
/* Create the new link for the new name. */
rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
if (new_node)
vfs_node_put(new_node);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
futex_down(&nodes_futex);
old_node->lnkcnt++;
futex_up(&nodes_futex);
/* Destroy the link for the old name. */
rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
vfs_node_put(old_node);
if (new_node)
vfs_node_put(new_node);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
futex_down(&nodes_futex);
old_node->lnkcnt--;
futex_up(&nodes_futex);
rwlock_write_unlock(&namespace_rwlock);
vfs_node_put(old_node);
if (new_node)
vfs_node_put(new_node);
free(old);
free(new);
ipc_answer_0(rid, EOK);
}
 
/**
* @}
*/
/branches/network/uspace/srv/vfs/vfs.h
35,29 → 35,29
 
#include <ipc/ipc.h>
#include <libadt/list.h>
#include <atomic.h>
#include <futex.h>
#include <rwlock.h>
#include <sys/types.h>
#include <bool.h>
 
#define dprintf(...) printf(__VA_ARGS__)
// FIXME: according to CONFIG_DEBUG
// #define dprintf(...) printf(__VA_ARGS__)
 
#define dprintf(...)
 
#define VFS_FIRST IPC_FIRST_USER_METHOD
 
#define IPC_METHOD_TO_VFS_OP(m) ((m) - VFS_FIRST)
 
/* Basic types. */
typedef int16_t fs_handle_t;
typedef int16_t dev_handle_t;
typedef uint32_t fs_index_t;
 
typedef enum {
VFS_OPEN = VFS_FIRST,
VFS_CLOSE,
VFS_READ,
VFS_READ = VFS_FIRST,
VFS_WRITE,
VFS_TRUNCATE,
VFS_RENAME,
VFS_OPENDIR,
VFS_READDIR,
VFS_CLOSEDIR,
VFS_MKDIR,
VFS_UNLINK,
VFS_MOUNT,
VFS_UNMOUNT,
VFS_LAST_CMN, /* keep this the last member of this enum */
65,12 → 65,19
 
typedef enum {
VFS_LOOKUP = VFS_LAST_CMN,
VFS_MOUNTED,
VFS_DESTROY,
VFS_LAST_CLNT, /* keep this the last member of this enum */
} vfs_request_clnt_t;
 
typedef enum {
VFS_REGISTER = VFS_LAST_CMN,
VFS_OPEN,
VFS_CLOSE,
VFS_SEEK,
VFS_MKDIR,
VFS_UNLINK,
VFS_RENAME,
VFS_LAST_SRV, /* keep this the last member of this enum */
} vfs_request_srv_t;
 
108,8 → 115,8
typedef struct {
link_t fs_link;
vfs_info_t vfs_info;
int fs_handle;
atomic_t phone_futex; /**< Phone serializing futex. */
fs_handle_t fs_handle;
futex_t phone_futex; /**< Phone serializing futex. */
ipcarg_t phone;
} fs_info_t;
 
116,9 → 123,9
/**
* VFS_PAIR uniquely represents a file system instance.
*/
#define VFS_PAIR \
int fs_handle; \
int dev_handle;
#define VFS_PAIR \
fs_handle_t fs_handle; \
dev_handle_t dev_handle;
 
/**
* VFS_TRIPLET uniquely identifies a file system node (e.g. directory, file) but
129,7 → 136,7
*/
#define VFS_TRIPLET \
VFS_PAIR; \
uint64_t index;
fs_index_t index;
 
typedef struct {
VFS_PAIR;
143,6 → 150,10
* Lookup flags.
*/
/**
* No lookup flags used.
*/
#define L_NONE 0
/**
* Lookup will succeed only if the object is a regular file. If L_CREATE is
* specified, an empty file will be created. This flag is mutually exclusive
* with L_DIRECTORY.
164,15 → 175,25
*/
#define L_CREATE 8
/**
* L_DESTROY is used to remove leaves from the file system namespace. This flag
* L_LINK is used for linking to an already existing nodes.
*/
#define L_LINK 16
/**
* L_UNLINK is used to remove leaves from the file system namespace. This flag
* cannot be passed directly by the client, but will be set by VFS during
* VFS_UNLINK.
*/
#define L_DESTROY 16
#define L_UNLINK 32
/**
* L_PARENT performs a lookup but returns the triplet of the parent node.
* This flag may not be combined with any other lookup flag.
*/
#define L_PARENT 64
 
typedef struct {
vfs_triplet_t triplet;
size_t size;
unsigned lnkcnt;
} vfs_lookup_res_t;
 
/**
181,9 → 202,18
*/
typedef struct {
VFS_TRIPLET; /**< Identity of the node. */
unsigned refcnt; /**< Usage counter. */
 
/**
* Usage counter. This includes, but is not limited to, all vfs_file_t
* structures that reference this node.
*/
unsigned refcnt;
/** Number of names this node has in the file system namespace. */
unsigned lnkcnt;
 
link_t nh_link; /**< Node hash-table link. */
size_t size; /**< Cached size of the file. */
size_t size; /**< Cached size if the node is a file. */
 
/**
* Holding this rwlock prevents modifications of the node's contents.
211,9 → 241,11
off_t pos;
} vfs_file_t;
 
extern futex_t nodes_futex;
 
extern link_t fs_head; /**< List of registered file systems. */
 
extern vfs_triplet_t rootfs; /**< Root node of the root file system. */
extern vfs_pair_t rootfs; /**< Root file system. */
 
#define MAX_PATH_LEN (64 * 1024)
 
226,7 → 258,7
size_t len; /**< Number of characters in this PLB entry. */
} plb_entry_t;
 
extern atomic_t plb_futex; /**< Futex protecting plb and plb_head. */
extern futex_t plb_futex; /**< Futex protecting plb and plb_head. */
extern uint8_t *plb; /**< Path Lookup Buffer */
extern link_t plb_head; /**< List of active PLB entries. */
 
233,13 → 265,13
/** Holding this rwlock prevents changes in file system namespace. */
extern rwlock_t namespace_rwlock;
 
extern int vfs_grab_phone(int);
extern int vfs_grab_phone(fs_handle_t);
extern void vfs_release_phone(int);
 
extern int fs_name_to_handle(char *, bool);
extern fs_handle_t fs_name_to_handle(char *, bool);
 
extern int vfs_lookup_internal(char *, size_t, int, vfs_lookup_res_t *,
vfs_pair_t *);
extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *, vfs_pair_t *,
...);
 
extern bool vfs_nodes_init(void);
extern vfs_node_t *vfs_node_get(vfs_lookup_res_t *);
250,7 → 282,7
extern bool vfs_files_init(void);
extern vfs_file_t *vfs_file_get(int);
extern int vfs_fd_alloc(void);
extern void vfs_fd_free(int);
extern int vfs_fd_free(int);
 
extern void vfs_file_addref(vfs_file_t *);
extern void vfs_file_delref(vfs_file_t *);
261,11 → 293,14
extern void vfs_register(ipc_callid_t, ipc_call_t *);
extern void vfs_mount(ipc_callid_t, ipc_call_t *);
extern void vfs_open(ipc_callid_t, ipc_call_t *);
extern void vfs_close(ipc_callid_t, ipc_call_t *);
extern void vfs_read(ipc_callid_t, ipc_call_t *);
extern void vfs_write(ipc_callid_t, ipc_call_t *);
extern void vfs_seek(ipc_callid_t, ipc_call_t *);
extern void vfs_truncate(ipc_callid_t, ipc_call_t *);
extern void vfs_mkdir(ipc_callid_t, ipc_call_t *);
extern void vfs_unlink(ipc_callid_t, ipc_call_t *);
extern void vfs_rename(ipc_callid_t, ipc_call_t *);
 
#endif
 
/branches/network/uspace/srv/vfs/vfs_file.c
97,13 → 97,17
/** Release file descriptor.
*
* @param fd File descriptor being released.
*
* @return EOK on success or EBADF if fd is an invalid file
* descriptor.
*/
void vfs_fd_free(int fd)
int vfs_fd_free(int fd)
{
assert(fd < MAX_OPEN_FILES);
assert(files[fd] != NULL);
if ((fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
return EBADF;
vfs_file_delref(files[fd]);
files[fd] = NULL;
return EOK;
}
 
/** Increment reference count of VFS file structure.
130,7 → 134,7
{
if (file->refcnt-- == 1) {
/*
* Lost last reference to a file, need to drop our reference
* Lost the last reference to a file, need to drop our reference
* to the underlying VFS node.
*/
vfs_node_delref(file->node);
/branches/network/uspace/srv/vfs/Makefile
52,7 → 52,7
 
.PHONY: all clean depend disasm
 
all: $(OUTPUT) disasm
all: $(OUTPUT) $(OUTPUT).disasm
 
-include Makefile.depend
 
65,9 → 65,11
$(OUTPUT): $(OBJECTS) $(LIBS)
$(LD) -T $(LIBC_PREFIX)/arch/$(ARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
 
disasm:
$(OBJDUMP) -d $(OUTPUT) >$(OUTPUT).disasm
disasm: $(OUTPUT).disasm
 
$(OUTPUT).disasm: $(OUTPUT)
$(OBJDUMP) -d $< >$@
 
%.o: %.S
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@
 
/branches/network/uspace/srv/vfs/vfs_lookup.c
35,26 → 35,27
* @brief
*/
 
#include "vfs.h"
#include <ipc/ipc.h>
#include <async.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <bool.h>
#include <futex.h>
#include <libadt/list.h>
#include <atomic.h>
#include "vfs.h"
#include <vfs/canonify.h>
 
#define min(a, b) ((a) < (b) ? (a) : (b))
 
atomic_t plb_futex = FUTEX_INITIALIZER;
futex_t plb_futex = FUTEX_INITIALIZER;
link_t plb_head; /**< PLB entry ring buffer. */
uint8_t *plb = NULL;
 
/** Perform a path lookup.
*
* @param path Path to be resolved; it needn't be an ASCIIZ string.
* @param len Number of path characters pointed by path.
* @param path Path to be resolved; it must be a NULL-terminated
* string.
* @param lflag Flags to be used during lookup.
* @param result Empty structure where the lookup result will be stored.
* Can be NULL.
63,22 → 64,33
*
* @return EOK on success or an error code from errno.h.
*/
int vfs_lookup_internal(char *path, size_t len, int lflag,
vfs_lookup_res_t *result, vfs_pair_t *altroot)
int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
vfs_pair_t *altroot, ...)
{
vfs_pair_t *root;
 
if (!len)
return EINVAL;
 
if (altroot)
root = altroot;
else
root = (vfs_pair_t *) &rootfs;
root = &rootfs;
 
if (!root->fs_handle)
return ENOENT;
size_t len;
path = canonify(path, &len);
if (!path)
return EINVAL;
fs_index_t index = 0;
if (lflag & L_LINK) {
va_list ap;
 
va_start(ap, altroot);
index = va_arg(ap, fs_index_t);
va_end(ap);
}
futex_down(&plb_futex);
 
plb_entry_t entry;
146,9 → 158,10
 
ipc_call_t answer;
int phone = vfs_grab_phone(root->fs_handle);
aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first,
aid_t req = async_send_5(phone, VFS_LOOKUP, (ipcarg_t) first,
(ipcarg_t) (first + len - 1) % PLB_SIZE,
(ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer);
(ipcarg_t) root->dev_handle, (ipcarg_t) lflag, (ipcarg_t) index,
&answer);
vfs_release_phone(phone);
 
ipcarg_t rc;
164,10 → 177,11
futex_up(&plb_futex);
 
if ((rc == EOK) && result) {
result->triplet.fs_handle = (int) IPC_GET_ARG1(answer);
result->triplet.dev_handle = (int) IPC_GET_ARG2(answer);
result->triplet.index = (int) IPC_GET_ARG3(answer);
result->triplet.fs_handle = (fs_handle_t) IPC_GET_ARG1(answer);
result->triplet.dev_handle = (dev_handle_t) IPC_GET_ARG2(answer);
result->triplet.index = (fs_index_t) IPC_GET_ARG3(answer);
result->size = (size_t) IPC_GET_ARG4(answer);
result->lnkcnt = (unsigned) IPC_GET_ARG5(answer);
}
 
return rc;
175,4 → 189,4
 
/**
* @}
*/
*/
/branches/network/uspace/srv/vfs/vfs.c
47,6 → 47,8
#include <atomic.h>
#include "vfs.h"
 
#define NAME "vfs"
 
#define dprintf(...) printf(__VA_ARGS__)
 
static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall)
53,8 → 55,6
{
bool keep_on_going = 1;
 
printf("Connection opened from %p\n", icall->in_phone_hash);
 
/*
* The connection was opened via the IPC_CONNECT_ME_TO call.
* This call needs to be answered.
77,8 → 77,6
 
callid = async_get_call(&call);
 
printf("Received call, method=%d\n", IPC_GET_METHOD(call));
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
keep_on_going = false;
93,6 → 91,9
case VFS_OPEN:
vfs_open(callid, &call);
break;
case VFS_CLOSE:
vfs_close(callid, &call);
break;
case VFS_READ:
vfs_read(callid, &call);
break;
108,6 → 109,12
case VFS_MKDIR:
vfs_mkdir(callid, &call);
break;
case VFS_UNLINK:
vfs_unlink(callid, &call);
break;
case VFS_RENAME:
vfs_rename(callid, &call);
break;
default:
ipc_answer_0(callid, ENOTSUP);
break;
122,7 → 129,7
{
ipcarg_t phonead;
 
printf("VFS: HelenOS VFS server\n");
printf(NAME ": HelenOS VFS server\n");
 
/*
* Initialize the list of registered file systems.
133,7 → 140,7
* Initialize VFS node hash table.
*/
if (!vfs_nodes_init()) {
printf("Failed to initialize the VFS node hash table.\n");
printf(NAME ": Failed to initialize VFS node hash table\n");
return ENOMEM;
}
 
143,12 → 150,12
list_initialize(&plb_head);
plb = as_get_mappable_page(PLB_SIZE);
if (!plb) {
printf("Cannot allocate a mappable piece of address space\n");
printf(NAME ": Cannot allocate a mappable piece of address space\n");
return ENOMEM;
}
if (as_area_create(plb, PLB_SIZE, AS_AREA_READ | AS_AREA_WRITE |
AS_AREA_CACHEABLE) != plb) {
printf("Cannot create address space area.\n");
printf(NAME ": Cannot create address space area\n");
return ENOMEM;
}
memset(plb, 0, PLB_SIZE);
166,6 → 173,7
/*
* Start accepting connections.
*/
printf(NAME ": Accepting connections\n");
async_manager();
return 0;
}
/branches/network/uspace/srv/vfs/vfs_register.c
106,14 → 106,6
dprintf("Operation VFS_LOOKUP not defined by the client.\n");
return false;
}
if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
dprintf("Operation VFS_OPEN not defined by the client.\n");
return false;
}
if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
dprintf("Operation VFS_CLOSE not defined by the client.\n");
return false;
}
if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
dprintf("Operation VFS_READ not defined by the client.\n");
return false;
213,6 → 205,7
}
futex_down(&fs_head_futex);
fibril_inc_sercount();
 
/*
* Check for duplicit registrations.
222,6 → 215,7
* We already register a fs like this.
*/
dprintf("FS is already registered.\n");
fibril_dec_sercount();
futex_up(&fs_head_futex);
free(fs_info);
ipc_answer_0(callid, EEXISTS);
234,7 → 228,7
*/
dprintf("Inserting FS into the list of registered file systems.\n");
list_append(&fs_info->fs_link, &fs_head);
 
/*
* Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
* that a callback connection is created and we have a phone through
244,6 → 238,7
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
list_remove(&fs_info->fs_link);
fibril_dec_sercount();
futex_up(&fs_head_futex);
free(fs_info);
ipc_answer_0(callid, EINVAL);
262,6 → 257,7
if (!ipc_share_in_receive(&callid, &size)) {
dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
list_remove(&fs_info->fs_link);
fibril_dec_sercount();
futex_up(&fs_head_futex);
ipc_hangup(fs_info->phone);
free(fs_info);
276,6 → 272,7
if (size != PLB_SIZE) {
dprintf("Client suggests wrong size of PFB, size = %d\n", size);
list_remove(&fs_info->fs_link);
fibril_dec_sercount();
futex_up(&fs_head_futex);
ipc_hangup(fs_info->phone);
free(fs_info);
297,9 → 294,10
* In reply to the VFS_REGISTER request, we assign the client file
* system a global file system handle.
*/
fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
fibril_dec_sercount();
futex_up(&fs_head_futex);
dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
313,7 → 311,7
* @return Phone over which a multi-call request can be safely
* sent. Return 0 if no phone was found.
*/
int vfs_grab_phone(int handle)
int vfs_grab_phone(fs_handle_t handle)
{
/*
* For now, we don't try to be very clever and very fast.
388,7 → 386,7
*
* @return File system handle or zero if file system not found.
*/
int fs_name_to_handle(char *name, bool lock)
fs_handle_t fs_name_to_handle(char *name, bool lock)
{
int handle = 0;
/branches/network/uspace/srv/vfs/vfs_node.c
38,14 → 38,15
#include "vfs.h"
#include <stdlib.h>
#include <string.h>
#include <atomic.h>
#include <futex.h>
#include <rwlock.h>
#include <libadt/hash_table.h>
#include <assert.h>
#include <async.h>
#include <errno.h>
 
/** Futex protecting the VFS node hash table. */
atomic_t nodes_futex = FUTEX_INITIALIZER;
futex_t nodes_futex = FUTEX_INITIALIZER;
 
#define NODES_BUCKETS_LOG 8
#define NODES_BUCKETS (1 << NODES_BUCKETS_LOG)
101,8 → 102,15
*/
void vfs_node_delref(vfs_node_t *node)
{
bool free_vfs_node = false;
bool free_fs_node = false;
 
futex_down(&nodes_futex);
if (node->refcnt-- == 1) {
/*
* We are dropping the last reference to this node.
* Remove it from the VFS node hash table.
*/
unsigned long key[] = {
[KEY_FS_HANDLE] = node->fs_handle,
[KEY_DEV_HANDLE] = node->dev_handle,
109,8 → 117,26
[KEY_INDEX] = node->index
};
hash_table_remove(&nodes, key, 3);
free_vfs_node = true;
if (!node->lnkcnt)
free_fs_node = true;
}
futex_up(&nodes_futex);
 
if (free_fs_node) {
/*
* The node is not visible in the file system namespace.
* Free up its resources.
*/
int phone = vfs_grab_phone(node->fs_handle);
ipcarg_t rc;
rc = async_req_2_0(phone, VFS_DESTROY,
(ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
assert(rc == EOK);
vfs_release_phone(phone);
}
if (free_vfs_node)
free(node);
}
 
/** Find VFS node.
145,9 → 171,10
}
memset(node, 0, sizeof(vfs_node_t));
node->fs_handle = result->triplet.fs_handle;
node->dev_handle = result->triplet.fs_handle;
node->dev_handle = result->triplet.dev_handle;
node->index = result->triplet.index;
node->size = result->size;
node->lnkcnt = result->lnkcnt;
link_initialize(&node->nh_link);
rwlock_initialize(&node->contents_rwlock);
hash_table_insert(&nodes, key, &node->nh_link);
156,6 → 183,7
}
 
assert(node->size == result->size);
assert(node->lnkcnt == result->lnkcnt);
 
_vfs_node_addref(node);
futex_up(&nodes_futex);
194,8 → 222,6
 
void nodes_remove_callback(link_t *item)
{
vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
free(node);
}
 
/**