xref: /illumos-gate/usr/src/cmd/rcm_daemon/common/swap_rcm.c (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
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
5648495d6Svikram  * Common Development and Distribution License (the "License").
6648495d6Svikram  * 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 /*
22648495d6Svikram  * 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 /*
277c478bd9Sstevel@tonic-gate  * RCM module providing support for swap areas
287c478bd9Sstevel@tonic-gate  * during reconfiguration operations.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <thread.h>
347c478bd9Sstevel@tonic-gate #include <synch.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/swap.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include "rcm_module.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* cache flags */
487c478bd9Sstevel@tonic-gate #define	SWAP_CACHE_NEW		0x01
497c478bd9Sstevel@tonic-gate #define	SWAP_CACHE_STALE	0x02
507c478bd9Sstevel@tonic-gate #define	SWAP_CACHE_OFFLINED	0x04
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	SWAP_CMD		"/usr/sbin/swap"
537c478bd9Sstevel@tonic-gate #define	SWAP_DELETE		SWAP_CMD" -d %s %ld"
547c478bd9Sstevel@tonic-gate #define	SWAP_ADD		SWAP_CMD" -a %s %ld %ld"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* LP64 hard code */
577c478bd9Sstevel@tonic-gate #define	MAXOFFSET_STRLEN	20
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate typedef struct swap_file {
607c478bd9Sstevel@tonic-gate 	char			path[MAXPATHLEN];
617c478bd9Sstevel@tonic-gate 	int			cache_flags;
627c478bd9Sstevel@tonic-gate 	struct swap_area	*areas;
637c478bd9Sstevel@tonic-gate 	struct swap_file	*next;
647c478bd9Sstevel@tonic-gate 	struct swap_file	*prev;
657c478bd9Sstevel@tonic-gate } swap_file_t;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* swap file may have multiple swap areas */
687c478bd9Sstevel@tonic-gate typedef struct swap_area {
697c478bd9Sstevel@tonic-gate 	off_t			start;
707c478bd9Sstevel@tonic-gate 	off_t			len;
717c478bd9Sstevel@tonic-gate 	int			cache_flags;
727c478bd9Sstevel@tonic-gate 	struct swap_area	*next;
737c478bd9Sstevel@tonic-gate 	struct swap_area	*prev;
747c478bd9Sstevel@tonic-gate } swap_area_t;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static swap_file_t	*cache;
777c478bd9Sstevel@tonic-gate static mutex_t		cache_lock;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int		swap_register(rcm_handle_t *);
807c478bd9Sstevel@tonic-gate static int		swap_unregister(rcm_handle_t *);
817c478bd9Sstevel@tonic-gate static int		swap_getinfo(rcm_handle_t *, char *, id_t, uint_t,
827c478bd9Sstevel@tonic-gate 			    char **, char **, nvlist_t *, rcm_info_t **);
837c478bd9Sstevel@tonic-gate static int		swap_suspend(rcm_handle_t *, char *, id_t, timespec_t *,
847c478bd9Sstevel@tonic-gate 			    uint_t, char **, rcm_info_t **);
857c478bd9Sstevel@tonic-gate static int		swap_resume(rcm_handle_t *, char *, id_t, uint_t,
867c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
877c478bd9Sstevel@tonic-gate static int		swap_offline(rcm_handle_t *, char *, id_t, uint_t,
887c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
897c478bd9Sstevel@tonic-gate static int		swap_online(rcm_handle_t *, char *, id_t, uint_t,
907c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
917c478bd9Sstevel@tonic-gate static int		swap_remove(rcm_handle_t *, char *, id_t, uint_t,
927c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int		alloc_usage(char **);
957c478bd9Sstevel@tonic-gate static void		cache_insert(swap_file_t *);
967c478bd9Sstevel@tonic-gate static swap_file_t	*cache_lookup(char *);
977c478bd9Sstevel@tonic-gate static void		cache_remove(swap_file_t *);
987c478bd9Sstevel@tonic-gate static void		free_cache(void);
997c478bd9Sstevel@tonic-gate static int		get_dumpdev(char []);
1007c478bd9Sstevel@tonic-gate static void		log_cmd_status(int);
1017c478bd9Sstevel@tonic-gate static int		swap_add(swap_file_t *, char **);
1027c478bd9Sstevel@tonic-gate static void		swap_area_add(swap_file_t *, swap_area_t *);
1037c478bd9Sstevel@tonic-gate static swap_area_t	*swap_area_alloc(swapent_t *);
1047c478bd9Sstevel@tonic-gate static swap_area_t	*swap_area_lookup(swap_file_t *, swapent_t *);
1057c478bd9Sstevel@tonic-gate static void		swap_area_remove(swap_file_t *, swap_area_t *);
1067c478bd9Sstevel@tonic-gate static int		swap_delete(swap_file_t *, char **);
1077c478bd9Sstevel@tonic-gate static swap_file_t	*swap_file_alloc(char *);
1087c478bd9Sstevel@tonic-gate static void		swap_file_free(swap_file_t *);
1097c478bd9Sstevel@tonic-gate static swaptbl_t	*sys_swaptbl(void);
1107c478bd9Sstevel@tonic-gate static int		update_cache(rcm_handle_t *);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct rcm_mod_ops swap_ops =
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	RCM_MOD_OPS_VERSION,
1157c478bd9Sstevel@tonic-gate 	swap_register,
1167c478bd9Sstevel@tonic-gate 	swap_unregister,
1177c478bd9Sstevel@tonic-gate 	swap_getinfo,
1187c478bd9Sstevel@tonic-gate 	swap_suspend,
1197c478bd9Sstevel@tonic-gate 	swap_resume,
1207c478bd9Sstevel@tonic-gate 	swap_offline,
1217c478bd9Sstevel@tonic-gate 	swap_online,
1227c478bd9Sstevel@tonic-gate 	swap_remove,
1237c478bd9Sstevel@tonic-gate 	NULL,
1247c478bd9Sstevel@tonic-gate 	NULL,
1257c478bd9Sstevel@tonic-gate 	NULL
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate struct rcm_mod_ops *
rcm_mod_init()1297c478bd9Sstevel@tonic-gate rcm_mod_init()
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	return (&swap_ops);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate const char *
rcm_mod_info()1357c478bd9Sstevel@tonic-gate rcm_mod_info()
1367c478bd9Sstevel@tonic-gate {
137648495d6Svikram 	return ("RCM Swap module 1.5");
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
rcm_mod_fini()1417c478bd9Sstevel@tonic-gate rcm_mod_fini()
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	free_cache();
1447c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&cache_lock);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static int
swap_register(rcm_handle_t * hdl)1507c478bd9Sstevel@tonic-gate swap_register(rcm_handle_t *hdl)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	return (update_cache(hdl));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static int
swap_unregister(rcm_handle_t * hdl)1567c478bd9Sstevel@tonic-gate swap_unregister(rcm_handle_t *hdl)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1617c478bd9Sstevel@tonic-gate 	while ((sf = cache) != NULL) {
1627c478bd9Sstevel@tonic-gate 		cache = cache->next;
1637c478bd9Sstevel@tonic-gate 		(void) rcm_unregister_interest(hdl, sf->path, 0);
1647c478bd9Sstevel@tonic-gate 		swap_file_free(sf);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1727c478bd9Sstevel@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)1737c478bd9Sstevel@tonic-gate swap_getinfo(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
1747c478bd9Sstevel@tonic-gate     char **infostr, char **errstr, nvlist_t *props, rcm_info_t **dependent)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && infostr != NULL);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1797c478bd9Sstevel@tonic-gate 	if (cache_lookup(rsrcname) == NULL) {
1807c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "unknown resource: %s\n",
1817c478bd9Sstevel@tonic-gate 		    rsrcname);
1827c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
1837c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1867c478bd9Sstevel@tonic-gate 	(void) alloc_usage(infostr);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Remove swap space to maintain availability of anonymous pages
1937c478bd9Sstevel@tonic-gate  * during device suspension. Swap will be reconfigured upon resume.
1947c478bd9Sstevel@tonic-gate  * Fail if operation will unconfigure dump device.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1977c478bd9Sstevel@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)1987c478bd9Sstevel@tonic-gate swap_suspend(rcm_handle_t *hdl, char *rsrcname, id_t id, timespec_t *interval,
1997c478bd9Sstevel@tonic-gate     uint_t flags, char **errstr, rcm_info_t **dependent)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
2027c478bd9Sstevel@tonic-gate 	int		rv;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (flags & RCM_QUERY)
2077c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2107c478bd9Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2117c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2127c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	rv = swap_delete(sf, errstr);
2167c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (rv);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2227c478bd9Sstevel@tonic-gate static int
swap_resume(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2237c478bd9Sstevel@tonic-gate swap_resume(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2247c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
2277c478bd9Sstevel@tonic-gate 	int		rv;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2327c478bd9Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2337c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2347c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	rv = swap_add(sf, errstr);
2387c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	return (rv);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * By default, reject offline request. If forced, attempt to
2457c478bd9Sstevel@tonic-gate  * delete swap. Fail if operation will unconfigure dump device.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2487c478bd9Sstevel@tonic-gate static int
swap_offline(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2497c478bd9Sstevel@tonic-gate swap_offline(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2507c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
2537c478bd9Sstevel@tonic-gate 	int		rv;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((flags & RCM_FORCE) && (flags & RCM_QUERY))
2587c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2617c478bd9Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2627c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2637c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (flags & RCM_FORCE) {
2677c478bd9Sstevel@tonic-gate 		rv = swap_delete(sf, errstr);
2687c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2697c478bd9Sstevel@tonic-gate 		return (rv);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	/* default reject */
2727c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2737c478bd9Sstevel@tonic-gate 	(void) alloc_usage(errstr);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	return (RCM_FAILURE);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2797c478bd9Sstevel@tonic-gate static int
swap_online(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2807c478bd9Sstevel@tonic-gate swap_online(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2817c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
2847c478bd9Sstevel@tonic-gate 	int		rv;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2897c478bd9Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
2907c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2917c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	rv = swap_add(sf, errstr);
2957c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	return (rv);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3017c478bd9Sstevel@tonic-gate static int
swap_remove(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)3027c478bd9Sstevel@tonic-gate swap_remove(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
3037c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
3107c478bd9Sstevel@tonic-gate 	if ((sf = cache_lookup(rsrcname)) == NULL) {
3117c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
3127c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	/* RCM framework handles unregistration */
3157c478bd9Sstevel@tonic-gate 	cache_remove(sf);
3167c478bd9Sstevel@tonic-gate 	swap_file_free(sf);
3177c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Delete all swap areas for swap file.
324*bbf21555SRichard Lowe  * Invoke swap(8) instead of swapctl(2) to
3257c478bd9Sstevel@tonic-gate  * handle relocation of dump device.
3267c478bd9Sstevel@tonic-gate  * If dump device is configured, fail if
3277c478bd9Sstevel@tonic-gate  * unable to relocate dump.
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate static int
swap_delete(swap_file_t * sf,char ** errstr)3327c478bd9Sstevel@tonic-gate swap_delete(swap_file_t *sf, char **errstr)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	swap_area_t	*sa;
3357c478bd9Sstevel@tonic-gate 	char		cmd[sizeof (SWAP_DELETE) + MAXPATHLEN +
3367c478bd9Sstevel@tonic-gate 			    MAXOFFSET_STRLEN];
3377c478bd9Sstevel@tonic-gate 	char		dumpdev[MAXPATHLEN];
3387c478bd9Sstevel@tonic-gate 	int		have_dump = 1;
3397c478bd9Sstevel@tonic-gate 	int		stat;
3407c478bd9Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (get_dumpdev(dumpdev) == 0 && dumpdev[0] == '\0')
3437c478bd9Sstevel@tonic-gate 		have_dump = 0;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
346*bbf21555SRichard Lowe 		/* swap(8) is not idempotent */
3477c478bd9Sstevel@tonic-gate 		if (sa->cache_flags & SWAP_CACHE_OFFLINED) {
3487c478bd9Sstevel@tonic-gate 			continue;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd), SWAP_DELETE, sf->path,
3527c478bd9Sstevel@tonic-gate 		    sa->start);
3537c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3547c478bd9Sstevel@tonic-gate 		if ((stat = rcm_exec_cmd(cmd)) != 0) {
3557c478bd9Sstevel@tonic-gate 			log_cmd_status(stat);
3567c478bd9Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to delete swap"));
3577c478bd9Sstevel@tonic-gate 			rv = RCM_FAILURE;
3587c478bd9Sstevel@tonic-gate 			goto out;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		sa->cache_flags |= SWAP_CACHE_OFFLINED;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * Fail on removal of dump device.
3647c478bd9Sstevel@tonic-gate 		 */
3657c478bd9Sstevel@tonic-gate 		if (have_dump == 0)
3667c478bd9Sstevel@tonic-gate 			continue;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		if (get_dumpdev(dumpdev) != 0) {
3697c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_WARNING, "unable to "
3707c478bd9Sstevel@tonic-gate 			    "check for removal of dump device\n");
3717c478bd9Sstevel@tonic-gate 		} else if (dumpdev[0] == '\0') {
3727c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "removed dump: "
3737c478bd9Sstevel@tonic-gate 			    "attempting recovery\n");
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 			/*
3767c478bd9Sstevel@tonic-gate 			 * Restore dump
3777c478bd9Sstevel@tonic-gate 			 */
3787c478bd9Sstevel@tonic-gate 			(void) snprintf(cmd, sizeof (cmd), SWAP_ADD,
3797c478bd9Sstevel@tonic-gate 				sf->path, sa->start, sa->len);
3807c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3817c478bd9Sstevel@tonic-gate 			if ((stat = rcm_exec_cmd(cmd)) != 0) {
3827c478bd9Sstevel@tonic-gate 				log_cmd_status(stat);
3837c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR,
3847c478bd9Sstevel@tonic-gate 				    "failed to restore dump\n");
3857c478bd9Sstevel@tonic-gate 			} else {
3867c478bd9Sstevel@tonic-gate 				sa->cache_flags &= ~SWAP_CACHE_OFFLINED;
3877c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_DEBUG, "dump restored\n");
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to relocate dump"));
3907c478bd9Sstevel@tonic-gate 			rv = RCM_FAILURE;
3917c478bd9Sstevel@tonic-gate 			goto out;
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	sf->cache_flags |= SWAP_CACHE_OFFLINED;
3957c478bd9Sstevel@tonic-gate out:
3967c478bd9Sstevel@tonic-gate 	return (rv);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
400*bbf21555SRichard Lowe  * Invoke swap(8) to add each registered swap area.
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate static int
swap_add(swap_file_t * sf,char ** errstr)4057c478bd9Sstevel@tonic-gate swap_add(swap_file_t *sf, char **errstr)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	swap_area_t	*sa;
4087c478bd9Sstevel@tonic-gate 	char		cmd[sizeof (SWAP_ADD) + MAXPATHLEN +
4097c478bd9Sstevel@tonic-gate 			    (2 * MAXOFFSET_STRLEN)];
4107c478bd9Sstevel@tonic-gate 	int		stat;
4117c478bd9Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
414*bbf21555SRichard Lowe 		/* swap(8) is not idempotent */
4157c478bd9Sstevel@tonic-gate 		if (!(sa->cache_flags & SWAP_CACHE_OFFLINED)) {
4167c478bd9Sstevel@tonic-gate 			continue;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
4207c478bd9Sstevel@tonic-gate 			SWAP_ADD, sf->path, sa->start, sa->len);
4217c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_TRACE1, "%s\n", cmd);
4227c478bd9Sstevel@tonic-gate 		if ((stat = rcm_exec_cmd(cmd)) != 0) {
4237c478bd9Sstevel@tonic-gate 			log_cmd_status(stat);
4247c478bd9Sstevel@tonic-gate 			*errstr = strdup(gettext("unable to add swap"));
4257c478bd9Sstevel@tonic-gate 			rv = RCM_FAILURE;
4267c478bd9Sstevel@tonic-gate 			break;
4277c478bd9Sstevel@tonic-gate 		} else {
4287c478bd9Sstevel@tonic-gate 			sa->cache_flags &= ~SWAP_CACHE_OFFLINED;
4297c478bd9Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_OFFLINED;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	return (rv);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate static int
update_cache(rcm_handle_t * hdl)4377c478bd9Sstevel@tonic-gate update_cache(rcm_handle_t *hdl)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	swaptbl_t	*swt;
4407c478bd9Sstevel@tonic-gate 	swap_file_t	*sf, *stale_sf;
4417c478bd9Sstevel@tonic-gate 	swap_area_t	*sa, *stale_sa;
4427c478bd9Sstevel@tonic-gate 	int		i;
4437c478bd9Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if ((swt = sys_swaptbl()) == NULL) {
4467c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "failed to read "
4477c478bd9Sstevel@tonic-gate 		    "current swap configuration\n");
4487c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * cache pass 1 - mark everyone stale
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 	for (sf = cache; sf != NULL; sf = sf->next) {
4577c478bd9Sstevel@tonic-gate 		sf->cache_flags |= SWAP_CACHE_STALE;
4587c478bd9Sstevel@tonic-gate 		for (sa = sf->areas; sa != NULL; sa = sa->next) {
4597c478bd9Sstevel@tonic-gate 			sa->cache_flags |= SWAP_CACHE_STALE;
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * add new entries
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	for (i = 0; i < swt->swt_n; i++) {
4677c478bd9Sstevel@tonic-gate 		if (swt->swt_ent[i].ste_flags & (ST_INDEL|ST_DOINGDEL)) {
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		/*
4727c478bd9Sstevel@tonic-gate 		 * assure swap_file_t
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		if ((sf = cache_lookup(swt->swt_ent[i].ste_path)) == NULL) {
4757c478bd9Sstevel@tonic-gate 			if ((sf = swap_file_alloc(swt->swt_ent[i].ste_path)) ==
4767c478bd9Sstevel@tonic-gate 			    NULL) {
4777c478bd9Sstevel@tonic-gate 				free(swt);
4787c478bd9Sstevel@tonic-gate 				return (RCM_FAILURE);
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 			sf->cache_flags |= SWAP_CACHE_NEW;
4817c478bd9Sstevel@tonic-gate 			cache_insert(sf);
4827c478bd9Sstevel@tonic-gate 		} else {
4837c478bd9Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_STALE;
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		/*
4877c478bd9Sstevel@tonic-gate 		 * assure swap_area_t
4887c478bd9Sstevel@tonic-gate 		 */
4897c478bd9Sstevel@tonic-gate 		if ((sa = swap_area_lookup(sf, &swt->swt_ent[i])) == NULL) {
4907c478bd9Sstevel@tonic-gate 			if ((sa = swap_area_alloc(&swt->swt_ent[i])) == NULL) {
4917c478bd9Sstevel@tonic-gate 				free(swt);
4927c478bd9Sstevel@tonic-gate 				return (RCM_FAILURE);
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 			swap_area_add(sf, sa);
4957c478bd9Sstevel@tonic-gate 		} else {
4967c478bd9Sstevel@tonic-gate 			sa->cache_flags &= ~SWAP_CACHE_STALE;
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	free(swt);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * cache pass 2
5047c478bd9Sstevel@tonic-gate 	 *
5057c478bd9Sstevel@tonic-gate 	 * swap_file_t - skip offlined, register new, unregister/remove stale
5067c478bd9Sstevel@tonic-gate 	 * swap_area_t - skip offlined, remove stale
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	sf = cache;
5097c478bd9Sstevel@tonic-gate 	while (sf != NULL) {
5107c478bd9Sstevel@tonic-gate 		sa = sf->areas;
5117c478bd9Sstevel@tonic-gate 		while (sa != NULL) {
5127c478bd9Sstevel@tonic-gate 			if (sa->cache_flags & SWAP_CACHE_OFFLINED) {
5137c478bd9Sstevel@tonic-gate 				sa->cache_flags &= ~SWAP_CACHE_STALE;
5147c478bd9Sstevel@tonic-gate 				sa = sa->next;
5157c478bd9Sstevel@tonic-gate 				continue;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 			if (sa->cache_flags & SWAP_CACHE_STALE) {
5187c478bd9Sstevel@tonic-gate 				stale_sa = sa;
5197c478bd9Sstevel@tonic-gate 				sa = sa->next;
5207c478bd9Sstevel@tonic-gate 				swap_area_remove(sf, stale_sa);
5217c478bd9Sstevel@tonic-gate 				free(stale_sa);
5227c478bd9Sstevel@tonic-gate 				continue;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 			sa = sa->next;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		if (sf->cache_flags & SWAP_CACHE_OFFLINED) {
5287c478bd9Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_STALE;
5297c478bd9Sstevel@tonic-gate 			sf = sf->next;
5307c478bd9Sstevel@tonic-gate 			continue;
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		if (sf->cache_flags & SWAP_CACHE_STALE) {
5347c478bd9Sstevel@tonic-gate 			if (rcm_unregister_interest(hdl, sf->path, 0) !=
5357c478bd9Sstevel@tonic-gate 			    RCM_SUCCESS) {
5367c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, "failed to register "
5377c478bd9Sstevel@tonic-gate 				    "%s\n", sf->path);
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 			stale_sf = sf;
5407c478bd9Sstevel@tonic-gate 			sf = sf->next;
5417c478bd9Sstevel@tonic-gate 			cache_remove(stale_sf);
5427c478bd9Sstevel@tonic-gate 			swap_file_free(stale_sf);
5437c478bd9Sstevel@tonic-gate 			continue;
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (!(sf->cache_flags & SWAP_CACHE_NEW)) {
5477c478bd9Sstevel@tonic-gate 			sf = sf->next;
5487c478bd9Sstevel@tonic-gate 			continue;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		if (rcm_register_interest(hdl, sf->path, 0, NULL) !=
5527c478bd9Sstevel@tonic-gate 		    RCM_SUCCESS) {
5537c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "failed to register %s\n",
5547c478bd9Sstevel@tonic-gate 			    sf->path);
5557c478bd9Sstevel@tonic-gate 			rv = RCM_FAILURE;
5567c478bd9Sstevel@tonic-gate 		} else {
5577c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "registered %s\n",
5587c478bd9Sstevel@tonic-gate 			    sf->path);
5597c478bd9Sstevel@tonic-gate 			sf->cache_flags &= ~SWAP_CACHE_NEW;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		sf = sf->next;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	return (rv);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * Returns system swap table.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static swaptbl_t *
sys_swaptbl()5727c478bd9Sstevel@tonic-gate sys_swaptbl()
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	swaptbl_t	*swt;
5757c478bd9Sstevel@tonic-gate 	char		*cp;
5767c478bd9Sstevel@tonic-gate 	int		i, n;
5777c478bd9Sstevel@tonic-gate 	size_t		tbl_size;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if ((n = swapctl(SC_GETNSWP, NULL)) == -1)
5807c478bd9Sstevel@tonic-gate 		return (NULL);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	tbl_size = sizeof (int) + n * sizeof (swapent_t) + n * MAXPATHLEN;
5837c478bd9Sstevel@tonic-gate 	if ((swt = (swaptbl_t *)malloc(tbl_size)) == NULL)
5847c478bd9Sstevel@tonic-gate 		return (NULL);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	swt->swt_n = n;
5877c478bd9Sstevel@tonic-gate 	cp = (char *)swt + (sizeof (int) + n * sizeof (swapent_t));
5887c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
5897c478bd9Sstevel@tonic-gate 		swt->swt_ent[i].ste_path = cp;
5907c478bd9Sstevel@tonic-gate 		cp += MAXPATHLEN;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if ((n = swapctl(SC_LIST, swt)) == -1) {
5947c478bd9Sstevel@tonic-gate 		free(swt);
5957c478bd9Sstevel@tonic-gate 		return (NULL);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (n != swt->swt_n) {
5997c478bd9Sstevel@tonic-gate 		/* mismatch, try again */
6007c478bd9Sstevel@tonic-gate 		free(swt);
6017c478bd9Sstevel@tonic-gate 		return (sys_swaptbl());
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	return (swt);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate static int
get_dumpdev(char dumpdev[])6087c478bd9Sstevel@tonic-gate get_dumpdev(char dumpdev[])
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int	fd;
6117c478bd9Sstevel@tonic-gate 	int	rv = 0;
6127c478bd9Sstevel@tonic-gate 	char	*err;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) == -1) {
6157c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "failed to open /dev/dump\n");
6167c478bd9Sstevel@tonic-gate 		return (-1);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
6207c478bd9Sstevel@tonic-gate 		if (errno == ENODEV) {
6217c478bd9Sstevel@tonic-gate 			dumpdev[0] = '\0';
6227c478bd9Sstevel@tonic-gate 		} else {
6237c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
6247c478bd9Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
6257c478bd9Sstevel@tonic-gate 			rv = -1;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	(void) close(fd);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	return (rv);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate static void
free_cache(void)6347c478bd9Sstevel@tonic-gate free_cache(void)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
6397c478bd9Sstevel@tonic-gate 	while ((sf = cache) != NULL) {
6407c478bd9Sstevel@tonic-gate 		cache = cache->next;
6417c478bd9Sstevel@tonic-gate 		swap_file_free(sf);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate static void
swap_file_free(swap_file_t * sf)6517c478bd9Sstevel@tonic-gate swap_file_free(swap_file_t *sf)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	swap_area_t	*sa;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	assert(sf != NULL);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	while ((sa = sf->areas) != NULL) {
6587c478bd9Sstevel@tonic-gate 		sf->areas = sf->areas->next;
6597c478bd9Sstevel@tonic-gate 		free(sa);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	free(sf);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate static void
cache_insert(swap_file_t * ent)6687c478bd9Sstevel@tonic-gate cache_insert(swap_file_t *ent)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	ent->next = cache;
6717c478bd9Sstevel@tonic-gate 	if (ent->next)
6727c478bd9Sstevel@tonic-gate 		ent->next->prev = ent;
6737c478bd9Sstevel@tonic-gate 	ent->prev = NULL;
6747c478bd9Sstevel@tonic-gate 	cache = ent;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate static swap_file_t *
cache_lookup(char * rsrc)6817c478bd9Sstevel@tonic-gate cache_lookup(char *rsrc)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	for (sf = cache; sf != NULL; sf = sf->next) {
6867c478bd9Sstevel@tonic-gate 		if (strcmp(rsrc, sf->path) == 0) {
6877c478bd9Sstevel@tonic-gate 			return (sf);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	return (NULL);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate static void
cache_remove(swap_file_t * ent)6977c478bd9Sstevel@tonic-gate cache_remove(swap_file_t *ent)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	assert(ent != NULL);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (ent->next != NULL) {
7027c478bd9Sstevel@tonic-gate 		ent->next->prev = ent->prev;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	if (ent->prev != NULL) {
7057c478bd9Sstevel@tonic-gate 		ent->prev->next = ent->next;
7067c478bd9Sstevel@tonic-gate 	} else {
7077c478bd9Sstevel@tonic-gate 		cache = ent->next;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	ent->next = NULL;
7107c478bd9Sstevel@tonic-gate 	ent->prev = NULL;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
7157c478bd9Sstevel@tonic-gate  */
7167c478bd9Sstevel@tonic-gate static void
swap_area_add(swap_file_t * sf,swap_area_t * sa)7177c478bd9Sstevel@tonic-gate swap_area_add(swap_file_t *sf, swap_area_t *sa)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	sa->next = sf->areas;
7207c478bd9Sstevel@tonic-gate 	if (sa->next)
7217c478bd9Sstevel@tonic-gate 		sa->next->prev = sa;
7227c478bd9Sstevel@tonic-gate 	sa->prev = NULL;
7237c478bd9Sstevel@tonic-gate 	sf->areas = sa;
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
7287c478bd9Sstevel@tonic-gate  */
7297c478bd9Sstevel@tonic-gate static void
swap_area_remove(swap_file_t * sf,swap_area_t * ent)7307c478bd9Sstevel@tonic-gate swap_area_remove(swap_file_t *sf, swap_area_t *ent)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	assert(sf != NULL && ent != NULL);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (ent->next != NULL) {
7357c478bd9Sstevel@tonic-gate 		ent->next->prev = ent->prev;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if (ent->prev != NULL) {
7387c478bd9Sstevel@tonic-gate 		ent->prev->next = ent->next;
7397c478bd9Sstevel@tonic-gate 	} else {
7407c478bd9Sstevel@tonic-gate 		sf->areas = ent->next;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	ent->next = NULL;
7437c478bd9Sstevel@tonic-gate 	ent->prev = NULL;
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate static swap_file_t *
swap_file_alloc(char * rsrc)7477c478bd9Sstevel@tonic-gate swap_file_alloc(char *rsrc)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	swap_file_t	*sf;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if ((sf = calloc(1, sizeof (*sf))) == NULL) {
7527c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
7537c478bd9Sstevel@tonic-gate 		return (NULL);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	(void) strlcpy(sf->path, rsrc, sizeof (sf->path));
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (sf);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static swap_area_t *
swap_area_alloc(swapent_t * swt_ent)7617c478bd9Sstevel@tonic-gate swap_area_alloc(swapent_t *swt_ent)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	swap_area_t	*sa;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if ((sa = calloc(1, sizeof (*sa))) == NULL) {
7667c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
7677c478bd9Sstevel@tonic-gate 		return (NULL);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	sa->start = swt_ent->ste_start;
7707c478bd9Sstevel@tonic-gate 	sa->len = swt_ent->ste_length;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	return (sa);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate static swap_area_t *
swap_area_lookup(swap_file_t * sf,swapent_t * swt_ent)7797c478bd9Sstevel@tonic-gate swap_area_lookup(swap_file_t *sf, swapent_t *swt_ent)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	swap_area_t	*sa;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	assert(sf != NULL && swt_ent != NULL);
7847c478bd9Sstevel@tonic-gate 	assert(strcmp(sf->path, swt_ent->ste_path) == 0);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	for (sa = sf->areas; sa != NULL; sa = sa->next) {
7877c478bd9Sstevel@tonic-gate 		if (sa->start == swt_ent->ste_start &&
7887c478bd9Sstevel@tonic-gate 		    sa->len == swt_ent->ste_length) {
7897c478bd9Sstevel@tonic-gate 			return (sa);
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 	return (NULL);
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate /*
7967c478bd9Sstevel@tonic-gate  * All-purpose usage string.
7977c478bd9Sstevel@tonic-gate  */
7987c478bd9Sstevel@tonic-gate static int
alloc_usage(char ** cpp)7997c478bd9Sstevel@tonic-gate alloc_usage(char **cpp)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	if ((*cpp = strdup(gettext("swap area"))) == NULL) {
8027c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "strdup failure\n");
8037c478bd9Sstevel@tonic-gate 		return (-1);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 	return (0);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate static void
log_cmd_status(int stat)8097c478bd9Sstevel@tonic-gate log_cmd_status(int stat)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	char	*err;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (stat == -1) {
8147c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait: %s\n",
8157c478bd9Sstevel@tonic-gate 		    ((err = strerror(errno)) == NULL) ? "" : err);
8167c478bd9Sstevel@tonic-gate 	} else if (WIFEXITED(stat)) {
8177c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "exit status: %d\n",
8187c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(stat));
8197c478bd9Sstevel@tonic-gate 	} else {
8207c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait status: %d\n", stat);
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate }
823