Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 1 Aug 2019 05:30:31 +0000 (UTC)
From:      Doug Moore <dougm@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r350495 - stable/12/sbin/swapon
Message-ID:  <201908010530.x715UVEs016449@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dougm
Date: Thu Aug  1 05:30:31 2019
New Revision: 350495
URL: https://svnweb.freebsd.org/changeset/base/350495

Log:
  MFC r350183, r350359
  
  In trimming on startup, define swapon_trim() to invoke swapon before
  closing the fd used for trimming so that a geli device isn't detached
  before swapon is invoked.  Add comments to explain the reason for the
  ordering of events.
  
  Reviewed by: alc
  Approved by: markj (mentor, implicit)

Modified:
  stable/12/sbin/swapon/swapon.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sbin/swapon/swapon.c
==============================================================================
--- stable/12/sbin/swapon/swapon.c	Thu Aug  1 03:59:25 2019	(r350494)
+++ stable/12/sbin/swapon/swapon.c	Thu Aug  1 05:30:31 2019	(r350495)
@@ -739,16 +739,18 @@ run_cmd(int *ofd, const char *cmdline, ...)
 	return (WEXITSTATUS(status));
 }
 
-static void
-swap_trim(const char *name)
+static int
+swapon_trim(const char *name)
 {
 	struct stat sb;
 	off_t ioarg[2], sz;
-	int fd;
+	int error, fd;
 
+	/* Open a descriptor to create a consumer of the device. */
 	fd = open(name, O_WRONLY);
 	if (fd < 0)
 		errx(1, "Cannot open %s", name);
+	/* Find the device size. */
 	if (fstat(fd, &sb) < 0)
 		errx(1, "Cannot stat %s", name);
 	if (S_ISREG(sb.st_mode))
@@ -758,11 +760,24 @@ swap_trim(const char *name)
 			err(1, "ioctl(DIOCGMEDIASIZE)");
 	} else
 		errx(1, "%s has an invalid file type", name);
+	/* Trim the device. */
 	ioarg[0] = 0;
 	ioarg[1] = sz;
 	if (ioctl(fd, DIOCGDELETE, ioarg) != 0)
 		warn("ioctl(DIOCGDELETE)");
+
+	/* Start using the device for swapping, creating a second consumer. */
+	error = swapon(name);
+
+	/*
+	 * Do not close the device until the swap pager has attempted to create
+	 * another consumer.  For GELI devices created with the 'detach -l'
+	 * option, removing the last consumer causes the device to be detached
+	 * - that is, to disappear.  This ordering ensures that the device will
+	 * not be detached until swapoff is called.
+	 */
 	close(fd);
+	return (error);
 }
 
 static const char *
@@ -770,11 +785,9 @@ swap_on_off_sfile(const char *name, int doingall)
 {
 	int error;
 
-	if (which_prog == SWAPON) {
-		if (Eflag)
-			swap_trim(name);
-		error = swapon(name);
-	} else /* SWAPOFF */
+	if (which_prog == SWAPON)
+		error = Eflag ? swapon_trim(name) : swapon(name);
+	else /* SWAPOFF */
 		error = swapoff(name);
 
 	if (error == -1) {



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