From patchwork Sat Sep 8 14:15:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?546L5b2l5YKR?= X-Patchwork-Id: 10593125 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DE2906CB for ; Sat, 8 Sep 2018 15:06:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BCC3B2A42C for ; Sat, 8 Sep 2018 15:06:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE7BC2A779; Sat, 8 Sep 2018 15:06:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3CDDD2A42C for ; Sat, 8 Sep 2018 15:06:39 +0000 (UTC) Received: from localhost ([::1]:43056 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fyeoo-0008GW-12 for patchwork-qemu-devel@patchwork.kernel.org; Sat, 08 Sep 2018 11:06:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fye1X-00054u-Q5 for qemu-devel@nongnu.org; Sat, 08 Sep 2018 10:15:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fye1T-0005D3-L2 for qemu-devel@nongnu.org; Sat, 08 Sep 2018 10:15:43 -0400 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:47000) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fye1T-00059g-Eb; Sat, 08 Sep 2018 10:15:39 -0400 Received: by mail-pl1-x641.google.com with SMTP id t19-v6so7805886ply.13; Sat, 08 Sep 2018 07:15:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=L6MpPuV1P00ixL6Tv78ochPPgcGkl02AuvFJMrws0Zo=; b=d79EIfabsPnRSxRMXgbueX2S9yXXowrfU0jrI7tzqm3U94wetcDYpPnxpMmnbysan+ FMOn6vTe0Z8B79TsqcZL1ZfAv+TTphJhWB8EthckPbsru7zrInICevmE+LEdGVwU2QpM r31wosjw8rCOYvbbcawhSywBYp4EAa7dsXLQiRfARmAfE7mzvoHGN6AN1uZRc3YZjZvM APrcQsFvtf8S9koqWghuYELnymqOaWd3OlvSc9gc1+cPkmUMH54KVhkRLqXwJU1LYH9P VNy+5IXTJbxN7V6UR+ylw7dKgLugNRmSryKLmbW2wjk11nUfgeWok73OZrMbx9vXKjV5 5FuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=L6MpPuV1P00ixL6Tv78ochPPgcGkl02AuvFJMrws0Zo=; b=kIS7otT2jO51O81IrGHAw6iIJPhf87kmhu4yvPdqs77WB3MBNq+CcFIcltJrWXE+74 JN/9cXPyAYFOzMUFM7arXkgV6ZPYFmaW3v6ZMcqH1hpWuWQ/6UzsB0/dPsqj0/GZzL5X V6s6NjBYYazV7ReuXwpSFlqwPK//+pwDg5ikd13G1PZheY3k8nmBUQjUT5ZPyvVoLI1e x+0nz5vWaWsD+k4unlzw4+gJ5PFIRyhOjxHIg7BkLvFVp0fm0sBqQ7/5sfH/A819Lmpt wYA87VWgWieEjsa0PPoEgcsFU64T4Q4XCmR0dJKP8wd0fNQmCMUnDeNt8hynLKw8xz3H vMCA== X-Gm-Message-State: APzg51Aalozd6lwZcnI9CsoRzLoRK0hz2zWhh8f2NQ1Jo8aq+eLx9/af 5oTwWpKXFx7KZi9qBkIYlP/1bEE5mzc= X-Google-Smtp-Source: ANB0VdZAbSg54AklzmjtbDAlOxcHuQKBi9B6swf9RomrroYrc5wYjX9ygqePgMXd+VeqSqYOZTLv7A== X-Received: by 2002:a17:902:9a0c:: with SMTP id v12-v6mr13154595plp.159.1536416137709; Sat, 08 Sep 2018 07:15:37 -0700 (PDT) Received: from localhost.localdomain (1-175-123-62.dynamic-ip.hinet.net. [1.175.123.62]) by smtp.gmail.com with ESMTPSA id o62-v6sm14003109pfb.0.2018.09.08.07.15.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 08 Sep 2018 07:15:37 -0700 (PDT) From: Yan-Jie Wang To: qemu-devel@nongnu.org Date: Sat, 8 Sep 2018 22:15:28 +0800 Message-Id: <20180908141528.71260-1-jaywang0.tw@gmail.com> X-Mailer: git-send-email 2.17.1 (Apple Git-112) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::641 X-Mailman-Approved-At: Sat, 08 Sep 2018 11:05:49 -0400 Subject: [Qemu-devel] [PATCH] block/file-posix: fix the wrong result of find_allocation() in macOS. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yan-Jie Wang , qemu-stable@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP In macOS, lseek with SEEK_DATA behaves differently. It seeks to the next data region even though offset is in the middle of a data region. In addition, there may be many data regions without any hole among them, like this: |---Data---|---Data---| Because of this, qemu-img convert with raw images as input may create corrupted images in macOS especially for large files, and qemu-img map may also report wrong things. This patch fixes this undesired behaviors. Signed-off-by: Yan-Jie Wang --- block/file-posix.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index fe83cbf0eb..5c208580e6 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2325,6 +2325,7 @@ static int find_allocation(BlockDriverState *bs, off_t start, BDRVRawState *s = bs->opaque; off_t offs; +#if !(defined(__APPLE__) && defined(__MACH__)) /* * SEEK_DATA cases: * D1. offs == start: start is in data @@ -2395,6 +2396,64 @@ static int find_allocation(BlockDriverState *bs, off_t start, *hole = offs; return 0; } +#else + /* + * In macOS, lseek with SEEK_DATA seeks to the next data region + * even though the offset is in the middle of a data region. + * In addition, there may be many data regions without any holes among + * them, like this: |----Data----|----Data----| + * + * Although the behavior of lseek with SEEK_DATA is different in macOS, + * the behavior of lseek with SEEK_HOLE in macOS is the same as the one in + * Linux. + * + * Therefore, the cases D1, D2 and H2 are changed to the followings + * for macOS: + * D1. offs == start: start is at the beginning of a data region. + * D2. offs > start: either start is in a hole, next data at offs + * or start is in the middle of a data region, + * next data at offs. + * H2. offs > start: start is in data, next hole at offs + */ + + offs = lseek(s->fd, start, SEEK_HOLE); + if (offs < 0) { + return -errno; /* H3 or H4 */ + } + + if (offs < start) { + /* This is not a valid return by lseek(). We are safe to just return + * -EIO in this case, and we'll treat it like D4. */ + return -EIO; + } + + if (offs > start) { + /* H2: start is in data, next hole at offs */ + *data = start; + *hole = offs; + return 0; + } + + /* H1: start is in a hole */ + offs = lseek(s->fd, start, SEEK_DATA); + + if (offs < 0) { + return -errno; /* H1 and (D3 or D4) */ + } + + if (offs < start) { + /* This is not a valid return by lseek(). We are safe to just return + * -EIO in this case, and we'll treat it like D4. */ + return -EIO; + } + + if (offs > start) { + /* H1 and D2: start is in a hole, next data at offs */ + *hole = start; + *data = offs; + return 0; + } +#endif /* D1 and H1 */ return -EBUSY;