From patchwork Thu May 26 06:15:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 9136709 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 78332607D3 for ; Thu, 26 May 2016 06:53:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6059127D11 for ; Thu, 26 May 2016 06:53:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 533422803C; Thu, 26 May 2016 06:53:38 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 036BC27D11 for ; Thu, 26 May 2016 06:53:37 +0000 (UTC) Received: from localhost ([::1]:36819 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5pAm-0000Py-EO for patchwork-qemu-devel@patchwork.kernel.org; Thu, 26 May 2016 02:53:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36481) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5oZl-0007eh-06 for qemu-devel@nongnu.org; Thu, 26 May 2016 02:15:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5oZd-0000Xq-Rm for qemu-devel@nongnu.org; Thu, 26 May 2016 02:15:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40257) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5oZb-0000X1-NB; Thu, 26 May 2016 02:15:11 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5DB346408A; Thu, 26 May 2016 06:15:11 +0000 (UTC) Received: from ad.usersys.redhat.com (dhcp-15-133.nay.redhat.com [10.66.15.133]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4Q6F6S0011738; Thu, 26 May 2016 02:15:09 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Thu, 26 May 2016 14:15:04 +0800 Message-Id: <1464243305-10661-2-git-send-email-famz@redhat.com> In-Reply-To: <1464243305-10661-1-git-send-email-famz@redhat.com> References: <1464243305-10661-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 26 May 2016 06:15:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs 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: Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This is sometimes a useful value we should count in. Signed-off-by: Fam Zheng --- block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) 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 *state) state->opaque = NULL; } +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 = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_kb", + major(dev), minor(dev)); + fd = open(path, O_RDONLY); + if (fd < 0) { + ret = -errno; + goto out; + } + ret = read(fd, buf, sizeof(buf)); + if (ret < 0) { + ret = -errno; + goto out; + } else if (ret == 0) { + ret = -EIO; + goto out; + } + buf[ret] = 0; + /* The file is ended with '\n', pass 'end' to accept that. */ + ret = qemu_strtol(buf, &end, 10, &len); + if (ret == 0 && end && *end == '\n') { + ret = len * 1024 / BDRV_SECTOR_SIZE; + } + + close(fd); +out: + g_free(path); + return ret; +} + static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s = bs->opaque; + struct stat st; + + if (!fstat(s->fd, &st)) { + if (S_ISBLK(st.st_mode)) { + int ret = hdev_get_max_transfer_length(st.st_rdev); + if (ret >= 0) { + bs->bl.max_transfer_length = ret; + } + } + } raw_probe_alignment(bs, s->fd, errp); bs->bl.min_mem_alignment = s->buf_align;