From nobody Wed Jun 26 13:35:39 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.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=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580397972497713.8708329304533; Thu, 30 Jan 2020 07:26:12 -0800 (PST) Received: from localhost ([::1]:34176 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBhr-00032J-5u for importer@patchew.org; Thu, 30 Jan 2020 10:26:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54425) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBeU-0007Fb-0I for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixBeS-0007TD-SZ for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:41 -0500 Received: from relay.sw.ru ([185.231.240.75]:47748) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ixBeQ-0007Ic-8j; Thu, 30 Jan 2020 10:22:38 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ixBeK-0008C5-G4; Thu, 30 Jan 2020 18:22:32 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH 1/3] block/file-posix: add raw_getlength_fd Date: Thu, 30 Jan 2020 18:22:16 +0300 Message-Id: <20200130152218.7600-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200130152218.7600-1-vsementsov@virtuozzo.com> References: <20200130152218.7600-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Add function which can handle separate fd, to be called from raw_probe_alignment in the following commit. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/file-posix.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 1b805bd938..7f366046c2 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -178,7 +178,21 @@ typedef struct BDRVRawReopenState { } BDRVRawReopenState; =20 static int fd_open(BlockDriverState *bs); -static int64_t raw_getlength(BlockDriverState *bs); +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd); + +static int64_t raw_getlength(BlockDriverState *bs) +{ + BDRVRawState *s =3D bs->opaque; + int ret; + + ret =3D fd_open(bs); + if (ret < 0) { + return ret; + } + + return raw_getlength_fd(bs, s->fd); +} + =20 typedef struct RawPosixAIOData { BlockDriverState *bs; @@ -2063,10 +2077,8 @@ static int coroutine_fn raw_co_truncate(BlockDriverS= tate *bs, int64_t offset, } =20 #ifdef __OpenBSD__ -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd) { - BDRVRawState *s =3D bs->opaque; - int fd =3D s->fd; struct stat st; =20 if (fstat(fd, &st)) @@ -2082,10 +2094,8 @@ static int64_t raw_getlength(BlockDriverState *bs) return st.st_size; } #elif defined(__NetBSD__) -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd) { - BDRVRawState *s =3D bs->opaque; - int fd =3D s->fd; struct stat st; =20 if (fstat(fd, &st)) @@ -2107,22 +2117,16 @@ static int64_t raw_getlength(BlockDriverState *bs) return st.st_size; } #elif defined(__sun__) -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd) { - BDRVRawState *s =3D bs->opaque; struct dk_minfo minfo; int ret; int64_t size; =20 - ret =3D fd_open(bs); - if (ret < 0) { - return ret; - } - /* * Use the DKIOCGMEDIAINFO ioctl to read the size. */ - ret =3D ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); + ret =3D ioctl(fd, DKIOCGMEDIAINFO, &minfo); if (ret !=3D -1) { return minfo.dki_lbsize * minfo.dki_capacity; } @@ -2131,17 +2135,16 @@ static int64_t raw_getlength(BlockDriverState *bs) * There are reports that lseek on some devices fails, but * irc discussion said that contingency on contingency was overkill. */ - size =3D lseek(s->fd, 0, SEEK_END); + size =3D lseek(fd, 0, SEEK_END); if (size < 0) { return -errno; } return size; } #elif defined(CONFIG_BSD) -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd) { BDRVRawState *s =3D bs->opaque; - int fd =3D s->fd; int64_t size; struct stat sb; #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) @@ -2212,9 +2215,8 @@ again: return size; } #else -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength_fd(BlockDriverState *bs, int fd) { - BDRVRawState *s =3D bs->opaque; int ret; int64_t size; =20 @@ -2223,7 +2225,7 @@ static int64_t raw_getlength(BlockDriverState *bs) return ret; } =20 - size =3D lseek(s->fd, 0, SEEK_END); + size =3D lseek(fd, 0, SEEK_END); if (size < 0) { return -errno; } --=20 2.21.0 From nobody Wed Jun 26 13:35:39 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.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=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580397832864708.4680322061287; Thu, 30 Jan 2020 07:23:52 -0800 (PST) Received: from localhost ([::1]:34130 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBfa-00006E-7u for importer@patchew.org; Thu, 30 Jan 2020 10:23:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54422) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBeT-0007FF-PK for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixBeS-0007Sm-Od for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:41 -0500 Received: from relay.sw.ru ([185.231.240.75]:47756) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ixBeQ-0007Ii-8e; Thu, 30 Jan 2020 10:22:38 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ixBeK-0008C5-M3; Thu, 30 Jan 2020 18:22:32 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH 2/3] block/file-posix: consider file size when fallback to max_align Date: Thu, 30 Jan 2020 18:22:17 +0300 Message-Id: <20200130152218.7600-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200130152218.7600-1-vsementsov@virtuozzo.com> References: <20200130152218.7600-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" If we failed to probe request_align, we fallback to max_align. But this is wrong, if file size is not aligned to our max_align. Let's instead chose alignment so that file size is a multiple of it. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/file-posix.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/block/file-posix.c b/block/file-posix.c index 7f366046c2..e9c4e509f6 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -385,11 +385,25 @@ static void raw_probe_alignment(BlockDriverState *bs,= int fd, Error **errp) align =3D alignments[i]; if (raw_is_io_aligned(fd, buf, align)) { /* Fallback to safe value. */ - bs->bl.request_alignment =3D (align !=3D 1) ? align : max_= align; + bs->bl.request_alignment =3D align; break; } } qemu_vfree(buf); + + if (bs->bl.request_alignment =3D=3D 1) { + /* + * Succeed to read with alignment =3D 1. But it may be unalloc= ated + * area on XFS, and we'll fail later if keep request_alignment= =3D 1. + * + * Chose safer alignment, keeping in mind file size if possibl= e. + */ + + int64_t len =3D raw_getlength_fd(bs, fd); + + bs->bl.request_alignment =3D + len <=3D 0 ? max_align : MIN(max_align, len & ~(len - = 1)); + } } =20 if (!s->buf_align) { --=20 2.21.0 From nobody Wed Jun 26 13:35:39 2024 Delivered-To: importer@patchew.org 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=lists.gnu.org; Authentication-Results: mx.zohomail.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=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580397916095411.8835148620659; Thu, 30 Jan 2020 07:25:16 -0800 (PST) Received: from localhost ([::1]:34154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBgu-00024x-8x for importer@patchew.org; Thu, 30 Jan 2020 10:25:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54418) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixBeT-0007F5-NC for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixBeS-0007Sa-MR for qemu-devel@nongnu.org; Thu, 30 Jan 2020 10:22:41 -0500 Received: from relay.sw.ru ([185.231.240.75]:47746) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ixBeQ-0007Ia-8f; Thu, 30 Jan 2020 10:22:38 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ixBeK-0008C5-V8; Thu, 30 Jan 2020 18:22:33 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH 3/3] block: fail on open when file size is unaligned to request_alignment Date: Thu, 30 Jan 2020 18:22:18 +0300 Message-Id: <20200130152218.7600-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200130152218.7600-1-vsementsov@virtuozzo.com> References: <20200130152218.7600-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Prior to the commit the following command lead to crash: ./qemu-io --image-opts -c 'write 0 512' \ driver=3Dblkdebug,align=3D4096,image.driver=3Dnull-co,image.size=3D512 It failes on assertion in bdrv_aligned_pwritev: "end_sector <=3D bs->total_sectors || child->perm & BLK_PERM_RESIZE" The problem is obvious: 512 is aligned to 4096 and becomes larger than file size. And the core bad thing is that file size is unaligned to request_alignment. Let's catch such case on bdrv_open_driver and fail. Note, that file size and request_alignment may become out of sync later, so this commit is not full fix of the problem, but it's better than nothing. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/block.c b/block.c index ecd09dbbfd..4cfc6c33a2 100644 --- a/block.c +++ b/block.c @@ -1324,6 +1324,13 @@ static int bdrv_open_driver(BlockDriverState *bs, Bl= ockDriver *drv, assert(bdrv_min_mem_align(bs) !=3D 0); assert(is_power_of_2(bs->bl.request_alignment)); =20 + if (bs->bl.request_alignment > 512 && + !QEMU_IS_ALIGNED(bs->total_sectors, bs->bl.request_alignment / 512= )) + { + error_setg(errp, "File size is unaligned to request alignment"); + return -EINVAL; + } + for (i =3D 0; i < bs->quiesce_counter; i++) { if (drv->bdrv_co_drain_begin) { drv->bdrv_co_drain_begin(bs); --=20 2.21.0