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/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, int ask);
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, iflag;
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 = iflag = 0;
75 while ((ch = getopt(argc, argv, "AadeghiklmqsU")) != -1) {
78 if (which_prog == SWAPCTL) {
86 if (which_prog == SWAPON || which_prog == SWAPOFF)
92 if (which_prog == SWAPCTL)
119 if (which_prog == SWAPON || which_prog == SWAPOFF)
126 if (which_prog == SWAPCTL) {
128 which_prog = SWAPOFF;
141 if (which_prog == SWAPON || which_prog == SWAPOFF) {
143 while ((fsp = getfsent()) != NULL) {
144 if (strcmp(fsp->fs_type, FSTAB_SW))
146 if (strstr(fsp->fs_mntops, "noauto"))
148 if (swap_on_off(fsp->fs_spec, 1, eflag, iflag)) {
152 printf("%s: %sing %s as swap device\n",
154 which_prog == SWAPOFF ? "remov" : "add",
159 } else if (*argv == NULL) {
162 for (; *argv; ++argv) {
163 if (swap_on_off(getdevpath(*argv, 0), 0, eflag, iflag)) {
165 } else if (orig_prog == SWAPCTL) {
166 printf("%s: %sing %s as swap device\n",
168 which_prog == SWAPOFF ? "remov" : "add",
174 swaplist(lflag, sflag, hflag);
186 trim_volume(char * name)
188 struct partinfo pinfo;
190 size_t bytes = 0,ksize;
196 * Determine if this device is already being used by swap without
199 if ((sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0) ||
201 err(1, "sysctlbyname()");
204 xswbuf = malloc(bytes);
205 if ((sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) ||
208 err(1, "sysctlbyname()");
211 ksize = ((struct xswdev *)xswbuf)->xsw_size;
212 n = (int)(bytes / ksize);
213 for (i = 0; i < n; ++i) {
214 xsw = (void *)((char *)xswbuf + i * ksize);
216 if (xsw->xsw_dev == NODEV )
218 if(!strcmp(devname(xsw->xsw_dev, S_IFCHR),
219 name + strlen("/dev/"))) {
220 warnx("%s: device already a swap device", name);
226 * Get the size and offset of this parititon/device
228 fd = open(name, O_RDWR);
230 err(1, "Unable to open %s R+W", name);
231 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
232 printf("Cannot trim regular file\n");
238 ioarg[0] = pinfo.media_offset;
239 ioarg[1] = pinfo.media_size;
240 printf("Trimming Device:%s, sectors (%llu -%llu)\n",name,
241 (unsigned long long)ioarg[0]/512,
242 (unsigned long long)ioarg[1]/512);
243 if (ioctl(fd, IOCTLTRIM, ioarg) < 0) {
244 printf("Device trim failed\n");
251 swap_on_off(char *name, int doingall, int trim, int ask)
254 if (ask && which_prog == SWAPON) {
255 printf("Do you really want to use device %s as a swap device ?\n", name);
256 printf("You might loose data. [Y/N]");
258 int c = fgetc(stdin);
260 if (c != 'y' && c != 'Y')
264 if (which_prog == SWAPON && trim){
265 char sysctl_name[64];
266 int trim_enabled = 0;
267 size_t olen = sizeof(trim_enabled);
268 char *dev_name = strdup(name);
269 dev_name = strtok(dev_name + strlen("/dev/da"),"s");
270 sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
271 sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
272 if(errno == ENOENT) {
273 printf("Device:%s does not support the TRIM command\n",
278 printf("Erase device option selected, but sysctl (%s) "
279 "is not enabled\n",sysctl_name);
286 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
290 warnx("%s: device already in use", name);
293 if (which_prog == SWAPON)
294 warnx("%s: NSWAPDEV limit reached", name);
310 fprintf(stderr, "usage: %s ", getprogname());
314 fprintf(stderr, "-aeiq | file ...\n");
317 fprintf(stderr, "[-AeghiklmsU] [-a file ... | -d file ...]\n");
324 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
330 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
331 HN_B | HN_NOSPACE | HN_DECIMAL);
332 snprintf(buf, bufsize, "%*s", hlen, tmp);
334 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
339 swaplist(int lflag, int sflag, int hflag)
341 size_t ksize, bytes = 0;
347 long long total, used, tmp_total, tmp_used;
350 pagesize = getpagesize();
353 blocksize = 1024 * 1024 * 1024;
354 strlcpy(buf, "1GB-blocks", sizeof(buf));
359 strlcpy(buf, "Bytes", sizeof(buf));
364 strlcpy(buf, "1kB-blocks", sizeof(buf));
368 blocksize = 1024 * 1024;
369 strlcpy(buf, "1MB-blocks", sizeof(buf));
373 getbsize(&hlen, &blocksize);
374 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
378 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
379 err(1, "sysctlbyname()");
381 err(1, "sysctlbyname()");
383 xswbuf = malloc(bytes);
384 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
386 err(1, "sysctlbyname()");
390 err(1, "sysctlbyname()");
394 * Calculate size of xsw entry returned by kernel (it can be larger
395 * than the one we have if there is a version mismatch).
397 ksize = ((struct xswdev *)xswbuf)->xsw_size;
398 n = (int)(bytes / ksize);
401 printf("%-13s %*s %*s\n",
407 total = used = tmp_total = tmp_used = 0;
408 for (i = 0; i < n; ++i) {
409 xsw = (void *)((char *)xswbuf + i * ksize);
411 if (xsw->xsw_nblks == 0)
414 tmp_total = (long long)xsw->xsw_nblks * pagesize;
415 tmp_used = (long long)xsw->xsw_used * pagesize;
419 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
421 if (xsw->xsw_dev == NODEV) {
422 printf("%-13s %s ", "[NFS swap]", buf);
424 printf("/dev/%-8s %s ",
425 devname(xsw->xsw_dev, S_IFCHR), buf);
428 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
435 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
436 printf("Total: %s ", buf);
437 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);