Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 26 Jul 2019 15:18:11 +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: r350359 - head/sbin/swapon
Message-ID:  <201907261518.x6QFIBPk059384@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dougm
Date: Fri Jul 26 15:18:11 2019
New Revision: 350359
URL: https://svnweb.freebsd.org/changeset/base/350359

Log:
  Rewrite the comments that explain swapon_trim() to make them more
  comprehensible.
  
  Suggested by: rpokala
  Approved by: markj (mentor)
  Differential Revision: https://reviews.freebsd.org/D21034

Modified:
  head/sbin/swapon/swapon.c

Modified: head/sbin/swapon/swapon.c
==============================================================================
--- head/sbin/swapon/swapon.c	Fri Jul 26 13:12:33 2019	(r350358)
+++ head/sbin/swapon/swapon.c	Fri Jul 26 15:18:11 2019	(r350359)
@@ -746,9 +746,11 @@ swapon_trim(const char *name)
 	off_t ioarg[2], sz;
 	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,18 +760,22 @@ swapon_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);
+
 	/*
-	 * 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.
+	 * 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.
 	 */
-	error = swapon(name);
 	close(fd);
 	return (error);
 }



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