Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/swapon/swapon.c
Show First 20 Lines • Show All 740 Lines • ▼ Show 20 Lines | |||||
static int | static int | ||||
swapon_trim(const char *name) | swapon_trim(const char *name) | ||||
{ | { | ||||
struct stat sb; | struct stat sb; | ||||
off_t ioarg[2], sz; | off_t ioarg[2], sz; | ||||
int error, fd; | int error, fd; | ||||
/* Open a descriptor to create a consumer of the device. */ | |||||
fd = open(name, O_WRONLY); | fd = open(name, O_WRONLY); | ||||
if (fd < 0) | if (fd < 0) | ||||
errx(1, "Cannot open %s", name); | errx(1, "Cannot open %s", name); | ||||
/* Find the device size. */ | |||||
if (fstat(fd, &sb) < 0) | if (fstat(fd, &sb) < 0) | ||||
errx(1, "Cannot stat %s", name); | errx(1, "Cannot stat %s", name); | ||||
if (S_ISREG(sb.st_mode)) | if (S_ISREG(sb.st_mode)) | ||||
sz = sb.st_size; | sz = sb.st_size; | ||||
else if (S_ISCHR(sb.st_mode)) { | else if (S_ISCHR(sb.st_mode)) { | ||||
if (ioctl(fd, DIOCGMEDIASIZE, &sz) != 0) | if (ioctl(fd, DIOCGMEDIASIZE, &sz) != 0) | ||||
err(1, "ioctl(DIOCGMEDIASIZE)"); | err(1, "ioctl(DIOCGMEDIASIZE)"); | ||||
} else | } else | ||||
errx(1, "%s has an invalid file type", name); | errx(1, "%s has an invalid file type", name); | ||||
/* Trim the device. */ | |||||
ioarg[0] = 0; | ioarg[0] = 0; | ||||
ioarg[1] = sz; | ioarg[1] = sz; | ||||
if (ioctl(fd, DIOCGDELETE, ioarg) != 0) | if (ioctl(fd, DIOCGDELETE, ioarg) != 0) | ||||
warn("ioctl(DIOCGDELETE)"); | 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 | * Do not close the device until the swap pager has attempted to create | ||||
* after the device is in use for swapping, but before the fd is closed, | * another consumer. For GELI devices created with the 'detach -l' | ||||
* for the benefit of geli, which could otherwise detach the device, | * option, removing the last consumer causes the device to be detached | ||||
* before swapon, on close. | * - that is, to disappear. This ordering ensures that the device will | ||||
* not be detached until swapoff is called. | |||||
*/ | */ | ||||
error = swapon(name); | |||||
close(fd); | close(fd); | ||||
return (error); | return (error); | ||||
} | } | ||||
static const char * | static const char * | ||||
swap_on_off_sfile(const char *name, int doingall) | swap_on_off_sfile(const char *name, int doingall) | ||||
{ | { | ||||
int error; | int error; | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |