git: 4bf34c597c44 - main - md(4): always trim the last partial sector

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 14 Oct 2024 08:08:59 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=4bf34c597c44a2393ac5ec483e9cecac128d8202

commit 4bf34c597c44a2393ac5ec483e9cecac128d8202
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-10-13 21:28:22 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-10-14 08:08:21 +0000

    md(4): always trim the last partial sector
    
    Do it also for the preloaded disk, in addition to the dynamically
    configured device.  This is needed to avoid geom checking alignment and
    panicing on read of the last sector, e.g. for partition schemes and
    label tasting.
    
    PR:     281978
    Reported by:    bz
    Reviewed by:    bz, markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D47102
---
 sys/dev/md/md.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/sys/dev/md/md.c b/sys/dev/md/md.c
index af05373ae1d8..5d4f91d05917 100644
--- a/sys/dev/md/md.c
+++ b/sys/dev/md/md.c
@@ -1297,6 +1297,7 @@ mdinit(struct md_s *sc)
 {
 	struct g_geom *gp;
 	struct g_provider *pp;
+	unsigned remn;
 
 	g_topology_lock();
 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
@@ -1305,6 +1306,13 @@ mdinit(struct md_s *sc)
 	devstat_remove_entry(pp->stat);
 	pp->stat = NULL;
 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
+	/* Prune off any residual fractional sector. */
+	remn = sc->mediasize % sc->sectorsize;
+	if (remn != 0) {
+		printf("md%d: truncating fractional last sector by %u bytes\n",
+		    sc->unit, remn);
+		sc->mediasize -= remn;
+	}
 	pp->mediasize = sc->mediasize;
 	pp->sectorsize = sc->sectorsize;
 	switch (sc->type) {
@@ -1674,7 +1682,7 @@ kern_mdattach_locked(struct thread *td, struct md_req *mdr)
 {
 	struct md_s *sc;
 	unsigned sectsize;
-	int error, i;
+	int error;
 
 	sx_assert(&md_sx, SA_XLOCKED);
 
@@ -1746,10 +1754,6 @@ err_after_new:
 		return (error);
 	}
 
-	/* Prune off any residual fractional sector */
-	i = sc->mediasize % sc->sectorsize;
-	sc->mediasize -= i;
-
 	mdinit(sc);
 	return (0);
 }