1*f6e214c7SGavin Maltby /*
2*f6e214c7SGavin Maltby  * CDDL HEADER START
3*f6e214c7SGavin Maltby  *
4*f6e214c7SGavin Maltby  * The contents of this file are subject to the terms of the
5*f6e214c7SGavin Maltby  * Common Development and Distribution License (the "License").
6*f6e214c7SGavin Maltby  * You may not use this file except in compliance with the License.
7*f6e214c7SGavin Maltby  *
8*f6e214c7SGavin Maltby  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*f6e214c7SGavin Maltby  * or http://www.opensolaris.org/os/licensing.
10*f6e214c7SGavin Maltby  * See the License for the specific language governing permissions
11*f6e214c7SGavin Maltby  * and limitations under the License.
12*f6e214c7SGavin Maltby  *
13*f6e214c7SGavin Maltby  * When distributing Covered Code, include this CDDL HEADER in each
14*f6e214c7SGavin Maltby  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*f6e214c7SGavin Maltby  * If applicable, add the following below this CDDL HEADER, with the
16*f6e214c7SGavin Maltby  * fields enclosed by brackets "[]" replaced with your own identifying
17*f6e214c7SGavin Maltby  * information: Portions Copyright [yyyy] [name of copyright owner]
18*f6e214c7SGavin Maltby  *
19*f6e214c7SGavin Maltby  * CDDL HEADER END
20*f6e214c7SGavin Maltby  */
21*f6e214c7SGavin Maltby 
22*f6e214c7SGavin Maltby /*
23*f6e214c7SGavin Maltby  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*f6e214c7SGavin Maltby  */
25*f6e214c7SGavin Maltby 
26*f6e214c7SGavin Maltby #include <stddef.h>
27*f6e214c7SGavin Maltby #include <strings.h>
28*f6e214c7SGavin Maltby 
29*f6e214c7SGavin Maltby #include "../common/sw_impl.h"
30*f6e214c7SGavin Maltby 
31*f6e214c7SGavin Maltby /*
32*f6e214c7SGavin Maltby  * We maintain a single list of all active cases across all
33*f6e214c7SGavin Maltby  * subsidary diagnosis "modules".  We also offer some serialization
34*f6e214c7SGavin Maltby  * services to them.
35*f6e214c7SGavin Maltby  *
36*f6e214c7SGavin Maltby  * To open a new case a subsidiary engine should use swde_case_open
37*f6e214c7SGavin Maltby  * indicating the subsidiary id (from which we lookup the enum sw_casetype)
38*f6e214c7SGavin Maltby  * and, optionally, a pointer to a structure for serialization and its size.
39*f6e214c7SGavin Maltby  *
40*f6e214c7SGavin Maltby  * For each case opened with swde_case_open we maintain an swde_case_t
41*f6e214c7SGavin Maltby  * structure in-core.  Embedded in this is the swde_case_data_t with
42*f6e214c7SGavin Maltby  * information we need to keep track of and manage this case - it's
43*f6e214c7SGavin Maltby  * case type, buffer name used for the sub-de-private data (if any)
44*f6e214c7SGavin Maltby  * and the size of the sub-de-private structure.  It is this
45*f6e214c7SGavin Maltby  * embedded structure which is serialized as the "casedata" buffer,
46*f6e214c7SGavin Maltby  * while the subsidiary-private structure is serialized into another buffer
47*f6e214c7SGavin Maltby  * "casedata_<casetype-in-hex>".
48*f6e214c7SGavin Maltby  *
49*f6e214c7SGavin Maltby  * The subsidiary-private data structure, if any, is required to start
50*f6e214c7SGavin Maltby  * with a uint32_t recording the data structure version.  This
51*f6e214c7SGavin Maltby  * version is also specified as an argument to swde_case_open, and
52*f6e214c7SGavin Maltby  * we note it in the "casedata" buffer we write out and require
53*f6e214c7SGavin Maltby  * a match on restore.
54*f6e214c7SGavin Maltby  *
55*f6e214c7SGavin Maltby  * When we unserialize we restore our management structure as well as
56*f6e214c7SGavin Maltby  * the sub-de-private structure.
57*f6e214c7SGavin Maltby  *
58*f6e214c7SGavin Maltby  * Here's how serialization works:
59*f6e214c7SGavin Maltby  *
60*f6e214c7SGavin Maltby  * In swde_case_open we create a case data buffer for the case named
61*f6e214c7SGavin Maltby  * SW_CASE_DATA_BUFNAME.  We write the buffer out after filling in the
62*f6e214c7SGavin Maltby  * structure version and recording the type of case this is, and if there
63*f6e214c7SGavin Maltby  * is data for the subsidiary then we call swde_subdata to note the
64*f6e214c7SGavin Maltby  * size and version of that data in the case data buf and then to create
65*f6e214c7SGavin Maltby  * and write the subdata in a buffer named SW_CASE_DATA_BUFNAME_<casetype>.
66*f6e214c7SGavin Maltby  *
67*f6e214c7SGavin Maltby  * If the subsidiary updates its case data it is required to call
68*f6e214c7SGavin Maltby  * swde_case_data_write.  This just calls fmd_buf_write for the subsidiary
69*f6e214c7SGavin Maltby  * buffer name.
70*f6e214c7SGavin Maltby  *
71*f6e214c7SGavin Maltby  * A subsidiary can retrieve its private data buffer for a case using
72*f6e214c7SGavin Maltby  * swde_case_data.  This also fills a uint32_t with the version of the
73*f6e214c7SGavin Maltby  * buffer that we have for this subsidiary;  if that is an old version
74*f6e214c7SGavin Maltby  * the subsidiary can cast appropriately and/or upgrade the buffer as
75*f6e214c7SGavin Maltby  * below.
76*f6e214c7SGavin Maltby  *
77*f6e214c7SGavin Maltby  * When the host module is reloaded it calls swde_case_init to iterate
78*f6e214c7SGavin Maltby  * through all cases we own.  For each we call swde_case_unserialize
79*f6e214c7SGavin Maltby  * which restores our case tracking data and any subsidiary-private
80*f6e214c7SGavin Maltby  * data that our case data notes.  We then call swde_case_verify which
81*f6e214c7SGavin Maltby  * calls any registered verify function in the subsidiary owner, and if this
82*f6e214c7SGavin Maltby  * returns 0 the case is closed.
83*f6e214c7SGavin Maltby  *
84*f6e214c7SGavin Maltby  * After initial write, we don't usually have to update the
85*f6e214c7SGavin Maltby  * SW_CASE_DATA_BUFNAME buffer unless the subsidiary changes the size or
86*f6e214c7SGavin Maltby  * version of its private buffer.  To do that the subsidiary must call
87*f6e214c7SGavin Maltby  * swde_case_data_upgrade.  In that function we destroy the old subsidiary
88*f6e214c7SGavin Maltby  * buffer and, if there is still a subsidiary data structure, create a
89*f6e214c7SGavin Maltby  * new buffer appropriately sized and call swde_subdata to write it out
90*f6e214c7SGavin Maltby  * after updating our case structure with new size etc.  Finally we write
91*f6e214c7SGavin Maltby  * out our updated case data structure.
92*f6e214c7SGavin Maltby  */
93*f6e214c7SGavin Maltby 
94*f6e214c7SGavin Maltby #define	SW_CASE_DATA_BUFNAME		"casedata"
95*f6e214c7SGavin Maltby 
96*f6e214c7SGavin Maltby #define	SW_CASE_DATA_VERSION_INITIAL		1
97*f6e214c7SGavin Maltby #define	SW_CASE_DATA_BUFNAMELEN	18	/* 8 + 1 + 8 + 1 */
98*f6e214c7SGavin Maltby typedef struct swde_case_data {
99*f6e214c7SGavin Maltby 	uint32_t sc_version;		/* buffer structure version */
100*f6e214c7SGavin Maltby 	int32_t sc_type;		/* enum sw_casetype */
101*f6e214c7SGavin Maltby 	uint32_t sc_sub_bufvers;	/* version expected in subsidiary */
102*f6e214c7SGavin Maltby 	char sc_sub_bufname[SW_CASE_DATA_BUFNAMELEN];	/* subsidiary bufname */
103*f6e214c7SGavin Maltby 	int32_t sc_sub_bufsz;		/* subsidiary structure size */
104*f6e214c7SGavin Maltby } swde_case_data_t;
105*f6e214c7SGavin Maltby 
106*f6e214c7SGavin Maltby #define	SW_CASE_DATA_VERSION		SW_CASE_DATA_VERSION_INITIAL
107*f6e214c7SGavin Maltby 
108*f6e214c7SGavin Maltby /*
109*f6e214c7SGavin Maltby  * In-core case structure.
110*f6e214c7SGavin Maltby  */
111*f6e214c7SGavin Maltby typedef struct swde_case {
112*f6e214c7SGavin Maltby 	fmd_case_t *swc_fmdcase;	/* fmd case handle */
113*f6e214c7SGavin Maltby 	swde_case_data_t swc_data;	/* case data for serialization */
114*f6e214c7SGavin Maltby 	void *swc_subdata;		/* subsidiary data for serialization */
115*f6e214c7SGavin Maltby } swde_case_t;
116*f6e214c7SGavin Maltby 
117*f6e214c7SGavin Maltby static void
swde_case_associate(fmd_hdl_t * hdl,fmd_case_t * cp,swde_case_t * scp,void * subdata)118*f6e214c7SGavin Maltby swde_case_associate(fmd_hdl_t *hdl, fmd_case_t *cp, swde_case_t *scp,
119*f6e214c7SGavin Maltby     void *subdata)
120*f6e214c7SGavin Maltby {
121*f6e214c7SGavin Maltby 	scp->swc_fmdcase = cp;
122*f6e214c7SGavin Maltby 	scp->swc_subdata = subdata;
123*f6e214c7SGavin Maltby 	fmd_case_setspecific(hdl, cp, scp);
124*f6e214c7SGavin Maltby }
125*f6e214c7SGavin Maltby 
126*f6e214c7SGavin Maltby static void
swde_case_unserialize(fmd_hdl_t * hdl,fmd_case_t * cp)127*f6e214c7SGavin Maltby swde_case_unserialize(fmd_hdl_t *hdl, fmd_case_t *cp)
128*f6e214c7SGavin Maltby {
129*f6e214c7SGavin Maltby 	swde_case_t *scp;
130*f6e214c7SGavin Maltby 	swde_case_data_t *datap;
131*f6e214c7SGavin Maltby 	void *subdata;
132*f6e214c7SGavin Maltby 	size_t sz;
133*f6e214c7SGavin Maltby 
134*f6e214c7SGavin Maltby 	scp = fmd_hdl_zalloc(hdl, sizeof (*scp), FMD_SLEEP);
135*f6e214c7SGavin Maltby 	datap = &scp->swc_data;
136*f6e214c7SGavin Maltby 
137*f6e214c7SGavin Maltby 	fmd_buf_read(hdl, cp, SW_CASE_DATA_BUFNAME, datap, sizeof (*datap));
138*f6e214c7SGavin Maltby 
139*f6e214c7SGavin Maltby 	if (datap->sc_version > SW_CASE_DATA_VERSION_INITIAL) {
140*f6e214c7SGavin Maltby 		fmd_hdl_free(hdl, scp, sizeof (*scp));
141*f6e214c7SGavin Maltby 		return;
142*f6e214c7SGavin Maltby 	}
143*f6e214c7SGavin Maltby 
144*f6e214c7SGavin Maltby 	if ((sz = datap->sc_sub_bufsz) != 0) {
145*f6e214c7SGavin Maltby 		subdata = fmd_hdl_alloc(hdl, sz, FMD_SLEEP);
146*f6e214c7SGavin Maltby 		fmd_buf_read(hdl, cp, datap->sc_sub_bufname, subdata, sz);
147*f6e214c7SGavin Maltby 
148*f6e214c7SGavin Maltby 		if (*((uint32_t *)subdata) != datap->sc_sub_bufvers) {
149*f6e214c7SGavin Maltby 			fmd_hdl_abort(hdl, "unserialize: expected subdata "
150*f6e214c7SGavin Maltby 			    "version %u but received %u\n",
151*f6e214c7SGavin Maltby 			    datap->sc_sub_bufvers, *((uint32_t *)subdata));
152*f6e214c7SGavin Maltby 		}
153*f6e214c7SGavin Maltby 	}
154*f6e214c7SGavin Maltby 
155*f6e214c7SGavin Maltby 	swde_case_associate(hdl, cp, scp, subdata);
156*f6e214c7SGavin Maltby }
157*f6e214c7SGavin Maltby 
158*f6e214c7SGavin Maltby static void
swde_subdata(fmd_hdl_t * hdl,fmd_case_t * cp,enum sw_casetype type,swde_case_t * scp,uint32_t subdata_vers,void * subdata,size_t subdata_sz)159*f6e214c7SGavin Maltby swde_subdata(fmd_hdl_t *hdl, fmd_case_t *cp, enum sw_casetype type,
160*f6e214c7SGavin Maltby     swde_case_t *scp, uint32_t subdata_vers, void *subdata, size_t subdata_sz)
161*f6e214c7SGavin Maltby {
162*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
163*f6e214c7SGavin Maltby 
164*f6e214c7SGavin Maltby 	if (*((uint32_t *)subdata) != subdata_vers)
165*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "swde_subdata: subdata version "
166*f6e214c7SGavin Maltby 		    "does not match argument\n");
167*f6e214c7SGavin Maltby 
168*f6e214c7SGavin Maltby 	(void) snprintf(datap->sc_sub_bufname, sizeof (datap->sc_sub_bufname),
169*f6e214c7SGavin Maltby 	    "%s_%08x", SW_CASE_DATA_BUFNAME, type);
170*f6e214c7SGavin Maltby 
171*f6e214c7SGavin Maltby 	datap->sc_sub_bufsz = subdata_sz;
172*f6e214c7SGavin Maltby 	datap->sc_sub_bufvers = subdata_vers;
173*f6e214c7SGavin Maltby 	fmd_buf_create(hdl, cp, datap->sc_sub_bufname, subdata_sz);
174*f6e214c7SGavin Maltby 	fmd_buf_write(hdl, cp, datap->sc_sub_bufname, subdata, subdata_sz);
175*f6e214c7SGavin Maltby }
176*f6e214c7SGavin Maltby 
177*f6e214c7SGavin Maltby fmd_case_t *
swde_case_open(fmd_hdl_t * hdl,id_t who,char * req_uuid,uint32_t subdata_vers,void * subdata,size_t subdata_sz)178*f6e214c7SGavin Maltby swde_case_open(fmd_hdl_t *hdl, id_t who, char *req_uuid,
179*f6e214c7SGavin Maltby     uint32_t subdata_vers, void *subdata, size_t subdata_sz)
180*f6e214c7SGavin Maltby {
181*f6e214c7SGavin Maltby 	enum sw_casetype ct = sw_id_to_casetype(hdl, who);
182*f6e214c7SGavin Maltby 	swde_case_data_t *datap;
183*f6e214c7SGavin Maltby 	swde_case_t *scp;
184*f6e214c7SGavin Maltby 	fmd_case_t *cp;
185*f6e214c7SGavin Maltby 
186*f6e214c7SGavin Maltby 	if (ct == SW_CASE_NONE)
187*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "swde_case_open for type SW_CASE_NONE\n");
188*f6e214c7SGavin Maltby 
189*f6e214c7SGavin Maltby 	if (subdata != NULL && subdata_sz <= sizeof (uint32_t) ||
190*f6e214c7SGavin Maltby 	    subdata_sz != 0 && subdata == NULL)
191*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "swde_case_open: bad subdata\n", ct);
192*f6e214c7SGavin Maltby 
193*f6e214c7SGavin Maltby 	scp = fmd_hdl_zalloc(hdl, sizeof (*scp), FMD_SLEEP);
194*f6e214c7SGavin Maltby 	datap = &scp->swc_data;
195*f6e214c7SGavin Maltby 
196*f6e214c7SGavin Maltby 	if (req_uuid == NULL) {
197*f6e214c7SGavin Maltby 		cp = fmd_case_open(hdl, (void *)scp);
198*f6e214c7SGavin Maltby 	} else {
199*f6e214c7SGavin Maltby 		cp = fmd_case_open_uuid(hdl, req_uuid, (void *)scp);
200*f6e214c7SGavin Maltby 		if (cp == NULL) {
201*f6e214c7SGavin Maltby 			fmd_hdl_free(hdl, scp, sizeof (*scp));
202*f6e214c7SGavin Maltby 			return (NULL);
203*f6e214c7SGavin Maltby 		}
204*f6e214c7SGavin Maltby 	}
205*f6e214c7SGavin Maltby 
206*f6e214c7SGavin Maltby 	fmd_buf_create(hdl, cp, SW_CASE_DATA_BUFNAME, sizeof (*datap));
207*f6e214c7SGavin Maltby 	datap->sc_version = SW_CASE_DATA_VERSION_INITIAL;
208*f6e214c7SGavin Maltby 	datap->sc_type = ct;
209*f6e214c7SGavin Maltby 
210*f6e214c7SGavin Maltby 	if (subdata)
211*f6e214c7SGavin Maltby 		swde_subdata(hdl, cp, ct, scp, subdata_vers, subdata,
212*f6e214c7SGavin Maltby 		    subdata_sz);
213*f6e214c7SGavin Maltby 
214*f6e214c7SGavin Maltby 	fmd_buf_write(hdl, cp, SW_CASE_DATA_BUFNAME, datap, sizeof (*datap));
215*f6e214c7SGavin Maltby 	swde_case_associate(hdl, cp, scp, subdata);
216*f6e214c7SGavin Maltby 
217*f6e214c7SGavin Maltby 	return (cp);
218*f6e214c7SGavin Maltby }
219*f6e214c7SGavin Maltby 
220*f6e214c7SGavin Maltby /*
221*f6e214c7SGavin Maltby  * fmdo_close entry point for software-diagnosis
222*f6e214c7SGavin Maltby  */
223*f6e214c7SGavin Maltby void
swde_close(fmd_hdl_t * hdl,fmd_case_t * cp)224*f6e214c7SGavin Maltby swde_close(fmd_hdl_t *hdl, fmd_case_t *cp)
225*f6e214c7SGavin Maltby {
226*f6e214c7SGavin Maltby 	swde_case_t *scp = fmd_case_getspecific(hdl, cp);
227*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
228*f6e214c7SGavin Maltby 	swsub_case_close_func_t *closefunc;
229*f6e214c7SGavin Maltby 
230*f6e214c7SGavin Maltby 	if ((closefunc = sw_sub_case_close_func(hdl, datap->sc_type)) != NULL)
231*f6e214c7SGavin Maltby 		closefunc(hdl, cp);
232*f6e214c7SGavin Maltby 
233*f6e214c7SGavin Maltby 	/*
234*f6e214c7SGavin Maltby 	 * Now that the sub-de has had a chance to clean up, do some ourselves.
235*f6e214c7SGavin Maltby 	 * Note that we free the sub-de-private subdata structure.
236*f6e214c7SGavin Maltby 	 */
237*f6e214c7SGavin Maltby 
238*f6e214c7SGavin Maltby 	if (scp->swc_subdata) {
239*f6e214c7SGavin Maltby 		fmd_hdl_free(hdl, scp->swc_subdata, datap->sc_sub_bufsz);
240*f6e214c7SGavin Maltby 		fmd_buf_destroy(hdl, cp, datap->sc_sub_bufname);
241*f6e214c7SGavin Maltby 	}
242*f6e214c7SGavin Maltby 
243*f6e214c7SGavin Maltby 	fmd_buf_destroy(hdl, cp, SW_CASE_DATA_BUFNAME);
244*f6e214c7SGavin Maltby 
245*f6e214c7SGavin Maltby 	fmd_hdl_free(hdl, scp, sizeof (*scp));
246*f6e214c7SGavin Maltby }
247*f6e214c7SGavin Maltby 
248*f6e214c7SGavin Maltby fmd_case_t *
swde_case_first(fmd_hdl_t * hdl,id_t who)249*f6e214c7SGavin Maltby swde_case_first(fmd_hdl_t *hdl, id_t who)
250*f6e214c7SGavin Maltby {
251*f6e214c7SGavin Maltby 	enum sw_casetype ct = sw_id_to_casetype(hdl, who);
252*f6e214c7SGavin Maltby 	swde_case_t *scp;
253*f6e214c7SGavin Maltby 	fmd_case_t *cp;
254*f6e214c7SGavin Maltby 
255*f6e214c7SGavin Maltby 	if (ct == SW_CASE_NONE)
256*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "swde_case_first for type SW_CASE_NONE\n");
257*f6e214c7SGavin Maltby 
258*f6e214c7SGavin Maltby 	for (cp = fmd_case_next(hdl, NULL); cp; cp = fmd_case_next(hdl, cp)) {
259*f6e214c7SGavin Maltby 		scp = fmd_case_getspecific(hdl, cp);
260*f6e214c7SGavin Maltby 		if (scp->swc_data.sc_type == ct)
261*f6e214c7SGavin Maltby 			break;
262*f6e214c7SGavin Maltby 	}
263*f6e214c7SGavin Maltby 
264*f6e214c7SGavin Maltby 	return (cp);
265*f6e214c7SGavin Maltby }
266*f6e214c7SGavin Maltby 
267*f6e214c7SGavin Maltby fmd_case_t *
swde_case_next(fmd_hdl_t * hdl,fmd_case_t * lastcp)268*f6e214c7SGavin Maltby swde_case_next(fmd_hdl_t *hdl, fmd_case_t *lastcp)
269*f6e214c7SGavin Maltby {
270*f6e214c7SGavin Maltby 	swde_case_t *scp;
271*f6e214c7SGavin Maltby 	fmd_case_t *cp;
272*f6e214c7SGavin Maltby 	int ct;
273*f6e214c7SGavin Maltby 
274*f6e214c7SGavin Maltby 	if (lastcp == NULL)
275*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "swde_case_next called for NULL lastcp\n");
276*f6e214c7SGavin Maltby 
277*f6e214c7SGavin Maltby 	scp = fmd_case_getspecific(hdl, lastcp);
278*f6e214c7SGavin Maltby 	ct = scp->swc_data.sc_type;
279*f6e214c7SGavin Maltby 
280*f6e214c7SGavin Maltby 	cp = lastcp;
281*f6e214c7SGavin Maltby 	while ((cp = fmd_case_next(hdl, cp)) != NULL) {
282*f6e214c7SGavin Maltby 		scp = fmd_case_getspecific(hdl, cp);
283*f6e214c7SGavin Maltby 		if (scp->swc_data.sc_type == ct)
284*f6e214c7SGavin Maltby 			break;
285*f6e214c7SGavin Maltby 	}
286*f6e214c7SGavin Maltby 
287*f6e214c7SGavin Maltby 	return (cp);
288*f6e214c7SGavin Maltby }
289*f6e214c7SGavin Maltby 
290*f6e214c7SGavin Maltby void *
swde_case_data(fmd_hdl_t * hdl,fmd_case_t * cp,uint32_t * svp)291*f6e214c7SGavin Maltby swde_case_data(fmd_hdl_t *hdl, fmd_case_t *cp, uint32_t *svp)
292*f6e214c7SGavin Maltby {
293*f6e214c7SGavin Maltby 	swde_case_t *scp = fmd_case_getspecific(hdl, cp);
294*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
295*f6e214c7SGavin Maltby 
296*f6e214c7SGavin Maltby 	if (svp != NULL && scp->swc_subdata)
297*f6e214c7SGavin Maltby 		*svp = datap->sc_sub_bufvers;
298*f6e214c7SGavin Maltby 
299*f6e214c7SGavin Maltby 	return (scp->swc_subdata);
300*f6e214c7SGavin Maltby }
301*f6e214c7SGavin Maltby 
302*f6e214c7SGavin Maltby void
swde_case_data_write(fmd_hdl_t * hdl,fmd_case_t * cp)303*f6e214c7SGavin Maltby swde_case_data_write(fmd_hdl_t *hdl, fmd_case_t *cp)
304*f6e214c7SGavin Maltby {
305*f6e214c7SGavin Maltby 	swde_case_t *scp = fmd_case_getspecific(hdl, cp);
306*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
307*f6e214c7SGavin Maltby 
308*f6e214c7SGavin Maltby 	if (scp->swc_subdata == NULL)
309*f6e214c7SGavin Maltby 		return;
310*f6e214c7SGavin Maltby 
311*f6e214c7SGavin Maltby 	fmd_buf_write(hdl, cp, scp->swc_data.sc_sub_bufname,
312*f6e214c7SGavin Maltby 	    scp->swc_subdata, datap->sc_sub_bufsz);
313*f6e214c7SGavin Maltby }
314*f6e214c7SGavin Maltby 
315*f6e214c7SGavin Maltby void
swde_case_data_upgrade(fmd_hdl_t * hdl,fmd_case_t * cp,uint32_t subdata_vers,void * subdata,size_t subdata_sz)316*f6e214c7SGavin Maltby swde_case_data_upgrade(fmd_hdl_t *hdl, fmd_case_t *cp, uint32_t subdata_vers,
317*f6e214c7SGavin Maltby     void *subdata, size_t subdata_sz)
318*f6e214c7SGavin Maltby {
319*f6e214c7SGavin Maltby 	swde_case_t *scp = fmd_case_getspecific(hdl, cp);
320*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
321*f6e214c7SGavin Maltby 
322*f6e214c7SGavin Maltby 	if (scp->swc_subdata) {
323*f6e214c7SGavin Maltby 		fmd_buf_destroy(hdl, cp, datap->sc_sub_bufname);
324*f6e214c7SGavin Maltby 		fmd_hdl_free(hdl, scp->swc_subdata, datap->sc_sub_bufsz);
325*f6e214c7SGavin Maltby 		scp->swc_subdata = NULL;
326*f6e214c7SGavin Maltby 		datap->sc_sub_bufsz = 0;
327*f6e214c7SGavin Maltby 		datap->sc_sub_bufname[0] = '\0';
328*f6e214c7SGavin Maltby 	}
329*f6e214c7SGavin Maltby 
330*f6e214c7SGavin Maltby 	if (subdata != NULL) {
331*f6e214c7SGavin Maltby 		scp->swc_subdata = subdata;
332*f6e214c7SGavin Maltby 		swde_subdata(hdl, cp, datap->sc_type, scp, subdata_vers,
333*f6e214c7SGavin Maltby 		    subdata, subdata_sz);
334*f6e214c7SGavin Maltby 	}
335*f6e214c7SGavin Maltby 
336*f6e214c7SGavin Maltby 	fmd_buf_write(hdl, scp->swc_fmdcase, SW_CASE_DATA_BUFNAME,
337*f6e214c7SGavin Maltby 	    datap, sizeof (*datap));
338*f6e214c7SGavin Maltby }
339*f6e214c7SGavin Maltby 
340*f6e214c7SGavin Maltby static void
swde_case_verify(fmd_hdl_t * hdl,fmd_case_t * cp)341*f6e214c7SGavin Maltby swde_case_verify(fmd_hdl_t *hdl, fmd_case_t *cp)
342*f6e214c7SGavin Maltby {
343*f6e214c7SGavin Maltby 	swde_case_t *scp = fmd_case_getspecific(hdl, cp);
344*f6e214c7SGavin Maltby 	swde_case_data_t *datap = &scp->swc_data;
345*f6e214c7SGavin Maltby 	sw_case_vrfy_func_t *vrfy_func;
346*f6e214c7SGavin Maltby 
347*f6e214c7SGavin Maltby 	if ((vrfy_func = sw_sub_case_vrfy_func(hdl, datap->sc_type)) != NULL) {
348*f6e214c7SGavin Maltby 		if (vrfy_func(hdl, cp) == 0)
349*f6e214c7SGavin Maltby 			fmd_case_close(hdl, cp);
350*f6e214c7SGavin Maltby 	}
351*f6e214c7SGavin Maltby }
352*f6e214c7SGavin Maltby 
353*f6e214c7SGavin Maltby void
swde_case_init(fmd_hdl_t * hdl)354*f6e214c7SGavin Maltby swde_case_init(fmd_hdl_t *hdl)
355*f6e214c7SGavin Maltby {
356*f6e214c7SGavin Maltby 	fmd_case_t *cp;
357*f6e214c7SGavin Maltby 
358*f6e214c7SGavin Maltby 	for (cp = fmd_case_next(hdl, NULL); cp; cp = fmd_case_next(hdl, cp)) {
359*f6e214c7SGavin Maltby 		swde_case_unserialize(hdl, cp);
360*f6e214c7SGavin Maltby 		swde_case_verify(hdl, cp);
361*f6e214c7SGavin Maltby 	}
362*f6e214c7SGavin Maltby }
363*f6e214c7SGavin Maltby 
364*f6e214c7SGavin Maltby /*ARGSUSED*/
365*f6e214c7SGavin Maltby void
swde_case_fini(fmd_hdl_t * hdl)366*f6e214c7SGavin Maltby swde_case_fini(fmd_hdl_t *hdl)
367*f6e214c7SGavin Maltby {
368*f6e214c7SGavin Maltby }
369