Received: from eggs.gnu.org ([2001:4830:134:3::10]:43685) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8RmK-0001eM-3H for qemu-devel@nongnu.org; Thu, 02 Jun 2016 08:31:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8RmE-0005Q6-Cd for qemu-devel@nongnu.org; Thu, 02 Jun 2016 08:31:12 -0400 References: <1464243305-10661-1-git-send-email-famz@redhat.com> <1464243305-10661-2-git-send-email-famz@redhat.com> From: Max Reitz Message-ID: <3290affb-9e0e-3bfe-49d8-56726bf04339@redhat.com> Date: Thu, 2 Jun 2016 14:30:54 +0200 MIME-Version: 1.0 In-Reply-To: <1464243305-10661-2-git-send-email-famz@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="tqGgt17rRw25ImSuVn48CQistgLxMH8Of" Subject: Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng , qemu-devel@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --tqGgt17rRw25ImSuVn48CQistgLxMH8Of From: Max Reitz To: Fam Zheng , qemu-devel@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org Message-ID: <3290affb-9e0e-3bfe-49d8-56726bf04339@redhat.com> Subject: Re: [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs References: <1464243305-10661-1-git-send-email-famz@redhat.com> <1464243305-10661-2-git-send-email-famz@redhat.com> In-Reply-To: <1464243305-10661-2-git-send-email-famz@redhat.com> Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable On 26.05.2016 08:15, Fam Zheng wrote: > This is sometimes a useful value we should count in. >=20 > Signed-off-by: Fam Zheng > --- > block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++= > 1 file changed, 47 insertions(+) >=20 > diff --git a/block/raw-posix.c b/block/raw-posix.c > index a4f5a1b..d3796ad 100644 > --- a/block/raw-posix.c > +++ b/block/raw-posix.c > @@ -729,9 +729,56 @@ static void raw_reopen_abort(BDRVReopenState *stat= e) > state->opaque =3D NULL; > } > =20 > +static int hdev_get_max_transfer_length(dev_t dev) > +{ > + int ret; > + int fd; > + char *path; > + const char *end; > + char buf[32]; > + long len; > + > + path =3D g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_k= b", > + major(dev), minor(dev)); I can't say I like this very much, but well, it won't do any harm on any systems that do not offer this path (i.e. any non-Linux system, I suppose). So I'm fine with it. > + fd =3D open(path, O_RDONLY); > + if (fd < 0) { > + ret =3D -errno; > + goto out; > + } > + ret =3D read(fd, buf, sizeof(buf)); > + if (ret < 0) { > + ret =3D -errno; > + goto out; > + } else if (ret =3D=3D 0) { > + ret =3D -EIO; > + goto out; > + } > + buf[ret] =3D 0; Potential buffer overflow if ret =3D=3D sizeof(buf). > + /* The file is ended with '\n', pass 'end' to accept that. */ > + ret =3D qemu_strtol(buf, &end, 10, &len); > + if (ret =3D=3D 0 && end && *end =3D=3D '\n') { > + ret =3D len * 1024 / BDRV_SECTOR_SIZE; Maybe there should be an overflow check here. > + } > + > + close(fd); This belongs in some error path, because otherwise the FD is leaked if the read() failed. Max > +out: > + g_free(path); > + return ret; > +} > + > static void raw_refresh_limits(BlockDriverState *bs, Error **errp) > { > BDRVRawState *s =3D bs->opaque; > + struct stat st; > + > + if (!fstat(s->fd, &st)) { > + if (S_ISBLK(st.st_mode)) { > + int ret =3D hdev_get_max_transfer_length(st.st_rdev); > + if (ret >=3D 0) { > + bs->bl.max_transfer_length =3D ret; > + } > + } > + } > =20 > raw_probe_alignment(bs, s->fd, errp); > bs->bl.min_mem_alignment =3D s->buf_align; >=20 --tqGgt17rRw25ImSuVn48CQistgLxMH8Of Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBCAAGBQJXUCb+AAoJEDuxQgLoOKytZ/wH/iRZVP5h0ts0Ual4PzFdgw3/ m6fHLOVfhWLx8EhN1F9M8AWLJWPM2sL7Lq94Bk6A5lPdpB/HUGgED+m9+8fJcqsf rHyxXoJZCAWtqAXmkUHrR4TyuJRNpWTCoVDCv+UIOl4A8rIvU0sb3mZieMWX0Soi gk6s3275CYWEDP3KgnrkyDEgwSZKIzizcddbs04jKjwof32Bp1FHnQNCkHhWz2rP UNen+wwGcjFyFjxYkQip2JcJR0DtItxvbnTbiCwJ7SP74U0nLCziYv+rmnrVNLIU oVnuf7EkFjEd2mETkP8qb0q3MY6L5pkSGkonX+yw/QX6+JpWC7lOtNajog8/qco= =NnCq -----END PGP SIGNATURE----- --tqGgt17rRw25ImSuVn48CQistgLxMH8Of--