xref: /onnv-gate/usr/src/lib/udapl/libdat/common/dat_dr.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 (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * MODULE: dat_dr.c
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  * PURPOSE: dynamic registry implementation
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * $Id: dat_dr.c,v 1.12 2003/08/20 14:28:40 hobie16 Exp $
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "dat_dr.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "dat_dictionary.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * Global Variables
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static DAT_OS_LOCK 		g_dr_lock;
55*0Sstevel@tonic-gate static DAT_DICTIONARY 		*g_dr_dictionary = NULL;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * External Functions
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Function: dat_dr_init
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate DAT_RETURN
dat_dr_init(void)70*0Sstevel@tonic-gate dat_dr_init(void)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	DAT_RETURN 	status;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	status = dat_os_lock_init(&g_dr_lock);
75*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
76*0Sstevel@tonic-gate 		return (status);
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	status = dat_dictionary_create(&g_dr_dictionary);
80*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
81*0Sstevel@tonic-gate 		return (status);
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * Function: dat_dr_fini
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate DAT_RETURN
dat_dr_fini(void)93*0Sstevel@tonic-gate dat_dr_fini(void)
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate 	DAT_RETURN 			status;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	status = dat_os_lock_destroy(&g_dr_lock);
98*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
99*0Sstevel@tonic-gate 		return (status);
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	status = dat_dictionary_destroy(g_dr_dictionary);
103*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
104*0Sstevel@tonic-gate 		return (status);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	return (DAT_SUCCESS);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  * Function: dat_dr_insert
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate extern DAT_RETURN
dat_dr_insert(IN const DAT_PROVIDER_INFO * info,IN DAT_DR_ENTRY * entry)116*0Sstevel@tonic-gate dat_dr_insert(
117*0Sstevel@tonic-gate     IN  const DAT_PROVIDER_INFO *info,
118*0Sstevel@tonic-gate     IN  DAT_DR_ENTRY 		*entry)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	DAT_RETURN 		status;
121*0Sstevel@tonic-gate 	DAT_DICTIONARY_ENTRY 	dict_entry;
122*0Sstevel@tonic-gate 	DAT_DR_ENTRY		*data;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	data = dat_os_alloc(sizeof (DAT_DR_ENTRY));
125*0Sstevel@tonic-gate 	if (NULL == data) {
126*0Sstevel@tonic-gate 		status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
127*0Sstevel@tonic-gate 		    DAT_RESOURCE_MEMORY);
128*0Sstevel@tonic-gate 		goto bail;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	*data = *entry;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	dict_entry = NULL;
134*0Sstevel@tonic-gate 	status = dat_dictionary_entry_create(&dict_entry);
135*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
136*0Sstevel@tonic-gate 		goto bail;
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	dat_os_lock(&g_dr_lock);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	status = dat_dictionary_insert(g_dr_dictionary,
142*0Sstevel@tonic-gate 		dict_entry,
143*0Sstevel@tonic-gate 		info,
144*0Sstevel@tonic-gate 		(DAT_DICTIONARY_DATA *) data);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	dat_os_unlock(&g_dr_lock);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate bail:
149*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
150*0Sstevel@tonic-gate 		if (NULL != data) {
151*0Sstevel@tonic-gate 			dat_os_free(data, sizeof (DAT_DR_ENTRY));
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		if (NULL != dict_entry) {
156*0Sstevel@tonic-gate 			(void) dat_dictionary_entry_destroy(dict_entry);
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	return (status);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Function: dat_dr_remove
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate extern DAT_RETURN
dat_dr_remove(IN const DAT_PROVIDER_INFO * info)169*0Sstevel@tonic-gate dat_dr_remove(
170*0Sstevel@tonic-gate     IN  const DAT_PROVIDER_INFO *info)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	DAT_DR_ENTRY 		*data;
173*0Sstevel@tonic-gate 	DAT_DICTIONARY_ENTRY 	dict_entry;
174*0Sstevel@tonic-gate 	DAT_RETURN 		status;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	dat_os_lock(&g_dr_lock);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	status = dat_dictionary_search(g_dr_dictionary,
179*0Sstevel@tonic-gate 	    info,
180*0Sstevel@tonic-gate 	    (DAT_DICTIONARY_DATA *) &data);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
183*0Sstevel@tonic-gate 		/* return status from dat_dictionary_search() */
184*0Sstevel@tonic-gate 		goto bail;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (0 != data->ref_count) {
188*0Sstevel@tonic-gate 		status = DAT_ERROR(DAT_PROVIDER_IN_USE, 0);
189*0Sstevel@tonic-gate 		goto bail;
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	dict_entry = NULL;
193*0Sstevel@tonic-gate 	status = dat_dictionary_remove(g_dr_dictionary,
194*0Sstevel@tonic-gate 	    &dict_entry,
195*0Sstevel@tonic-gate 	    info,
196*0Sstevel@tonic-gate 	    (DAT_DICTIONARY_DATA *) &data);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (DAT_SUCCESS != status) {
199*0Sstevel@tonic-gate 		/* return status from dat_dictionary_remove() */
200*0Sstevel@tonic-gate 		goto bail;
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	dat_os_free(data, sizeof (DAT_DR_ENTRY));
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate bail:
206*0Sstevel@tonic-gate 	dat_os_unlock(&g_dr_lock);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (NULL != dict_entry) {
209*0Sstevel@tonic-gate 		(void) dat_dictionary_entry_destroy(dict_entry);
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	return (status);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate /*
217*0Sstevel@tonic-gate  * Function: dat_dr_provider_open
218*0Sstevel@tonic-gate  */
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate extern DAT_RETURN
dat_dr_provider_open(IN const DAT_PROVIDER_INFO * info,OUT DAT_IA_OPEN_FUNC * p_ia_open_func)221*0Sstevel@tonic-gate dat_dr_provider_open(
222*0Sstevel@tonic-gate     IN  const DAT_PROVIDER_INFO *info,
223*0Sstevel@tonic-gate     OUT DAT_IA_OPEN_FUNC	*p_ia_open_func)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	DAT_RETURN 		status;
226*0Sstevel@tonic-gate 	DAT_DR_ENTRY 		*data;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	dat_os_lock(&g_dr_lock);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	status = dat_dictionary_search(g_dr_dictionary,
231*0Sstevel@tonic-gate 				info,
232*0Sstevel@tonic-gate 				(DAT_DICTIONARY_DATA *) &data);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	dat_os_unlock(&g_dr_lock);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (DAT_SUCCESS == status) {
237*0Sstevel@tonic-gate 		data->ref_count++;
238*0Sstevel@tonic-gate 		*p_ia_open_func = data->ia_open_func;
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	return (status);
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate /*
246*0Sstevel@tonic-gate  * Function: dat_dr_provider_close
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate extern DAT_RETURN
dat_dr_provider_close(IN const DAT_PROVIDER_INFO * info)250*0Sstevel@tonic-gate dat_dr_provider_close(
251*0Sstevel@tonic-gate     IN  const DAT_PROVIDER_INFO *info)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	DAT_RETURN 		status;
254*0Sstevel@tonic-gate 	DAT_DR_ENTRY 		*data;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	dat_os_lock(&g_dr_lock);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	status = dat_dictionary_search(g_dr_dictionary,
259*0Sstevel@tonic-gate 	    info,
260*0Sstevel@tonic-gate 	    (DAT_DICTIONARY_DATA *) &data);
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	dat_os_unlock(&g_dr_lock);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (DAT_SUCCESS == status) {
265*0Sstevel@tonic-gate 		data->ref_count--;
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	return (status);
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*
273*0Sstevel@tonic-gate  * Function: dat_dr_size
274*0Sstevel@tonic-gate  */
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate DAT_RETURN
dat_dr_size(OUT DAT_COUNT * size)277*0Sstevel@tonic-gate dat_dr_size(
278*0Sstevel@tonic-gate     OUT	DAT_COUNT		*size)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	return (dat_dictionary_size(g_dr_dictionary, size));
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate /*
285*0Sstevel@tonic-gate  * Function: dat_dr_list
286*0Sstevel@tonic-gate  */
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate DAT_RETURN
dat_dr_list(IN DAT_COUNT max_to_return,OUT DAT_COUNT * entries_returned,OUT DAT_PROVIDER_INFO * (dat_provider_list[]))289*0Sstevel@tonic-gate dat_dr_list(
290*0Sstevel@tonic-gate     IN  DAT_COUNT		max_to_return,
291*0Sstevel@tonic-gate     OUT DAT_COUNT		*entries_returned,
292*0Sstevel@tonic-gate     OUT DAT_PROVIDER_INFO	* (dat_provider_list[]))
293*0Sstevel@tonic-gate {
294*0Sstevel@tonic-gate 	DAT_DR_ENTRY		**array;
295*0Sstevel@tonic-gate 	DAT_COUNT 		array_size;
296*0Sstevel@tonic-gate 	DAT_COUNT 		i;
297*0Sstevel@tonic-gate 	DAT_RETURN 		status;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	array = NULL;
300*0Sstevel@tonic-gate 	status = DAT_SUCCESS;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/*
303*0Sstevel@tonic-gate 	 * The dictionary size may increase between the call to
304*0Sstevel@tonic-gate 	 * dat_dictionary_size() and dat_dictionary_enumerate().
305*0Sstevel@tonic-gate 	 * Therefore we loop until a successful enumeration is made.
306*0Sstevel@tonic-gate 	 */
307*0Sstevel@tonic-gate 	*entries_returned = 0;
308*0Sstevel@tonic-gate 	for (;;) {
309*0Sstevel@tonic-gate 		status = dat_dictionary_size(g_dr_dictionary, &array_size);
310*0Sstevel@tonic-gate 		if (status != DAT_SUCCESS) {
311*0Sstevel@tonic-gate 			goto bail;
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 		if (array_size == 0) {
315*0Sstevel@tonic-gate 			status = DAT_SUCCESS;
316*0Sstevel@tonic-gate 			goto bail;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		array = dat_os_alloc(array_size * sizeof (DAT_DR_ENTRY *));
320*0Sstevel@tonic-gate 		if (array == NULL) {
321*0Sstevel@tonic-gate 			status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
322*0Sstevel@tonic-gate 			    DAT_RESOURCE_MEMORY);
323*0Sstevel@tonic-gate 			goto bail;
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 		dat_os_lock(&g_dr_lock);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		status = dat_dictionary_enumerate(g_dr_dictionary,
329*0Sstevel@tonic-gate 		    (DAT_DICTIONARY_DATA *) array,
330*0Sstevel@tonic-gate 		    array_size);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		dat_os_unlock(&g_dr_lock);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 		if (DAT_SUCCESS == status) {
335*0Sstevel@tonic-gate 			break;
336*0Sstevel@tonic-gate 		} else {
337*0Sstevel@tonic-gate 			dat_os_free(array,
338*0Sstevel@tonic-gate 			    array_size * sizeof (DAT_DR_ENTRY *));
339*0Sstevel@tonic-gate 			array = NULL;
340*0Sstevel@tonic-gate 			continue;
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	for (i = 0; (i < max_to_return) && (i < array_size); i++) {
345*0Sstevel@tonic-gate 		if (NULL == dat_provider_list[i]) {
346*0Sstevel@tonic-gate 			status = DAT_ERROR(DAT_INVALID_PARAMETER,
347*0Sstevel@tonic-gate 			    DAT_INVALID_ARG3);
348*0Sstevel@tonic-gate 			goto bail;
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 		*dat_provider_list[i] = array[i]->info;
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	*entries_returned = i;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate bail:
357*0Sstevel@tonic-gate 	if (NULL != array) {
358*0Sstevel@tonic-gate 		dat_os_free(array, array_size * sizeof (DAT_DR_ENTRY *));
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	return (status);
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * Local variables:
366*0Sstevel@tonic-gate  *  c-indent-level: 4
367*0Sstevel@tonic-gate  *  c-basic-offset: 4
368*0Sstevel@tonic-gate  *  tab-width: 8
369*0Sstevel@tonic-gate  * End:
370*0Sstevel@tonic-gate  */
371