From patchwork Wed Oct 19 17:16:33 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: 120675 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 CD231B71C7 for ; Thu, 20 Oct 2011 04:17:22 +1100 (EST) Received: from localhost ([::1]:58690 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RGZlS-0003rp-Ta for incoming@patchwork.ozlabs.org; Wed, 19 Oct 2011 13:17:14 -0400 Received: from eggs.gnu.org ([140.186.70.92]:50916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RGZlD-0003mI-Py for qemu-devel@nongnu.org; Wed, 19 Oct 2011 13:17:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RGZlB-0006Ge-Jo for qemu-devel@nongnu.org; Wed, 19 Oct 2011 13:16:59 -0400 Received: from e23smtp07.au.ibm.com ([202.81.31.140]:40610) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RGZlA-0006FB-Uf for qemu-devel@nongnu.org; Wed, 19 Oct 2011 13:16:57 -0400 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [202.81.31.245]) by e23smtp07.au.ibm.com (8.14.4/8.13.1) with ESMTP id p9JHGk1p008716 for ; Thu, 20 Oct 2011 04:16:46 +1100 Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9JHGfOq2461774 for ; Thu, 20 Oct 2011 04:16:46 +1100 Received: from d23av01.au.ibm.com (loopback [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9JHGfG3016246 for ; Thu, 20 Oct 2011 04:16:41 +1100 Received: from explorer.in.ibm.com ([9.79.180.0]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9JHGbZw016187; Thu, 20 Oct 2011 04:16:40 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Wed, 19 Oct 2011 22:46:33 +0530 Message-Id: <1319044593-1161-2-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1319044593-1161-1-git-send-email-mohan@in.ibm.com> References: <1319044593-1161-1-git-send-email-mohan@in.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 202.81.31.140 Cc: "M. Mohan Kumar" Subject: [Qemu-devel] [PATCH 2/2] hw/9pfs: Read-only support for 9p export 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" A new fsdev parameter "readonly" is introduced to control accessing 9p export. readonly=on|off can be used to specify the access type. By default rw access is given to 9p export. Signed-off-by: M. Mohan Kumar --- Changes from previous version V4: * Updated on top of current for-upstream branch Changes from previous version V3: * Use opt_set_bool function to set readonly option * Change the flag from MS_READONLY to 9p specific Change from previous version V2: * QEMU_OPT_BOOL is used for readdonly parameter Changes from previous version: * Use "readonly" option instead of "access" * Change function return type to boolean where its needed fsdev/file-op-9p.h | 2 ++ fsdev/qemu-fsdev.c | 7 ++++++- hw/9pfs/virtio-9p.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-config.c | 7 +++++++ vl.c | 2 ++ 5 files changed, 68 insertions(+), 1 deletions(-) diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h index c7b4e38..ba564d4 100644 --- a/fsdev/file-op-9p.h +++ b/fsdev/file-op-9p.h @@ -60,6 +60,8 @@ typedef struct extended_ops { #define V9FS_SEC_MASK 0x0000001C +#define V9FS_RDONLY 0x00000020 + typedef struct FsContext { uid_t uid; diff --git a/fsdev/qemu-fsdev.c b/fsdev/qemu-fsdev.c index e8dc0fd..7fd2aa7 100644 --- a/fsdev/qemu-fsdev.c +++ b/fsdev/qemu-fsdev.c @@ -36,7 +36,7 @@ int qemu_fsdev_add(QemuOpts *opts) const char *path = qemu_opt_get(opts, "path"); const char *sec_model = qemu_opt_get(opts, "security_model"); const char *writeout = qemu_opt_get(opts, "writeout"); - + bool ro = qemu_opt_get_bool(opts, "readonly", 0); if (!fsdev_id) { fprintf(stderr, "fsdev: No id specified\n"); @@ -87,6 +87,11 @@ int qemu_fsdev_add(QemuOpts *opts) fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT; } } + if (ro) { + fsle->fse.export_flags |= V9FS_RDONLY; + } else { + fsle->fse.export_flags &= ~V9FS_RDONLY; + } if (strcmp(fsdriver, "local")) { goto done; diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 1c67bfe..b6770e2 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -1271,6 +1271,11 @@ static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) dst->size++; } +static inline bool is_ro_export(int export_flags) +{ + return export_flags & V9FS_RDONLY; +} + static void v9fs_version(void *opaque) { V9fsPDU *pdu = opaque; @@ -1692,6 +1697,15 @@ static void v9fs_open(void *opaque) } else { flags = omode_to_uflags(mode); } + if (is_ro_export(s->ctx.export_flags)) { + if (mode & O_WRONLY || mode & O_RDWR || + mode & O_APPEND || mode & O_TRUNC) { + err = -EROFS; + goto out; + } else { + flags |= O_NOATIME; + } + } err = v9fs_co_open(pdu, fidp, flags); if (err < 0) { goto out; @@ -3311,6 +3325,39 @@ static void v9fs_op_not_supp(void *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(int id) +{ + switch (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; @@ -3322,6 +3369,10 @@ static void submit_pdu(V9fsState *s, V9fsPDU *pdu) } else { handler = pdu_co_handlers[pdu->id]; } + + if (is_ro_export(s->ctx.export_flags) && !is_read_only_op(pdu->id)) { + handler = v9fs_fs_ro; + } co = qemu_coroutine_create(handler); qemu_coroutine_enter(co, pdu); } diff --git a/qemu-config.c b/qemu-config.c index 90b6b3e..597d7e1 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -180,7 +180,11 @@ QemuOptsList qemu_fsdev_opts = { }, { .name = "writeout", .type = QEMU_OPT_STRING, + }, { + .name = "readonly", + .type = QEMU_OPT_BOOL, }, + { /*End of list */ } }, }; @@ -205,6 +209,9 @@ QemuOptsList qemu_virtfs_opts = { }, { .name = "writeout", .type = QEMU_OPT_STRING, + }, { + .name = "readonly", + .type = QEMU_OPT_BOOL, }, { /*End of list */ } diff --git a/vl.c b/vl.c index c9de124..cf66ef9 100644 --- a/vl.c +++ b/vl.c @@ -2823,6 +2823,8 @@ int main(int argc, char **argv, char **envp) qemu_opt_set(fsdev, "security_model", qemu_opt_get(opts, "security_model")); + qemu_opt_set_bool(fsdev, "readonly", + qemu_opt_get_bool(opts, "readonly", 0)); device = qemu_opts_create(qemu_find_opts("device"), NULL, 0); qemu_opt_set(device, "driver", "virtio-9p-pci"); qemu_opt_set(device, "fsdev",