Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Jan 2016 00:54:08 +0000 (UTC)
From:      Allan Jude <allanjude@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r293454 - in head/sys/boot: i386/loader userboot/userboot zfs
Message-ID:  <201601090054.u090s8IU052456@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: allanjude
Date: Sat Jan  9 00:54:08 2016
New Revision: 293454
URL: https://svnweb.freebsd.org/changeset/base/293454

Log:
  Only call init_zfs_bootenv() when the system was booted with ZFS
  
  Add a few other safeguards to ensure things do not break when the
  boot device cannot be determined
  
  Reported by:	flo
  MFC after:	3 days
  Sponsored by:	ScaleEngine Inc.

Modified:
  head/sys/boot/i386/loader/main.c
  head/sys/boot/userboot/userboot/main.c
  head/sys/boot/zfs/zfs.c

Modified: head/sys/boot/i386/loader/main.c
==============================================================================
--- head/sys/boot/i386/loader/main.c	Sat Jan  9 00:47:01 2016	(r293453)
+++ head/sys/boot/i386/loader/main.c	Sat Jan  9 00:54:08 2016	(r293454)
@@ -262,6 +262,7 @@ extract_currdev(void)
 	    new_currdev.d_kind.zfs.root_guid = 0;
 	}
 	new_currdev.d_dev = &zfs_dev;
+	init_zfs_bootenv(zfs_fmtdev(&new_currdev));
 #endif
     } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
 	/* The passed-in boot device is bad */
@@ -295,10 +296,6 @@ extract_currdev(void)
 	new_currdev.d_unit = 0;
     }
 
-#ifdef LOADER_ZFS_SUPPORT
-    init_zfs_bootenv(zfs_fmtdev(&new_currdev));
-#endif
-
     env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
 	       i386_setcurrdev, env_nounset);
     env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
@@ -311,9 +308,14 @@ init_zfs_bootenv(char *currdev)
 {
 	char *beroot;
 
+	if (strlen(currdev) == 0)
+		return;
+	if(strncmp(currdev, "zfs:", 4) != 0)
+		return;
 	/* Remove the trailing : */
 	currdev[strlen(currdev) - 1] = '\0';
 	setenv("zfs_be_active", currdev, 1);
+	setenv("zfs_be_currpage", "1", 1);
 	/* Do not overwrite if already set */
 	setenv("vfs.root.mountfrom", currdev, 0);
 	/* Forward past zfs: */
@@ -323,9 +325,7 @@ init_zfs_bootenv(char *currdev)
 	beroot = strrchr(currdev, '/');
 	if (beroot != NULL)
 		beroot[0] = '\0';
-
 	beroot = currdev;
-	
 	setenv("zfs_be_root", beroot, 1);
 }
 #endif
@@ -394,6 +394,7 @@ static int
 command_reloadbe(int argc, char *argv[])
 {
     int err;
+    char *root;
 
     if (argc > 2) {
 	command_errmsg = "wrong number of arguments";
@@ -403,6 +404,11 @@ command_reloadbe(int argc, char *argv[])
     if (argc == 2) {
 	err = zfs_bootenv(argv[1]);
     } else {
+	root = getenv("zfs_be_root");
+	if (root == NULL) {
+	    /* There does not appear to be a ZFS pool here, exit without error */
+	    return (CMD_OK);
+	}
 	err = zfs_bootenv(getenv("zfs_be_root"));
     }
 

Modified: head/sys/boot/userboot/userboot/main.c
==============================================================================
--- head/sys/boot/userboot/userboot/main.c	Sat Jan  9 00:47:01 2016	(r293453)
+++ head/sys/boot/userboot/userboot/main.c	Sat Jan  9 00:54:08 2016	(r293454)
@@ -168,6 +168,7 @@ extract_currdev(void)
 		zdev.d_type = zdev.d_dev->dv_type;
 		
 		dev = *(struct disk_devdesc *)&zdev;
+		init_zfs_bootenv(zfs_fmtdev(&dev));
 	} else
 #endif
 
@@ -191,10 +192,6 @@ extract_currdev(void)
 		dev.d_unit = 0;
 	}
 
-#if defined(USERBOOT_ZFS_SUPPORT)
-	init_zfs_bootenv(zfs_fmtdev(&dev));
-#endif
-
 	env_setenv("currdev", EV_VOLATILE, userboot_fmtdev(&dev),
             userboot_setcurrdev, env_nounset);
 	env_setenv("loaddev", EV_VOLATILE, userboot_fmtdev(&dev),
@@ -207,9 +204,14 @@ init_zfs_bootenv(char *currdev)
 {
 	char *beroot;
 
+	if (strlen(currdev) == 0)
+		return;
+	if(strncmp(currdev, "zfs:", 4) != 0)
+		return;
 	/* Remove the trailing : */
 	currdev[strlen(currdev) - 1] = '\0';
 	setenv("zfs_be_active", currdev, 1);
+	setenv("zfs_be_currpage", "1", 1);
 	/* Do not overwrite if already set */
 	setenv("vfs.root.mountfrom", currdev, 0);
 	/* Forward past zfs: */
@@ -219,9 +221,7 @@ init_zfs_bootenv(char *currdev)
 	beroot = strrchr(currdev, '/');
 	if (beroot != NULL)
 		beroot[0] = '\0';
-
 	beroot = currdev;
-	
 	setenv("zfs_be_root", beroot, 1);
 }
 
@@ -273,6 +273,7 @@ static int
 command_reloadbe(int argc, char *argv[])
 {
 	int err;
+	char *root;
 
 	if (argc > 2) {
 		command_errmsg = "wrong number of arguments";
@@ -282,7 +283,11 @@ command_reloadbe(int argc, char *argv[])
 	if (argc == 2) {
 		err = zfs_bootenv(argv[1]);
 	} else {
-		err = zfs_bootenv(getenv("zfs_be_root"));
+		root = getenv("zfs_be_root");
+		if (root == NULL) {
+			return (CMD_OK);
+		}
+		err = zfs_bootenv(root);
 	}
 
 	if (err != 0) {

Modified: head/sys/boot/zfs/zfs.c
==============================================================================
--- head/sys/boot/zfs/zfs.c	Sat Jan  9 00:47:01 2016	(r293453)
+++ head/sys/boot/zfs/zfs.c	Sat Jan  9 00:54:08 2016	(r293454)
@@ -712,13 +712,18 @@ zfs_list(const char *name)
 int
 zfs_bootenv(const char *name)
 {
-	static char	poolname[ZFS_MAXNAMELEN], *dsname;
+	static char	poolname[ZFS_MAXNAMELEN], *dsname, *root;
 	char		becount[4];
 	uint64_t	objid;
 	spa_t		*spa;
 	int		len, rv, pages, perpage, currpage;
 
-	if (strcmp(name, getenv("zfs_be_root")) != 0) {
+	if (name == NULL)
+		return (EINVAL);
+	if ((root = getenv("zfs_be_root")) == NULL)
+		return (EINVAL);
+
+	if (strcmp(name, root) != 0) {
 		if (setenv("zfs_be_root", name, 1) != 0)
 			return (ENOMEM);
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201601090054.u090s8IU052456>