xref: /titanic_51/usr/src/cmd/swap/swap.c (revision fc30d4661fdf50c4c69dacbc572af35439c28cd8)
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 /*
22e7cbe64fSgw25295  * 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	*/
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 
39*fc30d466SJason King /*
40*fc30d466SJason King  * Copyright 2017 Jason King.
41*fc30d466SJason King  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * 	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>
68e7cbe64fSgw25295 #include	<sys/fs/zfs.h>
69*fc30d466SJason King #include	<libcmdutils.h>
70*fc30d466SJason 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
82*fc30d466SJason King /* make sure nicenum_scale() has enough space */
83*fc30d466SJason 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
967c478bd9Sstevel@tonic-gate main(int argc, char **argv)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	int c, flag = 0;
997c478bd9Sstevel@tonic-gate 	int ret;
1003e1bd7a2Ssjelinek 	int error = 0;
1017c478bd9Sstevel@tonic-gate 	off_t s_offset = 0;
1027c478bd9Sstevel@tonic-gate 	off_t length = 0;
1037c478bd9Sstevel@tonic-gate 	char *pathname;
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) {
1227c478bd9Sstevel@tonic-gate 		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 				exit(1);
2553e1bd7a2Ssjelinek 			}
2563e1bd7a2Ssjelinek 		}
2573e1bd7a2Ssjelinek 		if ((ret = valid(pathname,
2583e1bd7a2Ssjelinek 		    s_offset * 512, length * 512)) == 0) {
2593e1bd7a2Ssjelinek 			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
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 "
27756f10d37Spetede 	    "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 "
28256f10d37Spetede 	    "readable format rather than KB\n"));
2837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
2847c478bd9Sstevel@tonic-gate 	    prognamep);
2857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
2867c478bd9Sstevel@tonic-gate 	    " [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
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
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));
344*fc30d466SJason King 
345*fc30d466SJason King 		nicenum_scale(allocated, factor, numbuf, sizeof (numbuf), 0);
346*fc30d466SJason King 		(void) printf(gettext("total: %s allocated + "), numbuf);
347*fc30d466SJason King 
348*fc30d466SJason King 		nicenum_scale(reserved, factor, numbuf, sizeof (numbuf), 0);
349*fc30d466SJason King 		(void) printf(gettext("%s reserved = "), numbuf);
350*fc30d466SJason King 
351*fc30d466SJason King 		nicenum_scale(allocated + reserved, factor, numbuf,
352*fc30d466SJason King 		    sizeof (numbuf), 0);
353*fc30d466SJason King 		(void) printf(gettext("%s used, "), numbuf);
354*fc30d466SJason King 
355*fc30d466SJason King 		nicenum_scale(available, factor, numbuf, sizeof (numbuf), 0);
356*fc30d466SJason King 		(void) printf(gettext("%s available\n"), numbuf);
35756f10d37Spetede 	} else {
35856f10d37Spetede 		(void) printf(gettext("total: %luk bytes allocated + %luk"
35956f10d37Spetede 		    " reserved = %luk used, %luk available\n"),
36056f10d37Spetede 		    ctok(allocated), ctok(reserved),
36156f10d37Spetede 		    ctok(reserved) + ctok(allocated),
3627c478bd9Sstevel@tonic-gate 		    ctok(available));
36356f10d37Spetede 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (0);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static int
36956f10d37Spetede list(int flag)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	struct swaptable 	*st;
3727c478bd9Sstevel@tonic-gate 	struct swapent	*swapent;
3737c478bd9Sstevel@tonic-gate 	int	i;
3747c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
3757c478bd9Sstevel@tonic-gate 	char		*path;
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,
3927c478bd9Sstevel@tonic-gate 		    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,
3987c478bd9Sstevel@tonic-gate 		    gettext("Malloc failed. Please try later.\n"));
3997c478bd9Sstevel@tonic-gate 		perror(prognamep);
4007c478bd9Sstevel@tonic-gate 		return (2);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
4037c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
4047c478bd9Sstevel@tonic-gate 		swapent->ste_path = path;
4057c478bd9Sstevel@tonic-gate 		path += MAXPATHLEN;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	st->swt_n = num;
4097c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
4107c478bd9Sstevel@tonic-gate 		perror(prognamep);
4117c478bd9Sstevel@tonic-gate 		return (2);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
4167c478bd9Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
4177c478bd9Sstevel@tonic-gate 	 * alignment of header and output.
4187c478bd9Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
4197c478bd9Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
4207c478bd9Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
4217c478bd9Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
4227c478bd9Sstevel@tonic-gate 	 * 	1st & 2nd fields.
4237c478bd9Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
4247c478bd9Sstevel@tonic-gate 	 *
4257c478bd9Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
4267c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
4277c478bd9Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	(void) printf(
4307c478bd9Sstevel@tonic-gate 	    gettext("swapfile             dev    swaplo   blocks     free\n"));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
4337c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
4347c478bd9Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
4357c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4367c478bd9Sstevel@tonic-gate 			    "/dev/%s", swapent->ste_path);
4377c478bd9Sstevel@tonic-gate 		else
4387c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
4397c478bd9Sstevel@tonic-gate 			    "%s", swapent->ste_path);
4407c478bd9Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
4417c478bd9Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
4427c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
4437c478bd9Sstevel@tonic-gate 				    swapent->ste_path);
4447c478bd9Sstevel@tonic-gate 			else
4457c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
4467c478bd9Sstevel@tonic-gate 				    fullpath);
4477c478bd9Sstevel@tonic-gate 		else {
4484bc0a2efScasper 			if (S_ISBLK(statbuf.st_mode) ||
4494bc0a2efScasper 			    S_ISCHR(statbuf.st_mode)) {
4507c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
4517c478bd9Sstevel@tonic-gate 				    fullpath,
4527c478bd9Sstevel@tonic-gate 				    major(statbuf.st_rdev),
4537c478bd9Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
4544bc0a2efScasper 			} else {
4557c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
4567c478bd9Sstevel@tonic-gate 			}
4574bc0a2efScasper 		}
4587c478bd9Sstevel@tonic-gate 		{
4597c478bd9Sstevel@tonic-gate 		int diskblks_per_page =
4607c478bd9Sstevel@tonic-gate 		    (int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
46156f10d37Spetede 		if (flag & HFLAG) {
462*fc30d466SJason King 			nicenum_scale(swapent->ste_start, DEV_BSIZE, numbuf,
463*fc30d466SJason King 			    sizeof (numbuf), 0);
464*fc30d466SJason King 			(void) printf(gettext(" %8s"), numbuf);
465*fc30d466SJason King 
466*fc30d466SJason King 			nicenum_scale(swapent->ste_pages, DEV_BSIZE *
467*fc30d466SJason King 			    diskblks_per_page, numbuf, sizeof (numbuf), 0);
468*fc30d466SJason King 			(void) printf(gettext(" %8s"), numbuf);
469*fc30d466SJason King 
470*fc30d466SJason King 			nicenum_scale(swapent->ste_free, DEV_BSIZE *
471*fc30d466SJason King 			    diskblks_per_page, numbuf, sizeof (numbuf), 0);
472*fc30d466SJason King 			(void) printf(gettext(" %8s"), numbuf);
47356f10d37Spetede 		} else if (flag & KFLAG) {
47456f10d37Spetede 			(void) printf(gettext(" %7luK %7luK %7luK"),
47556f10d37Spetede 			    swapent->ste_start * DEV_BSIZE / 1024,
47656f10d37Spetede 			    swapent->ste_pages * diskblks_per_page *
47756f10d37Spetede 			    DEV_BSIZE / 1024,
47856f10d37Spetede 			    swapent->ste_free * diskblks_per_page *
47956f10d37Spetede 			    DEV_BSIZE / 1024);
48056f10d37Spetede 		} else {
48156f10d37Spetede 			(void) printf(gettext(" %8lu %8lu %8lu"),
48256f10d37Spetede 			    swapent->ste_start,
4837c478bd9Sstevel@tonic-gate 			    swapent->ste_pages * diskblks_per_page,
4847c478bd9Sstevel@tonic-gate 			    swapent->ste_free * diskblks_per_page);
4857c478bd9Sstevel@tonic-gate 		}
48656f10d37Spetede 		}
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 
4957c478bd9Sstevel@tonic-gate static void
4967c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
4997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
5007c478bd9Sstevel@tonic-gate 	    "run dumpadm(1M) to verify dump configuration\n"));
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static int
5047c478bd9Sstevel@tonic-gate delete(char *path, off_t offset)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	swapres_t swr;
5077c478bd9Sstevel@tonic-gate 	int fd;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
5107c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
5137c478bd9Sstevel@tonic-gate 		switch (errno) {
5147c478bd9Sstevel@tonic-gate 		case (ENOSYS):
5157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5167c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
5177c478bd9Sstevel@tonic-gate 			    path);
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 		default:
5207c478bd9Sstevel@tonic-gate 			perror(path);
5217c478bd9Sstevel@tonic-gate 			break;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 		return (2);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
5287c478bd9Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
5297c478bd9Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
5307c478bd9Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
5337c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5367c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
5377c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
5387c478bd9Sstevel@tonic-gate 				    "invoking dumpadm(1M) -d swap to "
5397c478bd9Sstevel@tonic-gate 				    "select new dump device\n"), path);
5407c478bd9Sstevel@tonic-gate 				/*
5417c478bd9Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
5427c478bd9Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
5437c478bd9Sstevel@tonic-gate 				 */
5447c478bd9Sstevel@tonic-gate 				(void) close(fd);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
5477c478bd9Sstevel@tonic-gate 					dumpadm_err(gettext(
5487c478bd9Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
5497c478bd9Sstevel@tonic-gate 			} else
5507c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
5517c478bd9Sstevel@tonic-gate 				"Warning: failed to check dump device"));
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 		(void) close(fd);
5547c478bd9Sstevel@tonic-gate 	} else
5557c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	return (0);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate static int
5647c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	swapres_t swr;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
5697c478bd9Sstevel@tonic-gate 	struct statvfs fsb;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
5737c478bd9Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
5747c478bd9Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
5757c478bd9Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
5787c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5817c478bd9Sstevel@tonic-gate 			if (errno == ENODEV)
5827c478bd9Sstevel@tonic-gate 				have_dumpdev = 0;
5837c478bd9Sstevel@tonic-gate 			else
5847c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
5857c478bd9Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		(void) close(fd);
5897c478bd9Sstevel@tonic-gate 
590e7cbe64fSgw25295 		/*
591e7cbe64fSgw25295 		 * zvols cannot act as both a swap device and dump device.
592e7cbe64fSgw25295 		 */
593e7cbe64fSgw25295 		if (strncmp(dumpdev, ZVOL_FULL_DEV_DIR,
594e7cbe64fSgw25295 		    strlen(ZVOL_FULL_DEV_DIR)) == 0) {
595e7cbe64fSgw25295 			if (strcmp(dumpdev, path) == 0) {
596e7cbe64fSgw25295 				(void) fprintf(stderr, gettext("%s: zvol "
597e7cbe64fSgw25295 				    "cannot be used as a swap device and a "
598e7cbe64fSgw25295 				    "dump device\n"), path);
599e7cbe64fSgw25295 				return (2);
600e7cbe64fSgw25295 			}
601e7cbe64fSgw25295 		}
602e7cbe64fSgw25295 
6037c478bd9Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
6047c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
6077c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
6087c478bd9Sstevel@tonic-gate 	swr.sr_length = cnt;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
6117c478bd9Sstevel@tonic-gate 		switch (errno) {
6127c478bd9Sstevel@tonic-gate 		case (ENOSYS):
6137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6147c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
6157c478bd9Sstevel@tonic-gate 			    path);
6167c478bd9Sstevel@tonic-gate 			break;
6177c478bd9Sstevel@tonic-gate 		case (EEXIST):
6187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6197c478bd9Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
6207c478bd9Sstevel@tonic-gate 			    path);
6217c478bd9Sstevel@tonic-gate 			break;
6227c478bd9Sstevel@tonic-gate 		default:
6237c478bd9Sstevel@tonic-gate 			perror(path);
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		return (2);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
6317c478bd9Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
6327c478bd9Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
6337c478bd9Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
6347c478bd9Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
6357c478bd9Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
6367c478bd9Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
6377c478bd9Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
6387c478bd9Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
6397c478bd9Sstevel@tonic-gate 	 * we would lose the user's previous setting.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
6427c478bd9Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		(void) printf(
6457c478bd9Sstevel@tonic-gate 		    gettext("operating system crash dump was previously "
6467c478bd9Sstevel@tonic-gate 		    "disabled --\ninvoking dumpadm(1M) -d swap to select "
6477c478bd9Sstevel@tonic-gate 		    "new dump device\n"));
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
6507c478bd9Sstevel@tonic-gate 			dumpadm_err(gettext(
6517c478bd9Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (0);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static int
6587c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	struct stat64		f;
6617c478bd9Sstevel@tonic-gate 	struct statvfs64	fs;
6627c478bd9Sstevel@tonic-gate 	off_t		need;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
6657c478bd9Sstevel@tonic-gate 		(void) perror(pathname);
6667c478bd9Sstevel@tonic-gate 		return (errno);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
6707c478bd9Sstevel@tonic-gate 	    S_ISBLK(f.st_mode))) {
6717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6727c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
6737c478bd9Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
6747c478bd9Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
6757c478bd9Sstevel@tonic-gate 		    pathname);
6767c478bd9Sstevel@tonic-gate 		return (EINVAL);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
6807c478bd9Sstevel@tonic-gate 		if (length == 0)
6817c478bd9Sstevel@tonic-gate 			length = (off_t)f.st_size;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		/*
6847c478bd9Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
6857c478bd9Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
6867c478bd9Sstevel@tonic-gate 		 */
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
6897c478bd9Sstevel@tonic-gate 		    f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) {
6907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
6917c478bd9Sstevel@tonic-gate 			    pathname);
6927c478bd9Sstevel@tonic-gate 			return (EINVAL);
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if (offset < 0) {
6967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6977c478bd9Sstevel@tonic-gate 			    gettext("%s: low block is invalid\n"),
6987c478bd9Sstevel@tonic-gate 			    pathname);
6997c478bd9Sstevel@tonic-gate 			return (EINVAL);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		/*
7057c478bd9Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
7067c478bd9Sstevel@tonic-gate 		 * Note:
7077c478bd9Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
7087c478bd9Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
7097c478bd9Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
7107c478bd9Sstevel@tonic-gate 		 *  about the underlying storage implementation.
7117c478bd9Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
7127c478bd9Sstevel@tonic-gate 		 *  there must be a hole.
7137c478bd9Sstevel@tonic-gate 		 */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (need > f.st_blocks) {
7167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7177c478bd9Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
7187c478bd9Sstevel@tonic-gate 			    pathname);
7197c478bd9Sstevel@tonic-gate 			return (EINVAL);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
7247c478bd9Sstevel@tonic-gate 	 * no meaningful checking can be done.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	return (0);
7287c478bd9Sstevel@tonic-gate }
729