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 * 4. 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/diskslice.h>
38 #include <sys/ioctl_compat.h>
39 #include <vm/vm_param.h>
51 static void usage(void);
52 static int swap_on_off(char *name, int doingall, int trim);
53 static void swaplist(int lflag, int sflag, int hflag);
55 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
58 main(int argc, char **argv)
64 int doall, sflag, lflag, hflag, qflag, eflag;
66 if ((ptr = strrchr(argv[0], '/')) == NULL)
68 if (strstr(ptr, "swapon"))
70 else if (strstr(ptr, "swapoff"))
72 orig_prog = which_prog;
74 sflag = lflag = hflag = qflag = doall = eflag = 0;
75 while ((ch = getopt(argc, argv, "AadeghklmqsU")) != -1) {
78 if (which_prog == SWAPCTL) {
86 if (which_prog == SWAPON || which_prog == SWAPOFF)
92 if (which_prog == SWAPCTL)
116 if (which_prog == SWAPON || which_prog == SWAPOFF)
123 if (which_prog == SWAPCTL) {
125 which_prog = SWAPOFF;
138 if (which_prog == SWAPON || which_prog == SWAPOFF) {
140 while ((fsp = getfsent()) != NULL) {
141 if (strcmp(fsp->fs_type, FSTAB_SW))
143 if (strstr(fsp->fs_mntops, "noauto"))
145 if (swap_on_off(fsp->fs_spec, 1, eflag)) {
149 printf("%s: %sing %s as swap device\n",
151 which_prog == SWAPOFF ? "remov" : "add",
156 } else if (*argv == NULL) {
159 for (; *argv; ++argv) {
160 if (swap_on_off(getdevpath(*argv, 0), 0, eflag)) {
162 } else if (orig_prog == SWAPCTL) {
163 printf("%s: %sing %s as swap device\n",
165 which_prog == SWAPOFF ? "remov" : "add",
171 swaplist(lflag, sflag, hflag);
183 trim_volume(char * name)
185 struct partinfo pinfo;
187 size_t bytes = 0,ksize;
193 * Determine if this device is already being used by swap without
196 if ((sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0) ||
198 err(1, "sysctlbyname()");
201 xswbuf = malloc(bytes);
202 if ((sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) ||
205 err(1, "sysctlbyname()");
208 ksize = ((struct xswdev *)xswbuf)->xsw_size;
209 n = (int)(bytes / ksize);
210 for (i = 0; i < n; ++i) {
211 xsw = (void *)((char *)xswbuf + i * ksize);
213 if (xsw->xsw_dev == NODEV )
215 if(!strcmp(devname(xsw->xsw_dev, S_IFCHR),
216 name + strlen("/dev/"))) {
217 warnx("%s: device already a swap device", name);
223 * Get the size and offset of this parititon/device
225 fd = open(name, O_RDWR);
227 err(1, "Unable to open %s R+W", name);
228 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
229 printf("Cannot trim regular file\n");
235 ioarg[0] = pinfo.media_offset;
236 ioarg[1] = pinfo.media_size;
237 printf("Trimming Device:%s, sectors (%llu -%llu)\n",name,
238 (unsigned long long)ioarg[0]/512,
239 (unsigned long long)ioarg[1]/512);
240 if (ioctl(fd, IOCTLTRIM, ioarg) < 0) {
241 printf("Device trim failed\n");
248 swap_on_off(char *name, int doingall, int trim)
250 if (which_prog == SWAPON && trim){
251 char sysctl_name[64];
252 int trim_enabled = 0;
253 size_t olen = sizeof(trim_enabled);
254 char *dev_name = strdup(name);
255 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
256 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
257 sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
258 if(errno == ENOENT) {
259 printf("Device:%s does not support the TRIM command\n",
264 printf("Erase device option selected, but sysctl (%s) "
265 "is not enabled\n",sysctl_name);
272 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
276 warnx("%s: device already in use", name);
279 if (which_prog == SWAPON)
280 warnx("%s: NSWAPDEV limit reached", name);
296 fprintf(stderr, "usage: %s ", getprogname());
300 fprintf(stderr, "-aeq | file ...\n");
303 fprintf(stderr, "[-AeghklmsU] [-a file ... | -d file ...]\n");
310 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
316 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
317 HN_B | HN_NOSPACE | HN_DECIMAL);
318 snprintf(buf, bufsize, "%*s", hlen, tmp);
320 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
325 swaplist(int lflag, int sflag, int hflag)
327 size_t ksize, bytes = 0;
333 long long total, used, tmp_total, tmp_used;
336 pagesize = getpagesize();
339 blocksize = 1024 * 1024 * 1024;
340 strlcpy(buf, "1GB-blocks", sizeof(buf));
345 strlcpy(buf, "Bytes", sizeof(buf));
350 strlcpy(buf, "1kB-blocks", sizeof(buf));
354 blocksize = 1024 * 1024;
355 strlcpy(buf, "1MB-blocks", sizeof(buf));
359 getbsize(&hlen, &blocksize);
360 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
364 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
365 err(1, "sysctlbyname()");
367 err(1, "sysctlbyname()");
369 xswbuf = malloc(bytes);
370 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
372 err(1, "sysctlbyname()");
376 err(1, "sysctlbyname()");
380 * Calculate size of xsw entry returned by kernel (it can be larger
381 * than the one we have if there is a version mismatch).
383 ksize = ((struct xswdev *)xswbuf)->xsw_size;
384 n = (int)(bytes / ksize);
387 printf("%-13s %*s %*s\n",
393 total = used = tmp_total = tmp_used = 0;
394 for (i = 0; i < n; ++i) {
395 xsw = (void *)((char *)xswbuf + i * ksize);
397 if (xsw->xsw_nblks == 0)
400 tmp_total = (long long)xsw->xsw_nblks * pagesize;
401 tmp_used = (long long)xsw->xsw_used * pagesize;
405 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
407 if (xsw->xsw_dev == NODEV) {
408 printf("%-13s %s ", "[NFS swap]", buf);
410 printf("/dev/%-8s %s ",
411 devname(xsw->xsw_dev, S_IFCHR), buf);
414 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
421 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
422 printf("Total: %s ", buf);
423 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);