From patchwork Mon Nov 2 09:13:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Guangrong X-Patchwork-Id: 7534881 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6D201BEEA4 for ; Mon, 2 Nov 2015 09:20:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A3B01205EB for ; Mon, 2 Nov 2015 09:20:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9D9BC20551 for ; Mon, 2 Nov 2015 09:20:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753465AbbKBJUC (ORCPT ); Mon, 2 Nov 2015 04:20:02 -0500 Received: from mga02.intel.com ([134.134.136.20]:57835 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752442AbbKBJTp (ORCPT ); Mon, 2 Nov 2015 04:19:45 -0500 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 02 Nov 2015 01:19:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,234,1444719600"; d="scan'208";a="840963119" Received: from xiaoreal1.sh.intel.com (HELO xiaoreal1.sh.intel.com.sh.intel.com) ([10.239.48.79]) by fmsmga002.fm.intel.com with ESMTP; 02 Nov 2015 01:19:30 -0800 From: Xiao Guangrong To: pbonzini@redhat.com, imammedo@redhat.com Cc: gleb@kernel.org, mtosatti@redhat.com, stefanha@redhat.com, mst@redhat.com, rth@twiddle.net, ehabkost@redhat.com, dan.j.williams@intel.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, vsementsov@virtuozzo.com, eblake@redhat.com, Xiao Guangrong Subject: [PATCH v7 12/35] util: let qemu_fd_getlength support block device Date: Mon, 2 Nov 2015 17:13:14 +0800 Message-Id: <1446455617-129562-13-git-send-email-guangrong.xiao@linux.intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> References: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP lseek can not work for all block devices as the man page says: | Some devices are incapable of seeking and POSIX does not specify | which devices must support lseek(). This patch tries to add the support on Linux by using BLKGETSIZE64 ioctl Signed-off-by: Xiao Guangrong Reviewed-by: Vladimir Sementsov-Ogievskiy --- util/osdep.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/util/osdep.c b/util/osdep.c index 5a61e19..b20c793 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -45,6 +45,11 @@ extern int madvise(caddr_t, size_t, int); #endif +#ifdef CONFIG_LINUX +#include +#include +#endif + #include "qemu-common.h" #include "qemu/sockets.h" #include "qemu/error-report.h" @@ -433,6 +438,21 @@ int64_t qemu_fd_getlength(int fd) { int64_t size; +#ifdef CONFIG_LINUX + struct stat stat_buf; + if (fstat(fd, &stat_buf) < 0) { + return -errno; + } + + if ((S_ISBLK(stat_buf.st_mode)) && !ioctl(fd, BLKGETSIZE64, &size)) { + /* The size of block device is larger than max int64_t? */ + if (size < 0) { + return -EOVERFLOW; + } + return size; + } +#endif + size = lseek(fd, 0, SEEK_END); if (size < 0) { return -errno;