xref: /onnv-gate/usr/src/cmd/rcm_daemon/common/swap_rcm.c (revision 7202:41866725ef2f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7202Svikram  * Common Development and Distribution License (the "License").
6*7202Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7202Svikram  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * RCM module providing support for swap areas
300Sstevel@tonic-gate  * during reconfiguration operations.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <thread.h>
360Sstevel@tonic-gate #include <synch.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <assert.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <libintl.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/swap.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/param.h>
450Sstevel@tonic-gate #include <sys/dumpadm.h>
460Sstevel@tonic-gate #include <sys/wait.h>
470Sstevel@tonic-gate #include "rcm_module.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* cache flags */
500Sstevel@tonic-gate #define	SWAP_CACHE_NEW		0x01
510Sstevel@tonic-gate #define	SWAP_CACHE_STALE	0x02
520Sstevel@tonic-gate #define	SWAP_CACHE_OFFLINED	0x04
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	SWAP_CMD		"/usr/sbin/swap"
550Sstevel@tonic-gate #define	SWAP_DELETE		SWAP_CMD" -d %s %ld"
560Sstevel@tonic-gate #define	SWAP_ADD		SWAP_CMD" -a %s %ld %ld"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* LP64 hard code */
590Sstevel@tonic-gate #define	MAXOFFSET_STRLEN	20
600Sstevel@tonic-gate 
610Sstevel@tonic-gate typedef struct swap_file {
620Sstevel@tonic-gate 	char			path[MAXPATHLEN];
630Sstevel@tonic-gate 	int			cache_flags;
640Sstevel@tonic-gate 	struct swap_area	*areas;
650Sstevel@tonic-gate 	struct swap_file	*next;
660Sstevel@tonic-gate 	struct swap_file	*prev;
670Sstevel@tonic-gate } swap_file_t;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* swap file may have multiple swap areas */
700Sstevel@tonic-gate typedef struct swap_area {
710Sstevel@tonic-gate 	off_t			start;
720Sstevel@tonic-gate 	off_t			len;
730Sstevel@tonic-gate 	int			cache_flags;
740Sstevel@tonic-gate 	struct swap_area	*next;
750Sstevel@tonic-gate 	struct swap_area	*prev;
760Sstevel@tonic-gate } swap_area_t;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static swap_file_t	*cache;
790Sstevel@tonic-gate static mutex_t		cache_lock;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int		swap_register(rcm_handle_t *);
820Sstevel@tonic-gate static int		swap_unregister(rcm_handle_t *);
830Sstevel@tonic-gate static int		swap_getinfo(rcm_handle_t *, char *, id_t, uint_t,
840Sstevel@tonic-gate 			    char **, char **, nvlist_t *, rcm_info_t **);
850Sstevel@tonic-gate static int		swap_suspend(rcm_handle_t *, char *, id_t, timespec_t *,
860Sstevel@tonic-gate 			    uint_t, char **, rcm_info_t **);
870Sstevel@tonic-gate static int		swap_resume(rcm_handle_t *, char *, id_t, uint_t,
880Sstevel@tonic-gate 			    char **, rcm_info_t **);
890Sstevel@tonic-gate static int		swap_offline(rcm_handle_t *, char *, id_t, uint_t,
900Sstevel@tonic-gate 			    char **, rcm_info_t **);
910Sstevel@tonic-gate static int		swap_online(rcm_handle_t *, char *, id_t, uint_t,
920Sstevel@tonic-gate 			    char **, rcm_info_t **);
930Sstevel@tonic-gate static int		swap_remove(rcm_handle_t *, char *, id_t, uint_t,
940Sstevel@tonic-gate 			    char **, rcm_info_t **);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static int		alloc_usage(char **);
970Sstevel@tonic-gate static void		cache_insert(swap_file_t *);
980Sstevel@tonic-gate static swap_file_t	*cache_lookup(char *);
990Sstevel@tonic-gate static void		cache_remove(swap_file_t *);
1000Sstevel@tonic-gate static void		free_cache(void);
1010Sstevel@tonic-gate static int		get_dumpdev(char []);
1020Sstevel@tonic-gate static void		log_cmd_status(int);
1030Sstevel@tonic-gate static int		swap_add(swap_file_t *, char **);
1040Sstevel@tonic-gate static void		swap_area_add(swap_file_t *, swap_area_t *);
1050Sstevel@tonic-gate static swap_area_t	*swap_area_alloc(swapent_t *);
1060Sstevel@tonic-gate static swap_area_t	*swap_area_lookup(swap_file_t *, swapent_t *);
1070Sstevel@tonic-gate static void		swap_area_remove(swap_file_t *, swap_area_t *);
1080Sstevel@tonic-gate static int		swap_delete(swap_file_t *, char **);
1090Sstevel@tonic-gate static swap_file_t	*swap_file_alloc(char *);
1100Sstevel@tonic-gate static void		swap_file_free(swap_file_t *);
1110Sstevel@tonic-gate static swaptbl_t	*sys_swaptbl(void);
1120Sstevel@tonic-gate static int		update_cache(rcm_handle_t *);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static struct rcm_mod_ops swap_ops =
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	RCM_MOD_OPS_VERSION,
1170Sstevel@tonic-gate 	swap_register,
1180Sstevel@tonic-gate 	swap_unregister,
1190Sstevel@tonic-gate 	swap_getinfo,
1200Sstevel@tonic-gate 	swap_suspend,
1210Sstevel@tonic-gate 	swap_resume,
1220Sstevel@tonic-gate 	swap_offline,
1230Sstevel@tonic-gate 	swap_online,
1240Sstevel@tonic-gate 	swap_remove,
1250Sstevel@tonic-gate 	NULL,
1260Sstevel@tonic-gate 	NULL,
1270Sstevel@tonic-gate 	NULL
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate struct rcm_mod_ops *
rcm_mod_init()1310Sstevel@tonic-gate rcm_mod_init()
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	return (&swap_ops);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate const char *
rcm_mod_info()1370Sstevel@tonic-gate rcm_mod_info()
1380Sstevel@tonic-gate {
139*7202Svikram 	return ("RCM Swap module 1.5");
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate int
rcm_mod_fini()1430Sstevel@tonic-gate rcm_mod_fini()
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	free_cache();
1460Sstevel@tonic-gate 	(void) mutex_destroy(&cache_lock);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	return (RCM_SUCCESS);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int
swap_register(rcm_handle_t * hdl)1520Sstevel@tonic-gate swap_register(rcm_handle_t *hdl)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	return (update_cache(hdl));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static int
swap_unregister(rcm_handle_t * hdl)1580Sstevel@tonic-gate swap_unregister(rcm_handle_t *hdl)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	swap_file_t	*sf;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1630Sstevel@tonic-gate 	while ((sf = cache) != NULL) {
1640Sstevel@tonic-gate 		cache = cache->next;
1650Sstevel@tonic-gate 		(void) rcm_unregister_interest(hdl, sf->path, 0);
1660Sstevel@tonic-gate 		swap_file_free(sf);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (RCM_SUCCESS);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*ARGSUSED*/
1740Sstevel@tonic-gate static int
swap_getinfo(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** infostr,char ** errstr,nvlist_t * props,rcm_info_t ** dependent)1750Sstevel@tonic-gate swap_getinfo(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
1760Sstevel@tonic-gate     char **infostr, char **errstr, nvlist_t *props, rcm_info_t **dependent)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	assert(rsrcname != NULL && infostr != NULL);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1810Sstevel@tonic-gate 	if (cache_lookup(rsrcname) == NULL) {
1820Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "unknown resource: %s\n",
1830Sstevel@tonic-gate 		    rsrcname);
1840Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
1850Sstevel@tonic-gate 		return (RCM_FAILURE);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1880Sstevel@tonic-gate 	(void) alloc_usage(infostr);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	return (RCM_SUCCESS);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate  * Remove swap space to maintain availability of anonymous pages
1950Sstevel@tonic-gate  * during device suspension. Swap will be reconfigured upon resume.
1960Sstevel@tonic-gate  * Fail if operation will unconfigure dump device.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate /*ARGSUSED*/
1990Sstevel@tonic-gate static int
swap_suspend(rcm_handle_t * hdl,char * rsrcname,id_t id,timespec_t * interval,uint_t flags,char ** errstr,rcm_info_t ** dependent)2000Sstevel@tonic-gate swap_suspend(rcm_handle_t *hdl, char *rsrcname, id_t id, timespec_t *interval,
2010Sstevel@tonic-gate     uint_t flags, char **errstr, rcm_info_t **dependent)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	swap_file_t	*sf;
2040Sstevel@tonic-gate 	int		rv;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (flags & RCM_QUERY)
2090Sstevel@tonic-gate 		return (RCM_SUCCESS);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2120Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2130Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2140Sstevel@tonic-gate 		return (RCM_SUCCESS);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	rv = swap_delete(sf, errstr);
2180Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (rv);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*ARGSUSED*/
2240Sstevel@tonic-gate static int
swap_resume(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2250Sstevel@tonic-gate swap_resume(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2260Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	swap_file_t	*sf;
2290Sstevel@tonic-gate 	int		rv;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2340Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2350Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2360Sstevel@tonic-gate 		return (RCM_SUCCESS);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	rv = swap_add(sf, errstr);
2400Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (rv);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * By default, reject offline request. If forced, attempt to
2470Sstevel@tonic-gate  * delete swap. Fail if operation will unconfigure dump device.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate /*ARGSUSED*/
2500Sstevel@tonic-gate static int
swap_offline(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2510Sstevel@tonic-gate swap_offline(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2520Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	swap_file_t	*sf;
2550Sstevel@tonic-gate 	int		rv;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if ((flags & RCM_FORCE) && (flags & RCM_QUERY))
2600Sstevel@tonic-gate 		return (RCM_SUCCESS);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2630Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2640Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2650Sstevel@tonic-gate 		return (RCM_SUCCESS);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (flags & RCM_FORCE) {
2690Sstevel@tonic-gate 		rv = swap_delete(sf, errstr);
2700Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2710Sstevel@tonic-gate 		return (rv);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 	/* default reject */
2740Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2750Sstevel@tonic-gate 	(void) alloc_usage(errstr);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (RCM_FAILURE);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*ARGSUSED*/
2810Sstevel@tonic-gate static int
swap_online(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2820Sstevel@tonic-gate swap_online(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2830Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	swap_file_t	*sf;
2860Sstevel@tonic-gate 	int		rv;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2910Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2920Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2930Sstevel@tonic-gate 		return (RCM_SUCCESS);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	rv = swap_add(sf, errstr);
2970Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (rv);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*ARGSUSED*/
3030Sstevel@tonic-gate static int
swap_remove(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)3040Sstevel@tonic-gate swap_remove(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
3050Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	swap_file_t	*sf;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	assert(rsrcname != NULL);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
3120Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
3130Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
3140Sstevel@tonic-gate 		return (RCM_SUCCESS);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	/* RCM framework handles unregistration */
3170Sstevel@tonic-gate 	cache_remove(sf);
3180Sstevel@tonic-gate 	swap_file_free(sf);
3190Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	return (RCM_SUCCESS);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Delete all swap areas for swap file.
3260Sstevel@tonic-gate  * Invoke swap(1M) instead of swapctl(2) to
3270Sstevel@tonic-gate  * handle relocation of dump device.
3280Sstevel@tonic-gate  * If dump device is configured, fail if
3290Sstevel@tonic-gate  * unable to relocate dump.
3300Sstevel@tonic-gate  *
3310Sstevel@tonic-gate  * Call with cache_lock held.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate static int
swap_delete(swap_file_t * sf,char ** errstr)3340Sstevel@tonic-gate swap_delete(swap_file_t *sf, char **errstr)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	swap_area_t	*sa;
3370Sstevel@tonic-gate 	char		cmd[sizeof (SWAP_DELETE) + MAXPATHLEN +
3380Sstevel@tonic-gate 			    MAXOFFSET_STRLEN];
3390Sstevel@tonic-gate 	char		dumpdev[MAXPATHLEN];
3400Sstevel@tonic-gate 	int		have_dump = 1;
3410Sstevel@tonic-gate 	int		stat;
3420Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (get_dumpdev(dumpdev) == 0 && dumpdev[0] == '\0')
3450Sstevel@tonic-gate 		have_dump = 0;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
3480Sstevel@tonic-gate 		/* swap(1M) is not idempotent */
3490Sstevel@tonic-gate 		if (sa->cache_flags & SWAP_CACHE_OFFLINED) {
3500Sstevel@tonic-gate 			continue;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd), SWAP_DELETE, sf->path,
3540Sstevel@tonic-gate 		    sa->start);
3550Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3560Sstevel@tonic-gate 		if ((stat = rcm_exec_cmd(cmd)) != 0) {
3570Sstevel@tonic-gate 			log_cmd_status(stat);
3580Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to delete swap"));
3590Sstevel@tonic-gate 			rv = RCM_FAILURE;
3600Sstevel@tonic-gate 			goto out;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 		sa->cache_flags |= SWAP_CACHE_OFFLINED;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		/*
3650Sstevel@tonic-gate 		 * Fail on removal of dump device.
3660Sstevel@tonic-gate 		 */
3670Sstevel@tonic-gate 		if (have_dump == 0)
3680Sstevel@tonic-gate 			continue;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		if (get_dumpdev(dumpdev) != 0) {
3710Sstevel@tonic-gate 			rcm_log_message(RCM_WARNING, "unable to "
3720Sstevel@tonic-gate 			    "check for removal of dump device\n");
3730Sstevel@tonic-gate 		} else if (dumpdev[0] == '\0') {
3740Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "removed dump: "
3750Sstevel@tonic-gate 			    "attempting recovery\n");
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 			/*
3780Sstevel@tonic-gate 			 * Restore dump
3790Sstevel@tonic-gate 			 */
3800Sstevel@tonic-gate 			(void) snprintf(cmd, sizeof (cmd), SWAP_ADD,
3810Sstevel@tonic-gate 				sf->path, sa->start, sa->len);
3820Sstevel@tonic-gate 			rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3830Sstevel@tonic-gate 			if ((stat = rcm_exec_cmd(cmd)) != 0) {
3840Sstevel@tonic-gate 				log_cmd_status(stat);
3850Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR,
3860Sstevel@tonic-gate 				    "failed to restore dump\n");
3870Sstevel@tonic-gate 			} else {
3880Sstevel@tonic-gate 				sa->cache_flags &= ~SWAP_CACHE_OFFLINED;
3890Sstevel@tonic-gate 				rcm_log_message(RCM_DEBUG, "dump restored\n");
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to relocate dump"));
3920Sstevel@tonic-gate 			rv = RCM_FAILURE;
3930Sstevel@tonic-gate 			goto out;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	sf->cache_flags |= SWAP_CACHE_OFFLINED;
3970Sstevel@tonic-gate out:
3980Sstevel@tonic-gate 	return (rv);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * Invoke swap(1M) to add each registered swap area.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * Call with cache_lock held.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate static int
swap_add(swap_file_t * sf,char ** errstr)4070Sstevel@tonic-gate swap_add(swap_file_t *sf, char **errstr)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	swap_area_t	*sa;
4100Sstevel@tonic-gate 	char		cmd[sizeof (SWAP_ADD) + MAXPATHLEN +
4110Sstevel@tonic-gate 			    (2 * MAXOFFSET_STRLEN)];
4120Sstevel@tonic-gate 	int		stat;
4130Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
4160Sstevel@tonic-gate 		/* swap(1M) is not idempotent */
4170Sstevel@tonic-gate 		if (!(sa->cache_flags & SWAP_CACHE_OFFLINED)) {
4180Sstevel@tonic-gate 			continue;
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
4220Sstevel@tonic-gate 			SWAP_ADD, sf->path, sa->start, sa->len);
4230Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE1, "%s\n", cmd);
4240Sstevel@tonic-gate 		if ((stat = rcm_exec_cmd(cmd)) != 0) {
4250Sstevel@tonic-gate 			log_cmd_status(stat);
4260Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to add swap"));
4270Sstevel@tonic-gate 			rv = RCM_FAILURE;
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 		} else {
4300Sstevel@tonic-gate 			sa->cache_flags &= ~SWAP_CACHE_OFFLINED;
4310Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_OFFLINED;
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	return (rv);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate static int
update_cache(rcm_handle_t * hdl)4390Sstevel@tonic-gate update_cache(rcm_handle_t *hdl)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	swaptbl_t	*swt;
4420Sstevel@tonic-gate 	swap_file_t	*sf, *stale_sf;
4430Sstevel@tonic-gate 	swap_area_t	*sa, *stale_sa;
4440Sstevel@tonic-gate 	int		i;
4450Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	if ((swt = sys_swaptbl()) == NULL) {
4480Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "failed to read "
4490Sstevel@tonic-gate 		    "current swap configuration\n");
4500Sstevel@tonic-gate 		return (RCM_FAILURE);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * cache pass 1 - mark everyone stale
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	for (sf = cache; sf != NULL; sf = sf->next) {
4590Sstevel@tonic-gate 		sf->cache_flags |= SWAP_CACHE_STALE;
4600Sstevel@tonic-gate 		for (sa = sf->areas; sa != NULL; sa = sa->next) {
4610Sstevel@tonic-gate 			sa->cache_flags |= SWAP_CACHE_STALE;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * add new entries
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	for (i = 0; i < swt->swt_n; i++) {
4690Sstevel@tonic-gate 		if (swt->swt_ent[i].ste_flags & (ST_INDEL|ST_DOINGDEL)) {
4700Sstevel@tonic-gate 			continue;
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 		/*
4740Sstevel@tonic-gate 		 * assure swap_file_t
4750Sstevel@tonic-gate 		 */
4760Sstevel@tonic-gate 		if ((sf = cache_lookup(swt->swt_ent[i].ste_path)) == NULL) {
4770Sstevel@tonic-gate 			if ((sf = swap_file_alloc(swt->swt_ent[i].ste_path)) ==
4780Sstevel@tonic-gate 			    NULL) {
4790Sstevel@tonic-gate 				free(swt);
4800Sstevel@tonic-gate 				return (RCM_FAILURE);
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 			sf->cache_flags |= SWAP_CACHE_NEW;
4830Sstevel@tonic-gate 			cache_insert(sf);
4840Sstevel@tonic-gate 		} else {
4850Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_STALE;
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		/*
4890Sstevel@tonic-gate 		 * assure swap_area_t
4900Sstevel@tonic-gate 		 */
4910Sstevel@tonic-gate 		if ((sa = swap_area_lookup(sf, &swt->swt_ent[i])) == NULL) {
4920Sstevel@tonic-gate 			if ((sa = swap_area_alloc(&swt->swt_ent[i])) == NULL) {
4930Sstevel@tonic-gate 				free(swt);
4940Sstevel@tonic-gate 				return (RCM_FAILURE);
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 			swap_area_add(sf, sa);
4970Sstevel@tonic-gate 		} else {
4980Sstevel@tonic-gate 			sa->cache_flags &= ~SWAP_CACHE_STALE;
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	free(swt);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/*
5050Sstevel@tonic-gate 	 * cache pass 2
5060Sstevel@tonic-gate 	 *
5070Sstevel@tonic-gate 	 * swap_file_t - skip offlined, register new, unregister/remove stale
5080Sstevel@tonic-gate 	 * swap_area_t - skip offlined, remove stale
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	sf = cache;
5110Sstevel@tonic-gate 	while (sf != NULL) {
5120Sstevel@tonic-gate 		sa = sf->areas;
5130Sstevel@tonic-gate 		while (sa != NULL) {
5140Sstevel@tonic-gate 			if (sa->cache_flags & SWAP_CACHE_OFFLINED) {
5150Sstevel@tonic-gate 				sa->cache_flags &= ~SWAP_CACHE_STALE;
5160Sstevel@tonic-gate 				sa = sa->next;
5170Sstevel@tonic-gate 				continue;
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate 			if (sa->cache_flags & SWAP_CACHE_STALE) {
5200Sstevel@tonic-gate 				stale_sa = sa;
5210Sstevel@tonic-gate 				sa = sa->next;
5220Sstevel@tonic-gate 				swap_area_remove(sf, stale_sa);
5230Sstevel@tonic-gate 				free(stale_sa);
5240Sstevel@tonic-gate 				continue;
5250Sstevel@tonic-gate 			}
5260Sstevel@tonic-gate 			sa = sa->next;
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if (sf->cache_flags & SWAP_CACHE_OFFLINED) {
5300Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_STALE;
5310Sstevel@tonic-gate 			sf = sf->next;
5320Sstevel@tonic-gate 			continue;
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		if (sf->cache_flags & SWAP_CACHE_STALE) {
5360Sstevel@tonic-gate 			if (rcm_unregister_interest(hdl, sf->path, 0) !=
5370Sstevel@tonic-gate 			    RCM_SUCCESS) {
5380Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, "failed to register "
5390Sstevel@tonic-gate 				    "%s\n", sf->path);
5400Sstevel@tonic-gate 			}
5410Sstevel@tonic-gate 			stale_sf = sf;
5420Sstevel@tonic-gate 			sf = sf->next;
5430Sstevel@tonic-gate 			cache_remove(stale_sf);
5440Sstevel@tonic-gate 			swap_file_free(stale_sf);
5450Sstevel@tonic-gate 			continue;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		if (!(sf->cache_flags & SWAP_CACHE_NEW)) {
5490Sstevel@tonic-gate 			sf = sf->next;
5500Sstevel@tonic-gate 			continue;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 		if (rcm_register_interest(hdl, sf->path, 0, NULL) !=
5540Sstevel@tonic-gate 		    RCM_SUCCESS) {
5550Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "failed to register %s\n",
5560Sstevel@tonic-gate 			    sf->path);
5570Sstevel@tonic-gate 			rv = RCM_FAILURE;
5580Sstevel@tonic-gate 		} else {
5590Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "registered %s\n",
5600Sstevel@tonic-gate 			    sf->path);
5610Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_NEW;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		sf = sf->next;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	return (rv);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate  * Returns system swap table.
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate static swaptbl_t *
sys_swaptbl()5740Sstevel@tonic-gate sys_swaptbl()
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	swaptbl_t	*swt;
5770Sstevel@tonic-gate 	char		*cp;
5780Sstevel@tonic-gate 	int		i, n;
5790Sstevel@tonic-gate 	size_t		tbl_size;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if ((n = swapctl(SC_GETNSWP, NULL)) == -1)
5820Sstevel@tonic-gate 		return (NULL);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	tbl_size = sizeof (int) + n * sizeof (swapent_t) + n * MAXPATHLEN;
5850Sstevel@tonic-gate 	if ((swt = (swaptbl_t *)malloc(tbl_size)) == NULL)
5860Sstevel@tonic-gate 		return (NULL);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	swt->swt_n = n;
5890Sstevel@tonic-gate 	cp = (char *)swt + (sizeof (int) + n * sizeof (swapent_t));
5900Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
5910Sstevel@tonic-gate 		swt->swt_ent[i].ste_path = cp;
5920Sstevel@tonic-gate 		cp += MAXPATHLEN;
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if ((n = swapctl(SC_LIST, swt)) == -1) {
5960Sstevel@tonic-gate 		free(swt);
5970Sstevel@tonic-gate 		return (NULL);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (n != swt->swt_n) {
6010Sstevel@tonic-gate 		/* mismatch, try again */
6020Sstevel@tonic-gate 		free(swt);
6030Sstevel@tonic-gate 		return (sys_swaptbl());
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	return (swt);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate static int
get_dumpdev(char dumpdev[])6100Sstevel@tonic-gate get_dumpdev(char dumpdev[])
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	int	fd;
6130Sstevel@tonic-gate 	int	rv = 0;
6140Sstevel@tonic-gate 	char	*err;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) == -1) {
6170Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "failed to open /dev/dump\n");
6180Sstevel@tonic-gate 		return (-1);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
6220Sstevel@tonic-gate 		if (errno == ENODEV) {
6230Sstevel@tonic-gate 			dumpdev[0] = '\0';
6240Sstevel@tonic-gate 		} else {
6250Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
6260Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
6270Sstevel@tonic-gate 			rv = -1;
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	(void) close(fd);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	return (rv);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate static void
free_cache(void)6360Sstevel@tonic-gate free_cache(void)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	swap_file_t	*sf;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
6410Sstevel@tonic-gate 	while ((sf = cache) != NULL) {
6420Sstevel@tonic-gate 		cache = cache->next;
6430Sstevel@tonic-gate 		swap_file_free(sf);
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * Call with cache_lock held.
6510Sstevel@tonic-gate  */
6520Sstevel@tonic-gate static void
swap_file_free(swap_file_t * sf)6530Sstevel@tonic-gate swap_file_free(swap_file_t *sf)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate 	swap_area_t	*sa;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	assert(sf != NULL);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	while ((sa = sf->areas) != NULL) {
6600Sstevel@tonic-gate 		sf->areas = sf->areas->next;
6610Sstevel@tonic-gate 		free(sa);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 	free(sf);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * Call with cache_lock held.
6680Sstevel@tonic-gate  */
6690Sstevel@tonic-gate static void
cache_insert(swap_file_t * ent)6700Sstevel@tonic-gate cache_insert(swap_file_t *ent)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 	ent->next = cache;
6730Sstevel@tonic-gate 	if (ent->next)
6740Sstevel@tonic-gate 		ent->next->prev = ent;
6750Sstevel@tonic-gate 	ent->prev = NULL;
6760Sstevel@tonic-gate 	cache = ent;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate  * Call with cache_lock held.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate static swap_file_t *
cache_lookup(char * rsrc)6830Sstevel@tonic-gate cache_lookup(char *rsrc)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	swap_file_t	*sf;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	for (sf = cache; sf != NULL; sf = sf->next) {
6880Sstevel@tonic-gate 		if (strcmp(rsrc, sf->path) == 0) {
6890Sstevel@tonic-gate 			return (sf);
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	return (NULL);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate  * Call with cache_lock held.
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate static void
cache_remove(swap_file_t * ent)6990Sstevel@tonic-gate cache_remove(swap_file_t *ent)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	assert(ent != NULL);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (ent->next != NULL) {
7040Sstevel@tonic-gate 		ent->next->prev = ent->prev;
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 	if (ent->prev != NULL) {
7070Sstevel@tonic-gate 		ent->prev->next = ent->next;
7080Sstevel@tonic-gate 	} else {
7090Sstevel@tonic-gate 		cache = ent->next;
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	ent->next = NULL;
7120Sstevel@tonic-gate 	ent->prev = NULL;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*
7160Sstevel@tonic-gate  * Call with cache_lock held.
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate static void
swap_area_add(swap_file_t * sf,swap_area_t * sa)7190Sstevel@tonic-gate swap_area_add(swap_file_t *sf, swap_area_t *sa)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	sa->next = sf->areas;
7220Sstevel@tonic-gate 	if (sa->next)
7230Sstevel@tonic-gate 		sa->next->prev = sa;
7240Sstevel@tonic-gate 	sa->prev = NULL;
7250Sstevel@tonic-gate 	sf->areas = sa;
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate  * Call with cache_lock held.
7300Sstevel@tonic-gate  */
7310Sstevel@tonic-gate static void
swap_area_remove(swap_file_t * sf,swap_area_t * ent)7320Sstevel@tonic-gate swap_area_remove(swap_file_t *sf, swap_area_t *ent)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	assert(sf != NULL && ent != NULL);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (ent->next != NULL) {
7370Sstevel@tonic-gate 		ent->next->prev = ent->prev;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	if (ent->prev != NULL) {
7400Sstevel@tonic-gate 		ent->prev->next = ent->next;
7410Sstevel@tonic-gate 	} else {
7420Sstevel@tonic-gate 		sf->areas = ent->next;
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 	ent->next = NULL;
7450Sstevel@tonic-gate 	ent->prev = NULL;
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate static swap_file_t *
swap_file_alloc(char * rsrc)7490Sstevel@tonic-gate swap_file_alloc(char *rsrc)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	swap_file_t	*sf;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if ((sf = calloc(1, sizeof (*sf))) == NULL) {
7540Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
7550Sstevel@tonic-gate 		return (NULL);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	(void) strlcpy(sf->path, rsrc, sizeof (sf->path));
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	return (sf);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate static swap_area_t *
swap_area_alloc(swapent_t * swt_ent)7630Sstevel@tonic-gate swap_area_alloc(swapent_t *swt_ent)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate 	swap_area_t	*sa;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if ((sa = calloc(1, sizeof (*sa))) == NULL) {
7680Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
7690Sstevel@tonic-gate 		return (NULL);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	sa->start = swt_ent->ste_start;
7720Sstevel@tonic-gate 	sa->len = swt_ent->ste_length;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	return (sa);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /*
7780Sstevel@tonic-gate  * Call with cache_lock held.
7790Sstevel@tonic-gate  */
7800Sstevel@tonic-gate static swap_area_t *
swap_area_lookup(swap_file_t * sf,swapent_t * swt_ent)7810Sstevel@tonic-gate swap_area_lookup(swap_file_t *sf, swapent_t *swt_ent)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	swap_area_t	*sa;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	assert(sf != NULL && swt_ent != NULL);
7860Sstevel@tonic-gate 	assert(strcmp(sf->path, swt_ent->ste_path) == 0);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
7890Sstevel@tonic-gate 		if (sa->start == swt_ent->ste_start &&
7900Sstevel@tonic-gate 		    sa->len == swt_ent->ste_length) {
7910Sstevel@tonic-gate 			return (sa);
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 	return (NULL);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate  * All-purpose usage string.
7990Sstevel@tonic-gate  */
8000Sstevel@tonic-gate static int
alloc_usage(char ** cpp)8010Sstevel@tonic-gate alloc_usage(char **cpp)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	if ((*cpp = strdup(gettext("swap area"))) == NULL) {
8040Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "strdup failure\n");
8050Sstevel@tonic-gate 		return (-1);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 	return (0);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate static void
log_cmd_status(int stat)8110Sstevel@tonic-gate log_cmd_status(int stat)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	char	*err;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (stat == -1) {
8160Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait: %s\n",
8170Sstevel@tonic-gate 		    ((err = strerror(errno)) == NULL) ? "" : err);
8180Sstevel@tonic-gate 	} else if (WIFEXITED(stat)) {
8190Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "exit status: %d\n",
8200Sstevel@tonic-gate 		    WEXITSTATUS(stat));
8210Sstevel@tonic-gate 	} else {
8220Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait status: %d\n", stat);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate }
825