1*4520Snw141292 /*
2*4520Snw141292  * CDDL HEADER START
3*4520Snw141292  *
4*4520Snw141292  * The contents of this file are subject to the terms of the
5*4520Snw141292  * Common Development and Distribution License (the "License").
6*4520Snw141292  * You may not use this file except in compliance with the License.
7*4520Snw141292  *
8*4520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4520Snw141292  * or http://www.opensolaris.org/os/licensing.
10*4520Snw141292  * See the License for the specific language governing permissions
11*4520Snw141292  * and limitations under the License.
12*4520Snw141292  *
13*4520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
14*4520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
16*4520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
17*4520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4520Snw141292  *
19*4520Snw141292  * CDDL HEADER END
20*4520Snw141292  */
21*4520Snw141292 /*
22*4520Snw141292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*4520Snw141292  * Use is subject to license terms.
24*4520Snw141292  */
25*4520Snw141292 
26*4520Snw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*4520Snw141292 
28*4520Snw141292 /*
29*4520Snw141292  * Utility routines
30*4520Snw141292  */
31*4520Snw141292 
32*4520Snw141292 #include <stdio.h>
33*4520Snw141292 #include <stdlib.h>
34*4520Snw141292 #include <errno.h>
35*4520Snw141292 #include <libintl.h>
36*4520Snw141292 #include "idmap_impl.h"
37*4520Snw141292 
38*4520Snw141292 #define	_UDT_SIZE_INCR	1
39*4520Snw141292 
40*4520Snw141292 #define	_GET_IDS_SIZE_INCR	1
41*4520Snw141292 
42*4520Snw141292 static struct timeval TIMEOUT = { 25, 0 };
43*4520Snw141292 
44*4520Snw141292 extern int __idmap_verbose;
45*4520Snw141292 
46*4520Snw141292 idmap_retcode
47*4520Snw141292 _udt_extend_batch(idmap_udt_handle_t *udthandle, int opnum) {
48*4520Snw141292 	idmap_update_op	*tmplist;
49*4520Snw141292 	size_t		nsize;
50*4520Snw141292 
51*4520Snw141292 	if (udthandle->next >= udthandle->batch.idmap_update_batch_len) {
52*4520Snw141292 		nsize = (udthandle->batch.idmap_update_batch_len +
53*4520Snw141292 				_UDT_SIZE_INCR) * sizeof (*tmplist);
54*4520Snw141292 		tmplist = realloc(
55*4520Snw141292 			udthandle->batch.idmap_update_batch_val, nsize);
56*4520Snw141292 		if (tmplist == NULL)
57*4520Snw141292 			return (IDMAP_ERR_MEMORY);
58*4520Snw141292 		(void) memset((uchar_t *)tmplist +
59*4520Snw141292 			(udthandle->batch.idmap_update_batch_len *
60*4520Snw141292 			sizeof (*tmplist)), 0,
61*4520Snw141292 			_UDT_SIZE_INCR * sizeof (*tmplist));
62*4520Snw141292 		udthandle->batch.idmap_update_batch_val = tmplist;
63*4520Snw141292 		udthandle->batch.idmap_update_batch_len += _UDT_SIZE_INCR;
64*4520Snw141292 	}
65*4520Snw141292 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
66*4520Snw141292 		opnum;
67*4520Snw141292 	return (IDMAP_SUCCESS);
68*4520Snw141292 }
69*4520Snw141292 
70*4520Snw141292 idmap_retcode
71*4520Snw141292 _get_ids_extend_batch(idmap_get_handle_t *gh) {
72*4520Snw141292 	idmap_mapping	*t1;
73*4520Snw141292 	idmap_get_res_t	*t2;
74*4520Snw141292 	size_t		nsize, len;
75*4520Snw141292 
76*4520Snw141292 	len = gh->batch.idmap_mapping_batch_len;
77*4520Snw141292 	if (gh->next >= len) {
78*4520Snw141292 		/* extend the request array */
79*4520Snw141292 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t1);
80*4520Snw141292 		t1 = realloc(gh->batch.idmap_mapping_batch_val, nsize);
81*4520Snw141292 		if (t1 == NULL)
82*4520Snw141292 			return (IDMAP_ERR_MEMORY);
83*4520Snw141292 		(void) memset((uchar_t *)t1 + (len * sizeof (*t1)), 0,
84*4520Snw141292 			_GET_IDS_SIZE_INCR * sizeof (*t1));
85*4520Snw141292 		gh->batch.idmap_mapping_batch_val = t1;
86*4520Snw141292 
87*4520Snw141292 		/* extend the return list */
88*4520Snw141292 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t2);
89*4520Snw141292 		t2 = realloc(gh->retlist, nsize);
90*4520Snw141292 		if (t2 == NULL)
91*4520Snw141292 			return (IDMAP_ERR_MEMORY);
92*4520Snw141292 		(void) memset((uchar_t *)t2 + (len * sizeof (*t2)), 0,
93*4520Snw141292 			_GET_IDS_SIZE_INCR * sizeof (*t2));
94*4520Snw141292 		gh->retlist = t2;
95*4520Snw141292 
96*4520Snw141292 		gh->batch.idmap_mapping_batch_len += _GET_IDS_SIZE_INCR;
97*4520Snw141292 	}
98*4520Snw141292 	return (IDMAP_SUCCESS);
99*4520Snw141292 }
100*4520Snw141292 
101*4520Snw141292 idmap_stat
102*4520Snw141292 _iter_get_next_list(int type, idmap_iter_t *iter,
103*4520Snw141292 		void *arg, uchar_t **list, size_t valsize,
104*4520Snw141292 		xdrproc_t xdr_arg_proc, xdrproc_t xdr_res_proc) {
105*4520Snw141292 
106*4520Snw141292 	CLIENT		*clnt;
107*4520Snw141292 	enum clnt_stat	clntstat;
108*4520Snw141292 	const char	*me = "_iter_get_next_list";
109*4520Snw141292 
110*4520Snw141292 	if (__idmap_verbose)
111*4520Snw141292 		(void) fprintf(stdout, "%s\n", me);
112*4520Snw141292 
113*4520Snw141292 	iter->next = 0;
114*4520Snw141292 	iter->retlist = NULL;
115*4520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(iter->ih, clnt);
116*4520Snw141292 
117*4520Snw141292 	/* init the result */
118*4520Snw141292 	if (*list) {
119*4520Snw141292 		xdr_free(xdr_res_proc, (caddr_t)*list);
120*4520Snw141292 	} else {
121*4520Snw141292 		if ((*list = malloc(valsize)) == NULL) {
122*4520Snw141292 			(void) fprintf(stderr, gettext("Out of memory\n"));
123*4520Snw141292 			errno = ENOMEM;
124*4520Snw141292 			return (IDMAP_ERR_MEMORY);
125*4520Snw141292 		}
126*4520Snw141292 	}
127*4520Snw141292 	(void) memset(*list, 0, valsize);
128*4520Snw141292 
129*4520Snw141292 	clntstat = clnt_call(clnt, type,
130*4520Snw141292 		xdr_arg_proc, (caddr_t)arg,
131*4520Snw141292 		xdr_res_proc, (caddr_t)*list,
132*4520Snw141292 		TIMEOUT);
133*4520Snw141292 	if (clntstat != RPC_SUCCESS) {
134*4520Snw141292 		free(*list);
135*4520Snw141292 		if (__idmap_verbose)
136*4520Snw141292 			clnt_perror(clnt, me);
137*4520Snw141292 		return (IDMAP_ERR_RPC);
138*4520Snw141292 	}
139*4520Snw141292 	iter->retlist = *list;
140*4520Snw141292 	return (IDMAP_SUCCESS);
141*4520Snw141292 }
142