From nobody Thu Apr 30 00:39:37 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776087991126180.42531194511912; Mon, 13 Apr 2026 06:46:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wCHcE-0004zj-0P; Mon, 13 Apr 2026 09:46:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcD-0004za-12 for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:13 -0400 Received: from mgamail.intel.com ([198.175.65.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcB-0002B8-2T for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:12 -0400 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:11 -0700 Received: from junjie-optiplex-micro-plus-7010.bj.intel.com ([10.238.152.98]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:09 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1776087971; x=1807623971; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7MG2x1Tpck0f/s3FRgTmVdtUJNJSEk2UYFSH4EpXP04=; b=JEEBKhg1mNrMasBylcYCSk4ua14iXtBF8RSQxvN+XBKh7IQfzGCmYztk Bk2jRX4JSEkxb7lhHKls5HahljNvlage3SWHn+0/D8PHzm2QYYv9WGErI QJEedV9jeTKI90MpN/nY5hc9Rh4tGfVgqk10I99hkoqyXtc5IvDMZACdX f92yZZRUohBR1qoSxvy89PHXXgE0FFInpxSnR7IIzLqsvRkasbsKkk1Ik cTgX/tNm0rgwwqzDshV9IXUIzdjwY9lUxBWcHkhmRffNLe603s0pLADxA 8f/41JVhPLRZY5meqvdM1jblgXKtGO4IpoubcltbDwcvTScfEfJ9QfRZp g==; X-CSE-ConnectionGUID: cpdLLcBOQuGeWD62R4hJmw== X-CSE-MsgGUID: hFBq0FW2SUuZaVFc/jp04w== X-IronPort-AV: E=McAfee;i="6800,10657,11758"; a="80885625" X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="80885625" X-CSE-ConnectionGUID: x3I1tAg6R8Cc1jAormWLEg== X-CSE-MsgGUID: QflxPTWTT1mZkJxiG56OOQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="234770373" From: Junjie Cao To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, farosas@suse.de, junjie.cao@intel.com Subject: [PATCH v3 1/4] io/channel: introduce qio_channel_pread{v, }_all{, _eof}() Date: Tue, 14 Apr 2026 05:45:46 +0800 Message-ID: <20260413214549.926435-2-junjie.cao@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260413214549.926435-1-junjie.cao@intel.com> References: <20260413214549.926435-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=198.175.65.14; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DATE_IN_FUTURE_06_12=1.947, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1776087993640158500 Content-Type: text/plain; charset="utf-8" qio_channel_pread() and qio_channel_preadv() perform a single positioned read and may return a short result. Callers that need all bytes currently have to open-code a retry loop or simply treat a short read as an error. Introduce four new helpers following the existing read_all / readv_all pattern: qio_channel_preadv_all_eof() -- retry loop; returns 1 on success, 0 on clean EOF, -1 on error. qio_channel_preadv_all() -- wraps _eof; treats early EOF as error; returns 0 / -1. qio_channel_pread_all_eof() -- single-buffer convenience wrapper around preadv_all_eof(). qio_channel_pread_all() -- single-buffer convenience wrapper around preadv_all(). These advance the file offset internally after each partial read. All four are marked coroutine_mixed_fn, consistent with the existing _all helpers. Suggested-by: Peter Xu Signed-off-by: Junjie Cao Acked-by: Daniel P. Berrang=C3=A9 Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Fabiano Rosas --- include/io/channel.h | 92 ++++++++++++++++++++++++++++++++++++++++++++ io/channel.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) diff --git a/include/io/channel.h b/include/io/channel.h index 1b02350437..47af409ede 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -634,6 +634,98 @@ ssize_t qio_channel_preadv(QIOChannel *ioc, const stru= ct iovec *iov, ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen, off_t offset, Error **errp); =20 +/** + * qio_channel_preadv_all_eof: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads @iov, possibly blocking or (if the channel is non-blocking) + * yielding from the current coroutine multiple times until the entire + * content is read. If end-of-file occurs immediately it is not an + * error, but if it occurs after data has been read it will return + * an error rather than a short-read. Otherwise behaves as + * qio_channel_preadv(). + * + * Returns: 1 if all bytes were read, 0 if end-of-file occurs + * without data, or -1 on error + */ +int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp); + +/** + * qio_channel_preadv_all: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads @iov, possibly blocking or (if the channel is non-blocking) + * yielding from the current coroutine multiple times until the entire + * content is read. If end-of-file occurs before all requested data + * has been read, an error will be reported. Otherwise behaves as + * qio_channel_preadv(). + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp); + +/** + * qio_channel_pread_all_eof: + * @ioc: the channel object + * @buf: the memory region to read data into + * @buflen: the number of bytes to read into @buf + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads @buflen bytes, possibly blocking or (if the channel is + * non-blocking) yielding from the current coroutine multiple times + * until the entire content is read. If end-of-file occurs + * immediately it is not an error, but if it occurs after data has + * been read it will return an error rather than a short-read. + * Otherwise behaves as qio_channel_pread(). + * + * Returns: 1 if all bytes were read, 0 if end-of-file occurs + * without data, or -1 on error + */ +int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp); + +/** + * qio_channel_pread_all: + * @ioc: the channel object + * @buf: the memory region to read data into + * @buflen: the number of bytes to read into @buf + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads @buflen bytes, possibly blocking or (if the channel is + * non-blocking) yielding from the current coroutine multiple times + * until the entire content is read. If end-of-file occurs before + * all requested data has been read, an error will be reported. + * Otherwise behaves as qio_channel_pread(). + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp); + /** * qio_channel_shutdown: * @ioc: the channel object diff --git a/io/channel.c b/io/channel.c index cc02d997a4..52c1abfcbc 100644 --- a/io/channel.c +++ b/io/channel.c @@ -507,6 +507,97 @@ ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, = size_t buflen, return qio_channel_preadv(ioc, &iov, 1, offset, errp); } =20 +int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp) +{ + int ret =3D -1; + struct iovec *local_iov =3D g_new(struct iovec, niov); + struct iovec *local_iov_head =3D local_iov; + unsigned int nlocal_iov =3D niov; + bool partial =3D false; + + nlocal_iov =3D iov_copy(local_iov, nlocal_iov, + iov, niov, + 0, iov_size(iov, niov)); + + while (nlocal_iov > 0) { + ssize_t len; + len =3D qio_channel_preadv(ioc, local_iov, nlocal_iov, offset, err= p); + + if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { + qio_channel_wait_cond(ioc, G_IO_IN); + continue; + } + + if (len =3D=3D 0) { + if (!partial) { + ret =3D 0; + goto cleanup; + } + error_setg(errp, + "Unexpected end-of-file before all data were read"); + goto cleanup; + } + + if (len < 0) { + goto cleanup; + } + + partial =3D true; + offset +=3D len; + iov_discard_front(&local_iov, &nlocal_iov, len); + } + + ret =3D 1; + + cleanup: + g_free(local_iov_head); + return ret; +} + +int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp) +{ + int ret =3D qio_channel_preadv_all_eof(ioc, iov, niov, offset, errp); + + if (ret =3D=3D 0) { + error_setg(errp, + "Unexpected end-of-file before all data were read"); + return -1; + } + if (ret =3D=3D 1) { + return 0; + } + + return ret; +} + +int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp) +{ + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D buflen }; + return qio_channel_preadv_all_eof(ioc, &iov, 1, offset, errp); +} + +int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp) +{ + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D buflen }; + return qio_channel_preadv_all(ioc, &iov, 1, offset, errp); +} + int qio_channel_shutdown(QIOChannel *ioc, QIOChannelShutdown how, Error **errp) --=20 2.43.0 From nobody Thu Apr 30 00:39:37 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1776087999; cv=none; d=zohomail.com; s=zohoarc; b=fqHh52iik1yjr6P69a0K84iNC1uqRIdBctVo9tI1rogzEF2UZcI+/C8XcxmVeVJl/PzVKFP1Rime8C/dcaIdwAfAWigN+4u0QJOnXlSL/fqA53iyJsW5LIwnN1TeP9BqB5s3FASnYd3MZ89/7wVXUEmvV3zM4BdLnKlVNh84suc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776087999; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cszjlPPvEEDMh+yVL0RAC5pJxs7wwCt/iTuSgd2j8xM=; b=lF+gnuuTokQUM2Ctza4PBRw/z5fVwyOfQTtyvHmEdHjEARqULXLiNoK9o0S8q8vIKDWhac3cGVeQNC6JmvLU+z3s8O99bW1oSXotOSls7+RkX8+0DYV1cLTizL2XjTiMlIV7Mu0S38WPGOpyWTltT5iWIXwdADznloU91D0gRMc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776087999379449.3216788785164; Mon, 13 Apr 2026 06:46:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wCHcS-00052V-TW; Mon, 13 Apr 2026 09:46:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcO-00050D-D0 for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:26 -0400 Received: from mgamail.intel.com ([198.175.65.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcM-0002Fp-9e for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:24 -0400 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:21 -0700 Received: from junjie-optiplex-micro-plus-7010.bj.intel.com ([10.238.152.98]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:20 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1776087982; x=1807623982; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=folrTF5beoC9lFULQ16CJB+/2zzJLPZS9SYdJ6UdO6M=; b=lIcmUOZXkcZjJ66AXInKSDtd03RcubRXn3UrEk3i9BVZWLZPI9JvF+jK 1rMOmtSIPdm28ao4bBdgOQJMMrbu7JKZG0gPDG3Kys70BLHcw2ZP53R4i jBbOg9EL3ps71EL7x/XqmzMSxJsBXtjqUh6L6bDIcdUmCtXOaP+AJXQtu /NxV7VUIl4xPyBVmddjVCTYQszWjg8wuC6EMt1QeOImsc4LrZsXQSww09 T6ct9b1jtphqAVce8WwRS4nfl0joVqQVRvnHNgVDA/EmNqcVxX0D6tTtZ V2mONT86sn4LXsV3+OdJEWvzro/bfiA3C0L8A4SjbK/gy+A0bxoIJBf5B A==; X-CSE-ConnectionGUID: 1dtrLPWRRWqWSM3XDMuOZQ== X-CSE-MsgGUID: DgEqc80fThWhN714Xoapmg== X-IronPort-AV: E=McAfee;i="6800,10657,11758"; a="80885643" X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="80885643" X-CSE-ConnectionGUID: Fy0io/UwR3KsF7LqnGC31A== X-CSE-MsgGUID: JUj+E3HsQ4qFcRY37r6ppg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="234770409" From: Junjie Cao To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, farosas@suse.de, junjie.cao@intel.com Subject: [PATCH v3 2/4] io/channel: introduce qio_channel_pwrite{v,}_all() Date: Tue, 14 Apr 2026 05:45:47 +0800 Message-ID: <20260413214549.926435-3-junjie.cao@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260413214549.926435-1-junjie.cao@intel.com> References: <20260413214549.926435-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=198.175.65.14; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DATE_IN_FUTURE_06_12=1.947, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1776088001522158500 Content-Type: text/plain; charset="utf-8" Add positioned write helpers that retry on short writes, matching the pread_all family from the previous patch. qio_channel_pwritev_all() -- retry loop; returns 0 on success, -1 on error. qio_channel_pwrite_all() -- single-buffer convenience wrapper. Signed-off-by: Junjie Cao Acked-by: Daniel P. Berrang=C3=A9 Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Fabiano Rosas --- include/io/channel.h | 41 +++++++++++++++++++++++++++++++++++++ io/channel.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/include/io/channel.h b/include/io/channel.h index 47af409ede..287d10cd6f 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -598,6 +598,47 @@ ssize_t qio_channel_pwritev(QIOChannel *ioc, const str= uct iovec *iov, ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen, off_t offset, Error **errp); =20 +/** + * qio_channel_pwritev_all: + * @ioc: the channel object + * @iov: the array of memory regions to write data from + * @niov: the length of the @iov array + * @offset: the starting offset in the channel to write to + * @errp: pointer to a NULL-initialized error object + * + * Writes @iov, possibly blocking or (if the channel is non-blocking) + * yielding from the current coroutine multiple times until the entire + * content is written. Otherwise behaves as qio_channel_pwritev(). + * + * Returns: 0 if all bytes were written, or -1 on error + */ +int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp); + +/** + * qio_channel_pwrite_all: + * @ioc: the channel object + * @buf: the memory region to write data from + * @buflen: the number of bytes to write from @buf + * @offset: the starting offset in the channel to write to + * @errp: pointer to a NULL-initialized error object + * + * Writes @buflen bytes from @buf, possibly blocking or (if the + * channel is non-blocking) yielding from the current coroutine + * multiple times until the entire content is written. Otherwise + * behaves as qio_channel_pwrite(). + * + * Returns: 0 if all bytes were written, or -1 on error + */ +int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc, + const void *buf, + size_t buflen, + off_t offset, + Error **errp); + /** * qio_channel_preadv * @ioc: the channel object diff --git a/io/channel.c b/io/channel.c index 52c1abfcbc..2853dadb68 100644 --- a/io/channel.c +++ b/io/channel.c @@ -478,6 +478,54 @@ ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf,= size_t buflen, return qio_channel_pwritev(ioc, &iov, 1, offset, errp); } =20 +int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp) +{ + int ret =3D -1; + struct iovec *local_iov =3D g_new(struct iovec, niov); + struct iovec *local_iov_head =3D local_iov; + unsigned int nlocal_iov =3D niov; + + nlocal_iov =3D iov_copy(local_iov, nlocal_iov, + iov, niov, + 0, iov_size(iov, niov)); + + while (nlocal_iov > 0) { + ssize_t len; + + len =3D qio_channel_pwritev(ioc, local_iov, nlocal_iov, offset, er= rp); + + if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { + qio_channel_wait_cond(ioc, G_IO_OUT); + continue; + } + if (len < 0) { + goto cleanup; + } + + offset +=3D len; + iov_discard_front(&local_iov, &nlocal_iov, len); + } + + ret =3D 0; + cleanup: + g_free(local_iov_head); + return ret; +} + +int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc, + const void *buf, + size_t buflen, + off_t offset, + Error **errp) +{ + struct iovec iov =3D { .iov_base =3D (char *)buf, .iov_len =3D buflen = }; + return qio_channel_pwritev_all(ioc, &iov, 1, offset, errp); +} + ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov, size_t niov, off_t offset, Error **errp) { --=20 2.43.0 From nobody Thu Apr 30 00:39:37 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1776088011; cv=none; d=zohomail.com; s=zohoarc; b=MRKGeCXP0a36jMM2YFeHTxZQugEqN5XVDaY+e4o/wT2uMsPNGDT/FLjuxwD4xxZIjMcCAZH/ctv8kPgVBmuoD3YbH8XkyzjyfRoEEfYMclCTc6+fzmL6fUGIBjcfrH9Rvo5Cgxq0StFBUBZfJPh4tpR3NpxvDGWZWbBMOdvwooM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776088011; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6H8V209KmKPLAPpy11Wl1lKfuy7+ij4qb/g7wbTYxcI=; b=Fle1itcLyJzJ30KhiIUzVbuXNXttLgVayjC41Q+hLMSWX0wej5+c9uVmhkF9S+OQB1c2+YM9UMP4UOWgb0KI1ND32E9cBpaWx63jA0aYqwXlsTZ8PDERVAM5q2hi+NBKXIDD2lwxkjyTNWzztxy2ylcpptOwI2FofhVHWHg66Ds= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776088011094922.6609639910703; Mon, 13 Apr 2026 06:46:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wCHcY-00057K-B6; Mon, 13 Apr 2026 09:46:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcX-00056l-1g for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:33 -0400 Received: from mgamail.intel.com ([198.175.65.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcV-0002H1-GE for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:32 -0400 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:30 -0700 Received: from junjie-optiplex-micro-plus-7010.bj.intel.com ([10.238.152.98]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:29 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1776087992; x=1807623992; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jRIYr/DJPikQoTUtLLf24NEzeIOFia6aU6GKa/BmoKA=; b=JuPavCBXydKLribUPiYNn7ONXxn6tvNffsOv1MzoYyZdX+uIPCFWBHq6 a7QcWHekemdFdKBsBUtnkQbYtMvVAtw4yXW/UuLU+DlzBv7ahdBi3AMev cVgxd2Gm3+uLfAUHIjREqucHL2keofRuiS0V/DMBliHrZeMIBqjGC/gaI UvED3JHS5YWSjrOwmXRiqM7TgElYr6s0XhIcKVIA+IWps4SiVPN/XcF8o FsGEjAgw3eDyqgx/ZMHNzG0KhnPR/ZBaZVnI1KKXv7O+Dcl77g52TNsFD fWjV2vhHjwMrNWfNJKEV0RLZq9Zvtyneeq198rc8uMh8LDIMCqjiZ9sIu Q==; X-CSE-ConnectionGUID: HFgKHtr+TIC+F5/5Z6eEiA== X-CSE-MsgGUID: 8M0HvRKgSwqlJyA52+tIxw== X-IronPort-AV: E=McAfee;i="6800,10657,11758"; a="80885675" X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="80885675" X-CSE-ConnectionGUID: mRaTV8cTR++7NbeU2U3tEA== X-CSE-MsgGUID: ffP93G3MQ9SAB2yOrdMGKw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="234770429" From: Junjie Cao To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, farosas@suse.de, junjie.cao@intel.com Subject: [PATCH v3 3/4] migration/file: fix type mismatch and NULL deref in multifd_file_recv_data Date: Tue, 14 Apr 2026 05:45:48 +0800 Message-ID: <20260413214549.926435-4-junjie.cao@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260413214549.926435-1-junjie.cao@intel.com> References: <20260413214549.926435-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=198.175.65.14; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DATE_IN_FUTURE_06_12=1.947, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1776088013287158500 multifd_file_recv_data() stores the return value of qio_channel_pread() (ssize_t) in a size_t variable. On I/O error the -1 return value wraps to SIZE_MAX, producing a nonsensical read size in the error message. More critically, a short read (0 <=3D ret < data->size) is possible when the migration file is truncated. In that case qio_channel_pread() returns a non-negative value without setting *errp. The function then calls error_prepend(errp, ...) which dereferences *errp -- a NULL pointer -- crashing QEMU. Fix both issues by switching to qio_channel_pread_all() introduced in a previous patch, which retries on short reads and treats end-of-file as an error, so the caller no longer needs to check the byte count manually. Add ERRP_GUARD() so that error_prepend() works correctly even when errp is &error_fatal or NULL. Fixes: a49d15a38d3d ("migration/multifd: Support incoming mapped-ram stream= format") Suggested-by: Peter Xu Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Junjie Cao Reviewed-by: Fabiano Rosas --- migration/file.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/migration/file.c b/migration/file.c index 5618aced49..0853188601 100644 --- a/migration/file.c +++ b/migration/file.c @@ -254,15 +254,16 @@ int file_write_ramblock_iov(QIOChannel *ioc, const st= ruct iovec *iov, =20 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp) { + ERRP_GUARD(); MultiFDRecvData *data =3D p->data; - size_t ret; + int ret; =20 - ret =3D qio_channel_pread(p->c, (char *) data->opaque, - data->size, data->file_offset, errp); - if (ret !=3D data->size) { + ret =3D qio_channel_pread_all(p->c, (char *) data->opaque, + data->size, data->file_offset, errp); + if (ret !=3D 0) { error_prepend(errp, - "multifd recv (%u): read 0x%zx, expected 0x%zx", - p->id, ret, data->size); + "multifd recv (%u): ", + p->id); return -1; } =20 --=20 2.43.0 From nobody Thu Apr 30 00:39:37 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1776088024; cv=none; d=zohomail.com; s=zohoarc; b=DcP+OClmluvFcRn8gbtv5I23qIqy3gyrOXWgAEF+b+j5PFi172eDvAJv6B5C4cTvc9BNABo+LkUi25zRFlx6MID7xwk0Q9Q5QbDuNpLZ5FrO1r6ZCfKgczUCPrx7uoFiivO/g0B81p4N31u8qK7hWvJJUC9axDgrXRd/YJevxfo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776088024; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nxDtNQ77QWtpXXKhqCy52ACSnJCEAenXUmXVtXzCYxo=; b=bsxnlFHy6JqIv+tFVLNfGiO7oXPUzAiBgxa75d7nIaHK3UEOeMdIIH+j8kQz0adbni729DXhjsqXCi5MUYnYv9tMhsyepPoZ8Ar9zyg6hKOb8hZr1BdKg2qUvZYfBKMANU1QLbHqYuQvH4X5cvGk58DZXjYJtW4zr3z9BD6a7uw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776088024897442.5074688371826; Mon, 13 Apr 2026 06:47:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wCHci-0005Qw-Mq; Mon, 13 Apr 2026 09:46:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHcg-0005OI-Tb for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:42 -0400 Received: from mgamail.intel.com ([198.175.65.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wCHce-0002Hd-Vl for qemu-devel@nongnu.org; Mon, 13 Apr 2026 09:46:42 -0400 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:40 -0700 Received: from junjie-optiplex-micro-plus-7010.bj.intel.com ([10.238.152.98]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2026 06:46:38 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1776088001; x=1807624001; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ApqyZHZa9fhGJ2xZs56EjVo0twC5/V/7fNPCkKeWwQs=; b=PaRSvuviHw0X4pwDubkC0GkYmbE8eR0ywl4Np2Pt3mzY1DZz8AKhPTIB dNKx9w+G7AW8KSZTxkgdIwDYNX5rIOUVWfltJ0OhpN8IkzcVdAoKFA7uI wHlMT7f1G+9yK9lL/2DT3hfFz69opN2iFqKyW9Wa2nm8vK+6p/wdRO4Lw MzorLheRfnfhiZ0ZMlWNLJN/aWorq0RsmLHxNb0fSFtMTYeaemTDZeiJ6 BM2ubyrsJSoj3zfIRXWC1Navo7EXS0gy/j8c2tC2ZW73bUcjkIHWMkCFc 8m02ekDSbbZZk3R0zrchPEzr3tybbk4Aw7n2B62VgDfVwBFlzcAYBdNWm g==; X-CSE-ConnectionGUID: xf0GMaG+RhS7+7lKbvVNpg== X-CSE-MsgGUID: jDYVNDPJRUuj4dl+b32flw== X-IronPort-AV: E=McAfee;i="6800,10657,11758"; a="80885702" X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="80885702" X-CSE-ConnectionGUID: bTHnEagBRkGVRw0IeTHWIA== X-CSE-MsgGUID: 07XdndmdTya6AiJ2a+osWQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,177,1770624000"; d="scan'208";a="234770444" From: Junjie Cao To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, farosas@suse.de, junjie.cao@intel.com Subject: [PATCH v3 4/4] tests/unit: add pread/pwrite _all tests for io channel file Date: Tue, 14 Apr 2026 05:45:49 +0800 Message-ID: <20260413214549.926435-5-junjie.cao@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260413214549.926435-1-junjie.cao@intel.com> References: <20260413214549.926435-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=198.175.65.14; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DATE_IN_FUTURE_06_12=1.947, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1776088026595154100 Content-Type: text/plain; charset="utf-8" Add unit tests for the new qio_channel_pread{v,}_all{,_eof}() and qio_channel_pwrite{v,}_all() APIs. The basic tests write data to a file channel, then read it back at various offsets using both the single-buffer and iovec variants to make sure the round-trip produces identical content. The _eof tests verify all three return cases -- full read (1), clean EOF (0), and partial-then-EOF (-1 with error set) -- and check that the strict wrappers (preadv_all / pread_all) treat a clean EOF as an error. All tests are guarded by CONFIG_PREADV since the underlying channel methods require preadv(2). Signed-off-by: Junjie Cao Acked-by: Daniel P. Berrang=C3=A9 Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Fabiano Rosas --- tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/tests/unit/test-io-channel-file.c b/tests/unit/test-io-channel= -file.c index 1977006ce9..b597350dca 100644 --- a/tests/unit/test-io-channel-file.c +++ b/tests/unit/test-io-channel-file.c @@ -102,6 +102,203 @@ static void test_io_channel_fd(void) } =20 =20 +#ifdef CONFIG_PREADV +static void test_io_channel_pread_all(void) +{ + QIOChannel *ioc; + char write_buf[] =3D "Hello World, pread_all"; + char read_buf[sizeof(write_buf)] =3D {0}; + int ret; + + unlink(TEST_FILE); + ioc =3D QIO_CHANNEL(qio_channel_file_new_path( + TEST_FILE, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, + TEST_MASK, + &error_abort)); + + ret =3D qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf), + 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + /* Read back at offset 0 */ + ret =3D qio_channel_pread_all(ioc, read_buf, sizeof(read_buf), + 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_cmpmem(write_buf, sizeof(write_buf), + read_buf, sizeof(read_buf)); + + /* Read at a non-zero offset */ + memset(read_buf, 0, sizeof(read_buf)); + ret =3D qio_channel_pread_all(ioc, read_buf, sizeof(write_buf) - 7, + 7, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_cmpmem(write_buf + 7, sizeof(write_buf) - 7, + read_buf, sizeof(write_buf) - 7); + + unlink(TEST_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_preadv_all(void) +{ + QIOChannel *ioc; + char write_buf[256]; + char read_buf[256] =3D {0}; + struct iovec write_iov[2]; + struct iovec read_iov[2]; + int ret; + size_t i; + + for (i =3D 0; i < sizeof(write_buf); i++) { + write_buf[i] =3D i & 0xff; + } + + unlink(TEST_FILE); + ioc =3D QIO_CHANNEL(qio_channel_file_new_path( + TEST_FILE, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, + TEST_MASK, + &error_abort)); + + /* Write using pwritev_all with 2 iovecs */ + write_iov[0].iov_base =3D write_buf; + write_iov[0].iov_len =3D 128; + write_iov[1].iov_base =3D write_buf + 128; + write_iov[1].iov_len =3D 128; + ret =3D qio_channel_pwritev_all(ioc, write_iov, 2, 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + /* Read back using preadv_all with 2 iovecs */ + read_iov[0].iov_base =3D read_buf; + read_iov[0].iov_len =3D 128; + read_iov[1].iov_base =3D read_buf + 128; + read_iov[1].iov_len =3D 128; + ret =3D qio_channel_preadv_all(ioc, read_iov, 2, 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + g_assert_cmpmem(write_buf, sizeof(write_buf), + read_buf, sizeof(read_buf)); + + /* Read at non-zero offset with preadv_all */ + memset(read_buf, 0, sizeof(read_buf)); + read_iov[0].iov_base =3D read_buf; + read_iov[0].iov_len =3D 64; + read_iov[1].iov_base =3D read_buf + 64; + read_iov[1].iov_len =3D 64; + ret =3D qio_channel_preadv_all(ioc, read_iov, 2, 128, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + g_assert_cmpmem(write_buf + 128, 128, + read_buf, 128); + + unlink(TEST_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_preadv_all_eof(void) +{ + QIOChannel *ioc; + char write_buf[] =3D "Hello World, preadv_all_eof"; + char read_buf[sizeof(write_buf)] =3D {0}; + struct iovec iov; + int ret; + Error *err =3D NULL; + + unlink(TEST_FILE); + ioc =3D QIO_CHANNEL(qio_channel_file_new_path( + TEST_FILE, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, + TEST_MASK, + &error_abort)); + + ret =3D qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf), + 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + /* Full read succeeds: should return 1 */ + iov.iov_base =3D read_buf; + iov.iov_len =3D sizeof(read_buf); + ret =3D qio_channel_preadv_all_eof(ioc, &iov, 1, 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 1); + g_assert_cmpmem(write_buf, sizeof(write_buf), + read_buf, sizeof(read_buf)); + + /* Clean EOF: offset at file end, should return 0 */ + iov.iov_base =3D read_buf; + iov.iov_len =3D 1; + ret =3D qio_channel_preadv_all_eof(ioc, &iov, 1, + sizeof(write_buf), &err); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_null(err); + + /* Partial EOF: start before end, request extends past */ + iov.iov_base =3D read_buf; + iov.iov_len =3D 8; + ret =3D qio_channel_preadv_all_eof(ioc, &iov, 1, + sizeof(write_buf) - 4, &err); + g_assert_cmpint(ret, =3D=3D, -1); + g_assert_nonnull(err); + error_free(err); + err =3D NULL; + + /* Strict wrapper (preadv_all) treats clean EOF as error */ + iov.iov_base =3D read_buf; + iov.iov_len =3D 1; + ret =3D qio_channel_preadv_all(ioc, &iov, 1, + sizeof(write_buf), &err); + g_assert_cmpint(ret, =3D=3D, -1); + g_assert_nonnull(err); + error_free(err); + + unlink(TEST_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_pread_all_eof(void) +{ + QIOChannel *ioc; + char write_buf[] =3D "Hello World, pread_all_eof"; + char read_buf[sizeof(write_buf)] =3D {0}; + int ret; + Error *err =3D NULL; + + unlink(TEST_FILE); + ioc =3D QIO_CHANNEL(qio_channel_file_new_path( + TEST_FILE, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, + TEST_MASK, + &error_abort)); + + ret =3D qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf), + 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + + /* Full read succeeds: should return 1 */ + ret =3D qio_channel_pread_all_eof(ioc, read_buf, sizeof(read_buf), + 0, &error_abort); + g_assert_cmpint(ret, =3D=3D, 1); + g_assert_cmpmem(write_buf, sizeof(write_buf), + read_buf, sizeof(read_buf)); + + /* Clean EOF: should return 0 */ + ret =3D qio_channel_pread_all_eof(ioc, read_buf, 1, + sizeof(write_buf), &err); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_null(err); + + /* Partial EOF: should return -1 */ + ret =3D qio_channel_pread_all_eof(ioc, read_buf, 8, + sizeof(write_buf) - 4, &err); + g_assert_cmpint(ret, =3D=3D, -1); + g_assert_nonnull(err); + error_free(err); + + unlink(TEST_FILE); + object_unref(OBJECT(ioc)); +} +#endif /* CONFIG_PREADV */ + #ifndef _WIN32 static void test_io_channel_pipe(bool async) { @@ -147,6 +344,16 @@ int main(int argc, char **argv) g_test_add_func("/io/channel/file", test_io_channel_file); g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr); g_test_add_func("/io/channel/file/fd", test_io_channel_fd); +#ifdef CONFIG_PREADV + g_test_add_func("/io/channel/file/pread-all", + test_io_channel_pread_all); + g_test_add_func("/io/channel/file/preadv-all", + test_io_channel_preadv_all); + g_test_add_func("/io/channel/file/preadv-all-eof", + test_io_channel_preadv_all_eof); + g_test_add_func("/io/channel/file/pread-all-eof", + test_io_channel_pread_all_eof); +#endif #ifndef _WIN32 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync); g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async); --=20 2.43.0