xref: /titanic_51/usr/src/cmd/fm/modules/sun4v/generic-mem/gmem_fmri.c (revision 1529f529004c61fcfd0d95ab79b0f257d6ad4451)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include <errno.h>
28 #include <strings.h>
29 
30 #include <gmem_fmri.h>
31 #include <gmem.h>
32 
33 void
34 gmem_fmri_init(fmd_hdl_t *hdl, gmem_fmri_t *fmri, nvlist_t *nvl,
35     const char *fmt, ...)
36 {
37 	va_list ap;
38 
39 	va_start(ap, fmt);
40 	gmem_vbufname(fmri->fmri_packnm, sizeof (fmri->fmri_packnm), fmt, ap);
41 	va_end(ap);
42 
43 	if ((errno = nvlist_dup(nvl, &fmri->fmri_nvl, 0)) != 0 ||
44 	    (errno = nvlist_size(nvl, &fmri->fmri_packsz,
45 	    NV_ENCODE_NATIVE)) != 0)
46 		fmd_hdl_abort(hdl, "failed to copy fmri for fmri create");
47 
48 	fmri->fmri_packbuf = fmd_hdl_alloc(hdl, fmri->fmri_packsz, FMD_SLEEP);
49 
50 	if ((errno = nvlist_pack(nvl, &fmri->fmri_packbuf, &fmri->fmri_packsz,
51 	    NV_ENCODE_NATIVE, 0)) != 0)
52 		fmd_hdl_abort(hdl, "failed to pack fmri for fmri create");
53 
54 	gmem_fmri_write(hdl, fmri);
55 }
56 
57 void
58 gmem_fmri_fini(fmd_hdl_t *hdl, gmem_fmri_t *fmri, int destroy)
59 {
60 	if (destroy)
61 		fmd_buf_destroy(hdl, NULL, fmri->fmri_packnm);
62 
63 	fmd_hdl_free(hdl, fmri->fmri_packbuf, fmri->fmri_packsz);
64 	nvlist_free(fmri->fmri_nvl);
65 }
66 
67 void
68 gmem_fmri_restore(fmd_hdl_t *hdl, gmem_fmri_t *fmri)
69 {
70 	if (fmd_buf_size(hdl, NULL, fmri->fmri_packnm) == 0) {
71 		bzero(fmri, sizeof (gmem_fmri_t));
72 		return;
73 	}
74 
75 	if ((fmri->fmri_packbuf = gmem_buf_read(hdl, NULL, fmri->fmri_packnm,
76 	    fmri->fmri_packsz)) == NULL) {
77 		fmd_hdl_abort(hdl, "failed to read fmri buffer %s",
78 		    fmri->fmri_packnm);
79 	}
80 
81 	if (nvlist_unpack(fmri->fmri_packbuf, fmri->fmri_packsz,
82 	    &fmri->fmri_nvl, 0) != 0) {
83 		fmd_hdl_abort(hdl, "failed to unpack fmri buffer %s\n",
84 		    fmri->fmri_packnm);
85 	}
86 }
87 
88 void
89 gmem_fmri_write(fmd_hdl_t *hdl, gmem_fmri_t *fmri)
90 {
91 	size_t sz;
92 
93 	if ((sz = fmd_buf_size(hdl, NULL, fmri->fmri_packnm)) !=
94 	    fmri->fmri_packsz && sz != 0)
95 		fmd_buf_destroy(hdl, NULL, fmri->fmri_packnm);
96 
97 	fmd_buf_write(hdl, NULL, fmri->fmri_packnm, fmri->fmri_packbuf,
98 	    fmri->fmri_packsz);
99 }
100