[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 21/28] block: try BSD disk size ioctls one after another
From: |
Paolo Bonzini |
Subject: |
[PULL 21/28] block: try BSD disk size ioctls one after another |
Date: |
Fri, 25 Jun 2021 16:18:15 +0200 |
Try all the possible ioctls for disk size as long as they are
supported, to keep the #if ladder simple.
Extracted and cleaned up from a patch by Joelle van Dyne and
Warner Losh.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
block/file-posix.c | 34 ++++++++++++++++------------------
1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index e56bb491a1..f16d987c07 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2327,39 +2327,37 @@ static int64_t raw_getlength(BlockDriverState *bs)
again:
#endif
if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+ size = 0;
#ifdef DIOCGMEDIASIZE
- if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
-#elif defined(DIOCGPART)
- {
- struct partinfo pi;
- if (ioctl(fd, DIOCGPART, &pi) == 0)
- size = pi.media_size;
- else
- size = 0;
+ if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {
+ size = 0;
+ }
+#endif
+#ifdef DIOCGPART
+ if (size == 0) {
+ struct partinfo pi;
+ if (ioctl(fd, DIOCGPART, &pi) == 0) {
+ size = pi.media_size;
+ }
}
- if (size == 0)
#endif
#if defined(__APPLE__) && defined(__MACH__)
- {
+ if (size == 0) {
uint64_t sectors = 0;
uint32_t sector_size = 0;
if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0
&& ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) {
size = sectors * sector_size;
- } else {
- size = lseek(fd, 0LL, SEEK_END);
- if (size < 0) {
- return -errno;
- }
}
}
-#else
- size = lseek(fd, 0LL, SEEK_END);
+#endif
+ if (size == 0) {
+ size = lseek(fd, 0LL, SEEK_END);
+ }
if (size < 0) {
return -errno;
}
-#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
switch(s->type) {
case FTYPE_CD:
--
2.31.1
- [PULL 07/28] configure, meson: convert libtasn1 detection to meson, (continued)
- [PULL 07/28] configure, meson: convert libtasn1 detection to meson, Paolo Bonzini, 2021/06/25
- [PULL 12/28] KVM: Fix dirty ring mmap incorrect size due to renaming accident, Paolo Bonzini, 2021/06/25
- [PULL 13/28] file-posix: fix max_iov for /dev/sg devices, Paolo Bonzini, 2021/06/25
- [PULL 14/28] scsi-generic: pass max_segments via max_iov field in BlockLimits, Paolo Bonzini, 2021/06/25
- [PULL 16/28] block-backend: align max_transfer to request alignment, Paolo Bonzini, 2021/06/25
- [PULL 15/28] osdep: provide ROUND_DOWN macro, Paolo Bonzini, 2021/06/25
- [PULL 17/28] block: add max_hw_transfer to BlockLimits, Paolo Bonzini, 2021/06/25
- [PULL 18/28] file-posix: try BLKSECTGET on block devices too, do not round to power of 2, Paolo Bonzini, 2021/06/25
- [PULL 19/28] block: feature detection for host block support, Paolo Bonzini, 2021/06/25
- [PULL 21/28] block: try BSD disk size ioctls one after another,
Paolo Bonzini <=
- [PULL 20/28] block: check for sys/disk.h, Paolo Bonzini, 2021/06/25
- [PULL 22/28] block: detect DKIOCGETBLOCKCOUNT/SIZE before use, Paolo Bonzini, 2021/06/25
- [PULL 23/28] file-posix: handle EINTR during ioctl, Paolo Bonzini, 2021/06/25
- [PULL 25/28] machine: move common smp_parse code to caller, Paolo Bonzini, 2021/06/25
- [PULL 26/28] machine: add error propagation to mc->smp_parse, Paolo Bonzini, 2021/06/25
- [PULL 24/28] machine: move dies from X86MachineState to CpuTopology, Paolo Bonzini, 2021/06/25
- [PULL 27/28] machine: pass QAPI struct to mc->smp_parse, Paolo Bonzini, 2021/06/25
- [PULL 28/28] machine: reject -smp dies!=1 for non-PC machines, Paolo Bonzini, 2021/06/25
- Re: [PULL v2 00/28] Misc (including block file-posix) for 2021-06-23, Peter Maydell, 2021/06/29