2011-08-08 20:03:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Virtio 9p backend
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fsdev/qemu-fsdev.h"
|
|
|
|
#include "qemu-thread.h"
|
|
|
|
#include "qemu-coroutine.h"
|
|
|
|
#include "virtio-9p-coth.h"
|
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
|
2011-08-08 20:03:48 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
ssize_t len;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-08-08 20:03:48 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-21 05:09:37 +02:00
|
|
|
buf->data = g_malloc(PATH_MAX);
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-08-08 20:03:48 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
len = s->ops->readlink(&s->ctx, path,
|
2011-08-08 20:03:48 +02:00
|
|
|
buf->data, PATH_MAX - 1);
|
|
|
|
if (len > -1) {
|
|
|
|
buf->size = len;
|
|
|
|
buf->data[len] = 0;
|
|
|
|
err = 0;
|
|
|
|
} else {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-08-08 20:03:48 +02:00
|
|
|
if (err) {
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(buf->data);
|
2011-08-08 20:03:48 +02:00
|
|
|
buf->data = NULL;
|
|
|
|
buf->size = 0;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2011-05-19 01:03:49 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path, struct statfs *stbuf)
|
2011-05-19 01:03:49 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:03:49 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:03:49 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->statfs(&s->ctx, path, stbuf);
|
2011-05-19 01:03:49 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:03:49 +02:00
|
|
|
return err;
|
|
|
|
}
|
2011-05-19 01:04:58 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
|
2011-05-19 01:04:58 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
FsCred cred;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:04:58 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-05-19 01:04:58 +02:00
|
|
|
cred_init(&cred);
|
|
|
|
cred.fc_mode = mode;
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->chmod(&s->ctx, path, &cred);
|
2011-05-19 01:04:58 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
|
2011-05-19 01:04:58 +02:00
|
|
|
struct timespec times[2])
|
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:04:58 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->utimensat(&s->ctx, path, times);
|
2011-05-19 01:04:58 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid, gid_t gid)
|
2011-05-19 01:04:58 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
FsCred cred;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:04:58 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-05-19 01:04:58 +02:00
|
|
|
cred_init(&cred);
|
|
|
|
cred.fc_uid = uid;
|
|
|
|
cred.fc_gid = gid;
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->chown(&s->ctx, path, &cred);
|
2011-05-19 01:04:58 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
|
2011-05-19 01:04:58 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:04:58 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->truncate(&s->ctx, path, size);
|
2011-05-19 01:04:58 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:04:58 +02:00
|
|
|
return err;
|
|
|
|
}
|
2011-05-19 01:06:38 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, uid_t uid,
|
2011-05-24 11:40:56 +02:00
|
|
|
gid_t gid, dev_t dev, mode_t mode, struct stat *stbuf)
|
2011-05-19 01:06:38 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-09-09 11:44:18 +02:00
|
|
|
V9fsPath path;
|
2011-05-19 01:06:38 +02:00
|
|
|
FsCred cred;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-19 01:06:38 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-05-19 01:06:38 +02:00
|
|
|
cred_init(&cred);
|
|
|
|
cred.fc_uid = uid;
|
|
|
|
cred.fc_gid = gid;
|
|
|
|
cred.fc_mode = mode;
|
|
|
|
cred.fc_rdev = dev;
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-05-19 01:06:38 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
|
2011-05-19 01:06:38 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
2011-05-24 11:40:56 +02:00
|
|
|
} else {
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_path_init(&path);
|
|
|
|
err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
|
|
|
|
if (!err) {
|
|
|
|
err = s->ops->lstat(&s->ctx, &path, stbuf);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
2011-05-24 11:40:56 +02:00
|
|
|
}
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_path_free(&path);
|
2011-05-19 01:06:38 +02:00
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-05-19 01:06:38 +02:00
|
|
|
return err;
|
|
|
|
}
|
2011-08-08 20:18:29 +02:00
|
|
|
|
2011-09-09 11:44:18 +02:00
|
|
|
/* Only works with path name based fid */
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
|
2011-08-08 20:18:29 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-08-08 20:18:29 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-08-08 20:18:29 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
|
|
|
err = s->ops->remove(&s->ctx, path->data);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-08-08 20:18:29 +02:00
|
|
|
return err;
|
|
|
|
}
|
2011-05-07 12:39:10 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path, V9fsString *name, int flags)
|
2011-09-09 11:44:18 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-09-09 11:44:18 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
|
|
|
err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-09-09 11:44:18 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only work with path name based fid */
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath, V9fsPath *newpath)
|
2011-05-07 12:39:10 +02:00
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-07 12:39:10 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-05-07 12:39:10 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
|
|
|
err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return err;
|
|
|
|
}
|
2011-08-08 20:28:39 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath, V9fsString *oldname,
|
2011-09-09 11:44:18 +02:00
|
|
|
V9fsPath *newdirpath, V9fsString *newname)
|
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-09-09 11:44:18 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
|
|
|
err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
|
|
|
|
newdirpath, newname->data);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp, V9fsString *name,
|
2011-05-24 11:40:56 +02:00
|
|
|
const char *oldpath, gid_t gid, struct stat *stbuf)
|
2011-08-08 20:28:39 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
FsCred cred;
|
2011-09-09 11:44:18 +02:00
|
|
|
V9fsPath path;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-05-24 11:40:56 +02:00
|
|
|
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-08 20:28:39 +02:00
|
|
|
cred_init(&cred);
|
2011-05-24 11:40:56 +02:00
|
|
|
cred.fc_uid = dfidp->uid;
|
2011-08-08 20:28:39 +02:00
|
|
|
cred.fc_gid = gid;
|
|
|
|
cred.fc_mode = 0777;
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_read_lock(s);
|
2011-08-08 20:28:39 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
2011-09-09 11:44:18 +02:00
|
|
|
err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
|
|
|
|
name->data, &cred);
|
2011-08-08 20:28:39 +02:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
2011-05-24 11:40:56 +02:00
|
|
|
} else {
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_path_init(&path);
|
|
|
|
err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
|
|
|
|
if (!err) {
|
|
|
|
err = s->ops->lstat(&s->ctx, &path, stbuf);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
2011-05-24 11:40:56 +02:00
|
|
|
}
|
2011-09-09 11:44:18 +02:00
|
|
|
v9fs_path_free(&path);
|
2011-08-08 20:28:39 +02:00
|
|
|
}
|
|
|
|
});
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_path_unlock(s);
|
2011-09-09 11:44:18 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For path name based fid we don't block. So we can
|
|
|
|
* directly call the fs driver ops.
|
|
|
|
*/
|
2011-08-02 08:06:17 +02:00
|
|
|
int v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
|
2011-09-09 11:44:18 +02:00
|
|
|
const char *name, V9fsPath *path)
|
|
|
|
{
|
|
|
|
int err;
|
2011-08-02 08:06:17 +02:00
|
|
|
V9fsState *s = pdu->s;
|
2011-08-02 08:05:54 +02:00
|
|
|
|
2011-10-12 17:29:18 +02:00
|
|
|
if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
|
2011-08-02 08:05:54 +02:00
|
|
|
err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-02 08:06:17 +02:00
|
|
|
if (v9fs_request_cancelled(pdu)) {
|
|
|
|
return -EINTR;
|
|
|
|
}
|
2011-08-02 08:05:54 +02:00
|
|
|
v9fs_co_run_in_worker(
|
|
|
|
{
|
|
|
|
err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
});
|
2011-09-09 11:44:18 +02:00
|
|
|
}
|
2011-08-08 20:28:39 +02:00
|
|
|
return err;
|
|
|
|
}
|