From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y2naF-0000q8-LD for qemu-devel@nongnu.org; Sun, 21 Dec 2014 15:58:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y2na7-00049C-Vn for qemu-devel@nongnu.org; Sun, 21 Dec 2014 15:58:35 -0500 Received: from e39.co.us.ibm.com ([32.97.110.160]:39204) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y2na7-00048P-P3 for qemu-devel@nongnu.org; Sun, 21 Dec 2014 15:58:27 -0500 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sun, 21 Dec 2014 13:58:25 -0700 Received: from b01cxnp23034.gho.pok.ibm.com (b01cxnp23034.gho.pok.ibm.com [9.57.198.29]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 4737DC90026 for ; Sun, 21 Dec 2014 15:50:25 -0500 (EST) Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id sBLKwMlw25362686 for ; Sun, 21 Dec 2014 20:58:22 GMT Received: from d01av04.pok.ibm.com (localhost [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id sBLKwLZk030443 for ; Sun, 21 Dec 2014 15:58:22 -0500 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth In-Reply-To: <1417849159-6568-3-git-send-email-zhang.zhanghailiang@huawei.com> References: <1417849159-6568-1-git-send-email-zhang.zhanghailiang@huawei.com> <1417849159-6568-3-git-send-email-zhang.zhanghailiang@huawei.com> Message-ID: <20141221205816.15420.70063@loki> Date: Sun, 21 Dec 2014 14:58:16 -0600 Subject: Re: [Qemu-devel] [PATCH RFC for-2.3 2/6] qga: introduce three help functions for memory block functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: zhanghailiang , qemu-devel@nongnu.org Cc: peter.huangpeng@huawei.com Quoting zhanghailiang (2014-12-06 00:59:15) > Signed-off-by: zhanghailiang > --- > qga/commands-posix.c | 130 +++++++++++++++++++++++++++++++++++++++++++++= ++++++ > 1 file changed, 130 insertions(+) > = > diff --git a/qga/commands-posix.c b/qga/commands-posix.c > index b0d6a5d..8917dca 100644 > --- a/qga/commands-posix.c > +++ b/qga/commands-posix.c > @@ -1875,6 +1875,136 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessor= List *vcpus, Error **errp) > return processed; > } I think these would break build bisectability. Please move these into the patches which introduce the first users. > = > +static void ga_read_sysfs_file(int dirfd, const char *pathname, char *bu= f, > + int size, Error **errp) > +{ > + int fd; > + int res; > + > + errno =3D 0; > + fd =3D openat(dirfd, pathname, O_RDONLY); > + if (fd =3D=3D -1) { > + error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname= ); > + return; > + } > + > + res =3D pread(fd, buf, size, 0); > + if (res =3D=3D -1) { > + error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathnam= e); > + } else if (res =3D=3D 0) { > + error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", path= name); > + } > + res =3D close(fd); > + g_assert(res =3D=3D 0); This should also be propagated up as an error, killing the guest agent shou= ld be avoided when possible. > +} > + > +static void ga_write_sysfs_file(int dirfd, const char *pathname, > + const char *buf, int size, Error **errp) > +{ > + int fd; > + int res; > + > + errno =3D 0; > + fd =3D openat(dirfd, pathname, O_WRONLY); > + if (fd =3D=3D -1) { > + error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname= ); > + return; > + } > + > + if (pwrite(fd, buf, size, 0) =3D=3D -1) { > + error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathna= me); > + } > + > + res =3D close(fd); > + g_assert(res =3D=3D 0); > +} > + > +/* Transfer online/offline status between @mem_blk and the guest system. > + * > + * On input either @errp or *@errp must be NULL. > + * > + * In system-to-@mem_blk direction, the following @mem_blk fields are ac= cessed: > + * - R: mem_blk->phys_index > + * - W: mem_blk->online > + * - W: mem_blk->can_offline > + * > + * In @mem_blk-to-system direction, the following @mem_blk fields are ac= cessed: > + * - R: mem_blk->phys_index > + * - R: mem_blk->online > + *- R: mem_blk->can_offline > + * Written members remain unmodified on error. > + */ > +static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2me= mblk, > + Error **errp) > +{ > + char *dirpath; > + int dirfd; > + int res; > + char *status; > + Error *local_err =3D NULL; > + > + dirpath =3D g_strdup_printf("/sys/devices/system/memory/memory%" PRI= d64 "/", > + mem_blk->phys_index); > + dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); > + if (dirfd =3D=3D -1) { > + error_setg_errno(errp, errno, "open(\"%s\")", dirpath); > + g_free(dirpath); > + return; > + } > + g_free(dirpath); > + > + status =3D g_malloc0(10); > + ga_read_sysfs_file(dirfd, "state", status, 10, &local_err); > + if (local_err) { > + /* treat with sysfs file that not exist in old kernel */ > + if (errno =3D=3D ENOENT) { > + error_free(local_err); > + if (sys2memblk) { > + mem_blk->online =3D true; > + mem_blk->can_offline =3D false; > + } else if (!mem_blk->online) { > + error_setg(errp, "memory block #%" PRId64 " can't be " > + "offlined", mem_blk->phys_index); > + } > + } else { > + error_propagate(errp, local_err); > + } > + return; Need to free 'status' here, and close 'dirfd'. perhaps just a goto to the end of the function since you already have common cleanup there. And, if you use 'local_err' in place of 'err' for error_set, you can maybe move all your error_propagate calls there as well. > + } > + > + if (sys2memblk) { > + char removable =3D '0'; > + > + mem_blk->online =3D (strncmp(status, "online", 6) =3D=3D 0); > + > + ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err= ); > + if (local_err) { > + /* if no 'removable' file, it does't support offline mem blk= */ > + if (errno =3D=3D ENOENT) { > + error_free(local_err); > + mem_blk->can_offline =3D false; > + } else { > + error_propagate(errp, local_err); > + } > + } else { > + mem_blk->can_offline =3D (removable !=3D '0'); > + } > + } else { > + if (mem_blk->online !=3D (strncmp(status, "online", 6) =3D=3D 0)= ) { > + char *new_state =3D mem_blk->online ? g_strdup("online") : > + g_strdup("offline"); > + > + ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_st= ate), > + errp); > + g_free(new_state); > + } /* otherwise pretend successful re-(on|off)-lining */ > + } > + > + g_free(status); > + res =3D close(dirfd); > + g_assert(res =3D=3D 0); > +} > + > #else /* defined(__linux__) */ > = > void qmp_guest_suspend_disk(Error **errp) > -- = > 1.7.12.4