xref: /onnv-gate/usr/src/cmd/swap/swap.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * 	Swap administrative interface
44*0Sstevel@tonic-gate  *	Used to add/delete/list swap devices.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include	<sys/types.h>
48*0Sstevel@tonic-gate #include	<sys/dumpadm.h>
49*0Sstevel@tonic-gate #include	<string.h>
50*0Sstevel@tonic-gate #include	<stdio.h>
51*0Sstevel@tonic-gate #include	<stdlib.h>
52*0Sstevel@tonic-gate #include	<unistd.h>
53*0Sstevel@tonic-gate #include	<errno.h>
54*0Sstevel@tonic-gate #include	<sys/param.h>
55*0Sstevel@tonic-gate #include	<dirent.h>
56*0Sstevel@tonic-gate #include	<sys/swap.h>
57*0Sstevel@tonic-gate #include	<sys/sysmacros.h>
58*0Sstevel@tonic-gate #include	<sys/mkdev.h>
59*0Sstevel@tonic-gate #include	<sys/stat.h>
60*0Sstevel@tonic-gate #include	<sys/statvfs.h>
61*0Sstevel@tonic-gate #include	<sys/uadmin.h>
62*0Sstevel@tonic-gate #include	<vm/anon.h>
63*0Sstevel@tonic-gate #include	<fcntl.h>
64*0Sstevel@tonic-gate #include	<locale.h>
65*0Sstevel@tonic-gate #include	<libintl.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #define	LFLAG	0x01	/* swap -l (list swap devices) */
68*0Sstevel@tonic-gate #define	DFLAG	0x02	/* swap -d (delete swap device) */
69*0Sstevel@tonic-gate #define	AFLAG	0x04	/* swap -a (add swap device) */
70*0Sstevel@tonic-gate #define	SFLAG	0x08	/* swap -s (swap info summary) */
71*0Sstevel@tonic-gate #define	P1FLAG	0x10	/* swap -1 (swapadd pass1; do not modify dump device) */
72*0Sstevel@tonic-gate #define	P2FLAG	0x20	/* swap -2 (swapadd pass2; do not modify dump device) */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static char *prognamep;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static int add(char *, off_t, off_t, int);
77*0Sstevel@tonic-gate static int delete(char *, off_t);
78*0Sstevel@tonic-gate static void usage(void);
79*0Sstevel@tonic-gate static int doswap(void);
80*0Sstevel@tonic-gate static int valid(char *, off_t, off_t);
81*0Sstevel@tonic-gate static int list(void);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate int
84*0Sstevel@tonic-gate main(int argc, char **argv)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	int c, flag = 0;
87*0Sstevel@tonic-gate 	int ret;
88*0Sstevel@tonic-gate 	off_t s_offset = 0;
89*0Sstevel@tonic-gate 	off_t length = 0;
90*0Sstevel@tonic-gate 	char *pathname;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
95*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
96*0Sstevel@tonic-gate #endif
97*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	prognamep = argv[0];
100*0Sstevel@tonic-gate 	if (argc < 2) {
101*0Sstevel@tonic-gate 		usage();
102*0Sstevel@tonic-gate 		exit(1);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "lsd:a:12")) != EOF) {
106*0Sstevel@tonic-gate 		char *char_p;
107*0Sstevel@tonic-gate 		switch (c) {
108*0Sstevel@tonic-gate 		case 'l': 	/* list all the swap devices */
109*0Sstevel@tonic-gate 			if (argc != 2 || flag) {
110*0Sstevel@tonic-gate 				usage();
111*0Sstevel@tonic-gate 				exit(1);
112*0Sstevel@tonic-gate 			}
113*0Sstevel@tonic-gate 			flag |= LFLAG;
114*0Sstevel@tonic-gate 			ret = list();
115*0Sstevel@tonic-gate 			break;
116*0Sstevel@tonic-gate 		case 's':
117*0Sstevel@tonic-gate 			if (argc != 2 || flag) {
118*0Sstevel@tonic-gate 				usage();
119*0Sstevel@tonic-gate 				exit(1);
120*0Sstevel@tonic-gate 			}
121*0Sstevel@tonic-gate 			flag |= SFLAG;
122*0Sstevel@tonic-gate 			ret = doswap();
123*0Sstevel@tonic-gate 			break;
124*0Sstevel@tonic-gate 		case 'd':
125*0Sstevel@tonic-gate 			/*
126*0Sstevel@tonic-gate 			 * The argument for starting offset is optional.
127*0Sstevel@tonic-gate 			 * If no argument is specified, the entire swap file
128*0Sstevel@tonic-gate 			 * is added although this will fail if a non-zero
129*0Sstevel@tonic-gate 			 * starting offset was specified when added.
130*0Sstevel@tonic-gate 			 */
131*0Sstevel@tonic-gate 			if ((argc - optind) > 1 || flag != 0) {
132*0Sstevel@tonic-gate 				usage();
133*0Sstevel@tonic-gate 				exit(1);
134*0Sstevel@tonic-gate 			}
135*0Sstevel@tonic-gate 			flag |= DFLAG;
136*0Sstevel@tonic-gate 			pathname = optarg;
137*0Sstevel@tonic-gate 			if (optind < argc) {
138*0Sstevel@tonic-gate 				errno = 0;
139*0Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
140*0Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
141*0Sstevel@tonic-gate 					(void) fprintf(stderr,
142*0Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
143*0Sstevel@tonic-gate 					exit(1);
144*0Sstevel@tonic-gate 				}
145*0Sstevel@tonic-gate 			}
146*0Sstevel@tonic-gate 			ret = delete(pathname, s_offset);
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		case 'a':
150*0Sstevel@tonic-gate 			/*
151*0Sstevel@tonic-gate 			 * The arguments for starting offset and number of
152*0Sstevel@tonic-gate 			 * blocks are optional.  If only the starting offset
153*0Sstevel@tonic-gate 			 * is specified, all the blocks to the end of the swap
154*0Sstevel@tonic-gate 			 * file will be added.  If no starting offset is
155*0Sstevel@tonic-gate 			 * specified, the entire swap file is assumed.
156*0Sstevel@tonic-gate 			 */
157*0Sstevel@tonic-gate 			if ((argc - optind) > 2 ||
158*0Sstevel@tonic-gate 			    (flag & ~(P1FLAG | P2FLAG)) != 0) {
159*0Sstevel@tonic-gate 				usage();
160*0Sstevel@tonic-gate 				exit(1);
161*0Sstevel@tonic-gate 			}
162*0Sstevel@tonic-gate 			if (*optarg != '/') {
163*0Sstevel@tonic-gate 				(void) fprintf(stderr,
164*0Sstevel@tonic-gate 				    gettext("%s: path must be absolute\n"),
165*0Sstevel@tonic-gate 				    prognamep);
166*0Sstevel@tonic-gate 				exit(1);
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 			flag |= AFLAG;
169*0Sstevel@tonic-gate 			pathname = optarg;
170*0Sstevel@tonic-gate 			if (optind < argc) {
171*0Sstevel@tonic-gate 				errno = 0;
172*0Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
173*0Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
174*0Sstevel@tonic-gate 					(void) fprintf(stderr,
175*0Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
176*0Sstevel@tonic-gate 					exit(1);
177*0Sstevel@tonic-gate 				}
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 			if (optind < argc) {
180*0Sstevel@tonic-gate 				errno = 0;
181*0Sstevel@tonic-gate 				length = strtol(argv[optind++], &char_p, 10);
182*0Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
183*0Sstevel@tonic-gate 					(void) fprintf(stderr,
184*0Sstevel@tonic-gate 					gettext("error in [nbr of blocks]\n"));
185*0Sstevel@tonic-gate 					exit(1);
186*0Sstevel@tonic-gate 				}
187*0Sstevel@tonic-gate 			}
188*0Sstevel@tonic-gate 			if ((ret = valid(pathname,
189*0Sstevel@tonic-gate 			    s_offset * 512, length * 512)) == 0)
190*0Sstevel@tonic-gate 				ret = add(pathname, s_offset, length, flag);
191*0Sstevel@tonic-gate 			break;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		case '1':
194*0Sstevel@tonic-gate 			flag |= P1FLAG;
195*0Sstevel@tonic-gate 			break;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		case '2':
198*0Sstevel@tonic-gate 			flag |= P2FLAG;
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		case '?':
202*0Sstevel@tonic-gate 			usage();
203*0Sstevel@tonic-gate 			exit(1);
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 	if (!flag) {
207*0Sstevel@tonic-gate 		usage();
208*0Sstevel@tonic-gate 		exit(1);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 	return (ret);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate static void
215*0Sstevel@tonic-gate usage(void)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep);
218*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s -s\n", prognamep);
219*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
220*0Sstevel@tonic-gate 			prognamep);
221*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
222*0Sstevel@tonic-gate 	    " [nbr of blocks]\n"), prognamep);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate /*
226*0Sstevel@tonic-gate  * Implement:
227*0Sstevel@tonic-gate  *	#define ctok(x) ((ctob(x))>>10)
228*0Sstevel@tonic-gate  * in a machine independent way. (Both assume a click > 1k)
229*0Sstevel@tonic-gate  */
230*0Sstevel@tonic-gate static size_t
231*0Sstevel@tonic-gate ctok(pgcnt_t clicks)
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate 	static int factor = -1;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	if (factor == -1)
236*0Sstevel@tonic-gate 		factor = (int)(sysconf(_SC_PAGESIZE) >> 10);
237*0Sstevel@tonic-gate 	return ((size_t)(clicks * factor));
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate static int
242*0Sstevel@tonic-gate doswap(void)
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate 	struct anoninfo ai;
245*0Sstevel@tonic-gate 	pgcnt_t allocated, reserved, available;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	/*
248*0Sstevel@tonic-gate 	 * max = total amount of swap space including physical memory
249*0Sstevel@tonic-gate 	 * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) +
250*0Sstevel@tonic-gate 	 *	availrmem - swapfs_minfree;
251*0Sstevel@tonic-gate 	 * ai.ani_free = amount of unallocated anonymous memory
252*0Sstevel@tonic-gate 	 *	(ie. = resverved_unallocated + unreserved)
253*0Sstevel@tonic-gate 	 * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree);
254*0Sstevel@tonic-gate 	 * ai.ani_resv = total amount of reserved anonymous memory
255*0Sstevel@tonic-gate 	 * ai.ani_resv = anoninfo.ani_resv;
256*0Sstevel@tonic-gate 	 *
257*0Sstevel@tonic-gate 	 * allocated = anon memory not free
258*0Sstevel@tonic-gate 	 * reserved = anon memory reserved but not allocated
259*0Sstevel@tonic-gate 	 * available = anon memory not reserved
260*0Sstevel@tonic-gate 	 */
261*0Sstevel@tonic-gate 	if (swapctl(SC_AINFO, &ai) == -1) {
262*0Sstevel@tonic-gate 		perror(prognamep);
263*0Sstevel@tonic-gate 		return (2);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	allocated = ai.ani_max - ai.ani_free;
267*0Sstevel@tonic-gate 	reserved = ai.ani_resv - allocated;
268*0Sstevel@tonic-gate 	available = ai.ani_max - ai.ani_resv;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * TRANSLATION_NOTE
272*0Sstevel@tonic-gate 	 * Translations (if any) of these keywords should match with
273*0Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
274*0Sstevel@tonic-gate 	 * -s option:  "allocated", "reserved", "used", "available"
275*0Sstevel@tonic-gate 	 */
276*0Sstevel@tonic-gate 	(void) printf(gettext("total: %luk bytes allocated + %luk reserved = \
277*0Sstevel@tonic-gate %luk used, %luk available\n"),
278*0Sstevel@tonic-gate 	    ctok(allocated), ctok(reserved), ctok(reserved) + ctok(allocated),
279*0Sstevel@tonic-gate 	    ctok(available));
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	return (0);
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate static int
285*0Sstevel@tonic-gate list(void)
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	struct swaptable 	*st;
288*0Sstevel@tonic-gate 	struct swapent	*swapent;
289*0Sstevel@tonic-gate 	int	i;
290*0Sstevel@tonic-gate 	struct stat64 statbuf;
291*0Sstevel@tonic-gate 	char		*path;
292*0Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN+1];
293*0Sstevel@tonic-gate 	int		num;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
296*0Sstevel@tonic-gate 		perror(prognamep);
297*0Sstevel@tonic-gate 		return (2);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 	if (num == 0) {
300*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No swap devices configured\n"));
301*0Sstevel@tonic-gate 		return (1);
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if ((st = malloc(num * sizeof (swapent_t) + sizeof (int)))
305*0Sstevel@tonic-gate 	    == NULL) {
306*0Sstevel@tonic-gate 		(void) fprintf(stderr,
307*0Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
308*0Sstevel@tonic-gate 		perror(prognamep);
309*0Sstevel@tonic-gate 		return (2);
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 	if ((path = malloc(num * MAXPATHLEN)) == NULL) {
312*0Sstevel@tonic-gate 		(void) fprintf(stderr,
313*0Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
314*0Sstevel@tonic-gate 		perror(prognamep);
315*0Sstevel@tonic-gate 		return (2);
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 	swapent = st->swt_ent;
318*0Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
319*0Sstevel@tonic-gate 		swapent->ste_path = path;
320*0Sstevel@tonic-gate 		path += MAXPATHLEN;
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	st->swt_n = num;
324*0Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
325*0Sstevel@tonic-gate 		perror(prognamep);
326*0Sstevel@tonic-gate 		return (2);
327*0Sstevel@tonic-gate 	}
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	/*
330*0Sstevel@tonic-gate 	 * TRANSLATION_NOTE
331*0Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
332*0Sstevel@tonic-gate 	 * alignment of header and output.
333*0Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
334*0Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
335*0Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
336*0Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
337*0Sstevel@tonic-gate 	 * 	1st & 2nd fields.
338*0Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
339*0Sstevel@tonic-gate 	 *
340*0Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
341*0Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
342*0Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
343*0Sstevel@tonic-gate 	 */
344*0Sstevel@tonic-gate 	(void) printf(
345*0Sstevel@tonic-gate 		gettext("swapfile             dev  swaplo blocks   free\n"));
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	swapent = st->swt_ent;
348*0Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
349*0Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
350*0Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
351*0Sstevel@tonic-gate 				"/dev/%s", swapent->ste_path);
352*0Sstevel@tonic-gate 		else
353*0Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
354*0Sstevel@tonic-gate 				"%s", swapent->ste_path);
355*0Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
356*0Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
357*0Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
358*0Sstevel@tonic-gate 					swapent->ste_path);
359*0Sstevel@tonic-gate 			else
360*0Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
361*0Sstevel@tonic-gate 					fullpath);
362*0Sstevel@tonic-gate 		else {
363*0Sstevel@tonic-gate 			if (statbuf.st_mode & (S_IFBLK | S_IFCHR))
364*0Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
365*0Sstevel@tonic-gate 				    fullpath,
366*0Sstevel@tonic-gate 				    major(statbuf.st_rdev),
367*0Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
368*0Sstevel@tonic-gate 			else
369*0Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 		{
372*0Sstevel@tonic-gate 		int diskblks_per_page =
373*0Sstevel@tonic-gate 			(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
374*0Sstevel@tonic-gate 		(void) printf(gettext(" %6lu %6lu %6lu"), swapent->ste_start,
375*0Sstevel@tonic-gate 		    swapent->ste_pages * diskblks_per_page,
376*0Sstevel@tonic-gate 		    swapent->ste_free * diskblks_per_page);
377*0Sstevel@tonic-gate 		}
378*0Sstevel@tonic-gate 		if (swapent->ste_flags & ST_INDEL)
379*0Sstevel@tonic-gate 			(void) printf(" INDEL\n");
380*0Sstevel@tonic-gate 		else
381*0Sstevel@tonic-gate 			(void) printf("\n");
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 	return (0);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate static void
387*0Sstevel@tonic-gate dumpadm_err(const char *warning)
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
390*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
391*0Sstevel@tonic-gate 	    "run dumpadm(1M) to verify dump configuration\n"));
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate static int
395*0Sstevel@tonic-gate delete(char *path, off_t offset)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	swapres_t swr;
398*0Sstevel@tonic-gate 	int fd;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	swr.sr_name = path;
401*0Sstevel@tonic-gate 	swr.sr_start = offset;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
404*0Sstevel@tonic-gate 		switch (errno) {
405*0Sstevel@tonic-gate 		case (ENOSYS):
406*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
407*0Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
408*0Sstevel@tonic-gate 			    path);
409*0Sstevel@tonic-gate 			break;
410*0Sstevel@tonic-gate 		default:
411*0Sstevel@tonic-gate 			perror(path);
412*0Sstevel@tonic-gate 			break;
413*0Sstevel@tonic-gate 		}
414*0Sstevel@tonic-gate 		return (2);
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	/*
418*0Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
419*0Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
420*0Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
421*0Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
422*0Sstevel@tonic-gate 	 */
423*0Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
424*0Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
427*0Sstevel@tonic-gate 			if (errno == ENODEV) {
428*0Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
429*0Sstevel@tonic-gate 				    "invoking dumpadm(1M) -d swap to "
430*0Sstevel@tonic-gate 				    "select new dump device\n"), path);
431*0Sstevel@tonic-gate 				/*
432*0Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
433*0Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
434*0Sstevel@tonic-gate 				 */
435*0Sstevel@tonic-gate 				(void) close(fd);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
438*0Sstevel@tonic-gate 					dumpadm_err(gettext(
439*0Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
440*0Sstevel@tonic-gate 			} else
441*0Sstevel@tonic-gate 				dumpadm_err(gettext(
442*0Sstevel@tonic-gate 				"Warning: failed to check dump device"));
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 		(void) close(fd);
445*0Sstevel@tonic-gate 	} else
446*0Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	return (0);
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate /*
452*0Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
453*0Sstevel@tonic-gate  */
454*0Sstevel@tonic-gate static int
455*0Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
456*0Sstevel@tonic-gate {
457*0Sstevel@tonic-gate 	swapres_t swr;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
460*0Sstevel@tonic-gate 	struct statvfs fsb;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	/*
463*0Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
464*0Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
465*0Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
466*0Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
467*0Sstevel@tonic-gate 	 */
468*0Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
469*0Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
472*0Sstevel@tonic-gate 			if (errno == ENODEV)
473*0Sstevel@tonic-gate 				have_dumpdev = 0;
474*0Sstevel@tonic-gate 			else
475*0Sstevel@tonic-gate 				dumpadm_err(gettext(
476*0Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 		(void) close(fd);
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
482*0Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	swr.sr_name = path;
485*0Sstevel@tonic-gate 	swr.sr_start = offset;
486*0Sstevel@tonic-gate 	swr.sr_length = cnt;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
489*0Sstevel@tonic-gate 		switch (errno) {
490*0Sstevel@tonic-gate 		case (ENOSYS):
491*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
492*0Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
493*0Sstevel@tonic-gate 			    path);
494*0Sstevel@tonic-gate 			break;
495*0Sstevel@tonic-gate 		case (EEXIST):
496*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
497*0Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
498*0Sstevel@tonic-gate 			    path);
499*0Sstevel@tonic-gate 			break;
500*0Sstevel@tonic-gate 		default:
501*0Sstevel@tonic-gate 			perror(path);
502*0Sstevel@tonic-gate 			break;
503*0Sstevel@tonic-gate 		}
504*0Sstevel@tonic-gate 		return (2);
505*0Sstevel@tonic-gate 	}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	/*
508*0Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
509*0Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
510*0Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
511*0Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
512*0Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
513*0Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
514*0Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
515*0Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
516*0Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
517*0Sstevel@tonic-gate 	 * we would lose the user's previous setting.
518*0Sstevel@tonic-gate 	 */
519*0Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
520*0Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 		(void) printf(
523*0Sstevel@tonic-gate 			gettext("operating system crash dump was previously "
524*0Sstevel@tonic-gate 		    "disabled --\ninvoking dumpadm(1M) -d swap to select "
525*0Sstevel@tonic-gate 		    "new dump device\n"));
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
528*0Sstevel@tonic-gate 			dumpadm_err(gettext(
529*0Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	return (0);
533*0Sstevel@tonic-gate }
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate static int
536*0Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	struct stat64		f;
539*0Sstevel@tonic-gate 	struct statvfs64	fs;
540*0Sstevel@tonic-gate 	off_t		need;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
543*0Sstevel@tonic-gate 		(void) perror(pathname);
544*0Sstevel@tonic-gate 		return (errno);
545*0Sstevel@tonic-gate 	}
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
548*0Sstevel@tonic-gate 		S_ISBLK(f.st_mode))) {
549*0Sstevel@tonic-gate 		(void) fprintf(stderr,
550*0Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
551*0Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
552*0Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
553*0Sstevel@tonic-gate 		    pathname);
554*0Sstevel@tonic-gate 		return (EINVAL);
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
558*0Sstevel@tonic-gate 		if (length == 0)
559*0Sstevel@tonic-gate 			length = (off_t)f.st_size;
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 		/*
562*0Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
563*0Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
564*0Sstevel@tonic-gate 		 */
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
567*0Sstevel@tonic-gate 		    f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) {
568*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
569*0Sstevel@tonic-gate 			    pathname);
570*0Sstevel@tonic-gate 			return (EINVAL);
571*0Sstevel@tonic-gate 		}
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 		if (offset < 0) {
574*0Sstevel@tonic-gate 			(void) fprintf(stderr,
575*0Sstevel@tonic-gate 				gettext("%s: low block is invalid\n"),
576*0Sstevel@tonic-gate 				pathname);
577*0Sstevel@tonic-gate 			return (EINVAL);
578*0Sstevel@tonic-gate 		}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 		/*
583*0Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
584*0Sstevel@tonic-gate 		 * Note:
585*0Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
586*0Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
587*0Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
588*0Sstevel@tonic-gate 		 *  about the underlying storage implementation.
589*0Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
590*0Sstevel@tonic-gate 		 *  there must be a hole.
591*0Sstevel@tonic-gate 		 */
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 		if (need > f.st_blocks) {
594*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
595*0Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
596*0Sstevel@tonic-gate 			    pathname);
597*0Sstevel@tonic-gate 			return (EINVAL);
598*0Sstevel@tonic-gate 		}
599*0Sstevel@tonic-gate 	}
600*0Sstevel@tonic-gate 	/*
601*0Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
602*0Sstevel@tonic-gate 	 * no meaningful checking can be done.
603*0Sstevel@tonic-gate 	 */
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	return (0);
606*0Sstevel@tonic-gate }
607