From patchwork Thu Apr 15 14:10:56 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: 50256 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 CFFC5B7C33 for ; Fri, 16 Apr 2010 00:45:29 +1000 (EST) Received: from localhost ([127.0.0.1]:44416 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2QJq-0004un-Qw for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 10:45:26 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2PtY-0003XT-0R for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:16 -0400 Received: from [140.186.70.92] (port=50519 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtQ-0003RM-4w for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pmv-0000Ge-4D for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:41 -0400 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:57178) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pmu-0000Fv-8Z for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:24 -0400 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by e28smtp02.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FEBLZd032157 for ; Thu, 15 Apr 2010 19:41:21 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBLtr1994976 for ; Thu, 15 Apr 2010 19:41:21 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3FEBK0Z030508 for ; Thu, 15 Apr 2010 19:41:21 +0530 Received: from skywalker.in.ibm.com ([9.77.204.187]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3FEBDaA030329; Thu, 15 Apr 2010 19:41:20 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:40:56 +0530 Message-Id: <1271340671-19558-7-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V5 06/21] virtio-9p: Add fid and qid management support. 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 Helper APIs for FID and QID management. Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/virtio-9p.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 123 insertions(+), 0 deletions(-) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 528c9cc..7db157a 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -176,6 +176,126 @@ static size_t v9fs_string_size(V9fsString *str) return str->size; } +static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid) +{ + V9fsFidState *f; + + for (f = s->fid_list; f; f = f->next) { + if (f->fid == fid) { + v9fs_do_setuid(s, f->uid); + return f; + } + } + + return NULL; +} + +static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) +{ + V9fsFidState *f; + + f = lookup_fid(s, fid); + if (f) { + return NULL; + } + + f = qemu_mallocz(sizeof(V9fsFidState)); + + f->fid = fid; + f->fd = -1; + f->dir = NULL; + + f->next = s->fid_list; + s->fid_list = f; + + return f; +} + +static int free_fid(V9fsState *s, int32_t fid) +{ + V9fsFidState **fidpp, *fidp; + + for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) { + if ((*fidpp)->fid == fid) { + break; + } + } + + if (*fidpp == NULL) { + return -ENOENT; + } + + fidp = *fidpp; + *fidpp = fidp->next; + + if (fidp->fd != -1) { + v9fs_do_close(s, fidp->fd); + } + if (fidp->dir) { + v9fs_do_closedir(s, fidp->dir); + } + v9fs_string_free(&fidp->path); + qemu_free(fidp); + + return 0; +} + +#define P9_QID_TYPE_DIR 0x80 +#define P9_QID_TYPE_SYMLINK 0x02 + +#define P9_STAT_MODE_DIR 0x80000000 +#define P9_STAT_MODE_APPEND 0x40000000 +#define P9_STAT_MODE_EXCL 0x20000000 +#define P9_STAT_MODE_MOUNT 0x10000000 +#define P9_STAT_MODE_AUTH 0x08000000 +#define P9_STAT_MODE_TMP 0x04000000 +#define P9_STAT_MODE_SYMLINK 0x02000000 +#define P9_STAT_MODE_LINK 0x01000000 +#define P9_STAT_MODE_DEVICE 0x00800000 +#define P9_STAT_MODE_NAMED_PIPE 0x00200000 +#define P9_STAT_MODE_SOCKET 0x00100000 +#define P9_STAT_MODE_SETUID 0x00080000 +#define P9_STAT_MODE_SETGID 0x00040000 +#define P9_STAT_MODE_SETVTX 0x00010000 + +#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ + P9_STAT_MODE_SYMLINK | \ + P9_STAT_MODE_LINK | \ + P9_STAT_MODE_DEVICE | \ + P9_STAT_MODE_NAMED_PIPE | \ + P9_STAT_MODE_SOCKET) + +/* This is the algorithm from ufs in spfs */ +static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp) +{ + size_t size; + + size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); + memcpy(&qidp->path, &stbuf->st_ino, size); + qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); + qidp->type = 0; + if (S_ISDIR(stbuf->st_mode)) { + qidp->type |= P9_QID_TYPE_DIR; + } + if (S_ISLNK(stbuf->st_mode)) { + qidp->type |= P9_QID_TYPE_SYMLINK; + } +} + +static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp) +{ + struct stat stbuf; + int err; + + err = v9fs_do_lstat(s, &fidp->path, &stbuf); + if (err) { + return err; + } + + stat_to_qid(&stbuf, qidp); + return 0; +} + static V9fsPDU *alloc_pdu(V9fsState *s) { V9fsPDU *pdu = NULL; @@ -479,6 +599,9 @@ static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu) (void) v9fs_do_readlink; (void) v9fs_do_close; (void) v9fs_do_closedir; + (void) alloc_fid; + (void) free_fid; + (void) fid_to_qid; } static void v9fs_version(V9fsState *s, V9fsPDU *pdu)