Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Jul 2019 20:47:08 +0000 (UTC)
From:      Doug Moore <dougm@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r350183 - head/sbin/swapon
Message-ID:  <201907202047.x6KKl8pJ005421@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dougm
Date: Sat Jul 20 20:47:07 2019
New Revision: 350183
URL: https://svnweb.freebsd.org/changeset/base/350183

Log:
  In trimming on startup, invoke swapon before closing the fd used for
  trimming so that a geli device isn't detached before swapon is
  invoked.
  
  Submitted by: sigsys_gmail.com
  Discussed with: alc
  Approved by: markj (mentor)
  Differential Revision:	https://reviews.freebsd.org/D21006

Modified:
  head/sbin/swapon/swapon.c

Modified: head/sbin/swapon/swapon.c
==============================================================================
--- head/sbin/swapon/swapon.c	Sat Jul 20 18:25:41 2019	(r350182)
+++ head/sbin/swapon/swapon.c	Sat Jul 20 20:47:07 2019	(r350183)
@@ -739,12 +739,12 @@ 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;
 
 	fd = open(name, O_WRONLY);
 	if (fd < 0)
@@ -762,7 +762,16 @@ swap_trim(const char *name)
 	ioarg[1] = sz;
 	if (ioctl(fd, DIOCGDELETE, ioarg) != 0)
 		warn("ioctl(DIOCGDELETE)");
+
+	/*
+	 * swapon is invoked after trimming, so that the trimming doesn't happen
+	 * after the device is in use for swapping, but before the fd is closed,
+	 * for the benefit of geli, which could otherwise detach the device,
+	 * before swapon, on close.
+	 */
+	error = swapon(name);
 	close(fd);
+	return (error);
 }
 
 static const char *
@@ -770,11 +779,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?201907202047.x6KKl8pJ005421>