git: 847cdccdb0f1 - stable/13 - bhyve: get mediasize for character devices when resizing virtio-blk

From: Robert Wing <rew_at_FreeBSD.org>
Date: Wed, 23 Feb 2022 17:44:45 UTC
The branch stable/13 has been updated by rew:

URL: https://cgit.FreeBSD.org/src/commit/?id=847cdccdb0f1992b74f0c73805461396070511fc

commit 847cdccdb0f1992b74f0c73805461396070511fc
Author:     Robert Wing <rew@FreeBSD.org>
AuthorDate: 2022-01-18 20:26:49 +0000
Commit:     Robert Wing <rew@FreeBSD.org>
CommitDate: 2022-02-23 17:43:14 +0000

    bhyve: get mediasize for character devices when resizing virtio-blk
    
    Reviewed by:    imp, allanjude, jhb
    Differential Revision:  https://reviews.freebsd.org/D33403
    
    (cherry picked from commit ae9ea22e14bf523aee508f1fe07091580b528a2e)
---
 usr.sbin/bhyve/block_if.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/bhyve/block_if.c b/usr.sbin/bhyve/block_if.c
index b12029ec4f04..0d41b5175166 100644
--- a/usr.sbin/bhyve/block_if.c
+++ b/usr.sbin/bhyve/block_if.c
@@ -651,14 +651,24 @@ blockif_resized(int fd, enum ev_type type, void *arg)
 {
 	struct blockif_ctxt *bc;
 	struct stat sb;
+	off_t mediasize;
 
 	if (fstat(fd, &sb) != 0)
 		return;
 
+	if (S_ISCHR(sb.st_mode)) {
+		if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
+			EPRINTLN("blockif_resized: get mediasize failed: %s",
+			    strerror(errno));
+			return;
+		}
+	} else
+		mediasize = sb.st_size;
+
 	bc = arg;
 	pthread_mutex_lock(&bc->bc_mtx);
-	if (sb.st_size != bc->bc_size) {
-		bc->bc_size = sb.st_size;
+	if (mediasize != bc->bc_size) {
+		bc->bc_size = mediasize;
 		bc->bc_resize_cb(bc, bc->bc_resize_cb_arg, bc->bc_size);
 	}
 	pthread_mutex_unlock(&bc->bc_mtx);