From patchwork Mon Nov 21 13:36:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohan Kumar M X-Patchwork-Id: 126782 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8C60DB71D4 for ; Tue, 22 Nov 2011 00:37:10 +1100 (EST) Received: from localhost ([::1]:46047 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSU3W-00007P-NS for incoming@patchwork.ozlabs.org; Mon, 21 Nov 2011 08:37:06 -0500 Received: from eggs.gnu.org ([140.186.70.92]:46596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSU3G-0008MS-Uc for qemu-devel@nongnu.org; Mon, 21 Nov 2011 08:36:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RSU3B-0008TK-Ds for qemu-devel@nongnu.org; Mon, 21 Nov 2011 08:36:50 -0500 Received: from e23smtp09.au.ibm.com ([202.81.31.142]:51846) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSU3A-0008Qx-DQ for qemu-devel@nongnu.org; Mon, 21 Nov 2011 08:36:45 -0500 Received: from /spool/local by e23smtp09.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 21 Nov 2011 14:30:34 +1000 Received: from d23relay04.au.ibm.com ([202.81.31.246]) by e23smtp09.au.ibm.com ([202.81.31.206]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 21 Nov 2011 14:30:32 +1000 Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pALDX86I3436550 for ; Tue, 22 Nov 2011 00:33:08 +1100 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pALDaMph004634 for ; Tue, 22 Nov 2011 00:36:22 +1100 Received: from explorer.in.ibm.com (explorer.in.ibm.com [9.122.21.115]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pALDaGu7004347; Tue, 22 Nov 2011 00:36:21 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, aneesh.kumar@linux.vnet.ibm.com, stefanha@gmail.com, berrange@redhat.com Date: Mon, 21 Nov 2011 19:06:08 +0530 Message-Id: <1321882578-7498-4-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1321882578-7498-1-git-send-email-mohan@in.ibm.com> References: <1321882578-7498-1-git-send-email-mohan@in.ibm.com> x-cbid: 11112104-3568-0000-0000-000000C191A7 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 202.81.31.142 Cc: "M. Mohan Kumar" Subject: [Qemu-devel] [PATCH V3 03/13] hw/9pfs: Add new proxy filesystem driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "M. Mohan Kumar" Add new proxy filesystem driver to add root privilege to qemu process. It needs a helper process to be started by root user. Following command line can be used to utilize proxy filesystem driver -virtfs proxy,id=,mount_tag=,socket_fd= Signed-off-by: M. Mohan Kumar --- Makefile.objs | 2 +- fsdev/file-op-9p.h | 1 - fsdev/qemu-fsdev.c | 1 + fsdev/qemu-fsdev.h | 1 + fsdev/virtio-9p-marshal.h | 2 +- hw/9pfs/virtio-9p-proxy.c | 374 +++++++++++++++++++++++++++++++++++++++++++++ hw/9pfs/virtio-9p-proxy.h | 10 ++ qemu-config.c | 6 + vl.c | 6 +- 9 files changed, 399 insertions(+), 4 deletions(-) create mode 100644 hw/9pfs/virtio-9p-proxy.c create mode 100644 hw/9pfs/virtio-9p-proxy.h diff --git a/Makefile.objs b/Makefile.objs index c256fdc..8201202 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -311,7 +311,7 @@ hw-obj-$(CONFIG_SOUND) += $(sound-obj-y) 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-xattr-user.o virtio-9p-posix-acl.o 9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-coth.o cofs.o codir.o cofile.o 9pfs-nested-$(CONFIG_VIRTFS) += coxattr.o virtio-9p-handle.o -9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-synth.o +9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-synth.o virtio-9p-proxy.o hw-obj-$(CONFIG_REALLY_VIRTFS) += $(addprefix 9pfs/, $(9pfs-nested-y)) $(addprefix 9pfs/, $(9pfs-nested-y)): QEMU_CFLAGS+=$(GLIB_CFLAGS) diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h index 22849c9..84e5375 100644 --- a/fsdev/file-op-9p.h +++ b/fsdev/file-op-9p.h @@ -60,7 +60,6 @@ typedef struct extended_ops { #define V9FS_SEC_MASK 0x0000001C - typedef struct FileOperations FileOperations; /* * Structure to store the various fsdev's passed through command line. diff --git a/fsdev/qemu-fsdev.c b/fsdev/qemu-fsdev.c index efbfea1..b31d116 100644 --- a/fsdev/qemu-fsdev.c +++ b/fsdev/qemu-fsdev.c @@ -25,6 +25,7 @@ static FsDriverTable FsDrivers[] = { { .name = "local", .ops = &local_ops}, { .name = "handle", .ops = &handle_ops}, { .name = "synth", .ops = &synth_ops}, + { .name = "proxy", .ops = &proxy_ops}, }; int qemu_fsdev_add(QemuOpts *opts) diff --git a/fsdev/qemu-fsdev.h b/fsdev/qemu-fsdev.h index 921452d..1af1f54 100644 --- a/fsdev/qemu-fsdev.h +++ b/fsdev/qemu-fsdev.h @@ -44,4 +44,5 @@ FsDriverEntry *get_fsdev_fsentry(char *id); extern FileOperations local_ops; extern FileOperations handle_ops; extern FileOperations synth_ops; +extern FileOperations proxy_ops; #endif diff --git a/fsdev/virtio-9p-marshal.h b/fsdev/virtio-9p-marshal.h index b268cbf..36302b7 100644 --- a/fsdev/virtio-9p-marshal.h +++ b/fsdev/virtio-9p-marshal.h @@ -30,7 +30,7 @@ typedef struct V9fsStat V9fsString muid; /* 9p2000.u */ V9fsString extension; - int32_t n_uid; + int32_t n_uid; int32_t n_gid; int32_t n_muid; } V9fsStat; diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c new file mode 100644 index 0000000..0e539e3 --- /dev/null +++ b/hw/9pfs/virtio-9p-proxy.c @@ -0,0 +1,374 @@ +/* + * Virtio 9p Proxy callback + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * M. Mohan Kumar + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ +#include +#include +#include "hw/virtio.h" +#include "virtio-9p.h" +#include "fsdev/qemu-fsdev.h" +#include "virtio-9p-proxy.h" + +typedef struct V9fsProxy { + int sockfd; + QemuMutex mutex; + struct iovec iovec; +} V9fsProxy; + +static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) +{ + errno = EOPNOTSUPP; + return -1; +} + +static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path, + char *buf, size_t bufsz) +{ + errno = EOPNOTSUPP; + return -1; +} + +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 = -1; + return fs->fd; +} + +static int proxy_opendir(FsContext *ctx, + V9fsPath *fs_path, V9fsFidOpenState *fs) +{ + fs->dir = NULL; + errno = EOPNOTSUPP; + return -1; +} + +static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) +{ + return 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) +{ + return seekdir(fs->dir, off); +} + +static ssize_t proxy_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 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 + 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 proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path, + const char *name, FsCred *credp) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, + const char *name, FsCred *credp) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_fstat(FsContext *fs_ctx, + V9fsFidOpenState *fs, struct stat *stbuf) +{ + int err; + err = fstat(fs->fd, stbuf); + return err; +} + +static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, + int flags, FsCred *credp, V9fsFidOpenState *fs) +{ + fs->fd = -1; + errno = EOPNOTSUPP; + return -1; +} + + +static int proxy_symlink(FsContext *fs_ctx, const char *oldpath, + V9fsPath *dir_path, const char *name, FsCred *credp) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_link(FsContext *ctx, V9fsPath *oldpath, + V9fsPath *dirpath, const char *name) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_rename(FsContext *ctx, const char *oldpath, + const char *newpath) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_utimensat(FsContext *s, V9fsPath *fs_path, + const struct timespec *buf) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_remove(FsContext *ctx, const char *path) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync) +{ + if (datasync) { + return qemu_fdatasync(fs->fd); + } else { + return fsync(fs->fd); + } +} + +static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) +{ + errno = EOPNOTSUPP; + return -1; +} + +static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path, + const char *name, void *value, size_t size) +{ + errno = EOPNOTSUPP; + return -1; +} + +static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path, + void *value, size_t size) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, + void *value, size_t size, int flags) +{ + errno = EOPNOTSUPP; + return -1; +} + +static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path, + const char *name) +{ + errno = EOPNOTSUPP; + return -1; +} + +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_parse_opts(QemuOpts *opts, struct FsDriverEntry *fs) +{ + const char *sock_fd = qemu_opt_get(opts, "sock_fd"); + + if (sock_fd) { + fprintf(stderr, "sock_fd option not specified\n"); + return -1; + } + fs->path = g_strdup(sock_fd); + return 0; +} + +static int proxy_init(FsContext *ctx) +{ + V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy)); + int sock_id; + + sock_id = atoi(ctx->fs_root); + if (sock_id < 0) { + fprintf(stderr, "socket descriptor not initialized\n"); + return -1; + } + g_free(ctx->fs_root); + + proxy->iovec.iov_base = g_malloc(BUFF_SZ); + proxy->iovec.iov_len = BUFF_SZ; + ctx->private = proxy; + proxy->sockfd = sock_id; + qemu_mutex_init(&proxy->mutex); + + ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; + 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/hw/9pfs/virtio-9p-proxy.h b/hw/9pfs/virtio-9p-proxy.h new file mode 100644 index 0000000..f5e1a02 --- /dev/null +++ b/hw/9pfs/virtio-9p-proxy.h @@ -0,0 +1,10 @@ +#ifndef _QEMU_VIRTIO_9P_PROXY_H +#define _QEMU_VIRTIO_9P_PROXY_H + +#define BUFF_SZ (4 * 1024) + +typedef struct { + int type; + int size; +} ProxyHeader; +#endif diff --git a/qemu-config.c b/qemu-config.c index 597d7e1..33367fe 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -183,6 +183,9 @@ QemuOptsList qemu_fsdev_opts = { }, { .name = "readonly", .type = QEMU_OPT_BOOL, + }, { + .name = "sock_fd", + .type = QEMU_OPT_NUMBER, }, { /*End of list */ } @@ -212,6 +215,9 @@ QemuOptsList qemu_virtfs_opts = { }, { .name = "readonly", .type = QEMU_OPT_BOOL, + }, { + .name = "sock_fd", + .type = QEMU_OPT_NUMBER, }, { /*End of list */ } diff --git a/vl.c b/vl.c index e3b2443..1feaf37 100644 --- a/vl.c +++ b/vl.c @@ -2663,7 +2663,7 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_virtfs: { QemuOpts *fsdev; QemuOpts *device; - const char *writeout; + const char *writeout, *sock_fd; olist = qemu_find_opts("virtfs"); if (!olist) { @@ -2703,6 +2703,10 @@ int main(int argc, char **argv, char **envp) qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path")); qemu_opt_set(fsdev, "security_model", qemu_opt_get(opts, "security_model")); + sock_fd = qemu_opt_get(opts, "sock_fd"); + if (sock_fd) { + qemu_opt_set(fsdev, "sock_fd", sock_fd); + } qemu_opt_set_bool(fsdev, "readonly", qemu_opt_get_bool(opts, "readonly", 0));