From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NuqAL-00074e-O6 for qemu-devel@nongnu.org; Thu, 25 Mar 2010 12:44:18 -0400 Received: from [140.186.70.92] (port=50390 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NuqAA-0006zR-BH for qemu-devel@nongnu.org; Thu, 25 Mar 2010 12:44:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NuqA0-000536-Q1 for qemu-devel@nongnu.org; Thu, 25 Mar 2010 12:44:06 -0400 Received: from e23smtp02.au.ibm.com ([202.81.31.144]:52062) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Nuq9z-00052W-CQ for qemu-devel@nongnu.org; Thu, 25 Mar 2010 12:43:56 -0400 Received: from d23relay04.au.ibm.com (d23relay04.au.ibm.com [202.81.31.246]) by e23smtp02.au.ibm.com (8.14.3/8.13.1) with ESMTP id o2PGeZk8005362 for ; Fri, 26 Mar 2010 03:40:35 +1100 Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o2PGbq3J1343658 for ; Fri, 26 Mar 2010 03:37:52 +1100 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o2PGhrwQ031051 for ; Fri, 26 Mar 2010 03:43:53 +1100 From: "Aneesh Kumar K.V" Date: Thu, 25 Mar 2010 22:13:12 +0530 Message-Id: <1269535420-31206-5-git-send-email-aneesh.kumar@linux.vnet.ibm.com> In-Reply-To: <1269535420-31206-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1269535420-31206-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH -V3 04/32] virtio-9p: Implement P9_TSTAT List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: ericvh@gmail.com, aliguori@us.ibm.com, "Aneesh Kumar K.V" , Gautham R Shenoy 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) -- 1.7.0.2.323.g0d092