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