2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)swapon.c 8.1 (Berkeley) 6/5/93
31 * $FreeBSD: src/sbin/swapon/swapon.c,v 1.8.2.2 2001/07/30 10:30:11 dd Exp $
34 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/linker.h>
38 #include <sys/diskslice.h>
39 #include <sys/ioctl_compat.h>
40 #include <vm/vm_param.h>
52 static void usage(void);
53 static int swap_on_off(char *name, int doingall, int trim, int ask);
54 static char *docrypt(char *fs_spec, int pass);
55 static void swaplist(int lflag, int sflag, int hflag);
59 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
62 main(int argc, char **argv)
68 int doall, sflag, lflag, hflag, eflag, cflag, iflag;
70 if ((ptr = strrchr(argv[0], '/')) == NULL)
72 if (strstr(ptr, "swapon"))
74 else if (strstr(ptr, "swapoff"))
76 orig_prog = which_prog;
78 sflag = lflag = hflag = doall = eflag = cflag = iflag = 0;
79 while ((ch = getopt(argc, argv, "AacdeghiklmqsU")) != -1) {
82 if (which_prog == SWAPCTL) {
90 if (which_prog == SWAPON || which_prog == SWAPOFF)
96 if (which_prog == SWAPCTL)
126 if (which_prog == SWAPON || which_prog == SWAPOFF)
133 if (which_prog == SWAPCTL) {
135 which_prog = SWAPOFF;
148 if (which_prog == SWAPON || which_prog == SWAPOFF) {
150 while ((fsp = getfsent()) != NULL) {
154 if (strcmp(fsp->fs_type, FSTAB_SW))
156 if (strstr(fsp->fs_mntops, "noauto"))
159 if (strstr(fsp->fs_mntops, "notrim"))
161 else if (strstr(fsp->fs_mntops, "trim"))
164 if (cflag || strstr(fsp->fs_mntops, "crypt"))
165 fs_spec = docrypt(fsp->fs_spec, 1);
167 fs_spec = strdup(fsp->fs_spec);
168 if (swap_on_off(fs_spec, 1, dotrim, iflag)) {
172 strstr(fsp->fs_mntops, "crypt")) {
173 docrypt(fsp->fs_spec, 2);
176 printf("%s: %sing %s as swap "
179 (which_prog == SWAPOFF ?
186 } else if (*argv == NULL) {
189 for (; *argv; ++argv) {
190 char *ospec = getdevpath(*argv, 0);
193 fs_spec = docrypt(ospec, 1);
195 fs_spec = strdup(ospec);
196 if (swap_on_off(fs_spec, 0, eflag, iflag)) {
202 printf("%s: %sing %s as swap device\n",
204 (which_prog == SWAPOFF ?
213 swaplist(lflag, sflag, hflag);
222 docrypt(char *fs_spec, int pass)
228 if ((id = strrchr(fs_spec, '/')) == NULL)
232 asprintf(&id, "swap-%s", id);
233 asprintf(&res, "/dev/mapper/%s", id);
239 asprintf(&buf, "/sbin/cryptsetup remove %s", id);
246 if (kldfind("dm_target_crypt") < 0)
247 kldload("dm_target_crypt");
250 "/sbin/cryptsetup --key-file /dev/urandom "
251 "--key-size 256 create %s %s",
260 * NOTE: Don't revert to /dev/da* on error because this could
261 * inadvertently add both /dev/da* and
262 * /dev/mapper/swap-da*.
264 * Allow the swapon operation to report failure or
265 * report a duplicate.
271 res = strdup(fs_spec);
287 trim_volume(char * name)
289 struct partinfo pinfo;
291 size_t bytes = 0,ksize;
297 * Determine if this device is already being used by swap without
300 if ((sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0) ||
302 err(1, "sysctlbyname()");
305 xswbuf = malloc(bytes);
306 if ((sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) ||
309 err(1, "sysctlbyname()");
312 ksize = ((struct xswdev *)xswbuf)->xsw_size;
313 n = (int)(bytes / ksize);
314 for (i = 0; i < n; ++i) {
315 xsw = (void *)((char *)xswbuf + i * ksize);
317 if (xsw->xsw_dev == NODEV )
319 if(!strcmp(devname(xsw->xsw_dev, S_IFCHR),
320 name + strlen("/dev/"))) {
321 warnx("%s: device already a swap device", name);
327 * Get the size and offset of this parititon/device
329 fd = open(name, O_RDWR);
331 err(1, "Unable to open %s R+W", name);
332 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
333 printf("Cannot trim regular file\n");
339 ioarg[0] = pinfo.media_offset;
340 ioarg[1] = pinfo.media_size;
341 printf("Trimming Device:%s, sectors (%llu -%llu)\n",name,
342 (unsigned long long)ioarg[0]/512,
343 (unsigned long long)ioarg[1]/512);
344 if (ioctl(fd, IOCTLTRIM, ioarg) < 0) {
345 printf("Device trim failed\n");
352 swap_on_off(char *name, int doingall, int trim, int ask)
355 if (ask && which_prog == SWAPON) {
356 printf("Do you really want to use device %s as a swap device ?\n", name);
357 printf("You might loose data. [Y/N]");
359 int c = fgetc(stdin);
361 if (c != 'y' && c != 'Y')
365 if (which_prog == SWAPON && trim) {
366 char sysctl_name[64];
367 int trim_enabled = 0;
368 size_t olen = sizeof(trim_enabled);
369 char *dev_name = strdup(name);
370 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
371 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
372 if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
374 printf("TRIM not supported on %s, "
379 } else if (!trim_enabled) {
381 printf("TRIM not enabled on %s (%s), "
389 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
393 warnx("%s: device already in use", name);
396 if (which_prog == SWAPON)
397 warnx("%s: NSWAPDEV limit reached", name);
413 fprintf(stderr, "usage: %s ", getprogname());
417 fprintf(stderr, "-aeiq | file ...\n");
420 fprintf(stderr, "[-AeghiklmsU] [-a file ... | -d file ...]\n");
427 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
433 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
434 HN_B | HN_NOSPACE | HN_DECIMAL);
435 snprintf(buf, bufsize, "%*s", hlen, tmp);
437 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
442 swaplist(int lflag, int sflag, int hflag)
444 size_t ksize, bytes = 0;
450 long long total, used, tmp_total, tmp_used;
453 pagesize = getpagesize();
456 blocksize = 1024 * 1024 * 1024;
457 strlcpy(buf, "1GB-blocks", sizeof(buf));
462 strlcpy(buf, "Bytes", sizeof(buf));
467 strlcpy(buf, "1kB-blocks", sizeof(buf));
471 blocksize = 1024 * 1024;
472 strlcpy(buf, "1MB-blocks", sizeof(buf));
476 getbsize(&hlen, &blocksize);
477 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
481 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
482 err(1, "sysctlbyname()");
484 err(1, "sysctlbyname()");
486 xswbuf = malloc(bytes);
487 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
489 err(1, "sysctlbyname()");
493 err(1, "sysctlbyname()");
497 * Calculate size of xsw entry returned by kernel (it can be larger
498 * than the one we have if there is a version mismatch).
500 ksize = ((struct xswdev *)xswbuf)->xsw_size;
501 n = (int)(bytes / ksize);
504 printf("%-13s %*s %*s\n",
510 total = used = tmp_total = tmp_used = 0;
511 for (i = 0; i < n; ++i) {
512 xsw = (void *)((char *)xswbuf + i * ksize);
514 if (xsw->xsw_nblks == 0)
517 tmp_total = (long long)xsw->xsw_nblks * pagesize;
518 tmp_used = (long long)xsw->xsw_used * pagesize;
522 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
524 if (xsw->xsw_dev == NODEV) {
525 printf("%-13s %s ", "[NFS swap]", buf);
527 printf("/dev/%-8s %s ",
528 devname(xsw->xsw_dev, S_IFCHR), buf);
531 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
538 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
539 printf("Total: %s ", buf);
540 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);