xref: /titanic_51/usr/src/lib/libidmap/common/utils.c (revision 4edd44c51fec55142bfb7fb1ee7aaa13a910a80f)
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 /*
22*4edd44c5Sjp151216  * Copyright 2008 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*4edd44c5Sjp151216 _udt_extend_batch(idmap_udt_handle_t *udthandle)
46*4edd44c5Sjp151216 {
47c5c4113dSnw141292 	idmap_update_op	*tmplist;
48c5c4113dSnw141292 	size_t		nsize;
49c5c4113dSnw141292 
50c5c4113dSnw141292 	if (udthandle->next >= udthandle->batch.idmap_update_batch_len) {
51c5c4113dSnw141292 		nsize = (udthandle->batch.idmap_update_batch_len +
52c5c4113dSnw141292 		    _UDT_SIZE_INCR) * sizeof (*tmplist);
53c5c4113dSnw141292 		tmplist = realloc(
54c5c4113dSnw141292 		    udthandle->batch.idmap_update_batch_val, nsize);
55c5c4113dSnw141292 		if (tmplist == NULL)
56c5c4113dSnw141292 			return (IDMAP_ERR_MEMORY);
57c5c4113dSnw141292 		(void) memset((uchar_t *)tmplist +
58c5c4113dSnw141292 		    (udthandle->batch.idmap_update_batch_len *
59c5c4113dSnw141292 		    sizeof (*tmplist)), 0,
60c5c4113dSnw141292 		    _UDT_SIZE_INCR * sizeof (*tmplist));
61c5c4113dSnw141292 		udthandle->batch.idmap_update_batch_val = tmplist;
62c5c4113dSnw141292 		udthandle->batch.idmap_update_batch_len += _UDT_SIZE_INCR;
63c5c4113dSnw141292 	}
64c5c4113dSnw141292 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
65651c0131Sbaban 	    OP_NONE;
66c5c4113dSnw141292 	return (IDMAP_SUCCESS);
67c5c4113dSnw141292 }
68c5c4113dSnw141292 
69c5c4113dSnw141292 idmap_retcode
70*4edd44c5Sjp151216 _get_ids_extend_batch(idmap_get_handle_t *gh)
71*4edd44c5Sjp151216 {
72c5c4113dSnw141292 	idmap_mapping	*t1;
73c5c4113dSnw141292 	idmap_get_res_t	*t2;
74c5c4113dSnw141292 	size_t		nsize, len;
75c5c4113dSnw141292 
76c5c4113dSnw141292 	len = gh->batch.idmap_mapping_batch_len;
77c5c4113dSnw141292 	if (gh->next >= len) {
78c5c4113dSnw141292 		/* extend the request array */
79c5c4113dSnw141292 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t1);
80c5c4113dSnw141292 		t1 = realloc(gh->batch.idmap_mapping_batch_val, nsize);
81c5c4113dSnw141292 		if (t1 == NULL)
82c5c4113dSnw141292 			return (IDMAP_ERR_MEMORY);
83c5c4113dSnw141292 		(void) memset((uchar_t *)t1 + (len * sizeof (*t1)), 0,
84c5c4113dSnw141292 		    _GET_IDS_SIZE_INCR * sizeof (*t1));
85c5c4113dSnw141292 		gh->batch.idmap_mapping_batch_val = t1;
86c5c4113dSnw141292 
87c5c4113dSnw141292 		/* extend the return list */
88c5c4113dSnw141292 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t2);
89c5c4113dSnw141292 		t2 = realloc(gh->retlist, nsize);
90c5c4113dSnw141292 		if (t2 == NULL)
91c5c4113dSnw141292 			return (IDMAP_ERR_MEMORY);
92c5c4113dSnw141292 		(void) memset((uchar_t *)t2 + (len * sizeof (*t2)), 0,
93c5c4113dSnw141292 		    _GET_IDS_SIZE_INCR * sizeof (*t2));
94c5c4113dSnw141292 		gh->retlist = t2;
95c5c4113dSnw141292 
96c5c4113dSnw141292 		gh->batch.idmap_mapping_batch_len += _GET_IDS_SIZE_INCR;
97c5c4113dSnw141292 	}
98c5c4113dSnw141292 	return (IDMAP_SUCCESS);
99c5c4113dSnw141292 }
100c5c4113dSnw141292 
101c5c4113dSnw141292 idmap_stat
102c5c4113dSnw141292 _iter_get_next_list(int type, idmap_iter_t *iter,
103c5c4113dSnw141292 		void *arg, uchar_t **list, size_t valsize,
104*4edd44c5Sjp151216 		xdrproc_t xdr_arg_proc, xdrproc_t xdr_res_proc)
105*4edd44c5Sjp151216 {
106c5c4113dSnw141292 
107c5c4113dSnw141292 	CLIENT		*clnt;
108c5c4113dSnw141292 	enum clnt_stat	clntstat;
109c5c4113dSnw141292 
110c5c4113dSnw141292 	iter->next = 0;
111c5c4113dSnw141292 	iter->retlist = NULL;
112c5c4113dSnw141292 	_IDMAP_GET_CLIENT_HANDLE(iter->ih, clnt);
113c5c4113dSnw141292 
114c5c4113dSnw141292 	/* init the result */
115c5c4113dSnw141292 	if (*list) {
116c5c4113dSnw141292 		xdr_free(xdr_res_proc, (caddr_t)*list);
117c5c4113dSnw141292 	} else {
118c5c4113dSnw141292 		if ((*list = malloc(valsize)) == NULL) {
119c5c4113dSnw141292 			errno = ENOMEM;
120c5c4113dSnw141292 			return (IDMAP_ERR_MEMORY);
121c5c4113dSnw141292 		}
122c5c4113dSnw141292 	}
123c5c4113dSnw141292 	(void) memset(*list, 0, valsize);
124c5c4113dSnw141292 
125c5c4113dSnw141292 	clntstat = clnt_call(clnt, type,
126c5c4113dSnw141292 	    xdr_arg_proc, (caddr_t)arg,
127c5c4113dSnw141292 	    xdr_res_proc, (caddr_t)*list,
128c5c4113dSnw141292 	    TIMEOUT);
129c5c4113dSnw141292 	if (clntstat != RPC_SUCCESS) {
130c5c4113dSnw141292 		free(*list);
131651c0131Sbaban 		return (_idmap_rpc2stat(clnt));
132c5c4113dSnw141292 	}
133c5c4113dSnw141292 	iter->retlist = *list;
134c5c4113dSnw141292 	return (IDMAP_SUCCESS);
135c5c4113dSnw141292 }
136651c0131Sbaban 
137651c0131Sbaban idmap_stat
138*4edd44c5Sjp151216 _idmap_rpc2stat(CLIENT *clnt)
139*4edd44c5Sjp151216 {
140651c0131Sbaban 	/*
141651c0131Sbaban 	 * We only deal with door_call(3C) errors here. We look at
142651c0131Sbaban 	 * r_err.re_errno instead of r_err.re_status because we need
143651c0131Sbaban 	 * to differentiate between RPC failures caused by bad door fd
144651c0131Sbaban 	 * and others.
145651c0131Sbaban 	 */
146651c0131Sbaban 	struct rpc_err r_err;
147651c0131Sbaban 	if (clnt) {
148651c0131Sbaban 		clnt_geterr(clnt, &r_err);
149651c0131Sbaban 		errno = r_err.re_errno;
150651c0131Sbaban 		switch (r_err.re_errno) {
151651c0131Sbaban 		case ENOMEM:
152651c0131Sbaban 			return (IDMAP_ERR_MEMORY);
153651c0131Sbaban 		case EBADF:
154651c0131Sbaban 			return (IDMAP_ERR_RPC_HANDLE);
155651c0131Sbaban 		default:
156651c0131Sbaban 			return (IDMAP_ERR_RPC);
157651c0131Sbaban 		}
158651c0131Sbaban 	}
159651c0131Sbaban 
160651c0131Sbaban 	/* null handle */
161651c0131Sbaban 	return (IDMAP_ERR_RPC_HANDLE);
162651c0131Sbaban }
163