xref: /illumos-gate/usr/src/cmd/rcm_daemon/common/dump_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.
23648495d6Svikram  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * RCM module for managing dump device during dynamic
287c478bd9Sstevel@tonic-gate  * reconfiguration.
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 <string.h>
347c478bd9Sstevel@tonic-gate #include <thread.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include "rcm_module.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* cache flags */
477c478bd9Sstevel@tonic-gate #define	DUMP_CACHE_NEW		0x01
487c478bd9Sstevel@tonic-gate #define	DUMP_CACHE_STALE	0x02
497c478bd9Sstevel@tonic-gate #define	DUMP_CACHE_OFFLINED	0x04
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	DUMPADM			"/usr/sbin/dumpadm -d "
527c478bd9Sstevel@tonic-gate #define	DUMPADM_SWAP		DUMPADM"swap"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct dump_conf {
557c478bd9Sstevel@tonic-gate 	char		device[MAXPATHLEN];
567c478bd9Sstevel@tonic-gate 	int		conf_flags;		/* defs in <sys/dumpadm.h> */
577c478bd9Sstevel@tonic-gate 	int		cache_flags;
587c478bd9Sstevel@tonic-gate 	struct dump_conf *next;
597c478bd9Sstevel@tonic-gate 	struct dump_conf *prev;
607c478bd9Sstevel@tonic-gate } dump_conf_t;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Registration cache.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * N.B.	Although we currently only support a single
667c478bd9Sstevel@tonic-gate  *	dump device, the cache is multi-entry since there
677c478bd9Sstevel@tonic-gate  *	may be multiple outstanding registrations.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate static dump_conf_t	*cache;
707c478bd9Sstevel@tonic-gate static mutex_t		cache_lock;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int		dump_register(rcm_handle_t *);
737c478bd9Sstevel@tonic-gate static int		dump_unregister(rcm_handle_t *);
747c478bd9Sstevel@tonic-gate static int		dump_getinfo(rcm_handle_t *, char *, id_t, uint_t,
757c478bd9Sstevel@tonic-gate 			    char **, char **, nvlist_t *, rcm_info_t **);
767c478bd9Sstevel@tonic-gate static int		dump_suspend(rcm_handle_t *, char *, id_t, timespec_t *,
777c478bd9Sstevel@tonic-gate 			    uint_t, char **, rcm_info_t **);
787c478bd9Sstevel@tonic-gate static int		dump_resume(rcm_handle_t *, char *, id_t, uint_t,
797c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
807c478bd9Sstevel@tonic-gate static int		dump_offline(rcm_handle_t *, char *, id_t, uint_t,
817c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
827c478bd9Sstevel@tonic-gate static int		dump_online(rcm_handle_t *, char *, id_t, uint_t,
837c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
847c478bd9Sstevel@tonic-gate static int		dump_remove(rcm_handle_t *, char *, id_t, uint_t,
857c478bd9Sstevel@tonic-gate 			    char **, rcm_info_t **);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static int		alloc_usage(char **, int);
887c478bd9Sstevel@tonic-gate static void		cache_insert(dump_conf_t *);
897c478bd9Sstevel@tonic-gate static dump_conf_t	*cache_lookup(char *);
907c478bd9Sstevel@tonic-gate static void		cache_remove(dump_conf_t *);
917c478bd9Sstevel@tonic-gate static dump_conf_t	*dump_conf_alloc(void);
927c478bd9Sstevel@tonic-gate static int		dump_configure(dump_conf_t *, char **);
937c478bd9Sstevel@tonic-gate static int		dump_relocate(dump_conf_t *, char **);
947c478bd9Sstevel@tonic-gate static void		free_cache(void);
957c478bd9Sstevel@tonic-gate static void		log_cmd_status(int);
967c478bd9Sstevel@tonic-gate static int		update_cache(rcm_handle_t *);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static struct rcm_mod_ops dump_ops =
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	RCM_MOD_OPS_VERSION,
1017c478bd9Sstevel@tonic-gate 	dump_register,
1027c478bd9Sstevel@tonic-gate 	dump_unregister,
1037c478bd9Sstevel@tonic-gate 	dump_getinfo,
1047c478bd9Sstevel@tonic-gate 	dump_suspend,
1057c478bd9Sstevel@tonic-gate 	dump_resume,
1067c478bd9Sstevel@tonic-gate 	dump_offline,
1077c478bd9Sstevel@tonic-gate 	dump_online,
1087c478bd9Sstevel@tonic-gate 	dump_remove,
1097c478bd9Sstevel@tonic-gate 	NULL,
1107c478bd9Sstevel@tonic-gate 	NULL,
1117c478bd9Sstevel@tonic-gate 	NULL
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate struct rcm_mod_ops *
rcm_mod_init()1157c478bd9Sstevel@tonic-gate rcm_mod_init()
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	return (&dump_ops);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate const char *
rcm_mod_info()1217c478bd9Sstevel@tonic-gate rcm_mod_info()
1227c478bd9Sstevel@tonic-gate {
123648495d6Svikram 	return ("RCM Dump module 1.3");
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate int
rcm_mod_fini()1277c478bd9Sstevel@tonic-gate rcm_mod_fini()
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	free_cache();
1307c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&cache_lock);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int
dump_register(rcm_handle_t * hdl)1367c478bd9Sstevel@tonic-gate dump_register(rcm_handle_t *hdl)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	return (update_cache(hdl));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static int
dump_unregister(rcm_handle_t * hdl)1427c478bd9Sstevel@tonic-gate dump_unregister(rcm_handle_t *hdl)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1477c478bd9Sstevel@tonic-gate 	while ((dc = cache) != NULL) {
1487c478bd9Sstevel@tonic-gate 		cache = cache->next;
1497c478bd9Sstevel@tonic-gate 		(void) rcm_unregister_interest(hdl, dc->device, 0);
1507c478bd9Sstevel@tonic-gate 		free(dc);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1587c478bd9Sstevel@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)1597c478bd9Sstevel@tonic-gate dump_getinfo(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
1607c478bd9Sstevel@tonic-gate     char **infostr, char **errstr, nvlist_t *props, rcm_info_t **dependent)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
1637c478bd9Sstevel@tonic-gate 	int		conf_flags;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && infostr != NULL);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1687c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
1697c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
1707c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "unknown resource: %s\n",
1717c478bd9Sstevel@tonic-gate 		    rsrcname);
1727c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	conf_flags = dc->conf_flags;
1757c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return ((alloc_usage(infostr, conf_flags) == 0) ?
1787c478bd9Sstevel@tonic-gate 	    RCM_SUCCESS : RCM_FAILURE);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Relocate dump device to maintain availability during suspension.
1837c478bd9Sstevel@tonic-gate  * Fail request if unable to relocate.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1867c478bd9Sstevel@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)1877c478bd9Sstevel@tonic-gate dump_suspend(rcm_handle_t *hdl, char *rsrcname, id_t id, timespec_t *interval,
1887c478bd9Sstevel@tonic-gate     uint_t flags, char **errstr, rcm_info_t **dependent)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
1917c478bd9Sstevel@tonic-gate 	int		rv;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (flags & RCM_QUERY)
1967c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1997c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2007c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2017c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	rv = dump_relocate(dc, errstr);
2057c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (rv);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2117c478bd9Sstevel@tonic-gate static int
dump_resume(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2127c478bd9Sstevel@tonic-gate dump_resume(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2137c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
2167c478bd9Sstevel@tonic-gate 	int		rv;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2217c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2227c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2237c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	rv = dump_configure(dc, errstr);
2277c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (rv);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * By default, reject offline. If offline request is
2347c478bd9Sstevel@tonic-gate  * forced, attempt to relocate the dump device.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2377c478bd9Sstevel@tonic-gate static int
dump_offline(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2387c478bd9Sstevel@tonic-gate dump_offline(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2397c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
2427c478bd9Sstevel@tonic-gate 	int		conf_flags;
2437c478bd9Sstevel@tonic-gate 	int		rv;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if ((flags & RCM_FORCE) && (flags & RCM_QUERY))
2487c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2517c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2527c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2537c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (flags & RCM_FORCE) {
2577c478bd9Sstevel@tonic-gate 		rv = dump_relocate(dc, errstr);
2587c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2597c478bd9Sstevel@tonic-gate 		return (rv);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/* default */
2637c478bd9Sstevel@tonic-gate 	conf_flags = dc->conf_flags;
2647c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2657c478bd9Sstevel@tonic-gate 	(void) alloc_usage(errstr, conf_flags);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (RCM_FAILURE);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2717c478bd9Sstevel@tonic-gate static int
dump_online(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2727c478bd9Sstevel@tonic-gate dump_online(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2737c478bd9Sstevel@tonic-gate     char  **errstr, rcm_info_t **dependent)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
2767c478bd9Sstevel@tonic-gate 	int		rv;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
2817c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
2827c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
2837c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	rv = dump_configure(dc, errstr);
2877c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	return (rv);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2937c478bd9Sstevel@tonic-gate static int
dump_remove(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)2947c478bd9Sstevel@tonic-gate dump_remove(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
2957c478bd9Sstevel@tonic-gate     char **errstr, rcm_info_t **dependent)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	assert(rsrcname != NULL && errstr != NULL);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
3027c478bd9Sstevel@tonic-gate 	if ((dc = cache_lookup(rsrcname)) == NULL) {
3037c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cache_lock);
3047c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	cache_remove(dc);
3077c478bd9Sstevel@tonic-gate 	free(dc);
3087c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
314*bbf21555SRichard Lowe  * For dedicated dump devices, invoke dumpadm(8)
3157c478bd9Sstevel@tonic-gate  * to relocate dump to swap. For dump device on
3167c478bd9Sstevel@tonic-gate  * swap, this is a no-op as the RCM swap module
317*bbf21555SRichard Lowe  * will relocate by invoking swap(8).
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate static int
dump_relocate(dump_conf_t * dc,char ** errstr)3227c478bd9Sstevel@tonic-gate dump_relocate(dump_conf_t *dc, char **errstr)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int		stat;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * This state may get out of sync for a dump device on swap,
3287c478bd9Sstevel@tonic-gate 	 * since we will will not know if the swap module succeeds.
3297c478bd9Sstevel@tonic-gate 	 * Worst case is we end up invoking dumpadm to configure
3307c478bd9Sstevel@tonic-gate 	 * the same device during a rollback.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	dc->cache_flags |= DUMP_CACHE_OFFLINED;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* RCM swap module will handle non-dedicated */
3357c478bd9Sstevel@tonic-gate 	if (!(dc->conf_flags & DUMP_EXCL))
3367c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "%s\n", DUMPADM_SWAP);
3397c478bd9Sstevel@tonic-gate 	if ((stat = rcm_exec_cmd(DUMPADM_SWAP)) != 0) {
3407c478bd9Sstevel@tonic-gate 		log_cmd_status(stat);
3417c478bd9Sstevel@tonic-gate 		*errstr = strdup(gettext("unable to relocate dump device"));
3427c478bd9Sstevel@tonic-gate 		dc->cache_flags &= ~DUMP_CACHE_OFFLINED;
3437c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * (Re)Configure dump device.
3517c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate static int
dump_configure(dump_conf_t * dc,char ** errstr)3547c478bd9Sstevel@tonic-gate dump_configure(dump_conf_t *dc, char **errstr)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	char		cmd[sizeof (DUMPADM) + MAXPATHLEN];
3577c478bd9Sstevel@tonic-gate 	int		stat;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	assert(dc != NULL && dc->device != NULL);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* minor optimization */
3627c478bd9Sstevel@tonic-gate 	if (!(dc->cache_flags & DUMP_CACHE_OFFLINED))
3637c478bd9Sstevel@tonic-gate 		return (RCM_SUCCESS);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd, sizeof (cmd), "%s%s", DUMPADM, dc->device);
3667c478bd9Sstevel@tonic-gate 	rcm_log_message(RCM_TRACE1, "%s\n", cmd);
3677c478bd9Sstevel@tonic-gate 	if ((stat = rcm_exec_cmd(cmd)) != 0) {
3687c478bd9Sstevel@tonic-gate 		log_cmd_status(stat);
3697c478bd9Sstevel@tonic-gate 		*errstr = strdup(gettext("unable to configure dump device"));
3707c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	dc->cache_flags &= ~DUMP_CACHE_OFFLINED;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (RCM_SUCCESS);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Returns current dump configuration
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate static dump_conf_t *
dump_conf_alloc(void)3817c478bd9Sstevel@tonic-gate dump_conf_alloc(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
3847c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
3857c478bd9Sstevel@tonic-gate 	int		fd;
3867c478bd9Sstevel@tonic-gate 	char		*err;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if ((dc = calloc(1, sizeof (*dc))) == NULL) {
3897c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "calloc failure\n");
3907c478bd9Sstevel@tonic-gate 		return (NULL);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) == -1) {
3947c478bd9Sstevel@tonic-gate 		/*
3957c478bd9Sstevel@tonic-gate 		 * Suppress reporting if no logical link.
3967c478bd9Sstevel@tonic-gate 		 */
3977c478bd9Sstevel@tonic-gate 		if (stat("/dev/dump", &sbuf) == 0 &&
3987c478bd9Sstevel@tonic-gate 		    (fd = open("/dev/dump", O_RDONLY)) == -1) {
3997c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR,
4007c478bd9Sstevel@tonic-gate 			    "failed to open /dev/dump: %s\n",
4017c478bd9Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		if (fd == -1) {
4057c478bd9Sstevel@tonic-gate 			free(dc);
4067c478bd9Sstevel@tonic-gate 			return (NULL);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DIOCGETDEV, dc->device) == -1) {
4117c478bd9Sstevel@tonic-gate 		if (errno == ENODEV) {
4127c478bd9Sstevel@tonic-gate 			dc->device[0] = '\0';
4137c478bd9Sstevel@tonic-gate 		} else {
4147c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
4157c478bd9Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4167c478bd9Sstevel@tonic-gate 			(void) close(fd);
4177c478bd9Sstevel@tonic-gate 			free(dc);
4187c478bd9Sstevel@tonic-gate 			return (NULL);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (dc->device[0] != '\0')  {
4237c478bd9Sstevel@tonic-gate 		if ((dc->conf_flags = ioctl(fd, DIOCGETCONF, 0)) == -1) {
4247c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "ioctl: %s\n",
4257c478bd9Sstevel@tonic-gate 			    ((err = strerror(errno)) == NULL) ? "" : err);
4267c478bd9Sstevel@tonic-gate 			(void) close(fd);
4277c478bd9Sstevel@tonic-gate 			free(dc);
4287c478bd9Sstevel@tonic-gate 			return (NULL);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	(void) close(fd);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	return (dc);
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 	dump_conf_t	*ent, *curr_dump, *tmp;
4407c478bd9Sstevel@tonic-gate 	int		rv = RCM_SUCCESS;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if ((curr_dump = dump_conf_alloc()) == NULL)
4437c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * pass 1 -  mark all current registrations stale
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	for (ent = cache; ent != NULL; ent = ent->next) {
4517c478bd9Sstevel@tonic-gate 		ent->cache_flags |= DUMP_CACHE_STALE;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * update current dump conf
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	if (curr_dump->device[0] == '\0') {
4587c478bd9Sstevel@tonic-gate 		free(curr_dump);
4597c478bd9Sstevel@tonic-gate 	} else if ((ent = cache_lookup(curr_dump->device)) != NULL) {
4607c478bd9Sstevel@tonic-gate 		ent->cache_flags &= ~DUMP_CACHE_STALE;
4617c478bd9Sstevel@tonic-gate 		ent->conf_flags = curr_dump->conf_flags;
4627c478bd9Sstevel@tonic-gate 		free(curr_dump);
4637c478bd9Sstevel@tonic-gate 	} else {
4647c478bd9Sstevel@tonic-gate 		curr_dump->cache_flags |= DUMP_CACHE_NEW;
4657c478bd9Sstevel@tonic-gate 		cache_insert(curr_dump);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * pass 2 - register, unregister, or no-op based on cache flags
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	ent = cache;
4727c478bd9Sstevel@tonic-gate 	while (ent != NULL) {
4737c478bd9Sstevel@tonic-gate 		if (ent->cache_flags & DUMP_CACHE_OFFLINED) {
4747c478bd9Sstevel@tonic-gate 			ent = ent->next;
4757c478bd9Sstevel@tonic-gate 			continue;
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		if (ent->cache_flags & DUMP_CACHE_STALE) {
4797c478bd9Sstevel@tonic-gate 			if (rcm_unregister_interest(hdl, ent->device, 0) !=
4807c478bd9Sstevel@tonic-gate 			    RCM_SUCCESS) {
4817c478bd9Sstevel@tonic-gate 				rcm_log_message(RCM_ERROR, "failed to "
4827c478bd9Sstevel@tonic-gate 				    "unregister %s\n", ent->device);
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 			tmp = ent;
4857c478bd9Sstevel@tonic-gate 			ent = ent->next;
4867c478bd9Sstevel@tonic-gate 			cache_remove(tmp);
4877c478bd9Sstevel@tonic-gate 			free(tmp);
4887c478bd9Sstevel@tonic-gate 			continue;
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		if (!(ent->cache_flags & DUMP_CACHE_NEW)) {
4927c478bd9Sstevel@tonic-gate 			ent = ent->next;
4937c478bd9Sstevel@tonic-gate 			continue;
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		if (rcm_register_interest(hdl, ent->device, 0, NULL) !=
4977c478bd9Sstevel@tonic-gate 		    RCM_SUCCESS) {
4987c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_ERROR, "failed to register "
4997c478bd9Sstevel@tonic-gate 			    "%s\n", ent->device);
5007c478bd9Sstevel@tonic-gate 			rv = RCM_FAILURE;
5017c478bd9Sstevel@tonic-gate 		} else {
5027c478bd9Sstevel@tonic-gate 			rcm_log_message(RCM_DEBUG, "registered %s\n",
5037c478bd9Sstevel@tonic-gate 			    ent->device);
5047c478bd9Sstevel@tonic-gate 			ent->cache_flags &= ~DUMP_CACHE_NEW;
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 		ent = ent->next;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	return (rv);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate static dump_conf_t *
cache_lookup(char * rsrc)5177c478bd9Sstevel@tonic-gate cache_lookup(char *rsrc)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	for (dc = cache; dc != NULL; dc = dc->next) {
5227c478bd9Sstevel@tonic-gate 		if (strcmp(rsrc, dc->device) == 0) {
5237c478bd9Sstevel@tonic-gate 			return (dc);
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	return (NULL);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * Link to front of list.
5317c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate static void
cache_insert(dump_conf_t * ent)5347c478bd9Sstevel@tonic-gate cache_insert(dump_conf_t *ent)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	ent->next = cache;
5377c478bd9Sstevel@tonic-gate 	if (ent->next)
5387c478bd9Sstevel@tonic-gate 		ent->next->prev = ent;
5397c478bd9Sstevel@tonic-gate 	ent->prev = NULL;
5407c478bd9Sstevel@tonic-gate 	cache = ent;
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Call with cache_lock held.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static void
cache_remove(dump_conf_t * ent)5477c478bd9Sstevel@tonic-gate cache_remove(dump_conf_t *ent)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	if (ent->next != NULL) {
5507c478bd9Sstevel@tonic-gate 		ent->next->prev = ent->prev;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	if (ent->prev != NULL) {
5537c478bd9Sstevel@tonic-gate 		ent->prev->next = ent->next;
5547c478bd9Sstevel@tonic-gate 	} else {
5557c478bd9Sstevel@tonic-gate 		cache = ent->next;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	ent->next = NULL;
5587c478bd9Sstevel@tonic-gate 	ent->prev = NULL;
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static void
free_cache(void)5627c478bd9Sstevel@tonic-gate free_cache(void)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	dump_conf_t	*dc;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
5677c478bd9Sstevel@tonic-gate 	while ((dc = cache) != NULL) {
5687c478bd9Sstevel@tonic-gate 		cache = cache->next;
5697c478bd9Sstevel@tonic-gate 		free(dc);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static int
alloc_usage(char ** cpp,int conf_flags)5757c478bd9Sstevel@tonic-gate alloc_usage(char **cpp, int conf_flags)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	/* simplifies message translation */
5787c478bd9Sstevel@tonic-gate 	if (conf_flags & DUMP_EXCL) {
5797c478bd9Sstevel@tonic-gate 		*cpp = strdup(gettext("dump device (dedicated)"));
5807c478bd9Sstevel@tonic-gate 	} else {
5817c478bd9Sstevel@tonic-gate 		*cpp = strdup(gettext("dump device (swap)"));
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (*cpp == NULL) {
5857c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "strdup failure\n");
5867c478bd9Sstevel@tonic-gate 		return (-1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	return (0);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate static void
log_cmd_status(int stat)5927c478bd9Sstevel@tonic-gate log_cmd_status(int stat)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	char	*err;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (stat == -1) {
5977c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait: %s\n",
5987c478bd9Sstevel@tonic-gate 		    ((err =  strerror(errno)) == NULL) ? "" : err);
5997c478bd9Sstevel@tonic-gate 	} else if (WIFEXITED(stat)) {
6007c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "exit status: %d\n",
6017c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(stat));
6027c478bd9Sstevel@tonic-gate 	} else {
6037c478bd9Sstevel@tonic-gate 		rcm_log_message(RCM_ERROR, "wait status: %d\n", stat);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate }
606