xref: /onnv-gate/usr/src/cmd/svc/configd/snapshot.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <assert.h>
30*0Sstevel@tonic-gate #include <pthread.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <strings.h>
33*0Sstevel@tonic-gate #include "configd.h"
34*0Sstevel@tonic-gate #include "repcache_protocol.h"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate typedef struct snapshot_bucket {
37*0Sstevel@tonic-gate 	pthread_mutex_t	sb_lock;
38*0Sstevel@tonic-gate 	rc_snapshot_t	*sb_head;
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 	char		sb_pad[64 - sizeof (pthread_mutex_t) -
41*0Sstevel@tonic-gate 			    sizeof (rc_snapshot_t *)];
42*0Sstevel@tonic-gate } snapshot_bucket_t;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	SN_HASH_SIZE	64
45*0Sstevel@tonic-gate #define	SN_HASH_MASK	(SN_HASH_SIZE - 1)
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #pragma align 64(snapshot_hash)
48*0Sstevel@tonic-gate static snapshot_bucket_t snapshot_hash[SN_HASH_SIZE];
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	SNAPSHOT_BUCKET(h)	(&snapshot_hash[(h) & SN_HASH_MASK])
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static rc_snapshot_t *
snapshot_alloc(void)53*0Sstevel@tonic-gate snapshot_alloc(void)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	rc_snapshot_t *sp;
56*0Sstevel@tonic-gate 	sp = uu_zalloc(sizeof (*sp));
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&sp->rs_lock, NULL);
59*0Sstevel@tonic-gate 	(void) pthread_cond_init(&sp->rs_cv, NULL);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	sp->rs_refcnt++;
62*0Sstevel@tonic-gate 	return (sp);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static void
snapshot_free(rc_snapshot_t * sp)66*0Sstevel@tonic-gate snapshot_free(rc_snapshot_t *sp)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	rc_snaplevel_t *lvl, *next;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	assert(sp->rs_refcnt == 0 && sp->rs_childref == 0);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&sp->rs_lock);
73*0Sstevel@tonic-gate 	(void) pthread_cond_destroy(&sp->rs_cv);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	for (lvl = sp->rs_levels; lvl != NULL; lvl = next) {
76*0Sstevel@tonic-gate 		next = lvl->rsl_next;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		assert(lvl->rsl_parent == sp);
79*0Sstevel@tonic-gate 		lvl->rsl_parent = NULL;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		if (lvl->rsl_service)
82*0Sstevel@tonic-gate 			free((char *)lvl->rsl_service);
83*0Sstevel@tonic-gate 		if (lvl->rsl_instance)
84*0Sstevel@tonic-gate 			free((char *)lvl->rsl_instance);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		uu_free(lvl);
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	uu_free(sp);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static void
rc_snapshot_hold(rc_snapshot_t * sp)92*0Sstevel@tonic-gate rc_snapshot_hold(rc_snapshot_t *sp)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&sp->rs_lock);
95*0Sstevel@tonic-gate 	sp->rs_refcnt++;
96*0Sstevel@tonic-gate 	assert(sp->rs_refcnt > 0);
97*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&sp->rs_lock);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate void
rc_snapshot_rele(rc_snapshot_t * sp)101*0Sstevel@tonic-gate rc_snapshot_rele(rc_snapshot_t *sp)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	int done;
104*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&sp->rs_lock);
105*0Sstevel@tonic-gate 	assert(sp->rs_refcnt > 0);
106*0Sstevel@tonic-gate 	sp->rs_refcnt--;
107*0Sstevel@tonic-gate 	done = ((sp->rs_flags & RC_SNAPSHOT_DEAD) &&
108*0Sstevel@tonic-gate 	    sp->rs_refcnt == 0 && sp->rs_childref == 0);
109*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&sp->rs_lock);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (done)
112*0Sstevel@tonic-gate 		snapshot_free(sp);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate void
rc_snaplevel_hold(rc_snaplevel_t * lvl)116*0Sstevel@tonic-gate rc_snaplevel_hold(rc_snaplevel_t *lvl)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	rc_snapshot_t *sp = lvl->rsl_parent;
119*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&sp->rs_lock);
120*0Sstevel@tonic-gate 	sp->rs_childref++;
121*0Sstevel@tonic-gate 	assert(sp->rs_childref > 0);
122*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&sp->rs_lock);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate void
rc_snaplevel_rele(rc_snaplevel_t * lvl)126*0Sstevel@tonic-gate rc_snaplevel_rele(rc_snaplevel_t *lvl)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	int done;
129*0Sstevel@tonic-gate 	rc_snapshot_t *sp = lvl->rsl_parent;
130*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&sp->rs_lock);
131*0Sstevel@tonic-gate 	assert(sp->rs_childref > 0);
132*0Sstevel@tonic-gate 	sp->rs_childref--;
133*0Sstevel@tonic-gate 	done = ((sp->rs_flags & RC_SNAPSHOT_DEAD) &&
134*0Sstevel@tonic-gate 	    sp->rs_refcnt == 0 && sp->rs_childref == 0);
135*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&sp->rs_lock);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if (done)
138*0Sstevel@tonic-gate 		snapshot_free(sp);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static snapshot_bucket_t *
snapshot_hold_bucket(uint32_t snap_id)142*0Sstevel@tonic-gate snapshot_hold_bucket(uint32_t snap_id)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	snapshot_bucket_t *bp = SNAPSHOT_BUCKET(snap_id);
145*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&bp->sb_lock);
146*0Sstevel@tonic-gate 	return (bp);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static void
snapshot_rele_bucket(snapshot_bucket_t * bp)150*0Sstevel@tonic-gate snapshot_rele_bucket(snapshot_bucket_t *bp)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&bp->sb_lock));
153*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&bp->sb_lock);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static rc_snapshot_t *
snapshot_lookup_unlocked(snapshot_bucket_t * bp,uint32_t snap_id)157*0Sstevel@tonic-gate snapshot_lookup_unlocked(snapshot_bucket_t *bp, uint32_t snap_id)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	rc_snapshot_t *sp;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&bp->sb_lock));
162*0Sstevel@tonic-gate 	assert(bp == SNAPSHOT_BUCKET(snap_id));
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	for (sp = bp->sb_head; sp != NULL; sp = sp->rs_hash_next) {
165*0Sstevel@tonic-gate 		if (sp->rs_snap_id == snap_id) {
166*0Sstevel@tonic-gate 			rc_snapshot_hold(sp);
167*0Sstevel@tonic-gate 			return (sp);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 	return (NULL);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate static void
snapshot_insert_unlocked(snapshot_bucket_t * bp,rc_snapshot_t * sp)174*0Sstevel@tonic-gate snapshot_insert_unlocked(snapshot_bucket_t *bp, rc_snapshot_t *sp)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&bp->sb_lock));
177*0Sstevel@tonic-gate 	assert(bp == SNAPSHOT_BUCKET(sp->rs_snap_id));
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	assert(sp->rs_hash_next == NULL);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	sp->rs_hash_next = bp->sb_head;
182*0Sstevel@tonic-gate 	bp->sb_head = sp;
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static void
snapshot_remove_unlocked(snapshot_bucket_t * bp,rc_snapshot_t * sp)186*0Sstevel@tonic-gate snapshot_remove_unlocked(snapshot_bucket_t *bp, rc_snapshot_t *sp)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	rc_snapshot_t **spp;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&bp->sb_lock));
191*0Sstevel@tonic-gate 	assert(bp == SNAPSHOT_BUCKET(sp->rs_snap_id));
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	assert(sp->rs_hash_next == NULL);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	for (spp = &bp->sb_head; *spp != NULL; spp = &(*spp)->rs_hash_next)
196*0Sstevel@tonic-gate 		if (*spp == sp)
197*0Sstevel@tonic-gate 			break;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	assert(*spp == sp);
200*0Sstevel@tonic-gate 	*spp = sp->rs_hash_next;
201*0Sstevel@tonic-gate 	sp->rs_hash_next = NULL;
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  * Look up the snapshot with id snap_id in the hash table, or create it
206*0Sstevel@tonic-gate  * & populate it with its snaplevels if it's not in the hash table yet.
207*0Sstevel@tonic-gate  *
208*0Sstevel@tonic-gate  * Fails with
209*0Sstevel@tonic-gate  *   _NO_RESOURCES
210*0Sstevel@tonic-gate  */
211*0Sstevel@tonic-gate int
rc_snapshot_get(uint32_t snap_id,rc_snapshot_t ** snpp)212*0Sstevel@tonic-gate rc_snapshot_get(uint32_t snap_id, rc_snapshot_t **snpp)
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	snapshot_bucket_t *bp;
215*0Sstevel@tonic-gate 	rc_snapshot_t *sp;
216*0Sstevel@tonic-gate 	int r;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	bp = snapshot_hold_bucket(snap_id);
219*0Sstevel@tonic-gate 	sp = snapshot_lookup_unlocked(bp, snap_id);
220*0Sstevel@tonic-gate 	if (sp != NULL) {
221*0Sstevel@tonic-gate 		snapshot_rele_bucket(bp);
222*0Sstevel@tonic-gate 		(void) pthread_mutex_lock(&sp->rs_lock);
223*0Sstevel@tonic-gate 		while (sp->rs_flags & RC_SNAPSHOT_FILLING)
224*0Sstevel@tonic-gate 			(void) pthread_cond_wait(&sp->rs_cv, &sp->rs_lock);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		if (sp->rs_flags & RC_SNAPSHOT_DEAD) {
227*0Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&sp->rs_lock);
228*0Sstevel@tonic-gate 			rc_snapshot_rele(sp);
229*0Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_NO_RESOURCES);
230*0Sstevel@tonic-gate 		}
231*0Sstevel@tonic-gate 		assert(sp->rs_flags & RC_SNAPSHOT_READY);
232*0Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&sp->rs_lock);
233*0Sstevel@tonic-gate 		*snpp = sp;
234*0Sstevel@tonic-gate 		return (REP_PROTOCOL_SUCCESS);
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 	sp = snapshot_alloc();
237*0Sstevel@tonic-gate 	sp->rs_snap_id = snap_id;
238*0Sstevel@tonic-gate 	sp->rs_flags |= RC_SNAPSHOT_FILLING;
239*0Sstevel@tonic-gate 	snapshot_insert_unlocked(bp, sp);
240*0Sstevel@tonic-gate 	snapshot_rele_bucket(bp);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	/*
243*0Sstevel@tonic-gate 	 * Now fill in the snapshot tree
244*0Sstevel@tonic-gate 	 */
245*0Sstevel@tonic-gate 	r = object_fill_snapshot(sp);
246*0Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
247*0Sstevel@tonic-gate 		assert(r == REP_PROTOCOL_FAIL_NO_RESOURCES);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 		/*
250*0Sstevel@tonic-gate 		 * failed -- first remove it from the hash table, then kill it
251*0Sstevel@tonic-gate 		 */
252*0Sstevel@tonic-gate 		bp = snapshot_hold_bucket(snap_id);
253*0Sstevel@tonic-gate 		snapshot_remove_unlocked(bp, sp);
254*0Sstevel@tonic-gate 		snapshot_rele_bucket(bp);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		(void) pthread_mutex_lock(&sp->rs_lock);
257*0Sstevel@tonic-gate 		sp->rs_flags &= ~RC_SNAPSHOT_FILLING;
258*0Sstevel@tonic-gate 		sp->rs_flags |= RC_SNAPSHOT_DEAD;
259*0Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&sp->rs_cv);
260*0Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&sp->rs_lock);
261*0Sstevel@tonic-gate 		rc_snapshot_rele(sp);		/* may free sp */
262*0Sstevel@tonic-gate 		return (r);
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&sp->rs_lock);
265*0Sstevel@tonic-gate 	sp->rs_flags &= ~RC_SNAPSHOT_FILLING;
266*0Sstevel@tonic-gate 	sp->rs_flags |= RC_SNAPSHOT_READY;
267*0Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&sp->rs_cv);
268*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&sp->rs_lock);
269*0Sstevel@tonic-gate 	*snpp = sp;
270*0Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);		/* pass on creation reference */
271*0Sstevel@tonic-gate }
272