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.
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.
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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
29 #include "../common/sw_impl.h"
32 * We maintain a single list of all active cases across all
33 * subsidary diagnosis "modules". We also offer some serialization
36 * To open a new case a subsidiary engine should use swde_case_open
37 * indicating the subsidiary id (from which we lookup the enum sw_casetype)
38 * and, optionally, a pointer to a structure for serialization and its size.
40 * For each case opened with swde_case_open we maintain an swde_case_t
41 * structure in-core. Embedded in this is the swde_case_data_t with
42 * information we need to keep track of and manage this case - it's
43 * case type, buffer name used for the sub-de-private data (if any)
44 * and the size of the sub-de-private structure. It is this
45 * embedded structure which is serialized as the "casedata" buffer,
46 * while the subsidiary-private structure is serialized into another buffer
47 * "casedata_<casetype-in-hex>".
49 * The subsidiary-private data structure, if any, is required to start
50 * with a uint32_t recording the data structure version. This
51 * version is also specified as an argument to swde_case_open, and
52 * we note it in the "casedata" buffer we write out and require
55 * When we unserialize we restore our management structure as well as
56 * the sub-de-private structure.
58 * Here's how serialization works:
60 * In swde_case_open we create a case data buffer for the case named
61 * SW_CASE_DATA_BUFNAME. We write the buffer out after filling in the
62 * structure version and recording the type of case this is, and if there
63 * is data for the subsidiary then we call swde_subdata to note the
64 * size and version of that data in the case data buf and then to create
65 * and write the subdata in a buffer named SW_CASE_DATA_BUFNAME_<casetype>.
67 * If the subsidiary updates its case data it is required to call
68 * swde_case_data_write. This just calls fmd_buf_write for the subsidiary
71 * A subsidiary can retrieve its private data buffer for a case using
72 * swde_case_data. This also fills a uint32_t with the version of the
73 * buffer that we have for this subsidiary; if that is an old version
74 * the subsidiary can cast appropriately and/or upgrade the buffer as
77 * When the host module is reloaded it calls swde_case_init to iterate
78 * through all cases we own. For each we call swde_case_unserialize
79 * which restores our case tracking data and any subsidiary-private
80 * data that our case data notes. We then call swde_case_verify which
81 * calls any registered verify function in the subsidiary owner, and if this
82 * returns 0 the case is closed.
84 * After initial write, we don't usually have to update the
85 * SW_CASE_DATA_BUFNAME buffer unless the subsidiary changes the size or
86 * version of its private buffer. To do that the subsidiary must call
87 * swde_case_data_upgrade. In that function we destroy the old subsidiary
88 * buffer and, if there is still a subsidiary data structure, create a
89 * new buffer appropriately sized and call swde_subdata to write it out
90 * after updating our case structure with new size etc. Finally we write
91 * out our updated case data structure.
94 #define SW_CASE_DATA_BUFNAME "casedata"
96 #define SW_CASE_DATA_VERSION_INITIAL 1
97 #define SW_CASE_DATA_BUFNAMELEN 18 /* 8 + 1 + 8 + 1 */
98 typedef struct swde_case_data
{
99 uint32_t sc_version
; /* buffer structure version */
100 int32_t sc_type
; /* enum sw_casetype */
101 uint32_t sc_sub_bufvers
; /* version expected in subsidiary */
102 char sc_sub_bufname
[SW_CASE_DATA_BUFNAMELEN
]; /* subsidiary bufname */
103 int32_t sc_sub_bufsz
; /* subsidiary structure size */
106 #define SW_CASE_DATA_VERSION SW_CASE_DATA_VERSION_INITIAL
109 * In-core case structure.
111 typedef struct swde_case
{
112 fmd_case_t
*swc_fmdcase
; /* fmd case handle */
113 swde_case_data_t swc_data
; /* case data for serialization */
114 void *swc_subdata
; /* subsidiary data for serialization */
118 swde_case_associate(fmd_hdl_t
*hdl
, fmd_case_t
*cp
, swde_case_t
*scp
,
121 scp
->swc_fmdcase
= cp
;
122 scp
->swc_subdata
= subdata
;
123 fmd_case_setspecific(hdl
, cp
, scp
);
127 swde_case_unserialize(fmd_hdl_t
*hdl
, fmd_case_t
*cp
)
130 swde_case_data_t
*datap
;
134 scp
= fmd_hdl_zalloc(hdl
, sizeof (*scp
), FMD_SLEEP
);
135 datap
= &scp
->swc_data
;
137 fmd_buf_read(hdl
, cp
, SW_CASE_DATA_BUFNAME
, datap
, sizeof (*datap
));
139 if (datap
->sc_version
> SW_CASE_DATA_VERSION_INITIAL
) {
140 fmd_hdl_free(hdl
, scp
, sizeof (*scp
));
144 if ((sz
= datap
->sc_sub_bufsz
) != 0) {
145 subdata
= fmd_hdl_alloc(hdl
, sz
, FMD_SLEEP
);
146 fmd_buf_read(hdl
, cp
, datap
->sc_sub_bufname
, subdata
, sz
);
148 if (*((uint32_t *)subdata
) != datap
->sc_sub_bufvers
) {
149 fmd_hdl_abort(hdl
, "unserialize: expected subdata "
150 "version %u but received %u\n",
151 datap
->sc_sub_bufvers
, *((uint32_t *)subdata
));
155 swde_case_associate(hdl
, cp
, scp
, subdata
);
159 swde_subdata(fmd_hdl_t
*hdl
, fmd_case_t
*cp
, enum sw_casetype type
,
160 swde_case_t
*scp
, uint32_t subdata_vers
, void *subdata
, size_t subdata_sz
)
162 swde_case_data_t
*datap
= &scp
->swc_data
;
164 if (*((uint32_t *)subdata
) != subdata_vers
)
165 fmd_hdl_abort(hdl
, "swde_subdata: subdata version "
166 "does not match argument\n");
168 (void) snprintf(datap
->sc_sub_bufname
, sizeof (datap
->sc_sub_bufname
),
169 "%s_%08x", SW_CASE_DATA_BUFNAME
, type
);
171 datap
->sc_sub_bufsz
= subdata_sz
;
172 datap
->sc_sub_bufvers
= subdata_vers
;
173 fmd_buf_create(hdl
, cp
, datap
->sc_sub_bufname
, subdata_sz
);
174 fmd_buf_write(hdl
, cp
, datap
->sc_sub_bufname
, subdata
, subdata_sz
);
178 swde_case_open(fmd_hdl_t
*hdl
, id_t who
, char *req_uuid
,
179 uint32_t subdata_vers
, void *subdata
, size_t subdata_sz
)
181 enum sw_casetype ct
= sw_id_to_casetype(hdl
, who
);
182 swde_case_data_t
*datap
;
186 if (ct
== SW_CASE_NONE
)
187 fmd_hdl_abort(hdl
, "swde_case_open for type SW_CASE_NONE\n");
189 if (subdata
!= NULL
&& subdata_sz
<= sizeof (uint32_t) ||
190 subdata_sz
!= 0 && subdata
== NULL
)
191 fmd_hdl_abort(hdl
, "swde_case_open: bad subdata\n", ct
);
193 scp
= fmd_hdl_zalloc(hdl
, sizeof (*scp
), FMD_SLEEP
);
194 datap
= &scp
->swc_data
;
196 if (req_uuid
== NULL
) {
197 cp
= fmd_case_open(hdl
, (void *)scp
);
199 cp
= fmd_case_open_uuid(hdl
, req_uuid
, (void *)scp
);
201 fmd_hdl_free(hdl
, scp
, sizeof (*scp
));
206 fmd_buf_create(hdl
, cp
, SW_CASE_DATA_BUFNAME
, sizeof (*datap
));
207 datap
->sc_version
= SW_CASE_DATA_VERSION_INITIAL
;
211 swde_subdata(hdl
, cp
, ct
, scp
, subdata_vers
, subdata
,
214 fmd_buf_write(hdl
, cp
, SW_CASE_DATA_BUFNAME
, datap
, sizeof (*datap
));
215 swde_case_associate(hdl
, cp
, scp
, subdata
);
221 * fmdo_close entry point for software-diagnosis
224 swde_close(fmd_hdl_t
*hdl
, fmd_case_t
*cp
)
226 swde_case_t
*scp
= fmd_case_getspecific(hdl
, cp
);
227 swde_case_data_t
*datap
= &scp
->swc_data
;
228 swsub_case_close_func_t
*closefunc
;
230 if ((closefunc
= sw_sub_case_close_func(hdl
, datap
->sc_type
)) != NULL
)
234 * Now that the sub-de has had a chance to clean up, do some ourselves.
235 * Note that we free the sub-de-private subdata structure.
238 if (scp
->swc_subdata
) {
239 fmd_hdl_free(hdl
, scp
->swc_subdata
, datap
->sc_sub_bufsz
);
240 fmd_buf_destroy(hdl
, cp
, datap
->sc_sub_bufname
);
243 fmd_buf_destroy(hdl
, cp
, SW_CASE_DATA_BUFNAME
);
245 fmd_hdl_free(hdl
, scp
, sizeof (*scp
));
249 swde_case_first(fmd_hdl_t
*hdl
, id_t who
)
251 enum sw_casetype ct
= sw_id_to_casetype(hdl
, who
);
255 if (ct
== SW_CASE_NONE
)
256 fmd_hdl_abort(hdl
, "swde_case_first for type SW_CASE_NONE\n");
258 for (cp
= fmd_case_next(hdl
, NULL
); cp
; cp
= fmd_case_next(hdl
, cp
)) {
259 scp
= fmd_case_getspecific(hdl
, cp
);
260 if (scp
->swc_data
.sc_type
== ct
)
268 swde_case_next(fmd_hdl_t
*hdl
, fmd_case_t
*lastcp
)
275 fmd_hdl_abort(hdl
, "swde_case_next called for NULL lastcp\n");
277 scp
= fmd_case_getspecific(hdl
, lastcp
);
278 ct
= scp
->swc_data
.sc_type
;
281 while ((cp
= fmd_case_next(hdl
, cp
)) != NULL
) {
282 scp
= fmd_case_getspecific(hdl
, cp
);
283 if (scp
->swc_data
.sc_type
== ct
)
291 swde_case_data(fmd_hdl_t
*hdl
, fmd_case_t
*cp
, uint32_t *svp
)
293 swde_case_t
*scp
= fmd_case_getspecific(hdl
, cp
);
294 swde_case_data_t
*datap
= &scp
->swc_data
;
296 if (svp
!= NULL
&& scp
->swc_subdata
)
297 *svp
= datap
->sc_sub_bufvers
;
299 return (scp
->swc_subdata
);
303 swde_case_data_write(fmd_hdl_t
*hdl
, fmd_case_t
*cp
)
305 swde_case_t
*scp
= fmd_case_getspecific(hdl
, cp
);
306 swde_case_data_t
*datap
= &scp
->swc_data
;
308 if (scp
->swc_subdata
== NULL
)
311 fmd_buf_write(hdl
, cp
, scp
->swc_data
.sc_sub_bufname
,
312 scp
->swc_subdata
, datap
->sc_sub_bufsz
);
316 swde_case_data_upgrade(fmd_hdl_t
*hdl
, fmd_case_t
*cp
, uint32_t subdata_vers
,
317 void *subdata
, size_t subdata_sz
)
319 swde_case_t
*scp
= fmd_case_getspecific(hdl
, cp
);
320 swde_case_data_t
*datap
= &scp
->swc_data
;
322 if (scp
->swc_subdata
) {
323 fmd_buf_destroy(hdl
, cp
, datap
->sc_sub_bufname
);
324 fmd_hdl_free(hdl
, scp
->swc_subdata
, datap
->sc_sub_bufsz
);
325 scp
->swc_subdata
= NULL
;
326 datap
->sc_sub_bufsz
= 0;
327 datap
->sc_sub_bufname
[0] = '\0';
330 if (subdata
!= NULL
) {
331 scp
->swc_subdata
= subdata
;
332 swde_subdata(hdl
, cp
, datap
->sc_type
, scp
, subdata_vers
,
333 subdata
, subdata_sz
);
336 fmd_buf_write(hdl
, scp
->swc_fmdcase
, SW_CASE_DATA_BUFNAME
,
337 datap
, sizeof (*datap
));
341 swde_case_verify(fmd_hdl_t
*hdl
, fmd_case_t
*cp
)
343 swde_case_t
*scp
= fmd_case_getspecific(hdl
, cp
);
344 swde_case_data_t
*datap
= &scp
->swc_data
;
345 sw_case_vrfy_func_t
*vrfy_func
;
347 if ((vrfy_func
= sw_sub_case_vrfy_func(hdl
, datap
->sc_type
)) != NULL
) {
348 if (vrfy_func(hdl
, cp
) == 0)
349 fmd_case_close(hdl
, cp
);
354 swde_case_init(fmd_hdl_t
*hdl
)
358 for (cp
= fmd_case_next(hdl
, NULL
); cp
; cp
= fmd_case_next(hdl
, cp
)) {
359 swde_case_unserialize(hdl
, cp
);
360 swde_case_verify(hdl
, cp
);
366 swde_case_fini(fmd_hdl_t
*hdl
)