Page MenuHomeFreeBSD

D21034.id60039.diff
No OneTemporary

D21034.id60039.diff

Index: sbin/swapon/swapon.c
===================================================================
--- sbin/swapon/swapon.c
+++ sbin/swapon/swapon.c
@@ -746,9 +746,11 @@
off_t ioarg[2], sz;
int error, fd;
+ /* Open a connection to 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,27 @@
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)");
/*
- * 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.
+ * Start using the device for swapping. This opens a second connection
+ * to the device.
*/
error = swapon(name);
+
+ /*
+ * Close the first connection to the device, leaving the second one
+ * intact. Deferring the close to this point ensures that once the
+ * device is opened, the number of connections cannot fall to zero
+ * before swapon is invoked. For geli devices created with the 'detach
+ * -l' option, closing the last connection causes the device to be
+ * detached - that is, to disappear. This ordering ensures that the
+ * last connection isn't closed until swapoff is called.
+ */
close(fd);
return (error);
}

File Metadata

Mime Type
text/plain
Expires
Sun, Mar 15, 7:32 PM (20 h, 28 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
29730155
Default Alt Text
D21034.id60039.diff (1 KB)

Event Timeline