summaryrefslogtreecommitdiffstats
path: root/src/hw/9pfs
diff options
context:
space:
mode:
Diffstat (limited to 'src/hw/9pfs')
-rw-r--r--src/hw/9pfs/Makefile.objs9
-rw-r--r--src/hw/9pfs/codir.c168
-rw-r--r--src/hw/9pfs/cofile.c275
-rw-r--r--src/hw/9pfs/cofs.c364
-rw-r--r--src/hw/9pfs/coxattr.c107
-rw-r--r--src/hw/9pfs/virtio-9p-coth.c41
-rw-r--r--src/hw/9pfs/virtio-9p-coth.h99
-rw-r--r--src/hw/9pfs/virtio-9p-device.c192
-rw-r--r--src/hw/9pfs/virtio-9p-handle.c708
-rw-r--r--src/hw/9pfs/virtio-9p-local.c1280
-rw-r--r--src/hw/9pfs/virtio-9p-posix-acl.c185
-rw-r--r--src/hw/9pfs/virtio-9p-proxy.c1219
-rw-r--r--src/hw/9pfs/virtio-9p-proxy.h95
-rw-r--r--src/hw/9pfs/virtio-9p-synth.c573
-rw-r--r--src/hw/9pfs/virtio-9p-synth.h54
-rw-r--r--src/hw/9pfs/virtio-9p-xattr-user.c128
-rw-r--r--src/hw/9pfs/virtio-9p-xattr.c164
-rw-r--r--src/hw/9pfs/virtio-9p-xattr.h120
-rw-r--r--src/hw/9pfs/virtio-9p.c3300
-rw-r--r--src/hw/9pfs/virtio-9p.h394
20 files changed, 9475 insertions, 0 deletions
diff --git a/src/hw/9pfs/Makefile.objs b/src/hw/9pfs/Makefile.objs
new file mode 100644
index 0000000..1e9b595
--- /dev/null
+++ b/src/hw/9pfs/Makefile.objs
@@ -0,0 +1,9 @@
+common-obj-y = virtio-9p.o
+common-obj-y += virtio-9p-local.o virtio-9p-xattr.o
+common-obj-y += virtio-9p-xattr-user.o virtio-9p-posix-acl.o
+common-obj-y += virtio-9p-coth.o cofs.o codir.o cofile.o
+common-obj-y += coxattr.o virtio-9p-synth.o
+common-obj-$(CONFIG_OPEN_BY_HANDLE) += virtio-9p-handle.o
+common-obj-y += virtio-9p-proxy.o
+
+obj-y += virtio-9p-device.o
diff --git a/src/hw/9pfs/codir.c b/src/hw/9pfs/codir.c
new file mode 100644
index 0000000..ec9cc7f
--- /dev/null
+++ b/src/hw/9pfs/codir.c
@@ -0,0 +1,168 @@
+
+/*
+ * 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"
+
+int v9fs_co_readdir_r(V9fsPDU *pdu, V9fsFidState *fidp, struct dirent *dent,
+ struct dirent **result)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ errno = 0;
+ err = s->ops->readdir_r(&s->ctx, &fidp->fs, dent, result);
+ if (!*result && errno) {
+ err = -errno;
+ } else {
+ err = 0;
+ }
+ });
+ return err;
+}
+
+off_t v9fs_co_telldir(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ off_t err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->telldir(&s->ctx, &fidp->fs);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
+
+void v9fs_co_seekdir(V9fsPDU *pdu, V9fsFidState *fidp, off_t offset)
+{
+ V9fsState *s = pdu->s;
+ if (v9fs_request_cancelled(pdu)) {
+ return;
+ }
+ v9fs_co_run_in_worker(
+ {
+ s->ops->seekdir(&s->ctx, &fidp->fs, offset);
+ });
+}
+
+void v9fs_co_rewinddir(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ V9fsState *s = pdu->s;
+ if (v9fs_request_cancelled(pdu)) {
+ return;
+ }
+ v9fs_co_run_in_worker(
+ {
+ s->ops->rewinddir(&s->ctx, &fidp->fs);
+ });
+}
+
+int v9fs_co_mkdir(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name,
+ mode_t mode, uid_t uid, gid_t gid, struct stat *stbuf)
+{
+ int err;
+ FsCred cred;
+ V9fsPath path;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_mode = mode;
+ cred.fc_uid = uid;
+ cred.fc_gid = gid;
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->mkdir(&s->ctx, &fidp->path, name->data, &cred);
+ if (err < 0) {
+ err = -errno;
+ } else {
+ 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;
+ }
+ }
+ v9fs_path_free(&path);
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_opendir(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->opendir(&s->ctx, &fidp->path, &fidp->fs);
+ if (err < 0) {
+ err = -errno;
+ } else {
+ err = 0;
+ }
+ });
+ v9fs_path_unlock(s);
+ if (!err) {
+ total_open_fd++;
+ if (total_open_fd > open_fd_hw) {
+ v9fs_reclaim_fd(pdu);
+ }
+ }
+ return err;
+}
+
+int v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->closedir(&s->ctx, fs);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ if (!err) {
+ total_open_fd--;
+ }
+ return err;
+}
diff --git a/src/hw/9pfs/cofile.c b/src/hw/9pfs/cofile.c
new file mode 100644
index 0000000..7cb55ee
--- /dev/null
+++ b/src/hw/9pfs/cofile.c
@@ -0,0 +1,275 @@
+
+/*
+ * 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"
+
+int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,
+ V9fsStatDotl *v9stat)
+{
+ int err = 0;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ if (s->ctx.exops.get_st_gen) {
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ctx.exops.get_st_gen(&s->ctx, path, st_mode,
+ &v9stat->st_gen);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ }
+ return err;
+}
+
+int v9fs_co_lstat(V9fsPDU *pdu, V9fsPath *path, struct stat *stbuf)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->lstat(&s->ctx, path, stbuf);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp, struct stat *stbuf)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ /*
+ * Some FS driver (local:mapped-file) can't support fetching attributes
+ * using file descriptor. Use Path name in that case.
+ */
+ if (err == -EOPNOTSUPP) {
+ err = v9fs_co_lstat(pdu, &fidp->path, stbuf);
+ if (err == -ENOENT) {
+ /*
+ * fstat on an unlinked file. Work with partial results
+ * returned from s->ops->fstat
+ */
+ err = 0;
+ }
+ }
+ return err;
+}
+
+int v9fs_co_open(V9fsPDU *pdu, V9fsFidState *fidp, int flags)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->open(&s->ctx, &fidp->path, flags, &fidp->fs);
+ if (err == -1) {
+ err = -errno;
+ } else {
+ err = 0;
+ }
+ });
+ v9fs_path_unlock(s);
+ if (!err) {
+ total_open_fd++;
+ if (total_open_fd > open_fd_hw) {
+ v9fs_reclaim_fd(pdu);
+ }
+ }
+ return err;
+}
+
+int v9fs_co_open2(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, gid_t gid,
+ int flags, int mode, struct stat *stbuf)
+{
+ int err;
+ FsCred cred;
+ V9fsPath path;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_mode = mode & 07777;
+ cred.fc_uid = fidp->uid;
+ cred.fc_gid = gid;
+ /*
+ * Hold the directory fid lock so that directory path name
+ * don't change. Read lock is fine because this fid cannot
+ * be used by any other operation.
+ */
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->open2(&s->ctx, &fidp->path,
+ name->data, flags, &cred, &fidp->fs);
+ if (err < 0) {
+ err = -errno;
+ } else {
+ 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;
+ s->ops->close(&s->ctx, &fidp->fs);
+ } else {
+ v9fs_path_copy(&fidp->path, &path);
+ }
+ } else {
+ s->ops->close(&s->ctx, &fidp->fs);
+ }
+ v9fs_path_free(&path);
+ }
+ });
+ v9fs_path_unlock(s);
+ if (!err) {
+ total_open_fd++;
+ if (total_open_fd > open_fd_hw) {
+ v9fs_reclaim_fd(pdu);
+ }
+ }
+ return err;
+}
+
+int v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->close(&s->ctx, fs);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ if (!err) {
+ total_open_fd--;
+ }
+ return err;
+}
+
+int v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
+
+int v9fs_co_link(V9fsPDU *pdu, V9fsFidState *oldfid,
+ V9fsFidState *newdirfid, V9fsString *name)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->link(&s->ctx, &oldfid->path,
+ &newdirfid->path, name->data);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_pwritev(V9fsPDU *pdu, V9fsFidState *fidp,
+ struct iovec *iov, int iovcnt, int64_t offset)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->pwritev(&s->ctx, &fidp->fs, iov, iovcnt, offset);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
+
+int v9fs_co_preadv(V9fsPDU *pdu, V9fsFidState *fidp,
+ struct iovec *iov, int iovcnt, int64_t offset)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->preadv(&s->ctx, &fidp->fs, iov, iovcnt, offset);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
diff --git a/src/hw/9pfs/cofs.c b/src/hw/9pfs/cofs.c
new file mode 100644
index 0000000..e1953a9
--- /dev/null
+++ b/src/hw/9pfs/cofs.c
@@ -0,0 +1,364 @@
+
+/*
+ * 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"
+
+static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
+{
+ ssize_t len, maxlen = PATH_MAX;
+
+ buf->data = g_malloc(PATH_MAX);
+ for(;;) {
+ len = s->ops->readlink(&s->ctx, path, buf->data, maxlen);
+ if (len < 0) {
+ g_free(buf->data);
+ buf->data = NULL;
+ buf->size = 0;
+ break;
+ } else if (len == maxlen) {
+ /*
+ * We dodn't have space to put the NULL or we have more
+ * to read. Increase the size and try again
+ */
+ maxlen *= 2;
+ g_free(buf->data);
+ buf->data = g_malloc(maxlen);
+ continue;
+ }
+ /*
+ * Null terminate the readlink output
+ */
+ buf->data[len] = '\0';
+ buf->size = len;
+ break;
+ }
+ return len;
+}
+
+int v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = __readlink(s, path, buf);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path, struct statfs *stbuf)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->statfs(&s->ctx, path, stbuf);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
+{
+ int err;
+ FsCred cred;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_mode = mode;
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->chmod(&s->ctx, path, &cred);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
+ struct timespec times[2])
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->utimensat(&s->ctx, path, times);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid, gid_t gid)
+{
+ int err;
+ FsCred cred;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_uid = uid;
+ cred.fc_gid = gid;
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->chown(&s->ctx, path, &cred);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->truncate(&s->ctx, path, size);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, uid_t uid,
+ gid_t gid, dev_t dev, mode_t mode, struct stat *stbuf)
+{
+ int err;
+ V9fsPath path;
+ FsCred cred;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_uid = uid;
+ cred.fc_gid = gid;
+ cred.fc_mode = mode;
+ cred.fc_rdev = dev;
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
+ if (err < 0) {
+ err = -errno;
+ } else {
+ 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;
+ }
+ }
+ v9fs_path_free(&path);
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+/* Only works with path name based fid */
+int v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->remove(&s->ctx, path->data);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path, V9fsString *name, int flags)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+/* Only work with path name based fid */
+int v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath, V9fsPath *newpath)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
+
+int v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath, V9fsString *oldname,
+ V9fsPath *newdirpath, V9fsString *newname)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
+ newdirpath, newname->data);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ return err;
+}
+
+int v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp, V9fsString *name,
+ const char *oldpath, gid_t gid, struct stat *stbuf)
+{
+ int err;
+ FsCred cred;
+ V9fsPath path;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ cred_init(&cred);
+ cred.fc_uid = dfidp->uid;
+ cred.fc_gid = gid;
+ cred.fc_mode = 0777;
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
+ name->data, &cred);
+ if (err < 0) {
+ err = -errno;
+ } else {
+ 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;
+ }
+ }
+ v9fs_path_free(&path);
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+/*
+ * For path name based fid we don't block. So we can
+ * directly call the fs driver ops.
+ */
+int v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
+ const char *name, V9fsPath *path)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
+ err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
+ if (err < 0) {
+ err = -errno;
+ }
+ } else {
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ }
+ return err;
+}
diff --git a/src/hw/9pfs/coxattr.c b/src/hw/9pfs/coxattr.c
new file mode 100644
index 0000000..55c0d23
--- /dev/null
+++ b/src/hw/9pfs/coxattr.c
@@ -0,0 +1,107 @@
+
+/*
+ * 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"
+
+int v9fs_co_llistxattr(V9fsPDU *pdu, V9fsPath *path, void *value, size_t size)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->llistxattr(&s->ctx, path, value, size);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_lgetxattr(V9fsPDU *pdu, V9fsPath *path,
+ V9fsString *xattr_name,
+ void *value, size_t size)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->lgetxattr(&s->ctx, path,
+ xattr_name->data,
+ value, size);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_lsetxattr(V9fsPDU *pdu, V9fsPath *path,
+ V9fsString *xattr_name, void *value,
+ size_t size, int flags)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->lsetxattr(&s->ctx, path,
+ xattr_name->data, value,
+ size, flags);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
+
+int v9fs_co_lremovexattr(V9fsPDU *pdu, V9fsPath *path,
+ V9fsString *xattr_name)
+{
+ int err;
+ V9fsState *s = pdu->s;
+
+ if (v9fs_request_cancelled(pdu)) {
+ return -EINTR;
+ }
+ v9fs_path_read_lock(s);
+ v9fs_co_run_in_worker(
+ {
+ err = s->ops->lremovexattr(&s->ctx, path, xattr_name->data);
+ if (err < 0) {
+ err = -errno;
+ }
+ });
+ v9fs_path_unlock(s);
+ return err;
+}
diff --git a/src/hw/9pfs/virtio-9p-coth.c b/src/hw/9pfs/virtio-9p-coth.c
new file mode 100644
index 0000000..ab9425c
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-coth.c
@@ -0,0 +1,41 @@
+/*
+ * Virtio 9p backend
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * Authors:
+ * Harsh Prateek Bora <harsh@linux.vnet.ibm.com>
+ * Venkateswararao Jujjuri(JV) <jvrao@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 "qemu-common.h"
+#include "block/thread-pool.h"
+#include "qemu/coroutine.h"
+#include "qemu/main-loop.h"
+#include "virtio-9p-coth.h"
+
+/* Called from QEMU I/O thread. */
+static void coroutine_enter_cb(void *opaque, int ret)
+{
+ Coroutine *co = opaque;
+ qemu_coroutine_enter(co, NULL);
+}
+
+/* Called from worker thread. */
+static int coroutine_enter_func(void *arg)
+{
+ Coroutine *co = arg;
+ qemu_coroutine_enter(co, NULL);
+ return 0;
+}
+
+void co_run_in_worker_bh(void *opaque)
+{
+ Coroutine *co = opaque;
+ thread_pool_submit_aio(aio_get_thread_pool(qemu_get_aio_context()),
+ coroutine_enter_func, co, coroutine_enter_cb, co);
+}
diff --git a/src/hw/9pfs/virtio-9p-coth.h b/src/hw/9pfs/virtio-9p-coth.h
new file mode 100644
index 0000000..4ac1aaf
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-coth.h
@@ -0,0 +1,99 @@
+/*
+ * Virtio 9p backend
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * Authors:
+ * Harsh Prateek Bora <harsh@linux.vnet.ibm.com>
+ * Venkateswararao Jujjuri(JV) <jvrao@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.
+ *
+ */
+
+#ifndef _QEMU_VIRTIO_9P_COTH_H
+#define _QEMU_VIRTIO_9P_COTH_H
+
+#include "qemu/thread.h"
+#include "qemu/coroutine.h"
+#include "virtio-9p.h"
+
+/*
+ * we want to use bottom half because we want to make sure the below
+ * sequence of events.
+ *
+ * 1. Yield the coroutine in the QEMU thread.
+ * 2. Submit the coroutine to a worker thread.
+ * 3. Enter the coroutine in the worker thread.
+ * we cannot swap step 1 and 2, because that would imply worker thread
+ * can enter coroutine while step1 is still running
+ */
+#define v9fs_co_run_in_worker(code_block) \
+ do { \
+ QEMUBH *co_bh; \
+ co_bh = qemu_bh_new(co_run_in_worker_bh, \
+ qemu_coroutine_self()); \
+ qemu_bh_schedule(co_bh); \
+ /* \
+ * yield in qemu thread and re-enter back \
+ * in worker thread \
+ */ \
+ qemu_coroutine_yield(); \
+ qemu_bh_delete(co_bh); \
+ code_block; \
+ /* re-enter back to qemu thread */ \
+ qemu_coroutine_yield(); \
+ } while (0)
+
+extern void co_run_in_worker_bh(void *);
+extern int v9fs_init_worker_threads(void);
+extern int v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *);
+extern int v9fs_co_readdir_r(V9fsPDU *, V9fsFidState *,
+ struct dirent *, struct dirent **result);
+extern off_t v9fs_co_telldir(V9fsPDU *, V9fsFidState *);
+extern void v9fs_co_seekdir(V9fsPDU *, V9fsFidState *, off_t);
+extern void v9fs_co_rewinddir(V9fsPDU *, V9fsFidState *);
+extern int v9fs_co_statfs(V9fsPDU *, V9fsPath *, struct statfs *);
+extern int v9fs_co_lstat(V9fsPDU *, V9fsPath *, struct stat *);
+extern int v9fs_co_chmod(V9fsPDU *, V9fsPath *, mode_t);
+extern int v9fs_co_utimensat(V9fsPDU *, V9fsPath *, struct timespec [2]);
+extern int v9fs_co_chown(V9fsPDU *, V9fsPath *, uid_t, gid_t);
+extern int v9fs_co_truncate(V9fsPDU *, V9fsPath *, off_t);
+extern int v9fs_co_llistxattr(V9fsPDU *, V9fsPath *, void *, size_t);
+extern int v9fs_co_lgetxattr(V9fsPDU *, V9fsPath *,
+ V9fsString *, void *, size_t);
+extern int v9fs_co_mknod(V9fsPDU *, V9fsFidState *, V9fsString *, uid_t,
+ gid_t, dev_t, mode_t, struct stat *);
+extern int v9fs_co_mkdir(V9fsPDU *, V9fsFidState *, V9fsString *,
+ mode_t, uid_t, gid_t, struct stat *);
+extern int v9fs_co_remove(V9fsPDU *, V9fsPath *);
+extern int v9fs_co_rename(V9fsPDU *, V9fsPath *, V9fsPath *);
+extern int v9fs_co_unlinkat(V9fsPDU *, V9fsPath *, V9fsString *, int flags);
+extern int v9fs_co_renameat(V9fsPDU *, V9fsPath *, V9fsString *,
+ V9fsPath *, V9fsString *);
+extern int v9fs_co_fstat(V9fsPDU *, V9fsFidState *, struct stat *);
+extern int v9fs_co_opendir(V9fsPDU *, V9fsFidState *);
+extern int v9fs_co_open(V9fsPDU *, V9fsFidState *, int);
+extern int v9fs_co_open2(V9fsPDU *, V9fsFidState *, V9fsString *,
+ gid_t, int, int, struct stat *);
+extern int v9fs_co_lsetxattr(V9fsPDU *, V9fsPath *, V9fsString *,
+ void *, size_t, int);
+extern int v9fs_co_lremovexattr(V9fsPDU *, V9fsPath *, V9fsString *);
+extern int v9fs_co_closedir(V9fsPDU *, V9fsFidOpenState *);
+extern int v9fs_co_close(V9fsPDU *, V9fsFidOpenState *);
+extern int v9fs_co_fsync(V9fsPDU *, V9fsFidState *, int);
+extern int v9fs_co_symlink(V9fsPDU *, V9fsFidState *, V9fsString *,
+ const char *, gid_t, struct stat *);
+extern int v9fs_co_link(V9fsPDU *, V9fsFidState *,
+ V9fsFidState *, V9fsString *);
+extern int v9fs_co_pwritev(V9fsPDU *, V9fsFidState *,
+ struct iovec *, int, int64_t);
+extern int v9fs_co_preadv(V9fsPDU *, V9fsFidState *,
+ struct iovec *, int, int64_t);
+extern int v9fs_co_name_to_path(V9fsPDU *, V9fsPath *,
+ const char *, V9fsPath *);
+extern int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t,
+ V9fsStatDotl *v9stat);
+
+#endif
diff --git a/src/hw/9pfs/virtio-9p-device.c b/src/hw/9pfs/virtio-9p-device.c
new file mode 100644
index 0000000..b42d3b3
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-device.c
@@ -0,0 +1,192 @@
+/*
+ * Virtio 9p backend
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.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 "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-9p.h"
+#include "hw/i386/pc.h"
+#include "qemu/sockets.h"
+#include "virtio-9p.h"
+#include "fsdev/qemu-fsdev.h"
+#include "virtio-9p-xattr.h"
+#include "virtio-9p-coth.h"
+#include "hw/virtio/virtio-access.h"
+
+static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
+ Error **errp)
+{
+ virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
+ return features;
+}
+
+static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+ int len;
+ struct virtio_9p_config *cfg;
+ V9fsState *s = VIRTIO_9P(vdev);
+
+ len = strlen(s->tag);
+ cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
+ virtio_stw_p(vdev, &cfg->tag_len, len);
+ /* We don't copy the terminating null to config space */
+ memcpy(cfg->tag, s->tag, len);
+ memcpy(config, cfg, s->config_size);
+ g_free(cfg);
+}
+
+static void virtio_9p_save(QEMUFile *f, void *opaque)
+{
+ virtio_save(VIRTIO_DEVICE(opaque), f);
+}
+
+static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id)
+{
+ return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
+}
+
+static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ V9fsState *s = VIRTIO_9P(dev);
+ int i, len;
+ struct stat stat;
+ FsDriverEntry *fse;
+ V9fsPath path;
+
+ virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P,
+ sizeof(struct virtio_9p_config) + MAX_TAG_LEN);
+
+ /* initialize pdu allocator */
+ QLIST_INIT(&s->free_list);
+ QLIST_INIT(&s->active_list);
+ for (i = 0; i < (MAX_REQ - 1); i++) {
+ QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
+ }
+
+ s->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
+
+ v9fs_path_init(&path);
+
+ fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
+
+ if (!fse) {
+ /* We don't have a fsdev identified by fsdev_id */
+ error_setg(errp, "Virtio-9p device couldn't find fsdev with the "
+ "id = %s",
+ s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
+ goto out;
+ }
+
+ if (!s->fsconf.tag) {
+ /* we haven't specified a mount_tag */
+ error_setg(errp, "fsdev with id %s needs mount_tag arguments",
+ s->fsconf.fsdev_id);
+ goto out;
+ }
+
+ s->ctx.export_flags = fse->export_flags;
+ s->ctx.fs_root = g_strdup(fse->path);
+ s->ctx.exops.get_st_gen = NULL;
+ len = strlen(s->fsconf.tag);
+ if (len > MAX_TAG_LEN - 1) {
+ error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
+ "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
+ goto out;
+ }
+
+ s->tag = g_strdup(s->fsconf.tag);
+ s->ctx.uid = -1;
+
+ s->ops = fse->ops;
+ s->config_size = sizeof(struct virtio_9p_config) + len;
+ s->fid_list = NULL;
+ qemu_co_rwlock_init(&s->rename_lock);
+
+ if (s->ops->init(&s->ctx) < 0) {
+ error_setg(errp, "Virtio-9p Failed to initialize fs-driver with id:%s"
+ " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
+ goto out;
+ }
+
+ /*
+ * Check details of export path, We need to use fs driver
+ * call back to do that. Since we are in the init path, we don't
+ * use co-routines here.
+ */
+ if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
+ error_setg(errp,
+ "error in converting name to path %s", strerror(errno));
+ goto out;
+ }
+ if (s->ops->lstat(&s->ctx, &path, &stat)) {
+ error_setg(errp, "share path %s does not exist", fse->path);
+ goto out;
+ } else if (!S_ISDIR(stat.st_mode)) {
+ error_setg(errp, "share path %s is not a directory", fse->path);
+ goto out;
+ }
+ v9fs_path_free(&path);
+
+ register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, s);
+ return;
+out:
+ g_free(s->ctx.fs_root);
+ g_free(s->tag);
+ virtio_cleanup(vdev);
+ v9fs_path_free(&path);
+}
+
+static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ V9fsState *s = VIRTIO_9P(dev);
+
+ virtio_cleanup(vdev);
+ unregister_savevm(dev, "virtio-9p", s);
+ g_free(s->ctx.fs_root);
+ g_free(s->tag);
+}
+
+/* virtio-9p device */
+
+static Property virtio_9p_properties[] = {
+ DEFINE_PROP_STRING("mount_tag", V9fsState, fsconf.tag),
+ DEFINE_PROP_STRING("fsdev", V9fsState, fsconf.fsdev_id),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void virtio_9p_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+ dc->props = virtio_9p_properties;
+ set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
+ vdc->realize = virtio_9p_device_realize;
+ vdc->unrealize = virtio_9p_device_unrealize;
+ vdc->get_features = virtio_9p_get_features;
+ vdc->get_config = virtio_9p_get_config;
+}
+
+static const TypeInfo virtio_device_info = {
+ .name = TYPE_VIRTIO_9P,
+ .parent = TYPE_VIRTIO_DEVICE,
+ .instance_size = sizeof(V9fsState),
+ .class_init = virtio_9p_class_init,
+};
+
+static void virtio_9p_register_types(void)
+{
+ type_register_static(&virtio_device_info);
+}
+
+type_init(virtio_9p_register_types)
diff --git a/src/hw/9pfs/virtio-9p-handle.c b/src/hw/9pfs/virtio-9p-handle.c
new file mode 100644
index 0000000..13eabb9
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-handle.c
@@ -0,0 +1,708 @@
+/*
+ * Virtio 9p handle callback
+ *
+ * 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 "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "virtio-9p-xattr.h"
+#include <arpa/inet.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include "qemu/xattr.h"
+#include <unistd.h>
+#include <linux/fs.h>
+#ifdef CONFIG_LINUX_MAGIC_H
+#include <linux/magic.h>
+#endif
+#include <sys/ioctl.h>
+
+#ifndef XFS_SUPER_MAGIC
+#define XFS_SUPER_MAGIC 0x58465342
+#endif
+#ifndef EXT2_SUPER_MAGIC
+#define EXT2_SUPER_MAGIC 0xEF53
+#endif
+#ifndef REISERFS_SUPER_MAGIC
+#define REISERFS_SUPER_MAGIC 0x52654973
+#endif
+#ifndef BTRFS_SUPER_MAGIC
+#define BTRFS_SUPER_MAGIC 0x9123683E
+#endif
+
+struct handle_data {
+ int mountfd;
+ int handle_bytes;
+};
+
+static inline int name_to_handle(int dirfd, const char *name,
+ struct file_handle *fh, int *mnt_id, int flags)
+{
+ return name_to_handle_at(dirfd, name, fh, mnt_id, flags);
+}
+
+static inline int open_by_handle(int mountfd, const char *fh, int flags)
+{
+ return open_by_handle_at(mountfd, (struct file_handle *)fh, flags);
+}
+
+static int handle_update_file_cred(int dirfd, const char *name, FsCred *credp)
+{
+ int fd, ret;
+ fd = openat(dirfd, name, O_NONBLOCK | O_NOFOLLOW);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
+ if (ret < 0) {
+ goto err_out;
+ }
+ ret = fchmod(fd, credp->fc_mode & 07777);
+err_out:
+ close(fd);
+ return ret;
+}
+
+
+static int handle_lstat(FsContext *fs_ctx, V9fsPath *fs_path,
+ struct stat *stbuf)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fstatat(fd, "", stbuf, AT_EMPTY_PATH);
+ close(fd);
+ return ret;
+}
+
+static ssize_t handle_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
+ char *buf, size_t bufsz)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = readlinkat(fd, "", buf, bufsz);
+ close(fd);
+ return ret;
+}
+
+static int handle_close(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return close(fs->fd);
+}
+
+static int handle_closedir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return closedir(fs->dir);
+}
+
+static int handle_open(FsContext *ctx, V9fsPath *fs_path,
+ int flags, V9fsFidOpenState *fs)
+{
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fs->fd = open_by_handle(data->mountfd, fs_path->data, flags);
+ return fs->fd;
+}
+
+static int handle_opendir(FsContext *ctx,
+ V9fsPath *fs_path, V9fsFidOpenState *fs)
+{
+ int ret;
+ ret = handle_open(ctx, fs_path, O_DIRECTORY, fs);
+ if (ret < 0) {
+ return -1;
+ }
+ fs->dir = fdopendir(ret);
+ if (!fs->dir) {
+ return -1;
+ }
+ return 0;
+}
+
+static void handle_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ rewinddir(fs->dir);
+}
+
+static off_t handle_telldir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return telldir(fs->dir);
+}
+
+static int handle_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
+ struct dirent *entry,
+ struct dirent **result)
+{
+ return readdir_r(fs->dir, entry, result);
+}
+
+static void handle_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
+{
+ seekdir(fs->dir, off);
+}
+
+static ssize_t handle_preadv(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+#ifdef CONFIG_PREADV
+ return preadv(fs->fd, iov, iovcnt, offset);
+#else
+ int err = lseek(fs->fd, offset, SEEK_SET);
+ if (err == -1) {
+ return err;
+ } else {
+ return readv(fs->fd, iov, iovcnt);
+ }
+#endif
+}
+
+static ssize_t handle_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ ssize_t ret;
+#ifdef CONFIG_PREADV
+ ret = pwritev(fs->fd, iov, iovcnt, offset);
+#else
+ int err = lseek(fs->fd, offset, SEEK_SET);
+ if (err == -1) {
+ return err;
+ } else {
+ ret = writev(fs->fd, iov, iovcnt);
+ }
+#endif
+#ifdef CONFIG_SYNC_FILE_RANGE
+ if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
+ /*
+ * Initiate a writeback. This is not a data integrity sync.
+ * We want to ensure that we don't leave dirty pages in the cache
+ * after write when writeout=immediate is sepcified.
+ */
+ sync_file_range(fs->fd, offset, ret,
+ SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
+ }
+#endif
+ return ret;
+}
+
+static int handle_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fchmod(fd, credp->fc_mode);
+ close(fd);
+ return ret;
+}
+
+static int handle_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ int dirfd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
+ if (dirfd < 0) {
+ return dirfd;
+ }
+ ret = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
+ if (!ret) {
+ ret = handle_update_file_cred(dirfd, name, credp);
+ }
+ close(dirfd);
+ return ret;
+}
+
+static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ int dirfd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
+ if (dirfd < 0) {
+ return dirfd;
+ }
+ ret = mkdirat(dirfd, name, credp->fc_mode);
+ if (!ret) {
+ ret = handle_update_file_cred(dirfd, name, credp);
+ }
+ close(dirfd);
+ return ret;
+}
+
+static int handle_fstat(FsContext *fs_ctx, int fid_type,
+ V9fsFidOpenState *fs, struct stat *stbuf)
+{
+ int fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+ return fstat(fd, stbuf);
+}
+
+static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
+ int flags, FsCred *credp, V9fsFidOpenState *fs)
+{
+ int ret;
+ int dirfd, fd;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
+ if (dirfd < 0) {
+ return dirfd;
+ }
+ fd = openat(dirfd, name, flags | O_NOFOLLOW, credp->fc_mode);
+ if (fd >= 0) {
+ ret = handle_update_file_cred(dirfd, name, credp);
+ if (ret < 0) {
+ close(fd);
+ fd = ret;
+ } else {
+ fs->fd = fd;
+ }
+ }
+ close(dirfd);
+ return fd;
+}
+
+
+static int handle_symlink(FsContext *fs_ctx, const char *oldpath,
+ V9fsPath *dir_path, const char *name, FsCred *credp)
+{
+ int fd, dirfd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
+ if (dirfd < 0) {
+ return dirfd;
+ }
+ ret = symlinkat(oldpath, dirfd, name);
+ if (!ret) {
+ fd = openat(dirfd, name, O_PATH | O_NOFOLLOW);
+ if (fd < 0) {
+ ret = fd;
+ goto err_out;
+ }
+ ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
+ close(fd);
+ }
+err_out:
+ close(dirfd);
+ return ret;
+}
+
+static int handle_link(FsContext *ctx, V9fsPath *oldpath,
+ V9fsPath *dirpath, const char *name)
+{
+ int oldfd, newdirfd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ oldfd = open_by_handle(data->mountfd, oldpath->data, O_PATH);
+ if (oldfd < 0) {
+ return oldfd;
+ }
+ newdirfd = open_by_handle(data->mountfd, dirpath->data, O_PATH);
+ if (newdirfd < 0) {
+ close(oldfd);
+ return newdirfd;
+ }
+ ret = linkat(oldfd, "", newdirfd, name, AT_EMPTY_PATH);
+ close(newdirfd);
+ close(oldfd);
+ return ret;
+}
+
+static int handle_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK | O_WRONLY);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = ftruncate(fd, size);
+ close(fd);
+ return ret;
+}
+
+static int handle_rename(FsContext *ctx, const char *oldpath,
+ const char *newpath)
+{
+ errno = EOPNOTSUPP;
+ return -1;
+}
+
+static int handle_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)fs_ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_PATH);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fchownat(fd, "", credp->fc_uid, credp->fc_gid, AT_EMPTY_PATH);
+ close(fd);
+ return ret;
+}
+
+static int handle_utimensat(FsContext *ctx, V9fsPath *fs_path,
+ const struct timespec *buf)
+{
+ int ret;
+#ifdef CONFIG_UTIMENSAT
+ int fd;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = futimens(fd, buf);
+ close(fd);
+#else
+ ret = -1;
+ errno = ENOSYS;
+#endif
+ return ret;
+}
+
+static int handle_remove(FsContext *ctx, const char *path)
+{
+ errno = EOPNOTSUPP;
+ return -1;
+}
+
+static int handle_fsync(FsContext *ctx, int fid_type,
+ V9fsFidOpenState *fs, int datasync)
+{
+ int fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+
+ if (datasync) {
+ return qemu_fdatasync(fd);
+ } else {
+ return fsync(fd);
+ }
+}
+
+static int handle_statfs(FsContext *ctx, V9fsPath *fs_path,
+ struct statfs *stbuf)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fstatfs(fd, stbuf);
+ close(fd);
+ return ret;
+}
+
+static ssize_t handle_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name, void *value, size_t size)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fgetxattr(fd, name, value, size);
+ close(fd);
+ return ret;
+}
+
+static ssize_t handle_llistxattr(FsContext *ctx, V9fsPath *fs_path,
+ void *value, size_t size)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = flistxattr(fd, value, size);
+ close(fd);
+ return ret;
+}
+
+static int handle_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
+ void *value, size_t size, int flags)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fsetxattr(fd, name, value, size, flags);
+ close(fd);
+ return ret;
+}
+
+static int handle_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name)
+{
+ int fd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ fd = open_by_handle(data->mountfd, fs_path->data, O_NONBLOCK);
+ if (fd < 0) {
+ return fd;
+ }
+ ret = fremovexattr(fd, name);
+ close(fd);
+ return ret;
+}
+
+static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
+ const char *name, V9fsPath *target)
+{
+ char *buffer;
+ struct file_handle *fh;
+ int dirfd, ret, mnt_id;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ /* "." and ".." are not allowed */
+ if (!strcmp(name, ".") || !strcmp(name, "..")) {
+ errno = EINVAL;
+ return -1;
+
+ }
+ if (dir_path) {
+ dirfd = open_by_handle(data->mountfd, dir_path->data, O_PATH);
+ } else {
+ /* relative to export root */
+ buffer = rpath(ctx, ".");
+ dirfd = open(buffer, O_DIRECTORY);
+ g_free(buffer);
+ }
+ if (dirfd < 0) {
+ return dirfd;
+ }
+ fh = g_malloc(sizeof(struct file_handle) + data->handle_bytes);
+ fh->handle_bytes = data->handle_bytes;
+ /* add a "./" at the beginning of the path */
+ buffer = g_strdup_printf("./%s", name);
+ /* flag = 0 imply don't follow symlink */
+ ret = name_to_handle(dirfd, buffer, fh, &mnt_id, 0);
+ if (!ret) {
+ target->data = (char *)fh;
+ target->size = sizeof(struct file_handle) + data->handle_bytes;
+ } else {
+ g_free(fh);
+ }
+ close(dirfd);
+ g_free(buffer);
+ return ret;
+}
+
+static int handle_renameat(FsContext *ctx, V9fsPath *olddir,
+ const char *old_name, V9fsPath *newdir,
+ const char *new_name)
+{
+ int olddirfd, newdirfd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+
+ olddirfd = open_by_handle(data->mountfd, olddir->data, O_PATH);
+ if (olddirfd < 0) {
+ return olddirfd;
+ }
+ newdirfd = open_by_handle(data->mountfd, newdir->data, O_PATH);
+ if (newdirfd < 0) {
+ close(olddirfd);
+ return newdirfd;
+ }
+ ret = renameat(olddirfd, old_name, newdirfd, new_name);
+ close(newdirfd);
+ close(olddirfd);
+ return ret;
+}
+
+static int handle_unlinkat(FsContext *ctx, V9fsPath *dir,
+ const char *name, int flags)
+{
+ int dirfd, ret;
+ struct handle_data *data = (struct handle_data *)ctx->private;
+ int rflags;
+
+ dirfd = open_by_handle(data->mountfd, dir->data, O_PATH);
+ if (dirfd < 0) {
+ return dirfd;
+ }
+
+ rflags = 0;
+ if (flags & P9_DOTL_AT_REMOVEDIR) {
+ rflags |= AT_REMOVEDIR;
+ }
+
+ ret = unlinkat(dirfd, name, rflags);
+
+ close(dirfd);
+ return ret;
+}
+
+static int handle_ioc_getversion(FsContext *ctx, V9fsPath *path,
+ mode_t st_mode, uint64_t *st_gen)
+{
+#ifdef FS_IOC_GETVERSION
+ int err;
+ V9fsFidOpenState fid_open;
+
+ /*
+ * Do not try to open special files like device nodes, fifos etc
+ * We can get fd for regular files and directories only
+ */
+ if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
+ errno = ENOTTY;
+ return -1;
+ }
+ err = handle_open(ctx, path, O_RDONLY, &fid_open);
+ if (err < 0) {
+ return err;
+ }
+ err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
+ handle_close(ctx, &fid_open);
+ return err;
+#else
+ errno = ENOTTY;
+ return -1;
+#endif
+}
+
+static int handle_init(FsContext *ctx)
+{
+ int ret, mnt_id;
+ struct statfs stbuf;
+ struct file_handle fh;
+ struct handle_data *data = g_malloc(sizeof(struct handle_data));
+
+ data->mountfd = open(ctx->fs_root, O_DIRECTORY);
+ if (data->mountfd < 0) {
+ ret = data->mountfd;
+ goto err_out;
+ }
+ ret = statfs(ctx->fs_root, &stbuf);
+ if (!ret) {
+ switch (stbuf.f_type) {
+ case EXT2_SUPER_MAGIC:
+ case BTRFS_SUPER_MAGIC:
+ case REISERFS_SUPER_MAGIC:
+ case XFS_SUPER_MAGIC:
+ ctx->exops.get_st_gen = handle_ioc_getversion;
+ break;
+ }
+ }
+ memset(&fh, 0, sizeof(struct file_handle));
+ ret = name_to_handle(data->mountfd, ".", &fh, &mnt_id, 0);
+ if (ret && errno == EOVERFLOW) {
+ data->handle_bytes = fh.handle_bytes;
+ ctx->private = data;
+ ret = 0;
+ goto out;
+ }
+ /* we got 0 byte handle ? */
+ ret = -1;
+ close(data->mountfd);
+err_out:
+ g_free(data);
+out:
+ return ret;
+}
+
+static int handle_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
+{
+ const char *sec_model = qemu_opt_get(opts, "security_model");
+ const char *path = qemu_opt_get(opts, "path");
+
+ if (sec_model) {
+ fprintf(stderr, "Invalid argument security_model specified with handle fsdriver\n");
+ return -1;
+ }
+
+ if (!path) {
+ fprintf(stderr, "fsdev: No path specified.\n");
+ return -1;
+ }
+ fse->path = g_strdup(path);
+ return 0;
+
+}
+
+FileOperations handle_ops = {
+ .parse_opts = handle_parse_opts,
+ .init = handle_init,
+ .lstat = handle_lstat,
+ .readlink = handle_readlink,
+ .close = handle_close,
+ .closedir = handle_closedir,
+ .open = handle_open,
+ .opendir = handle_opendir,
+ .rewinddir = handle_rewinddir,
+ .telldir = handle_telldir,
+ .readdir_r = handle_readdir_r,
+ .seekdir = handle_seekdir,
+ .preadv = handle_preadv,
+ .pwritev = handle_pwritev,
+ .chmod = handle_chmod,
+ .mknod = handle_mknod,
+ .mkdir = handle_mkdir,
+ .fstat = handle_fstat,
+ .open2 = handle_open2,
+ .symlink = handle_symlink,
+ .link = handle_link,
+ .truncate = handle_truncate,
+ .rename = handle_rename,
+ .chown = handle_chown,
+ .utimensat = handle_utimensat,
+ .remove = handle_remove,
+ .fsync = handle_fsync,
+ .statfs = handle_statfs,
+ .lgetxattr = handle_lgetxattr,
+ .llistxattr = handle_llistxattr,
+ .lsetxattr = handle_lsetxattr,
+ .lremovexattr = handle_lremovexattr,
+ .name_to_path = handle_name_to_path,
+ .renameat = handle_renameat,
+ .unlinkat = handle_unlinkat,
+};
diff --git a/src/hw/9pfs/virtio-9p-local.c b/src/hw/9pfs/virtio-9p-local.c
new file mode 100644
index 0000000..f1f2e25
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-local.c
@@ -0,0 +1,1280 @@
+/*
+ * Virtio 9p Posix callback
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.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 "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "virtio-9p-xattr.h"
+#include "fsdev/qemu-fsdev.h" /* local_ops */
+#include <arpa/inet.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include "qemu/xattr.h"
+#include <libgen.h>
+#include <linux/fs.h>
+#ifdef CONFIG_LINUX_MAGIC_H
+#include <linux/magic.h>
+#endif
+#include <sys/ioctl.h>
+
+#ifndef XFS_SUPER_MAGIC
+#define XFS_SUPER_MAGIC 0x58465342
+#endif
+#ifndef EXT2_SUPER_MAGIC
+#define EXT2_SUPER_MAGIC 0xEF53
+#endif
+#ifndef REISERFS_SUPER_MAGIC
+#define REISERFS_SUPER_MAGIC 0x52654973
+#endif
+#ifndef BTRFS_SUPER_MAGIC
+#define BTRFS_SUPER_MAGIC 0x9123683E
+#endif
+
+#define VIRTFS_META_DIR ".virtfs_metadata"
+
+static char *local_mapped_attr_path(FsContext *ctx, const char *path)
+{
+ int dirlen;
+ const char *name = strrchr(path, '/');
+ if (name) {
+ dirlen = name - path;
+ ++name;
+ } else {
+ name = path;
+ dirlen = 0;
+ }
+ return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
+ dirlen, path, VIRTFS_META_DIR, name);
+}
+
+static FILE *local_fopen(const char *path, const char *mode)
+{
+ int fd, o_mode = 0;
+ FILE *fp;
+ int flags = O_NOFOLLOW;
+ /*
+ * only supports two modes
+ */
+ if (mode[0] == 'r') {
+ flags |= O_RDONLY;
+ } else if (mode[0] == 'w') {
+ flags |= O_WRONLY | O_TRUNC | O_CREAT;
+ o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+ } else {
+ return NULL;
+ }
+ fd = open(path, flags, o_mode);
+ if (fd == -1) {
+ return NULL;
+ }
+ fp = fdopen(fd, mode);
+ if (!fp) {
+ close(fd);
+ }
+ return fp;
+}
+
+#define ATTR_MAX 100
+static void local_mapped_file_attr(FsContext *ctx, const char *path,
+ struct stat *stbuf)
+{
+ FILE *fp;
+ char buf[ATTR_MAX];
+ char *attr_path;
+
+ attr_path = local_mapped_attr_path(ctx, path);
+ fp = local_fopen(attr_path, "r");
+ g_free(attr_path);
+ if (!fp) {
+ return;
+ }
+ memset(buf, 0, ATTR_MAX);
+ while (fgets(buf, ATTR_MAX, fp)) {
+ if (!strncmp(buf, "virtfs.uid", 10)) {
+ stbuf->st_uid = atoi(buf+11);
+ } else if (!strncmp(buf, "virtfs.gid", 10)) {
+ stbuf->st_gid = atoi(buf+11);
+ } else if (!strncmp(buf, "virtfs.mode", 11)) {
+ stbuf->st_mode = atoi(buf+12);
+ } else if (!strncmp(buf, "virtfs.rdev", 11)) {
+ stbuf->st_rdev = atoi(buf+12);
+ }
+ memset(buf, 0, ATTR_MAX);
+ }
+ fclose(fp);
+}
+
+static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
+{
+ int err;
+ char *buffer;
+ char *path = fs_path->data;
+
+ buffer = rpath(fs_ctx, path);
+ err = lstat(buffer, stbuf);
+ if (err) {
+ goto err_out;
+ }
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ /* Actual credentials are part of extended attrs */
+ uid_t tmp_uid;
+ gid_t tmp_gid;
+ mode_t tmp_mode;
+ dev_t tmp_dev;
+ if (getxattr(buffer, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
+ stbuf->st_uid = le32_to_cpu(tmp_uid);
+ }
+ if (getxattr(buffer, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
+ stbuf->st_gid = le32_to_cpu(tmp_gid);
+ }
+ if (getxattr(buffer, "user.virtfs.mode",
+ &tmp_mode, sizeof(mode_t)) > 0) {
+ stbuf->st_mode = le32_to_cpu(tmp_mode);
+ }
+ if (getxattr(buffer, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
+ stbuf->st_rdev = le64_to_cpu(tmp_dev);
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ local_mapped_file_attr(fs_ctx, path, stbuf);
+ }
+
+err_out:
+ g_free(buffer);
+ return err;
+}
+
+static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
+{
+ int err;
+ char *attr_dir;
+ char *tmp_path = g_strdup(path);
+
+ attr_dir = g_strdup_printf("%s/%s/%s",
+ ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
+
+ err = mkdir(attr_dir, 0700);
+ if (err < 0 && errno == EEXIST) {
+ err = 0;
+ }
+ g_free(attr_dir);
+ g_free(tmp_path);
+ return err;
+}
+
+static int local_set_mapped_file_attr(FsContext *ctx,
+ const char *path, FsCred *credp)
+{
+ FILE *fp;
+ int ret = 0;
+ char buf[ATTR_MAX];
+ char *attr_path;
+ int uid = -1, gid = -1, mode = -1, rdev = -1;
+
+ attr_path = local_mapped_attr_path(ctx, path);
+ fp = local_fopen(attr_path, "r");
+ if (!fp) {
+ goto create_map_file;
+ }
+ memset(buf, 0, ATTR_MAX);
+ while (fgets(buf, ATTR_MAX, fp)) {
+ if (!strncmp(buf, "virtfs.uid", 10)) {
+ uid = atoi(buf+11);
+ } else if (!strncmp(buf, "virtfs.gid", 10)) {
+ gid = atoi(buf+11);
+ } else if (!strncmp(buf, "virtfs.mode", 11)) {
+ mode = atoi(buf+12);
+ } else if (!strncmp(buf, "virtfs.rdev", 11)) {
+ rdev = atoi(buf+12);
+ }
+ memset(buf, 0, ATTR_MAX);
+ }
+ fclose(fp);
+ goto update_map_file;
+
+create_map_file:
+ ret = local_create_mapped_attr_dir(ctx, path);
+ if (ret < 0) {
+ goto err_out;
+ }
+
+update_map_file:
+ fp = local_fopen(attr_path, "w");
+ if (!fp) {
+ ret = -1;
+ goto err_out;
+ }
+
+ if (credp->fc_uid != -1) {
+ uid = credp->fc_uid;
+ }
+ if (credp->fc_gid != -1) {
+ gid = credp->fc_gid;
+ }
+ if (credp->fc_mode != -1) {
+ mode = credp->fc_mode;
+ }
+ if (credp->fc_rdev != -1) {
+ rdev = credp->fc_rdev;
+ }
+
+
+ if (uid != -1) {
+ fprintf(fp, "virtfs.uid=%d\n", uid);
+ }
+ if (gid != -1) {
+ fprintf(fp, "virtfs.gid=%d\n", gid);
+ }
+ if (mode != -1) {
+ fprintf(fp, "virtfs.mode=%d\n", mode);
+ }
+ if (rdev != -1) {
+ fprintf(fp, "virtfs.rdev=%d\n", rdev);
+ }
+ fclose(fp);
+
+err_out:
+ g_free(attr_path);
+ return ret;
+}
+
+static int local_set_xattr(const char *path, FsCred *credp)
+{
+ int err;
+
+ if (credp->fc_uid != -1) {
+ uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
+ err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0);
+ if (err) {
+ return err;
+ }
+ }
+ if (credp->fc_gid != -1) {
+ uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
+ err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0);
+ if (err) {
+ return err;
+ }
+ }
+ if (credp->fc_mode != -1) {
+ uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
+ err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0);
+ if (err) {
+ return err;
+ }
+ }
+ if (credp->fc_rdev != -1) {
+ uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
+ err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0);
+ if (err) {
+ return err;
+ }
+ }
+ return 0;
+}
+
+static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
+ FsCred *credp)
+{
+ char *buffer;
+
+ buffer = rpath(fs_ctx, path);
+ if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) {
+ /*
+ * If we fail to change ownership and if we are
+ * using security model none. Ignore the error
+ */
+ if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
+ goto err;
+ }
+ }
+
+ if (chmod(buffer, credp->fc_mode & 07777) < 0) {
+ goto err;
+ }
+
+ g_free(buffer);
+ return 0;
+err:
+ g_free(buffer);
+ return -1;
+}
+
+static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
+ char *buf, size_t bufsz)
+{
+ ssize_t tsize = -1;
+ char *buffer;
+ char *path = fs_path->data;
+
+ if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
+ (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
+ int fd;
+ buffer = rpath(fs_ctx, path);
+ fd = open(buffer, O_RDONLY | O_NOFOLLOW);
+ g_free(buffer);
+ if (fd == -1) {
+ return -1;
+ }
+ do {
+ tsize = read(fd, (void *)buf, bufsz);
+ } while (tsize == -1 && errno == EINTR);
+ close(fd);
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ tsize = readlink(buffer, buf, bufsz);
+ g_free(buffer);
+ }
+ return tsize;
+}
+
+static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return close(fs->fd);
+}
+
+static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return closedir(fs->dir);
+}
+
+static int local_open(FsContext *ctx, V9fsPath *fs_path,
+ int flags, V9fsFidOpenState *fs)
+{
+ char *buffer;
+ char *path = fs_path->data;
+
+ buffer = rpath(ctx, path);
+ fs->fd = open(buffer, flags | O_NOFOLLOW);
+ g_free(buffer);
+ return fs->fd;
+}
+
+static int local_opendir(FsContext *ctx,
+ V9fsPath *fs_path, V9fsFidOpenState *fs)
+{
+ char *buffer;
+ char *path = fs_path->data;
+
+ buffer = rpath(ctx, path);
+ fs->dir = opendir(buffer);
+ g_free(buffer);
+ if (!fs->dir) {
+ return -1;
+ }
+ return 0;
+}
+
+static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ rewinddir(fs->dir);
+}
+
+static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return telldir(fs->dir);
+}
+
+static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
+ struct dirent *entry,
+ struct dirent **result)
+{
+ int ret;
+
+again:
+ ret = readdir_r(fs->dir, entry, result);
+ if (ctx->export_flags & V9FS_SM_MAPPED) {
+ entry->d_type = DT_UNKNOWN;
+ } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ if (!ret && *result != NULL &&
+ !strcmp(entry->d_name, VIRTFS_META_DIR)) {
+ /* skp the meta data directory */
+ goto again;
+ }
+ entry->d_type = DT_UNKNOWN;
+ }
+ return ret;
+}
+
+static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
+{
+ seekdir(fs->dir, off);
+}
+
+static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+#ifdef CONFIG_PREADV
+ return preadv(fs->fd, iov, iovcnt, offset);
+#else
+ int err = lseek(fs->fd, offset, SEEK_SET);
+ if (err == -1) {
+ return err;
+ } else {
+ return readv(fs->fd, iov, iovcnt);
+ }
+#endif
+}
+
+static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ ssize_t ret
+;
+#ifdef CONFIG_PREADV
+ ret = pwritev(fs->fd, iov, iovcnt, offset);
+#else
+ int err = lseek(fs->fd, offset, SEEK_SET);
+ if (err == -1) {
+ return err;
+ } else {
+ ret = writev(fs->fd, iov, iovcnt);
+ }
+#endif
+#ifdef CONFIG_SYNC_FILE_RANGE
+ if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
+ /*
+ * Initiate a writeback. This is not a data integrity sync.
+ * We want to ensure that we don't leave dirty pages in the cache
+ * after write when writeout=immediate is sepcified.
+ */
+ sync_file_range(fs->fd, offset, ret,
+ SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
+ }
+#endif
+ return ret;
+}
+
+static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ char *buffer;
+ int ret = -1;
+ char *path = fs_path->data;
+
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ buffer = rpath(fs_ctx, path);
+ ret = local_set_xattr(buffer, credp);
+ g_free(buffer);
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ return local_set_mapped_file_attr(fs_ctx, path, credp);
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ ret = chmod(buffer, credp->fc_mode);
+ g_free(buffer);
+ }
+ return ret;
+}
+
+static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ char *path;
+ int err = -1;
+ int serrno = 0;
+ V9fsString fullname;
+ char *buffer = NULL;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+ path = fullname.data;
+
+ /* Determine the security model */
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ buffer = rpath(fs_ctx, path);
+ err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
+ if (err == -1) {
+ goto out;
+ }
+ err = local_set_xattr(buffer, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+
+ buffer = rpath(fs_ctx, path);
+ err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
+ if (err == -1) {
+ goto out;
+ }
+ err = local_set_mapped_file_attr(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
+ if (err == -1) {
+ goto out;
+ }
+ err = local_post_create_passthrough(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ }
+ goto out;
+
+err_end:
+ remove(buffer);
+ errno = serrno;
+out:
+ g_free(buffer);
+ v9fs_string_free(&fullname);
+ return err;
+}
+
+static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ char *path;
+ int err = -1;
+ int serrno = 0;
+ V9fsString fullname;
+ char *buffer = NULL;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+ path = fullname.data;
+
+ /* Determine the security model */
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ buffer = rpath(fs_ctx, path);
+ err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
+ if (err == -1) {
+ goto out;
+ }
+ credp->fc_mode = credp->fc_mode|S_IFDIR;
+ err = local_set_xattr(buffer, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ buffer = rpath(fs_ctx, path);
+ err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
+ if (err == -1) {
+ goto out;
+ }
+ credp->fc_mode = credp->fc_mode|S_IFDIR;
+ err = local_set_mapped_file_attr(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ err = mkdir(buffer, credp->fc_mode);
+ if (err == -1) {
+ goto out;
+ }
+ err = local_post_create_passthrough(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ }
+ goto out;
+
+err_end:
+ remove(buffer);
+ errno = serrno;
+out:
+ g_free(buffer);
+ v9fs_string_free(&fullname);
+ return err;
+}
+
+static int local_fstat(FsContext *fs_ctx, int fid_type,
+ V9fsFidOpenState *fs, struct stat *stbuf)
+{
+ int err, fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+
+ err = fstat(fd, stbuf);
+ if (err) {
+ return err;
+ }
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ /* Actual credentials are part of extended attrs */
+ uid_t tmp_uid;
+ gid_t tmp_gid;
+ mode_t tmp_mode;
+ dev_t tmp_dev;
+
+ if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
+ stbuf->st_uid = le32_to_cpu(tmp_uid);
+ }
+ if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
+ stbuf->st_gid = le32_to_cpu(tmp_gid);
+ }
+ if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
+ stbuf->st_mode = le32_to_cpu(tmp_mode);
+ }
+ if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
+ stbuf->st_rdev = le64_to_cpu(tmp_dev);
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ errno = EOPNOTSUPP;
+ return -1;
+ }
+ return err;
+}
+
+static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
+ int flags, FsCred *credp, V9fsFidOpenState *fs)
+{
+ char *path;
+ int fd = -1;
+ int err = -1;
+ int serrno = 0;
+ V9fsString fullname;
+ char *buffer = NULL;
+
+ /*
+ * Mark all the open to not follow symlinks
+ */
+ flags |= O_NOFOLLOW;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+ path = fullname.data;
+
+ /* Determine the security model */
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ buffer = rpath(fs_ctx, path);
+ fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
+ if (fd == -1) {
+ err = fd;
+ goto out;
+ }
+ credp->fc_mode = credp->fc_mode|S_IFREG;
+ /* Set cleint credentials in xattr */
+ err = local_set_xattr(buffer, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ buffer = rpath(fs_ctx, path);
+ fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
+ if (fd == -1) {
+ err = fd;
+ goto out;
+ }
+ credp->fc_mode = credp->fc_mode|S_IFREG;
+ /* Set client credentials in .virtfs_metadata directory files */
+ err = local_set_mapped_file_attr(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ fd = open(buffer, flags, credp->fc_mode);
+ if (fd == -1) {
+ err = fd;
+ goto out;
+ }
+ err = local_post_create_passthrough(fs_ctx, path, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ }
+ err = fd;
+ fs->fd = fd;
+ goto out;
+
+err_end:
+ close(fd);
+ remove(buffer);
+ errno = serrno;
+out:
+ g_free(buffer);
+ v9fs_string_free(&fullname);
+ return err;
+}
+
+
+static int local_symlink(FsContext *fs_ctx, const char *oldpath,
+ V9fsPath *dir_path, const char *name, FsCred *credp)
+{
+ int err = -1;
+ int serrno = 0;
+ char *newpath;
+ V9fsString fullname;
+ char *buffer = NULL;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+ newpath = fullname.data;
+
+ /* Determine the security model */
+ if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ int fd;
+ ssize_t oldpath_size, write_size;
+ buffer = rpath(fs_ctx, newpath);
+ fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
+ if (fd == -1) {
+ err = fd;
+ goto out;
+ }
+ /* Write the oldpath (target) to the file. */
+ oldpath_size = strlen(oldpath);
+ do {
+ write_size = write(fd, (void *)oldpath, oldpath_size);
+ } while (write_size == -1 && errno == EINTR);
+
+ if (write_size != oldpath_size) {
+ serrno = errno;
+ close(fd);
+ err = -1;
+ goto err_end;
+ }
+ close(fd);
+ /* Set cleint credentials in symlink's xattr */
+ credp->fc_mode = credp->fc_mode|S_IFLNK;
+ err = local_set_xattr(buffer, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ int fd;
+ ssize_t oldpath_size, write_size;
+ buffer = rpath(fs_ctx, newpath);
+ fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
+ if (fd == -1) {
+ err = fd;
+ goto out;
+ }
+ /* Write the oldpath (target) to the file. */
+ oldpath_size = strlen(oldpath);
+ do {
+ write_size = write(fd, (void *)oldpath, oldpath_size);
+ } while (write_size == -1 && errno == EINTR);
+
+ if (write_size != oldpath_size) {
+ serrno = errno;
+ close(fd);
+ err = -1;
+ goto err_end;
+ }
+ close(fd);
+ /* Set cleint credentials in symlink's xattr */
+ credp->fc_mode = credp->fc_mode|S_IFLNK;
+ err = local_set_mapped_file_attr(fs_ctx, newpath, credp);
+ if (err == -1) {
+ serrno = errno;
+ goto err_end;
+ }
+ } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, newpath);
+ err = symlink(oldpath, buffer);
+ if (err) {
+ goto out;
+ }
+ err = lchown(buffer, credp->fc_uid, credp->fc_gid);
+ if (err == -1) {
+ /*
+ * If we fail to change ownership and if we are
+ * using security model none. Ignore the error
+ */
+ if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
+ serrno = errno;
+ goto err_end;
+ } else
+ err = 0;
+ }
+ }
+ goto out;
+
+err_end:
+ remove(buffer);
+ errno = serrno;
+out:
+ g_free(buffer);
+ v9fs_string_free(&fullname);
+ return err;
+}
+
+static int local_link(FsContext *ctx, V9fsPath *oldpath,
+ V9fsPath *dirpath, const char *name)
+{
+ int ret;
+ V9fsString newpath;
+ char *buffer, *buffer1;
+
+ v9fs_string_init(&newpath);
+ v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
+
+ buffer = rpath(ctx, oldpath->data);
+ buffer1 = rpath(ctx, newpath.data);
+ ret = link(buffer, buffer1);
+ g_free(buffer);
+ g_free(buffer1);
+
+ /* now link the virtfs_metadata files */
+ if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
+ /* Link the .virtfs_metadata files. Create the metada directory */
+ ret = local_create_mapped_attr_dir(ctx, newpath.data);
+ if (ret < 0) {
+ goto err_out;
+ }
+ buffer = local_mapped_attr_path(ctx, oldpath->data);
+ buffer1 = local_mapped_attr_path(ctx, newpath.data);
+ ret = link(buffer, buffer1);
+ g_free(buffer);
+ g_free(buffer1);
+ if (ret < 0 && errno != ENOENT) {
+ goto err_out;
+ }
+ }
+err_out:
+ v9fs_string_free(&newpath);
+ return ret;
+}
+
+static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
+{
+ char *buffer;
+ int ret;
+ char *path = fs_path->data;
+
+ buffer = rpath(ctx, path);
+ ret = truncate(buffer, size);
+ g_free(buffer);
+ return ret;
+}
+
+static int local_rename(FsContext *ctx, const char *oldpath,
+ const char *newpath)
+{
+ int err;
+ char *buffer, *buffer1;
+
+ if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ err = local_create_mapped_attr_dir(ctx, newpath);
+ if (err < 0) {
+ return err;
+ }
+ /* rename the .virtfs_metadata files */
+ buffer = local_mapped_attr_path(ctx, oldpath);
+ buffer1 = local_mapped_attr_path(ctx, newpath);
+ err = rename(buffer, buffer1);
+ g_free(buffer);
+ g_free(buffer1);
+ if (err < 0 && errno != ENOENT) {
+ return err;
+ }
+ }
+
+ buffer = rpath(ctx, oldpath);
+ buffer1 = rpath(ctx, newpath);
+ err = rename(buffer, buffer1);
+ g_free(buffer);
+ g_free(buffer1);
+ return err;
+}
+
+static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ char *buffer;
+ int ret = -1;
+ char *path = fs_path->data;
+
+ if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
+ (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
+ (fs_ctx->export_flags & V9FS_SM_NONE)) {
+ buffer = rpath(fs_ctx, path);
+ ret = lchown(buffer, credp->fc_uid, credp->fc_gid);
+ g_free(buffer);
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
+ buffer = rpath(fs_ctx, path);
+ ret = local_set_xattr(buffer, credp);
+ g_free(buffer);
+ } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ return local_set_mapped_file_attr(fs_ctx, path, credp);
+ }
+ return ret;
+}
+
+static int local_utimensat(FsContext *s, V9fsPath *fs_path,
+ const struct timespec *buf)
+{
+ char *buffer;
+ int ret;
+ char *path = fs_path->data;
+
+ buffer = rpath(s, path);
+ ret = qemu_utimens(buffer, buf);
+ g_free(buffer);
+ return ret;
+}
+
+static int local_remove(FsContext *ctx, const char *path)
+{
+ int err;
+ struct stat stbuf;
+ char *buffer;
+
+ if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ buffer = rpath(ctx, path);
+ err = lstat(buffer, &stbuf);
+ g_free(buffer);
+ if (err) {
+ goto err_out;
+ }
+ /*
+ * If directory remove .virtfs_metadata contained in the
+ * directory
+ */
+ if (S_ISDIR(stbuf.st_mode)) {
+ buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
+ path, VIRTFS_META_DIR);
+ err = remove(buffer);
+ g_free(buffer);
+ if (err < 0 && errno != ENOENT) {
+ /*
+ * We didn't had the .virtfs_metadata file. May be file created
+ * in non-mapped mode ?. Ignore ENOENT.
+ */
+ goto err_out;
+ }
+ }
+ /*
+ * Now remove the name from parent directory
+ * .virtfs_metadata directory
+ */
+ buffer = local_mapped_attr_path(ctx, path);
+ err = remove(buffer);
+ g_free(buffer);
+ if (err < 0 && errno != ENOENT) {
+ /*
+ * We didn't had the .virtfs_metadata file. May be file created
+ * in non-mapped mode ?. Ignore ENOENT.
+ */
+ goto err_out;
+ }
+ }
+
+ buffer = rpath(ctx, path);
+ err = remove(buffer);
+ g_free(buffer);
+err_out:
+ return err;
+}
+
+static int local_fsync(FsContext *ctx, int fid_type,
+ V9fsFidOpenState *fs, int datasync)
+{
+ int fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+
+ if (datasync) {
+ return qemu_fdatasync(fd);
+ } else {
+ return fsync(fd);
+ }
+}
+
+static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
+{
+ char *buffer;
+ int ret;
+ char *path = fs_path->data;
+
+ buffer = rpath(s, path);
+ ret = statfs(buffer, stbuf);
+ g_free(buffer);
+ return ret;
+}
+
+static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name, void *value, size_t size)
+{
+ char *path = fs_path->data;
+
+ return v9fs_get_xattr(ctx, path, name, value, size);
+}
+
+static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
+ void *value, size_t size)
+{
+ char *path = fs_path->data;
+
+ return v9fs_list_xattr(ctx, path, value, size);
+}
+
+static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
+ void *value, size_t size, int flags)
+{
+ char *path = fs_path->data;
+
+ return v9fs_set_xattr(ctx, path, name, value, size, flags);
+}
+
+static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name)
+{
+ char *path = fs_path->data;
+
+ return v9fs_remove_xattr(ctx, path, name);
+}
+
+static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
+ const char *name, V9fsPath *target)
+{
+ if (dir_path) {
+ v9fs_string_sprintf((V9fsString *)target, "%s/%s",
+ dir_path->data, name);
+ } else {
+ v9fs_string_sprintf((V9fsString *)target, "%s", name);
+ }
+ /* Bump the size for including terminating NULL */
+ target->size++;
+ return 0;
+}
+
+static int local_renameat(FsContext *ctx, V9fsPath *olddir,
+ const char *old_name, V9fsPath *newdir,
+ const char *new_name)
+{
+ int ret;
+ V9fsString old_full_name, new_full_name;
+
+ v9fs_string_init(&old_full_name);
+ v9fs_string_init(&new_full_name);
+
+ v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
+ v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
+
+ ret = local_rename(ctx, old_full_name.data, new_full_name.data);
+ v9fs_string_free(&old_full_name);
+ v9fs_string_free(&new_full_name);
+ return ret;
+}
+
+static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
+ const char *name, int flags)
+{
+ int ret;
+ V9fsString fullname;
+ char *buffer;
+
+ v9fs_string_init(&fullname);
+
+ v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
+ if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ if (flags == AT_REMOVEDIR) {
+ /*
+ * If directory remove .virtfs_metadata contained in the
+ * directory
+ */
+ buffer = g_strdup_printf("%s/%s/%s", ctx->fs_root,
+ fullname.data, VIRTFS_META_DIR);
+ ret = remove(buffer);
+ g_free(buffer);
+ if (ret < 0 && errno != ENOENT) {
+ /*
+ * We didn't had the .virtfs_metadata file. May be file created
+ * in non-mapped mode ?. Ignore ENOENT.
+ */
+ goto err_out;
+ }
+ }
+ /*
+ * Now remove the name from parent directory
+ * .virtfs_metadata directory.
+ */
+ buffer = local_mapped_attr_path(ctx, fullname.data);
+ ret = remove(buffer);
+ g_free(buffer);
+ if (ret < 0 && errno != ENOENT) {
+ /*
+ * We didn't had the .virtfs_metadata file. May be file created
+ * in non-mapped mode ?. Ignore ENOENT.
+ */
+ goto err_out;
+ }
+ }
+ /* Remove the name finally */
+ buffer = rpath(ctx, fullname.data);
+ ret = remove(buffer);
+ g_free(buffer);
+
+err_out:
+ v9fs_string_free(&fullname);
+ return ret;
+}
+
+static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
+ mode_t st_mode, uint64_t *st_gen)
+{
+#ifdef FS_IOC_GETVERSION
+ int err;
+ V9fsFidOpenState fid_open;
+
+ /*
+ * Do not try to open special files like device nodes, fifos etc
+ * We can get fd for regular files and directories only
+ */
+ if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
+ errno = ENOTTY;
+ return -1;
+ }
+ err = local_open(ctx, path, O_RDONLY, &fid_open);
+ if (err < 0) {
+ return err;
+ }
+ err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
+ local_close(ctx, &fid_open);
+ return err;
+#else
+ errno = ENOTTY;
+ return -1;
+#endif
+}
+
+static int local_init(FsContext *ctx)
+{
+ int err = 0;
+ struct statfs stbuf;
+
+ if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
+ ctx->xops = passthrough_xattr_ops;
+ } else if (ctx->export_flags & V9FS_SM_MAPPED) {
+ ctx->xops = mapped_xattr_ops;
+ } else if (ctx->export_flags & V9FS_SM_NONE) {
+ ctx->xops = none_xattr_ops;
+ } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
+ /*
+ * xattr operation for mapped-file and passthrough
+ * remain same.
+ */
+ ctx->xops = passthrough_xattr_ops;
+ }
+ ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
+#ifdef FS_IOC_GETVERSION
+ /*
+ * use ioc_getversion only if the iocl is definied
+ */
+ err = statfs(ctx->fs_root, &stbuf);
+ if (!err) {
+ switch (stbuf.f_type) {
+ case EXT2_SUPER_MAGIC:
+ case BTRFS_SUPER_MAGIC:
+ case REISERFS_SUPER_MAGIC:
+ case XFS_SUPER_MAGIC:
+ ctx->exops.get_st_gen = local_ioc_getversion;
+ break;
+ }
+ }
+#endif
+ return err;
+}
+
+static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse)
+{
+ const char *sec_model = qemu_opt_get(opts, "security_model");
+ const char *path = qemu_opt_get(opts, "path");
+
+ if (!sec_model) {
+ fprintf(stderr, "security model not specified, "
+ "local fs needs security model\nvalid options are:"
+ "\tsecurity_model=[passthrough|mapped|none]\n");
+ return -1;
+ }
+
+ if (!strcmp(sec_model, "passthrough")) {
+ fse->export_flags |= V9FS_SM_PASSTHROUGH;
+ } else if (!strcmp(sec_model, "mapped") ||
+ !strcmp(sec_model, "mapped-xattr")) {
+ fse->export_flags |= V9FS_SM_MAPPED;
+ } else if (!strcmp(sec_model, "none")) {
+ fse->export_flags |= V9FS_SM_NONE;
+ } else if (!strcmp(sec_model, "mapped-file")) {
+ fse->export_flags |= V9FS_SM_MAPPED_FILE;
+ } else {
+ fprintf(stderr, "Invalid security model %s specified, valid options are"
+ "\n\t [passthrough|mapped-xattr|mapped-file|none]\n",
+ sec_model);
+ return -1;
+ }
+
+ if (!path) {
+ fprintf(stderr, "fsdev: No path specified.\n");
+ return -1;
+ }
+ fse->path = g_strdup(path);
+
+ return 0;
+}
+
+FileOperations local_ops = {
+ .parse_opts = local_parse_opts,
+ .init = local_init,
+ .lstat = local_lstat,
+ .readlink = local_readlink,
+ .close = local_close,
+ .closedir = local_closedir,
+ .open = local_open,
+ .opendir = local_opendir,
+ .rewinddir = local_rewinddir,
+ .telldir = local_telldir,
+ .readdir_r = local_readdir_r,
+ .seekdir = local_seekdir,
+ .preadv = local_preadv,
+ .pwritev = local_pwritev,
+ .chmod = local_chmod,
+ .mknod = local_mknod,
+ .mkdir = local_mkdir,
+ .fstat = local_fstat,
+ .open2 = local_open2,
+ .symlink = local_symlink,
+ .link = local_link,
+ .truncate = local_truncate,
+ .rename = local_rename,
+ .chown = local_chown,
+ .utimensat = local_utimensat,
+ .remove = local_remove,
+ .fsync = local_fsync,
+ .statfs = local_statfs,
+ .lgetxattr = local_lgetxattr,
+ .llistxattr = local_llistxattr,
+ .lsetxattr = local_lsetxattr,
+ .lremovexattr = local_lremovexattr,
+ .name_to_path = local_name_to_path,
+ .renameat = local_renameat,
+ .unlinkat = local_unlinkat,
+};
diff --git a/src/hw/9pfs/virtio-9p-posix-acl.c b/src/hw/9pfs/virtio-9p-posix-acl.c
new file mode 100644
index 0000000..09dad07
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-posix-acl.c
@@ -0,0 +1,185 @@
+/*
+ * Virtio 9p system.posix* xattr callback
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * 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 <sys/types.h>
+#include "qemu/xattr.h"
+#include "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "fsdev/file-op-9p.h"
+#include "virtio-9p-xattr.h"
+
+#define MAP_ACL_ACCESS "user.virtfs.system.posix_acl_access"
+#define MAP_ACL_DEFAULT "user.virtfs.system.posix_acl_default"
+#define ACL_ACCESS "system.posix_acl_access"
+#define ACL_DEFAULT "system.posix_acl_default"
+
+static ssize_t mp_pacl_getxattr(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size)
+{
+ char *buffer;
+ ssize_t ret;
+
+ buffer = rpath(ctx, path);
+ ret = lgetxattr(buffer, MAP_ACL_ACCESS, value, size);
+ g_free(buffer);
+ return ret;
+}
+
+static ssize_t mp_pacl_listxattr(FsContext *ctx, const char *path,
+ char *name, void *value, size_t osize)
+{
+ ssize_t len = sizeof(ACL_ACCESS);
+
+ if (!value) {
+ return len;
+ }
+
+ if (osize < len) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ /* len includes the trailing NUL */
+ memcpy(value, ACL_ACCESS, len);
+ return 0;
+}
+
+static int mp_pacl_setxattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags)
+{
+ char *buffer;
+ int ret;
+
+ buffer = rpath(ctx, path);
+ ret = lsetxattr(buffer, MAP_ACL_ACCESS, value, size, flags);
+ g_free(buffer);
+ return ret;
+}
+
+static int mp_pacl_removexattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ int ret;
+ char *buffer;
+
+ buffer = rpath(ctx, path);
+ ret = lremovexattr(buffer, MAP_ACL_ACCESS);
+ if (ret == -1 && errno == ENODATA) {
+ /*
+ * We don't get ENODATA error when trying to remove a
+ * posix acl that is not present. So don't throw the error
+ * even in case of mapped security model
+ */
+ errno = 0;
+ ret = 0;
+ }
+ g_free(buffer);
+ return ret;
+}
+
+static ssize_t mp_dacl_getxattr(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size)
+{
+ char *buffer;
+ ssize_t ret;
+
+ buffer = rpath(ctx, path);
+ ret = lgetxattr(buffer, MAP_ACL_DEFAULT, value, size);
+ g_free(buffer);
+ return ret;
+}
+
+static ssize_t mp_dacl_listxattr(FsContext *ctx, const char *path,
+ char *name, void *value, size_t osize)
+{
+ ssize_t len = sizeof(ACL_DEFAULT);
+
+ if (!value) {
+ return len;
+ }
+
+ if (osize < len) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ /* len includes the trailing NUL */
+ memcpy(value, ACL_DEFAULT, len);
+ return 0;
+}
+
+static int mp_dacl_setxattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags)
+{
+ char *buffer;
+ int ret;
+
+ buffer = rpath(ctx, path);
+ ret = lsetxattr(buffer, MAP_ACL_DEFAULT, value, size, flags);
+ g_free(buffer);
+ return ret;
+}
+
+static int mp_dacl_removexattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ int ret;
+ char *buffer;
+
+ buffer = rpath(ctx, path);
+ ret = lremovexattr(buffer, MAP_ACL_DEFAULT);
+ if (ret == -1 && errno == ENODATA) {
+ /*
+ * We don't get ENODATA error when trying to remove a
+ * posix acl that is not present. So don't throw the error
+ * even in case of mapped security model
+ */
+ errno = 0;
+ ret = 0;
+ }
+ g_free(buffer);
+ return ret;
+}
+
+
+XattrOperations mapped_pacl_xattr = {
+ .name = "system.posix_acl_access",
+ .getxattr = mp_pacl_getxattr,
+ .setxattr = mp_pacl_setxattr,
+ .listxattr = mp_pacl_listxattr,
+ .removexattr = mp_pacl_removexattr,
+};
+
+XattrOperations mapped_dacl_xattr = {
+ .name = "system.posix_acl_default",
+ .getxattr = mp_dacl_getxattr,
+ .setxattr = mp_dacl_setxattr,
+ .listxattr = mp_dacl_listxattr,
+ .removexattr = mp_dacl_removexattr,
+};
+
+XattrOperations passthrough_acl_xattr = {
+ .name = "system.posix_acl_",
+ .getxattr = pt_getxattr,
+ .setxattr = pt_setxattr,
+ .listxattr = pt_listxattr,
+ .removexattr = pt_removexattr,
+};
+
+XattrOperations none_acl_xattr = {
+ .name = "system.posix_acl_",
+ .getxattr = notsup_getxattr,
+ .setxattr = notsup_setxattr,
+ .listxattr = notsup_listxattr,
+ .removexattr = notsup_removexattr,
+};
diff --git a/src/hw/9pfs/virtio-9p-proxy.c b/src/hw/9pfs/virtio-9p-proxy.c
new file mode 100644
index 0000000..1bc7881
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-proxy.c
@@ -0,0 +1,1219 @@
+/*
+ * Virtio 9p Proxy callback
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * M. Mohan Kumar <mohan@in.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 <sys/socket.h>
+#include <sys/un.h>
+#include "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "qemu/error-report.h"
+#include "fsdev/qemu-fsdev.h"
+#include "virtio-9p-proxy.h"
+
+typedef struct V9fsProxy {
+ int sockfd;
+ QemuMutex mutex;
+ struct iovec in_iovec;
+ struct iovec out_iovec;
+} V9fsProxy;
+
+/*
+ * Return received file descriptor on success in *status.
+ * errno is also returned on *status (which will be < 0)
+ * return < 0 on transport error.
+ */
+static int v9fs_receivefd(int sockfd, int *status)
+{
+ struct iovec iov;
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ int retval, data, fd;
+ union MsgControl msg_control;
+
+ iov.iov_base = &data;
+ iov.iov_len = sizeof(data);
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = &msg_control;
+ msg.msg_controllen = sizeof(msg_control);
+
+ do {
+ retval = recvmsg(sockfd, &msg, 0);
+ } while (retval < 0 && errno == EINTR);
+ if (retval <= 0) {
+ return retval;
+ }
+ /*
+ * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
+ * request doesn't need ancillary data (fd) or an error occurred,
+ * data is set to negative errno value.
+ */
+ if (data != V9FS_FD_VALID) {
+ *status = data;
+ return 0;
+ }
+ /*
+ * File descriptor (fd) is sent in the ancillary data. Check if we
+ * indeed received it. One of the reasons to fail to receive it is if
+ * we exceeded the maximum number of file descriptors!
+ */
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
+ cmsg->cmsg_level != SOL_SOCKET ||
+ cmsg->cmsg_type != SCM_RIGHTS) {
+ continue;
+ }
+ fd = *((int *)CMSG_DATA(cmsg));
+ *status = fd;
+ return 0;
+ }
+ *status = -ENFILE; /* Ancillary data sent but not received */
+ return 0;
+}
+
+static ssize_t socket_read(int sockfd, void *buff, size_t size)
+{
+ ssize_t retval, total = 0;
+
+ while (size) {
+ retval = read(sockfd, buff, size);
+ if (retval == 0) {
+ return -EIO;
+ }
+ if (retval < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ return -errno;
+ }
+ size -= retval;
+ buff += retval;
+ total += retval;
+ }
+ return total;
+}
+
+/* Converts proxy_statfs to VFS statfs structure */
+static void prstatfs_to_statfs(struct statfs *stfs, ProxyStatFS *prstfs)
+{
+ memset(stfs, 0, sizeof(*stfs));
+ stfs->f_type = prstfs->f_type;
+ stfs->f_bsize = prstfs->f_bsize;
+ stfs->f_blocks = prstfs->f_blocks;
+ stfs->f_bfree = prstfs->f_bfree;
+ stfs->f_bavail = prstfs->f_bavail;
+ stfs->f_files = prstfs->f_files;
+ stfs->f_ffree = prstfs->f_ffree;
+ stfs->f_fsid.__val[0] = prstfs->f_fsid[0] & 0xFFFFFFFFU;
+ stfs->f_fsid.__val[1] = prstfs->f_fsid[1] >> 32 & 0xFFFFFFFFU;
+ stfs->f_namelen = prstfs->f_namelen;
+ stfs->f_frsize = prstfs->f_frsize;
+}
+
+/* Converts proxy_stat structure to VFS stat structure */
+static void prstat_to_stat(struct stat *stbuf, ProxyStat *prstat)
+{
+ memset(stbuf, 0, sizeof(*stbuf));
+ stbuf->st_dev = prstat->st_dev;
+ stbuf->st_ino = prstat->st_ino;
+ stbuf->st_nlink = prstat->st_nlink;
+ stbuf->st_mode = prstat->st_mode;
+ stbuf->st_uid = prstat->st_uid;
+ stbuf->st_gid = prstat->st_gid;
+ stbuf->st_rdev = prstat->st_rdev;
+ stbuf->st_size = prstat->st_size;
+ stbuf->st_blksize = prstat->st_blksize;
+ stbuf->st_blocks = prstat->st_blocks;
+ stbuf->st_atim.tv_sec = prstat->st_atim_sec;
+ stbuf->st_atim.tv_nsec = prstat->st_atim_nsec;
+ stbuf->st_mtime = prstat->st_mtim_sec;
+ stbuf->st_mtim.tv_nsec = prstat->st_mtim_nsec;
+ stbuf->st_ctime = prstat->st_ctim_sec;
+ stbuf->st_ctim.tv_nsec = prstat->st_ctim_nsec;
+}
+
+/*
+ * Response contains two parts
+ * {header, data}
+ * header.type == T_ERROR, data -> -errno
+ * header.type == T_SUCCESS, data -> response
+ * size of errno/response is given by header.size
+ * returns < 0, on transport error. response is
+ * valid only if status >= 0.
+ */
+static int v9fs_receive_response(V9fsProxy *proxy, int type,
+ int *status, void *response)
+{
+ int retval;
+ ProxyHeader header;
+ struct iovec *reply = &proxy->in_iovec;
+
+ *status = 0;
+ reply->iov_len = 0;
+ retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
+ if (retval < 0) {
+ return retval;
+ }
+ reply->iov_len = PROXY_HDR_SZ;
+ proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
+ /*
+ * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
+ * return -ENOBUFS
+ */
+ if (header.size > PROXY_MAX_IO_SZ) {
+ int count;
+ while (header.size > 0) {
+ count = MIN(PROXY_MAX_IO_SZ, header.size);
+ count = socket_read(proxy->sockfd, reply->iov_base, count);
+ if (count < 0) {
+ return count;
+ }
+ header.size -= count;
+ }
+ *status = -ENOBUFS;
+ return 0;
+ }
+
+ retval = socket_read(proxy->sockfd,
+ reply->iov_base + PROXY_HDR_SZ, header.size);
+ if (retval < 0) {
+ return retval;
+ }
+ reply->iov_len += header.size;
+ /* there was an error during processing request */
+ if (header.type == T_ERROR) {
+ int ret;
+ ret = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
+ if (ret < 0) {
+ *status = ret;
+ }
+ return 0;
+ }
+
+ switch (type) {
+ case T_LSTAT: {
+ ProxyStat prstat;
+ retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
+ "qqqdddqqqqqqqqqq", &prstat.st_dev,
+ &prstat.st_ino, &prstat.st_nlink,
+ &prstat.st_mode, &prstat.st_uid,
+ &prstat.st_gid, &prstat.st_rdev,
+ &prstat.st_size, &prstat.st_blksize,
+ &prstat.st_blocks,
+ &prstat.st_atim_sec, &prstat.st_atim_nsec,
+ &prstat.st_mtim_sec, &prstat.st_mtim_nsec,
+ &prstat.st_ctim_sec, &prstat.st_ctim_nsec);
+ prstat_to_stat(response, &prstat);
+ break;
+ }
+ case T_STATFS: {
+ ProxyStatFS prstfs;
+ retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
+ "qqqqqqqqqqq", &prstfs.f_type,
+ &prstfs.f_bsize, &prstfs.f_blocks,
+ &prstfs.f_bfree, &prstfs.f_bavail,
+ &prstfs.f_files, &prstfs.f_ffree,
+ &prstfs.f_fsid[0], &prstfs.f_fsid[1],
+ &prstfs.f_namelen, &prstfs.f_frsize);
+ prstatfs_to_statfs(response, &prstfs);
+ break;
+ }
+ case T_READLINK: {
+ V9fsString target;
+ v9fs_string_init(&target);
+ retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &target);
+ strcpy(response, target.data);
+ v9fs_string_free(&target);
+ break;
+ }
+ case T_LGETXATTR:
+ case T_LLISTXATTR: {
+ V9fsString xattr;
+ v9fs_string_init(&xattr);
+ retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &xattr);
+ memcpy(response, xattr.data, xattr.size);
+ v9fs_string_free(&xattr);
+ break;
+ }
+ case T_GETVERSION:
+ proxy_unmarshal(reply, PROXY_HDR_SZ, "q", response);
+ break;
+ default:
+ return -1;
+ }
+ if (retval < 0) {
+ *status = retval;
+ }
+ return 0;
+}
+
+/*
+ * return < 0 on transport error.
+ * *status is valid only if return >= 0
+ */
+static int v9fs_receive_status(V9fsProxy *proxy,
+ struct iovec *reply, int *status)
+{
+ int retval;
+ ProxyHeader header;
+
+ *status = 0;
+ reply->iov_len = 0;
+ retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
+ if (retval < 0) {
+ return retval;
+ }
+ reply->iov_len = PROXY_HDR_SZ;
+ proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
+ if (header.size != sizeof(int)) {
+ *status = -ENOBUFS;
+ return 0;
+ }
+ retval = socket_read(proxy->sockfd,
+ reply->iov_base + PROXY_HDR_SZ, header.size);
+ if (retval < 0) {
+ return retval;
+ }
+ reply->iov_len += header.size;
+ proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
+ return 0;
+}
+
+/*
+ * Proxy->header and proxy->request written to socket by QEMU process.
+ * This request read by proxy helper process
+ * returns 0 on success and -errno on error
+ */
+static int v9fs_request(V9fsProxy *proxy, int type,
+ void *response, const char *fmt, ...)
+{
+ dev_t rdev;
+ va_list ap;
+ int size = 0;
+ int retval = 0;
+ uint64_t offset;
+ ProxyHeader header = { 0, 0};
+ struct timespec spec[2];
+ int flags, mode, uid, gid;
+ V9fsString *name, *value;
+ V9fsString *path, *oldpath;
+ struct iovec *iovec = NULL, *reply = NULL;
+
+ qemu_mutex_lock(&proxy->mutex);
+
+ if (proxy->sockfd == -1) {
+ retval = -EIO;
+ goto err_out;
+ }
+ iovec = &proxy->out_iovec;
+ reply = &proxy->in_iovec;
+ va_start(ap, fmt);
+ switch (type) {
+ case T_OPEN:
+ path = va_arg(ap, V9fsString *);
+ flags = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_OPEN;
+ }
+ break;
+ case T_CREATE:
+ path = va_arg(ap, V9fsString *);
+ flags = va_arg(ap, int);
+ mode = va_arg(ap, int);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdddd", path,
+ flags, mode, uid, gid);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_CREATE;
+ }
+ break;
+ case T_MKNOD:
+ path = va_arg(ap, V9fsString *);
+ mode = va_arg(ap, int);
+ rdev = va_arg(ap, long int);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsdq",
+ uid, gid, path, mode, rdev);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_MKNOD;
+ }
+ break;
+ case T_MKDIR:
+ path = va_arg(ap, V9fsString *);
+ mode = va_arg(ap, int);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsd",
+ uid, gid, path, mode);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_MKDIR;
+ }
+ break;
+ case T_SYMLINK:
+ oldpath = va_arg(ap, V9fsString *);
+ path = va_arg(ap, V9fsString *);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddss",
+ uid, gid, oldpath, path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_SYMLINK;
+ }
+ break;
+ case T_LINK:
+ oldpath = va_arg(ap, V9fsString *);
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss",
+ oldpath, path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LINK;
+ }
+ break;
+ case T_LSTAT:
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LSTAT;
+ }
+ break;
+ case T_READLINK:
+ path = va_arg(ap, V9fsString *);
+ size = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, size);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_READLINK;
+ }
+ break;
+ case T_STATFS:
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_STATFS;
+ }
+ break;
+ case T_CHMOD:
+ path = va_arg(ap, V9fsString *);
+ mode = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, mode);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_CHMOD;
+ }
+ break;
+ case T_CHOWN:
+ path = va_arg(ap, V9fsString *);
+ uid = va_arg(ap, int);
+ gid = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdd", path, uid, gid);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_CHOWN;
+ }
+ break;
+ case T_TRUNCATE:
+ path = va_arg(ap, V9fsString *);
+ offset = va_arg(ap, uint64_t);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sq", path, offset);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_TRUNCATE;
+ }
+ break;
+ case T_UTIME:
+ path = va_arg(ap, V9fsString *);
+ spec[0].tv_sec = va_arg(ap, long);
+ spec[0].tv_nsec = va_arg(ap, long);
+ spec[1].tv_sec = va_arg(ap, long);
+ spec[1].tv_nsec = va_arg(ap, long);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sqqqq", path,
+ spec[0].tv_sec, spec[1].tv_nsec,
+ spec[1].tv_sec, spec[1].tv_nsec);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_UTIME;
+ }
+ break;
+ case T_RENAME:
+ oldpath = va_arg(ap, V9fsString *);
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", oldpath, path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_RENAME;
+ }
+ break;
+ case T_REMOVE:
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_REMOVE;
+ }
+ break;
+ case T_LGETXATTR:
+ size = va_arg(ap, int);
+ path = va_arg(ap, V9fsString *);
+ name = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ,
+ "dss", size, path, name);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LGETXATTR;
+ }
+ break;
+ case T_LLISTXATTR:
+ size = va_arg(ap, int);
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ds", size, path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LLISTXATTR;
+ }
+ break;
+ case T_LSETXATTR:
+ path = va_arg(ap, V9fsString *);
+ name = va_arg(ap, V9fsString *);
+ value = va_arg(ap, V9fsString *);
+ size = va_arg(ap, int);
+ flags = va_arg(ap, int);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sssdd",
+ path, name, value, size, flags);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LSETXATTR;
+ }
+ break;
+ case T_LREMOVEXATTR:
+ path = va_arg(ap, V9fsString *);
+ name = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", path, name);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_LREMOVEXATTR;
+ }
+ break;
+ case T_GETVERSION:
+ path = va_arg(ap, V9fsString *);
+ retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
+ if (retval > 0) {
+ header.size = retval;
+ header.type = T_GETVERSION;
+ }
+ break;
+ default:
+ error_report("Invalid type %d", type);
+ retval = -EINVAL;
+ break;
+ }
+ va_end(ap);
+
+ if (retval < 0) {
+ goto err_out;
+ }
+
+ /* marshal the header details */
+ proxy_marshal(iovec, 0, "dd", header.type, header.size);
+ header.size += PROXY_HDR_SZ;
+
+ retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
+ if (retval != header.size) {
+ goto close_error;
+ }
+
+ switch (type) {
+ case T_OPEN:
+ case T_CREATE:
+ /*
+ * A file descriptor is returned as response for
+ * T_OPEN,T_CREATE on success
+ */
+ if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
+ goto close_error;
+ }
+ break;
+ case T_MKNOD:
+ case T_MKDIR:
+ case T_SYMLINK:
+ case T_LINK:
+ case T_CHMOD:
+ case T_CHOWN:
+ case T_RENAME:
+ case T_TRUNCATE:
+ case T_UTIME:
+ case T_REMOVE:
+ case T_LSETXATTR:
+ case T_LREMOVEXATTR:
+ if (v9fs_receive_status(proxy, reply, &retval) < 0) {
+ goto close_error;
+ }
+ break;
+ case T_LSTAT:
+ case T_READLINK:
+ case T_STATFS:
+ case T_GETVERSION:
+ if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
+ goto close_error;
+ }
+ break;
+ case T_LGETXATTR:
+ case T_LLISTXATTR:
+ if (!size) {
+ if (v9fs_receive_status(proxy, reply, &retval) < 0) {
+ goto close_error;
+ }
+ } else {
+ if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
+ goto close_error;
+ }
+ }
+ break;
+ }
+
+err_out:
+ qemu_mutex_unlock(&proxy->mutex);
+ return retval;
+
+close_error:
+ close(proxy->sockfd);
+ proxy->sockfd = -1;
+ qemu_mutex_unlock(&proxy->mutex);
+ return -EIO;
+}
+
+static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
+{
+ int retval;
+ retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, "s", fs_path);
+ if (retval < 0) {
+ errno = -retval;
+ return -1;
+ }
+ return retval;
+}
+
+static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
+ char *buf, size_t bufsz)
+{
+ int retval;
+ retval = v9fs_request(fs_ctx->private, T_READLINK, buf, "sd",
+ fs_path, bufsz);
+ if (retval < 0) {
+ errno = -retval;
+ return -1;
+ }
+ return strlen(buf);
+}
+
+static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return close(fs->fd);
+}
+
+static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return closedir(fs->dir);
+}
+
+static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
+ int flags, V9fsFidOpenState *fs)
+{
+ fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, flags);
+ if (fs->fd < 0) {
+ errno = -fs->fd;
+ fs->fd = -1;
+ }
+ return fs->fd;
+}
+
+static int proxy_opendir(FsContext *ctx,
+ V9fsPath *fs_path, V9fsFidOpenState *fs)
+{
+ int serrno, fd;
+
+ fs->dir = NULL;
+ fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY);
+ if (fd < 0) {
+ errno = -fd;
+ return -1;
+ }
+ fs->dir = fdopendir(fd);
+ if (!fs->dir) {
+ serrno = errno;
+ close(fd);
+ errno = serrno;
+ return -1;
+ }
+ return 0;
+}
+
+static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ rewinddir(fs->dir);
+}
+
+static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ return telldir(fs->dir);
+}
+
+static int proxy_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
+ struct dirent *entry,
+ struct dirent **result)
+{
+ return readdir_r(fs->dir, entry, result);
+}
+
+static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
+{
+ seekdir(fs->dir, off);
+}
+
+static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ ssize_t ret;
+#ifdef CONFIG_PREADV
+ ret = preadv(fs->fd, iov, iovcnt, offset);
+#else
+ ret = lseek(fs->fd, offset, SEEK_SET);
+ if (ret >= 0) {
+ ret = readv(fs->fd, iov, iovcnt);
+ }
+#endif
+ return ret;
+}
+
+static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_PREADV
+ ret = pwritev(fs->fd, iov, iovcnt, offset);
+#else
+ ret = lseek(fs->fd, offset, SEEK_SET);
+ if (ret >= 0) {
+ ret = writev(fs->fd, iov, iovcnt);
+ }
+#endif
+#ifdef CONFIG_SYNC_FILE_RANGE
+ if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
+ /*
+ * Initiate a writeback. This is not a data integrity sync.
+ * We want to ensure that we don't leave dirty pages in the cache
+ * after write when writeout=immediate is sepcified.
+ */
+ sync_file_range(fs->fd, offset, ret,
+ SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
+ }
+#endif
+ return ret;
+}
+
+static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ int retval;
+ retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, "sd",
+ fs_path, credp->fc_mode);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ int retval;
+ V9fsString fullname;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+ retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, "sdqdd",
+ &fullname, credp->fc_mode, credp->fc_rdev,
+ credp->fc_uid, credp->fc_gid);
+ v9fs_string_free(&fullname);
+ if (retval < 0) {
+ errno = -retval;
+ retval = -1;
+ }
+ return retval;
+}
+
+static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, FsCred *credp)
+{
+ int retval;
+ V9fsString fullname;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+ retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, "sddd", &fullname,
+ credp->fc_mode, credp->fc_uid, credp->fc_gid);
+ v9fs_string_free(&fullname);
+ if (retval < 0) {
+ errno = -retval;
+ retval = -1;
+ }
+ v9fs_string_free(&fullname);
+ return retval;
+}
+
+static int proxy_fstat(FsContext *fs_ctx, int fid_type,
+ V9fsFidOpenState *fs, struct stat *stbuf)
+{
+ int fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+ return fstat(fd, stbuf);
+}
+
+static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
+ int flags, FsCred *credp, V9fsFidOpenState *fs)
+{
+ V9fsString fullname;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+ fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, "sdddd",
+ &fullname, flags, credp->fc_mode,
+ credp->fc_uid, credp->fc_gid);
+ v9fs_string_free(&fullname);
+ if (fs->fd < 0) {
+ errno = -fs->fd;
+ fs->fd = -1;
+ }
+ return fs->fd;
+}
+
+static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
+ V9fsPath *dir_path, const char *name, FsCred *credp)
+{
+ int retval;
+ V9fsString fullname, target;
+
+ v9fs_string_init(&fullname);
+ v9fs_string_init(&target);
+
+ v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+ v9fs_string_sprintf(&target, "%s", oldpath);
+
+ retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, "ssdd",
+ &target, &fullname, credp->fc_uid, credp->fc_gid);
+ v9fs_string_free(&fullname);
+ v9fs_string_free(&target);
+ if (retval < 0) {
+ errno = -retval;
+ retval = -1;
+ }
+ return retval;
+}
+
+static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
+ V9fsPath *dirpath, const char *name)
+{
+ int retval;
+ V9fsString newpath;
+
+ v9fs_string_init(&newpath);
+ v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
+
+ retval = v9fs_request(ctx->private, T_LINK, NULL, "ss", oldpath, &newpath);
+ v9fs_string_free(&newpath);
+ if (retval < 0) {
+ errno = -retval;
+ retval = -1;
+ }
+ return retval;
+}
+
+static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
+{
+ int retval;
+
+ retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, "sq", fs_path, size);
+ if (retval < 0) {
+ errno = -retval;
+ return -1;
+ }
+ return 0;
+}
+
+static int proxy_rename(FsContext *ctx, const char *oldpath,
+ const char *newpath)
+{
+ int retval;
+ V9fsString oldname, newname;
+
+ v9fs_string_init(&oldname);
+ v9fs_string_init(&newname);
+
+ v9fs_string_sprintf(&oldname, "%s", oldpath);
+ v9fs_string_sprintf(&newname, "%s", newpath);
+ retval = v9fs_request(ctx->private, T_RENAME, NULL, "ss",
+ &oldname, &newname);
+ v9fs_string_free(&oldname);
+ v9fs_string_free(&newname);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
+{
+ int retval;
+ retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, "sdd",
+ fs_path, credp->fc_uid, credp->fc_gid);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
+ const struct timespec *buf)
+{
+ int retval;
+ retval = v9fs_request(s->private, T_UTIME, NULL, "sqqqq",
+ fs_path,
+ buf[0].tv_sec, buf[0].tv_nsec,
+ buf[1].tv_sec, buf[1].tv_nsec);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_remove(FsContext *ctx, const char *path)
+{
+ int retval;
+ V9fsString name;
+ v9fs_string_init(&name);
+ v9fs_string_sprintf(&name, "%s", path);
+ retval = v9fs_request(ctx->private, T_REMOVE, NULL, "s", &name);
+ v9fs_string_free(&name);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_fsync(FsContext *ctx, int fid_type,
+ V9fsFidOpenState *fs, int datasync)
+{
+ int fd;
+
+ if (fid_type == P9_FID_DIR) {
+ fd = dirfd(fs->dir);
+ } else {
+ fd = fs->fd;
+ }
+
+ if (datasync) {
+ return qemu_fdatasync(fd);
+ } else {
+ return fsync(fd);
+ }
+}
+
+static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
+{
+ int retval;
+ retval = v9fs_request(s->private, T_STATFS, stbuf, "s", fs_path);
+ if (retval < 0) {
+ errno = -retval;
+ return -1;
+ }
+ return retval;
+}
+
+static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name, void *value, size_t size)
+{
+ int retval;
+ V9fsString xname;
+
+ v9fs_string_init(&xname);
+ v9fs_string_sprintf(&xname, "%s", name);
+ retval = v9fs_request(ctx->private, T_LGETXATTR, value, "dss", size,
+ fs_path, &xname);
+ v9fs_string_free(&xname);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
+ void *value, size_t size)
+{
+ int retval;
+ retval = v9fs_request(ctx->private, T_LLISTXATTR, value, "ds", size,
+ fs_path);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
+ void *value, size_t size, int flags)
+{
+ int retval;
+ V9fsString xname, xvalue;
+
+ v9fs_string_init(&xname);
+ v9fs_string_sprintf(&xname, "%s", name);
+
+ v9fs_string_init(&xvalue);
+ xvalue.size = size;
+ xvalue.data = g_malloc(size);
+ memcpy(xvalue.data, value, size);
+
+ retval = v9fs_request(ctx->private, T_LSETXATTR, value, "sssdd",
+ fs_path, &xname, &xvalue, size, flags);
+ v9fs_string_free(&xname);
+ v9fs_string_free(&xvalue);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
+ const char *name)
+{
+ int retval;
+ V9fsString xname;
+
+ v9fs_string_init(&xname);
+ v9fs_string_sprintf(&xname, "%s", name);
+ retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, "ss",
+ fs_path, &xname);
+ v9fs_string_free(&xname);
+ if (retval < 0) {
+ errno = -retval;
+ }
+ return retval;
+}
+
+static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
+ const char *name, V9fsPath *target)
+{
+ if (dir_path) {
+ v9fs_string_sprintf((V9fsString *)target, "%s/%s",
+ dir_path->data, name);
+ } else {
+ v9fs_string_sprintf((V9fsString *)target, "%s", name);
+ }
+ /* Bump the size for including terminating NULL */
+ target->size++;
+ return 0;
+}
+
+static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
+ const char *old_name, V9fsPath *newdir,
+ const char *new_name)
+{
+ int ret;
+ V9fsString old_full_name, new_full_name;
+
+ v9fs_string_init(&old_full_name);
+ v9fs_string_init(&new_full_name);
+
+ v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
+ v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
+
+ ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
+ v9fs_string_free(&old_full_name);
+ v9fs_string_free(&new_full_name);
+ return ret;
+}
+
+static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
+ const char *name, int flags)
+{
+ int ret;
+ V9fsString fullname;
+ v9fs_string_init(&fullname);
+
+ v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
+ ret = proxy_remove(ctx, fullname.data);
+ v9fs_string_free(&fullname);
+
+ return ret;
+}
+
+static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
+ mode_t st_mode, uint64_t *st_gen)
+{
+ int err;
+
+ /* Do not try to open special files like device nodes, fifos etc
+ * we can get fd for regular files and directories only
+ */
+ if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
+ errno = ENOTTY;
+ return -1;
+ }
+ err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, "s", path);
+ if (err < 0) {
+ errno = -err;
+ err = -1;
+ }
+ return err;
+}
+
+static int connect_namedsocket(const char *path)
+{
+ int sockfd, size;
+ struct sockaddr_un helper;
+
+ if (strlen(path) >= sizeof(helper.sun_path)) {
+ fprintf(stderr, "Socket name too large\n");
+ return -1;
+ }
+ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sockfd < 0) {
+ fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
+ return -1;
+ }
+ strcpy(helper.sun_path, path);
+ helper.sun_family = AF_UNIX;
+ size = strlen(helper.sun_path) + sizeof(helper.sun_family);
+ if (connect(sockfd, (struct sockaddr *)&helper, size) < 0) {
+ fprintf(stderr, "failed to connect to %s: %s\n", path, strerror(errno));
+ close(sockfd);
+ return -1;
+ }
+
+ /* remove the socket for security reasons */
+ unlink(path);
+ return sockfd;
+}
+
+static int proxy_parse_opts(QemuOpts *opts, struct FsDriverEntry *fs)
+{
+ const char *socket = qemu_opt_get(opts, "socket");
+ const char *sock_fd = qemu_opt_get(opts, "sock_fd");
+
+ if (!socket && !sock_fd) {
+ fprintf(stderr, "socket and sock_fd none of the option specified\n");
+ return -1;
+ }
+ if (socket && sock_fd) {
+ fprintf(stderr, "Both socket and sock_fd options specified\n");
+ return -1;
+ }
+ if (socket) {
+ fs->path = g_strdup(socket);
+ fs->export_flags = V9FS_PROXY_SOCK_NAME;
+ } else {
+ fs->path = g_strdup(sock_fd);
+ fs->export_flags = V9FS_PROXY_SOCK_FD;
+ }
+ return 0;
+}
+
+static int proxy_init(FsContext *ctx)
+{
+ V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy));
+ int sock_id;
+
+ if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
+ sock_id = connect_namedsocket(ctx->fs_root);
+ } else {
+ sock_id = atoi(ctx->fs_root);
+ if (sock_id < 0) {
+ fprintf(stderr, "socket descriptor not initialized\n");
+ }
+ }
+ if (sock_id < 0) {
+ g_free(proxy);
+ return -1;
+ }
+ g_free(ctx->fs_root);
+ ctx->fs_root = NULL;
+
+ proxy->in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
+ proxy->in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
+ proxy->out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
+ proxy->out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
+
+ ctx->private = proxy;
+ proxy->sockfd = sock_id;
+ qemu_mutex_init(&proxy->mutex);
+
+ ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
+ ctx->exops.get_st_gen = proxy_ioc_getversion;
+ return 0;
+}
+
+FileOperations proxy_ops = {
+ .parse_opts = proxy_parse_opts,
+ .init = proxy_init,
+ .lstat = proxy_lstat,
+ .readlink = proxy_readlink,
+ .close = proxy_close,
+ .closedir = proxy_closedir,
+ .open = proxy_open,
+ .opendir = proxy_opendir,
+ .rewinddir = proxy_rewinddir,
+ .telldir = proxy_telldir,
+ .readdir_r = proxy_readdir_r,
+ .seekdir = proxy_seekdir,
+ .preadv = proxy_preadv,
+ .pwritev = proxy_pwritev,
+ .chmod = proxy_chmod,
+ .mknod = proxy_mknod,
+ .mkdir = proxy_mkdir,
+ .fstat = proxy_fstat,
+ .open2 = proxy_open2,
+ .symlink = proxy_symlink,
+ .link = proxy_link,
+ .truncate = proxy_truncate,
+ .rename = proxy_rename,
+ .chown = proxy_chown,
+ .utimensat = proxy_utimensat,
+ .remove = proxy_remove,
+ .fsync = proxy_fsync,
+ .statfs = proxy_statfs,
+ .lgetxattr = proxy_lgetxattr,
+ .llistxattr = proxy_llistxattr,
+ .lsetxattr = proxy_lsetxattr,
+ .lremovexattr = proxy_lremovexattr,
+ .name_to_path = proxy_name_to_path,
+ .renameat = proxy_renameat,
+ .unlinkat = proxy_unlinkat,
+};
diff --git a/src/hw/9pfs/virtio-9p-proxy.h b/src/hw/9pfs/virtio-9p-proxy.h
new file mode 100644
index 0000000..005c1ad
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-proxy.h
@@ -0,0 +1,95 @@
+/*
+ * Virtio 9p Proxy callback
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * M. Mohan Kumar <mohan@in.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef _QEMU_VIRTIO_9P_PROXY_H
+#define _QEMU_VIRTIO_9P_PROXY_H
+
+#define PROXY_MAX_IO_SZ (64 * 1024)
+#define V9FS_FD_VALID INT_MAX
+
+/*
+ * proxy iovec only support one element and
+ * marsha/unmarshal doesn't do little endian conversion.
+ */
+#define proxy_unmarshal(in_sg, offset, fmt, args...) \
+ v9fs_unmarshal(in_sg, 1, offset, 0, fmt, ##args)
+#define proxy_marshal(out_sg, offset, fmt, args...) \
+ v9fs_marshal(out_sg, 1, offset, 0, fmt, ##args)
+
+union MsgControl {
+ struct cmsghdr cmsg;
+ char control[CMSG_SPACE(sizeof(int))];
+};
+
+typedef struct {
+ uint32_t type;
+ uint32_t size;
+} ProxyHeader;
+
+#define PROXY_HDR_SZ (sizeof(ProxyHeader))
+
+enum {
+ T_SUCCESS = 0,
+ T_ERROR,
+ T_OPEN,
+ T_CREATE,
+ T_MKNOD,
+ T_MKDIR,
+ T_SYMLINK,
+ T_LINK,
+ T_LSTAT,
+ T_READLINK,
+ T_STATFS,
+ T_CHMOD,
+ T_CHOWN,
+ T_TRUNCATE,
+ T_UTIME,
+ T_RENAME,
+ T_REMOVE,
+ T_LGETXATTR,
+ T_LLISTXATTR,
+ T_LSETXATTR,
+ T_LREMOVEXATTR,
+ T_GETVERSION,
+};
+
+typedef struct {
+ uint64_t st_dev;
+ uint64_t st_ino;
+ uint64_t st_nlink;
+ uint32_t st_mode;
+ uint32_t st_uid;
+ uint32_t st_gid;
+ uint64_t st_rdev;
+ uint64_t st_size;
+ uint64_t st_blksize;
+ uint64_t st_blocks;
+ uint64_t st_atim_sec;
+ uint64_t st_atim_nsec;
+ uint64_t st_mtim_sec;
+ uint64_t st_mtim_nsec;
+ uint64_t st_ctim_sec;
+ uint64_t st_ctim_nsec;
+} ProxyStat;
+
+typedef struct {
+ uint64_t f_type;
+ uint64_t f_bsize;
+ uint64_t f_blocks;
+ uint64_t f_bfree;
+ uint64_t f_bavail;
+ uint64_t f_files;
+ uint64_t f_ffree;
+ uint64_t f_fsid[2];
+ uint64_t f_namelen;
+ uint64_t f_frsize;
+} ProxyStatFS;
+#endif
diff --git a/src/hw/9pfs/virtio-9p-synth.c b/src/hw/9pfs/virtio-9p-synth.c
new file mode 100644
index 0000000..a0ab9a8
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-synth.c
@@ -0,0 +1,573 @@
+/*
+ * Virtio 9p synthetic file system support
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * Malahal Naineni <malahal@us.ibm.com>
+ * 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 "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "virtio-9p-xattr.h"
+#include "fsdev/qemu-fsdev.h"
+#include "virtio-9p-synth.h"
+#include "qemu/rcu.h"
+#include "qemu/rcu_queue.h"
+#include <sys/stat.h>
+
+/* Root node for synth file system */
+static V9fsSynthNode v9fs_synth_root = {
+ .name = "/",
+ .actual_attr = {
+ .mode = 0555 | S_IFDIR,
+ .nlink = 1,
+ },
+ .attr = &v9fs_synth_root.actual_attr,
+};
+
+static QemuMutex v9fs_synth_mutex;
+static int v9fs_synth_node_count;
+/* set to 1 when the synth fs is ready */
+static int v9fs_synth_fs;
+
+static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
+ const char *name,
+ V9fsSynthNodeAttr *attr, int inode)
+{
+ V9fsSynthNode *node;
+
+ /* Add directory type and remove write bits */
+ mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
+ node = g_malloc0(sizeof(V9fsSynthNode));
+ if (attr) {
+ /* We are adding .. or . entries */
+ node->attr = attr;
+ node->attr->nlink++;
+ } else {
+ node->attr = &node->actual_attr;
+ node->attr->inode = inode;
+ node->attr->nlink = 1;
+ /* We don't allow write to directories */
+ node->attr->mode = mode;
+ node->attr->write = NULL;
+ node->attr->read = NULL;
+ }
+ node->private = node;
+ pstrcpy(node->name, sizeof(node->name), name);
+ QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
+ return node;
+}
+
+int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
+ const char *name, V9fsSynthNode **result)
+{
+ int ret;
+ V9fsSynthNode *node, *tmp;
+
+ if (!v9fs_synth_fs) {
+ return EAGAIN;
+ }
+ if (!name || (strlen(name) >= NAME_MAX)) {
+ return EINVAL;
+ }
+ if (!parent) {
+ parent = &v9fs_synth_root;
+ }
+ qemu_mutex_lock(&v9fs_synth_mutex);
+ QLIST_FOREACH(tmp, &parent->child, sibling) {
+ if (!strcmp(tmp->name, name)) {
+ ret = EEXIST;
+ goto err_out;
+ }
+ }
+ /* Add the name */
+ node = v9fs_add_dir_node(parent, mode, name, NULL, v9fs_synth_node_count++);
+ v9fs_add_dir_node(node, parent->attr->mode, "..",
+ parent->attr, parent->attr->inode);
+ v9fs_add_dir_node(node, node->attr->mode, ".",
+ node->attr, node->attr->inode);
+ *result = node;
+ ret = 0;
+err_out:
+ qemu_mutex_unlock(&v9fs_synth_mutex);
+ return ret;
+}
+
+int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
+ const char *name, v9fs_synth_read read,
+ v9fs_synth_write write, void *arg)
+{
+ int ret;
+ V9fsSynthNode *node, *tmp;
+
+ if (!v9fs_synth_fs) {
+ return EAGAIN;
+ }
+ if (!name || (strlen(name) >= NAME_MAX)) {
+ return EINVAL;
+ }
+ if (!parent) {
+ parent = &v9fs_synth_root;
+ }
+
+ qemu_mutex_lock(&v9fs_synth_mutex);
+ QLIST_FOREACH(tmp, &parent->child, sibling) {
+ if (!strcmp(tmp->name, name)) {
+ ret = EEXIST;
+ goto err_out;
+ }
+ }
+ /* Add file type and remove write bits */
+ mode = ((mode & 0777) | S_IFREG);
+ node = g_malloc0(sizeof(V9fsSynthNode));
+ node->attr = &node->actual_attr;
+ node->attr->inode = v9fs_synth_node_count++;
+ node->attr->nlink = 1;
+ node->attr->read = read;
+ node->attr->write = write;
+ node->attr->mode = mode;
+ node->private = arg;
+ pstrcpy(node->name, sizeof(node->name), name);
+ QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
+ ret = 0;
+err_out:
+ qemu_mutex_unlock(&v9fs_synth_mutex);
+ return ret;
+}
+
+static void v9fs_synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
+{
+ stbuf->st_dev = 0;
+ stbuf->st_ino = node->attr->inode;
+ stbuf->st_mode = node->attr->mode;
+ stbuf->st_nlink = node->attr->nlink;
+ stbuf->st_uid = 0;
+ stbuf->st_gid = 0;
+ stbuf->st_rdev = 0;
+ stbuf->st_size = 0;
+ stbuf->st_blksize = 0;
+ stbuf->st_blocks = 0;
+ stbuf->st_atime = 0;
+ stbuf->st_mtime = 0;
+ stbuf->st_ctime = 0;
+}
+
+static int v9fs_synth_lstat(FsContext *fs_ctx,
+ V9fsPath *fs_path, struct stat *stbuf)
+{
+ V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
+
+ v9fs_synth_fill_statbuf(node, stbuf);
+ return 0;
+}
+
+static int v9fs_synth_fstat(FsContext *fs_ctx, int fid_type,
+ V9fsFidOpenState *fs, struct stat *stbuf)
+{
+ V9fsSynthOpenState *synth_open = fs->private;
+ v9fs_synth_fill_statbuf(synth_open->node, stbuf);
+ return 0;
+}
+
+static int v9fs_synth_opendir(FsContext *ctx,
+ V9fsPath *fs_path, V9fsFidOpenState *fs)
+{
+ V9fsSynthOpenState *synth_open;
+ V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
+
+ synth_open = g_malloc(sizeof(*synth_open));
+ synth_open->node = node;
+ node->open_count++;
+ fs->private = synth_open;
+ return 0;
+}
+
+static int v9fs_synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ V9fsSynthOpenState *synth_open = fs->private;
+ V9fsSynthNode *node = synth_open->node;
+
+ node->open_count--;
+ g_free(synth_open);
+ fs->private = NULL;
+ return 0;
+}
+
+static off_t v9fs_synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ V9fsSynthOpenState *synth_open = fs->private;
+ return synth_open->offset;
+}
+
+static void v9fs_synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
+{
+ V9fsSynthOpenState *synth_open = fs->private;
+ synth_open->offset = off;
+}
+
+static void v9fs_synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ v9fs_synth_seekdir(ctx, fs, 0);
+}
+
+static void v9fs_synth_direntry(V9fsSynthNode *node,
+ struct dirent *entry, off_t off)
+{
+ strcpy(entry->d_name, node->name);
+ entry->d_ino = node->attr->inode;
+ entry->d_off = off + 1;
+}
+
+static int v9fs_synth_get_dentry(V9fsSynthNode *dir, struct dirent *entry,
+ struct dirent **result, off_t off)
+{
+ int i = 0;
+ V9fsSynthNode *node;
+
+ rcu_read_lock();
+ QLIST_FOREACH(node, &dir->child, sibling) {
+ /* This is the off child of the directory */
+ if (i == off) {
+ break;
+ }
+ i++;
+ }
+ rcu_read_unlock();
+ if (!node) {
+ /* end of directory */
+ *result = NULL;
+ return 0;
+ }
+ v9fs_synth_direntry(node, entry, off);
+ *result = entry;
+ return 0;
+}
+
+static int v9fs_synth_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
+ struct dirent *entry, struct dirent **result)
+{
+ int ret;
+ V9fsSynthOpenState *synth_open = fs->private;
+ V9fsSynthNode *node = synth_open->node;
+ ret = v9fs_synth_get_dentry(node, entry, result, synth_open->offset);
+ if (!ret && *result != NULL) {
+ synth_open->offset++;
+ }
+ return ret;
+}
+
+static int v9fs_synth_open(FsContext *ctx, V9fsPath *fs_path,
+ int flags, V9fsFidOpenState *fs)
+{
+ V9fsSynthOpenState *synth_open;
+ V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
+
+ synth_open = g_malloc(sizeof(*synth_open));
+ synth_open->node = node;
+ node->open_count++;
+ fs->private = synth_open;
+ return 0;
+}
+
+static int v9fs_synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
+ const char *name, int flags,
+ FsCred *credp, V9fsFidOpenState *fs)
+{
+ errno = ENOSYS;
+ return -1;
+}
+
+static int v9fs_synth_close(FsContext *ctx, V9fsFidOpenState *fs)
+{
+ V9fsSynthOpenState *synth_open = fs->private;
+ V9fsSynthNode *node = synth_open->node;
+
+ node->open_count--;
+ g_free(synth_open);
+ fs->private = NULL;
+ return 0;
+}
+
+static ssize_t v9fs_synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ int i, count = 0, wcount;
+ V9fsSynthOpenState *synth_open = fs->private;
+ V9fsSynthNode *node = synth_open->node;
+ if (!node->attr->write) {
+ errno = EPERM;
+ return -1;
+ }
+ for (i = 0; i < iovcnt; i++) {
+ wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
+ offset, node->private);
+ offset += wcount;
+ count += wcount;
+ /* If we wrote less than requested. we are done */
+ if (wcount < iov[i].iov_len) {
+ break;
+ }
+ }
+ return count;
+}
+
+static ssize_t v9fs_synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
+ const struct iovec *iov,
+ int iovcnt, off_t offset)
+{
+ int i, count = 0, rcount;
+ V9fsSynthOpenState *synth_open = fs->private;
+ V9fsSynthNode *node = synth_open->node;
+ if (!node->attr->read) {
+ errno = EPERM;
+ return -1;
+ }
+ for (i = 0; i < iovcnt; i++) {
+ rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
+ offset, node->private);
+ offset += rcount;
+ count += rcount;
+ /* If we read less than requested. we are done */
+ if (rcount < iov[i].iov_len) {
+ break;
+ }
+ }
+ return count;
+}
+
+static int v9fs_synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
+{
+ errno = ENOSYS;
+ return -1;
+}
+
+static int v9fs_synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_mknod(FsContext *fs_ctx, V9fsPath *path,
+ const char *buf, FsCred *credp)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
+ const char *buf, FsCred *credp)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static ssize_t v9fs_synth_readlink(FsContext *fs_ctx, V9fsPath *path,
+ char *buf, size_t bufsz)
+{
+ errno = ENOSYS;
+ return -1;
+}
+
+static int v9fs_synth_symlink(FsContext *fs_ctx, const char *oldpath,
+ V9fsPath *newpath, const char *buf, FsCred *credp)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
+ V9fsPath *newpath, const char *buf)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_rename(FsContext *ctx, const char *oldpath,
+ const char *newpath)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
+ const struct timespec *buf)
+{
+ errno = EPERM;
+ return 0;
+}
+
+static int v9fs_synth_remove(FsContext *ctx, const char *path)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_fsync(FsContext *ctx, int fid_type,
+ V9fsFidOpenState *fs, int datasync)
+{
+ errno = ENOSYS;
+ return 0;
+}
+
+static int v9fs_synth_statfs(FsContext *s, V9fsPath *fs_path,
+ struct statfs *stbuf)
+{
+ stbuf->f_type = 0xABCD;
+ stbuf->f_bsize = 512;
+ stbuf->f_blocks = 0;
+ stbuf->f_files = v9fs_synth_node_count;
+ stbuf->f_namelen = NAME_MAX;
+ return 0;
+}
+
+static ssize_t v9fs_synth_lgetxattr(FsContext *ctx, V9fsPath *path,
+ const char *name, void *value, size_t size)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static ssize_t v9fs_synth_llistxattr(FsContext *ctx, V9fsPath *path,
+ void *value, size_t size)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static int v9fs_synth_lsetxattr(FsContext *ctx, V9fsPath *path,
+ const char *name, void *value,
+ size_t size, int flags)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static int v9fs_synth_lremovexattr(FsContext *ctx,
+ V9fsPath *path, const char *name)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static int v9fs_synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
+ const char *name, V9fsPath *target)
+{
+ V9fsSynthNode *node;
+ V9fsSynthNode *dir_node;
+
+ /* "." and ".." are not allowed */
+ if (!strcmp(name, ".") || !strcmp(name, "..")) {
+ errno = EINVAL;
+ return -1;
+
+ }
+ if (!dir_path) {
+ dir_node = &v9fs_synth_root;
+ } else {
+ dir_node = *(V9fsSynthNode **)dir_path->data;
+ }
+ if (!strcmp(name, "/")) {
+ node = dir_node;
+ goto out;
+ }
+ /* search for the name in the childern */
+ rcu_read_lock();
+ QLIST_FOREACH(node, &dir_node->child, sibling) {
+ if (!strcmp(node->name, name)) {
+ break;
+ }
+ }
+ rcu_read_unlock();
+
+ if (!node) {
+ errno = ENOENT;
+ return -1;
+ }
+out:
+ /* Copy the node pointer to fid */
+ target->data = g_malloc(sizeof(void *));
+ memcpy(target->data, &node, sizeof(void *));
+ target->size = sizeof(void *);
+ return 0;
+}
+
+static int v9fs_synth_renameat(FsContext *ctx, V9fsPath *olddir,
+ const char *old_name, V9fsPath *newdir,
+ const char *new_name)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_unlinkat(FsContext *ctx, V9fsPath *dir,
+ const char *name, int flags)
+{
+ errno = EPERM;
+ return -1;
+}
+
+static int v9fs_synth_init(FsContext *ctx)
+{
+ QLIST_INIT(&v9fs_synth_root.child);
+ qemu_mutex_init(&v9fs_synth_mutex);
+
+ /* Add "." and ".." entries for root */
+ v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
+ "..", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
+ v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
+ ".", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
+
+ /* Mark the subsystem is ready for use */
+ v9fs_synth_fs = 1;
+ return 0;
+}
+
+FileOperations synth_ops = {
+ .init = v9fs_synth_init,
+ .lstat = v9fs_synth_lstat,
+ .readlink = v9fs_synth_readlink,
+ .close = v9fs_synth_close,
+ .closedir = v9fs_synth_closedir,
+ .open = v9fs_synth_open,
+ .opendir = v9fs_synth_opendir,
+ .rewinddir = v9fs_synth_rewinddir,
+ .telldir = v9fs_synth_telldir,
+ .readdir_r = v9fs_synth_readdir_r,
+ .seekdir = v9fs_synth_seekdir,
+ .preadv = v9fs_synth_preadv,
+ .pwritev = v9fs_synth_pwritev,
+ .chmod = v9fs_synth_chmod,
+ .mknod = v9fs_synth_mknod,
+ .mkdir = v9fs_synth_mkdir,
+ .fstat = v9fs_synth_fstat,
+ .open2 = v9fs_synth_open2,
+ .symlink = v9fs_synth_symlink,
+ .link = v9fs_synth_link,
+ .truncate = v9fs_synth_truncate,
+ .rename = v9fs_synth_rename,
+ .chown = v9fs_synth_chown,
+ .utimensat = v9fs_synth_utimensat,
+ .remove = v9fs_synth_remove,
+ .fsync = v9fs_synth_fsync,
+ .statfs = v9fs_synth_statfs,
+ .lgetxattr = v9fs_synth_lgetxattr,
+ .llistxattr = v9fs_synth_llistxattr,
+ .lsetxattr = v9fs_synth_lsetxattr,
+ .lremovexattr = v9fs_synth_lremovexattr,
+ .name_to_path = v9fs_synth_name_to_path,
+ .renameat = v9fs_synth_renameat,
+ .unlinkat = v9fs_synth_unlinkat,
+};
diff --git a/src/hw/9pfs/virtio-9p-synth.h b/src/hw/9pfs/virtio-9p-synth.h
new file mode 100644
index 0000000..ab05a8e
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-synth.h
@@ -0,0 +1,54 @@
+/*
+ * Virtio 9p
+ *
+ * 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.
+ *
+ */
+#ifndef HW_9PFS_VIRTIO9P_SYNTH_H
+#define HW_9PFS_VIRTIO9P_SYNTH_H 1
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <limits.h>
+
+typedef struct V9fsSynthNode V9fsSynthNode;
+typedef ssize_t (*v9fs_synth_read)(void *buf, int len, off_t offset,
+ void *arg);
+typedef ssize_t (*v9fs_synth_write)(void *buf, int len, off_t offset,
+ void *arg);
+typedef struct V9fsSynthNodeAttr {
+ int mode;
+ int inode;
+ int nlink;
+ v9fs_synth_read read;
+ v9fs_synth_write write;
+} V9fsSynthNodeAttr;
+
+struct V9fsSynthNode {
+ QLIST_HEAD(, V9fsSynthNode) child;
+ QLIST_ENTRY(V9fsSynthNode) sibling;
+ char name[NAME_MAX];
+ V9fsSynthNodeAttr *attr;
+ V9fsSynthNodeAttr actual_attr;
+ void *private;
+ int open_count;
+};
+
+typedef struct V9fsSynthOpenState {
+ off_t offset;
+ V9fsSynthNode *node;
+} V9fsSynthOpenState;
+
+extern int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
+ const char *name, V9fsSynthNode **result);
+extern int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
+ const char *name, v9fs_synth_read read,
+ v9fs_synth_write write, void *arg);
+
+#endif
diff --git a/src/hw/9pfs/virtio-9p-xattr-user.c b/src/hw/9pfs/virtio-9p-xattr-user.c
new file mode 100644
index 0000000..46133e0
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-xattr-user.c
@@ -0,0 +1,128 @@
+/*
+ * Virtio 9p user. xattr callback
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * 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 <sys/types.h>
+#include "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "fsdev/file-op-9p.h"
+#include "virtio-9p-xattr.h"
+
+
+static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size)
+{
+ char *buffer;
+ ssize_t ret;
+
+ if (strncmp(name, "user.virtfs.", 12) == 0) {
+ /*
+ * Don't allow fetch of user.virtfs namesapce
+ * in case of mapped security
+ */
+ errno = ENOATTR;
+ return -1;
+ }
+ buffer = rpath(ctx, path);
+ ret = lgetxattr(buffer, name, value, size);
+ g_free(buffer);
+ return ret;
+}
+
+static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
+ char *name, void *value, size_t size)
+{
+ int name_size = strlen(name) + 1;
+ if (strncmp(name, "user.virtfs.", 12) == 0) {
+
+ /* check if it is a mapped posix acl */
+ if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
+ /* adjust the name and size */
+ name += 12;
+ name_size -= 12;
+ } else {
+ /*
+ * Don't allow fetch of user.virtfs namesapce
+ * in case of mapped security
+ */
+ return 0;
+ }
+ }
+ if (!value) {
+ return name_size;
+ }
+
+ if (size < name_size) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ /* name_size includes the trailing NUL. */
+ memcpy(value, name, name_size);
+ return name_size;
+}
+
+static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags)
+{
+ char *buffer;
+ int ret;
+
+ if (strncmp(name, "user.virtfs.", 12) == 0) {
+ /*
+ * Don't allow fetch of user.virtfs namesapce
+ * in case of mapped security
+ */
+ errno = EACCES;
+ return -1;
+ }
+ buffer = rpath(ctx, path);
+ ret = lsetxattr(buffer, name, value, size, flags);
+ g_free(buffer);
+ return ret;
+}
+
+static int mp_user_removexattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ char *buffer;
+ int ret;
+
+ if (strncmp(name, "user.virtfs.", 12) == 0) {
+ /*
+ * Don't allow fetch of user.virtfs namesapce
+ * in case of mapped security
+ */
+ errno = EACCES;
+ return -1;
+ }
+ buffer = rpath(ctx, path);
+ ret = lremovexattr(buffer, name);
+ g_free(buffer);
+ return ret;
+}
+
+XattrOperations mapped_user_xattr = {
+ .name = "user.",
+ .getxattr = mp_user_getxattr,
+ .setxattr = mp_user_setxattr,
+ .listxattr = mp_user_listxattr,
+ .removexattr = mp_user_removexattr,
+};
+
+XattrOperations passthrough_user_xattr = {
+ .name = "user.",
+ .getxattr = pt_getxattr,
+ .setxattr = pt_setxattr,
+ .listxattr = pt_listxattr,
+ .removexattr = pt_removexattr,
+};
diff --git a/src/hw/9pfs/virtio-9p-xattr.c b/src/hw/9pfs/virtio-9p-xattr.c
new file mode 100644
index 0000000..0718388
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-xattr.c
@@ -0,0 +1,164 @@
+/*
+ * Virtio 9p xattr callback
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * 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 "hw/virtio/virtio.h"
+#include "virtio-9p.h"
+#include "fsdev/file-op-9p.h"
+#include "virtio-9p-xattr.h"
+
+
+static XattrOperations *get_xattr_operations(XattrOperations **h,
+ const char *name)
+{
+ XattrOperations *xops;
+ for (xops = *(h)++; xops != NULL; xops = *(h)++) {
+ if (!strncmp(name, xops->name, strlen(xops->name))) {
+ return xops;
+ }
+ }
+ return NULL;
+}
+
+ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size)
+{
+ XattrOperations *xops = get_xattr_operations(ctx->xops, name);
+ if (xops) {
+ return xops->getxattr(ctx, path, name, value, size);
+ }
+ errno = EOPNOTSUPP;
+ return -1;
+}
+
+ssize_t pt_listxattr(FsContext *ctx, const char *path,
+ char *name, void *value, size_t size)
+{
+ int name_size = strlen(name) + 1;
+ if (!value) {
+ return name_size;
+ }
+
+ if (size < name_size) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ /* no need for strncpy: name_size is strlen(name)+1 */
+ memcpy(value, name, name_size);
+ return name_size;
+}
+
+
+/*
+ * Get the list and pass to each layer to find out whether
+ * to send the data or not
+ */
+ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
+ void *value, size_t vsize)
+{
+ ssize_t size = 0;
+ char *buffer;
+ void *ovalue = value;
+ XattrOperations *xops;
+ char *orig_value, *orig_value_start;
+ ssize_t xattr_len, parsed_len = 0, attr_len;
+
+ /* Get the actual len */
+ buffer = rpath(ctx, path);
+ xattr_len = llistxattr(buffer, value, 0);
+ if (xattr_len <= 0) {
+ g_free(buffer);
+ return xattr_len;
+ }
+
+ /* Now fetch the xattr and find the actual size */
+ orig_value = g_malloc(xattr_len);
+ xattr_len = llistxattr(buffer, orig_value, xattr_len);
+ g_free(buffer);
+
+ /* store the orig pointer */
+ orig_value_start = orig_value;
+ while (xattr_len > parsed_len) {
+ xops = get_xattr_operations(ctx->xops, orig_value);
+ if (!xops) {
+ goto next_entry;
+ }
+
+ if (!value) {
+ size += xops->listxattr(ctx, path, orig_value, value, vsize);
+ } else {
+ size = xops->listxattr(ctx, path, orig_value, value, vsize);
+ if (size < 0) {
+ goto err_out;
+ }
+ value += size;
+ vsize -= size;
+ }
+next_entry:
+ /* Got the next entry */
+ attr_len = strlen(orig_value) + 1;
+ parsed_len += attr_len;
+ orig_value += attr_len;
+ }
+ if (value) {
+ size = value - ovalue;
+ }
+
+err_out:
+ g_free(orig_value_start);
+ return size;
+}
+
+int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags)
+{
+ XattrOperations *xops = get_xattr_operations(ctx->xops, name);
+ if (xops) {
+ return xops->setxattr(ctx, path, name, value, size, flags);
+ }
+ errno = EOPNOTSUPP;
+ return -1;
+
+}
+
+int v9fs_remove_xattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ XattrOperations *xops = get_xattr_operations(ctx->xops, name);
+ if (xops) {
+ return xops->removexattr(ctx, path, name);
+ }
+ errno = EOPNOTSUPP;
+ return -1;
+
+}
+
+XattrOperations *mapped_xattr_ops[] = {
+ &mapped_user_xattr,
+ &mapped_pacl_xattr,
+ &mapped_dacl_xattr,
+ NULL,
+};
+
+XattrOperations *passthrough_xattr_ops[] = {
+ &passthrough_user_xattr,
+ &passthrough_acl_xattr,
+ NULL,
+};
+
+/* for .user none model should be same as passthrough */
+XattrOperations *none_xattr_ops[] = {
+ &passthrough_user_xattr,
+ &none_acl_xattr,
+ NULL,
+};
diff --git a/src/hw/9pfs/virtio-9p-xattr.h b/src/hw/9pfs/virtio-9p-xattr.h
new file mode 100644
index 0000000..327b32b
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p-xattr.h
@@ -0,0 +1,120 @@
+/*
+ * Virtio 9p
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * 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.
+ *
+ */
+#ifndef _QEMU_VIRTIO_9P_XATTR_H
+#define _QEMU_VIRTIO_9P_XATTR_H
+
+#include "qemu/xattr.h"
+
+typedef struct xattr_operations
+{
+ const char *name;
+ ssize_t (*getxattr)(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size);
+ ssize_t (*listxattr)(FsContext *ctx, const char *path,
+ char *name, void *value, size_t size);
+ int (*setxattr)(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags);
+ int (*removexattr)(FsContext *ctx,
+ const char *path, const char *name);
+} XattrOperations;
+
+
+extern XattrOperations mapped_user_xattr;
+extern XattrOperations passthrough_user_xattr;
+
+extern XattrOperations mapped_pacl_xattr;
+extern XattrOperations mapped_dacl_xattr;
+extern XattrOperations passthrough_acl_xattr;
+extern XattrOperations none_acl_xattr;
+
+extern XattrOperations *mapped_xattr_ops[];
+extern XattrOperations *passthrough_xattr_ops[];
+extern XattrOperations *none_xattr_ops[];
+
+ssize_t v9fs_get_xattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size);
+ssize_t v9fs_list_xattr(FsContext *ctx, const char *path, void *value,
+ size_t vsize);
+int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
+ void *value, size_t size, int flags);
+int v9fs_remove_xattr(FsContext *ctx, const char *path, const char *name);
+ssize_t pt_listxattr(FsContext *ctx, const char *path, char *name, void *value,
+ size_t size);
+
+static inline ssize_t pt_getxattr(FsContext *ctx, const char *path,
+ const char *name, void *value, size_t size)
+{
+ char *buffer;
+ ssize_t ret;
+
+ buffer = rpath(ctx, path);
+ ret = lgetxattr(buffer, name, value, size);
+ g_free(buffer);
+ return ret;
+}
+
+static inline int pt_setxattr(FsContext *ctx, const char *path,
+ const char *name, void *value,
+ size_t size, int flags)
+{
+ char *buffer;
+ int ret;
+
+ buffer = rpath(ctx, path);
+ ret = lsetxattr(buffer, name, value, size, flags);
+ g_free(buffer);
+ return ret;
+}
+
+static inline int pt_removexattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ char *buffer;
+ int ret;
+
+ buffer = rpath(ctx, path);
+ ret = lremovexattr(path, name);
+ g_free(buffer);
+ return ret;
+}
+
+static inline ssize_t notsup_getxattr(FsContext *ctx, const char *path,
+ const char *name, void *value,
+ size_t size)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static inline int notsup_setxattr(FsContext *ctx, const char *path,
+ const char *name, void *value,
+ size_t size, int flags)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+static inline ssize_t notsup_listxattr(FsContext *ctx, const char *path,
+ char *name, void *value, size_t size)
+{
+ return 0;
+}
+
+static inline int notsup_removexattr(FsContext *ctx,
+ const char *path, const char *name)
+{
+ errno = ENOTSUP;
+ return -1;
+}
+
+#endif
diff --git a/src/hw/9pfs/virtio-9p.c b/src/hw/9pfs/virtio-9p.c
new file mode 100644
index 0000000..f972731
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p.c
@@ -0,0 +1,3300 @@
+/*
+ * Virtio 9p backend
+ *
+ * Copyright IBM, Corp. 2010
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.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 "hw/virtio/virtio.h"
+#include "hw/i386/pc.h"
+#include "qemu/error-report.h"
+#include "qemu/iov.h"
+#include "qemu/sockets.h"
+#include "virtio-9p.h"
+#include "fsdev/qemu-fsdev.h"
+#include "virtio-9p-xattr.h"
+#include "virtio-9p-coth.h"
+#include "trace.h"
+#include "migration/migration.h"
+
+int open_fd_hw;
+int total_open_fd;
+static int open_fd_rc;
+
+enum {
+ Oread = 0x00,
+ Owrite = 0x01,
+ Ordwr = 0x02,
+ Oexec = 0x03,
+ Oexcl = 0x04,
+ Otrunc = 0x10,
+ Orexec = 0x20,
+ Orclose = 0x40,
+ Oappend = 0x80,
+};
+
+static int omode_to_uflags(int8_t mode)
+{
+ int ret = 0;
+
+ switch (mode & 3) {
+ case Oread:
+ ret = O_RDONLY;
+ break;
+ case Ordwr:
+ ret = O_RDWR;
+ break;
+ case Owrite:
+ ret = O_WRONLY;
+ break;
+ case Oexec:
+ ret = O_RDONLY;
+ break;
+ }
+
+ if (mode & Otrunc) {
+ ret |= O_TRUNC;
+ }
+
+ if (mode & Oappend) {
+ ret |= O_APPEND;
+ }
+
+ if (mode & Oexcl) {
+ ret |= O_EXCL;
+ }
+
+ return ret;
+}
+
+struct dotl_openflag_map {
+ int dotl_flag;
+ int open_flag;
+};
+
+static int dotl_to_open_flags(int flags)
+{
+ int i;
+ /*
+ * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
+ * and P9_DOTL_NOACCESS
+ */
+ int oflags = flags & O_ACCMODE;
+
+ struct dotl_openflag_map dotl_oflag_map[] = {
+ { P9_DOTL_CREATE, O_CREAT },
+ { P9_DOTL_EXCL, O_EXCL },
+ { P9_DOTL_NOCTTY , O_NOCTTY },
+ { P9_DOTL_TRUNC, O_TRUNC },
+ { P9_DOTL_APPEND, O_APPEND },
+ { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
+ { P9_DOTL_DSYNC, O_DSYNC },
+ { P9_DOTL_FASYNC, FASYNC },
+ { P9_DOTL_DIRECT, O_DIRECT },
+ { P9_DOTL_LARGEFILE, O_LARGEFILE },
+ { P9_DOTL_DIRECTORY, O_DIRECTORY },
+ { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
+ { P9_DOTL_NOATIME, O_NOATIME },
+ { P9_DOTL_SYNC, O_SYNC },
+ };
+
+ for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
+ if (flags & dotl_oflag_map[i].dotl_flag) {
+ oflags |= dotl_oflag_map[i].open_flag;
+ }
+ }
+
+ return oflags;
+}
+
+void cred_init(FsCred *credp)
+{
+ credp->fc_uid = -1;
+ credp->fc_gid = -1;
+ credp->fc_mode = -1;
+ credp->fc_rdev = -1;
+}
+
+static int get_dotl_openflags(V9fsState *s, int oflags)
+{
+ int flags;
+ /*
+ * Filter the client open flags
+ */
+ flags = dotl_to_open_flags(oflags);
+ flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
+ /*
+ * Ignore direct disk access hint until the server supports it.
+ */
+ flags &= ~O_DIRECT;
+ return flags;
+}
+
+void v9fs_path_init(V9fsPath *path)
+{
+ path->data = NULL;
+ path->size = 0;
+}
+
+void v9fs_path_free(V9fsPath *path)
+{
+ g_free(path->data);
+ path->data = NULL;
+ path->size = 0;
+}
+
+void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
+{
+ v9fs_path_free(lhs);
+ lhs->data = g_malloc(rhs->size);
+ memcpy(lhs->data, rhs->data, rhs->size);
+ lhs->size = rhs->size;
+}
+
+int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
+ const char *name, V9fsPath *path)
+{
+ int err;
+ err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
+ if (err < 0) {
+ err = -errno;
+ }
+ return err;
+}
+
+/*
+ * Return TRUE if s1 is an ancestor of s2.
+ *
+ * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
+ * As a special case, We treat s1 as ancestor of s2 if they are same!
+ */
+static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
+{
+ if (!strncmp(s1->data, s2->data, s1->size - 1)) {
+ if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static size_t v9fs_string_size(V9fsString *str)
+{
+ return str->size;
+}
+
+/*
+ * returns 0 if fid got re-opened, 1 if not, < 0 on error */
+static int v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
+{
+ int err = 1;
+ if (f->fid_type == P9_FID_FILE) {
+ if (f->fs.fd == -1) {
+ do {
+ err = v9fs_co_open(pdu, f, f->open_flags);
+ } while (err == -EINTR && !pdu->cancelled);
+ }
+ } else if (f->fid_type == P9_FID_DIR) {
+ if (f->fs.dir == NULL) {
+ do {
+ err = v9fs_co_opendir(pdu, f);
+ } while (err == -EINTR && !pdu->cancelled);
+ }
+ }
+ return err;
+}
+
+static V9fsFidState *get_fid(V9fsPDU *pdu, int32_t fid)
+{
+ int err;
+ V9fsFidState *f;
+ V9fsState *s = pdu->s;
+
+ for (f = s->fid_list; f; f = f->next) {
+ BUG_ON(f->clunked);
+ if (f->fid == fid) {
+ /*
+ * Update the fid ref upfront so that
+ * we don't get reclaimed when we yield
+ * in open later.
+ */
+ f->ref++;
+ /*
+ * check whether we need to reopen the
+ * file. We might have closed the fd
+ * while trying to free up some file
+ * descriptors.
+ */
+ err = v9fs_reopen_fid(pdu, f);
+ if (err < 0) {
+ f->ref--;
+ return NULL;
+ }
+ /*
+ * Mark the fid as referenced so that the LRU
+ * reclaim won't close the file descriptor
+ */
+ f->flags |= FID_REFERENCED;
+ return f;
+ }
+ }
+ return NULL;
+}
+
+static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
+{
+ V9fsFidState *f;
+
+ for (f = s->fid_list; f; f = f->next) {
+ /* If fid is already there return NULL */
+ BUG_ON(f->clunked);
+ if (f->fid == fid) {
+ return NULL;
+ }
+ }
+ f = g_malloc0(sizeof(V9fsFidState));
+ f->fid = fid;
+ f->fid_type = P9_FID_NONE;
+ f->ref = 1;
+ /*
+ * Mark the fid as referenced so that the LRU
+ * reclaim won't close the file descriptor
+ */
+ f->flags |= FID_REFERENCED;
+ f->next = s->fid_list;
+ s->fid_list = f;
+
+ return f;
+}
+
+static int v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ int retval = 0;
+
+ if (fidp->fs.xattr.copied_len == -1) {
+ /* getxattr/listxattr fid */
+ goto free_value;
+ }
+ /*
+ * if this is fid for setxattr. clunk should
+ * result in setxattr localcall
+ */
+ if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
+ /* clunk after partial write */
+ retval = -EINVAL;
+ goto free_out;
+ }
+ if (fidp->fs.xattr.len) {
+ retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
+ fidp->fs.xattr.value,
+ fidp->fs.xattr.len,
+ fidp->fs.xattr.flags);
+ } else {
+ retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
+ }
+free_out:
+ v9fs_string_free(&fidp->fs.xattr.name);
+free_value:
+ g_free(fidp->fs.xattr.value);
+ return retval;
+}
+
+static int free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ int retval = 0;
+
+ if (fidp->fid_type == P9_FID_FILE) {
+ /* If we reclaimed the fd no need to close */
+ if (fidp->fs.fd != -1) {
+ retval = v9fs_co_close(pdu, &fidp->fs);
+ }
+ } else if (fidp->fid_type == P9_FID_DIR) {
+ if (fidp->fs.dir != NULL) {
+ retval = v9fs_co_closedir(pdu, &fidp->fs);
+ }
+ } else if (fidp->fid_type == P9_FID_XATTR) {
+ retval = v9fs_xattr_fid_clunk(pdu, fidp);
+ }
+ v9fs_path_free(&fidp->path);
+ g_free(fidp);
+ return retval;
+}
+
+static int put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
+{
+ BUG_ON(!fidp->ref);
+ fidp->ref--;
+ /*
+ * Don't free the fid if it is in reclaim list
+ */
+ if (!fidp->ref && fidp->clunked) {
+ if (fidp->fid == pdu->s->root_fid) {
+ /*
+ * if the clunked fid is root fid then we
+ * have unmounted the fs on the client side.
+ * delete the migration blocker. Ideally, this
+ * should be hooked to transport close notification
+ */
+ if (pdu->s->migration_blocker) {
+ migrate_del_blocker(pdu->s->migration_blocker);
+ error_free(pdu->s->migration_blocker);
+ pdu->s->migration_blocker = NULL;
+ }
+ }
+ return free_fid(pdu, fidp);
+ }
+ return 0;
+}
+
+static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
+{
+ V9fsFidState **fidpp, *fidp;
+
+ for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
+ if ((*fidpp)->fid == fid) {
+ break;
+ }
+ }
+ if (*fidpp == NULL) {
+ return NULL;
+ }
+ fidp = *fidpp;
+ *fidpp = fidp->next;
+ fidp->clunked = 1;
+ return fidp;
+}
+
+void v9fs_reclaim_fd(V9fsPDU *pdu)
+{
+ int reclaim_count = 0;
+ V9fsState *s = pdu->s;
+ V9fsFidState *f, *reclaim_list = NULL;
+
+ for (f = s->fid_list; f; f = f->next) {
+ /*
+ * Unlink fids cannot be reclaimed. Check
+ * for them and skip them. Also skip fids
+ * currently being operated on.
+ */
+ if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
+ continue;
+ }
+ /*
+ * if it is a recently referenced fid
+ * we leave the fid untouched and clear the
+ * reference bit. We come back to it later
+ * in the next iteration. (a simple LRU without
+ * moving list elements around)
+ */
+ if (f->flags & FID_REFERENCED) {
+ f->flags &= ~FID_REFERENCED;
+ continue;
+ }
+ /*
+ * Add fids to reclaim list.
+ */
+ if (f->fid_type == P9_FID_FILE) {
+ if (f->fs.fd != -1) {
+ /*
+ * Up the reference count so that
+ * a clunk request won't free this fid
+ */
+ f->ref++;
+ f->rclm_lst = reclaim_list;
+ reclaim_list = f;
+ f->fs_reclaim.fd = f->fs.fd;
+ f->fs.fd = -1;
+ reclaim_count++;
+ }
+ } else if (f->fid_type == P9_FID_DIR) {
+ if (f->fs.dir != NULL) {
+ /*
+ * Up the reference count so that
+ * a clunk request won't free this fid
+ */
+ f->ref++;
+ f->rclm_lst = reclaim_list;
+ reclaim_list = f;
+ f->fs_reclaim.dir = f->fs.dir;
+ f->fs.dir = NULL;
+ reclaim_count++;
+ }
+ }
+ if (reclaim_count >= open_fd_rc) {
+ break;
+ }
+ }
+ /*
+ * Now close the fid in reclaim list. Free them if they
+ * are already clunked.
+ */
+ while (reclaim_list) {
+ f = reclaim_list;
+ reclaim_list = f->rclm_lst;
+ if (f->fid_type == P9_FID_FILE) {
+ v9fs_co_close(pdu, &f->fs_reclaim);
+ } else if (f->fid_type == P9_FID_DIR) {
+ v9fs_co_closedir(pdu, &f->fs_reclaim);
+ }
+ f->rclm_lst = NULL;
+ /*
+ * Now drop the fid reference, free it
+ * if clunked.
+ */
+ put_fid(pdu, f);
+ }
+}
+
+static int v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
+{
+ int err;
+ V9fsState *s = pdu->s;
+ V9fsFidState *fidp, head_fid;
+
+ head_fid.next = s->fid_list;
+ for (fidp = s->fid_list; fidp; fidp = fidp->next) {
+ if (fidp->path.size != path->size) {
+ continue;
+ }
+ if (!memcmp(fidp->path.data, path->data, path->size)) {
+ /* Mark the fid non reclaimable. */
+ fidp->flags |= FID_NON_RECLAIMABLE;
+
+ /* reopen the file/dir if already closed */
+ err = v9fs_reopen_fid(pdu, fidp);
+ if (err < 0) {
+ return -1;
+ }
+ /*
+ * Go back to head of fid list because
+ * the list could have got updated when
+ * switched to the worker thread
+ */
+ if (err == 0) {
+ fidp = &head_fid;
+ }
+ }
+ }
+ return 0;
+}
+
+static void virtfs_reset(V9fsPDU *pdu)
+{
+ V9fsState *s = pdu->s;
+ V9fsFidState *fidp = NULL;
+
+ /* Free all fids */
+ while (s->fid_list) {
+ fidp = s->fid_list;
+ s->fid_list = fidp->next;
+
+ if (fidp->ref) {
+ fidp->clunked = 1;
+ } else {
+ free_fid(pdu, fidp);
+ }
+ }
+ if (fidp) {
+ /* One or more unclunked fids found... */
+ error_report("9pfs:%s: One or more uncluncked fids "
+ "found during reset", __func__);
+ }
+}
+
+#define P9_QID_TYPE_DIR 0x80
+#define P9_QID_TYPE_SYMLINK 0x02
+
+#define P9_STAT_MODE_DIR 0x80000000
+#define P9_STAT_MODE_APPEND 0x40000000
+#define P9_STAT_MODE_EXCL 0x20000000
+#define P9_STAT_MODE_MOUNT 0x10000000
+#define P9_STAT_MODE_AUTH 0x08000000
+#define P9_STAT_MODE_TMP 0x04000000
+#define P9_STAT_MODE_SYMLINK 0x02000000
+#define P9_STAT_MODE_LINK 0x01000000
+#define P9_STAT_MODE_DEVICE 0x00800000
+#define P9_STAT_MODE_NAMED_PIPE 0x00200000
+#define P9_STAT_MODE_SOCKET 0x00100000
+#define P9_STAT_MODE_SETUID 0x00080000
+#define P9_STAT_MODE_SETGID 0x00040000
+#define P9_STAT_MODE_SETVTX 0x00010000
+
+#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
+ P9_STAT_MODE_SYMLINK | \
+ P9_STAT_MODE_LINK | \
+ P9_STAT_MODE_DEVICE | \
+ P9_STAT_MODE_NAMED_PIPE | \
+ P9_STAT_MODE_SOCKET)
+
+/* This is the algorithm from ufs in spfs */
+static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
+{
+ size_t size;
+
+ memset(&qidp->path, 0, sizeof(qidp->path));
+ size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
+ memcpy(&qidp->path, &stbuf->st_ino, size);
+ qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
+ qidp->type = 0;
+ if (S_ISDIR(stbuf->st_mode)) {
+ qidp->type |= P9_QID_TYPE_DIR;
+ }
+ if (S_ISLNK(stbuf->st_mode)) {
+ qidp->type |= P9_QID_TYPE_SYMLINK;
+ }
+}
+
+static int fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, V9fsQID *qidp)
+{
+ struct stat stbuf;
+ int err;
+
+ err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (err < 0) {
+ return err;
+ }
+ stat_to_qid(&stbuf, qidp);
+ return 0;
+}
+
+static V9fsPDU *alloc_pdu(V9fsState *s)
+{
+ V9fsPDU *pdu = NULL;
+
+ if (!QLIST_EMPTY(&s->free_list)) {
+ pdu = QLIST_FIRST(&s->free_list);
+ QLIST_REMOVE(pdu, next);
+ QLIST_INSERT_HEAD(&s->active_list, pdu, next);
+ }
+ return pdu;
+}
+
+static void free_pdu(V9fsState *s, V9fsPDU *pdu)
+{
+ if (pdu) {
+ /*
+ * Cancelled pdu are added back to the freelist
+ * by flush request .
+ */
+ if (!pdu->cancelled) {
+ QLIST_REMOVE(pdu, next);
+ QLIST_INSERT_HEAD(&s->free_list, pdu, next);
+ }
+ }
+}
+
+/*
+ * We don't do error checking for pdu_marshal/unmarshal here
+ * because we always expect to have enough space to encode
+ * error details
+ */
+static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
+{
+ int8_t id = pdu->id + 1; /* Response */
+
+ if (len < 0) {
+ int err = -len;
+ len = 7;
+
+ if (s->proto_version != V9FS_PROTO_2000L) {
+ V9fsString str;
+
+ str.data = strerror(err);
+ str.size = strlen(str.data);
+
+ len += pdu_marshal(pdu, len, "s", &str);
+ id = P9_RERROR;
+ }
+
+ len += pdu_marshal(pdu, len, "d", err);
+
+ if (s->proto_version == V9FS_PROTO_2000L) {
+ id = P9_RLERROR;
+ }
+ trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
+ }
+
+ /* fill out the header */
+ pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
+
+ /* keep these in sync */
+ pdu->size = len;
+ pdu->id = id;
+
+ /* push onto queue and notify */
+ virtqueue_push(s->vq, &pdu->elem, len);
+
+ /* FIXME: we should batch these completions */
+ virtio_notify(VIRTIO_DEVICE(s), s->vq);
+
+ /* Now wakeup anybody waiting in flush for this request */
+ qemu_co_queue_next(&pdu->complete);
+
+ free_pdu(s, pdu);
+}
+
+static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
+{
+ mode_t ret;
+
+ ret = mode & 0777;
+ if (mode & P9_STAT_MODE_DIR) {
+ ret |= S_IFDIR;
+ }
+
+ if (mode & P9_STAT_MODE_SYMLINK) {
+ ret |= S_IFLNK;
+ }
+ if (mode & P9_STAT_MODE_SOCKET) {
+ ret |= S_IFSOCK;
+ }
+ if (mode & P9_STAT_MODE_NAMED_PIPE) {
+ ret |= S_IFIFO;
+ }
+ if (mode & P9_STAT_MODE_DEVICE) {
+ if (extension->size && extension->data[0] == 'c') {
+ ret |= S_IFCHR;
+ } else {
+ ret |= S_IFBLK;
+ }
+ }
+
+ if (!(ret&~0777)) {
+ ret |= S_IFREG;
+ }
+
+ if (mode & P9_STAT_MODE_SETUID) {
+ ret |= S_ISUID;
+ }
+ if (mode & P9_STAT_MODE_SETGID) {
+ ret |= S_ISGID;
+ }
+ if (mode & P9_STAT_MODE_SETVTX) {
+ ret |= S_ISVTX;
+ }
+
+ return ret;
+}
+
+static int donttouch_stat(V9fsStat *stat)
+{
+ if (stat->type == -1 &&
+ stat->dev == -1 &&
+ stat->qid.type == -1 &&
+ stat->qid.version == -1 &&
+ stat->qid.path == -1 &&
+ stat->mode == -1 &&
+ stat->atime == -1 &&
+ stat->mtime == -1 &&
+ stat->length == -1 &&
+ !stat->name.size &&
+ !stat->uid.size &&
+ !stat->gid.size &&
+ !stat->muid.size &&
+ stat->n_uid == -1 &&
+ stat->n_gid == -1 &&
+ stat->n_muid == -1) {
+ return 1;
+ }
+
+ return 0;
+}
+
+static void v9fs_stat_init(V9fsStat *stat)
+{
+ v9fs_string_init(&stat->name);
+ v9fs_string_init(&stat->uid);
+ v9fs_string_init(&stat->gid);
+ v9fs_string_init(&stat->muid);
+ v9fs_string_init(&stat->extension);
+}
+
+static void v9fs_stat_free(V9fsStat *stat)
+{
+ v9fs_string_free(&stat->name);
+ v9fs_string_free(&stat->uid);
+ v9fs_string_free(&stat->gid);
+ v9fs_string_free(&stat->muid);
+ v9fs_string_free(&stat->extension);
+}
+
+static uint32_t stat_to_v9mode(const struct stat *stbuf)
+{
+ uint32_t mode;
+
+ mode = stbuf->st_mode & 0777;
+ if (S_ISDIR(stbuf->st_mode)) {
+ mode |= P9_STAT_MODE_DIR;
+ }
+
+ if (S_ISLNK(stbuf->st_mode)) {
+ mode |= P9_STAT_MODE_SYMLINK;
+ }
+
+ if (S_ISSOCK(stbuf->st_mode)) {
+ mode |= P9_STAT_MODE_SOCKET;
+ }
+
+ if (S_ISFIFO(stbuf->st_mode)) {
+ mode |= P9_STAT_MODE_NAMED_PIPE;
+ }
+
+ if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
+ mode |= P9_STAT_MODE_DEVICE;
+ }
+
+ if (stbuf->st_mode & S_ISUID) {
+ mode |= P9_STAT_MODE_SETUID;
+ }
+
+ if (stbuf->st_mode & S_ISGID) {
+ mode |= P9_STAT_MODE_SETGID;
+ }
+
+ if (stbuf->st_mode & S_ISVTX) {
+ mode |= P9_STAT_MODE_SETVTX;
+ }
+
+ return mode;
+}
+
+static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
+ const struct stat *stbuf,
+ V9fsStat *v9stat)
+{
+ int err;
+ const char *str;
+
+ memset(v9stat, 0, sizeof(*v9stat));
+
+ stat_to_qid(stbuf, &v9stat->qid);
+ v9stat->mode = stat_to_v9mode(stbuf);
+ v9stat->atime = stbuf->st_atime;
+ v9stat->mtime = stbuf->st_mtime;
+ v9stat->length = stbuf->st_size;
+
+ v9fs_string_null(&v9stat->uid);
+ v9fs_string_null(&v9stat->gid);
+ v9fs_string_null(&v9stat->muid);
+
+ v9stat->n_uid = stbuf->st_uid;
+ v9stat->n_gid = stbuf->st_gid;
+ v9stat->n_muid = 0;
+
+ v9fs_string_null(&v9stat->extension);
+
+ if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
+ err = v9fs_co_readlink(pdu, name, &v9stat->extension);
+ if (err < 0) {
+ return err;
+ }
+ } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
+ v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
+ S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
+ major(stbuf->st_rdev), minor(stbuf->st_rdev));
+ } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
+ v9fs_string_sprintf(&v9stat->extension, "%s %lu",
+ "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
+ }
+
+ str = strrchr(name->data, '/');
+ if (str) {
+ str += 1;
+ } else {
+ str = name->data;
+ }
+
+ v9fs_string_sprintf(&v9stat->name, "%s", str);
+
+ v9stat->size = 61 +
+ v9fs_string_size(&v9stat->name) +
+ v9fs_string_size(&v9stat->uid) +
+ v9fs_string_size(&v9stat->gid) +
+ v9fs_string_size(&v9stat->muid) +
+ v9fs_string_size(&v9stat->extension);
+ return 0;
+}
+
+#define P9_STATS_MODE 0x00000001ULL
+#define P9_STATS_NLINK 0x00000002ULL
+#define P9_STATS_UID 0x00000004ULL
+#define P9_STATS_GID 0x00000008ULL
+#define P9_STATS_RDEV 0x00000010ULL
+#define P9_STATS_ATIME 0x00000020ULL
+#define P9_STATS_MTIME 0x00000040ULL
+#define P9_STATS_CTIME 0x00000080ULL
+#define P9_STATS_INO 0x00000100ULL
+#define P9_STATS_SIZE 0x00000200ULL
+#define P9_STATS_BLOCKS 0x00000400ULL
+
+#define P9_STATS_BTIME 0x00000800ULL
+#define P9_STATS_GEN 0x00001000ULL
+#define P9_STATS_DATA_VERSION 0x00002000ULL
+
+#define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
+#define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
+
+
+static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
+ V9fsStatDotl *v9lstat)
+{
+ memset(v9lstat, 0, sizeof(*v9lstat));
+
+ v9lstat->st_mode = stbuf->st_mode;
+ v9lstat->st_nlink = stbuf->st_nlink;
+ v9lstat->st_uid = stbuf->st_uid;
+ v9lstat->st_gid = stbuf->st_gid;
+ v9lstat->st_rdev = stbuf->st_rdev;
+ v9lstat->st_size = stbuf->st_size;
+ v9lstat->st_blksize = stbuf->st_blksize;
+ v9lstat->st_blocks = stbuf->st_blocks;
+ v9lstat->st_atime_sec = stbuf->st_atime;
+ v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
+ v9lstat->st_mtime_sec = stbuf->st_mtime;
+ v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
+ v9lstat->st_ctime_sec = stbuf->st_ctime;
+ v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
+ /* Currently we only support BASIC fields in stat */
+ v9lstat->st_result_mask = P9_STATS_BASIC;
+
+ stat_to_qid(stbuf, &v9lstat->qid);
+}
+
+static void print_sg(struct iovec *sg, int cnt)
+{
+ int i;
+
+ printf("sg[%d]: {", cnt);
+ for (i = 0; i < cnt; i++) {
+ if (i) {
+ printf(", ");
+ }
+ printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
+ }
+ printf("}\n");
+}
+
+/* Will call this only for path name based fid */
+static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
+{
+ V9fsPath str;
+ v9fs_path_init(&str);
+ v9fs_path_copy(&str, dst);
+ v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
+ v9fs_path_free(&str);
+ /* +1 to include terminating NULL */
+ dst->size++;
+}
+
+static inline bool is_ro_export(FsContext *ctx)
+{
+ return ctx->export_flags & V9FS_RDONLY;
+}
+
+static void v9fs_version(void *opaque)
+{
+ ssize_t err;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+ V9fsString version;
+ size_t offset = 7;
+
+ v9fs_string_init(&version);
+ err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
+ if (err < 0) {
+ offset = err;
+ goto out;
+ }
+ trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
+
+ virtfs_reset(pdu);
+
+ if (!strcmp(version.data, "9P2000.u")) {
+ s->proto_version = V9FS_PROTO_2000U;
+ } else if (!strcmp(version.data, "9P2000.L")) {
+ s->proto_version = V9FS_PROTO_2000L;
+ } else {
+ v9fs_string_sprintf(&version, "unknown");
+ }
+
+ err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
+ if (err < 0) {
+ offset = err;
+ goto out;
+ }
+ offset += err;
+ trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
+out:
+ complete_pdu(s, pdu, offset);
+ v9fs_string_free(&version);
+}
+
+static void v9fs_attach(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+ int32_t fid, afid, n_uname;
+ V9fsString uname, aname;
+ V9fsFidState *fidp;
+ size_t offset = 7;
+ V9fsQID qid;
+ ssize_t err;
+
+ v9fs_string_init(&uname);
+ v9fs_string_init(&aname);
+ err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
+ &afid, &uname, &aname, &n_uname);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
+
+ fidp = alloc_fid(s, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ fidp->uid = n_uname;
+ err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
+ if (err < 0) {
+ err = -EINVAL;
+ clunk_fid(s, fid);
+ goto out;
+ }
+ err = fid_to_qid(pdu, fidp, &qid);
+ if (err < 0) {
+ err = -EINVAL;
+ clunk_fid(s, fid);
+ goto out;
+ }
+ err = pdu_marshal(pdu, offset, "Q", &qid);
+ if (err < 0) {
+ clunk_fid(s, fid);
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_attach_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path);
+ /*
+ * disable migration if we haven't done already.
+ * attach could get called multiple times for the same export.
+ */
+ if (!s->migration_blocker) {
+ s->root_fid = fid;
+ error_setg(&s->migration_blocker,
+ "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
+ s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
+ migrate_add_blocker(s->migration_blocker);
+ }
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&uname);
+ v9fs_string_free(&aname);
+}
+
+static void v9fs_stat(void *opaque)
+{
+ int32_t fid;
+ V9fsStat v9stat;
+ ssize_t err = 0;
+ size_t offset = 7;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "d", &fid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_stat(pdu->tag, pdu->id, fid);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
+ if (err < 0) {
+ goto out;
+ }
+ err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
+ if (err < 0) {
+ v9fs_stat_free(&v9stat);
+ goto out;
+ }
+ trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
+ v9stat.atime, v9stat.mtime, v9stat.length);
+ err += offset;
+ v9fs_stat_free(&v9stat);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static void v9fs_getattr(void *opaque)
+{
+ int32_t fid;
+ size_t offset = 7;
+ ssize_t retval = 0;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ uint64_t request_mask;
+ V9fsStatDotl v9stat_dotl;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
+ if (retval < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ retval = -ENOENT;
+ goto out_nofid;
+ }
+ /*
+ * Currently we only support BASIC fields in stat, so there is no
+ * need to look at request_mask.
+ */
+ retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (retval < 0) {
+ goto out;
+ }
+ stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
+
+ /* fill st_gen if requested and supported by underlying fs */
+ if (request_mask & P9_STATS_GEN) {
+ retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
+ switch (retval) {
+ case 0:
+ /* we have valid st_gen: update result mask */
+ v9stat_dotl.st_result_mask |= P9_STATS_GEN;
+ break;
+ case -EINTR:
+ /* request cancelled, e.g. by Tflush */
+ goto out;
+ default:
+ /* failed to get st_gen: not fatal, ignore */
+ break;
+ }
+ }
+ retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
+ if (retval < 0) {
+ goto out;
+ }
+ retval += offset;
+ trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
+ v9stat_dotl.st_mode, v9stat_dotl.st_uid,
+ v9stat_dotl.st_gid);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, retval);
+}
+
+/* Attribute flags */
+#define P9_ATTR_MODE (1 << 0)
+#define P9_ATTR_UID (1 << 1)
+#define P9_ATTR_GID (1 << 2)
+#define P9_ATTR_SIZE (1 << 3)
+#define P9_ATTR_ATIME (1 << 4)
+#define P9_ATTR_MTIME (1 << 5)
+#define P9_ATTR_CTIME (1 << 6)
+#define P9_ATTR_ATIME_SET (1 << 7)
+#define P9_ATTR_MTIME_SET (1 << 8)
+
+#define P9_ATTR_MASK 127
+
+static void v9fs_setattr(void *opaque)
+{
+ int err = 0;
+ int32_t fid;
+ V9fsFidState *fidp;
+ size_t offset = 7;
+ V9fsIattr v9iattr;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
+ if (err < 0) {
+ goto out_nofid;
+ }
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ if (v9iattr.valid & P9_ATTR_MODE) {
+ err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
+ struct timespec times[2];
+ if (v9iattr.valid & P9_ATTR_ATIME) {
+ if (v9iattr.valid & P9_ATTR_ATIME_SET) {
+ times[0].tv_sec = v9iattr.atime_sec;
+ times[0].tv_nsec = v9iattr.atime_nsec;
+ } else {
+ times[0].tv_nsec = UTIME_NOW;
+ }
+ } else {
+ times[0].tv_nsec = UTIME_OMIT;
+ }
+ if (v9iattr.valid & P9_ATTR_MTIME) {
+ if (v9iattr.valid & P9_ATTR_MTIME_SET) {
+ times[1].tv_sec = v9iattr.mtime_sec;
+ times[1].tv_nsec = v9iattr.mtime_nsec;
+ } else {
+ times[1].tv_nsec = UTIME_NOW;
+ }
+ } else {
+ times[1].tv_nsec = UTIME_OMIT;
+ }
+ err = v9fs_co_utimensat(pdu, &fidp->path, times);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ /*
+ * If the only valid entry in iattr is ctime we can call
+ * chown(-1,-1) to update the ctime of the file
+ */
+ if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
+ ((v9iattr.valid & P9_ATTR_CTIME)
+ && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
+ if (!(v9iattr.valid & P9_ATTR_UID)) {
+ v9iattr.uid = -1;
+ }
+ if (!(v9iattr.valid & P9_ATTR_GID)) {
+ v9iattr.gid = -1;
+ }
+ err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
+ v9iattr.gid);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9iattr.valid & (P9_ATTR_SIZE)) {
+ err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ err = offset;
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
+{
+ int i;
+ ssize_t err;
+ size_t offset = 7;
+
+ err = pdu_marshal(pdu, offset, "w", nwnames);
+ if (err < 0) {
+ return err;
+ }
+ offset += err;
+ for (i = 0; i < nwnames; i++) {
+ err = pdu_marshal(pdu, offset, "Q", &qids[i]);
+ if (err < 0) {
+ return err;
+ }
+ offset += err;
+ }
+ return offset;
+}
+
+static void v9fs_walk(void *opaque)
+{
+ int name_idx;
+ V9fsQID *qids = NULL;
+ int i, err = 0;
+ V9fsPath dpath, path;
+ uint16_t nwnames;
+ struct stat stbuf;
+ size_t offset = 7;
+ int32_t fid, newfid;
+ V9fsString *wnames = NULL;
+ V9fsFidState *fidp;
+ V9fsFidState *newfidp = NULL;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
+ if (err < 0) {
+ complete_pdu(s, pdu, err);
+ return ;
+ }
+ offset += err;
+
+ trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
+
+ if (nwnames && nwnames <= P9_MAXWELEM) {
+ wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
+ qids = g_malloc0(sizeof(qids[0]) * nwnames);
+ for (i = 0; i < nwnames; i++) {
+ err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ offset += err;
+ }
+ } else if (nwnames > P9_MAXWELEM) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ v9fs_path_init(&dpath);
+ v9fs_path_init(&path);
+ /*
+ * Both dpath and path initially poin to fidp.
+ * Needed to handle request with nwnames == 0
+ */
+ v9fs_path_copy(&dpath, &fidp->path);
+ v9fs_path_copy(&path, &fidp->path);
+ for (name_idx = 0; name_idx < nwnames; name_idx++) {
+ err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_lstat(pdu, &path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qids[name_idx]);
+ v9fs_path_copy(&dpath, &path);
+ }
+ if (fid == newfid) {
+ BUG_ON(fidp->fid_type != P9_FID_NONE);
+ v9fs_path_copy(&fidp->path, &path);
+ } else {
+ newfidp = alloc_fid(s, newfid);
+ if (newfidp == NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+ newfidp->uid = fidp->uid;
+ v9fs_path_copy(&newfidp->path, &path);
+ }
+ err = v9fs_walk_marshal(pdu, nwnames, qids);
+ trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
+out:
+ put_fid(pdu, fidp);
+ if (newfidp) {
+ put_fid(pdu, newfidp);
+ }
+ v9fs_path_free(&dpath);
+ v9fs_path_free(&path);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ if (nwnames && nwnames <= P9_MAXWELEM) {
+ for (name_idx = 0; name_idx < nwnames; name_idx++) {
+ v9fs_string_free(&wnames[name_idx]);
+ }
+ g_free(wnames);
+ g_free(qids);
+ }
+}
+
+static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
+{
+ struct statfs stbuf;
+ int32_t iounit = 0;
+ V9fsState *s = pdu->s;
+
+ /*
+ * iounit should be multiples of f_bsize (host filesystem block size
+ * and as well as less than (client msize - P9_IOHDRSZ))
+ */
+ if (!v9fs_co_statfs(pdu, path, &stbuf)) {
+ iounit = stbuf.f_bsize;
+ iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
+ }
+ if (!iounit) {
+ iounit = s->msize - P9_IOHDRSZ;
+ }
+ return iounit;
+}
+
+static void v9fs_open(void *opaque)
+{
+ int flags;
+ int32_t fid;
+ int32_t mode;
+ V9fsQID qid;
+ int iounit = 0;
+ ssize_t err = 0;
+ size_t offset = 7;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ if (s->proto_version == V9FS_PROTO_2000L) {
+ err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
+ } else {
+ uint8_t modebyte;
+ err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
+ mode = modebyte;
+ }
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ BUG_ON(fidp->fid_type != P9_FID_NONE);
+
+ err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qid);
+ if (S_ISDIR(stbuf.st_mode)) {
+ err = v9fs_co_opendir(pdu, fidp);
+ if (err < 0) {
+ goto out;
+ }
+ fidp->fid_type = P9_FID_DIR;
+ err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ } else {
+ if (s->proto_version == V9FS_PROTO_2000L) {
+ flags = get_dotl_openflags(s, mode);
+ } else {
+ flags = omode_to_uflags(mode);
+ }
+ if (is_ro_export(&s->ctx)) {
+ if (mode & O_WRONLY || mode & O_RDWR ||
+ mode & O_APPEND || mode & O_TRUNC) {
+ err = -EROFS;
+ goto out;
+ }
+ }
+ err = v9fs_co_open(pdu, fidp, flags);
+ if (err < 0) {
+ goto out;
+ }
+ fidp->fid_type = P9_FID_FILE;
+ fidp->open_flags = flags;
+ if (flags & O_EXCL) {
+ /*
+ * We let the host file system do O_EXCL check
+ * We should not reclaim such fd
+ */
+ fidp->flags |= FID_NON_RECLAIMABLE;
+ }
+ iounit = get_iounit(pdu, &fidp->path);
+ err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ }
+ trace_v9fs_open_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path, iounit);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static void v9fs_lcreate(void *opaque)
+{
+ int32_t dfid, flags, mode;
+ gid_t gid;
+ ssize_t err = 0;
+ ssize_t offset = 7;
+ V9fsString name;
+ V9fsFidState *fidp;
+ struct stat stbuf;
+ V9fsQID qid;
+ int32_t iounit;
+ V9fsPDU *pdu = opaque;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
+ &name, &flags, &mode, &gid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
+
+ fidp = get_fid(pdu, dfid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ flags = get_dotl_openflags(pdu->s, flags);
+ err = v9fs_co_open2(pdu, fidp, &name, gid,
+ flags | O_CREAT, mode, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ fidp->fid_type = P9_FID_FILE;
+ fidp->open_flags = flags;
+ if (flags & O_EXCL) {
+ /*
+ * We let the host file system do O_EXCL check
+ * We should not reclaim such fd
+ */
+ fidp->flags |= FID_NON_RECLAIMABLE;
+ }
+ iounit = get_iounit(pdu, &fidp->path);
+ stat_to_qid(&stbuf, &qid);
+ err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_lcreate_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path, iounit);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+static void v9fs_fsync(void *opaque)
+{
+ int err;
+ int32_t fid;
+ int datasync;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_fsync(pdu, fidp, datasync);
+ if (!err) {
+ err = offset;
+ }
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static void v9fs_clunk(void *opaque)
+{
+ int err;
+ int32_t fid;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "d", &fid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_clunk(pdu->tag, pdu->id, fid);
+
+ fidp = clunk_fid(s, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ /*
+ * Bump the ref so that put_fid will
+ * free the fid.
+ */
+ fidp->ref++;
+ err = put_fid(pdu, fidp);
+ if (!err) {
+ err = offset;
+ }
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
+ uint64_t off, uint32_t max_count)
+{
+ ssize_t err;
+ size_t offset = 7;
+ int read_count;
+ int64_t xattr_len;
+
+ xattr_len = fidp->fs.xattr.len;
+ read_count = xattr_len - off;
+ if (read_count > max_count) {
+ read_count = max_count;
+ } else if (read_count < 0) {
+ /*
+ * read beyond XATTR value
+ */
+ read_count = 0;
+ }
+ err = pdu_marshal(pdu, offset, "d", read_count);
+ if (err < 0) {
+ return err;
+ }
+ offset += err;
+ err = v9fs_pack(pdu->elem.in_sg, pdu->elem.in_num, offset,
+ ((char *)fidp->fs.xattr.value) + off,
+ read_count);
+ if (err < 0) {
+ return err;
+ }
+ offset += err;
+ return offset;
+}
+
+static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
+ V9fsFidState *fidp, uint32_t max_count)
+{
+ V9fsPath path;
+ V9fsStat v9stat;
+ int len, err = 0;
+ int32_t count = 0;
+ struct stat stbuf;
+ off_t saved_dir_pos;
+ struct dirent *dent, *result;
+
+ /* save the directory position */
+ saved_dir_pos = v9fs_co_telldir(pdu, fidp);
+ if (saved_dir_pos < 0) {
+ return saved_dir_pos;
+ }
+
+ dent = g_malloc(sizeof(struct dirent));
+
+ while (1) {
+ v9fs_path_init(&path);
+ err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
+ if (err || !result) {
+ break;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_lstat(pdu, &path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
+ if (err < 0) {
+ goto out;
+ }
+ /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
+ len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
+ if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
+ /* Ran out of buffer. Set dir back to old position and return */
+ v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
+ v9fs_stat_free(&v9stat);
+ v9fs_path_free(&path);
+ g_free(dent);
+ return count;
+ }
+ count += len;
+ v9fs_stat_free(&v9stat);
+ v9fs_path_free(&path);
+ saved_dir_pos = dent->d_off;
+ }
+out:
+ g_free(dent);
+ v9fs_path_free(&path);
+ if (err < 0) {
+ return err;
+ }
+ return count;
+}
+
+/*
+ * Create a QEMUIOVector for a sub-region of PDU iovecs
+ *
+ * @qiov: uninitialized QEMUIOVector
+ * @skip: number of bytes to skip from beginning of PDU
+ * @size: number of bytes to include
+ * @is_write: true - write, false - read
+ *
+ * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
+ * with qemu_iovec_destroy().
+ */
+static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
+ size_t skip, size_t size,
+ bool is_write)
+{
+ QEMUIOVector elem;
+ struct iovec *iov;
+ unsigned int niov;
+
+ if (is_write) {
+ iov = pdu->elem.out_sg;
+ niov = pdu->elem.out_num;
+ } else {
+ iov = pdu->elem.in_sg;
+ niov = pdu->elem.in_num;
+ }
+
+ qemu_iovec_init_external(&elem, iov, niov);
+ qemu_iovec_init(qiov, niov);
+ qemu_iovec_concat(qiov, &elem, skip, size);
+}
+
+static void v9fs_read(void *opaque)
+{
+ int32_t fid;
+ uint64_t off;
+ ssize_t err = 0;
+ int32_t count = 0;
+ size_t offset = 7;
+ uint32_t max_count;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ if (fidp->fid_type == P9_FID_DIR) {
+
+ if (off == 0) {
+ v9fs_co_rewinddir(pdu, fidp);
+ }
+ count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
+ if (count < 0) {
+ err = count;
+ goto out;
+ }
+ err = pdu_marshal(pdu, offset, "d", count);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset + count;
+ } else if (fidp->fid_type == P9_FID_FILE) {
+ QEMUIOVector qiov_full;
+ QEMUIOVector qiov;
+ int32_t len;
+
+ v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
+ qemu_iovec_init(&qiov, qiov_full.niov);
+ do {
+ qemu_iovec_reset(&qiov);
+ qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
+ if (0) {
+ print_sg(qiov.iov, qiov.niov);
+ }
+ /* Loop in case of EINTR */
+ do {
+ len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
+ if (len >= 0) {
+ off += len;
+ count += len;
+ }
+ } while (len == -EINTR && !pdu->cancelled);
+ if (len < 0) {
+ /* IO error return the error */
+ err = len;
+ goto out;
+ }
+ } while (count < max_count && len > 0);
+ err = pdu_marshal(pdu, offset, "d", count);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset + count;
+ qemu_iovec_destroy(&qiov);
+ qemu_iovec_destroy(&qiov_full);
+ } else if (fidp->fid_type == P9_FID_XATTR) {
+ err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
+ } else {
+ err = -EINVAL;
+ }
+ trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+}
+
+static size_t v9fs_readdir_data_size(V9fsString *name)
+{
+ /*
+ * Size of each dirent on the wire: size of qid (13) + size of offset (8)
+ * size of type (1) + size of name.size (2) + strlen(name.data)
+ */
+ return 24 + v9fs_string_size(name);
+}
+
+static int v9fs_do_readdir(V9fsPDU *pdu,
+ V9fsFidState *fidp, int32_t max_count)
+{
+ size_t size;
+ V9fsQID qid;
+ V9fsString name;
+ int len, err = 0;
+ int32_t count = 0;
+ off_t saved_dir_pos;
+ struct dirent *dent, *result;
+
+ /* save the directory position */
+ saved_dir_pos = v9fs_co_telldir(pdu, fidp);
+ if (saved_dir_pos < 0) {
+ return saved_dir_pos;
+ }
+
+ dent = g_malloc(sizeof(struct dirent));
+
+ while (1) {
+ err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
+ if (err || !result) {
+ break;
+ }
+ v9fs_string_init(&name);
+ v9fs_string_sprintf(&name, "%s", dent->d_name);
+ if ((count + v9fs_readdir_data_size(&name)) > max_count) {
+ /* Ran out of buffer. Set dir back to old position and return */
+ v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
+ v9fs_string_free(&name);
+ g_free(dent);
+ return count;
+ }
+ /*
+ * Fill up just the path field of qid because the client uses
+ * only that. To fill the entire qid structure we will have
+ * to stat each dirent found, which is expensive
+ */
+ size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
+ memcpy(&qid.path, &dent->d_ino, size);
+ /* Fill the other fields with dummy values */
+ qid.type = 0;
+ qid.version = 0;
+
+ /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
+ len = pdu_marshal(pdu, 11 + count, "Qqbs",
+ &qid, dent->d_off,
+ dent->d_type, &name);
+ if (len < 0) {
+ v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
+ v9fs_string_free(&name);
+ g_free(dent);
+ return len;
+ }
+ count += len;
+ v9fs_string_free(&name);
+ saved_dir_pos = dent->d_off;
+ }
+ g_free(dent);
+ if (err < 0) {
+ return err;
+ }
+ return count;
+}
+
+static void v9fs_readdir(void *opaque)
+{
+ int32_t fid;
+ V9fsFidState *fidp;
+ ssize_t retval = 0;
+ size_t offset = 7;
+ uint64_t initial_offset;
+ int32_t count;
+ uint32_t max_count;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
+ &initial_offset, &max_count);
+ if (retval < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ retval = -EINVAL;
+ goto out_nofid;
+ }
+ if (!fidp->fs.dir) {
+ retval = -EINVAL;
+ goto out;
+ }
+ if (initial_offset == 0) {
+ v9fs_co_rewinddir(pdu, fidp);
+ } else {
+ v9fs_co_seekdir(pdu, fidp, initial_offset);
+ }
+ count = v9fs_do_readdir(pdu, fidp, max_count);
+ if (count < 0) {
+ retval = count;
+ goto out;
+ }
+ retval = pdu_marshal(pdu, offset, "d", count);
+ if (retval < 0) {
+ goto out;
+ }
+ retval += count + offset;
+ trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, retval);
+}
+
+static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
+ uint64_t off, uint32_t count,
+ struct iovec *sg, int cnt)
+{
+ int i, to_copy;
+ ssize_t err = 0;
+ int write_count;
+ int64_t xattr_len;
+ size_t offset = 7;
+
+
+ xattr_len = fidp->fs.xattr.len;
+ write_count = xattr_len - off;
+ if (write_count > count) {
+ write_count = count;
+ } else if (write_count < 0) {
+ /*
+ * write beyond XATTR value len specified in
+ * xattrcreate
+ */
+ err = -ENOSPC;
+ goto out;
+ }
+ err = pdu_marshal(pdu, offset, "d", write_count);
+ if (err < 0) {
+ return err;
+ }
+ err += offset;
+ fidp->fs.xattr.copied_len += write_count;
+ /*
+ * Now copy the content from sg list
+ */
+ for (i = 0; i < cnt; i++) {
+ if (write_count > sg[i].iov_len) {
+ to_copy = sg[i].iov_len;
+ } else {
+ to_copy = write_count;
+ }
+ memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
+ /* updating vs->off since we are not using below */
+ off += to_copy;
+ write_count -= to_copy;
+ }
+out:
+ return err;
+}
+
+static void v9fs_write(void *opaque)
+{
+ ssize_t err;
+ int32_t fid;
+ uint64_t off;
+ uint32_t count;
+ int32_t len = 0;
+ int32_t total = 0;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+ QEMUIOVector qiov_full;
+ QEMUIOVector qiov;
+
+ err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
+ if (err < 0) {
+ complete_pdu(s, pdu, err);
+ return;
+ }
+ offset += err;
+ v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
+ trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ if (fidp->fid_type == P9_FID_FILE) {
+ if (fidp->fs.fd == -1) {
+ err = -EINVAL;
+ goto out;
+ }
+ } else if (fidp->fid_type == P9_FID_XATTR) {
+ /*
+ * setxattr operation
+ */
+ err = v9fs_xattr_write(s, pdu, fidp, off, count,
+ qiov_full.iov, qiov_full.niov);
+ goto out;
+ } else {
+ err = -EINVAL;
+ goto out;
+ }
+ qemu_iovec_init(&qiov, qiov_full.niov);
+ do {
+ qemu_iovec_reset(&qiov);
+ qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
+ if (0) {
+ print_sg(qiov.iov, qiov.niov);
+ }
+ /* Loop in case of EINTR */
+ do {
+ len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
+ if (len >= 0) {
+ off += len;
+ total += len;
+ }
+ } while (len == -EINTR && !pdu->cancelled);
+ if (len < 0) {
+ /* IO error return the error */
+ err = len;
+ goto out_qiov;
+ }
+ } while (total < count && len > 0);
+
+ offset = 7;
+ err = pdu_marshal(pdu, offset, "d", total);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
+out_qiov:
+ qemu_iovec_destroy(&qiov);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ qemu_iovec_destroy(&qiov_full);
+ complete_pdu(s, pdu, err);
+}
+
+static void v9fs_create(void *opaque)
+{
+ int32_t fid;
+ int err = 0;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ V9fsQID qid;
+ int32_t perm;
+ int8_t mode;
+ V9fsPath path;
+ struct stat stbuf;
+ V9fsString name;
+ V9fsString extension;
+ int iounit;
+ V9fsPDU *pdu = opaque;
+
+ v9fs_path_init(&path);
+ v9fs_string_init(&name);
+ v9fs_string_init(&extension);
+ err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
+ &perm, &mode, &extension);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ if (perm & P9_STAT_MODE_DIR) {
+ err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
+ fidp->uid, -1, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ err = v9fs_co_opendir(pdu, fidp);
+ if (err < 0) {
+ goto out;
+ }
+ fidp->fid_type = P9_FID_DIR;
+ } else if (perm & P9_STAT_MODE_SYMLINK) {
+ err = v9fs_co_symlink(pdu, fidp, &name,
+ extension.data, -1 , &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ } else if (perm & P9_STAT_MODE_LINK) {
+ int32_t ofid = atoi(extension.data);
+ V9fsFidState *ofidp = get_fid(pdu, ofid);
+ if (ofidp == NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+ err = v9fs_co_link(pdu, ofidp, fidp, &name);
+ put_fid(pdu, ofidp);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ fidp->fid_type = P9_FID_NONE;
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (err < 0) {
+ fidp->fid_type = P9_FID_NONE;
+ goto out;
+ }
+ } else if (perm & P9_STAT_MODE_DEVICE) {
+ char ctype;
+ uint32_t major, minor;
+ mode_t nmode = 0;
+
+ if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
+ err = -errno;
+ goto out;
+ }
+
+ switch (ctype) {
+ case 'c':
+ nmode = S_IFCHR;
+ break;
+ case 'b':
+ nmode = S_IFBLK;
+ break;
+ default:
+ err = -EIO;
+ goto out;
+ }
+
+ nmode |= perm & 0777;
+ err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
+ makedev(major, minor), nmode, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
+ err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
+ 0, S_IFIFO | (perm & 0777), &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ } else if (perm & P9_STAT_MODE_SOCKET) {
+ err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
+ 0, S_IFSOCK | (perm & 0777), &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
+ if (err < 0) {
+ goto out;
+ }
+ v9fs_path_copy(&fidp->path, &path);
+ } else {
+ err = v9fs_co_open2(pdu, fidp, &name, -1,
+ omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ fidp->fid_type = P9_FID_FILE;
+ fidp->open_flags = omode_to_uflags(mode);
+ if (fidp->open_flags & O_EXCL) {
+ /*
+ * We let the host file system do O_EXCL check
+ * We should not reclaim such fd
+ */
+ fidp->flags |= FID_NON_RECLAIMABLE;
+ }
+ }
+ iounit = get_iounit(pdu, &fidp->path);
+ stat_to_qid(&stbuf, &qid);
+ err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_create_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path, iounit);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+ v9fs_string_free(&name);
+ v9fs_string_free(&extension);
+ v9fs_path_free(&path);
+}
+
+static void v9fs_symlink(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ V9fsString name;
+ V9fsString symname;
+ V9fsFidState *dfidp;
+ V9fsQID qid;
+ struct stat stbuf;
+ int32_t dfid;
+ int err = 0;
+ gid_t gid;
+ size_t offset = 7;
+
+ v9fs_string_init(&name);
+ v9fs_string_init(&symname);
+ err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
+
+ dfidp = get_fid(pdu, dfid);
+ if (dfidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qid);
+ err = pdu_marshal(pdu, offset, "Q", &qid);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_symlink_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path);
+out:
+ put_fid(pdu, dfidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+ v9fs_string_free(&name);
+ v9fs_string_free(&symname);
+}
+
+static void v9fs_flush(void *opaque)
+{
+ ssize_t err;
+ int16_t tag;
+ size_t offset = 7;
+ V9fsPDU *cancel_pdu;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ err = pdu_unmarshal(pdu, offset, "w", &tag);
+ if (err < 0) {
+ complete_pdu(s, pdu, err);
+ return;
+ }
+ trace_v9fs_flush(pdu->tag, pdu->id, tag);
+
+ QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
+ if (cancel_pdu->tag == tag) {
+ break;
+ }
+ }
+ if (cancel_pdu) {
+ cancel_pdu->cancelled = 1;
+ /*
+ * Wait for pdu to complete.
+ */
+ qemu_co_queue_wait(&cancel_pdu->complete);
+ cancel_pdu->cancelled = 0;
+ free_pdu(pdu->s, cancel_pdu);
+ }
+ complete_pdu(s, pdu, 7);
+}
+
+static void v9fs_link(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+ int32_t dfid, oldfid;
+ V9fsFidState *dfidp, *oldfidp;
+ V9fsString name;
+ size_t offset = 7;
+ int err = 0;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
+
+ dfidp = get_fid(pdu, dfid);
+ if (dfidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ oldfidp = get_fid(pdu, oldfid);
+ if (oldfidp == NULL) {
+ err = -ENOENT;
+ goto out;
+ }
+ err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
+ if (!err) {
+ err = offset;
+ }
+out:
+ put_fid(pdu, dfidp);
+out_nofid:
+ v9fs_string_free(&name);
+ complete_pdu(s, pdu, err);
+}
+
+/* Only works with path name based fid */
+static void v9fs_remove(void *opaque)
+{
+ int32_t fid;
+ int err = 0;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+
+ err = pdu_unmarshal(pdu, offset, "d", &fid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_remove(pdu->tag, pdu->id, fid);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ /* if fs driver is not path based, return EOPNOTSUPP */
+ if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
+ err = -EOPNOTSUPP;
+ goto out_err;
+ }
+ /*
+ * IF the file is unlinked, we cannot reopen
+ * the file later. So don't reclaim fd
+ */
+ err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
+ if (err < 0) {
+ goto out_err;
+ }
+ err = v9fs_co_remove(pdu, &fidp->path);
+ if (!err) {
+ err = offset;
+ }
+out_err:
+ /* For TREMOVE we need to clunk the fid even on failed remove */
+ clunk_fid(pdu->s, fidp->fid);
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+}
+
+static void v9fs_unlinkat(void *opaque)
+{
+ int err = 0;
+ V9fsString name;
+ int32_t dfid, flags;
+ size_t offset = 7;
+ V9fsPath path;
+ V9fsFidState *dfidp;
+ V9fsPDU *pdu = opaque;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ dfidp = get_fid(pdu, dfid);
+ if (dfidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ /*
+ * IF the file is unlinked, we cannot reopen
+ * the file later. So don't reclaim fd
+ */
+ v9fs_path_init(&path);
+ err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
+ if (err < 0) {
+ goto out_err;
+ }
+ err = v9fs_mark_fids_unreclaim(pdu, &path);
+ if (err < 0) {
+ goto out_err;
+ }
+ err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
+ if (!err) {
+ err = offset;
+ }
+out_err:
+ put_fid(pdu, dfidp);
+ v9fs_path_free(&path);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+
+/* Only works with path name based fid */
+static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
+ int32_t newdirfid, V9fsString *name)
+{
+ char *end;
+ int err = 0;
+ V9fsPath new_path;
+ V9fsFidState *tfidp;
+ V9fsState *s = pdu->s;
+ V9fsFidState *dirfidp = NULL;
+ char *old_name, *new_name;
+
+ v9fs_path_init(&new_path);
+ if (newdirfid != -1) {
+ dirfidp = get_fid(pdu, newdirfid);
+ if (dirfidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ BUG_ON(dirfidp->fid_type != P9_FID_NONE);
+ v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
+ } else {
+ old_name = fidp->path.data;
+ end = strrchr(old_name, '/');
+ if (end) {
+ end++;
+ } else {
+ end = old_name;
+ }
+ new_name = g_malloc0(end - old_name + name->size + 1);
+ strncat(new_name, old_name, end - old_name);
+ strncat(new_name + (end - old_name), name->data, name->size);
+ v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
+ g_free(new_name);
+ }
+ err = v9fs_co_rename(pdu, &fidp->path, &new_path);
+ if (err < 0) {
+ goto out;
+ }
+ /*
+ * Fixup fid's pointing to the old name to
+ * start pointing to the new name
+ */
+ for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
+ if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
+ /* replace the name */
+ v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
+ }
+ }
+out:
+ if (dirfidp) {
+ put_fid(pdu, dirfidp);
+ }
+ v9fs_path_free(&new_path);
+out_nofid:
+ return err;
+}
+
+/* Only works with path name based fid */
+static void v9fs_rename(void *opaque)
+{
+ int32_t fid;
+ ssize_t err = 0;
+ size_t offset = 7;
+ V9fsString name;
+ int32_t newdirfid;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ BUG_ON(fidp->fid_type != P9_FID_NONE);
+ /* if fs driver is not path based, return EOPNOTSUPP */
+ if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
+ err = -EOPNOTSUPP;
+ goto out;
+ }
+ v9fs_path_write_lock(s);
+ err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
+ v9fs_path_unlock(s);
+ if (!err) {
+ err = offset;
+ }
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
+ V9fsString *old_name, V9fsPath *newdir,
+ V9fsString *new_name)
+{
+ V9fsFidState *tfidp;
+ V9fsPath oldpath, newpath;
+ V9fsState *s = pdu->s;
+
+
+ v9fs_path_init(&oldpath);
+ v9fs_path_init(&newpath);
+ v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
+ v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
+
+ /*
+ * Fixup fid's pointing to the old name to
+ * start pointing to the new name
+ */
+ for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
+ if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
+ /* replace the name */
+ v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
+ }
+ }
+ v9fs_path_free(&oldpath);
+ v9fs_path_free(&newpath);
+}
+
+static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
+ V9fsString *old_name, int32_t newdirfid,
+ V9fsString *new_name)
+{
+ int err = 0;
+ V9fsState *s = pdu->s;
+ V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
+
+ olddirfidp = get_fid(pdu, olddirfid);
+ if (olddirfidp == NULL) {
+ err = -ENOENT;
+ goto out;
+ }
+ if (newdirfid != -1) {
+ newdirfidp = get_fid(pdu, newdirfid);
+ if (newdirfidp == NULL) {
+ err = -ENOENT;
+ goto out;
+ }
+ } else {
+ newdirfidp = get_fid(pdu, olddirfid);
+ }
+
+ err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
+ &newdirfidp->path, new_name);
+ if (err < 0) {
+ goto out;
+ }
+ if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
+ /* Only for path based fid we need to do the below fixup */
+ v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
+ &newdirfidp->path, new_name);
+ }
+out:
+ if (olddirfidp) {
+ put_fid(pdu, olddirfidp);
+ }
+ if (newdirfidp) {
+ put_fid(pdu, newdirfidp);
+ }
+ return err;
+}
+
+static void v9fs_renameat(void *opaque)
+{
+ ssize_t err = 0;
+ size_t offset = 7;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+ int32_t olddirfid, newdirfid;
+ V9fsString old_name, new_name;
+
+ v9fs_string_init(&old_name);
+ v9fs_string_init(&new_name);
+ err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
+ &old_name, &newdirfid, &new_name);
+ if (err < 0) {
+ goto out_err;
+ }
+
+ v9fs_path_write_lock(s);
+ err = v9fs_complete_renameat(pdu, olddirfid,
+ &old_name, newdirfid, &new_name);
+ v9fs_path_unlock(s);
+ if (!err) {
+ err = offset;
+ }
+
+out_err:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&old_name);
+ v9fs_string_free(&new_name);
+}
+
+static void v9fs_wstat(void *opaque)
+{
+ int32_t fid;
+ int err = 0;
+ int16_t unused;
+ V9fsStat v9stat;
+ size_t offset = 7;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_stat_init(&v9stat);
+ err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_wstat(pdu->tag, pdu->id, fid,
+ v9stat.mode, v9stat.atime, v9stat.mtime);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ /* do we need to sync the file? */
+ if (donttouch_stat(&v9stat)) {
+ err = v9fs_co_fsync(pdu, fidp, 0);
+ goto out;
+ }
+ if (v9stat.mode != -1) {
+ uint32_t v9_mode;
+ err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ v9_mode = stat_to_v9mode(&stbuf);
+ if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
+ (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
+ /* Attempting to change the type */
+ err = -EIO;
+ goto out;
+ }
+ err = v9fs_co_chmod(pdu, &fidp->path,
+ v9mode_to_mode(v9stat.mode,
+ &v9stat.extension));
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9stat.mtime != -1 || v9stat.atime != -1) {
+ struct timespec times[2];
+ if (v9stat.atime != -1) {
+ times[0].tv_sec = v9stat.atime;
+ times[0].tv_nsec = 0;
+ } else {
+ times[0].tv_nsec = UTIME_OMIT;
+ }
+ if (v9stat.mtime != -1) {
+ times[1].tv_sec = v9stat.mtime;
+ times[1].tv_nsec = 0;
+ } else {
+ times[1].tv_nsec = UTIME_OMIT;
+ }
+ err = v9fs_co_utimensat(pdu, &fidp->path, times);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
+ err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9stat.name.size != 0) {
+ err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ if (v9stat.length != -1) {
+ err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
+ if (err < 0) {
+ goto out;
+ }
+ }
+ err = offset;
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ v9fs_stat_free(&v9stat);
+ complete_pdu(s, pdu, err);
+}
+
+static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
+{
+ uint32_t f_type;
+ uint32_t f_bsize;
+ uint64_t f_blocks;
+ uint64_t f_bfree;
+ uint64_t f_bavail;
+ uint64_t f_files;
+ uint64_t f_ffree;
+ uint64_t fsid_val;
+ uint32_t f_namelen;
+ size_t offset = 7;
+ int32_t bsize_factor;
+
+ /*
+ * compute bsize factor based on host file system block size
+ * and client msize
+ */
+ bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
+ if (!bsize_factor) {
+ bsize_factor = 1;
+ }
+ f_type = stbuf->f_type;
+ f_bsize = stbuf->f_bsize;
+ f_bsize *= bsize_factor;
+ /*
+ * f_bsize is adjusted(multiplied) by bsize factor, so we need to
+ * adjust(divide) the number of blocks, free blocks and available
+ * blocks by bsize factor
+ */
+ f_blocks = stbuf->f_blocks/bsize_factor;
+ f_bfree = stbuf->f_bfree/bsize_factor;
+ f_bavail = stbuf->f_bavail/bsize_factor;
+ f_files = stbuf->f_files;
+ f_ffree = stbuf->f_ffree;
+ fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
+ (unsigned long long)stbuf->f_fsid.__val[1] << 32;
+ f_namelen = stbuf->f_namelen;
+
+ return pdu_marshal(pdu, offset, "ddqqqqqqd",
+ f_type, f_bsize, f_blocks, f_bfree,
+ f_bavail, f_files, f_ffree,
+ fsid_val, f_namelen);
+}
+
+static void v9fs_statfs(void *opaque)
+{
+ int32_t fid;
+ ssize_t retval = 0;
+ size_t offset = 7;
+ V9fsFidState *fidp;
+ struct statfs stbuf;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ retval = pdu_unmarshal(pdu, offset, "d", &fid);
+ if (retval < 0) {
+ goto out_nofid;
+ }
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ retval = -ENOENT;
+ goto out_nofid;
+ }
+ retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
+ if (retval < 0) {
+ goto out;
+ }
+ retval = v9fs_fill_statfs(s, pdu, &stbuf);
+ if (retval < 0) {
+ goto out;
+ }
+ retval += offset;
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, retval);
+}
+
+static void v9fs_mknod(void *opaque)
+{
+
+ int mode;
+ gid_t gid;
+ int32_t fid;
+ V9fsQID qid;
+ int err = 0;
+ int major, minor;
+ size_t offset = 7;
+ V9fsString name;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
+ &major, &minor, &gid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
+ makedev(major, minor), mode, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qid);
+ err = pdu_marshal(pdu, offset, "Q", &qid);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_mknod_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+/*
+ * Implement posix byte range locking code
+ * Server side handling of locking code is very simple, because 9p server in
+ * QEMU can handle only one client. And most of the lock handling
+ * (like conflict, merging) etc is done by the VFS layer itself, so no need to
+ * do any thing in * qemu 9p server side lock code path.
+ * So when a TLOCK request comes, always return success
+ */
+static void v9fs_lock(void *opaque)
+{
+ int8_t status;
+ V9fsFlock flock;
+ size_t offset = 7;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ int32_t fid, err = 0;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ status = P9_LOCK_ERROR;
+ v9fs_string_init(&flock.client_id);
+ err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
+ &flock.flags, &flock.start, &flock.length,
+ &flock.proc_id, &flock.client_id);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_lock(pdu->tag, pdu->id, fid,
+ flock.type, flock.start, flock.length);
+
+
+ /* We support only block flag now (that too ignored currently) */
+ if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_fstat(pdu, fidp, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ status = P9_LOCK_SUCCESS;
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ err = pdu_marshal(pdu, offset, "b", status);
+ if (err > 0) {
+ err += offset;
+ }
+ trace_v9fs_lock_return(pdu->tag, pdu->id, status);
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&flock.client_id);
+}
+
+/*
+ * When a TGETLOCK request comes, always return success because all lock
+ * handling is done by client's VFS layer.
+ */
+static void v9fs_getlock(void *opaque)
+{
+ size_t offset = 7;
+ struct stat stbuf;
+ V9fsFidState *fidp;
+ V9fsGetlock glock;
+ int32_t fid, err = 0;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_string_init(&glock.client_id);
+ err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
+ &glock.start, &glock.length, &glock.proc_id,
+ &glock.client_id);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_getlock(pdu->tag, pdu->id, fid,
+ glock.type, glock.start, glock.length);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_fstat(pdu, fidp, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ glock.type = P9_LOCK_TYPE_UNLCK;
+ err = pdu_marshal(pdu, offset, "bqqds", glock.type,
+ glock.start, glock.length, glock.proc_id,
+ &glock.client_id);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
+ glock.length, glock.proc_id);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&glock.client_id);
+}
+
+static void v9fs_mkdir(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ size_t offset = 7;
+ int32_t fid;
+ struct stat stbuf;
+ V9fsQID qid;
+ V9fsString name;
+ V9fsFidState *fidp;
+ gid_t gid;
+ int mode;
+ int err = 0;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
+
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
+ if (err < 0) {
+ goto out;
+ }
+ stat_to_qid(&stbuf, &qid);
+ err = pdu_marshal(pdu, offset, "Q", &qid);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_mkdir_return(pdu->tag, pdu->id,
+ qid.type, qid.version, qid.path, err);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+static void v9fs_xattrwalk(void *opaque)
+{
+ int64_t size;
+ V9fsString name;
+ ssize_t err = 0;
+ size_t offset = 7;
+ int32_t fid, newfid;
+ V9fsFidState *file_fidp;
+ V9fsFidState *xattr_fidp = NULL;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
+
+ file_fidp = get_fid(pdu, fid);
+ if (file_fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+ xattr_fidp = alloc_fid(s, newfid);
+ if (xattr_fidp == NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+ v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
+ if (name.data == NULL) {
+ /*
+ * listxattr request. Get the size first
+ */
+ size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
+ if (size < 0) {
+ err = size;
+ clunk_fid(s, xattr_fidp->fid);
+ goto out;
+ }
+ /*
+ * Read the xattr value
+ */
+ xattr_fidp->fs.xattr.len = size;
+ xattr_fidp->fid_type = P9_FID_XATTR;
+ xattr_fidp->fs.xattr.copied_len = -1;
+ if (size) {
+ xattr_fidp->fs.xattr.value = g_malloc(size);
+ err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
+ xattr_fidp->fs.xattr.value,
+ xattr_fidp->fs.xattr.len);
+ if (err < 0) {
+ clunk_fid(s, xattr_fidp->fid);
+ goto out;
+ }
+ }
+ err = pdu_marshal(pdu, offset, "q", size);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ } else {
+ /*
+ * specific xattr fid. We check for xattr
+ * presence also collect the xattr size
+ */
+ size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
+ &name, NULL, 0);
+ if (size < 0) {
+ err = size;
+ clunk_fid(s, xattr_fidp->fid);
+ goto out;
+ }
+ /*
+ * Read the xattr value
+ */
+ xattr_fidp->fs.xattr.len = size;
+ xattr_fidp->fid_type = P9_FID_XATTR;
+ xattr_fidp->fs.xattr.copied_len = -1;
+ if (size) {
+ xattr_fidp->fs.xattr.value = g_malloc(size);
+ err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
+ &name, xattr_fidp->fs.xattr.value,
+ xattr_fidp->fs.xattr.len);
+ if (err < 0) {
+ clunk_fid(s, xattr_fidp->fid);
+ goto out;
+ }
+ }
+ err = pdu_marshal(pdu, offset, "q", size);
+ if (err < 0) {
+ goto out;
+ }
+ err += offset;
+ }
+ trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
+out:
+ put_fid(pdu, file_fidp);
+ if (xattr_fidp) {
+ put_fid(pdu, xattr_fidp);
+ }
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+static void v9fs_xattrcreate(void *opaque)
+{
+ int flags;
+ int32_t fid;
+ int64_t size;
+ ssize_t err = 0;
+ V9fsString name;
+ size_t offset = 7;
+ V9fsFidState *file_fidp;
+ V9fsFidState *xattr_fidp;
+ V9fsPDU *pdu = opaque;
+ V9fsState *s = pdu->s;
+
+ v9fs_string_init(&name);
+ err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
+
+ file_fidp = get_fid(pdu, fid);
+ if (file_fidp == NULL) {
+ err = -EINVAL;
+ goto out_nofid;
+ }
+ /* Make the file fid point to xattr */
+ xattr_fidp = file_fidp;
+ xattr_fidp->fid_type = P9_FID_XATTR;
+ xattr_fidp->fs.xattr.copied_len = 0;
+ xattr_fidp->fs.xattr.len = size;
+ xattr_fidp->fs.xattr.flags = flags;
+ v9fs_string_init(&xattr_fidp->fs.xattr.name);
+ v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
+ xattr_fidp->fs.xattr.value = g_malloc(size);
+ err = offset;
+ put_fid(pdu, file_fidp);
+out_nofid:
+ complete_pdu(s, pdu, err);
+ v9fs_string_free(&name);
+}
+
+static void v9fs_readlink(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ size_t offset = 7;
+ V9fsString target;
+ int32_t fid;
+ int err = 0;
+ V9fsFidState *fidp;
+
+ err = pdu_unmarshal(pdu, offset, "d", &fid);
+ if (err < 0) {
+ goto out_nofid;
+ }
+ trace_v9fs_readlink(pdu->tag, pdu->id, fid);
+ fidp = get_fid(pdu, fid);
+ if (fidp == NULL) {
+ err = -ENOENT;
+ goto out_nofid;
+ }
+
+ v9fs_string_init(&target);
+ err = v9fs_co_readlink(pdu, &fidp->path, &target);
+ if (err < 0) {
+ goto out;
+ }
+ err = pdu_marshal(pdu, offset, "s", &target);
+ if (err < 0) {
+ v9fs_string_free(&target);
+ goto out;
+ }
+ err += offset;
+ trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
+ v9fs_string_free(&target);
+out:
+ put_fid(pdu, fidp);
+out_nofid:
+ complete_pdu(pdu->s, pdu, err);
+}
+
+static CoroutineEntry *pdu_co_handlers[] = {
+ [P9_TREADDIR] = v9fs_readdir,
+ [P9_TSTATFS] = v9fs_statfs,
+ [P9_TGETATTR] = v9fs_getattr,
+ [P9_TSETATTR] = v9fs_setattr,
+ [P9_TXATTRWALK] = v9fs_xattrwalk,
+ [P9_TXATTRCREATE] = v9fs_xattrcreate,
+ [P9_TMKNOD] = v9fs_mknod,
+ [P9_TRENAME] = v9fs_rename,
+ [P9_TLOCK] = v9fs_lock,
+ [P9_TGETLOCK] = v9fs_getlock,
+ [P9_TRENAMEAT] = v9fs_renameat,
+ [P9_TREADLINK] = v9fs_readlink,
+ [P9_TUNLINKAT] = v9fs_unlinkat,
+ [P9_TMKDIR] = v9fs_mkdir,
+ [P9_TVERSION] = v9fs_version,
+ [P9_TLOPEN] = v9fs_open,
+ [P9_TATTACH] = v9fs_attach,
+ [P9_TSTAT] = v9fs_stat,
+ [P9_TWALK] = v9fs_walk,
+ [P9_TCLUNK] = v9fs_clunk,
+ [P9_TFSYNC] = v9fs_fsync,
+ [P9_TOPEN] = v9fs_open,
+ [P9_TREAD] = v9fs_read,
+#if 0
+ [P9_TAUTH] = v9fs_auth,
+#endif
+ [P9_TFLUSH] = v9fs_flush,
+ [P9_TLINK] = v9fs_link,
+ [P9_TSYMLINK] = v9fs_symlink,
+ [P9_TCREATE] = v9fs_create,
+ [P9_TLCREATE] = v9fs_lcreate,
+ [P9_TWRITE] = v9fs_write,
+ [P9_TWSTAT] = v9fs_wstat,
+ [P9_TREMOVE] = v9fs_remove,
+};
+
+static void v9fs_op_not_supp(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
+}
+
+static void v9fs_fs_ro(void *opaque)
+{
+ V9fsPDU *pdu = opaque;
+ complete_pdu(pdu->s, pdu, -EROFS);
+}
+
+static inline bool is_read_only_op(V9fsPDU *pdu)
+{
+ switch (pdu->id) {
+ case P9_TREADDIR:
+ case P9_TSTATFS:
+ case P9_TGETATTR:
+ case P9_TXATTRWALK:
+ case P9_TLOCK:
+ case P9_TGETLOCK:
+ case P9_TREADLINK:
+ case P9_TVERSION:
+ case P9_TLOPEN:
+ case P9_TATTACH:
+ case P9_TSTAT:
+ case P9_TWALK:
+ case P9_TCLUNK:
+ case P9_TFSYNC:
+ case P9_TOPEN:
+ case P9_TREAD:
+ case P9_TAUTH:
+ case P9_TFLUSH:
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
+{
+ Coroutine *co;
+ CoroutineEntry *handler;
+
+ if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
+ (pdu_co_handlers[pdu->id] == NULL)) {
+ handler = v9fs_op_not_supp;
+ } else {
+ handler = pdu_co_handlers[pdu->id];
+ }
+
+ if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
+ handler = v9fs_fs_ro;
+ }
+ co = qemu_coroutine_create(handler);
+ qemu_coroutine_enter(co, pdu);
+}
+
+void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
+{
+ V9fsState *s = (V9fsState *)vdev;
+ V9fsPDU *pdu;
+ ssize_t len;
+
+ while ((pdu = alloc_pdu(s)) &&
+ (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
+ struct {
+ uint32_t size_le;
+ uint8_t id;
+ uint16_t tag_le;
+ } QEMU_PACKED out;
+ int len;
+
+ pdu->s = s;
+ BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
+ QEMU_BUILD_BUG_ON(sizeof out != 7);
+
+ len = iov_to_buf(pdu->elem.out_sg, pdu->elem.out_num, 0,
+ &out, sizeof out);
+ BUG_ON(len != sizeof out);
+
+ pdu->size = le32_to_cpu(out.size_le);
+
+ pdu->id = out.id;
+ pdu->tag = le16_to_cpu(out.tag_le);
+
+ qemu_co_queue_init(&pdu->complete);
+ submit_pdu(s, pdu);
+ }
+ free_pdu(s, pdu);
+}
+
+static void __attribute__((__constructor__)) virtio_9p_set_fd_limit(void)
+{
+ struct rlimit rlim;
+ if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
+ fprintf(stderr, "Failed to get the resource limit\n");
+ exit(1);
+ }
+ open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
+ open_fd_rc = rlim.rlim_cur/2;
+}
diff --git a/src/hw/9pfs/virtio-9p.h b/src/hw/9pfs/virtio-9p.h
new file mode 100644
index 0000000..d7a4dc1
--- /dev/null
+++ b/src/hw/9pfs/virtio-9p.h
@@ -0,0 +1,394 @@
+#ifndef _QEMU_VIRTIO_9P_H
+#define _QEMU_VIRTIO_9P_H
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/time.h>
+#include <utime.h>
+#include <sys/resource.h>
+#include <glib.h>
+#include "standard-headers/linux/virtio_9p.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-9p.h"
+#include "fsdev/file-op-9p.h"
+#include "fsdev/virtio-9p-marshal.h"
+#include "qemu/thread.h"
+#include "qemu/coroutine.h"
+
+enum {
+ P9_TLERROR = 6,
+ P9_RLERROR,
+ P9_TSTATFS = 8,
+ P9_RSTATFS,
+ P9_TLOPEN = 12,
+ P9_RLOPEN,
+ P9_TLCREATE = 14,
+ P9_RLCREATE,
+ P9_TSYMLINK = 16,
+ P9_RSYMLINK,
+ P9_TMKNOD = 18,
+ P9_RMKNOD,
+ P9_TRENAME = 20,
+ P9_RRENAME,
+ P9_TREADLINK = 22,
+ P9_RREADLINK,
+ P9_TGETATTR = 24,
+ P9_RGETATTR,
+ P9_TSETATTR = 26,
+ P9_RSETATTR,
+ P9_TXATTRWALK = 30,
+ P9_RXATTRWALK,
+ P9_TXATTRCREATE = 32,
+ P9_RXATTRCREATE,
+ P9_TREADDIR = 40,
+ P9_RREADDIR,
+ P9_TFSYNC = 50,
+ P9_RFSYNC,
+ P9_TLOCK = 52,
+ P9_RLOCK,
+ P9_TGETLOCK = 54,
+ P9_RGETLOCK,
+ P9_TLINK = 70,
+ P9_RLINK,
+ P9_TMKDIR = 72,
+ P9_RMKDIR,
+ P9_TRENAMEAT = 74,
+ P9_RRENAMEAT,
+ P9_TUNLINKAT = 76,
+ P9_RUNLINKAT,
+ P9_TVERSION = 100,
+ P9_RVERSION,
+ P9_TAUTH = 102,
+ P9_RAUTH,
+ P9_TATTACH = 104,
+ P9_RATTACH,
+ P9_TERROR = 106,
+ P9_RERROR,
+ P9_TFLUSH = 108,
+ P9_RFLUSH,
+ P9_TWALK = 110,
+ P9_RWALK,
+ P9_TOPEN = 112,
+ P9_ROPEN,
+ P9_TCREATE = 114,
+ P9_RCREATE,
+ P9_TREAD = 116,
+ P9_RREAD,
+ P9_TWRITE = 118,
+ P9_RWRITE,
+ P9_TCLUNK = 120,
+ P9_RCLUNK,
+ P9_TREMOVE = 122,
+ P9_RREMOVE,
+ P9_TSTAT = 124,
+ P9_RSTAT,
+ P9_TWSTAT = 126,
+ P9_RWSTAT,
+};
+
+
+/* qid.types */
+enum {
+ P9_QTDIR = 0x80,
+ P9_QTAPPEND = 0x40,
+ P9_QTEXCL = 0x20,
+ P9_QTMOUNT = 0x10,
+ P9_QTAUTH = 0x08,
+ P9_QTTMP = 0x04,
+ P9_QTSYMLINK = 0x02,
+ P9_QTLINK = 0x01,
+ P9_QTFILE = 0x00,
+};
+
+enum p9_proto_version {
+ V9FS_PROTO_2000U = 0x01,
+ V9FS_PROTO_2000L = 0x02,
+};
+
+#define P9_NOTAG (u16)(~0)
+#define P9_NOFID (u32)(~0)
+#define P9_MAXWELEM 16
+
+#define FID_REFERENCED 0x1
+#define FID_NON_RECLAIMABLE 0x2
+static inline char *rpath(FsContext *ctx, const char *path)
+{
+ return g_strdup_printf("%s/%s", ctx->fs_root, path);
+}
+
+/*
+ * ample room for Twrite/Rread header
+ * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
+ */
+#define P9_IOHDRSZ 24
+
+typedef struct V9fsPDU V9fsPDU;
+struct V9fsState;
+
+struct V9fsPDU
+{
+ uint32_t size;
+ uint16_t tag;
+ uint8_t id;
+ uint8_t cancelled;
+ CoQueue complete;
+ VirtQueueElement elem;
+ struct V9fsState *s;
+ QLIST_ENTRY(V9fsPDU) next;
+};
+
+
+/* FIXME
+ * 1) change user needs to set groups and stuff
+ */
+
+#define MAX_REQ 128
+#define MAX_TAG_LEN 32
+
+#define BUG_ON(cond) assert(!(cond))
+
+typedef struct V9fsFidState V9fsFidState;
+
+enum {
+ P9_FID_NONE = 0,
+ P9_FID_FILE,
+ P9_FID_DIR,
+ P9_FID_XATTR,
+};
+
+typedef struct V9fsXattr
+{
+ int64_t copied_len;
+ int64_t len;
+ void *value;
+ V9fsString name;
+ int flags;
+} V9fsXattr;
+
+/*
+ * Filled by fs driver on open and other
+ * calls.
+ */
+union V9fsFidOpenState {
+ int fd;
+ DIR *dir;
+ V9fsXattr xattr;
+ /*
+ * private pointer for fs drivers, that
+ * have its own internal representation of
+ * open files.
+ */
+ void *private;
+};
+
+struct V9fsFidState
+{
+ int fid_type;
+ int32_t fid;
+ V9fsPath path;
+ V9fsFidOpenState fs;
+ V9fsFidOpenState fs_reclaim;
+ int flags;
+ int open_flags;
+ uid_t uid;
+ int ref;
+ int clunked;
+ V9fsFidState *next;
+ V9fsFidState *rclm_lst;
+};
+
+typedef struct V9fsState
+{
+ VirtIODevice parent_obj;
+ VirtQueue *vq;
+ V9fsPDU pdus[MAX_REQ];
+ QLIST_HEAD(, V9fsPDU) free_list;
+ QLIST_HEAD(, V9fsPDU) active_list;
+ V9fsFidState *fid_list;
+ FileOperations *ops;
+ FsContext ctx;
+ char *tag;
+ size_t config_size;
+ enum p9_proto_version proto_version;
+ int32_t msize;
+ /*
+ * lock ensuring atomic path update
+ * on rename.
+ */
+ CoRwlock rename_lock;
+ int32_t root_fid;
+ Error *migration_blocker;
+ V9fsConf fsconf;
+} V9fsState;
+
+typedef struct V9fsStatState {
+ V9fsPDU *pdu;
+ size_t offset;
+ V9fsStat v9stat;
+ V9fsFidState *fidp;
+ struct stat stbuf;
+} V9fsStatState;
+
+typedef struct V9fsOpenState {
+ V9fsPDU *pdu;
+ size_t offset;
+ int32_t mode;
+ V9fsFidState *fidp;
+ V9fsQID qid;
+ struct stat stbuf;
+ int iounit;
+} V9fsOpenState;
+
+typedef struct V9fsReadState {
+ V9fsPDU *pdu;
+ size_t offset;
+ int32_t count;
+ int32_t total;
+ int64_t off;
+ V9fsFidState *fidp;
+ struct iovec iov[128]; /* FIXME: bad, bad, bad */
+ struct iovec *sg;
+ off_t dir_pos;
+ struct dirent *dent;
+ struct stat stbuf;
+ V9fsString name;
+ V9fsStat v9stat;
+ int32_t len;
+ int32_t cnt;
+ int32_t max_count;
+} V9fsReadState;
+
+typedef struct V9fsWriteState {
+ V9fsPDU *pdu;
+ size_t offset;
+ int32_t len;
+ int32_t count;
+ int32_t total;
+ int64_t off;
+ V9fsFidState *fidp;
+ struct iovec iov[128]; /* FIXME: bad, bad, bad */
+ struct iovec *sg;
+ int cnt;
+} V9fsWriteState;
+
+typedef struct V9fsMkState {
+ V9fsPDU *pdu;
+ size_t offset;
+ V9fsQID qid;
+ struct stat stbuf;
+ V9fsString name;
+ V9fsString fullname;
+} V9fsMkState;
+
+/* 9p2000.L open flags */
+#define P9_DOTL_RDONLY 00000000
+#define P9_DOTL_WRONLY 00000001
+#define P9_DOTL_RDWR 00000002
+#define P9_DOTL_NOACCESS 00000003
+#define P9_DOTL_CREATE 00000100
+#define P9_DOTL_EXCL 00000200
+#define P9_DOTL_NOCTTY 00000400
+#define P9_DOTL_TRUNC 00001000
+#define P9_DOTL_APPEND 00002000
+#define P9_DOTL_NONBLOCK 00004000
+#define P9_DOTL_DSYNC 00010000
+#define P9_DOTL_FASYNC 00020000
+#define P9_DOTL_DIRECT 00040000
+#define P9_DOTL_LARGEFILE 00100000
+#define P9_DOTL_DIRECTORY 00200000
+#define P9_DOTL_NOFOLLOW 00400000
+#define P9_DOTL_NOATIME 01000000
+#define P9_DOTL_CLOEXEC 02000000
+#define P9_DOTL_SYNC 04000000
+
+/* 9p2000.L at flags */
+#define P9_DOTL_AT_REMOVEDIR 0x200
+
+/* 9P2000.L lock type */
+#define P9_LOCK_TYPE_RDLCK 0
+#define P9_LOCK_TYPE_WRLCK 1
+#define P9_LOCK_TYPE_UNLCK 2
+
+#define P9_LOCK_SUCCESS 0
+#define P9_LOCK_BLOCKED 1
+#define P9_LOCK_ERROR 2
+#define P9_LOCK_GRACE 3
+
+#define P9_LOCK_FLAGS_BLOCK 1
+#define P9_LOCK_FLAGS_RECLAIM 2
+
+typedef struct V9fsFlock
+{
+ uint8_t type;
+ uint32_t flags;
+ uint64_t start; /* absolute offset */
+ uint64_t length;
+ uint32_t proc_id;
+ V9fsString client_id;
+} V9fsFlock;
+
+typedef struct V9fsGetlock
+{
+ uint8_t type;
+ uint64_t start; /* absolute offset */
+ uint64_t length;
+ uint32_t proc_id;
+ V9fsString client_id;
+} V9fsGetlock;
+
+extern int open_fd_hw;
+extern int total_open_fd;
+
+size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
+ size_t offset, size_t size, int pack);
+
+static inline size_t do_pdu_unpack(void *dst, struct iovec *sg, int sg_count,
+ size_t offset, size_t size)
+{
+ return pdu_packunpack(dst, sg, sg_count, offset, size, 0);
+}
+
+static inline void v9fs_path_write_lock(V9fsState *s)
+{
+ if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
+ qemu_co_rwlock_wrlock(&s->rename_lock);
+ }
+}
+
+static inline void v9fs_path_read_lock(V9fsState *s)
+{
+ if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
+ qemu_co_rwlock_rdlock(&s->rename_lock);
+ }
+}
+
+static inline void v9fs_path_unlock(V9fsState *s)
+{
+ if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
+ qemu_co_rwlock_unlock(&s->rename_lock);
+ }
+}
+
+static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
+{
+ return pdu->cancelled;
+}
+
+extern void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq);
+extern void v9fs_reclaim_fd(V9fsPDU *pdu);
+extern void v9fs_path_init(V9fsPath *path);
+extern void v9fs_path_free(V9fsPath *path);
+extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
+extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
+ const char *name, V9fsPath *path);
+
+#define pdu_marshal(pdu, offset, fmt, args...) \
+ v9fs_marshal(pdu->elem.in_sg, pdu->elem.in_num, offset, 1, fmt, ##args)
+#define pdu_unmarshal(pdu, offset, fmt, args...) \
+ v9fs_unmarshal(pdu->elem.out_sg, pdu->elem.out_num, offset, 1, fmt, ##args)
+
+#define TYPE_VIRTIO_9P "virtio-9p-device"
+#define VIRTIO_9P(obj) \
+ OBJECT_CHECK(V9fsState, (obj), TYPE_VIRTIO_9P)
+
+#endif
OpenPOWER on IntegriCloud