From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 7D7C45A0275 for ; Wed, 28 Feb 2024 02:02:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709082140; bh=++/Z9mfl2UDJAQRVKdy8QitvDX8VR19Heh/ewXlcOo0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Tc82UTnxnPfykr3yTgy9jHkbSzDnREJNHwgBkVVguON3mmbxE7qMpnAQeERAd4jYu iRnWhfZNipUrK1EB2Ogv8N/yYjH2lYyguocjA3184w8AJzyZzkol5l/Glv3hBGkHxa gPREua0YmlMU48A5Us+vVc4uJiobbi5pRCNGUgL98+IJf83oV8LK90FxFSaMGu+46Z ZYR1qfrVMqyfVIdCEIQ1fduJwPdX16NB8/qaCwm2f63rXSm5L1Fmy0oPacdTM2xawF KwOYjKqwhAZmVvl8fyqnVa2j1F1D6loIEITu+pzGpQlXKWhOmoorSDijoP4Hrxm6dn QUXX1nVn5DTcw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tkx0D5BkFz4wcH; Wed, 28 Feb 2024 12:02:20 +1100 (AEDT) Date: Wed, 28 Feb 2024 11:44:28 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 3/6] util: Add write_remainder() helper Message-ID: References: <20240222055602.1872516-1-david@gibson.dropbear.id.au> <20240222055602.1872516-4-david@gibson.dropbear.id.au> <20240227152551.72d8744f@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="OWVEoR3cNa0wFH7h" Content-Disposition: inline In-Reply-To: <20240227152551.72d8744f@elisabeth> Message-ID-Hash: BDWJGT3REYZHMKNXPQQO3SIWVGQZJOC7 X-Message-ID-Hash: BDWJGT3REYZHMKNXPQQO3SIWVGQZJOC7 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --OWVEoR3cNa0wFH7h Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Feb 27, 2024 at 03:25:51PM +0100, Stefano Brivio wrote: > On Thu, 22 Feb 2024 16:55:59 +1100 > David Gibson wrote: >=20 > > We have several places where we want to write(2) a buffer or buffers an= d we > > do (or should) handle sort write()s by retrying until everything is >=20 > After making sure that "handle sorting" doesn't exist (yet): is one > between "handle" and "sort" redundant, or is there another meaning to > this? Oops. s/sort/short/ is what makes this make sense. >=20 > > successfully written. Add a helper for this in util.c. > >=20 > > This version has some differences from the typical write_all() function. > > First, take an IO vector rather than a single buffer, because that will= be > > useful for some of our cases. Second, allow it to take an parameter to > > skip the first n bytes of the given buffers. This will be usefl for so= me > > of the cases we want, and also falls out quite naturally from the > > implementation. > >=20 > > Signed-off-by: David Gibson > > --- > > util.c | 32 ++++++++++++++++++++++++++++++++ > > util.h | 1 + > > 2 files changed, 33 insertions(+) > >=20 > > diff --git a/util.c b/util.c > > index fb6a0430..a475f2b5 100644 > > --- a/util.c > > +++ b/util.c > > @@ -19,6 +19,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -597,3 +598,34 @@ size_t iov_offset(const struct iovec *iov, size_t = n, size_t *offset) > > =20 > > return i; > > } > > + > > +/* write_remainder() - write the tail of an IO vector to an fd > > + * @fd: File descriptor > > + * @iov: IO vector > > + * @iovcnt: Number of entries in @iov > > + * @skip: Number of bytes of the vector to skip writing > > + * > > + * Return: 0 on success, -1 on error (with errno set) > > + * > > + * #syscalls write writev > > + */ > > +int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_= t skip) > > +{ > > + int i; > > + > > + while ((i =3D iov_offset(iov, iovcnt, &skip)) < iovcnt) { >=20 > With my proposal for 1/6 this becomes: >=20 > while ((i =3D iov_entry_index(iov, iovcnt, skip, &offset)) < iovcnt) { Hrm, the easiest conversion is to use (..., skip, &skip). Using a new variable means we'd need to feed that back into skip somehow on the next loop. >=20 > if (offset) > ... >=20 > which I don't really find brilliant, but at least we don't do if (skip) > where 'skip' used to be something completely different. So.. the version I have now you might not like based on this comment, because it still has 'skip' essentially become a local variable with a different meaning from the one it has at entry. > > + ssize_t rc; > > + > > + if (skip) >=20 > Curly brackets here for consistency (undecided about readability to be > honest). Uh.. consistency with what? We don't typically brace single line clauses in passt. >=20 > > + rc =3D write(fd, (char *)iov[i].iov_base + skip, > > + iov[i].iov_len - skip); > > + else > > + rc =3D writev(fd, &iov[i], iovcnt - i); > > + > > + if (rc < 0) > > + return -1; > > + skip +=3D rc; > > + } > > + > > + return 0; > > +} > > diff --git a/util.h b/util.h > > index 62fad6fe..ee380f55 100644 > > --- a/util.h > > +++ b/util.h > > @@ -230,6 +230,7 @@ int __daemon(int pidfile_fd, int devnull_fd); > > int fls(unsigned long x); > > int write_file(const char *path, const char *buf); > > size_t iov_offset(const struct iovec *iov, size_t n, size_t *offset); > > +int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_= t skip); > > =20 > > /** > > * mod_sub() - Modular arithmetic subtraction >=20 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --OWVEoR3cNa0wFH7h Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXegesACgkQzQJF27ox 2Gc5bhAAjUDjs36+v4FIBTm5YdWgDqEfJAsZOH+F+wfTCrPqgTVjR28LF84UH8Mt VlafepdmNttY3x5jHuXmHlEbLcor+/mZn4PtgeQcbRRMYSmXaoi8naN8Oxecf9LR ZDU4cjRmP1qYPMbnp3lD/JlcfLR+DsD3U9WIUIrLBuVoghksxm21itn8O5SbtkvN UGJA63CLA/Xioc2Sdd8b8DfetRQ0N6IxfGRHUhJh4cAF/Zpbq3a4wuSB8A1O3VTc 0TEjRfNacNDKFHgofX4g+ewyCsD85dcK7TFBLFcIgrMaIEPV/DmjQ/cGc2cJdBqB dypGgRQkDQWkFMrV08BUtZ1K/tqU0SRkFPy5YHZ3zDSTBzdrt8LUOdH43jW2r+FU hr/1s3kVCh3tFgI4I0iwuTuBesAr7IS13IQPl/ALGzbkqA6Xp6T+qdG2WDTEjaLl 4olm+rI0tRQGQisgXGepnlBfmdGkJVV5HIZa7x2FMwGlK9f/vFL+cXZx134bqsP4 /A33GswJm2YtV8KU6PRc06yZ9j5WQ/n9vQhmZQGfwkxmONncdVCA07svnfRnA8CF YYZENo7I0o/HuTyTXkHRIQhV8b154R7WApD7Oz549IVoK1ydgrR8hcHSCw5ZurLr pbcM07NCndSsni7TYzWVl8XHkAAjgOuBIpJEOHSFnW8kv+Q7dcw= =rMD3 -----END PGP SIGNATURE----- --OWVEoR3cNa0wFH7h--