From patchwork Mon Dec 5 16:18:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "M. Mohan Kumar" X-Patchwork-Id: 129361 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 E8D111007D4 for ; Tue, 6 Dec 2011 03:51:35 +1100 (EST) Received: from localhost ([::1]:54220 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXbJV-0001cc-9k for incoming@patchwork.ozlabs.org; Mon, 05 Dec 2011 11:22:45 -0500 Received: from eggs.gnu.org ([140.186.70.92]:35847) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXbIP-0006ol-25 for qemu-devel@nongnu.org; Mon, 05 Dec 2011 11:21:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RXbIN-0008SQ-2x for qemu-devel@nongnu.org; Mon, 05 Dec 2011 11:21:36 -0500 Received: from mail-gy0-f173.google.com ([209.85.160.173]:59968) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXbIM-0008Ql-St for qemu-devel@nongnu.org; Mon, 05 Dec 2011 11:21:35 -0500 Received: by mail-gy0-f173.google.com with SMTP id z10so2233252ghb.4 for ; Mon, 05 Dec 2011 08:21:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=lUbUjAQMzP7WZ0mENcSLXj+jAk4CDKvq8+ZOCyGN+ho=; b=UYz/ZvMCd+uAJWIIV+g3olxzlnUPlhhI6fx0/YJ6iP91m4oKlIY6jFZ5VOo8nA/TwS I68objGEWYvRbQPusDG/qrZ9GLGWknFKkBv/RtEPr9lUD2PC443sCkrAgrv0UxEadNoP RdlEQKfu94h8ON+VhkxSu7MtWciG1a1JZXj1g= Received: by 10.50.194.229 with SMTP id hz5mr10844461igc.36.1323102094426; Mon, 05 Dec 2011 08:21:34 -0800 (PST) Received: from explorer.in.ibm.com ([117.192.11.142]) by mx.google.com with ESMTPS id el2sm78245721ibb.10.2011.12.05.08.21.31 (version=SSLv3 cipher=OTHER); Mon, 05 Dec 2011 08:21:33 -0800 (PST) From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, aneesh.kumar@linux.vnet.ibm.com, stefanha@gmail.com Date: Mon, 5 Dec 2011 21:48:43 +0530 Message-Id: <1323101930-27163-7-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1323101930-27163-1-git-send-email-mohan@in.ibm.com> References: <1323101930-27163-1-git-send-email-mohan@in.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.160.173 Cc: "M. Mohan Kumar" Subject: [Qemu-devel] [PATCH V4 06/13] hw/9pfs: Create other filesystem objects 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 interfaces to create filesystem objects like directory, device nodes, symbolic links, links for proxy filesytem driver Signed-off-by: M. Mohan Kumar --- fsdev/virtfs-proxy-helper.c | 131 ++++++++++++++++++++++++++-- hw/9pfs/virtio-9p-proxy.c | 203 +++++++++++++++++++++++++++++++++++++++---- hw/9pfs/virtio-9p-proxy.h | 8 ++- 3 files changed, 318 insertions(+), 24 deletions(-) diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c index 241fcff..729c79c 100644 --- a/fsdev/virtfs-proxy-helper.c +++ b/fsdev/virtfs-proxy-helper.c @@ -243,6 +243,30 @@ static int send_fd(int sockfd, int fd) return 0; } +static int send_status(int sockfd, struct iovec *iovec, int status) +{ + ProxyHeader header; + int retval, msg_size;; + + if (status < 0) { + header.type = T_ERROR; + } else { + header.type = T_SUCCESS; + } + header.size = sizeof(status); + /* + * marshal the return status. We don't check error. + * because we are sure we have enough space for the status + */ + msg_size = proxy_marshal(iovec, 0, "ddd", header.type, + header.size, status); + retval = socket_write(sockfd, iovec->iov_base, msg_size); + if (retval < 0) { + return retval; + } + return 0; +} + /* * from man 7 capabilities, section * Effect of User ID Changes on Capabilities: @@ -274,6 +298,67 @@ static int setfsugid(int uid, int gid) } /* + * create other filesystem objects and send 0 on success + * return -errno on error + */ +static int do_create_others(int type, struct iovec *iovec) +{ + dev_t rdev; + int retval = 0; + int offset = PROXY_HDR_SZ; + V9fsString oldpath, path; + int mode, uid, gid, cur_uid, cur_gid; + + v9fs_string_init(&path); + v9fs_string_init(&oldpath); + cur_uid = geteuid(); + cur_gid = getegid(); + + retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid); + if (retval < 0) { + return retval; + } + offset += retval; + retval = setfsugid(uid, gid); + if (retval < 0) { + retval = -errno; + goto err_out; + } + switch (type) { + case T_MKNOD: + retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev); + if (retval < 0) { + goto err_out; + } + retval = mknod(path.data, mode, rdev); + break; + case T_MKDIR: + retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode); + if (retval < 0) { + goto err_out; + } + retval = mkdir(path.data, mode); + break; + case T_SYMLINK: + retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path); + if (retval < 0) { + goto err_out; + } + retval = symlink(oldpath.data, path.data); + break; + } + if (retval < 0) { + retval = -errno; + } + +err_out: + v9fs_string_free(&path); + v9fs_string_free(&oldpath); + setfsugid(cur_uid, cur_gid); + return retval; +} + +/* * create a file and send fd on success * return -errno on error */ @@ -343,7 +428,8 @@ static void usage(char *prog) basename(prog)); } -static int process_reply(int sock, int type, int retval) +static int process_reply(int sock, int type, + struct iovec *out_iovec, int retval) { switch (type) { case T_OPEN: @@ -352,6 +438,14 @@ static int process_reply(int sock, int type, int retval) return -1; } break; + case T_MKNOD: + case T_MKDIR: + case T_SYMLINK: + case T_LINK: + if (send_status(sock, out_iovec, retval) < 0) { + return -1; + } + break; default: return -1; break; @@ -363,10 +457,14 @@ static int process_requests(int sock) { int retval = 0; ProxyHeader header; - struct iovec in_iovec; + V9fsString oldpath, path; + struct iovec in_iovec, out_iovec; + + in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); + in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; + out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); + out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; - in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); - in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; while (1) { /* * initialize the header type, so that we send @@ -382,7 +480,7 @@ static int process_requests(int sock) * header.type update properly when we reach * here. */ - if (process_reply(sock, header.type, retval) < 0) { + if (process_reply(sock, header.type, &out_iovec, retval) < 0) { goto err_out; } continue; @@ -398,18 +496,37 @@ static int process_requests(int sock) case T_CREATE: retval = do_create(&in_iovec); break; + case T_MKNOD: + case T_MKDIR: + case T_SYMLINK: + retval = do_create_others(header.type, &in_iovec); + break; + case T_LINK: + v9fs_string_init(&path); + v9fs_string_init(&oldpath); + retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, + "ss", &oldpath, &path); + if (retval > 0) { + retval = link(oldpath.data, path.data); + if (retval < 0) { + retval = -errno; + } + } + v9fs_string_free(&oldpath); + v9fs_string_free(&path); + break; default: goto err_out; break; } - if (process_reply(sock, header.type, retval) < 0) { + if (process_reply(sock, header.type, &out_iovec, retval) < 0) { goto err_out; } } - (void)socket_write; err_out: g_free(in_iovec.iov_base); + g_free(out_iovec.iov_base); return -1; } diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c index 6a74666..da6d7f9 100644 --- a/hw/9pfs/virtio-9p-proxy.c +++ b/hw/9pfs/virtio-9p-proxy.c @@ -19,7 +19,8 @@ typedef struct V9fsProxy { int sockfd; QemuMutex mutex; - struct iovec iovec; + struct iovec in_iovec; + struct iovec out_iovec; } V9fsProxy; /* @@ -78,6 +79,60 @@ static int v9fs_receivefd(int sockfd, int *status) 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; +} + +/* + * 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 @@ -86,12 +141,13 @@ static int v9fs_receivefd(int sockfd, int *status) static int v9fs_request(V9fsProxy *proxy, int type, void *response, const char *fmt, ...) { + dev_t rdev; va_list ap; int retval = 0; - V9fsString *path; ProxyHeader header; - struct iovec *iovec = NULL; int flags, mode, uid, gid; + V9fsString *path, *oldpath; + struct iovec *iovec = NULL, *reply = NULL; qemu_mutex_lock(&proxy->mutex); @@ -99,8 +155,8 @@ static int v9fs_request(V9fsProxy *proxy, int type, retval = -EIO; goto err_out; } - iovec = &proxy->iovec; - + iovec = &proxy->out_iovec; + reply = &proxy->in_iovec; va_start(ap, fmt); switch (type) { case T_OPEN: @@ -127,6 +183,57 @@ static int v9fs_request(V9fsProxy *proxy, int type, } 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); + header.size = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsdq", + uid, gid, path, mode, rdev); + if (header.size < 0) { + retval = header.size; + break; + } + 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); + header.size = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsd", + uid, gid, path, mode); + if (header.size < 0) { + retval = header.size; + break; + } + 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); + header.size = proxy_marshal(iovec, PROXY_HDR_SZ, "ddss", + uid, gid, oldpath, path); + if (header.size < 0) { + retval = header.size; + break; + } + header.type = T_SYMLINK; + break; + case T_LINK: + oldpath = va_arg(ap, V9fsString *); + path = va_arg(ap, V9fsString *); + header.size = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", + oldpath, path); + if (header.size < 0) { + retval = header.size; + break; + } + header.type = T_LINK; + break; default: error_report("Invalid type %d\n", type); retval = -EINVAL; @@ -158,6 +265,14 @@ static int v9fs_request(V9fsProxy *proxy, int type, goto close_error; } break; + case T_MKNOD: + case T_MKDIR: + case T_SYMLINK: + case T_LINK: + if (v9fs_receive_status(proxy, reply, &retval) < 0) { + goto close_error; + } + break; } err_out: @@ -303,15 +418,41 @@ static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, FsCred *credp) { - errno = EOPNOTSUPP; - return -1; + 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) { - errno = EOPNOTSUPP; - return -1; + 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, @@ -346,19 +487,46 @@ static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, return fs->fd; } - static int proxy_symlink(FsContext *fs_ctx, const char *oldpath, V9fsPath *dir_path, const char *name, FsCred *credp) { - errno = EOPNOTSUPP; - return -1; + 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) { - errno = EOPNOTSUPP; - return -1; + 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) @@ -516,8 +684,11 @@ static int proxy_init(FsContext *ctx) } g_free(ctx->fs_root); - proxy->iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); - proxy->iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; + 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); diff --git a/hw/9pfs/virtio-9p-proxy.h b/hw/9pfs/virtio-9p-proxy.h index b8719ea..86c7b4b 100644 --- a/hw/9pfs/virtio-9p-proxy.h +++ b/hw/9pfs/virtio-9p-proxy.h @@ -37,8 +37,14 @@ typedef struct { #define PROXY_HDR_SZ (sizeof(ProxyHeader)) enum { - T_OPEN = 1, + T_SUCCESS = 0, + T_ERROR, + T_OPEN, T_CREATE, + T_MKNOD, + T_MKDIR, + T_SYMLINK, + T_LINK, }; #endif