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