From patchwork Tue May 24 12:21: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: 97156 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 12E3AB6FA7 for ; Tue, 24 May 2011 22:22:21 +1000 (EST) Received: from localhost ([::1]:37613 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOqcs-00087s-6K for incoming@patchwork.ozlabs.org; Tue, 24 May 2011 08:22:18 -0400 Received: from eggs.gnu.org ([140.186.70.92]:36365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOqcg-00087V-5o for qemu-devel@nongnu.org; Tue, 24 May 2011 08:22:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QOqcI-0003CQ-T4 for qemu-devel@nongnu.org; Tue, 24 May 2011 08:22:06 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:57128) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOqcH-0003Be-Oi for qemu-devel@nongnu.org; Tue, 24 May 2011 08:21:42 -0400 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp07.in.ibm.com (8.14.4/8.13.1) with ESMTP id p4OCLYAR025052 for ; Tue, 24 May 2011 17:51:34 +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 p4OCLYLw4391164 for ; Tue, 24 May 2011 17:51:34 +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 p4OCLX9K014417 for ; Tue, 24 May 2011 22:21:34 +1000 Received: from explorer.in.ibm.com ([9.122.21.132]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p4OCLXYB014408; Tue, 24 May 2011 22:21:33 +1000 From: "M. Mohan Kumar" To: qemu-devel@nongnu.org Date: Tue, 24 May 2011 17:51:33 +0530 Message-Id: <1306239693-6606-1-git-send-email-mohan@in.ibm.com> X-Mailer: git-send-email 1.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 122.248.162.7 Cc: stefanha@gmail.com, rlandley@parallels.com Subject: [Qemu-devel] [PATCH V3] virtio-9p: Add 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=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 Reviewed-by: Stefan Hajnoczi --- 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 | 1 + fsdev/qemu-fsdev.c | 11 ++++++++++- fsdev/qemu-fsdev.h | 3 +++ hw/9pfs/virtio-9p-device.c | 5 +++++ hw/9pfs/virtio-9p.c | 41 +++++++++++++++++++++++++++++++++++++++++ qemu-config.c | 7 +++++++ vl.c | 1 + 7 files changed, 68 insertions(+), 1 deletions(-) diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h index af9daf7..b1642eb 100644 --- a/fsdev/file-op-9p.h +++ b/fsdev/file-op-9p.h @@ -55,6 +55,7 @@ typedef struct FsContext SecModel fs_sm; uid_t uid; struct xattr_operations **xops; + int flags; } FsContext; void cred_init(FsCred *); diff --git a/fsdev/qemu-fsdev.c b/fsdev/qemu-fsdev.c index 0b33290..2be732f 100644 --- a/fsdev/qemu-fsdev.c +++ b/fsdev/qemu-fsdev.c @@ -28,11 +28,12 @@ static FsTypeTable FsTypes[] = { int qemu_fsdev_add(QemuOpts *opts) { struct FsTypeListEntry *fsle; - int i; + int i, flags; const char *fsdev_id = qemu_opts_id(opts); const char *fstype = qemu_opt_get(opts, "fstype"); const char *path = qemu_opt_get(opts, "path"); const char *sec_model = qemu_opt_get(opts, "security_model"); + int readonly = qemu_opt_get_bool(opts, "readonly", 0); if (!fsdev_id) { fprintf(stderr, "fsdev: No id specified\n"); @@ -65,12 +66,20 @@ int qemu_fsdev_add(QemuOpts *opts) return -1; } + flags = 0; + if (readonly) { + flags |= MS_RDONLY; + } else { + flags &= ~MS_RDONLY; + } + fsle = qemu_malloc(sizeof(*fsle)); fsle->fse.fsdev_id = qemu_strdup(fsdev_id); fsle->fse.path = qemu_strdup(path); fsle->fse.security_model = qemu_strdup(sec_model); fsle->fse.ops = FsTypes[i].ops; + fsle->fse.flags = flags; QTAILQ_INSERT_TAIL(&fstype_entries, fsle, next); return 0; diff --git a/fsdev/qemu-fsdev.h b/fsdev/qemu-fsdev.h index f9f08d3..4c66ce6 100644 --- a/fsdev/qemu-fsdev.h +++ b/fsdev/qemu-fsdev.h @@ -42,6 +42,7 @@ typedef struct FsTypeEntry { char *path; char *security_model; FileOperations *ops; + int flags; } FsTypeEntry; typedef struct FsTypeListEntry { @@ -52,4 +53,6 @@ typedef struct FsTypeListEntry { int qemu_fsdev_add(QemuOpts *opts); FsTypeEntry *get_fsdev_fsentry(char *id); extern FileOperations local_ops; + +#define MS_RDONLY 0x01 #endif diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c index f985486..4de32e4 100644 --- a/hw/9pfs/virtio-9p-device.c +++ b/hw/9pfs/virtio-9p-device.c @@ -107,6 +107,11 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) s->ctx.xops = none_xattr_ops; } + s->ctx.flags = 0; + if (fse->flags & MS_RDONLY) { + s->ctx.flags |= MS_RDONLY; + } + if (lstat(fse->path, &stat)) { fprintf(stderr, "share path %s does not exist\n", fse->path); exit(1); diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 21865b1..ceb4034 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -1192,6 +1192,11 @@ static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len) v9fs_string_free(&str); } +static inline bool is_ro_export(FsContext *fs_ctx) +{ + return fs_ctx->flags & MS_RDONLY; +} + static void v9fs_version(V9fsState *s, V9fsPDU *pdu) { V9fsString version; @@ -1734,6 +1739,11 @@ static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err) vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path); v9fs_open_post_opendir(s, vs, err); } else { + if (is_ro_export(&s->ctx) && + (vs->mode & O_WRONLY || vs->mode & O_RDWR || vs->mode & O_APPEND)) { + err = -EROFS; + goto out; + } if (s->proto_version == V9FS_PROTO_2000L) { flags = vs->mode; flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); @@ -3606,6 +3616,33 @@ static pdu_handler_t *pdu_handlers[] = { [P9_TREMOVE] = v9fs_remove, }; +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) { pdu_handler_t *handler; @@ -3619,6 +3656,10 @@ static void submit_pdu(V9fsState *s, V9fsPDU *pdu) handler = pdu_handlers[pdu->id]; BUG_ON(handler == NULL); + if (is_ro_export(&s->ctx) && !is_read_only_op(pdu->id)) { + complete_pdu(s, pdu, -EROFS); + return; + } handler(s, pdu); } diff --git a/qemu-config.c b/qemu-config.c index 14d3419..25a413b 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -170,7 +170,11 @@ QemuOptsList qemu_fsdev_opts = { }, { .name = "security_model", .type = QEMU_OPT_STRING, + }, { + .name = "readonly", + .type = QEMU_OPT_BOOL, }, + { /*End of list */ } }, }; @@ -192,6 +196,9 @@ QemuOptsList qemu_virtfs_opts = { }, { .name = "security_model", .type = QEMU_OPT_STRING, + }, { + .name = "readonly", + .type = QEMU_OPT_BOOL, }, { /*End of list */ } diff --git a/vl.c b/vl.c index 6b9a2f6..70be0d1 100644 --- a/vl.c +++ b/vl.c @@ -2483,6 +2483,7 @@ int main(int argc, char **argv, char **envp) qemu_opt_set(fsdev, "security_model", qemu_opt_get(opts, "security_model")); + qemu_opt_set(fsdev, "readonly", qemu_opt_get(opts, "readonly")); device = qemu_opts_create(qemu_find_opts("device"), NULL, 0); qemu_opt_set(device, "driver", "virtio-9p-pci"); qemu_opt_set(device, "fsdev",