From patchwork Sun Aug 29 19:02:41 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jvrao X-Patchwork-Id: 62953 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 BE356B70EF for ; Mon, 30 Aug 2010 05:02:49 +1000 (EST) Received: from localhost ([127.0.0.1]:60344 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Opn7S-0005TA-IA for incoming@patchwork.ozlabs.org; Sun, 29 Aug 2010 15:00:42 -0400 Received: from [140.186.70.92] (port=51353 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Opn00-00028t-2c for qemu-devel@nongnu.org; Sun, 29 Aug 2010 14:53:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Opmzx-0001b3-Lc for qemu-devel@nongnu.org; Sun, 29 Aug 2010 14:52:59 -0400 Received: from e7.ny.us.ibm.com ([32.97.182.137]:53825) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Opmzx-0001ar-Ib for qemu-devel@nongnu.org; Sun, 29 Aug 2010 14:52:57 -0400 Received: from d01relay02.pok.ibm.com (d01relay02.pok.ibm.com [9.56.227.234]) by e7.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id o7TIcqZY005140 for ; Sun, 29 Aug 2010 14:38:52 -0400 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d01relay02.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o7TIqufd427254 for ; Sun, 29 Aug 2010 14:52:56 -0400 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id o7TIquAQ004166 for ; Sun, 29 Aug 2010 12:52:56 -0600 Received: from localhost.localdomain (elm9m80.beaverton.ibm.com [9.47.81.80]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id o7TIqkJX003851; Sun, 29 Aug 2010 12:52:55 -0600 From: "Venkateswararao Jujjuri (JV)" To: qemu-devel@nongnu.org Date: Sun, 29 Aug 2010 12:02:41 -0700 Message-Id: <1283108572-20228-16-git-send-email-jvrao@linux.vnet.ibm.com> X-Mailer: git-send-email 1.6.0.6 In-Reply-To: <1283108572-20228-1-git-send-email-jvrao@linux.vnet.ibm.com> References: <1283108572-20228-1-git-send-email-jvrao@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: aliguori@us.ibm.com, "M. Mohan Kumar" , Venkateswararao Jujjuri Subject: [Qemu-devel] [PATCH -V4 15/26] rename - change name of file or directory 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: M. Mohan Kumar size[4] Trename tag[2] fid[4] newdirfid[4] name[s] size[4] Rrename tag[2] Implement the 2000.L rename operation. A new function v9fs_complete_rename is introduced that acts as a common entry point for 2000.L rename operation and 2000.U rename opearation (via wstat). As part of this change the field 'nname' (used only for rename) is removed from the structure V9fsWstatState. Instead a new structure V9fsRenameState is used for rename operations both by 2000.U and 2000.L code paths. Both 2000.U and 2000.L rename code paths construct the V9fsRenameState structure and passes that to v9fs_complete_rename function. Changes from previous version: Use qemu_mallocz to initialize Use strcpy,strcat functions instead of memcpy Changed the variable name to newdirfid Introduced post rename function Error checking Removed nname field from V9fsWstatState Signed-off-by: M. Mohan Kumar Signed-off-by: Venkateswararao Jujjuri --- hw/virtio-9p.c | 156 ++++++++++++++++++++++++++++++++++++++++--------------- hw/virtio-9p.h | 11 ++++- 2 files changed, 123 insertions(+), 44 deletions(-) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 7ea0e96..88dd496 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -2563,11 +2563,6 @@ static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err) if (err < 0) { goto out; } - - if (vs->v9stat.name.size != 0) { - v9fs_string_free(&vs->nname); - } - if (vs->v9stat.length != -1) { if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) { err = -errno; @@ -2582,17 +2577,30 @@ out: qemu_free(vs); } -static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err) +static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs) { - V9fsFidState *fidp; - if (err < 0) { - goto out; - } + int err = 0; + char *old_name, *new_name; + char *end; - if (vs->v9stat.name.size != 0) { - char *old_name, *new_name; - char *end; + if (vs->newdirfid != -1) { + V9fsFidState *dirfidp; + dirfidp = lookup_fid(s, vs->newdirfid); + + if (dirfidp == NULL) { + err = -ENOENT; + goto out; + } + + BUG_ON(dirfidp->fd != -1); + BUG_ON(dirfidp->dir); + new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2); + + strcpy(new_name, dirfidp->path.data); + strcat(new_name, "/"); + strcat(new_name + dirfidp->path.size, vs->name.data); + } else { old_name = vs->fidp->path.data; end = strrchr(old_name, '/'); if (end) { @@ -2600,43 +2608,75 @@ static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err) } else { end = old_name; } + new_name = qemu_mallocz(end - old_name + vs->name.size + 1); - new_name = qemu_mallocz(end - old_name + vs->v9stat.name.size + 1); + strncat(new_name, old_name, end - old_name); + strncat(new_name + (end - old_name), vs->name.data, vs->name.size); + } - memcpy(new_name, old_name, end - old_name); - memcpy(new_name + (end - old_name), vs->v9stat.name.data, - vs->v9stat.name.size); - vs->nname.data = new_name; - vs->nname.size = strlen(new_name); + v9fs_string_free(&vs->name); + vs->name.data = qemu_strdup(new_name); + vs->name.size = strlen(new_name); - if (strcmp(new_name, vs->fidp->path.data) != 0) { - if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) { - err = -errno; - } else { - /* - * Fixup fid's pointing to the old name to - * start pointing to the new name - */ - for (fidp = s->fid_list; fidp; fidp = fidp->next) { - - if (vs->fidp == fidp) { - /* - * we replace name of this fid towards the end - * so that our below strcmp will work - */ - continue; - } - if (!strncmp(vs->fidp->path.data, fidp->path.data, - strlen(vs->fidp->path.data))) { - /* replace the name */ - v9fs_fix_path(&fidp->path, &vs->nname, - strlen(vs->fidp->path.data)); - } + if (strcmp(new_name, vs->fidp->path.data) != 0) { + if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) { + err = -errno; + } else { + V9fsFidState *fidp; + /* + * Fixup fid's pointing to the old name to + * start pointing to the new name + */ + for (fidp = s->fid_list; fidp; fidp = fidp->next) { + if (vs->fidp == fidp) { + /* + * we replace name of this fid towards the end + * so that our below strcmp will work + */ + continue; + } + if (!strncmp(vs->fidp->path.data, fidp->path.data, + strlen(vs->fidp->path.data))) { + /* replace the name */ + v9fs_fix_path(&fidp->path, &vs->name, + strlen(vs->fidp->path.data)); } - v9fs_string_copy(&vs->fidp->path, &vs->nname); } + v9fs_string_copy(&vs->fidp->path, &vs->name); } } +out: + v9fs_string_free(&vs->name); + return err; +} + +static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err) +{ + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + +static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err) +{ + if (err < 0) { + goto out; + } + + if (vs->v9stat.name.size != 0) { + V9fsRenameState *vr; + + vr = qemu_malloc(sizeof(V9fsRenameState)); + memset(vr, sizeof(*vr), 0); + vr->newdirfid = -1; + vr->pdu = vs->pdu; + vr->fidp = vs->fidp; + vr->offset = vs->offset; + vr->name.size = vs->v9stat.name.size; + vr->name.data = qemu_strdup(vs->v9stat.name.data); + + err = v9fs_complete_rename(s, vr); + qemu_free(vr); + } v9fs_wstat_post_rename(s, vs, err); return; @@ -2646,6 +2686,35 @@ out: qemu_free(vs); } +static void v9fs_rename(V9fsState *s, V9fsPDU *pdu) +{ + int32_t fid; + V9fsRenameState *vs; + ssize_t err = 0; + + vs = qemu_malloc(sizeof(*vs)); + vs->pdu = pdu; + vs->offset = 7; + + pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name); + + vs->fidp = lookup_fid(s, fid); + if (vs->fidp == NULL) { + err = -ENOENT; + goto out; + } + + BUG_ON(vs->fidp->fd != -1); + BUG_ON(vs->fidp->dir); + + err = v9fs_complete_rename(s, vs); + v9fs_rename_post_rename(s, vs, err); + return; +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err) { if (err < 0) { @@ -3004,6 +3073,7 @@ static pdu_handler_t *pdu_handlers[] = { [P9_TGETATTR] = v9fs_getattr, [P9_TSETATTR] = v9fs_setattr, [P9_TMKNOD] = v9fs_mknod, + [P9_TRENAME] = v9fs_rename, [P9_TMKDIR] = v9fs_mkdir, [P9_TVERSION] = v9fs_version, [P9_TATTACH] = v9fs_attach, diff --git a/hw/virtio-9p.h b/hw/virtio-9p.h index 91cc0ae..4d179b7 100644 --- a/hw/virtio-9p.h +++ b/hw/virtio-9p.h @@ -21,6 +21,8 @@ enum { P9_RSYMLINK, P9_TMKNOD = 18, P9_RMKNOD, + P9_TRENAME = 20, + P9_RRENAME, P9_TGETATTR = 24, P9_RGETATTR, P9_TSETATTR = 26, @@ -310,7 +312,6 @@ typedef struct V9fsWstatState V9fsStat v9stat; V9fsFidState *fidp; struct stat stbuf; - V9fsString nname; } V9fsWstatState; typedef struct V9fsSymlinkState @@ -385,6 +386,14 @@ typedef struct V9fsMkState { V9fsString fullname; } V9fsMkState; +typedef struct V9fsRenameState { + V9fsPDU *pdu; + V9fsFidState *fidp; + size_t offset; + int32_t newdirfid; + V9fsString name; +} V9fsRenameState; + extern size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count, size_t offset, size_t size, int pack);