From patchwork Mon Jun 28 21:55:12 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jvrao X-Patchwork-Id: 57213 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 930F0B6EE8 for ; Tue, 29 Jun 2010 08:24:47 +1000 (EST) Received: from localhost ([127.0.0.1]:39528 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OTMku-0000T3-JV for incoming@patchwork.ozlabs.org; Mon, 28 Jun 2010 18:24:44 -0400 Received: from [140.186.70.92] (port=59217 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OTMFG-0005Br-Tv for qemu-devel@nongnu.org; Mon, 28 Jun 2010 17:52:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OTMF7-0006rt-On for qemu-devel@nongnu.org; Mon, 28 Jun 2010 17:52:02 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:39943) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OTMF7-0006rS-Fg for qemu-devel@nongnu.org; Mon, 28 Jun 2010 17:51:53 -0400 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by e34.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id o5SLhk3J013742 for ; Mon, 28 Jun 2010 15:43:46 -0600 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o5SLpjqV140990 for ; Mon, 28 Jun 2010 15:51:48 -0600 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 o5SLph18026788 for ; Mon, 28 Jun 2010 15:51:45 -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 o5SLpX8B026203; Mon, 28 Jun 2010 15:51:43 -0600 From: "Venkateswararao Jujjuri (JV)" To: qemu-devel@nongnu.org Date: Mon, 28 Jun 2010 14:55:12 -0700 Message-Id: <1277762117-11212-15-git-send-email-jvrao@linux.vnet.ibm.com> X-Mailer: git-send-email 1.6.0.6 In-Reply-To: <1277762117-11212-1-git-send-email-jvrao@linux.vnet.ibm.com> References: <1277762117-11212-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 15/20] 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 | 157 ++++++++++++++++++++++++++++++++++++++++---------------- hw/virtio-9p.h | 11 ++++- 2 files changed, 123 insertions(+), 45 deletions(-) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 4f28d86..156d5dd 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -2533,11 +2533,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; @@ -2552,17 +2547,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) { @@ -2570,44 +2578,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_malloc(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); + } - memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1); - 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; @@ -2617,6 +2656,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) { @@ -2975,6 +3043,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 81a1489..e9cc458 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, @@ -305,7 +307,6 @@ typedef struct V9fsWstatState V9fsStat v9stat; V9fsFidState *fidp; struct stat stbuf; - V9fsString nname; } V9fsWstatState; typedef struct V9fsSymlinkState @@ -380,6 +381,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);