xref: /titanic_44/usr/src/cmd/fm/fmd/common/fmd_asru.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/fm/protocol.h>
31 #include <uuid/uuid.h>
32 
33 #include <dirent.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <alloca.h>
37 
38 #include <fmd_alloc.h>
39 #include <fmd_string.h>
40 #include <fmd_error.h>
41 #include <fmd_subr.h>
42 #include <fmd_protocol.h>
43 #include <fmd_event.h>
44 #include <fmd_conf.h>
45 #include <fmd_fmri.h>
46 #include <fmd_dispq.h>
47 #include <fmd_case.h>
48 #include <fmd_module.h>
49 #include <fmd_asru.h>
50 
51 #include <fmd.h>
52 
53 static const char *const _fmd_asru_events[] = {
54 	FMD_RSRC_CLASS "asru.ok",		/* UNUSABLE=0 FAULTED=0 */
55 	FMD_RSRC_CLASS "asru.degraded",		/* UNUSABLE=0 FAULTED=1 */
56 	FMD_RSRC_CLASS "asru.unknown",		/* UNUSABLE=1 FAULTED=0 */
57 	FMD_RSRC_CLASS "asru.faulted"		/* UNUSABLE=1 FAULTED=1 */
58 };
59 
60 static const char *const _fmd_asru_snames[] = {
61 	"uf", "uF", "Uf", "UF"			/* same order as above */
62 };
63 
64 static fmd_asru_t *
65 fmd_asru_create(fmd_asru_hash_t *ahp, const char *uuid,
66     const char *name, nvlist_t *fmri)
67 {
68 	fmd_asru_t *ap = fmd_alloc(sizeof (fmd_asru_t), FMD_SLEEP);
69 	char *s;
70 
71 	(void) pthread_mutex_init(&ap->asru_lock, NULL);
72 	(void) pthread_cond_init(&ap->asru_cv, NULL);
73 
74 	ap->asru_next = NULL;
75 	ap->asru_name = fmd_strdup(name, FMD_SLEEP);
76 	(void) nvlist_xdup(fmri, &ap->asru_fmri, &fmd.d_nva);
77 	ap->asru_root = fmd_strdup(ahp->ah_dirpath, FMD_SLEEP);
78 	ap->asru_uuid = fmd_strdup(uuid, FMD_SLEEP);
79 	ap->asru_uuidlen = ap->asru_uuid ? strlen(ap->asru_uuid) : 0;
80 	ap->asru_log = NULL;
81 	ap->asru_refs = 1;
82 	ap->asru_flags = 0;
83 	ap->asru_case = NULL;
84 	ap->asru_event = NULL;
85 
86 	if (nvlist_lookup_string(ap->asru_fmri, FM_FMRI_SCHEME, &s) == 0 &&
87 	    strcmp(s, FM_FMRI_SCHEME_FMD) == 0)
88 		ap->asru_flags |= FMD_ASRU_INTERNAL;
89 
90 	return (ap);
91 }
92 
93 static void
94 fmd_asru_destroy(fmd_asru_t *ap)
95 {
96 	ASSERT(MUTEX_HELD(&ap->asru_lock));
97 	ASSERT(ap->asru_refs == 0);
98 
99 	if (ap->asru_log != NULL)
100 		fmd_log_rele(ap->asru_log);
101 
102 	if (ap->asru_case != NULL)
103 		fmd_case_rele(ap->asru_case);
104 
105 	fmd_strfree(ap->asru_name);
106 	nvlist_free(ap->asru_fmri);
107 	fmd_strfree(ap->asru_root);
108 	fmd_free(ap->asru_uuid, ap->asru_uuidlen + 1);
109 	fmd_free(ap, sizeof (fmd_asru_t));
110 }
111 
112 static void
113 fmd_asru_hash_insert(fmd_asru_hash_t *ahp, fmd_asru_t *ap)
114 {
115 	uint_t h = fmd_strhash(ap->asru_name) % ahp->ah_hashlen;
116 
117 	ASSERT(RW_WRITE_HELD(&ahp->ah_lock));
118 	ap->asru_next = ahp->ah_hash[h];
119 	ahp->ah_hash[h] = ap;
120 	ahp->ah_count++;
121 }
122 
123 static fmd_asru_t *
124 fmd_asru_hold(fmd_asru_t *ap)
125 {
126 	(void) pthread_mutex_lock(&ap->asru_lock);
127 	ap->asru_refs++;
128 	ASSERT(ap->asru_refs != 0);
129 	(void) pthread_mutex_unlock(&ap->asru_lock);
130 	return (ap);
131 }
132 
133 /*
134  * Lookup an asru in the hash by name and place a hold on it.  If the asru is
135  * not found, no entry is created and NULL is returned.  This internal function
136  * is for callers who have the ah_lock held and is used by lookup_name below.
137  */
138 fmd_asru_t *
139 fmd_asru_hash_lookup(fmd_asru_hash_t *ahp, const char *name)
140 {
141 	fmd_asru_t *ap;
142 	uint_t h;
143 
144 	ASSERT(RW_LOCK_HELD(&ahp->ah_lock));
145 	h = fmd_strhash(name) % ahp->ah_hashlen;
146 
147 	for (ap = ahp->ah_hash[h]; ap != NULL; ap = ap->asru_next) {
148 		if (strcmp(ap->asru_name, name) == 0)
149 			break;
150 	}
151 
152 	if (ap != NULL)
153 		(void) fmd_asru_hold(ap);
154 	else
155 		(void) fmd_set_errno(EFMD_ASRU_NOENT);
156 
157 	return (ap);
158 }
159 
160 static void
161 fmd_asru_hash_recreate(fmd_log_t *lp, fmd_event_t *ep, fmd_asru_hash_t *ahp)
162 {
163 	nvlist_t *nvl = FMD_EVENT_NVL(ep);
164 	char *case_uuid = NULL, *case_code = NULL;
165 	char *name = NULL;
166 	ssize_t namelen;
167 
168 	nvlist_t *fmri, *flt;
169 	fmd_event_t *e;
170 	char *class;
171 
172 	boolean_t f, u, m;
173 	fmd_asru_t *ap;
174 	int ps, us;
175 
176 	/*
177 	 * Extract the resource FMRI and most recent values of 'faulty' and
178 	 * 'unusable' from the event log.  If the event is malformed, return.
179 	 */
180 	if (nvlist_lookup_nvlist(nvl, FM_RSRC_RESOURCE, &fmri) != 0 ||
181 	    nvlist_lookup_boolean_value(nvl, FM_RSRC_ASRU_FAULTY, &f) != 0 ||
182 	    nvlist_lookup_boolean_value(nvl, FM_RSRC_ASRU_UNUSABLE, &u) != 0) {
183 		fmd_error(EFMD_ASRU_EVENT, "failed to reload asru %s: "
184 		    "invalid event log record\n", lp->log_name);
185 		ahp->ah_error = EFMD_ASRU_EVENT;
186 		return;
187 	}
188 
189 	/*
190 	 * Check to see if the resource is still present in the system.  If
191 	 * so, then update the value of the unusable bit based on the current
192 	 * system configuration.  If not, then either keep the entry in our
193 	 * cache if it is recent, or return and discard it if it is too old.
194 	 */
195 	if ((ps = fmd_fmri_present(fmri)) == -1) {
196 		fmd_error(EFMD_ASRU_FMRI, "failed to locate %s", lp->log_name);
197 		ahp->ah_error = EFMD_ASRU_FMRI;
198 		return;
199 	}
200 
201 	if (ps) {
202 		if ((us = fmd_fmri_unusable(fmri)) == -1) {
203 			fmd_error(EFMD_ASRU_FMRI, "failed to update "
204 			    "status of asru %s", lp->log_name);
205 			u = FMD_B_FALSE;
206 		} else
207 			u = us != 0;
208 
209 	} else if ((hrtime_t)(lp->log_stat.st_atime -
210 	    lp->log_stat.st_mtime) * NANOSEC < ahp->ah_lifetime) {
211 		u = FMD_B_TRUE;	/* not present; set unusable */
212 	} else
213 		return;		/* too old; discard this log */
214 
215 	/*
216 	 * In order to insert the ASRU into our hash, convert the FMRI from
217 	 * nvlist form into a string form and assign this name to the ASRU.
218 	 */
219 	if ((namelen = fmd_fmri_nvl2str(fmri, NULL, 0)) == -1 ||
220 	    (name = fmd_alloc(namelen + 1, FMD_NOSLEEP)) == NULL ||
221 	    fmd_fmri_nvl2str(fmri, name, namelen + 1) == -1) {
222 		fmd_error(EFMD_ASRU_FMRI,
223 		    "failed to reload asru %s", lp->log_name);
224 		if (name != NULL)
225 			fmd_free(name, namelen + 1);
226 		ahp->ah_error = EFMD_ASRU_FMRI;
227 		return;
228 	}
229 
230 	/*
231 	 * Look to see if the ASRU already exists in the hash: if it does and
232 	 * the existing ASRU entry is unusable but the duplicate is not, then
233 	 * delete the existing entry and continue on using the new entry; if
234 	 * the new entry is no "better", return an error and ignore it.
235 	 */
236 	if ((ap = fmd_asru_hash_lookup(ahp, name)) != NULL) {
237 		if (!u && (ap->asru_flags & FMD_ASRU_UNUSABLE)) {
238 			(void) fmd_asru_hash_delete_name(ahp, name);
239 			fmd_asru_hash_release(ahp, ap);
240 		} else {
241 			fmd_error(EFMD_ASRU_DUP, "removing duplicate asru "
242 			    "log %s for %s\n", lp->log_name, name);
243 			fmd_free(name, namelen + 1);
244 			fmd_asru_hash_release(ahp, ap);
245 			ahp->ah_error = EFMD_ASRU_DUP;
246 			return;
247 		}
248 	}
249 
250 	ap = fmd_asru_create(ahp, fmd_strbasename(lp->log_name), name, fmri);
251 	fmd_free(name, namelen + 1);
252 
253 	if (f)
254 		ap->asru_flags |= FMD_ASRU_FAULTY;
255 	if (u)
256 		ap->asru_flags |= FMD_ASRU_UNUSABLE;
257 
258 	if (nvlist_lookup_boolean_value(nvl,
259 	    FM_SUSPECT_MESSAGE, &m) == 0 && m == B_FALSE)
260 		ap->asru_flags |= FMD_ASRU_INVISIBLE;
261 
262 	/*
263 	 * If the ASRU is present and the Faulty bit is set and a case event is
264 	 * saved in the log, attempt to recreate the case in the CLOSED state.
265 	 * If the case is already present, fmd_case_recreate() will return it.
266 	 * If not, we'll create a new orphaned case, in which case we use the
267 	 * ASRU event to insert a suspect into the partially-restored case.
268 	 */
269 	(void) nvlist_lookup_string(nvl, FM_RSRC_ASRU_UUID, &case_uuid);
270 	(void) nvlist_lookup_string(nvl, FM_RSRC_ASRU_CODE, &case_code);
271 
272 	if (ps > 0 && !(ap->asru_flags & FMD_ASRU_INTERNAL) &&
273 	    (ap->asru_flags & FMD_ASRU_FAULTY) && case_uuid != NULL &&
274 	    nvlist_lookup_nvlist(nvl, FM_RSRC_ASRU_EVENT, &flt) == 0) {
275 
276 		fmd_module_lock(fmd.d_rmod);
277 
278 		ap->asru_case = fmd_case_recreate(fmd.d_rmod, NULL,
279 		    FMD_CASE_CLOSED, case_uuid, case_code);
280 
281 		if (ap->asru_case != NULL)
282 			fmd_case_hold(ap->asru_case);
283 
284 		fmd_module_unlock(fmd.d_rmod);
285 
286 		if (ap->asru_case != NULL && fmd_case_orphaned(ap->asru_case)) {
287 			(void) nvlist_xdup(flt, &ap->asru_event, &fmd.d_nva);
288 			fmd_case_recreate_suspect(
289 			    ap->asru_case, ap->asru_event);
290 		}
291 	}
292 
293 	ap->asru_flags |= FMD_ASRU_VALID;
294 	fmd_asru_hash_insert(ahp, ap);
295 
296 	TRACE((FMD_DBG_ASRU, "asru %s recreated as %p (%s)", ap->asru_uuid,
297 	    (void *)ap, _fmd_asru_snames[ap->asru_flags & FMD_ASRU_STATE]));
298 
299 	/*
300 	 * If the resource is present and faulty but not unusable, replay the
301 	 * fault event that caused it be marked faulty.  This will cause the
302 	 * agent subscribing to this fault class to again disable the resource.
303 	 */
304 	if (ap->asru_case != NULL && !(ap->asru_flags & FMD_ASRU_UNUSABLE)) {
305 		fmd_dprintf(FMD_DBG_ASRU,
306 		    "replaying fault event for %s", ap->asru_name);
307 
308 		(void) nvlist_xdup(flt, &flt, &fmd.d_nva);
309 		(void) nvlist_lookup_string(flt, FM_CLASS, &class);
310 
311 		if (case_uuid != NULL)
312 			(void) nvlist_add_string(flt, FMD_EVN_UUID, case_uuid);
313 
314 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, flt, class);
315 		fmd_dispq_dispatch(fmd.d_disp, e, class);
316 	}
317 }
318 
319 static void
320 fmd_asru_hash_discard(fmd_asru_hash_t *ahp, const char *uuid, int err)
321 {
322 	char src[PATH_MAX], dst[PATH_MAX];
323 
324 	(void) snprintf(src, PATH_MAX, "%s/%s", ahp->ah_dirpath, uuid);
325 	(void) snprintf(dst, PATH_MAX, "%s/%s-", ahp->ah_dirpath, uuid);
326 
327 	if (err != 0)
328 		err = rename(src, dst);
329 	else
330 		err = unlink(src);
331 
332 	if (err != 0 && errno != ENOENT)
333 		fmd_error(EFMD_ASRU_EVENT, "failed to rename log %s", src);
334 }
335 
336 /*
337  * Open a saved log file and restore it into the ASRU hash.  If we can't even
338  * open the log, rename the log file to <uuid>- to indicate it is corrupt.  If
339  * fmd_log_replay() fails, we either delete the file (if it has reached the
340  * upper limit on cache age) or rename it for debugging if it was corrupted.
341  */
342 static void
343 fmd_asru_hash_logopen(fmd_asru_hash_t *ahp, const char *uuid)
344 {
345 	fmd_log_t *lp = fmd_log_tryopen(ahp->ah_dirpath, uuid, FMD_LOG_ASRU);
346 	uint_t n;
347 
348 	if (lp == NULL) {
349 		fmd_asru_hash_discard(ahp, uuid, errno);
350 		return;
351 	}
352 
353 	ahp->ah_error = 0;
354 	n = ahp->ah_count;
355 
356 	fmd_log_replay(lp, (fmd_log_f *)fmd_asru_hash_recreate, ahp);
357 	fmd_log_rele(lp);
358 
359 	if (ahp->ah_count == n)
360 		fmd_asru_hash_discard(ahp, uuid, ahp->ah_error);
361 }
362 
363 void
364 fmd_asru_hash_refresh(fmd_asru_hash_t *ahp)
365 {
366 	struct dirent *dp;
367 	DIR *dirp;
368 	int zero;
369 
370 	if ((dirp = opendir(ahp->ah_dirpath)) == NULL) {
371 		fmd_error(EFMD_ASRU_NODIR,
372 		    "failed to open asru cache directory %s", ahp->ah_dirpath);
373 		return;
374 	}
375 
376 	(void) fmd_conf_getprop(fmd.d_conf, "rsrc.zero", &zero);
377 
378 	(void) pthread_rwlock_wrlock(&ahp->ah_lock);
379 
380 	while ((dp = readdir(dirp)) != NULL) {
381 		if (dp->d_name[0] == '.')
382 			continue; /* skip "." and ".." */
383 
384 		if (zero)
385 			fmd_asru_hash_discard(ahp, dp->d_name, 0);
386 		else if (!fmd_strmatch(dp->d_name, "*-"))
387 			fmd_asru_hash_logopen(ahp, dp->d_name);
388 	}
389 
390 	(void) pthread_rwlock_unlock(&ahp->ah_lock);
391 	(void) closedir(dirp);
392 }
393 
394 fmd_asru_hash_t *
395 fmd_asru_hash_create(const char *root, const char *dir)
396 {
397 	fmd_asru_hash_t *ahp;
398 	char path[PATH_MAX];
399 
400 	ahp = fmd_alloc(sizeof (fmd_asru_hash_t), FMD_SLEEP);
401 	(void) pthread_rwlock_init(&ahp->ah_lock, NULL);
402 	ahp->ah_hashlen = fmd.d_str_buckets;
403 	ahp->ah_hash = fmd_zalloc(sizeof (void *) * ahp->ah_hashlen, FMD_SLEEP);
404 	(void) snprintf(path, sizeof (path), "%s/%s", root, dir);
405 	ahp->ah_dirpath = fmd_strdup(path, FMD_SLEEP);
406 	(void) fmd_conf_getprop(fmd.d_conf, "rsrc.age", &ahp->ah_lifetime);
407 	ahp->ah_count = 0;
408 	ahp->ah_error = 0;
409 
410 	return (ahp);
411 }
412 
413 void
414 fmd_asru_hash_destroy(fmd_asru_hash_t *ahp)
415 {
416 	fmd_asru_t *ap, *np;
417 	uint_t i;
418 
419 	for (i = 0; i < ahp->ah_hashlen; i++) {
420 		for (ap = ahp->ah_hash[i]; ap != NULL; ap = np) {
421 			np = ap->asru_next;
422 			ap->asru_next = NULL;
423 			fmd_asru_hash_release(ahp, ap);
424 		}
425 	}
426 
427 	fmd_strfree(ahp->ah_dirpath);
428 	fmd_free(ahp->ah_hash, sizeof (void *) * ahp->ah_hashlen);
429 	fmd_free(ahp, sizeof (fmd_asru_hash_t));
430 }
431 
432 /*
433  * Take a snapshot of the ASRU database by placing an additional hold on each
434  * member in an auxiliary array, and then call 'func' for each ASRU.
435  */
436 void
437 fmd_asru_hash_apply(fmd_asru_hash_t *ahp,
438     void (*func)(fmd_asru_t *, void *), void *arg)
439 {
440 	fmd_asru_t *ap, **aps, **app;
441 	uint_t apc, i;
442 
443 	(void) pthread_rwlock_rdlock(&ahp->ah_lock);
444 
445 	aps = app = fmd_alloc(ahp->ah_count * sizeof (fmd_asru_t *), FMD_SLEEP);
446 	apc = ahp->ah_count;
447 
448 	for (i = 0; i < ahp->ah_hashlen; i++) {
449 		for (ap = ahp->ah_hash[i]; ap != NULL; ap = ap->asru_next)
450 			*app++ = fmd_asru_hold(ap);
451 	}
452 
453 	ASSERT(app == aps + apc);
454 	(void) pthread_rwlock_unlock(&ahp->ah_lock);
455 
456 	for (i = 0; i < apc; i++) {
457 		func(aps[i], arg);
458 		fmd_asru_hash_release(ahp, aps[i]);
459 	}
460 
461 	fmd_free(aps, apc * sizeof (fmd_asru_t *));
462 }
463 
464 /*
465  * Lookup an asru in the hash by name and place a hold on it.  If the asru is
466  * not found, no entry is created and NULL is returned.
467  */
468 fmd_asru_t *
469 fmd_asru_hash_lookup_name(fmd_asru_hash_t *ahp, const char *name)
470 {
471 	fmd_asru_t *ap;
472 
473 	(void) pthread_rwlock_rdlock(&ahp->ah_lock);
474 	ap = fmd_asru_hash_lookup(ahp, name);
475 	(void) pthread_rwlock_unlock(&ahp->ah_lock);
476 
477 	return (ap);
478 }
479 
480 /*
481  * Lookup an asru in the hash and place a hold on it.  If 'create' is true, an
482  * absent entry will be created for the caller; otherwise NULL is returned.
483  */
484 fmd_asru_t *
485 fmd_asru_hash_lookup_nvl(fmd_asru_hash_t *ahp, nvlist_t *fmri, int create)
486 {
487 	fmd_asru_t *ap;
488 	char *name = NULL;
489 	ssize_t namelen;
490 	uint_t h;
491 
492 	/*
493 	 * In order to lookup the ASRU in our hash, convert the FMRI from
494 	 * nvlist form into a string form using the scheme module.
495 	 */
496 	if ((namelen = fmd_fmri_nvl2str(fmri, NULL, 0)) == -1 ||
497 	    (name = fmd_alloc(namelen + 1, FMD_NOSLEEP)) == NULL ||
498 	    fmd_fmri_nvl2str(fmri, name, namelen + 1) == -1) {
499 		if (name != NULL)
500 			fmd_free(name, namelen + 1);
501 		return (NULL);
502 	}
503 
504 	/*
505 	 * If we must create the asru, grab the rwlock as a writer; otherwise
506 	 * reader is sufficient.  Then search the hash for the given asru name.
507 	 * If we didn't find the asru in the hash and we need to create it,
508 	 * create and insert the asru with ahp->ah_lock held and hash it in.
509 	 * We'll then drop the rwlock and proceed to initializing the asru.
510 	 */
511 	if (create)
512 		(void) pthread_rwlock_wrlock(&ahp->ah_lock);
513 	else
514 		(void) pthread_rwlock_rdlock(&ahp->ah_lock);
515 
516 	h = fmd_strhash(name) % ahp->ah_hashlen;
517 
518 	for (ap = ahp->ah_hash[h]; ap != NULL; ap = ap->asru_next) {
519 		if (strcmp(ap->asru_name, name) == 0)
520 			break;
521 	}
522 
523 	if (ap == NULL && create == FMD_B_TRUE) {
524 		ap = fmd_asru_create(ahp, NULL, name, fmri);
525 		fmd_asru_hash_insert(ahp, ap);
526 		(void) pthread_mutex_lock(&ap->asru_lock);
527 	} else
528 		create = FMD_B_FALSE;
529 
530 	(void) pthread_rwlock_unlock(&ahp->ah_lock);
531 	fmd_free(name, namelen + 1);
532 
533 	/*
534 	 * If 'create' is still true, then we need to initialize the asru log;
535 	 * If 'create' is false and an asru was found, we must cond_wait for
536 	 * the FMD_ASRU_VALID bit to be set before returning.  In both cases,
537 	 * we increment asru_refs for the caller.
538 	 */
539 	if (create == FMD_B_TRUE) {
540 		uuid_t uuid;
541 
542 		ASSERT(MUTEX_HELD(&ap->asru_lock));
543 		ASSERT(ap->asru_uuid == NULL && ap->asru_log == NULL);
544 
545 		/*
546 		 * Generate a UUID for the ASRU.  libuuid cleverly gives us no
547 		 * interface for specifying or learning the buffer size.  Sigh.
548 		 * The spec says 36 bytes but we use a tunable just to be safe.
549 		 */
550 		(void) fmd_conf_getprop(fmd.d_conf,
551 		    "uuidlen", &ap->asru_uuidlen);
552 
553 		ap->asru_uuid = fmd_zalloc(ap->asru_uuidlen + 1, FMD_SLEEP);
554 		uuid_generate(uuid);
555 		uuid_unparse(uuid, ap->asru_uuid);
556 
557 		ASSERT(!(ap->asru_flags & FMD_ASRU_VALID));
558 		ap->asru_flags |= FMD_ASRU_VALID;
559 
560 		ap->asru_refs++;
561 		ASSERT(ap->asru_refs != 0);
562 		(void) pthread_cond_broadcast(&ap->asru_cv);
563 		(void) pthread_mutex_unlock(&ap->asru_lock);
564 
565 		TRACE((FMD_DBG_ASRU, "asru %s created as %p",
566 		    ap->asru_uuid, (void *)ap));
567 
568 	} else if (ap != NULL) {
569 		(void) pthread_mutex_lock(&ap->asru_lock);
570 
571 		while (!(ap->asru_flags & FMD_ASRU_VALID))
572 			(void) pthread_cond_wait(&ap->asru_cv, &ap->asru_lock);
573 
574 		ap->asru_refs++;
575 		ASSERT(ap->asru_refs != 0);
576 		(void) pthread_mutex_unlock(&ap->asru_lock);
577 	}
578 
579 	return (ap);
580 }
581 
582 /*
583  * Release the reference count on an asru obtained using fmd_asru_hash_lookup.
584  * We take 'ahp' for symmetry and in case we need to use it in future work.
585  */
586 /*ARGSUSED*/
587 void
588 fmd_asru_hash_release(fmd_asru_hash_t *ahp, fmd_asru_t *ap)
589 {
590 	(void) pthread_mutex_lock(&ap->asru_lock);
591 
592 	ASSERT(ap->asru_refs != 0);
593 	if (--ap->asru_refs == 0)
594 		fmd_asru_destroy(ap);
595 	else
596 		(void) pthread_mutex_unlock(&ap->asru_lock);
597 }
598 
599 int
600 fmd_asru_hash_delete_name(fmd_asru_hash_t *ahp, const char *name)
601 {
602 	fmd_asru_t *ap, **pp;
603 	char path[PATH_MAX];
604 	uint_t h;
605 
606 	(void) pthread_rwlock_wrlock(&ahp->ah_lock);
607 
608 	h = fmd_strhash(name) % ahp->ah_hashlen;
609 	pp = &ahp->ah_hash[h];
610 
611 	for (ap = *pp; ap != NULL; ap = ap->asru_next) {
612 		if (strcmp(ap->asru_name, name) == 0)
613 			break;
614 		else
615 			pp = &ap->asru_next;
616 	}
617 
618 	if (ap != NULL) {
619 		*pp = ap->asru_next;
620 		ap->asru_next = NULL;
621 		ASSERT(ahp->ah_count != 0);
622 		ahp->ah_count--;
623 	}
624 
625 	(void) pthread_rwlock_unlock(&ahp->ah_lock);
626 
627 	if (ap == NULL)
628 		return (fmd_set_errno(EFMD_ASRU_NOENT));
629 
630 	/*
631 	 * If we found a matching ASRU, unlink its log file and then release
632 	 * the hash entry.  Note that it may still be referenced if another
633 	 * thread is manipulating it; this is ok because once we unlink, the
634 	 * log file will not be restored, and the log data will be freed when
635 	 * all of the referencing threads release their respective references.
636 	 */
637 	(void) snprintf(path, sizeof (path),
638 	    "%s/%s", ahp->ah_dirpath, ap->asru_uuid);
639 
640 	if (unlink(path) != 0)
641 		fmd_error(EFMD_ASRU_UNLINK, "failed to unlink asru %s", path);
642 
643 	fmd_asru_hash_release(ahp, ap);
644 	return (0);
645 }
646 
647 static void
648 fmd_asru_logevent(fmd_asru_t *ap)
649 {
650 	boolean_t f = (ap->asru_flags & FMD_ASRU_FAULTY) != 0;
651 	boolean_t u = (ap->asru_flags & FMD_ASRU_UNUSABLE) != 0;
652 	boolean_t m = (ap->asru_flags & FMD_ASRU_INVISIBLE) == 0;
653 
654 	fmd_case_impl_t *cip;
655 	fmd_event_t *e;
656 	fmd_log_t *lp;
657 	nvlist_t *nvl;
658 	char *class;
659 
660 	ASSERT(MUTEX_HELD(&ap->asru_lock));
661 	ASSERT(ap->asru_case != NULL);
662 	cip = (fmd_case_impl_t *)ap->asru_case;
663 
664 	if ((lp = ap->asru_log) == NULL)
665 		lp = fmd_log_open(ap->asru_root, ap->asru_uuid, FMD_LOG_ASRU);
666 
667 	if (lp == NULL)
668 		return; /* can't log events if we can't open the log */
669 
670 	nvl = fmd_protocol_rsrc_asru(_fmd_asru_events[f | (u << 1)],
671 	    ap->asru_fmri, cip->ci_uuid, cip->ci_code, f, u, m, ap->asru_event);
672 
673 	(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
674 	e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
675 
676 	fmd_event_hold(e);
677 	fmd_log_append(lp, e, NULL);
678 	fmd_event_rele(e);
679 
680 	/*
681 	 * For now, we close the log file after every update to conserve file
682 	 * descriptors and daemon overhead.  If this becomes a performance
683 	 * issue this code can change to keep a fixed-size LRU cache of logs.
684 	 */
685 	fmd_log_rele(lp);
686 	ap->asru_log = NULL;
687 }
688 
689 int
690 fmd_asru_setflags(fmd_asru_t *ap, uint_t sflag, fmd_case_t *cp, nvlist_t *nvl)
691 {
692 	fmd_case_t *old_case = NULL;
693 	uint_t nstate, ostate;
694 	boolean_t msg;
695 
696 	ASSERT(!(sflag & ~FMD_ASRU_STATE));
697 	ASSERT(sflag != FMD_ASRU_STATE);
698 
699 	(void) pthread_mutex_lock(&ap->asru_lock);
700 
701 	ostate = ap->asru_flags & FMD_ASRU_STATE;
702 	ap->asru_flags |= sflag;
703 	nstate = ap->asru_flags & FMD_ASRU_STATE;
704 
705 	if (nstate == ostate) {
706 		(void) pthread_mutex_unlock(&ap->asru_lock);
707 		return (0);
708 	}
709 
710 	if (cp != NULL && cp != ap->asru_case) {
711 		old_case = ap->asru_case;
712 		fmd_case_hold_locked(cp);
713 		ap->asru_case = cp;
714 		ap->asru_event = nvl;
715 	}
716 
717 	if (nvl != NULL && nvlist_lookup_boolean_value(nvl,
718 	    FM_SUSPECT_MESSAGE, &msg) == 0 && msg == B_FALSE)
719 		ap->asru_flags |= FMD_ASRU_INVISIBLE;
720 
721 	TRACE((FMD_DBG_ASRU, "asru %s %s->%s", ap->asru_uuid,
722 	    _fmd_asru_snames[ostate], _fmd_asru_snames[nstate]));
723 
724 	fmd_asru_logevent(ap);
725 
726 	(void) pthread_cond_broadcast(&ap->asru_cv);
727 	(void) pthread_mutex_unlock(&ap->asru_lock);
728 
729 	if (old_case != NULL)
730 		fmd_case_rele(old_case);
731 
732 	return (1);
733 }
734 
735 int
736 fmd_asru_clrflags(fmd_asru_t *ap, uint_t sflag, fmd_case_t *cp, nvlist_t *nvl)
737 {
738 	fmd_case_t *old_case = NULL;
739 	uint_t nstate, ostate;
740 
741 	ASSERT(!(sflag & ~FMD_ASRU_STATE));
742 	ASSERT(sflag != FMD_ASRU_STATE);
743 
744 	(void) pthread_mutex_lock(&ap->asru_lock);
745 
746 	ostate = ap->asru_flags & FMD_ASRU_STATE;
747 	ap->asru_flags &= ~sflag;
748 	nstate = ap->asru_flags & FMD_ASRU_STATE;
749 
750 	if (nstate == ostate) {
751 		(void) pthread_mutex_unlock(&ap->asru_lock);
752 		return (0);
753 	}
754 
755 	if (cp != NULL && cp != ap->asru_case) {
756 		old_case = ap->asru_case;
757 		fmd_case_hold_locked(cp);
758 		ap->asru_case = cp;
759 		ap->asru_event = nvl;
760 	}
761 
762 	TRACE((FMD_DBG_ASRU, "asru %s %s->%s", ap->asru_uuid,
763 	    _fmd_asru_snames[ostate], _fmd_asru_snames[nstate]));
764 
765 	fmd_asru_logevent(ap);
766 
767 	if (cp == NULL && (sflag & FMD_ASRU_FAULTY)) {
768 		old_case = ap->asru_case;
769 		ap->asru_case = NULL;
770 		ap->asru_event = NULL;
771 	}
772 
773 	(void) pthread_cond_broadcast(&ap->asru_cv);
774 	(void) pthread_mutex_unlock(&ap->asru_lock);
775 
776 	if (old_case != NULL) {
777 		if (cp == NULL && (sflag & FMD_ASRU_FAULTY))
778 			fmd_case_update(old_case);
779 		fmd_case_rele(old_case);
780 	}
781 
782 	return (1);
783 }
784 
785 /*
786  * Report the current known state of the ASRU by refreshing its unusable status
787  * based upon the routines provided by the scheme module.  If the unusable bit
788  * is different, we do *not* generate a state change here because that change
789  * may be unrelated to fmd activities and therefore we have no case or event.
790  * The absence of the transition is harmless as this function is only provided
791  * for RPC observability and fmd's clients are only concerned with ASRU_FAULTY.
792  */
793 int
794 fmd_asru_getstate(fmd_asru_t *ap)
795 {
796 	int us, st;
797 
798 	if (!(ap->asru_flags & FMD_ASRU_INTERNAL) &&
799 	    fmd_fmri_present(ap->asru_fmri) <= 0)
800 		return (0); /* do not report non-fmd non-present resources */
801 
802 	us = fmd_fmri_unusable(ap->asru_fmri);
803 	st = ap->asru_flags & FMD_ASRU_STATE;
804 
805 	if (us > 0)
806 		st |= FMD_ASRU_UNUSABLE;
807 	else if (us == 0)
808 		st &= ~FMD_ASRU_UNUSABLE;
809 
810 	return (st);
811 }
812