28,11 → 28,11 |
|
/** @addtogroup fs |
* @{ |
*/ |
*/ |
|
/** |
* @file vfs_ops.c |
* @brief Operations that VFS offers to its clients. |
* @file vfs_ops.c |
* @brief Operations that VFS offers to its clients. |
*/ |
|
#include "vfs.h" |
43,9 → 43,8 |
#include <stdlib.h> |
#include <string.h> |
#include <bool.h> |
#include <futex.h> |
#include <rwlock.h> |
#include <libadt/list.h> |
#include <fibril_sync.h> |
#include <adt/list.h> |
#include <unistd.h> |
#include <ctype.h> |
#include <fcntl.h> |
59,190 → 58,109 |
* This rwlock prevents the race between a triplet-to-VFS-node resolution and a |
* concurrent VFS operation which modifies the file system namespace. |
*/ |
RWLOCK_INITIALIZE(namespace_rwlock); |
FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock); |
|
futex_t rootfs_futex = FUTEX_INITIALIZER; |
vfs_pair_t rootfs = { |
.fs_handle = 0, |
.dev_handle = 0 |
}; |
|
void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle, |
fs_handle_t fs_handle, char *mp, char *opts) |
{ |
dev_handle_t dev_handle; |
vfs_lookup_res_t mp_res; |
vfs_lookup_res_t mr_res; |
vfs_node_t *mp_node = NULL; |
ipc_callid_t callid; |
ipc_call_t data; |
int rc; |
vfs_node_t *mr_node; |
fs_index_t rindex; |
size_t rsize; |
unsigned rlnkcnt; |
ipcarg_t rc; |
int phone; |
size_t size; |
|
/* |
* We expect the library to do the device-name to device-handle |
* translation for us, thus the device handle will arrive as ARG1 |
* in the 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 |
* carry mount options in the future. |
*/ |
|
/* |
* Now, we expect the client to send us data with the name of the file |
* system. |
*/ |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
/* |
* Don't receive more than is necessary for storing a full file system |
* name. |
*/ |
if (size < 1 || size > FS_NAME_MAXLEN) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
/* Deliver the file system name. */ |
char fs_name[FS_NAME_MAXLEN + 1]; |
(void) ipc_data_write_finalize(callid, fs_name, size); |
fs_name[size] = '\0'; |
aid_t msg; |
ipc_call_t answer; |
|
/* |
* 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. |
*/ |
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); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
/* Check whether size is reasonable wrt. the mount point. */ |
if (size < 1 || size > MAX_PATH_LEN) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* Allocate buffer for the mount point data being received. */ |
uint8_t *buf; |
buf = malloc(size + 1); |
if (!buf) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* Deliver the mount point. */ |
(void) ipc_data_write_finalize(callid, buf, size); |
buf[size] = '\0'; |
|
/* Resolve the path to the mountpoint. */ |
vfs_lookup_res_t mp_res; |
futex_down(&rootfs_futex); |
fibril_rwlock_write_lock(&namespace_rwlock); |
if (rootfs.fs_handle) { |
/* We already have the root FS. */ |
rwlock_write_lock(&namespace_rwlock); |
if ((size == 1) && (buf[0] == '/')) { |
if (str_cmp(mp, "/") == 0) { |
/* Trying to mount root FS over root FS */ |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
free(buf); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, EBUSY); |
return; |
} |
rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL); |
|
rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL); |
if (rc != EOK) { |
/* The lookup failed for some reason. */ |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
free(buf); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
|
mp_node = vfs_node_get(&mp_res); |
if (!mp_node) { |
rwlock_write_unlock(&namespace_rwlock); |
futex_up(&rootfs_futex); |
free(buf); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* |
* Now we hold a reference to mp_node. |
* It will be dropped upon the corresponding VFS_UNMOUNT. |
* It will be dropped upon the corresponding VFS_IN_UNMOUNT. |
* This prevents the mount point from being deleted. |
*/ |
rwlock_write_unlock(&namespace_rwlock); |
} else { |
/* We still don't have the root file system mounted. */ |
if ((size == 1) && (buf[0] == '/')) { |
vfs_lookup_res_t mr_res; |
vfs_node_t *mr_node; |
ipcarg_t rindex; |
ipcarg_t rsize; |
ipcarg_t rlnkcnt; |
|
if (str_cmp(mp, "/") == 0) { |
/* |
* 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); |
msg = async_send_1(phone, VFS_OUT_MOUNTED, |
(ipcarg_t) dev_handle, &answer); |
/* send the mount options */ |
rc = ipc_data_write_start(phone, (void *)opts, |
str_size(opts)); |
if (rc != EOK) { |
async_wait_for(msg, NULL); |
vfs_release_phone(phone); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
async_wait_for(msg, &rc); |
vfs_release_phone(phone); |
|
if (rc != EOK) { |
futex_up(&rootfs_futex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
|
rindex = (fs_index_t) IPC_GET_ARG1(answer); |
rsize = (size_t) IPC_GET_ARG2(answer); |
rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
|
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; |
|
mr_res.triplet.index = rindex; |
mr_res.size = rsize; |
mr_res.lnkcnt = rlnkcnt; |
mr_res.type = VFS_NODE_DIRECTORY; |
|
rootfs.fs_handle = fs_handle; |
rootfs.dev_handle = dev_handle; |
futex_up(&rootfs_futex); |
|
|
/* Add reference to the mounted root. */ |
mr_node = vfs_node_get(&mr_res); |
assert(mr_node); |
|
|
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} else { |
250,38 → 168,266 |
* We can't resolve this without the root filesystem |
* being mounted first. |
*/ |
futex_up(&rootfs_futex); |
free(buf); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
} |
futex_up(&rootfs_futex); |
|
free(buf); /* The buffer is not needed anymore. */ |
|
/* |
* At this point, we have all necessary pieces: file system and device |
* handles, and we know the mount point VFS node. |
*/ |
|
int mountee_phone = vfs_grab_phone(fs_handle); |
assert(mountee_phone >= 0); |
|
phone = vfs_grab_phone(mp_res.triplet.fs_handle); |
rc = async_req_4_0(phone, VFS_MOUNT, |
msg = async_send_4(phone, VFS_OUT_MOUNT, |
(ipcarg_t) mp_res.triplet.dev_handle, |
(ipcarg_t) mp_res.triplet.index, |
(ipcarg_t) fs_handle, |
(ipcarg_t) dev_handle); |
vfs_release_phone(phone); |
(ipcarg_t) dev_handle, &answer); |
|
/* send connection */ |
rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone); |
if (rc != EOK) { |
async_wait_for(msg, NULL); |
vfs_release_phone(mountee_phone); |
vfs_release_phone(phone); |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
ipc_answer_0(rid, rc); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
return; |
} |
|
vfs_release_phone(mountee_phone); |
|
/* send the mount options */ |
rc = ipc_data_write_start(phone, (void *)opts, str_size(opts)); |
if (rc != EOK) { |
async_wait_for(msg, NULL); |
vfs_release_phone(phone); |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
async_wait_for(msg, &rc); |
vfs_release_phone(phone); |
|
if (rc == EOK) { |
rindex = (fs_index_t) IPC_GET_ARG1(answer); |
rsize = (size_t) IPC_GET_ARG2(answer); |
rlnkcnt = (unsigned) IPC_GET_ARG3(answer); |
|
mr_res.triplet.fs_handle = fs_handle; |
mr_res.triplet.dev_handle = dev_handle; |
mr_res.triplet.index = rindex; |
mr_res.size = rsize; |
mr_res.lnkcnt = rlnkcnt; |
mr_res.type = VFS_NODE_DIRECTORY; |
|
/* Add reference to the mounted root. */ |
mr_node = vfs_node_get(&mr_res); |
assert(mr_node); |
} else { |
/* Mount failed, drop reference to mp_node. */ |
if (mp_node) |
vfs_node_put(mp_node); |
} |
|
ipc_answer_0(rid, rc); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
} |
|
void vfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* |
* We expect the library to do the device-name to device-handle |
* translation for us, thus the device handle will arrive as ARG1 |
* in the request. |
*/ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
|
/* |
* Mount flags are passed as ARG2. |
*/ |
unsigned int flags = (unsigned int) IPC_GET_ARG2(*request); |
|
/* |
* For now, don't make use of ARG3, but it can be used to |
* carry mount options in the future. |
*/ |
|
/* We want the client to send us the mount point. */ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
/* Check whether size is reasonable wrt. the mount point. */ |
if ((size < 1) || (size > MAX_PATH_LEN)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
/* Allocate buffer for the mount point data being received. */ |
char *mp = malloc(size + 1); |
if (!mp) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* Deliver the mount point. */ |
ipcarg_t retval = ipc_data_write_finalize(callid, mp, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(mp); |
return; |
} |
mp[size] = '\0'; |
|
/* Now we expect to receive the mount options. */ |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
free(mp); |
return; |
} |
|
/* Check the offered options size. */ |
if (size < 0 || size > MAX_MNTOPTS_LEN) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
free(mp); |
return; |
} |
|
/* Allocate buffer for the mount options. */ |
char *opts = (char *) malloc(size + 1); |
if (!opts) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
free(mp); |
return; |
} |
|
/* Deliver the mount options. */ |
retval = ipc_data_write_finalize(callid, opts, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(mp); |
free(opts); |
return; |
} |
opts[size] = '\0'; |
|
/* |
* Now, we expect the client to send us data with the name of the file |
* system. |
*/ |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
free(mp); |
free(opts); |
return; |
} |
|
/* |
* Don't receive more than is necessary for storing a full file system |
* name. |
*/ |
if ((size < 1) || (size > FS_NAME_MAXLEN)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
free(mp); |
free(opts); |
return; |
} |
|
/* |
* Allocate buffer for file system name. |
*/ |
char *fs_name = (char *) malloc(size + 1); |
if (fs_name == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
free(mp); |
free(opts); |
return; |
} |
|
/* Deliver the file system name. */ |
retval = ipc_data_write_finalize(callid, fs_name, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(mp); |
free(opts); |
free(fs_name); |
return; |
} |
fs_name[size] = '\0'; |
|
/* |
* Wait for IPC_M_PING so that we can return an error if we don't know |
* fs_name. |
*/ |
ipc_call_t data; |
callid = async_get_call(&data); |
if (IPC_GET_METHOD(data) != IPC_M_PING) { |
ipc_answer_0(callid, ENOTSUP); |
ipc_answer_0(rid, ENOTSUP); |
free(mp); |
free(opts); |
free(fs_name); |
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. |
*/ |
fibril_mutex_lock(&fs_head_lock); |
fs_handle_t fs_handle; |
recheck: |
fs_handle = fs_name_to_handle(fs_name, false); |
if (!fs_handle) { |
if (flags & IPC_FLAG_BLOCKING) { |
fibril_condvar_wait(&fs_head_cv, &fs_head_lock); |
goto recheck; |
} |
|
fibril_mutex_unlock(&fs_head_lock); |
ipc_answer_0(callid, ENOENT); |
ipc_answer_0(rid, ENOENT); |
free(mp); |
free(fs_name); |
free(opts); |
return; |
} |
fibril_mutex_unlock(&fs_head_lock); |
|
/* Acknowledge that we know fs_name. */ |
ipc_answer_0(callid, EOK); |
|
/* Do the mount */ |
vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts); |
free(mp); |
free(fs_name); |
free(opts); |
} |
|
void vfs_open(ipc_callid_t rid, ipc_call_t *request) |
{ |
if (!vfs_files_init()) { |
288,11 → 434,11 |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
|
/* |
* The POSIX interface is open(path, oflag, mode). |
* We can receive oflags and mode along with the VFS_OPEN call; the path |
* will need to arrive in another call. |
* We can receive oflags and mode along with the VFS_IN_OPEN call; |
* the path will need to arrive in another call. |
* |
* We also receive one private, non-POSIX set of flags called lflag |
* used to pass information to vfs_lookup_internal(). |
301,19 → 447,30 |
int oflag = IPC_GET_ARG2(*request); |
int mode = IPC_GET_ARG3(*request); |
size_t len; |
|
|
/* |
* Make sure that we are called with exactly one of L_FILE and |
* L_DIRECTORY. Make sure that the user does not pass L_OPEN. |
*/ |
if (((lflag & (L_FILE | L_DIRECTORY)) == 0) || |
((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) || |
((lflag & L_OPEN) != 0)) { |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
if (oflag & O_CREAT) |
lflag |= L_CREATE; |
if (oflag & O_EXCL) |
lflag |= L_EXCLUSIVE; |
|
|
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); |
320,6 → 477,7 |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
int rc; |
if ((rc = ipc_data_write_finalize(callid, path, len))) { |
ipc_answer_0(rid, rc); |
334,40 → 492,40 |
* triplet. |
*/ |
if (lflag & L_CREATE) |
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
else |
rwlock_read_lock(&namespace_rwlock); |
|
fibril_rwlock_read_lock(&namespace_rwlock); |
|
/* The path is now populated and we can call vfs_lookup_internal(). */ |
vfs_lookup_res_t lr; |
rc = vfs_lookup_internal(path, lflag, &lr, NULL); |
if (rc) { |
rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL); |
if (rc != EOK) { |
if (lflag & L_CREATE) |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
else |
rwlock_read_unlock(&namespace_rwlock); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(path); |
return; |
} |
|
|
/* Path is no longer needed. */ |
free(path); |
|
|
vfs_node_t *node = vfs_node_get(&lr); |
if (lflag & L_CREATE) |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
else |
rwlock_read_unlock(&namespace_rwlock); |
|
fibril_rwlock_read_unlock(&namespace_rwlock); |
|
/* Truncate the file if requested and if necessary. */ |
if (oflag & O_TRUNC) { |
rwlock_write_lock(&node->contents_rwlock); |
fibril_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); |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, rc); |
return; |
374,9 → 532,9 |
} |
node->size = 0; |
} |
rwlock_write_unlock(&node->contents_rwlock); |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
} |
|
|
/* |
* Get ourselves a file descriptor and the corresponding vfs_file_t |
* structure. |
389,30 → 547,173 |
} |
vfs_file_t *file = vfs_file_get(fd); |
file->node = node; |
if (oflag & O_APPEND) |
if (oflag & O_APPEND) |
file->append = true; |
|
/* |
* The following increase in reference count is for the fact that the |
* file is being opened and that a file structure is pointing to it. |
* It is necessary so that the file will not disappear when |
* vfs_node_put() is called. The reference will be dropped by the |
* respective VFS_IN_CLOSE. |
*/ |
vfs_node_addref(node); |
vfs_node_put(node); |
|
/* Success! Return the new file descriptor to the client. */ |
ipc_answer_1(rid, EOK, fd); |
} |
|
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
// FIXME: check for sanity of the supplied fs, dev and index |
|
if (!vfs_files_init()) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
/* |
* The interface is open_node(fs, dev, index, oflag). |
*/ |
vfs_lookup_res_t lr; |
|
lr.triplet.fs_handle = IPC_GET_ARG1(*request); |
lr.triplet.dev_handle = IPC_GET_ARG2(*request); |
lr.triplet.index = IPC_GET_ARG3(*request); |
int oflag = IPC_GET_ARG4(*request); |
|
fibril_rwlock_read_lock(&namespace_rwlock); |
|
int rc = vfs_open_node_internal(&lr); |
if (rc != EOK) { |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
|
vfs_node_t *node = vfs_node_get(&lr); |
fibril_rwlock_read_unlock(&namespace_rwlock); |
|
/* Truncate the file if requested and if necessary. */ |
if (oflag & O_TRUNC) { |
fibril_rwlock_write_lock(&node->contents_rwlock); |
if (node->size) { |
rc = vfs_truncate_internal(node->fs_handle, |
node->dev_handle, node->index, 0); |
if (rc) { |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, rc); |
return; |
} |
node->size = 0; |
} |
fibril_rwlock_write_unlock(&node->contents_rwlock); |
} |
|
/* |
* Get ourselves a file descriptor and the corresponding vfs_file_t |
* structure. |
*/ |
int fd = vfs_fd_alloc(); |
if (fd < 0) { |
vfs_node_put(node); |
ipc_answer_0(rid, fd); |
return; |
} |
vfs_file_t *file = vfs_file_get(fd); |
file->node = node; |
if (oflag & O_APPEND) |
file->append = true; |
|
/* |
* The following increase in reference count is for the fact that the |
* file is being opened and that a file structure is pointing to it. |
* It is necessary so that the file will not disappear when |
* vfs_node_put() is called. The reference will be dropped by the |
* respective VFS_CLOSE. |
* respective VFS_IN_CLOSE. |
*/ |
vfs_node_addref(node); |
vfs_node_put(node); |
|
|
/* Success! Return the new file descriptor to the client. */ |
ipc_answer_1(rid, EOK, fd); |
} |
|
void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
void vfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
int rc = vfs_fd_free(fd); |
|
/* Lookup the file structure corresponding to the file descriptor. */ |
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
|
/* |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
fibril_mutex_lock(&file->lock); |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
/* Make a VFS_OUT_SYMC request at the destination FS server. */ |
aid_t msg; |
ipc_call_t answer; |
msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle, |
file->node->index, &answer); |
|
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
|
vfs_release_phone(fs_phone); |
fibril_mutex_unlock(&file->lock); |
|
ipc_answer_0(rid, rc); |
} |
|
void vfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
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) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
|
/* |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
fibril_mutex_lock(&file->lock); |
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
/* Make a VFS_OUT_CLOSE request at the destination FS server. */ |
aid_t msg; |
ipc_call_t answer; |
msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle, |
file->node->index, &answer); |
|
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
|
vfs_release_phone(fs_phone); |
fibril_mutex_unlock(&file->lock); |
|
int retval = IPC_GET_ARG1(answer); |
if (retval != EOK) |
ipc_answer_0(rid, retval); |
|
retval = vfs_fd_free(fd); |
ipc_answer_0(rid, retval); |
} |
|
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
{ |
|
455,16 → 756,25 |
* Lock the open file structure so that no other thread can manipulate |
* the same open file at a time. |
*/ |
futex_down(&file->lock); |
|
fibril_mutex_lock(&file->lock); |
|
/* |
* Lock the file's node so that no other client can read/write to it at |
* the same time. |
*/ |
if (read) |
rwlock_read_lock(&file->node->contents_rwlock); |
fibril_rwlock_read_lock(&file->node->contents_rwlock); |
else |
rwlock_write_lock(&file->node->contents_rwlock); |
fibril_rwlock_write_lock(&file->node->contents_rwlock); |
|
if (file->node->type == VFS_NODE_DIRECTORY) { |
/* |
* Make sure that no one is modifying the namespace |
* while we are in readdir(). |
*/ |
assert(read); |
fibril_rwlock_read_lock(&namespace_rwlock); |
} |
|
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
473,7 → 783,7 |
ipc_call_t answer; |
if (!read && file->append) |
file->pos = file->node->size; |
msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE, |
file->node->dev_handle, file->node->index, file->pos, &answer); |
|
/* |
483,28 → 793,32 |
* don't have to bother. |
*/ |
ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
/* Wait for reply from the FS server. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
|
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); |
|
if (file->node->type == VFS_NODE_DIRECTORY) |
fibril_rwlock_read_unlock(&namespace_rwlock); |
|
/* Unlock the VFS node. */ |
if (read) |
rwlock_read_unlock(&file->node->contents_rwlock); |
fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
else { |
/* Update the cached version of node's size. */ |
if (rc == EOK) |
file->node->size = IPC_GET_ARG2(answer); |
rwlock_write_unlock(&file->node->contents_rwlock); |
fibril_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); |
fibril_mutex_unlock(&file->lock); |
|
/* |
* FS server's reply is the final result of the whole operation we |
538,40 → 852,40 |
} |
|
off_t newpos; |
futex_down(&file->lock); |
fibril_mutex_lock(&file->lock); |
if (whence == SEEK_SET) { |
file->pos = off; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, off); |
return; |
} |
if (whence == SEEK_CUR) { |
if (file->pos + off < file->pos) { |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EOVERFLOW); |
return; |
} |
file->pos += off; |
newpos = file->pos; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, newpos); |
return; |
} |
if (whence == SEEK_END) { |
rwlock_read_lock(&file->node->contents_rwlock); |
fibril_rwlock_read_lock(&file->node->contents_rwlock); |
size_t size = file->node->size; |
rwlock_read_unlock(&file->node->contents_rwlock); |
fibril_rwlock_read_unlock(&file->node->contents_rwlock); |
if (size + off < size) { |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EOVERFLOW); |
return; |
} |
newpos = size + off; |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_1(rid, EOK, newpos); |
return; |
} |
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, EINVAL); |
} |
|
583,7 → 897,7 |
int fs_phone; |
|
fs_phone = vfs_grab_phone(fs_handle); |
rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle, |
rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle, |
(ipcarg_t)index, (ipcarg_t)size); |
vfs_release_phone(fs_phone); |
return (int)rc; |
600,19 → 914,119 |
ipc_answer_0(rid, ENOENT); |
return; |
} |
futex_down(&file->lock); |
fibril_mutex_lock(&file->lock); |
|
rwlock_write_lock(&file->node->contents_rwlock); |
fibril_rwlock_write_lock(&file->node->contents_rwlock); |
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); |
fibril_rwlock_write_unlock(&file->node->contents_rwlock); |
|
futex_up(&file->lock); |
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, (ipcarg_t)rc); |
} |
|
void vfs_fstat(ipc_callid_t rid, ipc_call_t *request) |
{ |
int fd = IPC_GET_ARG1(*request); |
size_t size = IPC_GET_ARG2(*request); |
ipcarg_t rc; |
|
vfs_file_t *file = vfs_file_get(fd); |
if (!file) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
|
ipc_callid_t callid; |
if (!ipc_data_read_receive(&callid, NULL)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
fibril_mutex_lock(&file->lock); |
|
int fs_phone = vfs_grab_phone(file->node->fs_handle); |
|
aid_t msg; |
msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle, |
file->node->index, true, NULL); |
ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
async_wait_for(msg, &rc); |
vfs_release_phone(fs_phone); |
|
fibril_mutex_unlock(&file->lock); |
ipc_answer_0(rid, rc); |
} |
|
void vfs_stat(ipc_callid_t rid, ipc_call_t *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'; |
|
if (!ipc_data_read_receive(&callid, NULL)) { |
free(path); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
vfs_lookup_res_t lr; |
fibril_rwlock_read_lock(&namespace_rwlock); |
rc = vfs_lookup_internal(path, L_NONE, &lr, NULL); |
free(path); |
if (rc != EOK) { |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(callid, rc); |
ipc_answer_0(rid, rc); |
return; |
} |
vfs_node_t *node = vfs_node_get(&lr); |
if (!node) { |
fibril_rwlock_read_unlock(&namespace_rwlock); |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
|
fibril_rwlock_read_unlock(&namespace_rwlock); |
|
int fs_phone = vfs_grab_phone(node->fs_handle); |
aid_t msg; |
msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle, |
node->index, false, NULL); |
ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
ipcarg_t rv; |
async_wait_for(msg, &rv); |
vfs_release_phone(fs_phone); |
|
ipc_answer_0(rid, rv); |
|
vfs_node_put(node); |
} |
|
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request) |
{ |
int mode = IPC_GET_ARG1(*request); |
639,10 → 1053,10 |
} |
path[len] = '\0'; |
|
rwlock_write_lock(&namespace_rwlock); |
fibril_rwlock_write_lock(&namespace_rwlock); |
int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE; |
rc = vfs_lookup_internal(path, lflag, NULL, NULL); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
free(path); |
ipc_answer_0(rid, rc); |
} |
673,13 → 1087,13 |
} |
path[len] = '\0'; |
|
rwlock_write_lock(&namespace_rwlock); |
fibril_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); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
return; |
} |
687,13 → 1101,13 |
/* |
* 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_OUT_DESTROY'ed after the last reference to it is dropped. |
*/ |
vfs_node_t *node = vfs_node_get(&lr); |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
node->lnkcnt--; |
futex_up(&nodes_futex); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_mutex_unlock(&nodes_mutex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(node); |
ipc_answer_0(rid, EOK); |
} |
700,37 → 1114,37 |
|
void vfs_rename(ipc_callid_t rid, ipc_call_t *request) |
{ |
size_t len; |
size_t olen, nlen; |
ipc_callid_t callid; |
int rc; |
|
/* Retrieve the old path. */ |
if (!ipc_data_write_receive(&callid, &len)) { |
if (!ipc_data_write_receive(&callid, &olen)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *old = malloc(len + 1); |
char *old = malloc(olen + 1); |
if (!old) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
if ((rc = ipc_data_write_finalize(callid, old, len))) { |
if ((rc = ipc_data_write_finalize(callid, old, olen))) { |
ipc_answer_0(rid, rc); |
free(old); |
return; |
} |
old[len] = '\0'; |
old[olen] = '\0'; |
|
/* Retrieve the new path. */ |
if (!ipc_data_write_receive(&callid, &len)) { |
if (!ipc_data_write_receive(&callid, &nlen)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
free(old); |
return; |
} |
char *new = malloc(len + 1); |
char *new = malloc(nlen + 1); |
if (!new) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
737,16 → 1151,16 |
free(old); |
return; |
} |
if ((rc = ipc_data_write_finalize(callid, new, len))) { |
if ((rc = ipc_data_write_finalize(callid, new, nlen))) { |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
return; |
} |
new[len] = '\0'; |
new[nlen] = '\0'; |
|
char *oldc = canonify(old, &len); |
char *newc = canonify(new, NULL); |
char *oldc = canonify(old, &olen); |
char *newc = canonify(new, &nlen); |
if (!oldc || !newc) { |
ipc_answer_0(rid, EINVAL); |
free(old); |
753,8 → 1167,18 |
free(new); |
return; |
} |
if (!strncmp(newc, oldc, len)) { |
/* oldc is a prefix of newc */ |
oldc[olen] = '\0'; |
newc[nlen] = '\0'; |
if ((!str_lcmp(newc, oldc, str_length(oldc))) && |
((newc[str_length(oldc)] == '/') || |
(str_length(oldc) == 1) || |
(str_length(oldc) == str_length(newc)))) { |
/* |
* oldc is a prefix of newc and either |
* - newc continues with a / where oldc ends, or |
* - oldc was / itself, or |
* - oldc and newc are equal. |
*/ |
ipc_answer_0(rid, EINVAL); |
free(old); |
free(new); |
764,11 → 1188,11 |
vfs_lookup_res_t old_lr; |
vfs_lookup_res_t new_lr; |
vfs_lookup_res_t new_par_lr; |
rwlock_write_lock(&namespace_rwlock); |
fibril_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); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
776,16 → 1200,31 |
} |
vfs_node_t *old_node = vfs_node_get(&old_lr); |
if (!old_node) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
free(old); |
free(new); |
return; |
} |
/* Determine the path to the parent of the node with the new name. */ |
char *parentc = str_dup(newc); |
if (!parentc) { |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
return; |
} |
char *lastsl = str_rchr(parentc + 1, '/'); |
if (lastsl) |
*lastsl = '\0'; |
else |
parentc[1] = '\0'; |
/* Lookup parent of the new file name. */ |
rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL); |
rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL); |
free(parentc); /* not needed anymore */ |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, rc); |
free(old); |
free(new); |
794,7 → 1233,7 |
/* 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); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, EXDEV); /* different file systems */ |
free(old); |
free(new); |
810,18 → 1249,18 |
case EOK: |
new_node = vfs_node_get(&new_lr); |
if (!new_node) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOMEM); |
free(old); |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
new_node->lnkcnt--; |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
break; |
default: |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
ipc_answer_0(rid, ENOTEMPTY); |
free(old); |
free(new); |
830,7 → 1269,7 |
/* 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); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
if (new_node) |
vfs_node_put(new_node); |
ipc_answer_0(rid, rc); |
838,13 → 1277,13 |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
old_node->lnkcnt++; |
futex_up(&nodes_futex); |
fibril_mutex_unlock(&nodes_mutex); |
/* Destroy the link for the old name. */ |
rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL); |
if (rc != EOK) { |
rwlock_write_unlock(&namespace_rwlock); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(old_node); |
if (new_node) |
vfs_node_put(new_node); |
853,10 → 1292,10 |
free(new); |
return; |
} |
futex_down(&nodes_futex); |
fibril_mutex_lock(&nodes_mutex); |
old_node->lnkcnt--; |
futex_up(&nodes_futex); |
rwlock_write_unlock(&namespace_rwlock); |
fibril_mutex_unlock(&nodes_mutex); |
fibril_rwlock_write_unlock(&namespace_rwlock); |
vfs_node_put(old_node); |
if (new_node) |
vfs_node_put(new_node); |
867,4 → 1306,4 |
|
/** |
* @} |
*/ |
*/ |