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 <libintl.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <search.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "volume_dlist.h"
39*0Sstevel@tonic-gate #include "volume_error.h"
40*0Sstevel@tonic-gate #include "volume_output.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "layout_device_cache.h"
43*0Sstevel@tonic-gate #include "layout_dlist_util.h"
44*0Sstevel@tonic-gate #include "layout_request.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * Implementation note:
48*0Sstevel@tonic-gate  * The current caches are implemented as linked lists of data
49*0Sstevel@tonic-gate  * structures described below. Cached object lookup uses hsearch()
50*0Sstevel@tonic-gate  * where possible to minimize the inefficiency of linear search.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * The name and attribute maps use hesarch() for faster lookup
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static const uint32_t	MAX_CACHED_OBJECTS = 50000;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * The attribute cache is maintained as a list of these
60*0Sstevel@tonic-gate  * structs which map a device name to attributes.  The
61*0Sstevel@tonic-gate  * device name is the unique device name returned from
62*0Sstevel@tonic-gate  * the device library, typically a devfs path.  It should
63*0Sstevel@tonic-gate  * not be confused with the "display" name of the device
64*0Sstevel@tonic-gate  * which is typically a CTD or DID name.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate typedef struct {
67*0Sstevel@tonic-gate 	char		*name;
68*0Sstevel@tonic-gate 	nvlist_t	*attrs;
69*0Sstevel@tonic-gate } attr_cache_t;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static dlist_t	*_attr_cache = NULL;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * The name cache is maintained via a list of these structs
75*0Sstevel@tonic-gate  * which map a descriptor to its name.
76*0Sstevel@tonic-gate  * The descriptor is saved as a string for hsearch()
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate typedef struct {
79*0Sstevel@tonic-gate 	char		*desc;
80*0Sstevel@tonic-gate 	char		*name;
81*0Sstevel@tonic-gate } name_cache_t;
82*0Sstevel@tonic-gate static dlist_t *_name_cache = NULL;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * The desc cache is maintained as a list of these
86*0Sstevel@tonic-gate  * structs which map a device display name (CTD or DID)
87*0Sstevel@tonic-gate  * or alias to a descriptor.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate typedef struct {
90*0Sstevel@tonic-gate 	char		*name;
91*0Sstevel@tonic-gate 	dm_descriptor_t desc;
92*0Sstevel@tonic-gate } desc_cache_t;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate static dlist_t	*_desc_cache = NULL;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * Since each of the lookup caches shares the same hsearch()
98*0Sstevel@tonic-gate  * hash table, the names used as lookup keys for the desc_cache_t
99*0Sstevel@tonic-gate  * and attr_cache_t may cause collisions.
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  * The desc_cache_t map alters the device name by prepending
102*0Sstevel@tonic-gate  * this string to avoid collisions.
103*0Sstevel@tonic-gate  */
104*0Sstevel@tonic-gate static const char *DESC_CACHE_KEY_PREFIX = "desc_cache";
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * The set of descriptors to be returned to libdiskmgt is
108*0Sstevel@tonic-gate  * maintained via a list of dm_descriptor_t handles.
109*0Sstevel@tonic-gate  * descriptors are added by new_descriptor() and
110*0Sstevel@tonic-gate  * cache_descriptor_to_free().
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate typedef struct {
113*0Sstevel@tonic-gate 	dm_descriptor_t desc;
114*0Sstevel@tonic-gate 	boolean_t	virtual;
115*0Sstevel@tonic-gate } desc_free_t;
116*0Sstevel@tonic-gate static dlist_t	*_desc_to_free = NULL;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static char	*find_cached_name(dm_descriptor_t desc);
119*0Sstevel@tonic-gate static nvlist_t *find_cached_attrs(char *name);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static int	add_descriptor_to_free(dm_descriptor_t desc);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static void	release_name_cache();
124*0Sstevel@tonic-gate static void	release_desc_to_free_cache();
125*0Sstevel@tonic-gate static void	release_attribute_cache();
126*0Sstevel@tonic-gate static void	release_descriptor_cache();
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static uint32_t interal_name_count = 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate /*
131*0Sstevel@tonic-gate  * FUNCTION:	create_device_caches()
132*0Sstevel@tonic-gate  *
133*0Sstevel@tonic-gate  * PURPOSE:	Helper which initializes the module's private data
134*0Sstevel@tonic-gate  *		structures.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate int
create_device_caches()137*0Sstevel@tonic-gate create_device_caches()
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	if (hcreate(MAX_CACHED_OBJECTS) == 0) {
140*0Sstevel@tonic-gate 	    return (ENOMEM);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	return (0);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate /*
147*0Sstevel@tonic-gate  * FUNCTION:	release_device_caches()
148*0Sstevel@tonic-gate  *
149*0Sstevel@tonic-gate  * PURPOSE:	Helper which cleans up memory allocated to the module's
150*0Sstevel@tonic-gate  *		private data structures.
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate int
release_device_caches()153*0Sstevel@tonic-gate release_device_caches()
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	release_name_cache();
156*0Sstevel@tonic-gate 	release_desc_to_free_cache();
157*0Sstevel@tonic-gate 	release_attribute_cache();
158*0Sstevel@tonic-gate 	release_descriptor_cache();
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	return (0);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * FUNCTION:	free_desc_cache_object(void *obj)
165*0Sstevel@tonic-gate  *
166*0Sstevel@tonic-gate  * INPUT:	obj	- opaque pointer
167*0Sstevel@tonic-gate  *
168*0Sstevel@tonic-gate  * PURPOSE:	Frees memory associated with an entry in the
169*0Sstevel@tonic-gate  *		desc cache.
170*0Sstevel@tonic-gate  *
171*0Sstevel@tonic-gate  *		Assumes that the input object is a pointer
172*0Sstevel@tonic-gate  *		to a desc_cache_t struct.
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate static void
free_desc_cache_object(void * obj)175*0Sstevel@tonic-gate free_desc_cache_object(
176*0Sstevel@tonic-gate 	void	*obj)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	if (obj == NULL) {
179*0Sstevel@tonic-gate 	    return;
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	free(((desc_cache_t *)obj)->name);
183*0Sstevel@tonic-gate 	free(obj);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * FUNCTION:	release_descriptor_cache()
187*0Sstevel@tonic-gate  *
188*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
189*0Sstevel@tonic-gate  *			 !0 otherwise
190*0Sstevel@tonic-gate  *
191*0Sstevel@tonic-gate  * PURPOSE:	Frees all entries in the name cache.
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate static void
release_descriptor_cache()194*0Sstevel@tonic-gate release_descriptor_cache()
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
197*0Sstevel@tonic-gate 		gettext("  destroying descriptor cache (%d items)\n"),
198*0Sstevel@tonic-gate 		dlist_length(_desc_cache));
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	dlist_free_items(_desc_cache, free_desc_cache_object);
201*0Sstevel@tonic-gate 	_desc_cache = NULL;
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  * FUNCTION:	add_cached_descriptor(char *name, dm_descriptor_t desc)
206*0Sstevel@tonic-gate  *
207*0Sstevel@tonic-gate  * INPUT:	name	- a device name
208*0Sstevel@tonic-gate  *		desc	- a dm_descriptor_t handle
209*0Sstevel@tonic-gate  *
210*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
211*0Sstevel@tonic-gate  *			 !0 otherwise
212*0Sstevel@tonic-gate  *
213*0Sstevel@tonic-gate  * PURPOSE:	Adds an entry to the descriptor cache using the input
214*0Sstevel@tonic-gate  *		descriptor and name.
215*0Sstevel@tonic-gate  *
216*0Sstevel@tonic-gate  * 		Note that all of the lookup caches shares the same hsearch()
217*0Sstevel@tonic-gate  *		hash table and that the names used as lookup keys for the
218*0Sstevel@tonic-gate  *		desc_cache_t and attr_cache_t cause collisions.
219*0Sstevel@tonic-gate  *
220*0Sstevel@tonic-gate  *		The desc_cache_t map alters the device name to avoid collisions.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate int
add_cached_descriptor(char * name,dm_descriptor_t desc)223*0Sstevel@tonic-gate add_cached_descriptor(
224*0Sstevel@tonic-gate 	char		*name,
225*0Sstevel@tonic-gate 	dm_descriptor_t	desc)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	desc_cache_t	*dcp;
228*0Sstevel@tonic-gate 	char		buf[MAXNAMELEN+1];
229*0Sstevel@tonic-gate 	dlist_t		*item;
230*0Sstevel@tonic-gate 	ENTRY		entry;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if ((dcp = (desc_cache_t *)
233*0Sstevel@tonic-gate 	    calloc(1, sizeof (desc_cache_t))) == NULL) {
234*0Sstevel@tonic-gate 	    return (ENOMEM);
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	dcp->desc = desc;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	(void) snprintf(buf, MAXNAMELEN, "%s-%s", DESC_CACHE_KEY_PREFIX, name);
240*0Sstevel@tonic-gate 	dcp->name = strdup(buf);
241*0Sstevel@tonic-gate 	if (dcp->name == NULL) {
242*0Sstevel@tonic-gate 	    free(dcp);
243*0Sstevel@tonic-gate 	    return (ENOMEM);
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/*
247*0Sstevel@tonic-gate 	 * insert into the hashtable... ignore the return from hsearch(),
248*0Sstevel@tonic-gate 	 * there is no existing entry corresponding to desc since the
249*0Sstevel@tonic-gate 	 * map was already searched just before this function is called,
250*0Sstevel@tonic-gate 	 * see get_name() below
251*0Sstevel@tonic-gate 	 */
252*0Sstevel@tonic-gate 	entry.key  = dcp->name;
253*0Sstevel@tonic-gate 	entry.data = (void *)dcp;
254*0Sstevel@tonic-gate 	(void) hsearch(entry, ENTER);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/* insert into the list cache... */
257*0Sstevel@tonic-gate 	if ((item = dlist_new_item((void *)dcp)) == NULL) {
258*0Sstevel@tonic-gate 	    free(dcp);
259*0Sstevel@tonic-gate 	    return (ENOMEM);
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	_desc_cache = dlist_append(item, _desc_cache, AT_HEAD);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	return (0);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * FUNCTION:	dm_descriptor_t find_cached_descriptor(char *name)
269*0Sstevel@tonic-gate  *
270*0Sstevel@tonic-gate  * INPUT:	char * - pointer to a name or alias.
271*0Sstevel@tonic-gate  *
272*0Sstevel@tonic-gate  * RETURNS:	dm_descriptor_t - dm_descriptor_t handle cached under the
273*0Sstevel@tonic-gate  *			input name if a match is found.  A null descriptor
274*0Sstevel@tonic-gate  *			is returned if no match is found.
275*0Sstevel@tonic-gate  *
276*0Sstevel@tonic-gate  * PURPOSE:	Searches for the desc that has been cached for
277*0Sstevel@tonic-gate  *		the input device name.
278*0Sstevel@tonic-gate  *
279*0Sstevel@tonic-gate  * 		Note that all of the lookup caches shares the same hsearch()
280*0Sstevel@tonic-gate  *		hash table and that the names used as lookup keys for the
281*0Sstevel@tonic-gate  *		desc_cache_t and attr_cache_t cause collisions.
282*0Sstevel@tonic-gate  *
283*0Sstevel@tonic-gate  *		The desc_cache_t map alters the device name to avoid collisions.
284*0Sstevel@tonic-gate  */
285*0Sstevel@tonic-gate dm_descriptor_t
find_cached_descriptor(char * name)286*0Sstevel@tonic-gate find_cached_descriptor(
287*0Sstevel@tonic-gate 	char		*name)
288*0Sstevel@tonic-gate {
289*0Sstevel@tonic-gate 	ENTRY		item;
290*0Sstevel@tonic-gate 	ENTRY		*cached_item = NULL;
291*0Sstevel@tonic-gate 	char		buf[MAXNAMELEN+1];
292*0Sstevel@tonic-gate 	dm_descriptor_t	desc = (dm_descriptor_t)0;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	(void) snprintf(buf, MAXNAMELEN, "%s-%s", DESC_CACHE_KEY_PREFIX, name);
295*0Sstevel@tonic-gate 	item.key = buf;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	/* get descriptor associated with this name */
298*0Sstevel@tonic-gate 	if ((cached_item = hsearch(item, FIND)) != NULL) {
299*0Sstevel@tonic-gate 	    /* LINTED */
300*0Sstevel@tonic-gate 	    desc = ((desc_cache_t *)cached_item->data)->desc;
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	return (desc);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate  * FUNCTION:	free_name_cache_object(void *obj)
308*0Sstevel@tonic-gate  *
309*0Sstevel@tonic-gate  * INPUT:	obj	- opaque pointer
310*0Sstevel@tonic-gate  *
311*0Sstevel@tonic-gate  * PURPOSE:	Frees memory associated with an entry in the
312*0Sstevel@tonic-gate  *		name cache.
313*0Sstevel@tonic-gate  *
314*0Sstevel@tonic-gate  *		Assumes that the input object is a pointer
315*0Sstevel@tonic-gate  *		to a name_cache_t struct.
316*0Sstevel@tonic-gate  */
317*0Sstevel@tonic-gate static void
free_name_cache_object(void * obj)318*0Sstevel@tonic-gate free_name_cache_object(
319*0Sstevel@tonic-gate 	void	*obj)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	if (obj == NULL) {
322*0Sstevel@tonic-gate 	    return;
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	free(((name_cache_t *)obj)->desc);
326*0Sstevel@tonic-gate 	free(((name_cache_t *)obj)->name);
327*0Sstevel@tonic-gate 	free(obj);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate /*
331*0Sstevel@tonic-gate  * FUNCTION:	release_name_cache()
332*0Sstevel@tonic-gate  *
333*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
334*0Sstevel@tonic-gate  *			 !0 otherwise
335*0Sstevel@tonic-gate  *
336*0Sstevel@tonic-gate  * PURPOSE:	Frees all entries in the name cache.
337*0Sstevel@tonic-gate  */
338*0Sstevel@tonic-gate static void
release_name_cache()339*0Sstevel@tonic-gate release_name_cache()
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
342*0Sstevel@tonic-gate 		gettext("  destroying name cache (%d items)\n"),
343*0Sstevel@tonic-gate 		dlist_length(_name_cache));
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	dlist_free_items(_name_cache, free_name_cache_object);
346*0Sstevel@tonic-gate 	_name_cache = NULL;
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate /*
350*0Sstevel@tonic-gate  * FUNCTION:	add_cached_name(dm_descriptor_t desc, char *name)
351*0Sstevel@tonic-gate  *
352*0Sstevel@tonic-gate  * INPUT:	desc	- a dm_descriptor_t handle
353*0Sstevel@tonic-gate  *		name	- a device name
354*0Sstevel@tonic-gate  *
355*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
356*0Sstevel@tonic-gate  *			 !0 otherwise
357*0Sstevel@tonic-gate  *
358*0Sstevel@tonic-gate  * PURPOSE:	Adds an entry to the name cache using the input
359*0Sstevel@tonic-gate  *		descriptor and name.
360*0Sstevel@tonic-gate  */
361*0Sstevel@tonic-gate int
add_cached_name(dm_descriptor_t desc,char * name)362*0Sstevel@tonic-gate add_cached_name(
363*0Sstevel@tonic-gate 	dm_descriptor_t	desc,
364*0Sstevel@tonic-gate 	char		*name)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate 	name_cache_t	*ncp;
367*0Sstevel@tonic-gate 	char		buf[MAXNAMELEN+1];
368*0Sstevel@tonic-gate 	dlist_t		*item;
369*0Sstevel@tonic-gate 	ENTRY		entry;
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	if ((ncp = (name_cache_t *)
372*0Sstevel@tonic-gate 	    calloc(1, sizeof (name_cache_t))) == NULL) {
373*0Sstevel@tonic-gate 	    return (ENOMEM);
374*0Sstevel@tonic-gate 	}
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	(void) snprintf(buf, MAXNAMELEN, "%llu", desc);
377*0Sstevel@tonic-gate 	ncp->desc = strdup(buf);
378*0Sstevel@tonic-gate 	if (ncp->desc == NULL) {
379*0Sstevel@tonic-gate 	    free(ncp);
380*0Sstevel@tonic-gate 	    return (ENOMEM);
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	ncp->name = strdup(name);
384*0Sstevel@tonic-gate 	if (ncp->name == NULL) {
385*0Sstevel@tonic-gate 	    free(ncp->desc);
386*0Sstevel@tonic-gate 	    free(ncp);
387*0Sstevel@tonic-gate 	    return (ENOMEM);
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	/*
391*0Sstevel@tonic-gate 	 * insert into the hashtable... ignore the return from hsearch(),
392*0Sstevel@tonic-gate 	 * there is no existing entry corresponding to desc since the
393*0Sstevel@tonic-gate 	 * map was already searched just before this function is called,
394*0Sstevel@tonic-gate 	 * see get_name() below
395*0Sstevel@tonic-gate 	 */
396*0Sstevel@tonic-gate 	entry.key  = ncp->desc;
397*0Sstevel@tonic-gate 	entry.data = (void *)ncp;
398*0Sstevel@tonic-gate 	(void) hsearch(entry, ENTER);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/* insert into the list cache... */
401*0Sstevel@tonic-gate 	if ((item = dlist_new_item((void *)ncp)) == NULL) {
402*0Sstevel@tonic-gate 	    free(ncp->desc);
403*0Sstevel@tonic-gate 	    free(ncp->name);
404*0Sstevel@tonic-gate 	    free(ncp);
405*0Sstevel@tonic-gate 	    return (ENOMEM);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	_name_cache = dlist_append(item, _name_cache, AT_HEAD);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	return (0);
411*0Sstevel@tonic-gate }
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate /*
414*0Sstevel@tonic-gate  * FUNCTION:	char *find_cached_name(dm_descriptor_t desc)
415*0Sstevel@tonic-gate  *
416*0Sstevel@tonic-gate  * INPUT:	desc	- a dm_descriptor_t handle
417*0Sstevel@tonic-gate  *
418*0Sstevel@tonic-gate  * RETURNS:	char * - pointer to the name cached for the descriptor.
419*0Sstevel@tonic-gate  *			 Null otherwise.
420*0Sstevel@tonic-gate  *
421*0Sstevel@tonic-gate  * PURPOSE:	Searches for the name that has been cached for
422*0Sstevel@tonic-gate  *		the input dm_descriptor_t.
423*0Sstevel@tonic-gate  *
424*0Sstevel@tonic-gate  *		Search linked list.
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate static char *
find_cached_name(dm_descriptor_t desc)427*0Sstevel@tonic-gate find_cached_name(
428*0Sstevel@tonic-gate 	dm_descriptor_t	desc)
429*0Sstevel@tonic-gate {
430*0Sstevel@tonic-gate 	char		buf[MAXNAMELEN+1];
431*0Sstevel@tonic-gate 	ENTRY		item;
432*0Sstevel@tonic-gate 	ENTRY		*cached_item = NULL;
433*0Sstevel@tonic-gate 	char		*name = NULL;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	(void) snprintf(buf, MAXNAMELEN, "%llu", desc);
436*0Sstevel@tonic-gate 	item.key = buf;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	/* get name associated with this descriptor */
439*0Sstevel@tonic-gate 	if ((cached_item = hsearch(item, FIND)) != NULL) {
440*0Sstevel@tonic-gate 	    /* LINTED */
441*0Sstevel@tonic-gate 	    name = ((name_cache_t *)cached_item->data)->name;
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	return (name);
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate /*
448*0Sstevel@tonic-gate  * FUNCTION:	get_name(dm_descriptor_t desc,
449*0Sstevel@tonic-gate  *			char_t **name)
450*0Sstevel@tonic-gate  *
451*0Sstevel@tonic-gate  * INPUT:	desc	- a dm_descriptor_t handle
452*0Sstevel@tonic-gate  *
453*0Sstevel@tonic-gate  * OUTPUT:	name	- pointer to char * to hold the name
454*0Sstevel@tonic-gate  *
455*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
456*0Sstevel@tonic-gate  *			 !0 otherwise
457*0Sstevel@tonic-gate  *
458*0Sstevel@tonic-gate  * PURPOSE:	Searches for the name that has been cached for the
459*0Sstevel@tonic-gate  *		input dm_descriptor_t.
460*0Sstevel@tonic-gate  *
461*0Sstevel@tonic-gate  *		Names are cached using the dm_descriptor.
462*0Sstevel@tonic-gate  *		If no name has yet been cached, it is retrieved from
463*0Sstevel@tonic-gate  *		libdiskmgt and added to the cache.
464*0Sstevel@tonic-gate  *
465*0Sstevel@tonic-gate  *		Names are cached so that all name strings obtained from
466*0Sstevel@tonic-gate  *		libdiskmgt will get properly released when layout completes.
467*0Sstevel@tonic-gate  */
468*0Sstevel@tonic-gate int
get_name(dm_descriptor_t desc,char ** name)469*0Sstevel@tonic-gate get_name(
470*0Sstevel@tonic-gate 	dm_descriptor_t	desc,
471*0Sstevel@tonic-gate 	char		**name)
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	int		dm_free = 1;
475*0Sstevel@tonic-gate 	int		error = 0;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if ((desc != (dm_descriptor_t)0) &&
478*0Sstevel@tonic-gate 	    (*name = find_cached_name(desc)) == NULL) {
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	    /* not in descriptor->name cache/map, add it */
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	    if (is_virtual_slice(desc) != B_TRUE) {
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		dm_desc_type_t	type;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		*name = dm_get_name(desc, &error);
487*0Sstevel@tonic-gate 		if (error != 0) {
488*0Sstevel@tonic-gate 		    volume_set_error(
489*0Sstevel@tonic-gate 			    gettext("failed to get name for descriptor: %d\n"),
490*0Sstevel@tonic-gate 			    error);
491*0Sstevel@tonic-gate 		    return (-1);
492*0Sstevel@tonic-gate 		}
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 		/*
495*0Sstevel@tonic-gate 		 * some devices can be unnamed...
496*0Sstevel@tonic-gate 		 * assign a unique internal name if necessary
497*0Sstevel@tonic-gate 		 */
498*0Sstevel@tonic-gate 		if (*name == NULL) {
499*0Sstevel@tonic-gate 		    char buf[MAXNAMELEN];
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		    dm_free = 0;
502*0Sstevel@tonic-gate 		    (void) snprintf(buf, MAXNAMELEN-1, "temp-name-%lu",
503*0Sstevel@tonic-gate 			    interal_name_count++);
504*0Sstevel@tonic-gate 		    *name = strdup(buf);
505*0Sstevel@tonic-gate 		    if (*name == NULL) {
506*0Sstevel@tonic-gate 			volume_set_error(
507*0Sstevel@tonic-gate 			    gettext("failed to get name for descriptor: %d\n"),
508*0Sstevel@tonic-gate 			    errno);
509*0Sstevel@tonic-gate 			return (-1);
510*0Sstevel@tonic-gate 		    }
511*0Sstevel@tonic-gate 		    oprintf(OUTPUT_DEBUG,
512*0Sstevel@tonic-gate 			    gettext("unnamed descriptor %llu assigned %s\n"),
513*0Sstevel@tonic-gate 			    desc, *name);
514*0Sstevel@tonic-gate 		}
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 		/*
517*0Sstevel@tonic-gate 		 * media can have the same name as the associated drive
518*0Sstevel@tonic-gate 		 * which hoses the attribute caching scheme, so unique-ify
519*0Sstevel@tonic-gate 		 */
520*0Sstevel@tonic-gate 		if ((type = dm_get_type(desc)) == DM_MEDIA) {
521*0Sstevel@tonic-gate 		    char buf[MAXNAMELEN];
522*0Sstevel@tonic-gate 		    (void) snprintf(buf, MAXNAMELEN-1, "%s-%d", *name, type);
523*0Sstevel@tonic-gate 		    error = add_cached_name(desc, buf);
524*0Sstevel@tonic-gate 		} else {
525*0Sstevel@tonic-gate 		    error = add_cached_name(desc, *name);
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 		if (dm_free)
528*0Sstevel@tonic-gate 		    dm_free_name(*name);
529*0Sstevel@tonic-gate 		else
530*0Sstevel@tonic-gate 		    free(*name);
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		if (error == 0) {
533*0Sstevel@tonic-gate 		    /* return copied name */
534*0Sstevel@tonic-gate 		    *name = find_cached_name(desc);
535*0Sstevel@tonic-gate 		} else {
536*0Sstevel@tonic-gate 		    *name = NULL;
537*0Sstevel@tonic-gate 		}
538*0Sstevel@tonic-gate 	    }
539*0Sstevel@tonic-gate 	}
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	return (error);
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate /*
545*0Sstevel@tonic-gate  * FUNCTION:	free_attr_cache_object(void *obj)
546*0Sstevel@tonic-gate  *
547*0Sstevel@tonic-gate  * INPUT:	obj	- opaque pointer
548*0Sstevel@tonic-gate  *
549*0Sstevel@tonic-gate  * PURPOSE:	Frees memory associated with an entry in the
550*0Sstevel@tonic-gate  *		attribute cache.
551*0Sstevel@tonic-gate  *
552*0Sstevel@tonic-gate  *		Assumes that the input object is a pointer
553*0Sstevel@tonic-gate  *		to a attr_cache_t struct.
554*0Sstevel@tonic-gate  */
555*0Sstevel@tonic-gate static void
free_attr_cache_object(void * obj)556*0Sstevel@tonic-gate free_attr_cache_object(
557*0Sstevel@tonic-gate 	void		*obj)
558*0Sstevel@tonic-gate {
559*0Sstevel@tonic-gate 	if (obj == NULL) {
560*0Sstevel@tonic-gate 	    return;
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	nvlist_free(((attr_cache_t *)obj)->attrs);
564*0Sstevel@tonic-gate 	free(obj);
565*0Sstevel@tonic-gate }
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate /*
568*0Sstevel@tonic-gate  * FUNCTION:	release_attribute_cache()
569*0Sstevel@tonic-gate  *
570*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
571*0Sstevel@tonic-gate  *			 !0 otherwise
572*0Sstevel@tonic-gate  *
573*0Sstevel@tonic-gate  * PURPOSE:	Frees all entries in the attribute cache.
574*0Sstevel@tonic-gate  */
575*0Sstevel@tonic-gate void
release_attribute_cache()576*0Sstevel@tonic-gate release_attribute_cache()
577*0Sstevel@tonic-gate {
578*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
579*0Sstevel@tonic-gate 		gettext("  destroying attribute cache (%d items)\n"),
580*0Sstevel@tonic-gate 		dlist_length(_attr_cache));
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	dlist_free_items(_attr_cache, free_attr_cache_object);
583*0Sstevel@tonic-gate 	_attr_cache = NULL;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	/* cleanup attribute cache lookup hashtable */
586*0Sstevel@tonic-gate 	hdestroy();
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate /*
590*0Sstevel@tonic-gate  * FUNCTION:	add_cached_attributes(char *name, nvlist_t *attrs)
591*0Sstevel@tonic-gate  *
592*0Sstevel@tonic-gate  * INPUT:	name	- a device name
593*0Sstevel@tonic-gate  *		attrs	- pointer to an nvlist_t attribute structure
594*0Sstevel@tonic-gate  *
595*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
596*0Sstevel@tonic-gate  *			 !0 otherwise
597*0Sstevel@tonic-gate  *
598*0Sstevel@tonic-gate  * PURPOSE:	Adds an entry to the attribute cache using the input
599*0Sstevel@tonic-gate  *		name and attributes.
600*0Sstevel@tonic-gate  *
601*0Sstevel@tonic-gate  *		Uses a linked list to cache attributes.
602*0Sstevel@tonic-gate  *		Keeps a parallel hash table for faster lookup.
603*0Sstevel@tonic-gate  */
604*0Sstevel@tonic-gate int
add_cached_attributes(char * name,nvlist_t * attrs)605*0Sstevel@tonic-gate add_cached_attributes(
606*0Sstevel@tonic-gate 	char		*name,
607*0Sstevel@tonic-gate 	nvlist_t 	*attrs)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	attr_cache_t	*acp = NULL;
610*0Sstevel@tonic-gate 	dlist_t		*item = NULL;
611*0Sstevel@tonic-gate 	ENTRY		*exist = NULL;
612*0Sstevel@tonic-gate 	ENTRY		entry;
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	/* insert into the hashtable... */
615*0Sstevel@tonic-gate 	entry.key  = name;
616*0Sstevel@tonic-gate 	entry.data = (void *)attrs;
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	if ((exist = hsearch(entry, ENTER)) != NULL) {
619*0Sstevel@tonic-gate 	    /* replace the existing attrs entry */
620*0Sstevel@tonic-gate 	    exist->data = (void *)attrs;
621*0Sstevel@tonic-gate 	}
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if ((acp = (attr_cache_t *)calloc(1, sizeof (attr_cache_t))) == NULL) {
624*0Sstevel@tonic-gate 	    return (ENOMEM);
625*0Sstevel@tonic-gate 	}
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 	acp->name = name;
628*0Sstevel@tonic-gate 	acp->attrs = attrs;
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	/* and cache of attr structs to be freed */
631*0Sstevel@tonic-gate 	if ((item = dlist_new_item((void *)acp)) == NULL) {
632*0Sstevel@tonic-gate 	    free(acp);
633*0Sstevel@tonic-gate 	    return (ENOMEM);
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	_attr_cache = dlist_append(item, _attr_cache, AT_HEAD);
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	return (0);
639*0Sstevel@tonic-gate }
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate /*
642*0Sstevel@tonic-gate  * FUNCTION:	nvlist_t *find_cached_attrs(char *name)
643*0Sstevel@tonic-gate  *
644*0Sstevel@tonic-gate  * INPUT:	name	- a device name
645*0Sstevel@tonic-gate  *
646*0Sstevel@tonic-gate  * RETURNS:	nvlist_t * - pointer to an nvlist_t attribute structure
647*0Sstevel@tonic-gate  *			cached under 'name'.  Null otherwise.
648*0Sstevel@tonic-gate  *
649*0Sstevel@tonic-gate  * PURPOSE:	Searches for the nvlist attributes that have been
650*0Sstevel@tonic-gate  *		cached for the input name.
651*0Sstevel@tonic-gate  */
652*0Sstevel@tonic-gate static nvlist_t *
find_cached_attrs(char * name)653*0Sstevel@tonic-gate find_cached_attrs(
654*0Sstevel@tonic-gate 	char		*name)
655*0Sstevel@tonic-gate {
656*0Sstevel@tonic-gate 	ENTRY		item;
657*0Sstevel@tonic-gate 	ENTRY		*cached_item = NULL;
658*0Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	item.key = name;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	/* get attributes cached under this name */
663*0Sstevel@tonic-gate 	if ((cached_item = hsearch(item, FIND)) != NULL) {
664*0Sstevel@tonic-gate 	    /* LINTED */
665*0Sstevel@tonic-gate 	    attrs = (nvlist_t *)cached_item->data;
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	return (attrs);
669*0Sstevel@tonic-gate }
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate /*
672*0Sstevel@tonic-gate  * FUNCTION:	get_cached_attributes(dm_descriptor_t desc,
673*0Sstevel@tonic-gate  *			nvlist_t **attrs)
674*0Sstevel@tonic-gate  *
675*0Sstevel@tonic-gate  * INPUT:	desc	- a dm_descriptor_t handle
676*0Sstevel@tonic-gate  *
677*0Sstevel@tonic-gate  * OUTPUT:	attrs	- pointer to an nvlist_t attribute structure
678*0Sstevel@tonic-gate  *
679*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
680*0Sstevel@tonic-gate  *			 !0 otherwise
681*0Sstevel@tonic-gate  *
682*0Sstevel@tonic-gate  * PURPOSE:	Searches for the nvlist attributes that have been
683*0Sstevel@tonic-gate  *		cached for the input dm_descriptor_t.
684*0Sstevel@tonic-gate  *
685*0Sstevel@tonic-gate  *		Attributes are cached using the name associated with
686*0Sstevel@tonic-gate  *		the descriptor.  If no attributes have yet been cached
687*0Sstevel@tonic-gate  *		they are retrieved from libdiskmgt and added to the
688*0Sstevel@tonic-gate  *		cache.
689*0Sstevel@tonic-gate  *
690*0Sstevel@tonic-gate  *		Attributes are cached so that layout may store transient
691*0Sstevel@tonic-gate  *		data relevant to the layout process.
692*0Sstevel@tonic-gate  */
693*0Sstevel@tonic-gate int
get_cached_attributes(dm_descriptor_t desc,nvlist_t ** attrs)694*0Sstevel@tonic-gate get_cached_attributes(
695*0Sstevel@tonic-gate 	dm_descriptor_t	desc,
696*0Sstevel@tonic-gate 	nvlist_t 	**attrs)
697*0Sstevel@tonic-gate {
698*0Sstevel@tonic-gate 	int		error = 0;
699*0Sstevel@tonic-gate 	char		*name = NULL;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	if ((desc != (dm_descriptor_t)0) &&
702*0Sstevel@tonic-gate 	    (error = get_name(desc, &name)) == 0) {
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	    if ((*attrs = find_cached_attrs(name)) == NULL) {
705*0Sstevel@tonic-gate 		/* get attrs and cache them */
706*0Sstevel@tonic-gate 		*attrs = dm_get_attributes(desc, &error);
707*0Sstevel@tonic-gate 		if (error == 0) {
708*0Sstevel@tonic-gate 		    error = add_cached_attributes(name, *attrs);
709*0Sstevel@tonic-gate 		}
710*0Sstevel@tonic-gate 	    }
711*0Sstevel@tonic-gate 	}
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	return (error);
714*0Sstevel@tonic-gate }
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate /*
717*0Sstevel@tonic-gate  * FUNCTION:	new_descriptor(dm_descriptor_t *desc)
718*0Sstevel@tonic-gate  *
719*0Sstevel@tonic-gate  * INPUT:	desc	- a pointer to a dm_descriptor_t to hold
720*0Sstevel@tonic-gate  *				the result.
721*0Sstevel@tonic-gate  *
722*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
723*0Sstevel@tonic-gate  *			 !0 otherwise
724*0Sstevel@tonic-gate  *
725*0Sstevel@tonic-gate  * PURPOSE:	Allocates a new dm_descriptor_t handle.
726*0Sstevel@tonic-gate  *
727*0Sstevel@tonic-gate  *		This is necessary because the process may have to
728*0Sstevel@tonic-gate  *		create "virtual" objects to represent devices that
729*0Sstevel@tonic-gate  *		do not yet exist on the system and hence are unknown
730*0Sstevel@tonic-gate  *		to libdiskmgt and diskmgtd.
731*0Sstevel@tonic-gate  *
732*0Sstevel@tonic-gate  *		A unique handle is created for such objects and may
733*0Sstevel@tonic-gate  *		be used by layout to access the virtual devices as
734*0Sstevel@tonic-gate  *		if they were obtained from libdiskmgt.
735*0Sstevel@tonic-gate  */
736*0Sstevel@tonic-gate int
new_descriptor(dm_descriptor_t * desc)737*0Sstevel@tonic-gate new_descriptor(
738*0Sstevel@tonic-gate 	dm_descriptor_t	*desc)
739*0Sstevel@tonic-gate {
740*0Sstevel@tonic-gate 	desc_free_t	*dfp;
741*0Sstevel@tonic-gate 	dlist_t		*item;
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	*desc = NULL;
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	if ((dfp = (desc_free_t *)
746*0Sstevel@tonic-gate 	    calloc(1, sizeof (desc_free_t))) == NULL) {
747*0Sstevel@tonic-gate 	    return (ENOMEM);
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	dfp->desc = (uintptr_t)dfp;
751*0Sstevel@tonic-gate 	dfp->virtual = B_TRUE;
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	if ((item = dlist_new_item((void *)dfp)) == NULL) {
754*0Sstevel@tonic-gate 	    free(dfp);
755*0Sstevel@tonic-gate 	    return (ENOMEM);
756*0Sstevel@tonic-gate 	}
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	_desc_to_free = dlist_append(item, _desc_to_free, AT_HEAD);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	*desc = (uintptr_t)dfp;
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	return (0);
763*0Sstevel@tonic-gate }
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate /*
766*0Sstevel@tonic-gate  * FUNCTION:	add_descriptors_to_free(dm_descriptor_t *desc)
767*0Sstevel@tonic-gate  *
768*0Sstevel@tonic-gate  * INPUT:	desc	- an array of dm_descriptor_t handles from
769*0Sstevel@tonic-gate  *				libdiskmgt
770*0Sstevel@tonic-gate  *
771*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
772*0Sstevel@tonic-gate  *			 !0 otherwise
773*0Sstevel@tonic-gate  *
774*0Sstevel@tonic-gate  * PURPOSE:	Function which accepts an array of dm_descriptor_t handles
775*0Sstevel@tonic-gate  *		that need to be returned to libdiskmgt.
776*0Sstevel@tonic-gate  *
777*0Sstevel@tonic-gate  *		The array is iterated and each handle is passed to
778*0Sstevel@tonic-gate  *		add_descriptor_to_free.
779*0Sstevel@tonic-gate  */
780*0Sstevel@tonic-gate int
add_descriptors_to_free(dm_descriptor_t * desc_list)781*0Sstevel@tonic-gate add_descriptors_to_free(
782*0Sstevel@tonic-gate 	dm_descriptor_t *desc_list)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	int i = 0;
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate 	if (desc_list != NULL) {
787*0Sstevel@tonic-gate 	    for (i = 0; desc_list[i] != NULL; i++) {
788*0Sstevel@tonic-gate 		(void) add_descriptor_to_free(desc_list[i]);
789*0Sstevel@tonic-gate 	    }
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	return (0);
793*0Sstevel@tonic-gate }
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate /*
796*0Sstevel@tonic-gate  * FUNCTION:	add_descriptor_to_free(dm_descriptor_t desc)
797*0Sstevel@tonic-gate  *
798*0Sstevel@tonic-gate  * INPUT:	desc	- dm_descriptor_t handle from libdiskmgt
799*0Sstevel@tonic-gate  *
800*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
801*0Sstevel@tonic-gate  *			 !0 otherwise
802*0Sstevel@tonic-gate  *
803*0Sstevel@tonic-gate  * PURPOSE:	Remembers a dm_descriptor_t handle which needs to be
804*0Sstevel@tonic-gate  *		returned to libdiskmgt. These handles represent memory
805*0Sstevel@tonic-gate  *		allocated by the the diskmgtd and must be returned in
806*0Sstevel@tonic-gate  *		order for that memory to be released.
807*0Sstevel@tonic-gate  *
808*0Sstevel@tonic-gate  *		The handles are cached for the duration of layout
809*0Sstevel@tonic-gate  *	        processing so that layout is guaranteed to have
810*0Sstevel@tonic-gate  *		unique handles for all objects received from
811*0Sstevel@tonic-gate  *		libdiskmgt.
812*0Sstevel@tonic-gate  *
813*0Sstevel@tonic-gate  *		The caching is accomplished by adding the handle to
814*0Sstevel@tonic-gate  *		a list of desc_free_t structs.
815*0Sstevel@tonic-gate  */
816*0Sstevel@tonic-gate static int
add_descriptor_to_free(dm_descriptor_t desc)817*0Sstevel@tonic-gate add_descriptor_to_free(
818*0Sstevel@tonic-gate 	dm_descriptor_t desc)
819*0Sstevel@tonic-gate {
820*0Sstevel@tonic-gate 	desc_free_t	*dfp = NULL;
821*0Sstevel@tonic-gate 	dlist_t		*item = NULL;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	if (desc == (dm_descriptor_t)0) {
824*0Sstevel@tonic-gate 	    return (0);
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	if (is_virtual_slice(desc) == B_TRUE) {
828*0Sstevel@tonic-gate 	    /* don't return virtual slice descriptors to libdiskmgt */
829*0Sstevel@tonic-gate 	    return (0);
830*0Sstevel@tonic-gate 	}
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	if ((dfp = calloc(1, sizeof (desc_free_t))) == NULL) {
833*0Sstevel@tonic-gate 	    return (ENOMEM);
834*0Sstevel@tonic-gate 	}
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	dfp->desc = desc;
837*0Sstevel@tonic-gate 	dfp->virtual = B_FALSE;
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 	if ((item = dlist_new_item((void *)dfp)) == NULL) {
840*0Sstevel@tonic-gate 	    free(dfp);
841*0Sstevel@tonic-gate 	    return (ENOMEM);
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	_desc_to_free = dlist_append(item, _desc_to_free, AT_HEAD);
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate 	return (0);
847*0Sstevel@tonic-gate }
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate /*
850*0Sstevel@tonic-gate  * FUNCTION:	release_desc_to_free_cache()
851*0Sstevel@tonic-gate  *
852*0Sstevel@tonic-gate  * PURPOSE:	Frees all entries in the desc_to_free cache.
853*0Sstevel@tonic-gate  *
854*0Sstevel@tonic-gate  *		Iterates the _desc_to_free list and builds an
855*0Sstevel@tonic-gate  *		array with all dm_descriptor_t handles that were
856*0Sstevel@tonic-gate  *		obtained from libdiskmgt.  Passing this array to
857*0Sstevel@tonic-gate  *		dm_free_descriptors() is faster than calling
858*0Sstevel@tonic-gate  *		dm_free_descriptor() to free individual	handles.
859*0Sstevel@tonic-gate  */
860*0Sstevel@tonic-gate void
release_desc_to_free_cache()861*0Sstevel@tonic-gate release_desc_to_free_cache()
862*0Sstevel@tonic-gate {
863*0Sstevel@tonic-gate 	dlist_t *iter;
864*0Sstevel@tonic-gate 	dm_descriptor_t *array;
865*0Sstevel@tonic-gate 	int i = 0;
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
868*0Sstevel@tonic-gate 		gettext("  destroying desc_to_free cache (%d items)\n"),
869*0Sstevel@tonic-gate 		dlist_length(_desc_to_free));
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	array = (dm_descriptor_t *)calloc(
872*0Sstevel@tonic-gate 		dlist_length(_desc_to_free) + 1, sizeof (dm_descriptor_t));
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 	if (array != NULL) {
875*0Sstevel@tonic-gate 	    for (iter = _desc_to_free; iter != NULL; iter = iter->next) {
876*0Sstevel@tonic-gate 		desc_free_t *dfp = (desc_free_t *)iter->obj;
877*0Sstevel@tonic-gate 		if (dfp->virtual == B_FALSE) {
878*0Sstevel@tonic-gate 		    array[i++] = dfp->desc;
879*0Sstevel@tonic-gate 		}
880*0Sstevel@tonic-gate 	    }
881*0Sstevel@tonic-gate 	    array[i] = (dm_descriptor_t)0;
882*0Sstevel@tonic-gate 	    dm_free_descriptors(array);
883*0Sstevel@tonic-gate 	}
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	/*
886*0Sstevel@tonic-gate 	 * If the calloc failed, the descriptors aren't explicitly freed,
887*0Sstevel@tonic-gate 	 * but the libdiskmgt daemon will eventually reclaim them after
888*0Sstevel@tonic-gate 	 * a period of inactivity.
889*0Sstevel@tonic-gate 	 */
890*0Sstevel@tonic-gate 	dlist_free_items(_desc_to_free, free);
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	_desc_to_free = NULL;
893*0Sstevel@tonic-gate }
894