xref: /titanic_54/usr/src/cmd/swap/swap.c (revision 56f10d37c1fa50cc562c3a4c2633af1ccef2aaa4)
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
5*56f10d37Spetede  * Common Development and Distribution License (the "License").
6*56f10d37Spetede  * 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 /*
22*56f10d37Spetede  * Copyright 2006 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	*/
277c478bd9Sstevel@tonic-gate /*	  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 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * 	Swap administrative interface
437c478bd9Sstevel@tonic-gate  *	Used to add/delete/list swap devices.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include	<sys/types.h>
477c478bd9Sstevel@tonic-gate #include	<sys/dumpadm.h>
487c478bd9Sstevel@tonic-gate #include	<string.h>
497c478bd9Sstevel@tonic-gate #include	<stdio.h>
507c478bd9Sstevel@tonic-gate #include	<stdlib.h>
517c478bd9Sstevel@tonic-gate #include	<unistd.h>
527c478bd9Sstevel@tonic-gate #include	<errno.h>
537c478bd9Sstevel@tonic-gate #include	<sys/param.h>
547c478bd9Sstevel@tonic-gate #include	<dirent.h>
557c478bd9Sstevel@tonic-gate #include	<sys/swap.h>
567c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
577c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
587c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
597c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
607c478bd9Sstevel@tonic-gate #include	<sys/uadmin.h>
617c478bd9Sstevel@tonic-gate #include	<vm/anon.h>
627c478bd9Sstevel@tonic-gate #include	<fcntl.h>
637c478bd9Sstevel@tonic-gate #include	<locale.h>
647c478bd9Sstevel@tonic-gate #include	<libintl.h>
653e1bd7a2Ssjelinek #include	<libdiskmgt.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	LFLAG	0x01	/* swap -l (list swap devices) */
687c478bd9Sstevel@tonic-gate #define	DFLAG	0x02	/* swap -d (delete swap device) */
697c478bd9Sstevel@tonic-gate #define	AFLAG	0x04	/* swap -a (add swap device) */
707c478bd9Sstevel@tonic-gate #define	SFLAG	0x08	/* swap -s (swap info summary) */
717c478bd9Sstevel@tonic-gate #define	P1FLAG	0x10	/* swap -1 (swapadd pass1; do not modify dump device) */
727c478bd9Sstevel@tonic-gate #define	P2FLAG	0x20	/* swap -2 (swapadd pass2; do not modify dump device) */
73*56f10d37Spetede #define	HFLAG	0x40	/* swap -h (size in human readable format) */
74*56f10d37Spetede #define	KFLAG	0x80	/* swap -k (size in kilobytes) */
75*56f10d37Spetede 
76*56f10d37Spetede #define	NUMBER_WIDTH	64
77*56f10d37Spetede typedef char numbuf_t[NUMBER_WIDTH];
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static char *prognamep;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int add(char *, off_t, off_t, int);
827c478bd9Sstevel@tonic-gate static int delete(char *, off_t);
837c478bd9Sstevel@tonic-gate static void usage(void);
84*56f10d37Spetede static int doswap(int flag);
857c478bd9Sstevel@tonic-gate static int valid(char *, off_t, off_t);
86*56f10d37Spetede static int list(int flag);
87*56f10d37Spetede static char *number_to_scaled_string(numbuf_t buf, unsigned long long number,
88*56f10d37Spetede 		unsigned long long unit_from, unsigned long long scale);
89*56f10d37Spetede 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
927c478bd9Sstevel@tonic-gate main(int argc, char **argv)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	int c, flag = 0;
957c478bd9Sstevel@tonic-gate 	int ret;
963e1bd7a2Ssjelinek 	int error = 0;
977c478bd9Sstevel@tonic-gate 	off_t s_offset = 0;
987c478bd9Sstevel@tonic-gate 	off_t length = 0;
997c478bd9Sstevel@tonic-gate 	char *pathname;
1003e1bd7a2Ssjelinek 	char *msg;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1057c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	prognamep = argv[0];
1107c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1117c478bd9Sstevel@tonic-gate 		usage();
1127c478bd9Sstevel@tonic-gate 		exit(1);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
115*56f10d37Spetede 	while ((c = getopt(argc, argv, "khlsd:a:12")) != EOF) {
1167c478bd9Sstevel@tonic-gate 		char *char_p;
1177c478bd9Sstevel@tonic-gate 		switch (c) {
1187c478bd9Sstevel@tonic-gate 		case 'l': 	/* list all the swap devices */
1197c478bd9Sstevel@tonic-gate 			flag |= LFLAG;
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 		case 's':
1227c478bd9Sstevel@tonic-gate 			flag |= SFLAG;
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 		case 'd':
1257c478bd9Sstevel@tonic-gate 			/*
1267c478bd9Sstevel@tonic-gate 			 * The argument for starting offset is optional.
1277c478bd9Sstevel@tonic-gate 			 * If no argument is specified, the entire swap file
1287c478bd9Sstevel@tonic-gate 			 * is added although this will fail if a non-zero
1297c478bd9Sstevel@tonic-gate 			 * starting offset was specified when added.
1307c478bd9Sstevel@tonic-gate 			 */
1317c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 1 || flag != 0) {
1327c478bd9Sstevel@tonic-gate 				usage();
1337c478bd9Sstevel@tonic-gate 				exit(1);
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			flag |= DFLAG;
1367c478bd9Sstevel@tonic-gate 			pathname = optarg;
1377c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1387c478bd9Sstevel@tonic-gate 				errno = 0;
1397c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1407c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1417c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1427c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1437c478bd9Sstevel@tonic-gate 					exit(1);
1447c478bd9Sstevel@tonic-gate 				}
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 			ret = delete(pathname, s_offset);
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case 'a':
1507c478bd9Sstevel@tonic-gate 			/*
1517c478bd9Sstevel@tonic-gate 			 * The arguments for starting offset and number of
1527c478bd9Sstevel@tonic-gate 			 * blocks are optional.  If only the starting offset
1537c478bd9Sstevel@tonic-gate 			 * is specified, all the blocks to the end of the swap
1547c478bd9Sstevel@tonic-gate 			 * file will be added.  If no starting offset is
1557c478bd9Sstevel@tonic-gate 			 * specified, the entire swap file is assumed.
1567c478bd9Sstevel@tonic-gate 			 */
1577c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 2 ||
1587c478bd9Sstevel@tonic-gate 			    (flag & ~(P1FLAG | P2FLAG)) != 0) {
1597c478bd9Sstevel@tonic-gate 				usage();
1607c478bd9Sstevel@tonic-gate 				exit(1);
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 			if (*optarg != '/') {
1637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1647c478bd9Sstevel@tonic-gate 				    gettext("%s: path must be absolute\n"),
1657c478bd9Sstevel@tonic-gate 				    prognamep);
1667c478bd9Sstevel@tonic-gate 				exit(1);
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 			flag |= AFLAG;
1697c478bd9Sstevel@tonic-gate 			pathname = optarg;
1707c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1717c478bd9Sstevel@tonic-gate 				errno = 0;
1727c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1737c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1747c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1757c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1767c478bd9Sstevel@tonic-gate 					exit(1);
1777c478bd9Sstevel@tonic-gate 				}
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1807c478bd9Sstevel@tonic-gate 				errno = 0;
1817c478bd9Sstevel@tonic-gate 				length = strtol(argv[optind++], &char_p, 10);
1827c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1837c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1847c478bd9Sstevel@tonic-gate 					gettext("error in [nbr of blocks]\n"));
1857c478bd9Sstevel@tonic-gate 					exit(1);
1867c478bd9Sstevel@tonic-gate 				}
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 			break;
189*56f10d37Spetede 		case 'h':
190*56f10d37Spetede 			flag |= HFLAG;
191*56f10d37Spetede 			break;
192*56f10d37Spetede 
193*56f10d37Spetede 		case 'k':
194*56f10d37Spetede 			flag |= KFLAG;
195*56f10d37Spetede 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case '1':
1987c478bd9Sstevel@tonic-gate 			flag |= P1FLAG;
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		case '2':
2027c478bd9Sstevel@tonic-gate 			flag |= P2FLAG;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case '?':
2067c478bd9Sstevel@tonic-gate 			usage();
2077c478bd9Sstevel@tonic-gate 			exit(1);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
210*56f10d37Spetede 
211*56f10d37Spetede 	if (flag & SFLAG) {
212*56f10d37Spetede 		if (flag & ~SFLAG & ~HFLAG) {
213*56f10d37Spetede 			/*
214*56f10d37Spetede 			 * The only option that can be used with -s is -h.
215*56f10d37Spetede 			 */
216*56f10d37Spetede 			usage();
217*56f10d37Spetede 			exit(1);
218*56f10d37Spetede 		}
219*56f10d37Spetede 
220*56f10d37Spetede 		ret = doswap(flag);
221*56f10d37Spetede 
222*56f10d37Spetede 	}
223*56f10d37Spetede 
224*56f10d37Spetede 	if (flag & LFLAG) {
225*56f10d37Spetede 		if (flag & ~KFLAG & ~HFLAG & ~LFLAG) {
226*56f10d37Spetede 			usage();
227*56f10d37Spetede 			exit(1);
228*56f10d37Spetede 		}
229*56f10d37Spetede 		ret = list(flag);
230*56f10d37Spetede 	}
231*56f10d37Spetede 
2323e1bd7a2Ssjelinek 	/*
2333e1bd7a2Ssjelinek 	 * do the add here. Check for in use prior to add.
2343e1bd7a2Ssjelinek 	 * The values for length and offset are set above.
2353e1bd7a2Ssjelinek 	 */
2363e1bd7a2Ssjelinek 	if (flag & AFLAG) {
2373e1bd7a2Ssjelinek 		/*
2383e1bd7a2Ssjelinek 		 * If device is in use for a swap device, print message
2393e1bd7a2Ssjelinek 		 * and exit.
2403e1bd7a2Ssjelinek 		 */
2413e1bd7a2Ssjelinek 		if (dm_inuse(pathname, &msg, DM_WHO_SWAP, &error) ||
2423e1bd7a2Ssjelinek 		    error) {
2433e1bd7a2Ssjelinek 			if (error != 0) {
2443e1bd7a2Ssjelinek 				(void) fprintf(stderr, gettext("Error occurred"
2453e1bd7a2Ssjelinek 				    " with device in use checking: %s\n"),
2463e1bd7a2Ssjelinek 				    strerror(error));
2473e1bd7a2Ssjelinek 			} else {
2483e1bd7a2Ssjelinek 				(void) fprintf(stderr, "%s", msg);
2493e1bd7a2Ssjelinek 				free(msg);
2503e1bd7a2Ssjelinek 				exit(1);
2513e1bd7a2Ssjelinek 			}
2523e1bd7a2Ssjelinek 		}
2533e1bd7a2Ssjelinek 		if ((ret = valid(pathname,
2543e1bd7a2Ssjelinek 		    s_offset * 512, length * 512)) == 0) {
2553e1bd7a2Ssjelinek 		    ret = add(pathname, s_offset, length, flag);
2563e1bd7a2Ssjelinek 		}
2573e1bd7a2Ssjelinek 	}
258*56f10d37Spetede 	if (!(flag & ~HFLAG & ~KFLAG)) {
259*56f10d37Spetede 		/* only -h and/or -k flag, or no flag */
2607c478bd9Sstevel@tonic-gate 		usage();
2617c478bd9Sstevel@tonic-gate 		exit(1);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	return (ret);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static void
2687c478bd9Sstevel@tonic-gate usage(void)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep);
271*56f10d37Spetede 	(void) fprintf(stderr, gettext("\tsub option :\n"));
272*56f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-h : displays size in human "
273*56f10d37Spetede 				"readable format\n"));
274*56f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-k : displays size in KB\n"));
2757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s -s\n", prognamep);
276*56f10d37Spetede 	(void) fprintf(stderr, gettext("\tsub option :\n"));
277*56f10d37Spetede 	(void) fprintf(stderr, gettext("\t\t-h : displays size in human "
278*56f10d37Spetede 				"readable format rather than KB\n"));
2797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
2807c478bd9Sstevel@tonic-gate 				prognamep);
2817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
2827c478bd9Sstevel@tonic-gate 				" [nbr of blocks]\n"), prognamep);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Implement:
2877c478bd9Sstevel@tonic-gate  *	#define ctok(x) ((ctob(x))>>10)
2887c478bd9Sstevel@tonic-gate  * in a machine independent way. (Both assume a click > 1k)
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate static size_t
2917c478bd9Sstevel@tonic-gate ctok(pgcnt_t clicks)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	static int factor = -1;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (factor == -1)
2967c478bd9Sstevel@tonic-gate 		factor = (int)(sysconf(_SC_PAGESIZE) >> 10);
2977c478bd9Sstevel@tonic-gate 	return ((size_t)(clicks * factor));
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int
302*56f10d37Spetede doswap(int flag)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct anoninfo ai;
3057c478bd9Sstevel@tonic-gate 	pgcnt_t allocated, reserved, available;
306*56f10d37Spetede 	numbuf_t numbuf;
307*56f10d37Spetede 	unsigned long long scale = 1024L;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * max = total amount of swap space including physical memory
3117c478bd9Sstevel@tonic-gate 	 * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) +
3127c478bd9Sstevel@tonic-gate 	 *	availrmem - swapfs_minfree;
3137c478bd9Sstevel@tonic-gate 	 * ai.ani_free = amount of unallocated anonymous memory
3147c478bd9Sstevel@tonic-gate 	 *	(ie. = resverved_unallocated + unreserved)
3157c478bd9Sstevel@tonic-gate 	 * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree);
3167c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = total amount of reserved anonymous memory
3177c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = anoninfo.ani_resv;
3187c478bd9Sstevel@tonic-gate 	 *
3197c478bd9Sstevel@tonic-gate 	 * allocated = anon memory not free
3207c478bd9Sstevel@tonic-gate 	 * reserved = anon memory reserved but not allocated
3217c478bd9Sstevel@tonic-gate 	 * available = anon memory not reserved
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if (swapctl(SC_AINFO, &ai) == -1) {
3247c478bd9Sstevel@tonic-gate 		perror(prognamep);
3257c478bd9Sstevel@tonic-gate 		return (2);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	allocated = ai.ani_max - ai.ani_free;
3297c478bd9Sstevel@tonic-gate 	reserved = ai.ani_resv - allocated;
3307c478bd9Sstevel@tonic-gate 	available = ai.ani_max - ai.ani_resv;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
3347c478bd9Sstevel@tonic-gate 	 * Translations (if any) of these keywords should match with
3357c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
3367c478bd9Sstevel@tonic-gate 	 * -s option:  "allocated", "reserved", "used", "available"
3377c478bd9Sstevel@tonic-gate 	 */
338*56f10d37Spetede 
339*56f10d37Spetede 	if (flag & HFLAG) {
340*56f10d37Spetede 		int factor = (int)(sysconf(_SC_PAGESIZE));
341*56f10d37Spetede 		(void) printf(gettext("total: %s allocated + "),
342*56f10d37Spetede 				number_to_scaled_string(numbuf, allocated,
343*56f10d37Spetede 				factor, scale));
344*56f10d37Spetede 		(void) printf(gettext("%s reserved = "),
345*56f10d37Spetede 				number_to_scaled_string(numbuf, allocated,
346*56f10d37Spetede 				factor, scale));
347*56f10d37Spetede 		(void) printf(gettext("%s used, "),
348*56f10d37Spetede 				number_to_scaled_string(numbuf,
349*56f10d37Spetede 				allocated + reserved, factor, scale));
350*56f10d37Spetede 		(void) printf(gettext("%s available\n"),
351*56f10d37Spetede 				number_to_scaled_string(numbuf, available,
352*56f10d37Spetede 				factor, scale));
353*56f10d37Spetede 	} else {
354*56f10d37Spetede 		(void) printf(gettext("total: %luk bytes allocated + %luk"
355*56f10d37Spetede 				" reserved = %luk used, %luk available\n"),
356*56f10d37Spetede 				ctok(allocated), ctok(reserved),
357*56f10d37Spetede 				ctok(reserved) + ctok(allocated),
3587c478bd9Sstevel@tonic-gate 				ctok(available));
359*56f10d37Spetede 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (0);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate static int
365*56f10d37Spetede list(int flag)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	struct swaptable 	*st;
3687c478bd9Sstevel@tonic-gate 	struct swapent	*swapent;
3697c478bd9Sstevel@tonic-gate 	int	i;
3707c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
3717c478bd9Sstevel@tonic-gate 	char		*path;
3727c478bd9Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN+1];
3737c478bd9Sstevel@tonic-gate 	int		num;
374*56f10d37Spetede 	numbuf_t numbuf;
375*56f10d37Spetede 	unsigned long long scale = 1024L;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
3787c478bd9Sstevel@tonic-gate 		perror(prognamep);
3797c478bd9Sstevel@tonic-gate 		return (2);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	if (num == 0) {
3827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No swap devices configured\n"));
3837c478bd9Sstevel@tonic-gate 		return (1);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if ((st = malloc(num * sizeof (swapent_t) + sizeof (int)))
3877c478bd9Sstevel@tonic-gate 	    == NULL) {
3887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3897c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
3907c478bd9Sstevel@tonic-gate 		perror(prognamep);
3917c478bd9Sstevel@tonic-gate 		return (2);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	if ((path = malloc(num * MAXPATHLEN)) == NULL) {
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3957c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
3967c478bd9Sstevel@tonic-gate 		perror(prognamep);
3977c478bd9Sstevel@tonic-gate 		return (2);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
4007c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
4017c478bd9Sstevel@tonic-gate 		swapent->ste_path = path;
4027c478bd9Sstevel@tonic-gate 		path += MAXPATHLEN;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	st->swt_n = num;
4067c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
4077c478bd9Sstevel@tonic-gate 		perror(prognamep);
4087c478bd9Sstevel@tonic-gate 		return (2);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
4137c478bd9Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
4147c478bd9Sstevel@tonic-gate 	 * alignment of header and output.
4157c478bd9Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
4167c478bd9Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
4177c478bd9Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
4187c478bd9Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
4197c478bd9Sstevel@tonic-gate 	 * 	1st & 2nd fields.
4207c478bd9Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
4217c478bd9Sstevel@tonic-gate 	 *
4227c478bd9Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
4237c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
4247c478bd9Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	(void) printf(
4277c478bd9Sstevel@tonic-gate 	    gettext("swapfile             dev    swaplo   blocks     free\n"));
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
4307c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
4317c478bd9Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
4327c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4337c478bd9Sstevel@tonic-gate 				"/dev/%s", swapent->ste_path);
4347c478bd9Sstevel@tonic-gate 		else
4357c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4367c478bd9Sstevel@tonic-gate 				"%s", swapent->ste_path);
4377c478bd9Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
4387c478bd9Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
4397c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
4407c478bd9Sstevel@tonic-gate 					swapent->ste_path);
4417c478bd9Sstevel@tonic-gate 			else
4427c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
4437c478bd9Sstevel@tonic-gate 					fullpath);
4447c478bd9Sstevel@tonic-gate 		else {
4454bc0a2efScasper 			if (S_ISBLK(statbuf.st_mode) ||
4464bc0a2efScasper 			    S_ISCHR(statbuf.st_mode)) {
4477c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
4487c478bd9Sstevel@tonic-gate 				    fullpath,
4497c478bd9Sstevel@tonic-gate 				    major(statbuf.st_rdev),
4507c478bd9Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
4514bc0a2efScasper 			} else {
4527c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
4537c478bd9Sstevel@tonic-gate 			}
4544bc0a2efScasper 		}
4557c478bd9Sstevel@tonic-gate 		{
4567c478bd9Sstevel@tonic-gate 		int diskblks_per_page =
4577c478bd9Sstevel@tonic-gate 			(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
458*56f10d37Spetede 		if (flag & HFLAG) {
459*56f10d37Spetede 			(void) printf(gettext(" %8s"),
460*56f10d37Spetede 					number_to_scaled_string(numbuf,
461*56f10d37Spetede 					swapent->ste_start, DEV_BSIZE,
462*56f10d37Spetede 					scale));
463*56f10d37Spetede 			(void) printf(gettext(" %8s"),
464*56f10d37Spetede 					number_to_scaled_string(numbuf,
465*56f10d37Spetede 					swapent->ste_pages *
466*56f10d37Spetede 						diskblks_per_page,
467*56f10d37Spetede 					DEV_BSIZE, scale));
468*56f10d37Spetede 			(void) printf(gettext(" %8s"),
469*56f10d37Spetede 					number_to_scaled_string(numbuf,
470*56f10d37Spetede 					swapent->ste_free *
471*56f10d37Spetede 						diskblks_per_page,
472*56f10d37Spetede 					DEV_BSIZE, scale));
473*56f10d37Spetede 		} else if (flag & KFLAG) {
474*56f10d37Spetede 			(void) printf(gettext(" %7luK %7luK %7luK"),
475*56f10d37Spetede 					swapent->ste_start * DEV_BSIZE / 1024,
476*56f10d37Spetede 					swapent->ste_pages * diskblks_per_page *
477*56f10d37Spetede 						DEV_BSIZE / 1024,
478*56f10d37Spetede 					swapent->ste_free * diskblks_per_page *
479*56f10d37Spetede 						DEV_BSIZE / 1024);
480*56f10d37Spetede 		} else {
481*56f10d37Spetede 			(void) printf(gettext(" %8lu %8lu %8lu"),
482*56f10d37Spetede 					swapent->ste_start,
4837c478bd9Sstevel@tonic-gate 					swapent->ste_pages * diskblks_per_page,
4847c478bd9Sstevel@tonic-gate 					swapent->ste_free * diskblks_per_page);
4857c478bd9Sstevel@tonic-gate 		}
486*56f10d37Spetede 		}
4877c478bd9Sstevel@tonic-gate 		if (swapent->ste_flags & ST_INDEL)
4887c478bd9Sstevel@tonic-gate 			(void) printf(" INDEL\n");
4897c478bd9Sstevel@tonic-gate 		else
4907c478bd9Sstevel@tonic-gate 			(void) printf("\n");
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	return (0);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
495*56f10d37Spetede /* Copied from du.c */
496*56f10d37Spetede static char *
497*56f10d37Spetede number_to_scaled_string(
498*56f10d37Spetede 	numbuf_t buf,			/* put the result here */
499*56f10d37Spetede 	unsigned long long number,	/* convert this number */
500*56f10d37Spetede 	unsigned long long unit_from,	/* number of byes per input unit */
501*56f10d37Spetede 	unsigned long long scale)	/* 1024 (-h) or 1000 (-H) */
502*56f10d37Spetede {
503*56f10d37Spetede 	unsigned long long save = 0;
504*56f10d37Spetede 	char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */
505*56f10d37Spetede 	char *uom = M;	/* unit of measurement, initially 'K' (=M[0]) */
506*56f10d37Spetede 
507*56f10d37Spetede 	if ((long long)number == (long long) -1) {
508*56f10d37Spetede 		(void) strcpy(buf, "-1");
509*56f10d37Spetede 		return (buf);
510*56f10d37Spetede 	}
511*56f10d37Spetede 
512*56f10d37Spetede 	/*
513*56f10d37Spetede 	 * Convert number from unit_from to given scale (1024 or 1000)
514*56f10d37Spetede 	 * This means multiply number with unit_from and divide by scale.
515*56f10d37Spetede 	 * if number is large enough, we first divide and then multiply
516*56f10d37Spetede 	 * to avoid an overflow (large enough here means 100 (rather arbitrary
517*56f10d37Spetede 	 * value) times scale in order to reduce rounding errors)
518*56f10d37Spetede 	 * otherwise, we first multiply and then divide to avoid an underflow.
519*56f10d37Spetede 	 */
520*56f10d37Spetede 	if (number >= 100L * scale) {
521*56f10d37Spetede 		number = number / scale;
522*56f10d37Spetede 		number = number * unit_from;
523*56f10d37Spetede 	} else {
524*56f10d37Spetede 		number = number * unit_from;
525*56f10d37Spetede 		number = number / scale;
526*56f10d37Spetede 	}
527*56f10d37Spetede 
528*56f10d37Spetede 	/*
529*56f10d37Spetede 	 * Now we have number as a count of scale units.
530*56f10d37Spetede 	 * Stop scaling when we reached exa bytes, then something is
531*56f10d37Spetede 	 * probably wrong with our number.probably wrong with our number.
532*56f10d37Spetede 	 */
533*56f10d37Spetede 	while ((number >= scale) && (*uom != 'E')) {
534*56f10d37Spetede 		uom++;	/* Next unit of measurement */
535*56f10d37Spetede 		save = number;
536*56f10d37Spetede 		number = (number + (scale / 2)) / scale;
537*56f10d37Spetede 	}
538*56f10d37Spetede 
539*56f10d37Spetede 	/* Check if we should output a decimal place after the point */
540*56f10d37Spetede 	if (save && ((save / scale) < 10)) {
541*56f10d37Spetede 		/* sprintf() will round for us */
542*56f10d37Spetede 		float fnum = (float)save / scale;
543*56f10d37Spetede 		(void) sprintf(buf, "%.1f%c", fnum, *uom);
544*56f10d37Spetede 	} else {
545*56f10d37Spetede 		(void) sprintf(buf, "%llu%c", number, *uom);
546*56f10d37Spetede 	}
547*56f10d37Spetede 	return (buf);
548*56f10d37Spetede }
549*56f10d37Spetede 
550*56f10d37Spetede 
551*56f10d37Spetede 
552*56f10d37Spetede 
5537c478bd9Sstevel@tonic-gate static void
5547c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
5577c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
5587c478bd9Sstevel@tonic-gate 	    "run dumpadm(1M) to verify dump configuration\n"));
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static int
5627c478bd9Sstevel@tonic-gate delete(char *path, off_t offset)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	swapres_t swr;
5657c478bd9Sstevel@tonic-gate 	int fd;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
5687c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
5717c478bd9Sstevel@tonic-gate 		switch (errno) {
5727c478bd9Sstevel@tonic-gate 		case (ENOSYS):
5737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5747c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
5757c478bd9Sstevel@tonic-gate 			    path);
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		default:
5787c478bd9Sstevel@tonic-gate 			perror(path);
5797c478bd9Sstevel@tonic-gate 			break;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		return (2);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
5867c478bd9Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
5877c478bd9Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
5887c478bd9Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
5917c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5947c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
5957c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
5967c478bd9Sstevel@tonic-gate 				    "invoking dumpadm(1M) -d swap to "
5977c478bd9Sstevel@tonic-gate 				    "select new dump device\n"), path);
5987c478bd9Sstevel@tonic-gate 				/*
5997c478bd9Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
6007c478bd9Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
6017c478bd9Sstevel@tonic-gate 				 */
6027c478bd9Sstevel@tonic-gate 				(void) close(fd);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
6057c478bd9Sstevel@tonic-gate 					dumpadm_err(gettext(
6067c478bd9Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
6077c478bd9Sstevel@tonic-gate 			} else
6087c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
6097c478bd9Sstevel@tonic-gate 				"Warning: failed to check dump device"));
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		(void) close(fd);
6127c478bd9Sstevel@tonic-gate 	} else
6137c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	return (0);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate static int
6227c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	swapres_t swr;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
6277c478bd9Sstevel@tonic-gate 	struct statvfs fsb;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
6317c478bd9Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
6327c478bd9Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
6337c478bd9Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
6367c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
6397c478bd9Sstevel@tonic-gate 			if (errno == ENODEV)
6407c478bd9Sstevel@tonic-gate 				have_dumpdev = 0;
6417c478bd9Sstevel@tonic-gate 			else
6427c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
6437c478bd9Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		(void) close(fd);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
6497c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
6527c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
6537c478bd9Sstevel@tonic-gate 	swr.sr_length = cnt;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
6567c478bd9Sstevel@tonic-gate 		switch (errno) {
6577c478bd9Sstevel@tonic-gate 		case (ENOSYS):
6587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6597c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
6607c478bd9Sstevel@tonic-gate 			    path);
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 		case (EEXIST):
6637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6647c478bd9Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
6657c478bd9Sstevel@tonic-gate 			    path);
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		default:
6687c478bd9Sstevel@tonic-gate 			perror(path);
6697c478bd9Sstevel@tonic-gate 			break;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		return (2);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
6767c478bd9Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
6777c478bd9Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
6787c478bd9Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
6797c478bd9Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
6807c478bd9Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
6817c478bd9Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
6827c478bd9Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
6837c478bd9Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
6847c478bd9Sstevel@tonic-gate 	 * we would lose the user's previous setting.
6857c478bd9Sstevel@tonic-gate 	 */
6867c478bd9Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
6877c478bd9Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		(void) printf(
6907c478bd9Sstevel@tonic-gate 			gettext("operating system crash dump was previously "
6917c478bd9Sstevel@tonic-gate 		    "disabled --\ninvoking dumpadm(1M) -d swap to select "
6927c478bd9Sstevel@tonic-gate 		    "new dump device\n"));
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
6957c478bd9Sstevel@tonic-gate 			dumpadm_err(gettext(
6967c478bd9Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (0);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate static int
7037c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	struct stat64		f;
7067c478bd9Sstevel@tonic-gate 	struct statvfs64	fs;
7077c478bd9Sstevel@tonic-gate 	off_t		need;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
7107c478bd9Sstevel@tonic-gate 		(void) perror(pathname);
7117c478bd9Sstevel@tonic-gate 		return (errno);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
7157c478bd9Sstevel@tonic-gate 		S_ISBLK(f.st_mode))) {
7167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7177c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
7187c478bd9Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
7197c478bd9Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
7207c478bd9Sstevel@tonic-gate 		    pathname);
7217c478bd9Sstevel@tonic-gate 		return (EINVAL);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
7257c478bd9Sstevel@tonic-gate 		if (length == 0)
7267c478bd9Sstevel@tonic-gate 			length = (off_t)f.st_size;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
7307c478bd9Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
7347c478bd9Sstevel@tonic-gate 		    f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) {
7357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
7367c478bd9Sstevel@tonic-gate 			    pathname);
7377c478bd9Sstevel@tonic-gate 			return (EINVAL);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (offset < 0) {
7417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7427c478bd9Sstevel@tonic-gate 				gettext("%s: low block is invalid\n"),
7437c478bd9Sstevel@tonic-gate 				pathname);
7447c478bd9Sstevel@tonic-gate 			return (EINVAL);
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
7517c478bd9Sstevel@tonic-gate 		 * Note:
7527c478bd9Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
7537c478bd9Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
7547c478bd9Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
7557c478bd9Sstevel@tonic-gate 		 *  about the underlying storage implementation.
7567c478bd9Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
7577c478bd9Sstevel@tonic-gate 		 *  there must be a hole.
7587c478bd9Sstevel@tonic-gate 		 */
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		if (need > f.st_blocks) {
7617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7627c478bd9Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
7637c478bd9Sstevel@tonic-gate 			    pathname);
7647c478bd9Sstevel@tonic-gate 			return (EINVAL);
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
7697c478bd9Sstevel@tonic-gate 	 * no meaningful checking can be done.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	return (0);
7737c478bd9Sstevel@tonic-gate }
774