git: 5c20bfc78706 - main - gpart: Avoid geom_gettree() call in "bootcode -b".

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Tue, 08 Mar 2022 19:01:08 UTC
The branch main has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=5c20bfc78706d2fe6e262ddb55af0a8b928d3835

commit 5c20bfc78706d2fe6e262ddb55af0a8b928d3835
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2022-03-08 18:44:22 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2022-03-08 19:01:04 +0000

    gpart: Avoid geom_gettree() call in "bootcode -b".
    
    geom_gettree() may be pretty expensive on large systems, and it is
    not needed if only -b flag specified, that is processed by kernel.
    
    MFC after:      1 month
---
 lib/geom/part/geom_part.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/lib/geom/part/geom_part.c b/lib/geom/part/geom_part.c
index 41131646e246..330a4708251d 100644
--- a/lib/geom/part/geom_part.c
+++ b/lib/geom/part/geom_part.c
@@ -1217,6 +1217,14 @@ gpart_bootcode(struct gctl_req *req, unsigned int fl)
 	} else
 		bootcode = NULL;
 
+	if (!gctl_has_param(req, GPART_PARAM_PARTCODE)) {
+		if (bootcode == NULL)
+			errx(EXIT_FAILURE, "neither -b nor -p specified");
+		if (gctl_has_param(req, GPART_PARAM_INDEX))
+			errx(EXIT_FAILURE, "-i is only valid with -p");
+		goto nopartcode;
+	}
+
 	s = gctl_get_ascii(req, "class");
 	if (s == NULL)
 		abort();
@@ -1244,22 +1252,7 @@ gpart_bootcode(struct gctl_req *req, unsigned int fl)
 	else
 		vtoc8 = 0;
 
-	if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
-		s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
-		if (vtoc8 != 0)
-			partsize = VTOC_BOOTSIZE;
-		else
-			partsize = 1024 * 1024;		/* Arbitrary limit. */
-		partcode = gpart_bootfile_read(s, &partsize);
-		error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
-		if (error)
-			errc(EXIT_FAILURE, error, "internal error");
-	} else
-		partcode = NULL;
-
 	if (gctl_has_param(req, GPART_PARAM_INDEX)) {
-		if (partcode == NULL)
-			errx(EXIT_FAILURE, "-i is only valid with -p");
 		idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
 		if (idx < 1)
 			errx(EXIT_FAILURE, "invalid partition index");
@@ -1269,7 +1262,16 @@ gpart_bootcode(struct gctl_req *req, unsigned int fl)
 	} else
 		idx = 0;
 
-	if (partcode != NULL) {
+	if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
+		s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
+		if (vtoc8 != 0)
+			partsize = VTOC_BOOTSIZE;
+		else
+			partsize = 1024 * 1024;		/* Arbitrary limit. */
+		partcode = gpart_bootfile_read(s, &partsize);
+		error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
+		if (error)
+			errc(EXIT_FAILURE, error, "internal error");
 		if (vtoc8 == 0) {
 			if (idx == 0)
 				errx(EXIT_FAILURE, "missing -i option");
@@ -1279,15 +1281,14 @@ gpart_bootcode(struct gctl_req *req, unsigned int fl)
 				errx(EXIT_FAILURE, "invalid bootcode");
 			gpart_write_partcode_vtoc8(gp, idx, partcode);
 		}
-	} else
-		if (bootcode == NULL)
-			errx(EXIT_FAILURE, "no -b nor -p");
+		free(partcode);
+	}
 
+	geom_deletetree(&mesh);
+
+nopartcode:
 	if (bootcode != NULL)
 		gpart_issue(req, fl);
-
-	geom_deletetree(&mesh);
-	free(partcode);
 }
 
 static void