xref: /onnv-gate/usr/src/cmd/rcm_daemon/common/dump_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.
23*7202Svikram  * 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 for managing dump device during dynamic
300Sstevel@tonic-gate  * reconfiguration.
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 <string.h>
360Sstevel@tonic-gate #include <thread.h>
370Sstevel@tonic-gate #include <synch.h>
380Sstevel@tonic-gate #include <assert.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <libintl.h>
410Sstevel@tonic-gate #include <sys/dumpadm.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <sys/wait.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include "rcm_module.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* cache flags */
490Sstevel@tonic-gate #define	DUMP_CACHE_NEW		0x01
500Sstevel@tonic-gate #define	DUMP_CACHE_STALE	0x02
510Sstevel@tonic-gate #define	DUMP_CACHE_OFFLINED	0x04
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	DUMPADM			"/usr/sbin/dumpadm -d "
540Sstevel@tonic-gate #define	DUMPADM_SWAP		DUMPADM"swap"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate typedef struct dump_conf {
570Sstevel@tonic-gate 	char		device[MAXPATHLEN];
580Sstevel@tonic-gate 	int		conf_flags;		/* defs in <sys/dumpadm.h> */
590Sstevel@tonic-gate 	int		cache_flags;
600Sstevel@tonic-gate 	struct dump_conf *next;
610Sstevel@tonic-gate 	struct dump_conf *prev;
620Sstevel@tonic-gate } dump_conf_t;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * Registration cache.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * N.B.	Although we currently only support a single
680Sstevel@tonic-gate  *	dump device, the cache is multi-entry since there
690Sstevel@tonic-gate  *	may be multiple outstanding registrations.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static dump_conf_t	*cache;
720Sstevel@tonic-gate static mutex_t		cache_lock;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int		dump_register(rcm_handle_t *);
750Sstevel@tonic-gate static int		dump_unregister(rcm_handle_t *);
760Sstevel@tonic-gate static int		dump_getinfo(rcm_handle_t *, char *, id_t, uint_t,
770Sstevel@tonic-gate 			    char **, char **, nvlist_t *, rcm_info_t **);
780Sstevel@tonic-gate static int		dump_suspend(rcm_handle_t *, char *, id_t, timespec_t *,
790Sstevel@tonic-gate 			    uint_t, char **, rcm_info_t **);
800Sstevel@tonic-gate static int		dump_resume(rcm_handle_t *, char *, id_t, uint_t,
810Sstevel@tonic-gate 			    char **, rcm_info_t **);
820Sstevel@tonic-gate static int		dump_offline(rcm_handle_t *, char *, id_t, uint_t,
830Sstevel@tonic-gate 			    char **, rcm_info_t **);
840Sstevel@tonic-gate static int		dump_online(rcm_handle_t *, char *, id_t, uint_t,
850Sstevel@tonic-gate 			    char **, rcm_info_t **);
860Sstevel@tonic-gate static int		dump_remove(rcm_handle_t *, char *, id_t, uint_t,
870Sstevel@tonic-gate 			    char **, rcm_info_t **);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int		alloc_usage(char **, int);
900Sstevel@tonic-gate static void		cache_insert(dump_conf_t *);
910Sstevel@tonic-gate static dump_conf_t	*cache_lookup(char *);
920Sstevel@tonic-gate static void		cache_remove(dump_conf_t *);
930Sstevel@tonic-gate static dump_conf_t	*dump_conf_alloc(void);
940Sstevel@tonic-gate static int		dump_configure(dump_conf_t *, char **);
950Sstevel@tonic-gate static int		dump_relocate(dump_conf_t *, char **);
960Sstevel@tonic-gate static void		free_cache(void);
970Sstevel@tonic-gate static void		log_cmd_status(int);
980Sstevel@tonic-gate static int		update_cache(rcm_handle_t *);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static struct rcm_mod_ops dump_ops =
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	RCM_MOD_OPS_VERSION,
1030Sstevel@tonic-gate 	dump_register,
1040Sstevel@tonic-gate 	dump_unregister,
1050Sstevel@tonic-gate 	dump_getinfo,
1060Sstevel@tonic-gate 	dump_suspend,
1070Sstevel@tonic-gate 	dump_resume,
1080Sstevel@tonic-gate 	dump_offline,
1090Sstevel@tonic-gate 	dump_online,
1100Sstevel@tonic-gate 	dump_remove,
1110Sstevel@tonic-gate 	NULL,
1120Sstevel@tonic-gate 	NULL,
1130Sstevel@tonic-gate 	NULL
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate struct rcm_mod_ops *
rcm_mod_init()1170Sstevel@tonic-gate rcm_mod_init()
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	return (&dump_ops);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate const char *
rcm_mod_info()1230Sstevel@tonic-gate rcm_mod_info()
1240Sstevel@tonic-gate {
125*7202Svikram 	return ("RCM Dump module 1.3");
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate int
rcm_mod_fini()1290Sstevel@tonic-gate rcm_mod_fini()
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	free_cache();
1320Sstevel@tonic-gate 	(void) mutex_destroy(&cache_lock);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (RCM_SUCCESS);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int
dump_register(rcm_handle_t * hdl)1380Sstevel@tonic-gate dump_register(rcm_handle_t *hdl)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	return (update_cache(hdl));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static int
dump_unregister(rcm_handle_t * hdl)1440Sstevel@tonic-gate dump_unregister(rcm_handle_t *hdl)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	dump_conf_t	*dc;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1490Sstevel@tonic-gate 	while ((dc = cache) != NULL) {
1500Sstevel@tonic-gate 		cache = cache->next;
1510Sstevel@tonic-gate 		(void) rcm_unregister_interest(hdl, dc->device, 0);
1520Sstevel@tonic-gate 		free(dc);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (RCM_SUCCESS);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*ARGSUSED*/
1600Sstevel@tonic-gate static int
dump_getinfo(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** infostr,char ** errstr,nvlist_t * props,rcm_info_t ** dependent)1610Sstevel@tonic-gate dump_getinfo(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
1620Sstevel@tonic-gate     char **infostr, char **errstr, nvlist_t *props, rcm_info_t **dependent)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	dump_conf_t	*dc;
1650Sstevel@tonic-gate 	int		conf_flags;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	assert(rsrcname != NULL && infostr != NULL);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1700Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
1710Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
1720Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "unknown resource: %s\n",
1730Sstevel@tonic-gate 		    rsrcname);
1740Sstevel@tonic-gate 		return (RCM_FAILURE);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	conf_flags = dc->conf_flags;
1770Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return ((alloc_usage(infostr, conf_flags) == 0) ?
1800Sstevel@tonic-gate 	    RCM_SUCCESS : RCM_FAILURE);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Relocate dump device to maintain availability during suspension.
1850Sstevel@tonic-gate  * Fail request if unable to relocate.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate /*ARGSUSED*/
1880Sstevel@tonic-gate static int
dump_suspend(rcm_handle_t * hdl,char * rsrcname,id_t id,timespec_t * interval,uint_t flags,char ** errstr,rcm_info_t ** dependent)1890Sstevel@tonic-gate dump_suspend(rcm_handle_t *hdl, char *rsrcname, id_t id, timespec_t *interval,
1900Sstevel@tonic-gate     uint_t flags, char **errstr, rcm_info_t **dependent)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	dump_conf_t	*dc;
1930Sstevel@tonic-gate 	int		rv;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (flags & RCM_QUERY)
1980Sstevel@tonic-gate 		return (RCM_SUCCESS);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2010Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2020Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2030Sstevel@tonic-gate 		return (RCM_SUCCESS);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	rv = dump_relocate(dc, errstr);
2070Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	return (rv);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*ARGSUSED*/
2130Sstevel@tonic-gate static int
dump_resume(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2140Sstevel@tonic-gate dump_resume(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2150Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	dump_conf_t	*dc;
2180Sstevel@tonic-gate 	int		rv;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2230Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2240Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2250Sstevel@tonic-gate 		return (RCM_SUCCESS);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	rv = dump_configure(dc, errstr);
2290Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	return (rv);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * By default, reject offline. If offline request is
2360Sstevel@tonic-gate  * forced, attempt to relocate the dump device.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate /*ARGSUSED*/
2390Sstevel@tonic-gate static int
dump_offline(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2400Sstevel@tonic-gate dump_offline(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2410Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	dump_conf_t	*dc;
2440Sstevel@tonic-gate 	int		conf_flags;
2450Sstevel@tonic-gate 	int		rv;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if ((flags & RCM_FORCE) && (flags & RCM_QUERY))
2500Sstevel@tonic-gate 		return (RCM_SUCCESS);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2530Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2540Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2550Sstevel@tonic-gate 		return (RCM_SUCCESS);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (flags & RCM_FORCE) {
2590Sstevel@tonic-gate 		rv = dump_relocate(dc, errstr);
2600Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2610Sstevel@tonic-gate 		return (rv);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* default */
2650Sstevel@tonic-gate 	conf_flags = dc->conf_flags;
2660Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2670Sstevel@tonic-gate 	(void) alloc_usage(errstr, conf_flags);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	return (RCM_FAILURE);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*ARGSUSED*/
2730Sstevel@tonic-gate static int
dump_online(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2740Sstevel@tonic-gate dump_online(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2750Sstevel@tonic-gate     char  **errstr, rcm_info_t **dependent)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	dump_conf_t	*dc;
2780Sstevel@tonic-gate 	int		rv;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2830Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2840Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2850Sstevel@tonic-gate 		return (RCM_SUCCESS);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	rv = dump_configure(dc, errstr);
2890Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	return (rv);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*ARGSUSED*/
2950Sstevel@tonic-gate static int
dump_remove(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2960Sstevel@tonic-gate dump_remove(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2970Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	dump_conf_t	*dc;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
3040Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
3050Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
3060Sstevel@tonic-gate 		return (RCM_SUCCESS);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	cache_remove(dc);
3090Sstevel@tonic-gate 	free(dc);
3100Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (RCM_SUCCESS);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * For dedicated dump devices, invoke dumpadm(1M)
3170Sstevel@tonic-gate  * to relocate dump to swap. For dump device on
3180Sstevel@tonic-gate  * swap, this is a no-op as the RCM swap module
3190Sstevel@tonic-gate  * will relocate by invoking swap(1M).
3200Sstevel@tonic-gate  *
3210Sstevel@tonic-gate  * Call with cache_lock held.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate static int
dump_relocate(dump_conf_t * dc,char ** errstr)3240Sstevel@tonic-gate dump_relocate(dump_conf_t *dc, char **errstr)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	int		stat;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * This state may get out of sync for a dump device on swap,
3300Sstevel@tonic-gate 	 * since we will will not know if the swap module succeeds.
3310Sstevel@tonic-gate 	 * Worst case is we end up invoking dumpadm to configure
3320Sstevel@tonic-gate 	 * the same device during a rollback.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	dc->cache_flags |= DUMP_CACHE_OFFLINED;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/* RCM swap module will handle non-dedicated */
3370Sstevel@tonic-gate 	if (!(dc->conf_flags & DUMP_EXCL))
3380Sstevel@tonic-gate 		return (RCM_SUCCESS);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "%s\n", DUMPADM_SWAP);
3410Sstevel@tonic-gate 	if ((stat = rcm_exec_cmd(DUMPADM_SWAP)) != 0) {
3420Sstevel@tonic-gate 		log_cmd_status(stat);
3430Sstevel@tonic-gate 		*errstr = strdup(gettext("unable to relocate dump device"));
3440Sstevel@tonic-gate 		dc->cache_flags &= ~DUMP_CACHE_OFFLINED;
3450Sstevel@tonic-gate 		return (RCM_FAILURE);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	return (RCM_SUCCESS);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * (Re)Configure dump device.
3530Sstevel@tonic-gate  * Call with cache_lock held.
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate static int
dump_configure(dump_conf_t * dc,char ** errstr)3560Sstevel@tonic-gate dump_configure(dump_conf_t *dc, char **errstr)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	char		cmd[sizeof (DUMPADM) + MAXPATHLEN];
3590Sstevel@tonic-gate 	int		stat;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	assert(dc != NULL && dc->device != NULL);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/* minor optimization */
3640Sstevel@tonic-gate 	if (!(dc->cache_flags & DUMP_CACHE_OFFLINED))
3650Sstevel@tonic-gate 		return (RCM_SUCCESS);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	(void) snprintf(cmd, sizeof (cmd), "%s%s", DUMPADM, dc->device);
3680Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3690Sstevel@tonic-gate 	if ((stat = rcm_exec_cmd(cmd)) != 0) {
3700Sstevel@tonic-gate 		log_cmd_status(stat);
3710Sstevel@tonic-gate 		*errstr = strdup(gettext("unable to configure dump device"));
3720Sstevel@tonic-gate 		return (RCM_FAILURE);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	dc->cache_flags &= ~DUMP_CACHE_OFFLINED;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	return (RCM_SUCCESS);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * Returns current dump configuration
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate static dump_conf_t *
dump_conf_alloc(void)3830Sstevel@tonic-gate dump_conf_alloc(void)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	dump_conf_t	*dc;
3860Sstevel@tonic-gate 	struct stat	sbuf;
3870Sstevel@tonic-gate 	int		fd;
3880Sstevel@tonic-gate 	char		*err;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if ((dc = calloc(1, sizeof (*dc))) == NULL) {
3910Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
3920Sstevel@tonic-gate 		return (NULL);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) == -1) {
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * Suppress reporting if no logical link.
3980Sstevel@tonic-gate 		 */
3990Sstevel@tonic-gate 		if (stat("/dev/dump", &sbuf) == 0 &&
4000Sstevel@tonic-gate 		    (fd = open("/dev/dump", O_RDONLY)) == -1) {
4010Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
4020Sstevel@tonic-gate 			    "failed to open /dev/dump: %s\n",
4030Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		if (fd == -1) {
4070Sstevel@tonic-gate 			free(dc);
4080Sstevel@tonic-gate 			return (NULL);
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if (ioctl(fd, DIOCGETDEV, dc->device) == -1) {
4130Sstevel@tonic-gate 		if (errno == ENODEV) {
4140Sstevel@tonic-gate 			dc->device[0] = '\0';
4150Sstevel@tonic-gate 		} else {
4160Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
4170Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4180Sstevel@tonic-gate 			(void) close(fd);
4190Sstevel@tonic-gate 			free(dc);
4200Sstevel@tonic-gate 			return (NULL);
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (dc->device[0] != '\0')  {
4250Sstevel@tonic-gate 		if ((dc->conf_flags = ioctl(fd, DIOCGETCONF, 0)) == -1) {
4260Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
4270Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4280Sstevel@tonic-gate 			(void) close(fd);
4290Sstevel@tonic-gate 			free(dc);
4300Sstevel@tonic-gate 			return (NULL);
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	(void) close(fd);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	return (dc);
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 	dump_conf_t	*ent, *curr_dump, *tmp;
4420Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if ((curr_dump = dump_conf_alloc()) == NULL)
4450Sstevel@tonic-gate 		return (RCM_FAILURE);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * pass 1 -  mark all current registrations stale
4510Sstevel@tonic-gate 	 */
4520Sstevel@tonic-gate 	for (ent = cache; ent != NULL; ent = ent->next) {
4530Sstevel@tonic-gate 		ent->cache_flags |= DUMP_CACHE_STALE;
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * update current dump conf
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	if (curr_dump->device[0] == '\0') {
4600Sstevel@tonic-gate 		free(curr_dump);
4610Sstevel@tonic-gate 	} else if ((ent = cache_lookup(curr_dump->device)) != NULL) {
4620Sstevel@tonic-gate 		ent->cache_flags &= ~DUMP_CACHE_STALE;
4630Sstevel@tonic-gate 		ent->conf_flags = curr_dump->conf_flags;
4640Sstevel@tonic-gate 		free(curr_dump);
4650Sstevel@tonic-gate 	} else {
4660Sstevel@tonic-gate 		curr_dump->cache_flags |= DUMP_CACHE_NEW;
4670Sstevel@tonic-gate 		cache_insert(curr_dump);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * pass 2 - register, unregister, or no-op based on cache flags
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	ent = cache;
4740Sstevel@tonic-gate 	while (ent != NULL) {
4750Sstevel@tonic-gate 		if (ent->cache_flags & DUMP_CACHE_OFFLINED) {
4760Sstevel@tonic-gate 			ent = ent->next;
4770Sstevel@tonic-gate 			continue;
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		if (ent->cache_flags & DUMP_CACHE_STALE) {
4810Sstevel@tonic-gate 			if (rcm_unregister_interest(hdl, ent->device, 0) !=
4820Sstevel@tonic-gate 			    RCM_SUCCESS) {
4830Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, "failed to "
4840Sstevel@tonic-gate 				    "unregister %s\n", ent->device);
4850Sstevel@tonic-gate 			}
4860Sstevel@tonic-gate 			tmp = ent;
4870Sstevel@tonic-gate 			ent = ent->next;
4880Sstevel@tonic-gate 			cache_remove(tmp);
4890Sstevel@tonic-gate 			free(tmp);
4900Sstevel@tonic-gate 			continue;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		if (!(ent->cache_flags & DUMP_CACHE_NEW)) {
4940Sstevel@tonic-gate 			ent = ent->next;
4950Sstevel@tonic-gate 			continue;
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		if (rcm_register_interest(hdl, ent->device, 0, NULL) !=
4990Sstevel@tonic-gate 		    RCM_SUCCESS) {
5000Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "failed to register "
5010Sstevel@tonic-gate 			    "%s\n", ent->device);
5020Sstevel@tonic-gate 			rv = RCM_FAILURE;
5030Sstevel@tonic-gate 		} else {
5040Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "registered %s\n",
5050Sstevel@tonic-gate 			    ent->device);
5060Sstevel@tonic-gate 			ent->cache_flags &= ~DUMP_CACHE_NEW;
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 		ent = ent->next;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	return (rv);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * Call with cache_lock held.
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate static dump_conf_t *
cache_lookup(char * rsrc)5190Sstevel@tonic-gate cache_lookup(char *rsrc)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	dump_conf_t	*dc;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	for (dc = cache; dc != NULL; dc = dc->next) {
5240Sstevel@tonic-gate 		if (strcmp(rsrc, dc->device) == 0) {
5250Sstevel@tonic-gate 			return (dc);
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	return (NULL);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*
5320Sstevel@tonic-gate  * Link to front of list.
5330Sstevel@tonic-gate  * Call with cache_lock held.
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate static void
cache_insert(dump_conf_t * ent)5360Sstevel@tonic-gate cache_insert(dump_conf_t *ent)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	ent->next = cache;
5390Sstevel@tonic-gate 	if (ent->next)
5400Sstevel@tonic-gate 		ent->next->prev = ent;
5410Sstevel@tonic-gate 	ent->prev = NULL;
5420Sstevel@tonic-gate 	cache = ent;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * Call with cache_lock held.
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate static void
cache_remove(dump_conf_t * ent)5490Sstevel@tonic-gate cache_remove(dump_conf_t *ent)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	if (ent->next != NULL) {
5520Sstevel@tonic-gate 		ent->next->prev = ent->prev;
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	if (ent->prev != NULL) {
5550Sstevel@tonic-gate 		ent->prev->next = ent->next;
5560Sstevel@tonic-gate 	} else {
5570Sstevel@tonic-gate 		cache = ent->next;
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 	ent->next = NULL;
5600Sstevel@tonic-gate 	ent->prev = NULL;
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate static void
free_cache(void)5640Sstevel@tonic-gate free_cache(void)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	dump_conf_t	*dc;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
5690Sstevel@tonic-gate 	while ((dc = cache) != NULL) {
5700Sstevel@tonic-gate 		cache = cache->next;
5710Sstevel@tonic-gate 		free(dc);
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate static int
alloc_usage(char ** cpp,int conf_flags)5770Sstevel@tonic-gate alloc_usage(char **cpp, int conf_flags)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	/* simplifies message translation */
5800Sstevel@tonic-gate 	if (conf_flags & DUMP_EXCL) {
5810Sstevel@tonic-gate 		*cpp = strdup(gettext("dump device (dedicated)"));
5820Sstevel@tonic-gate 	} else {
5830Sstevel@tonic-gate 		*cpp = strdup(gettext("dump device (swap)"));
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (*cpp == NULL) {
5870Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "strdup failure\n");
5880Sstevel@tonic-gate 		return (-1);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	return (0);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate static void
log_cmd_status(int stat)5940Sstevel@tonic-gate log_cmd_status(int stat)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	char	*err;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (stat == -1) {
5990Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait: %s\n",
6000Sstevel@tonic-gate 		    ((err =  strerror(errno)) == NULL) ? "" : err);
6010Sstevel@tonic-gate 	} else if (WIFEXITED(stat)) {
6020Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "exit status: %d\n",
6030Sstevel@tonic-gate 		    WEXITSTATUS(stat));
6040Sstevel@tonic-gate 	} else {
6050Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait status: %d\n", stat);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate }
608