HelenOS sources
This source file includes following definitions.
- get_parent_and_child
- vfs_absolutize
- vfs_clone
- vfs_cwd_get
- vfs_cwd_set
- vfs_exchange_begin
- vfs_exchange_end
- vfs_fd_session
- vfs_fsprobe
- vfs_fstypes
- vfs_fstypes_free
- vfs_link
- vfs_link_path
- vfs_lookup
- vfs_lookup_open
- vfs_mount
- vfs_mount_path
- vfs_open
- vfs_pass_handle
- vfs_put
- vfs_receive_handle
- vfs_read
- vfs_read_short
- vfs_rename_path
- vfs_resize
- vfs_root
- vfs_root_set
- vfs_stat
- vfs_stat_path
- vfs_statfs
- vfs_statfs_path
- vfs_sync
- vfs_unlink
- vfs_unlink_path
- vfs_unmount
- vfs_unmount_path
- vfs_walk
- vfs_write
- vfs_write_short
#include <vfs/vfs.h>
#include <vfs/canonify.h>
#include <vfs/vfs_mtab.h>
#include <vfs/vfs_sess.h>
#include <macros.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <ipc/services.h>
#include <ns.h>
#include <async.h>
#include <fibril_synch.h>
#include <errno.h>
#include <assert.h>
#include <str.h>
#include <loc.h>
#include <ipc/vfs.h>
#include <ipc/loc.h>
static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
static async_sess_t *vfs_sess = NULL;
static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
static int cwd_fd = -1;
static char *cwd_path = NULL;
static size_t cwd_size = 0;
static FIBRIL_MUTEX_INITIALIZE(root_mutex);
static int root_fd = -1;
static errno_t get_parent_and_child(const char *path, int *parent, char **child)
{
size_t size;
char *apath = vfs_absolutize(path, &size);
if (!apath)
return ENOMEM;
char *slash = str_rchr(apath, L'/');
if (slash == apath) {
*parent = vfs_root();
if (*parent < 0) {
free(apath);
return EBADF;
}
*child = apath;
return EOK;
} else {
*slash = '\0';
errno_t rc = vfs_lookup(apath, WALK_DIRECTORY, parent);
if (rc != EOK) {
free(apath);
return rc;
}
*slash = '/';
*child = str_dup(slash);
free(apath);
if (!*child) {
vfs_put(*parent);
return ENOMEM;
}
return rc;
}
}
char *vfs_absolutize(const char *path, size_t *retlen)
{
char *ncwd_path;
char *ncwd_path_nc;
fibril_mutex_lock(&cwd_mutex);
size_t size = str_size(path);
if (*path != '/') {
if (cwd_path == NULL) {
fibril_mutex_unlock(&cwd_mutex);
return NULL;
}
ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
if (ncwd_path_nc == NULL) {
fibril_mutex_unlock(&cwd_mutex);
return NULL;
}
str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
ncwd_path_nc[cwd_size] = '/';
ncwd_path_nc[cwd_size + 1] = '\0';
} else {
ncwd_path_nc = malloc(size + 1);
if (ncwd_path_nc == NULL) {
fibril_mutex_unlock(&cwd_mutex);
return NULL;
}
ncwd_path_nc[0] = '\0';
}
str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
ncwd_path = canonify(ncwd_path_nc, retlen);
if (ncwd_path == NULL) {
fibril_mutex_unlock(&cwd_mutex);
free(ncwd_path_nc);
return NULL;
}
ncwd_path = str_dup(ncwd_path);
free(ncwd_path_nc);
if (ncwd_path == NULL) {
fibril_mutex_unlock(&cwd_mutex);
return NULL;
}
fibril_mutex_unlock(&cwd_mutex);
return ncwd_path;
}
errno_t vfs_clone(int file_from, int file_to, bool high, int *handle)
{
assert(handle != NULL);
async_exch_t *vfs_exch = vfs_exchange_begin();
sysarg_t ret;
errno_t rc = async_req_3_1(vfs_exch, VFS_IN_CLONE, (sysarg_t) file_from,
(sysarg_t) file_to, (sysarg_t) high, &ret);
vfs_exchange_end(vfs_exch);
if (rc == EOK) {
*handle = ret;
}
return rc;
}
errno_t vfs_cwd_get(char *buf, size_t size)
{
fibril_mutex_lock(&cwd_mutex);
if ((cwd_size == 0) || (size < cwd_size + 1)) {
fibril_mutex_unlock(&cwd_mutex);
return ERANGE;
}
str_cpy(buf, size, cwd_path);
fibril_mutex_unlock(&cwd_mutex);
return EOK;
}
errno_t vfs_cwd_set(const char *path)
{
size_t abs_size;
char *abs_path = vfs_absolutize(path, &abs_size);
if (abs_path == NULL)
return ENOMEM;
int fd;
errno_t rc = vfs_lookup(abs_path, WALK_DIRECTORY, &fd);
if (rc != EOK) {
free(abs_path);
return rc;
}
fibril_mutex_lock(&cwd_mutex);
if (cwd_fd >= 0)
vfs_put(cwd_fd);
if (cwd_path)
free(cwd_path);
cwd_fd = fd;
cwd_path = abs_path;
cwd_size = abs_size;
fibril_mutex_unlock(&cwd_mutex);
return EOK;
}
async_exch_t *vfs_exchange_begin(void)
{
fibril_mutex_lock(&vfs_mutex);
while (vfs_sess == NULL) {
vfs_sess = service_connect_blocking(SERVICE_VFS, INTERFACE_VFS,
0, NULL);
}
fibril_mutex_unlock(&vfs_mutex);
return async_exchange_begin(vfs_sess);
}
void vfs_exchange_end(async_exch_t *exch)
{
async_exchange_end(exch);
}
async_sess_t *vfs_fd_session(int file, iface_t iface)
{
vfs_stat_t stat;
errno_t rc = vfs_stat(file, &stat);
if (rc != EOK)
return NULL;
if (stat.service == 0)
return NULL;
return loc_service_connect(stat.service, iface, 0);
}
errno_t vfs_fsprobe(const char *fs_name, service_id_t serv,
vfs_fs_probe_info_t *info)
{
errno_t rc;
ipc_call_t answer;
async_exch_t *exch = vfs_exchange_begin();
aid_t req = async_send_1(exch, VFS_IN_FSPROBE, serv, &answer);
rc = async_data_write_start(exch, (void *) fs_name,
str_size(fs_name));
async_wait_for(req, &rc);
if (rc != EOK) {
vfs_exchange_end(exch);
return rc;
}
rc = async_data_read_start(exch, info, sizeof(*info));
vfs_exchange_end(exch);
return rc;
}
errno_t vfs_fstypes(vfs_fstypes_t *fstypes)
{
sysarg_t size;
char *buf;
char dummybuf[1];
size_t count, i;
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_0_1(exch, VFS_IN_FSTYPES, &size);
if (rc != EOK) {
vfs_exchange_end(exch);
return rc;
}
buf = malloc(size);
if (buf == NULL) {
buf = dummybuf;
size = 1;
}
rc = async_data_read_start(exch, buf, size);
vfs_exchange_end(exch);
if (buf == dummybuf)
return ENOMEM;
count = 0;
i = 0;
while (i < size) {
if (buf[i] == '\0')
++count;
++i;
}
if (count == 0) {
free(buf);
return EIO;
}
fstypes->fstypes = calloc(sizeof(char *), count + 1);
if (fstypes->fstypes == NULL) {
free(buf);
return ENOMEM;
}
if (buf[0] != '\0')
fstypes->fstypes[0] = &buf[0];
count = 0;
i = 0;
while (i < size) {
if (buf[i] == '\0')
fstypes->fstypes[++count] = &buf[i + 1];
++i;
}
fstypes->fstypes[count] = NULL;
fstypes->buf = buf;
fstypes->size = size;
return rc;
}
void vfs_fstypes_free(vfs_fstypes_t *fstypes)
{
free(fstypes->buf);
fstypes->buf = NULL;
free(fstypes->fstypes);
fstypes->fstypes = NULL;
fstypes->size = 0;
}
errno_t vfs_link(int parent, const char *child, vfs_file_kind_t kind, int *linkedfd)
{
int flags = (kind == KIND_DIRECTORY) ? WALK_DIRECTORY : WALK_REGULAR;
int file = -1;
errno_t rc = vfs_walk(parent, child, WALK_MUST_CREATE | flags, &file);
if (rc != EOK)
return rc;
if (linkedfd)
*linkedfd = file;
else
vfs_put(file);
return EOK;
}
errno_t vfs_link_path(const char *path, vfs_file_kind_t kind, int *linkedfd)
{
char *child;
int parent;
errno_t rc = get_parent_and_child(path, &parent, &child);
if (rc != EOK)
return rc;
rc = vfs_link(parent, child, kind, linkedfd);
free(child);
vfs_put(parent);
return rc;
}
errno_t vfs_lookup(const char *path, int flags, int *handle)
{
size_t size;
char *p = vfs_absolutize(path, &size);
if (!p)
return ENOMEM;
int root = vfs_root();
if (root < 0) {
free(p);
return ENOENT;
}
*handle = -1;
errno_t rc = vfs_walk(root, p, flags, handle);
vfs_put(root);
free(p);
return rc;
}
errno_t vfs_lookup_open(const char *path, int flags, int mode, int *handle)
{
int file;
errno_t rc = vfs_lookup(path, flags, &file);
if (rc != EOK)
return rc;
rc = vfs_open(file, mode);
if (rc != EOK) {
vfs_put(file);
return rc;
}
*handle = file;
return EOK;
}
errno_t vfs_mount(int mp, const char *fs_name, service_id_t serv, const char *opts,
unsigned int flags, unsigned int instance, int *mountedfd)
{
errno_t rc, rc1;
if (!mountedfd)
flags |= VFS_MOUNT_NO_REF;
if (mp < 0)
flags |= VFS_MOUNT_CONNECT_ONLY;
ipc_call_t answer;
async_exch_t *exch = vfs_exchange_begin();
aid_t req = async_send_4(exch, VFS_IN_MOUNT, mp, serv, flags, instance,
&answer);
rc1 = async_data_write_start(exch, (void *) opts, str_size(opts));
if (rc1 == EOK) {
rc1 = async_data_write_start(exch, (void *) fs_name,
str_size(fs_name));
}
vfs_exchange_end(exch);
async_wait_for(req, &rc);
if (mountedfd)
*mountedfd = (int) ipc_get_arg1(&answer);
if (rc != EOK)
return rc;
return rc1;
}
errno_t vfs_mount_path(const char *mp, const char *fs_name, const char *fqsn,
const char *opts, unsigned int flags, unsigned int instance)
{
int null_id = -1;
char null[LOC_NAME_MAXLEN];
if (str_cmp(fqsn, "") == 0) {
null_id = loc_null_create();
if (null_id == -1)
return ENOMEM;
snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
fqsn = null;
}
if (flags & IPC_FLAG_BLOCKING)
flags = VFS_MOUNT_BLOCKING;
else
flags = 0;
service_id_t service_id;
errno_t res = loc_service_get_id(fqsn, &service_id, flags);
if (res != EOK) {
if (null_id != -1)
loc_null_destroy(null_id);
return res;
}
size_t mpa_size;
char *mpa = vfs_absolutize(mp, &mpa_size);
if (mpa == NULL) {
if (null_id != -1)
loc_null_destroy(null_id);
return ENOMEM;
}
fibril_mutex_lock(&root_mutex);
errno_t rc;
if (str_cmp(mpa, "/") == 0) {
if (root_fd >= 0) {
fibril_mutex_unlock(&root_mutex);
if (null_id != -1)
loc_null_destroy(null_id);
return EBUSY;
}
int root;
rc = vfs_mount(-1, fs_name, service_id, opts, flags, instance,
&root);
if (rc == EOK)
root_fd = root;
} else {
if (root_fd < 0) {
fibril_mutex_unlock(&root_mutex);
if (null_id != -1)
loc_null_destroy(null_id);
return EINVAL;
}
int mpfd;
rc = vfs_walk(root_fd, mpa, WALK_DIRECTORY, &mpfd);
if (rc == EOK) {
rc = vfs_mount(mpfd, fs_name, service_id, opts, flags,
instance, NULL);
vfs_put(mpfd);
}
}
fibril_mutex_unlock(&root_mutex);
if ((rc != EOK) && (null_id != -1))
loc_null_destroy(null_id);
return (errno_t) rc;
}
errno_t vfs_open(int file, int mode)
{
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_2_0(exch, VFS_IN_OPEN, file, mode);
vfs_exchange_end(exch);
return rc;
}
errno_t vfs_pass_handle(async_exch_t *vfs_exch, int file, async_exch_t *exch)
{
return async_state_change_start(exch, VFS_PASS_HANDLE, (sysarg_t) file,
0, vfs_exch);
}
errno_t vfs_put(int file)
{
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_1_0(exch, VFS_IN_PUT, file);
vfs_exchange_end(exch);
return rc;
}
errno_t vfs_receive_handle(bool high, int *handle)
{
ipc_call_t call;
if (!async_state_change_receive(&call)) {
async_answer_0(&call, EINVAL);
return EINVAL;
}
async_exch_t *vfs_exch = vfs_exchange_begin();
async_state_change_finalize(&call, vfs_exch);
sysarg_t ret;
errno_t rc = async_req_1_1(vfs_exch, VFS_IN_WAIT_HANDLE, high, &ret);
async_exchange_end(vfs_exch);
if (rc == EOK) {
*handle = (int) ret;
}
return rc;
}
errno_t vfs_read(int file, aoff64_t *pos, void *buf, size_t nbyte, size_t *nread)
{
ssize_t cnt = 0;
size_t nr = 0;
uint8_t *bp = (uint8_t *) buf;
errno_t rc;
do {
bp += cnt;
nr += cnt;
*pos += cnt;
rc = vfs_read_short(file, *pos, bp, nbyte - nr, &cnt);
} while (rc == EOK && cnt > 0 && (nbyte - nr - cnt) > 0);
if (rc != EOK) {
*nread = nr;
return rc;
}
nr += cnt;
*pos += cnt;
*nread = nr;
return EOK;
}
errno_t vfs_read_short(int file, aoff64_t pos, void *buf, size_t nbyte,
ssize_t *nread)
{
errno_t rc;
ipc_call_t answer;
aid_t req;
if (nbyte > DATA_XFER_LIMIT)
nbyte = DATA_XFER_LIMIT;
async_exch_t *exch = vfs_exchange_begin();
req = async_send_3(exch, VFS_IN_READ, file, LOWER32(pos),
UPPER32(pos), &answer);
rc = async_data_read_start(exch, (void *) buf, nbyte);
vfs_exchange_end(exch);
if (rc == EOK)
async_wait_for(req, &rc);
else
async_forget(req);
if (rc != EOK)
return rc;
*nread = (ssize_t) ipc_get_arg1(&answer);
return EOK;
}
errno_t vfs_rename_path(const char *old, const char *new)
{
errno_t rc;
errno_t rc_orig;
aid_t req;
size_t olda_size;
char *olda = vfs_absolutize(old, &olda_size);
if (olda == NULL)
return ENOMEM;
size_t newa_size;
char *newa = vfs_absolutize(new, &newa_size);
if (newa == NULL) {
free(olda);
return ENOMEM;
}
async_exch_t *exch = vfs_exchange_begin();
int root = vfs_root();
if (root < 0) {
free(olda);
free(newa);
return ENOENT;
}
req = async_send_1(exch, VFS_IN_RENAME, root, NULL);
rc = async_data_write_start(exch, olda, olda_size);
if (rc != EOK) {
vfs_exchange_end(exch);
free(olda);
free(newa);
vfs_put(root);
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
rc = rc_orig;
return rc;
}
rc = async_data_write_start(exch, newa, newa_size);
if (rc != EOK) {
vfs_exchange_end(exch);
free(olda);
free(newa);
vfs_put(root);
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
rc = rc_orig;
return rc;
}
vfs_exchange_end(exch);
free(olda);
free(newa);
vfs_put(root);
async_wait_for(req, &rc);
return rc;
}
errno_t vfs_resize(int file, aoff64_t length)
{
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_3_0(exch, VFS_IN_RESIZE, file, LOWER32(length),
UPPER32(length));
vfs_exchange_end(exch);
return rc;
}
int vfs_root(void)
{
fibril_mutex_lock(&root_mutex);
int fd;
if (root_fd < 0) {
fd = -1;
} else {
errno_t rc = vfs_clone(root_fd, -1, true, &fd);
if (rc != EOK) {
fd = -1;
}
}
fibril_mutex_unlock(&root_mutex);
return fd;
}
errno_t vfs_root_set(int nroot)
{
int new_root;
errno_t rc = vfs_clone(nroot, -1, true, &new_root);
if (rc != EOK) {
return rc;
}
fibril_mutex_lock(&root_mutex);
if (root_fd >= 0)
vfs_put(root_fd);
root_fd = new_root;
fibril_mutex_unlock(&root_mutex);
return EOK;
}
errno_t vfs_stat(int file, vfs_stat_t *stat)
{
errno_t rc;
aid_t req;
async_exch_t *exch = vfs_exchange_begin();
req = async_send_1(exch, VFS_IN_STAT, file, NULL);
rc = async_data_read_start(exch, (void *) stat, sizeof(vfs_stat_t));
if (rc != EOK) {
vfs_exchange_end(exch);
errno_t rc_orig;
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
rc = rc_orig;
return rc;
}
vfs_exchange_end(exch);
async_wait_for(req, &rc);
return rc;
}
errno_t vfs_stat_path(const char *path, vfs_stat_t *stat)
{
int file;
errno_t rc = vfs_lookup(path, 0, &file);
if (rc != EOK)
return rc;
rc = vfs_stat(file, stat);
vfs_put(file);
return rc;
}
errno_t vfs_statfs(int file, vfs_statfs_t *st)
{
errno_t rc, ret;
aid_t req;
async_exch_t *exch = vfs_exchange_begin();
req = async_send_1(exch, VFS_IN_STATFS, file, NULL);
rc = async_data_read_start(exch, (void *) st, sizeof(*st));
vfs_exchange_end(exch);
async_wait_for(req, &ret);
rc = (ret != EOK ? ret : rc);
return rc;
}
errno_t vfs_statfs_path(const char *path, vfs_statfs_t *st)
{
int file;
errno_t rc = vfs_lookup(path, 0, &file);
if (rc != EOK)
return rc;
rc = vfs_statfs(file, st);
vfs_put(file);
return rc;
}
errno_t vfs_sync(int file)
{
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_1_0(exch, VFS_IN_SYNC, file);
vfs_exchange_end(exch);
return rc;
}
errno_t vfs_unlink(int parent, const char *child, int expect)
{
errno_t rc;
aid_t req;
async_exch_t *exch = vfs_exchange_begin();
req = async_send_2(exch, VFS_IN_UNLINK, parent, expect, NULL);
rc = async_data_write_start(exch, child, str_size(child));
vfs_exchange_end(exch);
errno_t rc_orig;
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
return (errno_t) rc_orig;
return rc;
}
errno_t vfs_unlink_path(const char *path)
{
int expect;
errno_t rc = vfs_lookup(path, 0, &expect);
if (rc != EOK)
return rc;
char *child;
int parent;
rc = get_parent_and_child(path, &parent, &child);
if (rc != EOK) {
vfs_put(expect);
return rc;
}
rc = vfs_unlink(parent, child, expect);
free(child);
vfs_put(parent);
vfs_put(expect);
return rc;
}
errno_t vfs_unmount(int mp)
{
async_exch_t *exch = vfs_exchange_begin();
errno_t rc = async_req_1_0(exch, VFS_IN_UNMOUNT, mp);
vfs_exchange_end(exch);
return rc;
}
errno_t vfs_unmount_path(const char *mpp)
{
int mp;
errno_t rc = vfs_lookup(mpp, WALK_MOUNT_POINT | WALK_DIRECTORY, &mp);
if (rc != EOK)
return rc;
rc = vfs_unmount(mp);
vfs_put(mp);
return rc;
}
errno_t vfs_walk(int parent, const char *path, int flags, int *handle)
{
async_exch_t *exch = vfs_exchange_begin();
ipc_call_t answer;
aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer);
errno_t rc = async_data_write_start(exch, path, str_size(path));
vfs_exchange_end(exch);
errno_t rc_orig;
async_wait_for(req, &rc_orig);
if (rc_orig != EOK)
return (errno_t) rc_orig;
if (rc != EOK)
return (errno_t) rc;
*handle = (int) ipc_get_arg1(&answer);
return EOK;
}
errno_t vfs_write(int file, aoff64_t *pos, const void *buf, size_t nbyte,
size_t *nwritten)
{
ssize_t cnt = 0;
ssize_t nwr = 0;
const uint8_t *bp = (uint8_t *) buf;
errno_t rc;
do {
bp += cnt;
nwr += cnt;
*pos += cnt;
rc = vfs_write_short(file, *pos, bp, nbyte - nwr, &cnt);
} while (rc == EOK && ((ssize_t)nbyte - nwr - cnt) > 0);
if (rc != EOK) {
*nwritten = nwr;
return rc;
}
nwr += cnt;
*pos += cnt;
*nwritten = nwr;
return EOK;
}
errno_t vfs_write_short(int file, aoff64_t pos, const void *buf, size_t nbyte,
ssize_t *nwritten)
{
errno_t rc;
ipc_call_t answer;
aid_t req;
if (nbyte > DATA_XFER_LIMIT)
nbyte = DATA_XFER_LIMIT;
async_exch_t *exch = vfs_exchange_begin();
req = async_send_3(exch, VFS_IN_WRITE, file, LOWER32(pos),
UPPER32(pos), &answer);
rc = async_data_write_start(exch, (void *) buf, nbyte);
vfs_exchange_end(exch);
if (rc == EOK)
async_wait_for(req, &rc);
else
async_forget(req);
if (rc != EOK)
return rc;
*nwritten = (ssize_t) ipc_get_arg1(&answer);
return EOK;
}
HelenOS homepage, sources at GitHub