From patchwork Tue Mar 16 09:15:02 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 47818 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9883EB7D61 for ; Tue, 16 Mar 2010 20:32:36 +1100 (EST) Received: from localhost ([127.0.0.1]:54380 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NrT7P-0005z6-FI for incoming@patchwork.ozlabs.org; Tue, 16 Mar 2010 05:31:19 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NrSsN-0001qH-Ho for qemu-devel@nongnu.org; Tue, 16 Mar 2010 05:15:47 -0400 Received: from [199.232.76.173] (port=39539 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NrSsM-0001pt-T2 for qemu-devel@nongnu.org; Tue, 16 Mar 2010 05:15:46 -0400 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NrSsL-00006K-8D for qemu-devel@nongnu.org; Tue, 16 Mar 2010 05:15:46 -0400 Received: from mx20.gnu.org ([199.232.41.8]:47213) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NrSsK-0008Tt-Kp for qemu-devel@nongnu.org; Tue, 16 Mar 2010 05:15:44 -0400 Received: from e23smtp04.au.ibm.com ([202.81.31.146]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NrSsI-0001X3-Er for qemu-devel@nongnu.org; Tue, 16 Mar 2010 05:15:43 -0400 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [202.81.31.245]) by e23smtp04.au.ibm.com (8.14.3/8.13.1) with ESMTP id o2G9BsN1006536 for ; Tue, 16 Mar 2010 20:11:54 +1100 Received: from d23av02.au.ibm.com (d23av02.au.ibm.com [9.190.235.138]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o2G9Fb6h1687804 for ; Tue, 16 Mar 2010 20:15:37 +1100 Received: from d23av02.au.ibm.com (loopback [127.0.0.1]) by d23av02.au.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o2G9Fa16005655 for ; Tue, 16 Mar 2010 20:15:37 +1100 Received: from localhost.localdomain ([9.124.214.164]) by d23av02.au.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o2G9FRvB005290; Tue, 16 Mar 2010 20:15:35 +1100 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Tue, 16 Mar 2010 14:45:02 +0530 Message-Id: <1268730920-14584-5-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.2.273.gc2413 In-Reply-To: <1268730920-14584-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1268730920-14584-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by mx20.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, "Aneesh Kumar K.V" , Gautham R Shenoy Subject: [Qemu-devel] [PATCH -v2 04/22] virtio-9p: Implement P9_TSTAT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Anthony Liguori This get the mount to work on the guest [kiran@linux.vnet.ibm.com: malloc to qemu_malloc conversion] Signed-off-by: Anthony Liguori Signed-off-by: Gautham R Shenoy Signed-off-by: Aneesh Kumar K.V --- hw/virtio-9p-local.c | 7 ++ hw/virtio-9p.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 174 insertions(+), 2 deletions(-) diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 1d2523b..aefb5a8 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -72,9 +72,16 @@ static int local_setuid(void *opaque, uid_t uid) return 0; } +static ssize_t local_readlink(void *opaque, const char *path, + char *buf, size_t bufsz) +{ + return readlink(rpath(path), buf, bufsz); +} + static V9fsPosixFileOperations ops = { .lstat = local_lstat, .setuid = local_setuid, + .readlink = local_readlink, }; V9fsPosixFileOperations *virtio_9p_init_local(const char *path) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index fdff589..de5f6b0 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -102,6 +102,21 @@ static int posix_setuid(V9fsState *s, uid_t uid) return s->ops->setuid(s->ops->opaque, uid); } +static ssize_t posix_readlink(V9fsState *s, V9fsString *path, V9fsString *buf) +{ + ssize_t len; + + buf->data = qemu_malloc(1024); + + len = s->ops->readlink(s->ops->opaque, path->data, buf->data, 1024 - 1); + if (len > -1) { + buf->size = len; + buf->data[len] = 0; + } + + return len; +} + static void v9fs_string_free(V9fsString *str) { qemu_free(str->data); @@ -109,6 +124,11 @@ static void v9fs_string_free(V9fsString *str) str->size = 0; } +static void v9fs_string_null(V9fsString *str) +{ + v9fs_string_free(str); +} + static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) { va_list ap; @@ -124,6 +144,11 @@ static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) str->size = err; } +static size_t v9fs_string_size(V9fsString *str) +{ + return str->size; +} + static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid) { V9fsFidState *f; @@ -437,6 +462,15 @@ static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) return offset - old_offset; } +static void v9fs_stat_free(V9fsStat *stat) +{ + v9fs_string_free(&stat->name); + v9fs_string_free(&stat->uid); + v9fs_string_free(&stat->gid); + v9fs_string_free(&stat->muid); + v9fs_string_free(&stat->extension); +} + static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len) { int8_t id = pdu->id + 1; /* Response */ @@ -472,6 +506,88 @@ static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len) free_pdu(s, pdu); } +static uint32_t stat_to_v9mode(const struct stat *stbuf) +{ + uint32_t mode; + + mode = stbuf->st_mode & 0777; + if (S_ISDIR(stbuf->st_mode)) + mode |= P9_STAT_MODE_DIR; + + if (dotu) { + if (S_ISLNK(stbuf->st_mode)) + mode |= P9_STAT_MODE_SYMLINK; + if (S_ISSOCK(stbuf->st_mode)) + mode |= P9_STAT_MODE_SOCKET; + if (S_ISFIFO(stbuf->st_mode)) + mode |= P9_STAT_MODE_NAMED_PIPE; + if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) + mode |= P9_STAT_MODE_DEVICE; + if (stbuf->st_mode & S_ISUID) + mode |= P9_STAT_MODE_SETUID; + if (stbuf->st_mode & S_ISGID) + mode |= P9_STAT_MODE_SETGID; + if (stbuf->st_mode & S_ISVTX) + mode |= P9_STAT_MODE_SETVTX; + } + + return mode; +} + +static void stat_to_v9stat(V9fsState *s, V9fsString *name, + const struct stat *stbuf, + V9fsStat *v9stat) +{ + int err; + const char *str; + + memset(v9stat, 0, sizeof(*v9stat)); + + stat_to_qid(stbuf, &v9stat->qid); + v9stat->mode = stat_to_v9mode(stbuf); + v9stat->atime = stbuf->st_atime; + v9stat->mtime = stbuf->st_mtime; + v9stat->length = stbuf->st_size; + + v9fs_string_null(&v9stat->uid); + v9fs_string_null(&v9stat->gid); + v9fs_string_null(&v9stat->muid); + + if (dotu) { + v9stat->n_uid = stbuf->st_uid; + v9stat->n_gid = stbuf->st_gid; + v9stat->n_muid = 0; + + v9fs_string_null(&v9stat->extension); + + if (v9stat->mode & P9_STAT_MODE_SYMLINK) { + err = posix_readlink(s, name, &v9stat->extension); + BUG_ON(err == -1); + v9stat->extension.data[err] = 0; + v9stat->extension.size = err; + } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { + v9fs_string_sprintf(&v9stat->extension, "%c %u %u", + S_ISCHR(stbuf->st_mode) ? 'c' : 'b', + major(stbuf->st_rdev), minor(stbuf->st_rdev)); + } + } + + str = strrchr(name->data, '/'); + if (str) + str += 1; + else + str = name->data; + + v9fs_string_sprintf(&v9stat->name, "%s", str); + + v9stat->size = 61 + + v9fs_string_size(&v9stat->name) + + v9fs_string_size(&v9stat->uid) + + v9fs_string_size(&v9stat->gid) + + v9fs_string_size(&v9stat->muid) + + v9fs_string_size(&v9stat->extension); +} + static void v9fs_version(V9fsState *s, V9fsPDU *pdu) { int32_t msize; @@ -518,10 +634,59 @@ out: v9fs_string_free(&aname); } +typedef struct V9fsStatState { + V9fsPDU *pdu; + size_t offset; + int32_t fid; + V9fsStat v9stat; + V9fsFidState *fidp; + struct stat stbuf; +} V9fsStatState; + +static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err) +{ + if (err == -1) { + err = -errno; + goto out; + } + + stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat); + vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat); + err = vs->offset; + +out: + complete_pdu(s, vs->pdu, err); + v9fs_stat_free(&vs->v9stat); + qemu_free(vs); +} + static void v9fs_stat(V9fsState *s, V9fsPDU *pdu) { - if (debug_9p_pdu) - pprint_pdu(pdu); + V9fsStatState *vs; + ssize_t err = 0; + + vs = qemu_malloc(sizeof(*vs)); + vs->pdu = pdu; + vs->offset = 7; + + memset(&vs->v9stat, 0, sizeof(vs->v9stat)); + + pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid); + + vs->fidp = lookup_fid(s, vs->fid); + if (vs->fidp == NULL) { + err = -ENOENT; + goto out; + } + + err = posix_lstat(s, &vs->fidp->path, &vs->stbuf); + v9fs_stat_post_lstat(s, vs, err); + return; + +out: + complete_pdu(s, vs->pdu, err); + v9fs_stat_free(&vs->v9stat); + qemu_free(vs); } static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)