git: 959f0d0cdb14 - stable/14 - md(4): always trim the last partial sector

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 20 Oct 2024 00:34:49 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=959f0d0cdb1482d754f24b7ae16bda0ac2e7b31a

commit 959f0d0cdb1482d754f24b7ae16bda0ac2e7b31a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-10-13 21:28:22 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-10-20 00:33:04 +0000

    md(4): always trim the last partial sector
    
    PR:     281978
    
    (cherry picked from commit 4bf34c597c44a2393ac5ec483e9cecac128d8202)
---
 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 1d531b88193f..3285cfef830d 100644
--- a/sys/dev/md/md.c
+++ b/sys/dev/md/md.c
@@ -1303,6 +1303,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);
@@ -1311,6 +1312,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) {
@@ -1680,7 +1688,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);
 
@@ -1752,10 +1760,6 @@ err_after_new:
 		return (error);
 	}
 
-	/* Prune off any residual fractional sector */
-	i = sc->mediasize % sc->sectorsize;
-	sc->mediasize -= i;
-
 	mdinit(sc);
 	return (0);
 }