xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_ckpt.c (revision 9120:fe1f7d8cd967)
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
55609Scy152378  * Common Development and Distribution License (the "License").
65609Scy152378  * 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  */
211193Smws 
220Sstevel@tonic-gate /*
23*9120SStephen.Hanson@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/mkdev.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <strings.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <fmd_module.h>
370Sstevel@tonic-gate #include <fmd_error.h>
380Sstevel@tonic-gate #include <fmd_alloc.h>
390Sstevel@tonic-gate #include <fmd_case.h>
400Sstevel@tonic-gate #include <fmd_serd.h>
410Sstevel@tonic-gate #include <fmd_subr.h>
420Sstevel@tonic-gate #include <fmd_conf.h>
430Sstevel@tonic-gate #include <fmd_event.h>
440Sstevel@tonic-gate #include <fmd_log.h>
450Sstevel@tonic-gate #include <fmd_api.h>
460Sstevel@tonic-gate #include <fmd_ckpt.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <fmd.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	P2ROUNDUP(x, align)	(-(-(x) & -(align)))
510Sstevel@tonic-gate #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * The fmd_ckpt_t structure is used to manage all of the state needed by the
550Sstevel@tonic-gate  * various subroutines that save and restore checkpoints.  The structure is
560Sstevel@tonic-gate  * initialized using fmd_ckpt_create() or fmd_ckpt_open() and is destroyed
570Sstevel@tonic-gate  * by fmd_ckpt_destroy().  Refer to the subroutines below for more details.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate typedef struct fmd_ckpt {
600Sstevel@tonic-gate 	char ckp_src[PATH_MAX];	/* ckpt input or output filename */
610Sstevel@tonic-gate 	char ckp_dst[PATH_MAX];	/* ckpt rename filename */
620Sstevel@tonic-gate 	uchar_t *ckp_buf;	/* data buffer base address */
630Sstevel@tonic-gate 	fcf_hdr_t *ckp_hdr;	/* file header pointer */
640Sstevel@tonic-gate 	uchar_t *ckp_ptr;	/* data buffer pointer */
650Sstevel@tonic-gate 	size_t ckp_size;	/* data buffer size */
660Sstevel@tonic-gate 	fcf_sec_t *ckp_secp;	/* section header table pointer */
670Sstevel@tonic-gate 	fcf_sec_t *ckp_modp;	/* section header for module */
680Sstevel@tonic-gate 	uint_t ckp_secs;	/* number of sections */
690Sstevel@tonic-gate 	char *ckp_strs;		/* string table base pointer */
700Sstevel@tonic-gate 	char *ckp_strp;		/* string table pointer */
710Sstevel@tonic-gate 	size_t ckp_strn;	/* string table size */
720Sstevel@tonic-gate 	int ckp_fd;		/* output descriptor */
730Sstevel@tonic-gate 	fmd_module_t *ckp_mp;	/* checkpoint module */
740Sstevel@tonic-gate 	void *ckp_arg;		/* private arg for callbacks */
750Sstevel@tonic-gate } fmd_ckpt_t;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate typedef struct fmd_ckpt_desc {
780Sstevel@tonic-gate 	uint64_t secd_size;	/* minimum section size */
790Sstevel@tonic-gate 	uint32_t secd_entsize;	/* minimum section entry size */
800Sstevel@tonic-gate 	uint32_t secd_align;	/* section alignment */
810Sstevel@tonic-gate } fmd_ckpt_desc_t;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Table of FCF section descriptions.  Here we record the minimum size for each
850Sstevel@tonic-gate  * section (for use during restore) and the expected entry size and alignment
860Sstevel@tonic-gate  * for each section (for use during both checkpoint and restore).
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate static const fmd_ckpt_desc_t _fmd_ckpt_sections[] = {
890Sstevel@tonic-gate { 0, 0, sizeof (uint8_t) },					   /* NONE */
900Sstevel@tonic-gate { 1, 0, sizeof (char) },					   /* STRTAB */
910Sstevel@tonic-gate { sizeof (fcf_module_t), 0, sizeof (uint32_t) },		   /* MODULE */
920Sstevel@tonic-gate { sizeof (fcf_case_t), 0, sizeof (uint32_t) },			   /* CASE */
930Sstevel@tonic-gate { sizeof (fcf_buf_t), sizeof (fcf_buf_t), sizeof (uint32_t) },	   /* BUFS */
940Sstevel@tonic-gate { 0, 0, _MAX_ALIGNMENT },					   /* BUFFER */
950Sstevel@tonic-gate { sizeof (fcf_serd_t), sizeof (fcf_serd_t), sizeof (uint64_t) },   /* SERD */
960Sstevel@tonic-gate { sizeof (fcf_event_t), sizeof (fcf_event_t), sizeof (uint64_t) }, /* EVENTS */
970Sstevel@tonic-gate { sizeof (fcf_nvl_t), sizeof (fcf_nvl_t), sizeof (uint64_t) },	   /* NVLISTS */
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static int
fmd_ckpt_create(fmd_ckpt_t * ckp,fmd_module_t * mp)1010Sstevel@tonic-gate fmd_ckpt_create(fmd_ckpt_t *ckp, fmd_module_t *mp)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	const char *dir = mp->mod_ckpt;
1040Sstevel@tonic-gate 	const char *name = mp->mod_name;
1050Sstevel@tonic-gate 	mode_t mode;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	bzero(ckp, sizeof (fmd_ckpt_t));
1080Sstevel@tonic-gate 	ckp->ckp_mp = mp;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	ckp->ckp_size = sizeof (fcf_hdr_t);
1110Sstevel@tonic-gate 	ckp->ckp_strn = 1; /* for \0 */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	(void) snprintf(ckp->ckp_src, PATH_MAX, "%s/%s+", dir, name);
1140Sstevel@tonic-gate 	(void) snprintf(ckp->ckp_dst, PATH_MAX, "%s/%s", dir, name);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	(void) unlink(ckp->ckp_src);
1170Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "ckpt.mode", &mode);
1180Sstevel@tonic-gate 	ckp->ckp_fd = open64(ckp->ckp_src, O_WRONLY | O_CREAT | O_EXCL, mode);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (ckp->ckp_fd);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*PRINTFLIKE2*/
1240Sstevel@tonic-gate static int
fmd_ckpt_inval(fmd_ckpt_t * ckp,const char * format,...)1250Sstevel@tonic-gate fmd_ckpt_inval(fmd_ckpt_t *ckp, const char *format, ...)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	va_list ap;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	va_start(ap, format);
1300Sstevel@tonic-gate 	fmd_verror(EFMD_CKPT_INVAL, format, ap);
1310Sstevel@tonic-gate 	va_end(ap);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	fmd_free(ckp->ckp_buf, ckp->ckp_size);
1340Sstevel@tonic-gate 	return (fmd_set_errno(EFMD_CKPT_INVAL));
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int
fmd_ckpt_open(fmd_ckpt_t * ckp,fmd_module_t * mp)1380Sstevel@tonic-gate fmd_ckpt_open(fmd_ckpt_t *ckp, fmd_module_t *mp)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	struct stat64 st;
1410Sstevel@tonic-gate 	uint64_t seclen;
1420Sstevel@tonic-gate 	uint_t i;
1430Sstevel@tonic-gate 	int err;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	bzero(ckp, sizeof (fmd_ckpt_t));
1460Sstevel@tonic-gate 	ckp->ckp_mp = mp;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	(void) snprintf(ckp->ckp_src, PATH_MAX, "%s/%s",
1490Sstevel@tonic-gate 	    mp->mod_ckpt, mp->mod_name);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if ((ckp->ckp_fd = open(ckp->ckp_src, O_RDONLY)) == -1)
1520Sstevel@tonic-gate 		return (-1); /* failed to open checkpoint file */
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (fstat64(ckp->ckp_fd, &st) == -1) {
1550Sstevel@tonic-gate 		err = errno;
1560Sstevel@tonic-gate 		(void) close(ckp->ckp_fd);
1570Sstevel@tonic-gate 		return (fmd_set_errno(err));
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	ckp->ckp_buf = fmd_alloc(st.st_size, FMD_SLEEP);
1610Sstevel@tonic-gate 	ckp->ckp_hdr = (void *)ckp->ckp_buf;
1620Sstevel@tonic-gate 	ckp->ckp_size = read(ckp->ckp_fd, ckp->ckp_buf, st.st_size);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (ckp->ckp_size != st.st_size || ckp->ckp_size < sizeof (fcf_hdr_t) ||
1650Sstevel@tonic-gate 	    ckp->ckp_size != ckp->ckp_hdr->fcfh_filesz) {
1660Sstevel@tonic-gate 		err = ckp->ckp_size == (size_t)-1L ? errno : EFMD_CKPT_SHORT;
1670Sstevel@tonic-gate 		fmd_free(ckp->ckp_buf, st.st_size);
1680Sstevel@tonic-gate 		(void) close(ckp->ckp_fd);
1690Sstevel@tonic-gate 		return (fmd_set_errno(err));
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	(void) close(ckp->ckp_fd);
1730Sstevel@tonic-gate 	ckp->ckp_fd = -1;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * Once we've read in a consistent copy of the FCF file and we're sure
1770Sstevel@tonic-gate 	 * the header can be accessed, go through it and make sure everything
1780Sstevel@tonic-gate 	 * is valid.  We also check that unused bits are zero so we can expand
1790Sstevel@tonic-gate 	 * to use them safely in the future and support old files if needed.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	if (bcmp(&ckp->ckp_hdr->fcfh_ident[FCF_ID_MAG0],
1820Sstevel@tonic-gate 	    FCF_MAG_STRING, FCF_MAG_STRLEN) != 0)
1830Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "bad checkpoint magic string\n"));
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_ident[FCF_ID_MODEL] != FCF_MODEL_NATIVE)
1860Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "bad checkpoint data model\n"));
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_ident[FCF_ID_ENCODING] != FCF_ENCODE_NATIVE)
1890Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "bad checkpoint data encoding\n"));
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_ident[FCF_ID_VERSION] != FCF_VERSION_1) {
1920Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "bad checkpoint version %u\n",
1930Sstevel@tonic-gate 		    ckp->ckp_hdr->fcfh_ident[FCF_ID_VERSION]));
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	for (i = FCF_ID_PAD; i < FCF_ID_SIZE; i++) {
1970Sstevel@tonic-gate 		if (ckp->ckp_hdr->fcfh_ident[i] != 0) {
1980Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp,
1990Sstevel@tonic-gate 			    "bad checkpoint padding at id[%d]", i));
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_flags & ~FCF_FL_VALID)
2040Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "bad checkpoint flags\n"));
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_pad != 0)
2070Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "reserved field in use\n"));
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_hdrsize < sizeof (fcf_hdr_t) ||
2100Sstevel@tonic-gate 	    ckp->ckp_hdr->fcfh_secsize < sizeof (fcf_sec_t)) {
2110Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp,
2120Sstevel@tonic-gate 		    "bad header and/or section size\n"));
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	seclen = (uint64_t)ckp->ckp_hdr->fcfh_secnum *
2160Sstevel@tonic-gate 	    (uint64_t)ckp->ckp_hdr->fcfh_secsize;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (ckp->ckp_hdr->fcfh_secoff > ckp->ckp_size ||
2190Sstevel@tonic-gate 	    seclen > ckp->ckp_size ||
2200Sstevel@tonic-gate 	    ckp->ckp_hdr->fcfh_secoff + seclen > ckp->ckp_size ||
2210Sstevel@tonic-gate 	    ckp->ckp_hdr->fcfh_secoff + seclen < ckp->ckp_hdr->fcfh_secoff)
2220Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "truncated section headers\n"));
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (!IS_P2ALIGNED(ckp->ckp_hdr->fcfh_secoff, sizeof (uint64_t)) ||
2250Sstevel@tonic-gate 	    !IS_P2ALIGNED(ckp->ckp_hdr->fcfh_secsize, sizeof (uint64_t)))
2260Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "misaligned section headers\n"));
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Once the header is validated, iterate over the section headers
2300Sstevel@tonic-gate 	 * ensuring that each one is valid w.r.t. offset, alignment, and size.
2310Sstevel@tonic-gate 	 * We also pick up the string table pointer during this pass.
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	ckp->ckp_secp = (void *)(ckp->ckp_buf + ckp->ckp_hdr->fcfh_secoff);
2340Sstevel@tonic-gate 	ckp->ckp_secs = ckp->ckp_hdr->fcfh_secnum;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	for (i = 0; i < ckp->ckp_secs; i++) {
2370Sstevel@tonic-gate 		fcf_sec_t *sp = (void *)(ckp->ckp_buf +
2380Sstevel@tonic-gate 		    ckp->ckp_hdr->fcfh_secoff + ckp->ckp_hdr->fcfh_secsize * i);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		const fmd_ckpt_desc_t *dp = &_fmd_ckpt_sections[sp->fcfs_type];
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		if (sp->fcfs_flags != 0) {
2430Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has invalid "
2440Sstevel@tonic-gate 			    "section flags (0x%x)\n", i, sp->fcfs_flags));
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		if (sp->fcfs_align & (sp->fcfs_align - 1)) {
2480Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has invalid "
2490Sstevel@tonic-gate 			    "alignment (%u)\n", i, sp->fcfs_align));
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		if (sp->fcfs_offset & (sp->fcfs_align - 1)) {
2530Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u is not properly"
2540Sstevel@tonic-gate 			    " aligned (offset %llu)\n", i, sp->fcfs_offset));
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		if (sp->fcfs_entsize != 0 &&
2580Sstevel@tonic-gate 		    (sp->fcfs_entsize & (sp->fcfs_align - 1)) != 0) {
2590Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has misaligned "
2600Sstevel@tonic-gate 			    "entsize %u\n", i, sp->fcfs_entsize));
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 		if (sp->fcfs_offset > ckp->ckp_size ||
2640Sstevel@tonic-gate 		    sp->fcfs_size > ckp->ckp_size ||
2650Sstevel@tonic-gate 		    sp->fcfs_offset + sp->fcfs_size > ckp->ckp_size ||
2660Sstevel@tonic-gate 		    sp->fcfs_offset + sp->fcfs_size < sp->fcfs_offset) {
2670Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has corrupt "
2680Sstevel@tonic-gate 			    "size or offset\n", i));
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		if (sp->fcfs_type >= sizeof (_fmd_ckpt_sections) /
2720Sstevel@tonic-gate 		    sizeof (_fmd_ckpt_sections[0])) {
2730Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has unknown "
2740Sstevel@tonic-gate 			    "section type %u\n", i, sp->fcfs_type));
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		if (sp->fcfs_align != dp->secd_align) {
2780Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has align %u "
2790Sstevel@tonic-gate 			    "(not %u)\n", i, sp->fcfs_align, dp->secd_align));
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		if (sp->fcfs_size < dp->secd_size ||
2830Sstevel@tonic-gate 		    sp->fcfs_entsize < dp->secd_entsize) {
2840Sstevel@tonic-gate 			return (fmd_ckpt_inval(ckp, "section %u has short "
2850Sstevel@tonic-gate 			    "size or entsize\n", i));
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		switch (sp->fcfs_type) {
2890Sstevel@tonic-gate 		case FCF_SECT_STRTAB:
2900Sstevel@tonic-gate 			if (ckp->ckp_strs != NULL) {
2910Sstevel@tonic-gate 				return (fmd_ckpt_inval(ckp, "multiple string "
2920Sstevel@tonic-gate 				    "tables are present in checkpoint file\n"));
2930Sstevel@tonic-gate 			}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 			ckp->ckp_strs = (char *)ckp->ckp_buf + sp->fcfs_offset;
2960Sstevel@tonic-gate 			ckp->ckp_strn = sp->fcfs_size;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 			if (ckp->ckp_strs[ckp->ckp_strn - 1] != '\0') {
2990Sstevel@tonic-gate 				return (fmd_ckpt_inval(ckp, "string table %u "
3000Sstevel@tonic-gate 				    "is missing terminating nul byte\n", i));
3010Sstevel@tonic-gate 			}
3020Sstevel@tonic-gate 			break;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		case FCF_SECT_MODULE:
3050Sstevel@tonic-gate 			if (ckp->ckp_modp != NULL) {
3060Sstevel@tonic-gate 				return (fmd_ckpt_inval(ckp, "multiple module "
3070Sstevel@tonic-gate 				    "sects are present in checkpoint file\n"));
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 			ckp->ckp_modp = sp;
3100Sstevel@tonic-gate 			break;
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Ensure that the first section is an empty one of type FCF_SECT_NONE.
3160Sstevel@tonic-gate 	 * This is done to ensure that links can use index 0 as a null section.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	if (ckp->ckp_secs == 0 || ckp->ckp_secp->fcfs_type != FCF_SECT_NONE ||
3190Sstevel@tonic-gate 	    ckp->ckp_secp->fcfs_entsize != 0 || ckp->ckp_secp->fcfs_size != 0) {
3200Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp, "section 0 is not of the "
3210Sstevel@tonic-gate 		    "appropriate size and/or attributes (SECT_NONE)\n"));
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (ckp->ckp_modp == NULL) {
3250Sstevel@tonic-gate 		return (fmd_ckpt_inval(ckp,
3260Sstevel@tonic-gate 		    "no module section found in file\n"));
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	return (0);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static void
fmd_ckpt_destroy(fmd_ckpt_t * ckp)3330Sstevel@tonic-gate fmd_ckpt_destroy(fmd_ckpt_t *ckp)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	if (ckp->ckp_buf != NULL)
3360Sstevel@tonic-gate 		fmd_free(ckp->ckp_buf, ckp->ckp_size);
3370Sstevel@tonic-gate 	if (ckp->ckp_fd >= 0)
3380Sstevel@tonic-gate 		(void) close(ckp->ckp_fd);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * fmd_ckpt_error() is used as a wrapper around fmd_error() for ckpt routines.
3430Sstevel@tonic-gate  * It calls fmd_module_unlock() on behalf of its caller, logs the error, and
3440Sstevel@tonic-gate  * then aborts the API call and the surrounding module entry point by doing an
3450Sstevel@tonic-gate  * fmd_module_abort(), which longjmps to the place where we entered the module.
3460Sstevel@tonic-gate  * Depending on the type of error and conf settings, we will reset or fail.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate /*PRINTFLIKE3*/
3490Sstevel@tonic-gate static void
fmd_ckpt_error(fmd_ckpt_t * ckp,int err,const char * format,...)3500Sstevel@tonic-gate fmd_ckpt_error(fmd_ckpt_t *ckp, int err, const char *format, ...)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	fmd_module_t *mp = ckp->ckp_mp;
3530Sstevel@tonic-gate 	va_list ap;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	va_start(ap, format);
3560Sstevel@tonic-gate 	fmd_verror(err, format, ap);
3570Sstevel@tonic-gate 	va_end(ap);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (fmd_module_locked(mp))
3600Sstevel@tonic-gate 		fmd_module_unlock(mp);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	fmd_ckpt_destroy(ckp);
3630Sstevel@tonic-gate 	fmd_module_abort(mp, err);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate static fcf_secidx_t
fmd_ckpt_section(fmd_ckpt_t * ckp,const void * data,uint_t type,uint64_t size)3670Sstevel@tonic-gate fmd_ckpt_section(fmd_ckpt_t *ckp, const void *data, uint_t type, uint64_t size)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	const fmd_ckpt_desc_t *dp;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	ASSERT(type < sizeof (_fmd_ckpt_sections) / sizeof (fmd_ckpt_desc_t));
3720Sstevel@tonic-gate 	dp = &_fmd_ckpt_sections[type];
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	ckp->ckp_ptr = (uchar_t *)
3750Sstevel@tonic-gate 	    P2ROUNDUP((uintptr_t)ckp->ckp_ptr, dp->secd_align);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_type = type;
3780Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_align = dp->secd_align;
3790Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_flags = 0;
3800Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_entsize = dp->secd_entsize;
3810Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_offset = (size_t)(ckp->ckp_ptr - ckp->ckp_buf);
3820Sstevel@tonic-gate 	ckp->ckp_secp->fcfs_size = size;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/*
3850Sstevel@tonic-gate 	 * If the data pointer is non-NULL, copy the data to our buffer; else
3860Sstevel@tonic-gate 	 * the caller is responsible for doing so and updating ckp->ckp_ptr.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (data != NULL) {
3890Sstevel@tonic-gate 		bcopy(data, ckp->ckp_ptr, size);
3900Sstevel@tonic-gate 		ckp->ckp_ptr += size;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	ckp->ckp_secp++;
3940Sstevel@tonic-gate 	return (ckp->ckp_secs++);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static fcf_stridx_t
fmd_ckpt_string(fmd_ckpt_t * ckp,const char * s)3980Sstevel@tonic-gate fmd_ckpt_string(fmd_ckpt_t *ckp, const char *s)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	fcf_stridx_t idx = (fcf_stridx_t)(ckp->ckp_strp - ckp->ckp_strs);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	(void) strcpy(ckp->ckp_strp, s);
4030Sstevel@tonic-gate 	ckp->ckp_strp += strlen(s) + 1;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	return (idx);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate static int
fmd_ckpt_alloc(fmd_ckpt_t * ckp,uint64_t gen)4090Sstevel@tonic-gate fmd_ckpt_alloc(fmd_ckpt_t *ckp, uint64_t gen)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * We've added up all the sections by now: add two more for SECT_NONE
4130Sstevel@tonic-gate 	 * and SECT_STRTAB, and add the size of the section header table and
4140Sstevel@tonic-gate 	 * string table to the total size.  We know that the fcf_hdr_t is
4150Sstevel@tonic-gate 	 * aligned so that that fcf_sec_t's can follow it, and that fcf_sec_t
4160Sstevel@tonic-gate 	 * is aligned so that any section can follow it, so no extra padding
4170Sstevel@tonic-gate 	 * bytes need to be allocated between any of these items.
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	ckp->ckp_secs += 2; /* for FCF_SECT_NONE and FCF_SECT_STRTAB */
4200Sstevel@tonic-gate 	ckp->ckp_size += sizeof (fcf_sec_t) * ckp->ckp_secs;
4210Sstevel@tonic-gate 	ckp->ckp_size += ckp->ckp_strn;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "alloc fcf buf size %u", ckp->ckp_size));
4240Sstevel@tonic-gate 	ckp->ckp_buf = fmd_zalloc(ckp->ckp_size, FMD_NOSLEEP);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (ckp->ckp_buf == NULL)
4270Sstevel@tonic-gate 		return (-1); /* errno is set for us */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	ckp->ckp_hdr = (void *)ckp->ckp_buf;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_MAG0] = FCF_MAG_MAG0;
4320Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_MAG1] = FCF_MAG_MAG1;
4330Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_MAG2] = FCF_MAG_MAG2;
4340Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_MAG3] = FCF_MAG_MAG3;
4350Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_MODEL] = FCF_MODEL_NATIVE;
4360Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_ENCODING] = FCF_ENCODE_NATIVE;
4370Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_ident[FCF_ID_VERSION] = FCF_VERSION;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_hdrsize = sizeof (fcf_hdr_t);
4400Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_secsize = sizeof (fcf_sec_t);
4410Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_secnum = ckp->ckp_secs;
4420Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_secoff = sizeof (fcf_hdr_t);
4430Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_filesz = ckp->ckp_size;
4440Sstevel@tonic-gate 	ckp->ckp_hdr->fcfh_cgen = gen;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	ckp->ckp_secs = 0; /* reset section counter for second pass */
4470Sstevel@tonic-gate 	ckp->ckp_secp = (void *)(ckp->ckp_buf + sizeof (fcf_hdr_t));
4480Sstevel@tonic-gate 	ckp->ckp_strs = (char *)ckp->ckp_buf + ckp->ckp_size - ckp->ckp_strn;
4490Sstevel@tonic-gate 	ckp->ckp_strp = ckp->ckp_strs + 1; /* use first byte as \0 */
4500Sstevel@tonic-gate 	ckp->ckp_ptr = (uchar_t *)(ckp->ckp_secp + ckp->ckp_hdr->fcfh_secnum);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	(void) fmd_ckpt_section(ckp, NULL, FCF_SECT_NONE, 0);
4530Sstevel@tonic-gate 	return (0);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate static int
fmd_ckpt_commit(fmd_ckpt_t * ckp)4570Sstevel@tonic-gate fmd_ckpt_commit(fmd_ckpt_t *ckp)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	fcf_sec_t *secbase = (void *)(ckp->ckp_buf + sizeof (fcf_hdr_t));
4600Sstevel@tonic-gate 	size_t stroff = ckp->ckp_size - ckp->ckp_strn;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * Before committing the checkpoint, we assert that fmd_ckpt_t's sizes
4640Sstevel@tonic-gate 	 * and current pointer locations all add up appropriately.  Any ASSERTs
4650Sstevel@tonic-gate 	 * which trip here likely indicate an inconsistency in the code for the
4660Sstevel@tonic-gate 	 * reservation pass and the buffer update pass of the FCF subroutines.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	ASSERT((size_t)(ckp->ckp_ptr - ckp->ckp_buf) == stroff);
4690Sstevel@tonic-gate 	(void) fmd_ckpt_section(ckp, NULL, FCF_SECT_STRTAB, ckp->ckp_strn);
4700Sstevel@tonic-gate 	ckp->ckp_ptr += ckp->ckp_strn; /* string table is already filled in */
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	ASSERT(ckp->ckp_secs == ckp->ckp_hdr->fcfh_secnum);
4730Sstevel@tonic-gate 	ASSERT(ckp->ckp_secp == secbase + ckp->ckp_hdr->fcfh_secnum);
4740Sstevel@tonic-gate 	ASSERT(ckp->ckp_ptr == ckp->ckp_buf + ckp->ckp_hdr->fcfh_filesz);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (write(ckp->ckp_fd, ckp->ckp_buf, ckp->ckp_size) != ckp->ckp_size ||
4770Sstevel@tonic-gate 	    fsync(ckp->ckp_fd) != 0 || close(ckp->ckp_fd) != 0)
4780Sstevel@tonic-gate 		return (-1); /* errno is set for us */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	ckp->ckp_fd = -1; /* fd is now closed */
4810Sstevel@tonic-gate 	return (rename(ckp->ckp_src, ckp->ckp_dst) != 0);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate static void
fmd_ckpt_resv(fmd_ckpt_t * ckp,size_t size,size_t align)4850Sstevel@tonic-gate fmd_ckpt_resv(fmd_ckpt_t *ckp, size_t size, size_t align)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	if (size != 0) {
4880Sstevel@tonic-gate 		ckp->ckp_size = P2ROUNDUP(ckp->ckp_size, align) + size;
4890Sstevel@tonic-gate 		ckp->ckp_secs++;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate static void
fmd_ckpt_resv_buf(fmd_buf_t * bp,fmd_ckpt_t * ckp)4940Sstevel@tonic-gate fmd_ckpt_resv_buf(fmd_buf_t *bp, fmd_ckpt_t *ckp)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate 	ckp->ckp_size = P2ROUNDUP(ckp->ckp_size, _MAX_ALIGNMENT) + bp->buf_size;
4970Sstevel@tonic-gate 	ckp->ckp_strn += strlen(bp->buf_name) + 1;
4980Sstevel@tonic-gate 	ckp->ckp_secs++;
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static void
fmd_ckpt_save_buf(fmd_buf_t * bp,fmd_ckpt_t * ckp)5020Sstevel@tonic-gate fmd_ckpt_save_buf(fmd_buf_t *bp, fmd_ckpt_t *ckp)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	fcf_buf_t *fcfb = ckp->ckp_arg;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	fcfb->fcfb_name = fmd_ckpt_string(ckp, bp->buf_name);
5070Sstevel@tonic-gate 	fcfb->fcfb_data = fmd_ckpt_section(ckp,
5080Sstevel@tonic-gate 	    bp->buf_data, FCF_SECT_BUFFER, bp->buf_size);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	ckp->ckp_arg = fcfb + 1;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate static void
fmd_ckpt_save_event(fmd_ckpt_t * ckp,fmd_event_t * e)5140Sstevel@tonic-gate fmd_ckpt_save_event(fmd_ckpt_t *ckp, fmd_event_t *e)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	fcf_event_t *fcfe = (void *)ckp->ckp_ptr;
5170Sstevel@tonic-gate 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
5180Sstevel@tonic-gate 	fmd_log_t *lp = ep->ev_log;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	fcfe->fcfe_todsec = ep->ev_time.ftv_sec;
5210Sstevel@tonic-gate 	fcfe->fcfe_todnsec = ep->ev_time.ftv_nsec;
5220Sstevel@tonic-gate 	fcfe->fcfe_major = lp ? major(lp->log_stat.st_dev) : -1U;
5230Sstevel@tonic-gate 	fcfe->fcfe_minor = lp ? minor(lp->log_stat.st_dev) : -1U;
5240Sstevel@tonic-gate 	fcfe->fcfe_inode = lp ? lp->log_stat.st_ino : -1ULL;
5250Sstevel@tonic-gate 	fcfe->fcfe_offset = ep->ev_off;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	ckp->ckp_ptr += sizeof (fcf_event_t);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate static void
fmd_ckpt_save_nvlist(fmd_ckpt_t * ckp,nvlist_t * nvl)5310Sstevel@tonic-gate fmd_ckpt_save_nvlist(fmd_ckpt_t *ckp, nvlist_t *nvl)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	fcf_nvl_t *fcfn = (void *)ckp->ckp_ptr;
5340Sstevel@tonic-gate 	char *nvbuf = (char *)ckp->ckp_ptr + sizeof (fcf_nvl_t);
5350Sstevel@tonic-gate 	size_t nvsize = 0;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	(void) nvlist_size(nvl, &nvsize, NV_ENCODE_NATIVE);
5380Sstevel@tonic-gate 	fcfn->fcfn_size = (uint64_t)nvsize;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	(void) nvlist_pack(nvl, &nvbuf, &nvsize, NV_ENCODE_NATIVE, 0);
5410Sstevel@tonic-gate 	ckp->ckp_ptr += sizeof (fcf_nvl_t) + nvsize;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	ckp->ckp_ptr = (uchar_t *)
5440Sstevel@tonic-gate 	    P2ROUNDUP((uintptr_t)ckp->ckp_ptr, sizeof (uint64_t));
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static void
fmd_ckpt_resv_serd(fmd_serd_eng_t * sgp,fmd_ckpt_t * ckp)5480Sstevel@tonic-gate fmd_ckpt_resv_serd(fmd_serd_eng_t *sgp, fmd_ckpt_t *ckp)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	fmd_ckpt_resv(ckp,
5510Sstevel@tonic-gate 	    sizeof (fcf_event_t) * sgp->sg_count, sizeof (uint64_t));
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	ckp->ckp_strn += strlen(sgp->sg_name) + 1;
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate static void
fmd_ckpt_save_serd(fmd_serd_eng_t * sgp,fmd_ckpt_t * ckp)5570Sstevel@tonic-gate fmd_ckpt_save_serd(fmd_serd_eng_t *sgp, fmd_ckpt_t *ckp)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	fcf_serd_t *fcfd = ckp->ckp_arg;
5600Sstevel@tonic-gate 	fcf_secidx_t evsec = FCF_SECT_NONE;
5610Sstevel@tonic-gate 	fmd_serd_elem_t *sep;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (sgp->sg_count != 0) {
5640Sstevel@tonic-gate 		evsec = fmd_ckpt_section(ckp, NULL, FCF_SECT_EVENTS,
5650Sstevel@tonic-gate 		    sizeof (fcf_event_t) * sgp->sg_count);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 		for (sep = fmd_list_next(&sgp->sg_list);
5680Sstevel@tonic-gate 		    sep != NULL; sep = fmd_list_next(sep))
5690Sstevel@tonic-gate 			fmd_ckpt_save_event(ckp, sep->se_event);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	fcfd->fcfd_name = fmd_ckpt_string(ckp, sgp->sg_name);
5730Sstevel@tonic-gate 	fcfd->fcfd_events = evsec;
5740Sstevel@tonic-gate 	fcfd->fcfd_pad = 0;
5750Sstevel@tonic-gate 	fcfd->fcfd_n = sgp->sg_n;
5760Sstevel@tonic-gate 	fcfd->fcfd_t = sgp->sg_t;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	ckp->ckp_arg = fcfd + 1;
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate static void
fmd_ckpt_resv_case(fmd_ckpt_t * ckp,fmd_case_t * cp)5820Sstevel@tonic-gate fmd_ckpt_resv_case(fmd_ckpt_t *ckp, fmd_case_t *cp)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
5850Sstevel@tonic-gate 	fmd_case_susp_t *cis;
5860Sstevel@tonic-gate 	uint_t n;
5870Sstevel@tonic-gate 
5881193Smws 	if (cip->ci_xprt != NULL)
5891193Smws 		return; /* do not checkpoint cases from remote transports */
5901193Smws 
5910Sstevel@tonic-gate 	n = fmd_buf_hash_count(&cip->ci_bufs);
5920Sstevel@tonic-gate 	fmd_buf_hash_apply(&cip->ci_bufs, (fmd_buf_f *)fmd_ckpt_resv_buf, ckp);
5930Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, sizeof (fcf_buf_t) * n, sizeof (uint32_t));
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (cip->ci_principal != NULL)
5960Sstevel@tonic-gate 		fmd_ckpt_resv(ckp, sizeof (fcf_event_t), sizeof (uint64_t));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	fmd_ckpt_resv(ckp,
5990Sstevel@tonic-gate 	    sizeof (fcf_event_t) * cip->ci_nitems, sizeof (uint64_t));
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (cip->ci_nsuspects != 0)
6020Sstevel@tonic-gate 		ckp->ckp_size = P2ROUNDUP(ckp->ckp_size, sizeof (uint64_t));
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	cip->ci_nvsz = 0; /* compute size of packed suspect nvlist array */
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
6070Sstevel@tonic-gate 		size_t nvsize = 0;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		(void) nvlist_size(cis->cis_nvl, &nvsize, NV_ENCODE_NATIVE);
6100Sstevel@tonic-gate 		cip->ci_nvsz += sizeof (fcf_nvl_t) + nvsize;
6110Sstevel@tonic-gate 		cip->ci_nvsz = P2ROUNDUP(cip->ci_nvsz, sizeof (uint64_t));
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, cip->ci_nvsz, sizeof (uint64_t));
6150Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, sizeof (fcf_case_t), sizeof (uint32_t));
6160Sstevel@tonic-gate 	ckp->ckp_strn += strlen(cip->ci_uuid) + 1;
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate static void
fmd_ckpt_save_case(fmd_ckpt_t * ckp,fmd_case_t * cp)6200Sstevel@tonic-gate fmd_ckpt_save_case(fmd_ckpt_t *ckp, fmd_case_t *cp)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	fmd_case_item_t *cit;
6250Sstevel@tonic-gate 	fmd_case_susp_t *cis;
6260Sstevel@tonic-gate 	fcf_case_t fcfc;
6270Sstevel@tonic-gate 	uint_t n;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	fcf_secidx_t bufsec = FCF_SECIDX_NONE;
6300Sstevel@tonic-gate 	fcf_secidx_t evsec = FCF_SECIDX_NONE;
6310Sstevel@tonic-gate 	fcf_secidx_t nvsec = FCF_SECIDX_NONE;
6320Sstevel@tonic-gate 	fcf_secidx_t prsec = FCF_SECIDX_NONE;
6330Sstevel@tonic-gate 
6341193Smws 	if (cip->ci_xprt != NULL)
6351193Smws 		return; /* do not checkpoint cases from remote transports */
6361193Smws 
6370Sstevel@tonic-gate 	if ((n = fmd_buf_hash_count(&cip->ci_bufs)) != 0) {
6380Sstevel@tonic-gate 		size_t size = sizeof (fcf_buf_t) * n;
6390Sstevel@tonic-gate 		fcf_buf_t *bufs = ckp->ckp_arg = fmd_alloc(size, FMD_SLEEP);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		fmd_buf_hash_apply(&cip->ci_bufs,
6420Sstevel@tonic-gate 		    (fmd_buf_f *)fmd_ckpt_save_buf, ckp);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		bufsec = fmd_ckpt_section(ckp, bufs, FCF_SECT_BUFS, size);
6450Sstevel@tonic-gate 		fmd_free(bufs, size);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (cip->ci_principal != NULL) {
6490Sstevel@tonic-gate 		prsec = fmd_ckpt_section(ckp, NULL, FCF_SECT_EVENTS,
6500Sstevel@tonic-gate 		    sizeof (fcf_event_t));
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 		fmd_ckpt_save_event(ckp, cip->ci_principal);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (cip->ci_nitems != 0) {
6560Sstevel@tonic-gate 		evsec = fmd_ckpt_section(ckp, NULL, FCF_SECT_EVENTS,
6570Sstevel@tonic-gate 		    sizeof (fcf_event_t) * cip->ci_nitems);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next)
6600Sstevel@tonic-gate 			fmd_ckpt_save_event(ckp, cit->cit_event);
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (cip->ci_nsuspects != 0) {
6640Sstevel@tonic-gate 		nvsec = fmd_ckpt_section(ckp, NULL,
6650Sstevel@tonic-gate 		    FCF_SECT_NVLISTS, cip->ci_nvsz);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next)
6680Sstevel@tonic-gate 			fmd_ckpt_save_nvlist(ckp, cis->cis_nvl);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	fcfc.fcfc_uuid = fmd_ckpt_string(ckp, cip->ci_uuid);
6720Sstevel@tonic-gate 	fcfc.fcfc_bufs = bufsec;
6730Sstevel@tonic-gate 	fcfc.fcfc_principal = prsec;
6740Sstevel@tonic-gate 	fcfc.fcfc_events = evsec;
6750Sstevel@tonic-gate 	fcfc.fcfc_suspects = nvsec;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	switch (cip->ci_state) {
6780Sstevel@tonic-gate 	case FMD_CASE_UNSOLVED:
6790Sstevel@tonic-gate 		fcfc.fcfc_state = FCF_CASE_UNSOLVED;
6800Sstevel@tonic-gate 		break;
6810Sstevel@tonic-gate 	case FMD_CASE_SOLVED:
6820Sstevel@tonic-gate 		fcfc.fcfc_state = FCF_CASE_SOLVED;
6830Sstevel@tonic-gate 		break;
6841193Smws 	case FMD_CASE_CLOSE_WAIT:
6851193Smws 		fcfc.fcfc_state = FCF_CASE_CLOSE_WAIT;
6860Sstevel@tonic-gate 		break;
6870Sstevel@tonic-gate 	default:
6880Sstevel@tonic-gate 		fmd_panic("case %p (%s) has invalid state %u",
6890Sstevel@tonic-gate 		    (void *)cp, cip->ci_uuid, cip->ci_state);
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	(void) fmd_ckpt_section(ckp, &fcfc, FCF_SECT_CASE, sizeof (fcf_case_t));
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate static void
fmd_ckpt_resv_module(fmd_ckpt_t * ckp,fmd_module_t * mp)6960Sstevel@tonic-gate fmd_ckpt_resv_module(fmd_ckpt_t *ckp, fmd_module_t *mp)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate 	fmd_case_t *cp;
6990Sstevel@tonic-gate 	uint_t n;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	for (cp = fmd_list_next(&mp->mod_cases); cp; cp = fmd_list_next(cp))
7020Sstevel@tonic-gate 		fmd_ckpt_resv_case(ckp, cp);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	n = fmd_serd_hash_count(&mp->mod_serds);
7050Sstevel@tonic-gate 	fmd_serd_hash_apply(&mp->mod_serds,
7060Sstevel@tonic-gate 	    (fmd_serd_eng_f *)fmd_ckpt_resv_serd, ckp);
7070Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, sizeof (fcf_serd_t) * n, sizeof (uint64_t));
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	n = fmd_buf_hash_count(&mp->mod_bufs);
7100Sstevel@tonic-gate 	fmd_buf_hash_apply(&mp->mod_bufs, (fmd_buf_f *)fmd_ckpt_resv_buf, ckp);
7110Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, sizeof (fcf_buf_t) * n, sizeof (uint32_t));
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	fmd_ckpt_resv(ckp, sizeof (fcf_module_t), sizeof (uint32_t));
7140Sstevel@tonic-gate 	ckp->ckp_strn += strlen(mp->mod_name) + 1;
7150Sstevel@tonic-gate 	ckp->ckp_strn += strlen(mp->mod_path) + 1;
7160Sstevel@tonic-gate 	ckp->ckp_strn += strlen(mp->mod_info->fmdi_desc) + 1;
7170Sstevel@tonic-gate 	ckp->ckp_strn += strlen(mp->mod_info->fmdi_vers) + 1;
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static void
fmd_ckpt_save_module(fmd_ckpt_t * ckp,fmd_module_t * mp)7210Sstevel@tonic-gate fmd_ckpt_save_module(fmd_ckpt_t *ckp, fmd_module_t *mp)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	fcf_secidx_t bufsec = FCF_SECIDX_NONE;
7240Sstevel@tonic-gate 	fcf_module_t fcfm;
7250Sstevel@tonic-gate 	fmd_case_t *cp;
7260Sstevel@tonic-gate 	uint_t n;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	for (cp = fmd_list_next(&mp->mod_cases); cp; cp = fmd_list_next(cp))
7290Sstevel@tonic-gate 		fmd_ckpt_save_case(ckp, cp);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if ((n = fmd_serd_hash_count(&mp->mod_serds)) != 0) {
7320Sstevel@tonic-gate 		size_t size = sizeof (fcf_serd_t) * n;
7330Sstevel@tonic-gate 		fcf_serd_t *serds = ckp->ckp_arg = fmd_alloc(size, FMD_SLEEP);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		fmd_serd_hash_apply(&mp->mod_serds,
7360Sstevel@tonic-gate 		    (fmd_serd_eng_f *)fmd_ckpt_save_serd, ckp);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		(void) fmd_ckpt_section(ckp, serds, FCF_SECT_SERD, size);
7390Sstevel@tonic-gate 		fmd_free(serds, size);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if ((n = fmd_buf_hash_count(&mp->mod_bufs)) != 0) {
7430Sstevel@tonic-gate 		size_t size = sizeof (fcf_buf_t) * n;
7440Sstevel@tonic-gate 		fcf_buf_t *bufs = ckp->ckp_arg = fmd_alloc(size, FMD_SLEEP);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		fmd_buf_hash_apply(&mp->mod_bufs,
7470Sstevel@tonic-gate 		    (fmd_buf_f *)fmd_ckpt_save_buf, ckp);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		bufsec = fmd_ckpt_section(ckp, bufs, FCF_SECT_BUFS, size);
7500Sstevel@tonic-gate 		fmd_free(bufs, size);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	fcfm.fcfm_name = fmd_ckpt_string(ckp, mp->mod_name);
7540Sstevel@tonic-gate 	fcfm.fcfm_path = fmd_ckpt_string(ckp, mp->mod_path);
7550Sstevel@tonic-gate 	fcfm.fcfm_desc = fmd_ckpt_string(ckp, mp->mod_info->fmdi_desc);
7560Sstevel@tonic-gate 	fcfm.fcfm_vers = fmd_ckpt_string(ckp, mp->mod_info->fmdi_vers);
7570Sstevel@tonic-gate 	fcfm.fcfm_bufs = bufsec;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	(void) fmd_ckpt_section(ckp, &fcfm,
7600Sstevel@tonic-gate 	    FCF_SECT_MODULE, sizeof (fcf_module_t));
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate void
fmd_ckpt_save(fmd_module_t * mp)7640Sstevel@tonic-gate fmd_ckpt_save(fmd_module_t *mp)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	struct stat64 st;
7670Sstevel@tonic-gate 	char path[PATH_MAX];
7680Sstevel@tonic-gate 	mode_t dirmode;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	hrtime_t now = gethrtime();
7710Sstevel@tonic-gate 	fmd_ckpt_t ckp;
7720Sstevel@tonic-gate 	int err;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	ASSERT(fmd_module_locked(mp));
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/*
7770Sstevel@tonic-gate 	 * If checkpointing is disabled for the module, just return.  We must
7780Sstevel@tonic-gate 	 * commit the module state anyway to transition pending log events.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	if (mp->mod_stats->ms_ckpt_save.fmds_value.bool == FMD_B_FALSE) {
7810Sstevel@tonic-gate 		fmd_module_commit(mp);
7820Sstevel@tonic-gate 		return;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (!(mp->mod_flags & (FMD_MOD_MDIRTY | FMD_MOD_CDIRTY)))
7860Sstevel@tonic-gate 		return; /* no checkpoint is necessary for this module */
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "ckpt save begin %s %llu",
7890Sstevel@tonic-gate 	    mp->mod_name, mp->mod_gen + 1));
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * If the per-module checkpoint directory isn't found or isn't of type
7930Sstevel@tonic-gate 	 * directory, move aside whatever is there (if anything) and attempt
7940Sstevel@tonic-gate 	 * to mkdir(2) a new module checkpoint directory.  If this fails, we
7950Sstevel@tonic-gate 	 * have no choice but to abort the checkpoint and try again later.
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	if (stat64(mp->mod_ckpt, &st) != 0 || !S_ISDIR(st.st_mode)) {
7980Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s-", mp->mod_ckpt);
7990Sstevel@tonic-gate 		(void) rename(mp->mod_ckpt, path);
8000Sstevel@tonic-gate 		(void) fmd_conf_getprop(fmd.d_conf, "ckpt.dirmode", &dirmode);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		if (mkdir(mp->mod_ckpt, dirmode) != 0) {
8030Sstevel@tonic-gate 			fmd_error(EFMD_CKPT_MKDIR,
8040Sstevel@tonic-gate 			    "failed to mkdir %s", mp->mod_ckpt);
8050Sstevel@tonic-gate 			return; /* return without clearing dirty bits */
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Create a temporary file to write out the checkpoint into, and create
8110Sstevel@tonic-gate 	 * a fmd_ckpt_t structure to manage construction of the checkpoint.  We
8120Sstevel@tonic-gate 	 * then figure out how much space will be required, and allocate it.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	if (fmd_ckpt_create(&ckp, mp) == -1) {
8150Sstevel@tonic-gate 		fmd_error(EFMD_CKPT_CREATE, "failed to create %s", ckp.ckp_src);
8160Sstevel@tonic-gate 		return;
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	fmd_ckpt_resv_module(&ckp, mp);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (fmd_ckpt_alloc(&ckp, mp->mod_gen + 1) != 0) {
8220Sstevel@tonic-gate 		fmd_error(EFMD_CKPT_NOMEM, "failed to build %s", ckp.ckp_src);
8230Sstevel@tonic-gate 		fmd_ckpt_destroy(&ckp);
8240Sstevel@tonic-gate 		return;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * Fill in the checkpoint content, write it to disk, sync it, and then
8290Sstevel@tonic-gate 	 * atomically rename it to the destination path.  If this fails, we
8300Sstevel@tonic-gate 	 * have no choice but to leave all our dirty bits set and return.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	fmd_ckpt_save_module(&ckp, mp);
8330Sstevel@tonic-gate 	err = fmd_ckpt_commit(&ckp);
8340Sstevel@tonic-gate 	fmd_ckpt_destroy(&ckp);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (err != 0) {
8370Sstevel@tonic-gate 		fmd_error(EFMD_CKPT_COMMIT, "failed to commit %s", ckp.ckp_dst);
8380Sstevel@tonic-gate 		return; /* return without clearing dirty bits */
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	fmd_module_commit(mp);
8420Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "ckpt save end %s", mp->mod_name));
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	mp->mod_stats->ms_ckpt_cnt.fmds_value.ui64++;
8450Sstevel@tonic-gate 	mp->mod_stats->ms_ckpt_time.fmds_value.ui64 += gethrtime() - now;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	fmd_dprintf(FMD_DBG_CKPT, "saved checkpoint of %s (%llu)\n",
8480Sstevel@tonic-gate 	    mp->mod_name, mp->mod_gen);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate  * Utility function to retrieve a pointer to a section's header and verify that
8530Sstevel@tonic-gate  * it is of the expected type or it is a FCF_SECT_NONE reference.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate static const fcf_sec_t *
fmd_ckpt_secptr(fmd_ckpt_t * ckp,fcf_secidx_t sid,uint_t type)8560Sstevel@tonic-gate fmd_ckpt_secptr(fmd_ckpt_t *ckp, fcf_secidx_t sid, uint_t type)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	const fcf_sec_t *sp = (void *)(ckp->ckp_buf +
8590Sstevel@tonic-gate 	    ckp->ckp_hdr->fcfh_secoff + ckp->ckp_hdr->fcfh_secsize * sid);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	return (sid < ckp->ckp_secs && (sp->fcfs_type == type ||
8620Sstevel@tonic-gate 	    sp->fcfs_type == FCF_SECT_NONE) ? sp : NULL);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * Utility function to retrieve the data pointer for a particular section.  The
8670Sstevel@tonic-gate  * validity of the header values has already been checked by fmd_ckpt_open().
8680Sstevel@tonic-gate  */
8690Sstevel@tonic-gate static const void *
fmd_ckpt_dataptr(fmd_ckpt_t * ckp,const fcf_sec_t * sp)8700Sstevel@tonic-gate fmd_ckpt_dataptr(fmd_ckpt_t *ckp, const fcf_sec_t *sp)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	return (ckp->ckp_buf + sp->fcfs_offset);
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate /*
8760Sstevel@tonic-gate  * Utility function to retrieve the end of the data region for a particular
8770Sstevel@tonic-gate  * section.  The validity of this value has been confirmed by fmd_ckpt_open().
8780Sstevel@tonic-gate  */
8790Sstevel@tonic-gate static const void *
fmd_ckpt_datalim(fmd_ckpt_t * ckp,const fcf_sec_t * sp)8800Sstevel@tonic-gate fmd_ckpt_datalim(fmd_ckpt_t *ckp, const fcf_sec_t *sp)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	return (ckp->ckp_buf + sp->fcfs_offset + sp->fcfs_size);
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * Utility function to retrieve a string pointer (fcf_stridx_t).  If the string
8870Sstevel@tonic-gate  * index is valid, the string data is returned; otherwise 'defstr' is returned.
8880Sstevel@tonic-gate  */
8890Sstevel@tonic-gate static const char *
fmd_ckpt_strptr(fmd_ckpt_t * ckp,fcf_stridx_t sid,const char * defstr)8900Sstevel@tonic-gate fmd_ckpt_strptr(fmd_ckpt_t *ckp, fcf_stridx_t sid, const char *defstr)
8910Sstevel@tonic-gate {
8920Sstevel@tonic-gate 	return (sid < ckp->ckp_strn ? ckp->ckp_strs + sid : defstr);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate static void
fmd_ckpt_restore_events(fmd_ckpt_t * ckp,fcf_secidx_t sid,void (* func)(void *,fmd_event_t *),void * arg)8960Sstevel@tonic-gate fmd_ckpt_restore_events(fmd_ckpt_t *ckp, fcf_secidx_t sid,
8970Sstevel@tonic-gate     void (*func)(void *, fmd_event_t *), void *arg)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	const fcf_event_t *fcfe;
9000Sstevel@tonic-gate 	const fcf_sec_t *sp;
9010Sstevel@tonic-gate 	fmd_timeval_t ftv;
9020Sstevel@tonic-gate 	fmd_log_t *lp, *errlp;
9030Sstevel@tonic-gate 	uint_t i, n;
9040Sstevel@tonic-gate 	uint32_t e_maj, e_min;
9050Sstevel@tonic-gate 	uint64_t e_ino;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if ((sp = fmd_ckpt_secptr(ckp, sid, FCF_SECT_EVENTS)) == NULL) {
9080Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
9090Sstevel@tonic-gate 		    "invalid link to section %u: expected events\n", sid);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (sp->fcfs_size == 0)
9130Sstevel@tonic-gate 		return; /* empty events section or type none */
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	fcfe = fmd_ckpt_dataptr(ckp, sp);
9160Sstevel@tonic-gate 	n = sp->fcfs_size / sp->fcfs_entsize;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/*
9190Sstevel@tonic-gate 	 * Hold the reader lock on log pointers to block log rotation during
9200Sstevel@tonic-gate 	 * the section restore so that we can safely insert refs to d_errlog.
9210Sstevel@tonic-gate 	 */
9220Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
9230Sstevel@tonic-gate 	errlp = fmd.d_errlog;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	e_maj = major(errlp->log_stat.st_dev);
9260Sstevel@tonic-gate 	e_min = minor(errlp->log_stat.st_dev);
9270Sstevel@tonic-gate 	e_ino = errlp->log_stat.st_ino;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
9305609Scy152378 		fmd_event_t *ep;
9315609Scy152378 
9320Sstevel@tonic-gate 		ftv.ftv_sec = fcfe->fcfe_todsec;
9330Sstevel@tonic-gate 		ftv.ftv_nsec = fcfe->fcfe_todnsec;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 		if (e_ino == fcfe->fcfe_inode &&
9360Sstevel@tonic-gate 		    e_maj == fcfe->fcfe_major &&
9370Sstevel@tonic-gate 		    e_min == fcfe->fcfe_minor)
9380Sstevel@tonic-gate 			lp = errlp;
9390Sstevel@tonic-gate 		else
9400Sstevel@tonic-gate 			lp = NULL;
9410Sstevel@tonic-gate 
9425609Scy152378 		ep = fmd_event_recreate(FMD_EVT_PROTOCOL,
9435609Scy152378 		    &ftv, NULL, NULL, lp, fcfe->fcfe_offset, 0);
9445609Scy152378 		fmd_event_hold(ep);
9455609Scy152378 		func(arg, ep);
9465609Scy152378 		fmd_event_rele(ep);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		fcfe = (fcf_event_t *)((uintptr_t)fcfe + sp->fcfs_entsize);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&fmd.d_log_lock);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9541193Smws static int
fmd_ckpt_restore_suspects(fmd_ckpt_t * ckp,fmd_case_t * cp,fcf_secidx_t sid)9550Sstevel@tonic-gate fmd_ckpt_restore_suspects(fmd_ckpt_t *ckp, fmd_case_t *cp, fcf_secidx_t sid)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	const fcf_nvl_t *fcfn, *endn;
9580Sstevel@tonic-gate 	const fcf_sec_t *sp;
9590Sstevel@tonic-gate 	nvlist_t *nvl;
9600Sstevel@tonic-gate 	int err, i;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	if ((sp = fmd_ckpt_secptr(ckp, sid, FCF_SECT_NVLISTS)) == NULL) {
9630Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
9640Sstevel@tonic-gate 		    "invalid link to section %u: expected nvlists\n", sid);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	fcfn = fmd_ckpt_dataptr(ckp, sp);
9680Sstevel@tonic-gate 	endn = fmd_ckpt_datalim(ckp, sp);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	for (i = 0; fcfn < endn; i++) {
9710Sstevel@tonic-gate 		char *data = (char *)fcfn + sp->fcfs_entsize;
9720Sstevel@tonic-gate 		size_t size = (size_t)fcfn->fcfn_size;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		if (fcfn->fcfn_size > (size_t)((char *)endn - data)) {
9750Sstevel@tonic-gate 			fmd_ckpt_error(ckp, EFMD_CKPT_INVAL, "nvlist %u [%d] "
9760Sstevel@tonic-gate 			    "size %u exceeds buffer\n", sid, i, size);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 		if ((err = nvlist_xunpack(data, size, &nvl, &fmd.d_nva)) != 0) {
9800Sstevel@tonic-gate 			fmd_ckpt_error(ckp, EFMD_CKPT_INVAL, "failed to "
9810Sstevel@tonic-gate 			    "unpack nvlist %u [%d]: %s\n", sid, i,
9820Sstevel@tonic-gate 			    fmd_strerror(err));
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		fmd_case_insert_suspect(cp, nvl);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		size = sp->fcfs_entsize + fcfn->fcfn_size;
9880Sstevel@tonic-gate 		size = P2ROUNDUP(size, sizeof (uint64_t));
9890Sstevel@tonic-gate 		fcfn = (fcf_nvl_t *)((uintptr_t)fcfn + size);
9900Sstevel@tonic-gate 	}
9911193Smws 
9921193Smws 	return (i);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate static void
fmd_ckpt_restore_bufs(fmd_ckpt_t * ckp,fmd_module_t * mp,fmd_case_t * cp,fcf_secidx_t sid)9960Sstevel@tonic-gate fmd_ckpt_restore_bufs(fmd_ckpt_t *ckp, fmd_module_t *mp,
9970Sstevel@tonic-gate     fmd_case_t *cp, fcf_secidx_t sid)
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	const fcf_sec_t *sp, *dsp;
10000Sstevel@tonic-gate 	const fcf_buf_t *fcfb;
10010Sstevel@tonic-gate 	uint_t i, n;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if ((sp = fmd_ckpt_secptr(ckp, sid, FCF_SECT_BUFS)) == NULL) {
10040Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
10050Sstevel@tonic-gate 		    "invalid link to section %u: expected bufs\n", sid);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (sp->fcfs_size == 0)
10090Sstevel@tonic-gate 		return; /* empty events section or type none */
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	fcfb = fmd_ckpt_dataptr(ckp, sp);
10120Sstevel@tonic-gate 	n = sp->fcfs_size / sp->fcfs_entsize;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
10150Sstevel@tonic-gate 		dsp = fmd_ckpt_secptr(ckp, fcfb->fcfb_data, FCF_SECT_BUFFER);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 		if (dsp == NULL) {
10180Sstevel@tonic-gate 			fmd_ckpt_error(ckp, EFMD_CKPT_INVAL, "invalid %u "
10190Sstevel@tonic-gate 			    "buffer link %u\n", sid, fcfb->fcfb_data);
10200Sstevel@tonic-gate 		}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 		fmd_buf_write((fmd_hdl_t *)mp, cp,
10230Sstevel@tonic-gate 		    fmd_ckpt_strptr(ckp, fcfb->fcfb_name, "<CORRUPT>"),
10240Sstevel@tonic-gate 		    ckp->ckp_buf + dsp->fcfs_offset, dsp->fcfs_size);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		fcfb = (fcf_buf_t *)((uintptr_t)fcfb + sp->fcfs_entsize);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate static void
fmd_ckpt_restore_case(fmd_ckpt_t * ckp,fmd_module_t * mp,const fcf_sec_t * sp)10310Sstevel@tonic-gate fmd_ckpt_restore_case(fmd_ckpt_t *ckp, fmd_module_t *mp, const fcf_sec_t *sp)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	const fcf_case_t *fcfc = fmd_ckpt_dataptr(ckp, sp);
10340Sstevel@tonic-gate 	const char *uuid = fmd_ckpt_strptr(ckp, fcfc->fcfc_uuid, NULL);
10350Sstevel@tonic-gate 	fmd_case_t *cp;
10361193Smws 	int n;
10370Sstevel@tonic-gate 
10381193Smws 	if (uuid == NULL || fcfc->fcfc_state > FCF_CASE_CLOSE_WAIT) {
10390Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL, "corrupt %u case uuid "
10400Sstevel@tonic-gate 		    "and/or state\n", (uint_t)(sp - ckp->ckp_secp));
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	fmd_module_lock(mp);
10440Sstevel@tonic-gate 
10451193Smws 	if ((cp = fmd_case_recreate(mp, NULL,
10466228Sstephh 	    fcfc->fcfc_state != FCF_CASE_UNSOLVED ? FCF_CASE_SOLVED :
10471193Smws 	    FMD_CASE_UNSOLVED, uuid, NULL)) == NULL) {
10480Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
10490Sstevel@tonic-gate 		    "duplicate case uuid: %s\n", uuid);
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	fmd_ckpt_restore_events(ckp, fcfc->fcfc_principal,
10530Sstevel@tonic-gate 	    (void (*)(void *, fmd_event_t *))fmd_case_insert_principal, cp);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	fmd_ckpt_restore_events(ckp, fcfc->fcfc_events,
10560Sstevel@tonic-gate 	    (void (*)(void *, fmd_event_t *))fmd_case_insert_event, cp);
10570Sstevel@tonic-gate 
10587913SStephen.Hanson@Sun.COM 	/*
10597913SStephen.Hanson@Sun.COM 	 * Once solved, treat suspects from resource cache as master copy.
1060*9120SStephen.Hanson@Sun.COM 	 *
1061*9120SStephen.Hanson@Sun.COM 	 * If !fmd.d_running, this module must be a builtin, and so we don't
1062*9120SStephen.Hanson@Sun.COM 	 * want to restore suspects or call fmd_case_transition_update() at this
1063*9120SStephen.Hanson@Sun.COM 	 * stage. The suspects will be added later from the resource cache.
1064*9120SStephen.Hanson@Sun.COM 	 * Calling fmd_case_transition("SOLVED") is OK here as the state is
1065*9120SStephen.Hanson@Sun.COM 	 * already solved, so all it does is update the case flags.
10667913SStephen.Hanson@Sun.COM 	 */
1067*9120SStephen.Hanson@Sun.COM 	if (fmd.d_running && (n = ((fmd_case_impl_t *)cp)->ci_nsuspects) == 0)
10687913SStephen.Hanson@Sun.COM 		n = fmd_ckpt_restore_suspects(ckp, cp, fcfc->fcfc_suspects);
10690Sstevel@tonic-gate 
1070*9120SStephen.Hanson@Sun.COM 	if (!fmd.d_running)
1071*9120SStephen.Hanson@Sun.COM 		fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
1072*9120SStephen.Hanson@Sun.COM 	else if (fcfc->fcfc_state == FCF_CASE_SOLVED)
10731429Smws 		fmd_case_transition_update(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
10741193Smws 	else if (fcfc->fcfc_state == FCF_CASE_CLOSE_WAIT && n != 0)
10751193Smws 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_SOLVED);
10761193Smws 	else if (fcfc->fcfc_state == FCF_CASE_CLOSE_WAIT && n == 0)
10771193Smws 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, 0);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	fmd_module_unlock(mp);
10800Sstevel@tonic-gate 	fmd_ckpt_restore_bufs(ckp, mp, cp, fcfc->fcfc_bufs);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate static void
fmd_ckpt_restore_serd(fmd_ckpt_t * ckp,fmd_module_t * mp,const fcf_sec_t * sp)10840Sstevel@tonic-gate fmd_ckpt_restore_serd(fmd_ckpt_t *ckp, fmd_module_t *mp, const fcf_sec_t *sp)
10850Sstevel@tonic-gate {
10860Sstevel@tonic-gate 	const fcf_serd_t *fcfd = fmd_ckpt_dataptr(ckp, sp);
10870Sstevel@tonic-gate 	uint_t i, n = sp->fcfs_size / sp->fcfs_entsize;
10880Sstevel@tonic-gate 	const fcf_sec_t *esp;
10890Sstevel@tonic-gate 	const char *s;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
10920Sstevel@tonic-gate 		esp = fmd_ckpt_secptr(ckp, fcfd->fcfd_events, FCF_SECT_EVENTS);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		if (esp == NULL) {
10950Sstevel@tonic-gate 			fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
10960Sstevel@tonic-gate 			    "invalid events link %u\n", fcfd->fcfd_events);
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		if ((s = fmd_ckpt_strptr(ckp, fcfd->fcfd_name, NULL)) == NULL) {
11000Sstevel@tonic-gate 			fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
11010Sstevel@tonic-gate 			    "serd name %u is corrupt\n", fcfd->fcfd_name);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 		fmd_serd_create((fmd_hdl_t *)mp, s, fcfd->fcfd_n, fcfd->fcfd_t);
11050Sstevel@tonic-gate 		fmd_module_lock(mp);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		fmd_ckpt_restore_events(ckp, fcfd->fcfd_events,
11080Sstevel@tonic-gate 		    (void (*)(void *, fmd_event_t *))fmd_serd_eng_record,
11090Sstevel@tonic-gate 		    fmd_serd_eng_lookup(&mp->mod_serds, s));
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		fmd_module_unlock(mp);
11120Sstevel@tonic-gate 		fcfd = (fcf_serd_t *)((uintptr_t)fcfd + sp->fcfs_entsize);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate static void
fmd_ckpt_restore_module(fmd_ckpt_t * ckp,fmd_module_t * mp)11170Sstevel@tonic-gate fmd_ckpt_restore_module(fmd_ckpt_t *ckp, fmd_module_t *mp)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	const fcf_module_t *fcfm = fmd_ckpt_dataptr(ckp, ckp->ckp_modp);
11200Sstevel@tonic-gate 	const fcf_sec_t *sp;
11210Sstevel@tonic-gate 	uint_t i;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (strcmp(mp->mod_name, fmd_ckpt_strptr(ckp, fcfm->fcfm_name, "")) ||
11240Sstevel@tonic-gate 	    strcmp(mp->mod_path, fmd_ckpt_strptr(ckp, fcfm->fcfm_path, ""))) {
11250Sstevel@tonic-gate 		fmd_ckpt_error(ckp, EFMD_CKPT_INVAL,
11260Sstevel@tonic-gate 		    "checkpoint is not for module %s\n", mp->mod_name);
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	for (i = 0; i < ckp->ckp_secs; i++) {
11300Sstevel@tonic-gate 		sp = (void *)(ckp->ckp_buf +
11310Sstevel@tonic-gate 		    ckp->ckp_hdr->fcfh_secoff + ckp->ckp_hdr->fcfh_secsize * i);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 		switch (sp->fcfs_type) {
11340Sstevel@tonic-gate 		case FCF_SECT_CASE:
11350Sstevel@tonic-gate 			fmd_ckpt_restore_case(ckp, mp, sp);
11360Sstevel@tonic-gate 			break;
11370Sstevel@tonic-gate 		case FCF_SECT_SERD:
11380Sstevel@tonic-gate 			fmd_ckpt_restore_serd(ckp, mp, sp);
11390Sstevel@tonic-gate 			break;
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	fmd_ckpt_restore_bufs(ckp, mp, NULL, fcfm->fcfm_bufs);
11440Sstevel@tonic-gate 	mp->mod_gen = ckp->ckp_hdr->fcfh_cgen;
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * Restore a checkpoint for the specified module.  Any errors which occur
11490Sstevel@tonic-gate  * during restore will call fmd_ckpt_error() or trigger an fmd_api_error(),
11500Sstevel@tonic-gate  * either of which will automatically unlock the module and trigger an abort.
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate void
fmd_ckpt_restore(fmd_module_t * mp)11530Sstevel@tonic-gate fmd_ckpt_restore(fmd_module_t *mp)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	fmd_ckpt_t ckp;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if (mp->mod_stats->ms_ckpt_restore.fmds_value.bool == FMD_B_FALSE)
11580Sstevel@tonic-gate 		return; /* never restore checkpoints for this module */
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "ckpt restore begin %s", mp->mod_name));
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	if (fmd_ckpt_open(&ckp, mp) == -1) {
11630Sstevel@tonic-gate 		if (errno != ENOENT)
11640Sstevel@tonic-gate 			fmd_error(EFMD_CKPT_OPEN, "can't open %s", ckp.ckp_src);
11650Sstevel@tonic-gate 		TRACE((FMD_DBG_CKPT, "ckpt restore end %s", mp->mod_name));
11660Sstevel@tonic-gate 		return;
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	ASSERT(!fmd_module_locked(mp));
11700Sstevel@tonic-gate 	fmd_ckpt_restore_module(&ckp, mp);
11710Sstevel@tonic-gate 	fmd_ckpt_destroy(&ckp);
11720Sstevel@tonic-gate 	fmd_module_clrdirty(mp);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "ckpt restore end %s", mp->mod_name));
11750Sstevel@tonic-gate 	fmd_dprintf(FMD_DBG_CKPT, "restored checkpoint of %s\n", mp->mod_name);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate  * Delete the module's checkpoint file.  This is used by the ckpt.zero property
11800Sstevel@tonic-gate  * code or by the fmadm reset RPC service path to force a checkpoint delete.
11810Sstevel@tonic-gate  */
11820Sstevel@tonic-gate void
fmd_ckpt_delete(fmd_module_t * mp)11830Sstevel@tonic-gate fmd_ckpt_delete(fmd_module_t *mp)
11840Sstevel@tonic-gate {
11850Sstevel@tonic-gate 	char path[PATH_MAX];
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path),
11880Sstevel@tonic-gate 	    "%s/%s", mp->mod_ckpt, mp->mod_name);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "delete %s ckpt", mp->mod_name));
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (unlink(path) != 0 && errno != ENOENT)
11930Sstevel@tonic-gate 		fmd_error(EFMD_CKPT_DELETE, "failed to delete %s", path);
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate /*
11970Sstevel@tonic-gate  * Move aside the module's checkpoint file if checkpoint restore has failed.
11980Sstevel@tonic-gate  * We rename the file rather than deleting it in the hopes that someone might
11990Sstevel@tonic-gate  * send it to us for post-mortem analysis of whether we have a checkpoint bug.
12000Sstevel@tonic-gate  */
12010Sstevel@tonic-gate void
fmd_ckpt_rename(fmd_module_t * mp)12020Sstevel@tonic-gate fmd_ckpt_rename(fmd_module_t *mp)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate 	char src[PATH_MAX], dst[PATH_MAX];
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	(void) snprintf(src, sizeof (src), "%s/%s", mp->mod_ckpt, mp->mod_name);
12070Sstevel@tonic-gate 	(void) snprintf(dst, sizeof (dst), "%s-", src);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	TRACE((FMD_DBG_CKPT, "rename %s ckpt", mp->mod_name));
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if (rename(src, dst) != 0 && errno != ENOENT)
12120Sstevel@tonic-gate 		fmd_error(EFMD_CKPT_DELETE, "failed to rename %s", src);
12130Sstevel@tonic-gate }
1214