From patchwork Thu Jan 26 17:22:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 138042 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (unknown [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3F46BB6EF1 for ; Fri, 27 Jan 2012 07:49:36 +1100 (EST) Received: from localhost ([::1]:48497 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqT3c-0006Kd-Vc for incoming@patchwork.ozlabs.org; Thu, 26 Jan 2012 12:24:20 -0500 Received: from eggs.gnu.org ([140.186.70.92]:52559) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqT2q-0004Wf-QB for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:23:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RqT2k-0003Pk-P6 for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:23:32 -0500 Received: from mail-gx0-f173.google.com ([209.85.161.173]:46582) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RqT2k-0003NH-Ko for qemu-devel@nongnu.org; Thu, 26 Jan 2012 12:23:26 -0500 Received: by mail-gx0-f173.google.com with SMTP id h1so470435ggn.4 for ; Thu, 26 Jan 2012 09:23:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=vtCaYK4Ijutw8uMxezeINzfgq7sKVexTVfOAP8F+1Us=; b=Jc6bzU602NtbX9Q1g5s8GSK9yn8aJvP+YrPHjMxf6ieyhoniVBS4WzGkjeRM/zVdz3 8huyqkrn/0hOmmbMW4LsXt/BCtFbnLn08yuBqIndf6/GTAPSYTyF2oK6QECnHytv+Unq 7ExNAfMfPMVfmltD1LZn20KWRNznBfRuKjmSY= Received: by 10.50.169.9 with SMTP id aa9mr3527455igc.23.1327598605809; Thu, 26 Jan 2012 09:23:25 -0800 (PST) Received: from yakj.usersys.redhat.com (93-34-182-16.ip50.fastwebnet.it. [93.34.182.16]) by mx.google.com with ESMTPS id ak10sm2142476igc.6.2012.01.26.09.23.23 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 26 Jan 2012 09:23:24 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 26 Jan 2012 18:22:41 +0100 Message-Id: <1327598569-5199-11-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: <1327598569-5199-1-git-send-email-pbonzini@redhat.com> References: <1327598569-5199-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.161.173 Cc: kwolf@redhat.com Subject: [Qemu-devel] [PATCH v2 10/18] raw: probe host_block_size X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Use ioctls if possible, else see what alignment it takes for O_DIRECT to succeed. Signed-off-by: Paolo Bonzini --- block/raw-posix.c | 72 ++++++++++++++++++++++++++++++++++++++++------------ block/raw-win32.c | 42 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 17 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index 49a8c21..3537394 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -52,6 +52,7 @@ #include #include #include +#include #endif #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) #include @@ -179,6 +180,58 @@ static int raw_normalize_devicepath(const char **filename) } #endif +static void raw_probe_alignment(BlockDriverState *bs) +{ + BDRVRawState *s = bs->opaque; + char *buf; + unsigned int sector_size; + + /* For /dev/sg devices the alignment is not really used. */ + if (bs->sg) { + return; + } + + /* For block devices, try to get the actual sector size even if we + * do not need it, so that it can be passed down to the guest. + */ +#ifdef BLKSSZGET + if (ioctl(s->fd, BLKSSZGET, §or_size) >= 0) { + bs->host_block_size = sector_size; + } +#endif +#ifdef DKIOCGETBLOCKSIZE + if (ioctl(s->fd, DKIOCGETBLOCKSIZE, §or_size) >= 0) { + bs->host_block_size = sector_size; + } +#endif +#ifdef DIOCGSECTORSIZE + if (ioctl(s->fd, DIOCGSECTORSIZE, §or_size) >= 0) { + bs->host_block_size = sector_size; + } +#endif + + /* If we could not get the size so far, we can only guess it if the file + * was opened with O_DIRECT. Using the minimal value (512) is okay. + * It may or may not be safe if the guest logical block size is >512; + * however, we will print a scary message suggesting usage of cache=none. + * If they hear our advice, the host block size will be detected correctly + * and the scary message will go away. + */ + if (!(bs->open_flags & BDRV_O_NOCACHE)) { + return; + } + + buf = qemu_memalign(MAX_BLOCKSIZE, MAX_BLOCKSIZE); + for (sector_size = 512; sector_size < MAX_BLOCKSIZE; sector_size <<= 1) { + /* The buffer must be aligned to sector_size, but not sector_size*2. */ + if (pread(s->fd, buf + sector_size, sector_size, 0) >= 0) { + break; + } + } + bs->host_block_size = sector_size; + qemu_vfree(buf); +} + static int raw_open_common(BlockDriverState *bs, const char *filename, int bdrv_flags, int open_flags) { @@ -214,6 +267,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, return ret; } s->fd = fd; + raw_probe_alignment(bs); /* We're falling back to POSIX AIO in some cases so init always */ if (paio_init() < 0) { @@ -262,22 +316,6 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags) return raw_open_common(bs, filename, flags, 0); } -/* XXX: use host sector size if necessary with: -#ifdef DIOCGSECTORSIZE - { - unsigned int sectorsize = 512; - if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && - sectorsize > bufsize) - bufsize = sectorsize; - } -#endif -#ifdef CONFIG_COCOA - uint32_t blockSize = 512; - if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { - bufsize = blockSize; - } -#endif -*/ /* * Check if all memory in this vector is sector aligned. @@ -287,7 +325,7 @@ static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) int i; for (i = 0; i < qiov->niov; i++) { - if ((uintptr_t) qiov->iov[i].iov_base % bs->guest_block_size) { + if ((uintptr_t) qiov->iov[i].iov_base % bs->host_block_size) { return 0; } } diff --git a/block/raw-win32.c b/block/raw-win32.c index e4b0b75..d8b76de 100644 --- a/block/raw-win32.c +++ b/block/raw-win32.c @@ -77,6 +77,35 @@ static int set_sparse(int fd) NULL, 0, NULL, 0, &returned, NULL); } +static void raw_probe_alignment(BlockDriverState *bs) +{ + BDRVRawState *s = bs->opaque; + DWORD sectorsPerCluster, freeClusters, totalClusters, count; + DISK_GEOMETRY_EX dg; + BOOL status; + + if (s->type == FTYPE_CD) { + bs->host_block_size = 2048; + return; + } + if (s->type == FTYPE_HARDDISK) { + status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, + NULL, 0, &dg, sizeof(dg), &count, NULL); + if (status != 0) { + bs->host_block_size = dg.Geometry.BytesPerSector; + return; + } + /* try GetDiskFreeSpace too */ + } + + if (s->drive_path[0]) { + GetDiskFreeSpace(s->drive_path, §orsPerCluster, + &dg.Geometry.BytesPerSector, + &freeClusters, &totalClusters); + bs->host_block_size = dg.Geometry.BytesPerSector; + } +} + static int raw_open(BlockDriverState *bs, const char *filename, int flags) { BDRVRawState *s = bs->opaque; @@ -96,6 +125,18 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags) overlapped |= FILE_FLAG_NO_BUFFERING; if (!(flags & BDRV_O_CACHE_WB)) overlapped |= FILE_FLAG_WRITE_THROUGH; + + if (filename[0] && filename[1] == ':') { + snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); + } else if (filename[0] == '\\' && filename[1] == '\\') { + s->drive_path[0] = 0; + } else { + /* Relative path. */ + char buf[MAX_PATH]; + GetCurrentDirectory(MAX_PATH, buf); + snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); + } + s->hfile = CreateFile(filename, access_flags, FILE_SHARE_READ, NULL, OPEN_EXISTING, overlapped, NULL); @@ -106,6 +147,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags) return -EACCES; return -1; } + raw_probe_alignment(bs); return 0; }