xref: /illumos-gate/usr/src/cmd/swap/swap.c (revision 5f01ecd8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
556f10d37Spetede  * Common Development and Distribution License (the "License").
656f10d37Spetede  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e7cbe64fSgw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*5f01ecd8SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
390a055120SJason King /*
400a055120SJason King  * Copyright 2017 Jason King.
410a055120SJason King  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
44*5f01ecd8SToomas Soome  *	Swap administrative interface
457c478bd9Sstevel@tonic-gate  *	Used to add/delete/list swap devices.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include	<sys/types.h>
497c478bd9Sstevel@tonic-gate #include	<sys/dumpadm.h>
507c478bd9Sstevel@tonic-gate #include	<string.h>
517c478bd9Sstevel@tonic-gate #include	<stdio.h>
527c478bd9Sstevel@tonic-gate #include	<stdlib.h>
537c478bd9Sstevel@tonic-gate #include	<unistd.h>
547c478bd9Sstevel@tonic-gate #include	<errno.h>
557c478bd9Sstevel@tonic-gate #include	<sys/param.h>
567c478bd9Sstevel@tonic-gate #include	<dirent.h>
577c478bd9Sstevel@tonic-gate #include	<sys/swap.h>
587c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
597c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
607c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
617c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
627c478bd9Sstevel@tonic-gate #include	<sys/uadmin.h>
637c478bd9Sstevel@tonic-gate #include	<vm/anon.h>
647c478bd9Sstevel@tonic-gate #include	<fcntl.h>
657c478bd9Sstevel@tonic-gate #include	<locale.h>
667c478bd9Sstevel@tonic-gate #include	<libintl.h>
673e1bd7a2Ssjelinek #include	<libdiskmgt.h>
68e7cbe64fSgw #include	<sys/fs/zfs.h>
690a055120SJason King #include	<libcmdutils.h>
700a055120SJason King #include	<sys/debug.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	LFLAG	0x01	/* swap -l (list swap devices) */
737c478bd9Sstevel@tonic-gate #define	DFLAG	0x02	/* swap -d (delete swap device) */
747c478bd9Sstevel@tonic-gate #define	AFLAG	0x04	/* swap -a (add swap device) */
757c478bd9Sstevel@tonic-gate #define	SFLAG	0x08	/* swap -s (swap info summary) */
767c478bd9Sstevel@tonic-gate #define	P1FLAG	0x10	/* swap -1 (swapadd pass1; do not modify dump device) */
777c478bd9Sstevel@tonic-gate #define	P2FLAG	0x20	/* swap -2 (swapadd pass2; do not modify dump device) */
7856f10d37Spetede #define	HFLAG	0x40	/* swap -h (size in human readable format) */
7956f10d37Spetede #define	KFLAG	0x80	/* swap -k (size in kilobytes) */
8056f10d37Spetede 
8156f10d37Spetede #define	NUMBER_WIDTH	64
820a055120SJason King /* make sure nicenum_scale() has enough space */
830a055120SJason King CTASSERT(NUMBER_WIDTH >= NN_NUMBUF_SZ);
8456f10d37Spetede typedef char numbuf_t[NUMBER_WIDTH];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static char *prognamep;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int add(char *, off_t, off_t, int);
897c478bd9Sstevel@tonic-gate static int delete(char *, off_t);
907c478bd9Sstevel@tonic-gate static void usage(void);
9156f10d37Spetede static int doswap(int flag);
927c478bd9Sstevel@tonic-gate static int valid(char *, off_t, off_t);
9356f10d37Spetede static int list(int flag);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)967c478bd9Sstevel@tonic-gate main(int argc, char **argv)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	int c, flag = 0;
99*5f01ecd8SToomas Soome 	int ret = 0;
1003e1bd7a2Ssjelinek 	int error = 0;
1017c478bd9Sstevel@tonic-gate 	off_t s_offset = 0;
1027c478bd9Sstevel@tonic-gate 	off_t length = 0;
103*5f01ecd8SToomas Soome 	char *pathname = NULL;
1043e1bd7a2Ssjelinek 	char *msg;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1097c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	prognamep = argv[0];
1147c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1157c478bd9Sstevel@tonic-gate 		usage();
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
11956f10d37Spetede 	while ((c = getopt(argc, argv, "khlsd:a:12")) != EOF) {
1207c478bd9Sstevel@tonic-gate 		char *char_p;
1217c478bd9Sstevel@tonic-gate 		switch (c) {
122*5f01ecd8SToomas Soome 		case 'l':	/* list all the swap devices */
1237c478bd9Sstevel@tonic-gate 			flag |= LFLAG;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case 's':
1267c478bd9Sstevel@tonic-gate 			flag |= SFLAG;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case 'd':
1297c478bd9Sstevel@tonic-gate 			/*
1307c478bd9Sstevel@tonic-gate 			 * The argument for starting offset is optional.
1317c478bd9Sstevel@tonic-gate 			 * If no argument is specified, the entire swap file
1327c478bd9Sstevel@tonic-gate 			 * is added although this will fail if a non-zero
1337c478bd9Sstevel@tonic-gate 			 * starting offset was specified when added.
1347c478bd9Sstevel@tonic-gate 			 */
1357c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 1 || flag != 0) {
1367c478bd9Sstevel@tonic-gate 				usage();
1377c478bd9Sstevel@tonic-gate 				exit(1);
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 			flag |= DFLAG;
1407c478bd9Sstevel@tonic-gate 			pathname = optarg;
1417c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1427c478bd9Sstevel@tonic-gate 				errno = 0;
1437c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1447c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1467c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1477c478bd9Sstevel@tonic-gate 					exit(1);
1487c478bd9Sstevel@tonic-gate 				}
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 			ret = delete(pathname, s_offset);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case 'a':
1547c478bd9Sstevel@tonic-gate 			/*
1557c478bd9Sstevel@tonic-gate 			 * The arguments for starting offset and number of
1567c478bd9Sstevel@tonic-gate 			 * blocks are optional.  If only the starting offset
1577c478bd9Sstevel@tonic-gate 			 * is specified, all the blocks to the end of the swap
1587c478bd9Sstevel@tonic-gate 			 * file will be added.  If no starting offset is
1597c478bd9Sstevel@tonic-gate 			 * specified, the entire swap file is assumed.
1607c478bd9Sstevel@tonic-gate 			 */
1617c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 2 ||
1627c478bd9Sstevel@tonic-gate 			    (flag & ~(P1FLAG | P2FLAG)) != 0) {
1637c478bd9Sstevel@tonic-gate 				usage();
1647c478bd9Sstevel@tonic-gate 				exit(1);
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			if (*optarg != '/') {
1677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1687c478bd9Sstevel@tonic-gate 				    gettext("%s: path must be absolute\n"),
1697c478bd9Sstevel@tonic-gate 				    prognamep);
1707c478bd9Sstevel@tonic-gate 				exit(1);
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			flag |= AFLAG;
1737c478bd9Sstevel@tonic-gate 			pathname = optarg;
1747c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1757c478bd9Sstevel@tonic-gate 				errno = 0;
1767c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1777c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1787c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1797c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1807c478bd9Sstevel@tonic-gate 					exit(1);
1817c478bd9Sstevel@tonic-gate 				}
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1847c478bd9Sstevel@tonic-gate 				errno = 0;
1857c478bd9Sstevel@tonic-gate 				length = strtol(argv[optind++], &char_p, 10);
1867c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1887c478bd9Sstevel@tonic-gate 					gettext("error in [nbr of blocks]\n"));
1897c478bd9Sstevel@tonic-gate 					exit(1);
1907c478bd9Sstevel@tonic-gate 				}
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 			break;
19356f10d37Spetede 		case 'h':
19456f10d37Spetede 			flag |= HFLAG;
19556f10d37Spetede 			break;
19656f10d37Spetede 
19756f10d37Spetede 		case 'k':
19856f10d37Spetede 			flag |= KFLAG;
19956f10d37Spetede 			break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		case '1':
2027c478bd9Sstevel@tonic-gate 			flag |= P1FLAG;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case '2':
2067c478bd9Sstevel@tonic-gate 			flag |= P2FLAG;
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		case '?':
2107c478bd9Sstevel@tonic-gate 			usage();
2117c478bd9Sstevel@tonic-gate 			exit(1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
21456f10d37Spetede 
21556f10d37Spetede 	if (flag & SFLAG) {
21656f10d37Spetede 		if (flag & ~SFLAG & ~HFLAG) {
21756f10d37Spetede 			/*
21856f10d37Spetede 			 * The only option that can be used with -s is -h.
21956f10d37Spetede 			 */
22056f10d37Spetede 			usage();
22156f10d37Spetede 			exit(1);
22256f10d37Spetede 		}
22356f10d37Spetede 
22456f10d37Spetede 		ret = doswap(flag);
22556f10d37Spetede 
22656f10d37Spetede 	}
22756f10d37Spetede 
22856f10d37Spetede 	if (flag & LFLAG) {
22956f10d37Spetede 		if (flag & ~KFLAG & ~HFLAG & ~LFLAG) {
23056f10d37Spetede 			usage();
23156f10d37Spetede 			exit(1);
23256f10d37Spetede 		}
23356f10d37Spetede 		ret = list(flag);
23456f10d37Spetede 	}
23556f10d37Spetede 
2363e1bd7a2Ssjelinek 	/*
2373e1bd7a2Ssjelinek 	 * do the add here. Check for in use prior to add.
2383e1bd7a2Ssjelinek 	 * The values for length and offset are set above.
2393e1bd7a2Ssjelinek 	 */
2403e1bd7a2Ssjelinek 	if (flag & AFLAG) {
2413e1bd7a2Ssjelinek 		/*
2423e1bd7a2Ssjelinek 		 * If device is in use for a swap device, print message
2433e1bd7a2Ssjelinek 		 * and exit.
2443e1bd7a2Ssjelinek 		 */
2453e1bd7a2Ssjelinek 		if (dm_inuse(pathname, &msg, DM_WHO_SWAP, &error) ||
2463e1bd7a2Ssjelinek 		    error) {
2473e1bd7a2Ssjelinek 			if (error != 0) {
2483e1bd7a2Ssjelinek 				(void) fprintf(stderr, gettext("Error occurred"
2493e1bd7a2Ssjelinek 				    " with device in use checking: %s\n"),
2503e1bd7a2Ssjelinek 				    strerror(error));
2513e1bd7a2Ssjelinek 			} else {
2523e1bd7a2Ssjelinek 				(void) fprintf(stderr, "%s", msg);
2533e1bd7a2Ssjelinek 				free(msg);
2543e1bd7a2Ssjelinek 			}
255*5f01ecd8SToomas Soome 			exit(1);
2563e1bd7a2Ssjelinek 		}
2573e1bd7a2Ssjelinek 		if ((ret = valid(pathname,
2583e1bd7a2Ssjelinek 		    s_offset * 512, length * 512)) == 0) {
2590a055120SJason King 			ret = add(pathname, s_offset, length, flag);
2603e1bd7a2Ssjelinek 		}
2613e1bd7a2Ssjelinek 	}
26256f10d37Spetede 	if (!(flag & ~HFLAG & ~KFLAG)) {
26356f10d37Spetede 		/* only -h and/or -k flag, or no flag */
2647c478bd9Sstevel@tonic-gate 		usage();
2657c478bd9Sstevel@tonic-gate 		exit(1);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	return (ret);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static void
usage(void)2727c478bd9Sstevel@tonic-gate usage(void)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep);
27556f10d37Spetede 	(void) fprintf(stderr, gettext("\tsub option :\n"));
27656f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-h : displays size in human "
2770a055120SJason King 	    "readable format\n"));
27856f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-k : displays size in KB\n"));
2797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s -s\n", prognamep);
28056f10d37Spetede 	(void) fprintf(stderr, gettext("\tsub option :\n"));
28156f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-h : displays size in human "
2820a055120SJason King 	    "readable format rather than KB\n"));
2837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
2840a055120SJason King 	    prognamep);
2857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
2860a055120SJason King 	    " [nbr of blocks]\n"), prognamep);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Implement:
2917c478bd9Sstevel@tonic-gate  *	#define ctok(x) ((ctob(x))>>10)
2927c478bd9Sstevel@tonic-gate  * in a machine independent way. (Both assume a click > 1k)
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate static size_t
ctok(pgcnt_t clicks)2957c478bd9Sstevel@tonic-gate ctok(pgcnt_t clicks)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	static int factor = -1;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (factor == -1)
3007c478bd9Sstevel@tonic-gate 		factor = (int)(sysconf(_SC_PAGESIZE) >> 10);
3017c478bd9Sstevel@tonic-gate 	return ((size_t)(clicks * factor));
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate static int
doswap(int flag)30656f10d37Spetede doswap(int flag)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	struct anoninfo ai;
3097c478bd9Sstevel@tonic-gate 	pgcnt_t allocated, reserved, available;
31056f10d37Spetede 	numbuf_t numbuf;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * max = total amount of swap space including physical memory
3147c478bd9Sstevel@tonic-gate 	 * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) +
3157c478bd9Sstevel@tonic-gate 	 *	availrmem - swapfs_minfree;
3167c478bd9Sstevel@tonic-gate 	 * ai.ani_free = amount of unallocated anonymous memory
3177c478bd9Sstevel@tonic-gate 	 *	(ie. = resverved_unallocated + unreserved)
3187c478bd9Sstevel@tonic-gate 	 * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree);
3197c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = total amount of reserved anonymous memory
3207c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = anoninfo.ani_resv;
3217c478bd9Sstevel@tonic-gate 	 *
3227c478bd9Sstevel@tonic-gate 	 * allocated = anon memory not free
3237c478bd9Sstevel@tonic-gate 	 * reserved = anon memory reserved but not allocated
3247c478bd9Sstevel@tonic-gate 	 * available = anon memory not reserved
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if (swapctl(SC_AINFO, &ai) == -1) {
3277c478bd9Sstevel@tonic-gate 		perror(prognamep);
3287c478bd9Sstevel@tonic-gate 		return (2);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	allocated = ai.ani_max - ai.ani_free;
3327c478bd9Sstevel@tonic-gate 	reserved = ai.ani_resv - allocated;
3337c478bd9Sstevel@tonic-gate 	available = ai.ani_max - ai.ani_resv;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
3377c478bd9Sstevel@tonic-gate 	 * Translations (if any) of these keywords should match with
3387c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
3397c478bd9Sstevel@tonic-gate 	 * -s option:  "allocated", "reserved", "used", "available"
3407c478bd9Sstevel@tonic-gate 	 */
34156f10d37Spetede 
34256f10d37Spetede 	if (flag & HFLAG) {
34356f10d37Spetede 		int factor = (int)(sysconf(_SC_PAGESIZE));
3440a055120SJason King 
3450a055120SJason King 		nicenum_scale(allocated, factor, numbuf, sizeof (numbuf), 0);
3460a055120SJason King 		(void) printf(gettext("total: %s allocated + "), numbuf);
3470a055120SJason King 
3480a055120SJason King 		nicenum_scale(reserved, factor, numbuf, sizeof (numbuf), 0);
3490a055120SJason King 		(void) printf(gettext("%s reserved = "), numbuf);
3500a055120SJason King 
3510a055120SJason King 		nicenum_scale(allocated + reserved, factor, numbuf,
3520a055120SJason King 		    sizeof (numbuf), 0);
3530a055120SJason King 		(void) printf(gettext("%s used, "), numbuf);
3540a055120SJason King 
3550a055120SJason King 		nicenum_scale(available, factor, numbuf, sizeof (numbuf), 0);
3560a055120SJason King 		(void) printf(gettext("%s available\n"), numbuf);
35756f10d37Spetede 	} else {
35856f10d37Spetede 		(void) printf(gettext("total: %luk bytes allocated + %luk"
3590a055120SJason King 		    " reserved = %luk used, %luk available\n"),
3600a055120SJason King 		    ctok(allocated), ctok(reserved),
3610a055120SJason King 		    ctok(reserved) + ctok(allocated),
3620a055120SJason King 		    ctok(available));
36356f10d37Spetede 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (0);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static int
list(int flag)36956f10d37Spetede list(int flag)
3707c478bd9Sstevel@tonic-gate {
371*5f01ecd8SToomas Soome 	struct swaptable	*st;
3727c478bd9Sstevel@tonic-gate 	struct swapent	*swapent;
3737c478bd9Sstevel@tonic-gate 	int	i;
3747c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
375*5f01ecd8SToomas Soome 	char		*path, *ptr;
3767c478bd9Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN+1];
3777c478bd9Sstevel@tonic-gate 	int		num;
37856f10d37Spetede 	numbuf_t numbuf;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
3817c478bd9Sstevel@tonic-gate 		perror(prognamep);
3827c478bd9Sstevel@tonic-gate 		return (2);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	if (num == 0) {
3857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No swap devices configured\n"));
3867c478bd9Sstevel@tonic-gate 		return (1);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if ((st = malloc(num * sizeof (swapent_t) + sizeof (int)))
3907c478bd9Sstevel@tonic-gate 	    == NULL) {
3917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3920a055120SJason King 		    gettext("Malloc failed. Please try later.\n"));
3937c478bd9Sstevel@tonic-gate 		perror(prognamep);
3947c478bd9Sstevel@tonic-gate 		return (2);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	if ((path = malloc(num * MAXPATHLEN)) == NULL) {
3977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3980a055120SJason King 		    gettext("Malloc failed. Please try later.\n"));
3997c478bd9Sstevel@tonic-gate 		perror(prognamep);
400*5f01ecd8SToomas Soome 		free(st);
4017c478bd9Sstevel@tonic-gate 		return (2);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
404*5f01ecd8SToomas Soome 	ptr = path;
4057c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
406*5f01ecd8SToomas Soome 		swapent->ste_path = ptr;
407*5f01ecd8SToomas Soome 		ptr += MAXPATHLEN;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	st->swt_n = num;
4117c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
4127c478bd9Sstevel@tonic-gate 		perror(prognamep);
413*5f01ecd8SToomas Soome 		free(path);
414*5f01ecd8SToomas Soome 		free(st);
4157c478bd9Sstevel@tonic-gate 		return (2);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
4207c478bd9Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
4217c478bd9Sstevel@tonic-gate 	 * alignment of header and output.
4227c478bd9Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
4237c478bd9Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
4247c478bd9Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
4257c478bd9Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
426*5f01ecd8SToomas Soome 	 *	1st & 2nd fields.
4277c478bd9Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
4287c478bd9Sstevel@tonic-gate 	 *
4297c478bd9Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
4307c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
4317c478bd9Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	(void) printf(
43456f10d37Spetede 	    gettext("swapfile             dev    swaplo   blocks     free\n"));
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
4377c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
4387c478bd9Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
4397c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4400a055120SJason King 			    "/dev/%s", swapent->ste_path);
4417c478bd9Sstevel@tonic-gate 		else
4427c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4430a055120SJason King 			    "%s", swapent->ste_path);
4447c478bd9Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
4457c478bd9Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
4467c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
4470a055120SJason King 				    swapent->ste_path);
4487c478bd9Sstevel@tonic-gate 			else
4497c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
4500a055120SJason King 				    fullpath);
4517c478bd9Sstevel@tonic-gate 		else {
4524bc0a2efScasper 			if (S_ISBLK(statbuf.st_mode) ||
4534bc0a2efScasper 			    S_ISCHR(statbuf.st_mode)) {
4547c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
4557c478bd9Sstevel@tonic-gate 				    fullpath,
4567c478bd9Sstevel@tonic-gate 				    major(statbuf.st_rdev),
4577c478bd9Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
4584bc0a2efScasper 			} else {
4597c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
4604bc0a2efScasper 			}
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		{
4637c478bd9Sstevel@tonic-gate 		int diskblks_per_page =
4640a055120SJason King 		    (int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
46556f10d37Spetede 		if (flag & HFLAG) {
4660a055120SJason King 			nicenum_scale(swapent->ste_start, DEV_BSIZE, numbuf,
4670a055120SJason King 			    sizeof (numbuf), 0);
4680a055120SJason King 			(void) printf(gettext(" %8s"), numbuf);
4690a055120SJason King 
4700a055120SJason King 			nicenum_scale(swapent->ste_pages, DEV_BSIZE *
4710a055120SJason King 			    diskblks_per_page, numbuf, sizeof (numbuf), 0);
4720a055120SJason King 			(void) printf(gettext(" %8s"), numbuf);
4730a055120SJason King 
4740a055120SJason King 			nicenum_scale(swapent->ste_free, DEV_BSIZE *
4750a055120SJason King 			    diskblks_per_page, numbuf, sizeof (numbuf), 0);
4760a055120SJason King 			(void) printf(gettext(" %8s"), numbuf);
47756f10d37Spetede 		} else if (flag & KFLAG) {
47856f10d37Spetede 			(void) printf(gettext(" %7luK %7luK %7luK"),
4790a055120SJason King 			    swapent->ste_start * DEV_BSIZE / 1024,
4800a055120SJason King 			    swapent->ste_pages * diskblks_per_page *
4810a055120SJason King 			    DEV_BSIZE / 1024,
4820a055120SJason King 			    swapent->ste_free * diskblks_per_page *
4830a055120SJason King 			    DEV_BSIZE / 1024);
48456f10d37Spetede 		} else {
48556f10d37Spetede 			(void) printf(gettext(" %8lu %8lu %8lu"),
4860a055120SJason King 			    swapent->ste_start,
4870a055120SJason King 			    swapent->ste_pages * diskblks_per_page,
4880a055120SJason King 			    swapent->ste_free * diskblks_per_page);
48956f10d37Spetede 		}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		if (swapent->ste_flags & ST_INDEL)
4927c478bd9Sstevel@tonic-gate 			(void) printf(" INDEL\n");
4937c478bd9Sstevel@tonic-gate 		else
4947c478bd9Sstevel@tonic-gate 			(void) printf("\n");
4957c478bd9Sstevel@tonic-gate 	}
496*5f01ecd8SToomas Soome 	free(path);
497*5f01ecd8SToomas Soome 	free(st);
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static void
dumpadm_err(const char * warning)5027c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
5057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
506bbf21555SRichard Lowe 	    "run dumpadm(8) to verify dump configuration\n"));
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
delete(char * path,off_t offset)5107c478bd9Sstevel@tonic-gate delete(char *path, off_t offset)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	swapres_t swr;
5137c478bd9Sstevel@tonic-gate 	int fd;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
5167c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
5197c478bd9Sstevel@tonic-gate 		switch (errno) {
5207c478bd9Sstevel@tonic-gate 		case (ENOSYS):
5217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5227c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
5237c478bd9Sstevel@tonic-gate 			    path);
5247c478bd9Sstevel@tonic-gate 			break;
5257c478bd9Sstevel@tonic-gate 		default:
5267c478bd9Sstevel@tonic-gate 			perror(path);
5277c478bd9Sstevel@tonic-gate 			break;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		return (2);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
5347c478bd9Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
5357c478bd9Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
5367c478bd9Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
5377c478bd9Sstevel@tonic-gate 	 */
5387c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
5397c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5427c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
5437c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
544bbf21555SRichard Lowe 				    "invoking dumpadm(8) -d swap to "
5457c478bd9Sstevel@tonic-gate 				    "select new dump device\n"), path);
5467c478bd9Sstevel@tonic-gate 				/*
5477c478bd9Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
5487c478bd9Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
5497c478bd9Sstevel@tonic-gate 				 */
5507c478bd9Sstevel@tonic-gate 				(void) close(fd);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
5537c478bd9Sstevel@tonic-gate 					dumpadm_err(gettext(
5547c478bd9Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
5557c478bd9Sstevel@tonic-gate 			} else
5567c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
5577c478bd9Sstevel@tonic-gate 				"Warning: failed to check dump device"));
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		(void) close(fd);
5607c478bd9Sstevel@tonic-gate 	} else
5617c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (0);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate static int
add(char * path,off_t offset,off_t cnt,int flags)5707c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	swapres_t swr;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
5757c478bd9Sstevel@tonic-gate 	struct statvfs fsb;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
5797c478bd9Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
5807c478bd9Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
5817c478bd9Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
5847c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5877c478bd9Sstevel@tonic-gate 			if (errno == ENODEV)
5887c478bd9Sstevel@tonic-gate 				have_dumpdev = 0;
5897c478bd9Sstevel@tonic-gate 			else
5907c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
5917c478bd9Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		(void) close(fd);
5957c478bd9Sstevel@tonic-gate 
596e7cbe64fSgw 		/*
597e7cbe64fSgw 		 * zvols cannot act as both a swap device and dump device.
598e7cbe64fSgw 		 */
599e7cbe64fSgw 		if (strncmp(dumpdev, ZVOL_FULL_DEV_DIR,
600e7cbe64fSgw 		    strlen(ZVOL_FULL_DEV_DIR)) == 0) {
601e7cbe64fSgw 			if (strcmp(dumpdev, path) == 0) {
602e7cbe64fSgw 				(void) fprintf(stderr, gettext("%s: zvol "
603e7cbe64fSgw 				    "cannot be used as a swap device and a "
604e7cbe64fSgw 				    "dump device\n"), path);
605e7cbe64fSgw 				return (2);
606e7cbe64fSgw 			}
607e7cbe64fSgw 		}
608e7cbe64fSgw 
6097c478bd9Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
6107c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
6137c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
6147c478bd9Sstevel@tonic-gate 	swr.sr_length = cnt;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
6177c478bd9Sstevel@tonic-gate 		switch (errno) {
6187c478bd9Sstevel@tonic-gate 		case (ENOSYS):
6197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6207c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
6217c478bd9Sstevel@tonic-gate 			    path);
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 		case (EEXIST):
6247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6257c478bd9Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
6267c478bd9Sstevel@tonic-gate 			    path);
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		default:
6297c478bd9Sstevel@tonic-gate 			perror(path);
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 		return (2);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
6377c478bd9Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
6387c478bd9Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
6397c478bd9Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
6407c478bd9Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
6417c478bd9Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
6427c478bd9Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
6437c478bd9Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
6447c478bd9Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
6457c478bd9Sstevel@tonic-gate 	 * we would lose the user's previous setting.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
6487c478bd9Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		(void) printf(
6510a055120SJason King 		    gettext("operating system crash dump was previously "
652bbf21555SRichard Lowe 		    "disabled --\ninvoking dumpadm(8) -d swap to select "
6537c478bd9Sstevel@tonic-gate 		    "new dump device\n"));
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
6567c478bd9Sstevel@tonic-gate 			dumpadm_err(gettext(
6577c478bd9Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	return (0);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate static int
valid(char * pathname,off_t offset,off_t length)6647c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	struct stat64		f;
6677c478bd9Sstevel@tonic-gate 	struct statvfs64	fs;
6687c478bd9Sstevel@tonic-gate 	off_t		need;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
6717c478bd9Sstevel@tonic-gate 		(void) perror(pathname);
6727c478bd9Sstevel@tonic-gate 		return (errno);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
6760a055120SJason King 	    S_ISBLK(f.st_mode))) {
6777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6787c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
6797c478bd9Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
6807c478bd9Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
6817c478bd9Sstevel@tonic-gate 		    pathname);
6827c478bd9Sstevel@tonic-gate 		return (EINVAL);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
6867c478bd9Sstevel@tonic-gate 		if (length == 0)
6877c478bd9Sstevel@tonic-gate 			length = (off_t)f.st_size;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		/*
6907c478bd9Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
6917c478bd9Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
695*5f01ecd8SToomas Soome 		    f.st_blocks < 8 || length < 0) {
6967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
6977c478bd9Sstevel@tonic-gate 			    pathname);
6987c478bd9Sstevel@tonic-gate 			return (EINVAL);
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (offset < 0) {
7027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7030a055120SJason King 			    gettext("%s: low block is invalid\n"),
7040a055120SJason King 			    pathname);
7057c478bd9Sstevel@tonic-gate 			return (EINVAL);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		/*
7117c478bd9Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
7127c478bd9Sstevel@tonic-gate 		 * Note:
7137c478bd9Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
7147c478bd9Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
7157c478bd9Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
7167c478bd9Sstevel@tonic-gate 		 *  about the underlying storage implementation.
7177c478bd9Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
7187c478bd9Sstevel@tonic-gate 		 *  there must be a hole.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		if (need > f.st_blocks) {
7227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7237c478bd9Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
7247c478bd9Sstevel@tonic-gate 			    pathname);
7257c478bd9Sstevel@tonic-gate 			return (EINVAL);
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
7307c478bd9Sstevel@tonic-gate 	 * no meaningful checking can be done.
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	return (0);
7347c478bd9Sstevel@tonic-gate }
735