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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)swapon.c 8.1 (Berkeley) 6/5/93
35 * $FreeBSD: src/sbin/swapon/swapon.c,v 1.8.2.2 2001/07/30 10:30:11 dd Exp $
38 #include <sys/param.h>
40 #include <sys/sysctl.h>
41 #include <sys/diskslice.h>
42 #include <sys/ioctl_compat.h>
43 #include <vm/vm_param.h>
55 static void usage(void);
56 static int swap_on_off(char *name, int doingall, int trim);
57 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, qflag, eflag;
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 = qflag = doall = eflag = 0;
79 while ((ch = getopt(argc, argv, "AadeghklmqsU")) != -1) {
82 if (which_prog == SWAPCTL) {
90 if (which_prog == SWAPON || which_prog == SWAPOFF)
96 if (which_prog == SWAPCTL)
120 if (which_prog == SWAPON || which_prog == SWAPOFF)
127 if (which_prog == SWAPCTL) {
129 which_prog = SWAPOFF;
142 if (which_prog == SWAPON || which_prog == SWAPOFF) {
144 while ((fsp = getfsent()) != NULL) {
145 if (strcmp(fsp->fs_type, FSTAB_SW))
147 if (strstr(fsp->fs_mntops, "noauto"))
149 if (swap_on_off(fsp->fs_spec, 1, eflag)) {
153 printf("%s: %sing %s as swap device\n",
155 which_prog == SWAPOFF ? "remov" : "add",
160 } else if (*argv == NULL) {
163 for (; *argv; ++argv) {
164 if (swap_on_off(getdevpath(*argv, 0), 0, eflag)) {
166 } else if (orig_prog == SWAPCTL) {
167 printf("%s: %sing %s as swap device\n",
169 which_prog == SWAPOFF ? "remov" : "add",
175 swaplist(lflag, sflag, hflag);
187 trim_volume(char * name)
189 struct partinfo pinfo;
191 size_t bytes = 0,ksize;
197 * Determine if this device is already being used by swap without
200 if ((sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0) ||
202 err(1, "sysctlbyname()");
205 xswbuf = malloc(bytes);
206 if ((sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) ||
209 err(1, "sysctlbyname()");
212 ksize = ((struct xswdev *)xswbuf)->xsw_size;
213 n = (int)(bytes / ksize);
214 for (i = 0; i < n; ++i) {
215 xsw = (void *)((char *)xswbuf + i * ksize);
217 if (xsw->xsw_dev == NODEV )
219 if(!strcmp(devname(xsw->xsw_dev, S_IFCHR),
220 name + strlen("/dev/"))) {
221 warnx("%s: device already a swap device", name);
227 * Get the size and offset of this parititon/device
229 fd = open(name, O_RDWR);
231 err(1, "Unable to open %s R+W", name);
232 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
233 printf("Cannot trim regular file\n");
239 ioarg[0] = pinfo.media_offset;
240 ioarg[1] = pinfo.media_size;
241 printf("Trimming Device:%s, sectors (%llu -%llu)\n",name,
242 (unsigned long long)ioarg[0]/512,
243 (unsigned long long)ioarg[1]/512);
244 if (ioctl(fd, IOCTLTRIM, ioarg) < 0) {
245 printf("Device trim failed\n");
252 swap_on_off(char *name, int doingall, int trim)
254 if (which_prog == SWAPON && trim){
255 char sysctl_name[64];
256 int trim_enabled = 0;
257 size_t olen = sizeof(trim_enabled);
258 char *dev_name = strdup(name);
259 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
260 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
261 sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
262 if(errno == ENOENT) {
263 printf("Device:%s does not support the TRIM command\n",
268 printf("Erase device option selected, but sysctl (%s) "
269 "is not enabled\n",sysctl_name);
276 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
280 warnx("%s: device already in use", name);
283 if (which_prog == SWAPON)
284 warnx("%s: NSWAPDEV limit reached", name);
300 fprintf(stderr, "usage: %s ", getprogname());
304 fprintf(stderr, "-aeq | file ...\n");
307 fprintf(stderr, "[-AeghklmsU] [-a file ... | -d file ...]\n");
314 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
320 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
321 HN_B | HN_NOSPACE | HN_DECIMAL);
322 snprintf(buf, bufsize, "%*s", hlen, tmp);
324 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
329 swaplist(int lflag, int sflag, int hflag)
331 size_t ksize, bytes = 0;
337 long long total, used, tmp_total, tmp_used;
340 pagesize = getpagesize();
343 blocksize = 1024 * 1024 * 1024;
344 strlcpy(buf, "1GB-blocks", sizeof(buf));
349 strlcpy(buf, "Bytes", sizeof(buf));
354 strlcpy(buf, "1kB-blocks", sizeof(buf));
358 blocksize = 1024 * 1024;
359 strlcpy(buf, "1MB-blocks", sizeof(buf));
363 getbsize(&hlen, &blocksize);
364 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
368 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
369 err(1, "sysctlbyname()");
371 err(1, "sysctlbyname()");
373 xswbuf = malloc(bytes);
374 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
376 err(1, "sysctlbyname()");
380 err(1, "sysctlbyname()");
384 * Calculate size of xsw entry returned by kernel (it can be larger
385 * than the one we have if there is a version mismatch).
387 ksize = ((struct xswdev *)xswbuf)->xsw_size;
388 n = (int)(bytes / ksize);
391 printf("%-13s %*s %*s\n",
397 total = used = tmp_total = tmp_used = 0;
398 for (i = 0; i < n; ++i) {
399 xsw = (void *)((char *)xswbuf + i * ksize);
401 if (xsw->xsw_nblks == 0)
404 tmp_total = (long long)xsw->xsw_nblks * pagesize;
405 tmp_used = (long long)xsw->xsw_used * pagesize;
409 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
411 if (xsw->xsw_dev == NODEV) {
412 printf("%-13s %s ", "[NFS swap]", buf);
414 printf("/dev/%-8s %s ",
415 devname(xsw->xsw_dev, S_IFCHR), buf);
418 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
425 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
426 printf("Total: %s ", buf);
427 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);