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); |
} |
|
/** |
* @} |
*/ |