From patchwork Tue Oct 25 07:56:25 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: 121505 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 C78B7B6FD7 for ; Tue, 25 Oct 2011 18:57:04 +1100 (EST) Received: from localhost ([::1]:41187 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RIbsa-0004hL-6X for incoming@patchwork.ozlabs.org; Tue, 25 Oct 2011 03:57:00 -0400 Received: from eggs.gnu.org ([140.186.70.92]:55217) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RIbsM-0004fm-UV for qemu-devel@nongnu.org; Tue, 25 Oct 2011 03:56:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RIbsI-0000K7-Lh for qemu-devel@nongnu.org; Tue, 25 Oct 2011 03:56:46 -0400 Received: from e28smtp03.in.ibm.com ([122.248.162.3]:53684) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RIbsG-0000JW-H3 for qemu-devel@nongnu.org; Tue, 25 Oct 2011 03:56:42 -0400 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp03.in.ibm.com (8.14.4/8.13.1) with ESMTP id p9P7uWXe001246 for ; Tue, 25 Oct 2011 13:26:32 +0530 Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9P7uVsd4481072 for ; Tue, 25 Oct 2011 13:26:31 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9P7uVW8028396 for ; Tue, 25 Oct 2011 18:56:31 +1100 Received: from explorer.in.ibm.com ([9.79.176.30]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9P7uSPt028310; Tue, 25 Oct 2011 18:56:30 +1100 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org, "Aneesh Kumar K.V" Date: Tue, 25 Oct 2011 13:26:25 +0530 Message-Id: <1319529385-27212-2-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1319529385-27212-1-git-send-email-mohan@in.ibm.com> References: <1319529385-27212-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: 122.248.162.3 Subject: [Qemu-devel] [V3 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 A new fsdev parameter "readonly" is introduced to control accessing 9p export. "readonly" can be used to specify the access type. By default "rw" access is given to 9p export. Signed-off-by: M. Mohan Kumar --- fsdev/file-op-9p.h | 4 +++- fsdev/qemu-fsdev.c | 7 ++++++- hw/9pfs/virtio-9p.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-config.c | 7 +++++++ qemu-options.hx | 14 ++++++++++---- vl.c | 2 ++ 6 files changed, 78 insertions(+), 6 deletions(-) diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h index 908e2a5..5788ff9 100644 --- a/fsdev/file-op-9p.h +++ b/fsdev/file-op-9p.h @@ -56,10 +56,12 @@ typedef struct extended_ops { * On failure ignore the error. */ #define V9FS_SM_NONE 0x00000010 - +#define V9FS_RDONLY 0x00000020 #define V9FS_SEC_MASK 0x0000001C + + typedef struct FsContext { uid_t uid; diff --git a/fsdev/qemu-fsdev.c b/fsdev/qemu-fsdev.c index 5977bcc..27d10cb 100644 --- a/fsdev/qemu-fsdev.c +++ b/fsdev/qemu-fsdev.c @@ -35,7 +35,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"); @@ -86,6 +86,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 8b6813f..e7618d7 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(FsContext *ctx) +{ + return ctx->export_flags & V9FS_RDONLY; +} + static void v9fs_version(void *opaque) { V9fsPDU *pdu = opaque; @@ -1690,6 +1695,14 @@ static void v9fs_open(void *opaque) } 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; + } + flags |= O_NOATIME; + } err = v9fs_co_open(pdu, fidp, flags); if (err < 0) { goto out; @@ -3309,6 +3322,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(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; @@ -3320,6 +3366,10 @@ static void submit_pdu(V9fsState *s, V9fsPDU *pdu) } 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); } 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/qemu-options.hx b/qemu-options.hx index 5d2a776..1baa381 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -528,12 +528,12 @@ DEFHEADING(File system options:) DEF("fsdev", HAS_ARG, QEMU_OPTION_fsdev, "-fsdev fsdriver,id=id,path=path,[security_model={mapped|passthrough|none}]\n" - " [,writeout=immediate]\n", + " [,writeout=immediate][,readonly]\n", QEMU_ARCH_ALL) STEXI -@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}] +@item -fsdev @var{fsdriver},id=@var{id},path=@var{path},[security_model=@var{security_model}][,writeout=@var{writeout}][,readonly] @findex -fsdev Define a new file system device. Valid options are: @table @option @@ -563,6 +563,9 @@ This is an optional argument. The only supported value is "immediate". This means that host page cache will be used to read and write data but write notification will be sent to the guest only when the data has been reported as written by the storage subsystem. +@item readonly +Enables exporting 9p share as a readonly mount for guests. By default +read-write access is given. @end table -fsdev option is used along with -device driver "virtio-9p-pci". @@ -583,12 +586,12 @@ DEFHEADING(Virtual File system pass-through options:) DEF("virtfs", HAS_ARG, QEMU_OPTION_virtfs, "-virtfs local,path=path,mount_tag=tag,security_model=[mapped|passthrough|none]\n" - " [,writeout=immediate]\n", + " [,writeout=immediate][,readonly]\n", QEMU_ARCH_ALL) STEXI -@item -virtfs @var{fsdriver},path=@var{path},mount_tag=@var{mount_tag},security_model=@var{security_model}[,writeout=@var{writeout}] +@item -virtfs @var{fsdriver},path=@var{path},mount_tag=@var{mount_tag},security_model=@var{security_model}[,writeout=@var{writeout}][,readonly] @findex -virtfs The general form of a Virtual File system pass-through options are: @@ -619,6 +622,9 @@ This is an optional argument. The only supported value is "immediate". This means that host page cache will be used to read and write data but write notification will be sent to the guest only when the data has been reported as written by the storage subsystem. +@item readonly +Enables exporting 9p share as a readonly mount for guests. By default +read-write access is given. @end table ETEXI diff --git a/vl.c b/vl.c index 66f70fb..4a5ac9f 100644 --- a/vl.c +++ b/vl.c @@ -2827,6 +2827,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",