2 /* $Id: nfs4_idmap.c,v 1.4 2003/11/05 14:58:59 rees Exp $ */
6 * the regents of the university of michigan
9 * permission is granted to use, copy, create derivative works and redistribute
10 * this software and such derivative works for any purpose, so long as the name
11 * of the university of michigan is not used in any advertising or publicity
12 * pertaining to the use or distribution of this software without specific,
13 * written prior authorization. if the above copyright notice or any other
14 * identification of the university of michigan is included in any copy of any
15 * portion of this software, then the disclaimer below must also be included.
17 * this software is provided as is, without representation from the university
18 * of michigan as to its fitness for any purpose, and without warranty by the
19 * university of michigan of any kind, either express or implied, including
20 * without limitation the implied warranties of merchantability and fitness for
21 * a particular purpose. the regents of the university of michigan shall not be
22 * liable for any damages, including special, indirect, incidental, or
23 * consequential damages, with respect to any claim arising out of or in
24 * connection with the use of the software, even if it has been or is hereafter
25 * advised of the possibility of such damages.
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/lockmgr.h>
36 #include <sys/fnv_hash.h>
38 #include <sys/syscall.h>
39 #include <sys/sysent.h>
40 #include <sys/libkern.h>
42 #include <rpc/rpcclnt.h>
44 #include <nfs4client/nfs4_dev.h>
45 #include <nfs4client/nfs4_idmap.h>
49 #define IDMAP_DEBUG(X...) printf(X);
51 #define IDMAP_DEBUG(X...)
54 #define IDMAP_HASH_SIZE 37
56 MALLOC_DEFINE(M_IDMAP, "idmap", "idmap");
58 #define idmap_entry_get(ID) MALLOC((ID), struct idmap_entry, sizeof(struct idmap_entry), M_IDMAP, M_WAITOK | M_ZERO)
59 #define idmap_entry_put(ID) FREE((ID), M_IDMAP)
64 struct idmap_msg id_info;
66 TAILQ_ENTRY(idmap_entry) id_entry_id;
67 TAILQ_ENTRY(idmap_entry) id_entry_name;
71 TAILQ_HEAD(, idmap_entry) hash_name[IDMAP_HASH_SIZE];
72 TAILQ_HEAD(, idmap_entry) hash_id[IDMAP_HASH_SIZE];
74 struct lock hash_lock;
77 #define IDMAP_RLOCK(lock) lockmgr(lock, LK_SHARED, NULL, curthread)
78 #define IDMAP_WLOCK(lock) lockmgr(lock, LK_EXCLUSIVE, NULL, curthread)
79 #define IDMAP_UNLOCK(lock) lockmgr(lock, LK_RELEASE, NULL, curthread)
82 static struct idmap_hash idmap_uid_hash;
83 static struct idmap_hash idmap_gid_hash;
85 static struct idmap_entry * idmap_name_lookup(uint32_t, char *);
86 static struct idmap_entry * idmap_id_lookup(uint32_t, ident_t);
87 static int idmap_upcall_name(uint32_t, char *, struct idmap_entry **);
88 static int idmap_upcall_id(uint32_t , ident_t, struct idmap_entry ** );
89 static int idmap_add(struct idmap_entry *);
92 idmap_upcall_name(uint32_t type, char * name, struct idmap_entry ** found)
95 struct idmap_entry * e;
98 if (type > IDMAP_MAX_TYPE || type == 0) {
99 IDMAP_DEBUG("bad type %d\n", type);
100 return EINVAL; /* XXX */
103 if (name == NULL || (len = strlen(name)) == 0 || len > IDMAP_MAXNAMELEN) {
104 IDMAP_DEBUG("idmap_upcall_name: bad name\n");
105 return EFAULT; /* XXX */
108 MALLOC(e, struct idmap_entry *, sizeof(struct idmap_entry), M_IDMAP,
111 e->id_info.id_type = type;
112 bcopy(name, e->id_info.id_name, len);
113 e->id_info.id_namelen = len;
116 siz = sizeof(struct idmap_msg);
117 error = nfs4dev_call(NFS4DEV_TYPE_IDMAP, (caddr_t)&e->id_info, siz,
118 (caddr_t)&e->id_info, &siz);
121 IDMAP_DEBUG("error %d in nfs4dev_upcall()\n", error);
126 if (siz != sizeof(struct idmap_msg)) {
127 IDMAP_DEBUG("bad size of returned message\n");
138 idmap_upcall_id(uint32_t type, ident_t id, struct idmap_entry ** found)
141 struct idmap_entry * e;
144 if (type > IDMAP_MAX_TYPE)
145 panic("bad type"); /* XXX */
147 MALLOC(e, struct idmap_entry *, sizeof(struct idmap_entry), M_IDMAP,
150 e->id_info.id_type = type;
151 e->id_info.id_namelen = 0; /* should already */
152 e->id_info.id_id = id;
154 siz = sizeof(struct idmap_msg);
155 error = nfs4dev_call(NFS4DEV_TYPE_IDMAP, (caddr_t)&e->id_info, siz,
156 (caddr_t)&e->id_info, &siz);
159 IDMAP_DEBUG("error %d in nfs4dev_upcall()\n", error);
164 if (siz != sizeof(struct idmap_msg)) {
165 IDMAP_DEBUG("bad size of returned message\n");
175 idmap_hashf(struct idmap_entry *e, uint32_t * hval_id, uint32_t * hval_name)
177 switch (e->id_info.id_type) {
179 *hval_id = e->id_info.id_id.uid % IDMAP_HASH_SIZE;
182 *hval_id = e->id_info.id_id.gid % IDMAP_HASH_SIZE;
186 panic("hashf: bad type!");
190 if (e->id_info.id_namelen == 0)
191 /* XXX */ panic("hashf: bad name");
193 *hval_name = fnv_32_str(e->id_info.id_name, FNV1_32_INIT) % IDMAP_HASH_SIZE;
198 idmap_add(struct idmap_entry * e)
200 struct idmap_hash * hash;
201 uint32_t hval_id, hval_name;
204 panic("idmap_add null");
206 if (e->id_info.id_namelen == 0)
207 panic("idmap_add name of len 0");
209 switch (e->id_info.id_type) {
211 hash = &idmap_uid_hash;
214 hash = &idmap_gid_hash;
218 panic("idmap add: bad type!");
222 if (idmap_hashf(e, &hval_id, &hval_name) != 0) {
223 IDMAP_DEBUG("idmap_hashf fails!\n");
227 IDMAP_WLOCK(&hash->hash_lock);
229 TAILQ_INSERT_TAIL(&hash->hash_id[hval_id], e, id_entry_id);
230 TAILQ_INSERT_TAIL(&hash->hash_name[hval_name], e, id_entry_name);
232 IDMAP_UNLOCK(&hash->hash_lock);
237 static struct idmap_entry *
238 idmap_id_lookup(uint32_t type, ident_t id)
240 struct idmap_hash * hash;
242 struct idmap_entry * e;
246 hash = &idmap_uid_hash;
247 hval = id.uid % IDMAP_HASH_SIZE;
250 hash = &idmap_gid_hash;
251 hval = id.gid % IDMAP_HASH_SIZE;
255 panic("lookup: bad type!");
260 IDMAP_RLOCK(&hash->hash_lock);
262 TAILQ_FOREACH(e, &hash->hash_id[hval], id_entry_name) {
263 if ((type == IDMAP_TYPE_UID && e->id_info.id_id.uid == id.uid)||
264 (type == IDMAP_TYPE_GID && e->id_info.id_id.gid == id.gid)) {
265 IDMAP_UNLOCK(&hash->hash_lock);
270 IDMAP_UNLOCK(&hash->hash_lock);
274 static struct idmap_entry *
275 idmap_name_lookup(uint32_t type, char * name)
277 struct idmap_hash * hash;
279 struct idmap_entry * e;
284 hash = &idmap_uid_hash;
287 hash = &idmap_gid_hash;
291 panic("lookup: bad type!");
297 if (len == 0 || len > IDMAP_MAXNAMELEN) {
298 IDMAP_DEBUG("bad name length %d\n", len);
302 hval = fnv_32_str(name, FNV1_32_INIT) % IDMAP_HASH_SIZE;
304 IDMAP_RLOCK(&hash->hash_lock);
306 TAILQ_FOREACH(e, &hash->hash_name[hval], id_entry_name) {
307 if ((strlen(e->id_info.id_name) == strlen(name)) && strncmp(e->id_info.id_name, name, strlen(name)) == 0) {
308 IDMAP_UNLOCK(&hash->hash_lock);
313 IDMAP_UNLOCK(&hash->hash_lock);
322 for (i=0; i<IDMAP_HASH_SIZE; i++) {
323 TAILQ_INIT(&idmap_uid_hash.hash_name[i]);
324 TAILQ_INIT(&idmap_uid_hash.hash_id[i]);
326 TAILQ_INIT(&idmap_gid_hash.hash_name[i]);
327 TAILQ_INIT(&idmap_gid_hash.hash_id[i]);
330 lockinit(&idmap_uid_hash.hash_lock, PLOCK, "idmap uid hash table", 0,0);
331 lockinit(&idmap_gid_hash.hash_lock, PLOCK, "idmap gid hash table", 0,0);
335 void idmap_uninit(void)
337 struct idmap_entry * e;
340 lockdestroy(&idmap_uid_hash.hash_lock);
341 lockdestroy(&idmap_gid_hash.hash_lock);
343 for (i=0; i<IDMAP_HASH_SIZE; i++) {
344 while(!TAILQ_EMPTY(&idmap_uid_hash.hash_name[i])) {
345 e = TAILQ_FIRST(&idmap_uid_hash.hash_name[i]);
346 TAILQ_REMOVE(&idmap_uid_hash.hash_name[i], e, id_entry_name);
347 TAILQ_REMOVE(&idmap_uid_hash.hash_id[i], e, id_entry_id);
351 while(!TAILQ_EMPTY(&idmap_gid_hash.hash_name[i])) {
352 e = TAILQ_FIRST(&idmap_gid_hash.hash_name[i]);
353 TAILQ_REMOVE(&idmap_gid_hash.hash_name[i], e, id_entry_name);
354 TAILQ_REMOVE(&idmap_gid_hash.hash_id[i], e, id_entry_id);
362 idmap_uid_to_name(uid_t uid, char ** name, size_t * len)
364 struct idmap_entry * e;
371 if ((e = idmap_id_lookup(IDMAP_TYPE_UID, id)) == NULL) {
372 if ((error = idmap_upcall_id(IDMAP_TYPE_UID, id, &e)) != 0) {
373 IDMAP_DEBUG("error in upcall\n");
378 IDMAP_DEBUG("no error from upcall, but no data returned\n");
382 if (idmap_add(e) != 0) {
383 IDMAP_DEBUG("idmap_add failed\n");
388 *name = e->id_info.id_name;
389 *len = e->id_info.id_namelen;
394 idmap_gid_to_name(gid_t gid, char ** name, size_t * len)
396 struct idmap_entry * e;
403 if ((e = idmap_id_lookup(IDMAP_TYPE_GID, id)) == NULL) {
404 if ((error = idmap_upcall_id(IDMAP_TYPE_GID, (ident_t)id, &e))) {
405 IDMAP_DEBUG("error in upcall\n");
410 IDMAP_DEBUG("no error from upcall, but no data returned\n");
414 if (idmap_add(e) != 0) {
415 IDMAP_DEBUG("idmap_add failed\n");
420 *name = e->id_info.id_name;
421 *len = e->id_info.id_namelen;
426 idmap_name_to_uid(char * name, size_t len, uid_t * id)
428 struct idmap_entry * e;
435 if (len == 0 || len > IDMAP_MAXNAMELEN) {
436 IDMAP_DEBUG("idmap_name_to_uid: bad len\n");
441 MALLOC(namestr, char *, len + 1, M_TEMP, M_WAITOK);
442 bcopy(name, namestr, len);
446 if ((e = idmap_name_lookup(IDMAP_TYPE_UID, namestr)) == NULL) {
447 if ((error = idmap_upcall_name(IDMAP_TYPE_UID, namestr, &e))) {
448 FREE(namestr, M_TEMP);
453 IDMAP_DEBUG("no error from upcall, but no data returned\n");
454 FREE(namestr, M_TEMP);
458 if (idmap_add(e) != 0) {
459 IDMAP_DEBUG("idmap_add failed\n");
464 *id = e->id_info.id_id.uid;
465 FREE(namestr, M_TEMP);
470 idmap_name_to_gid(char * name, size_t len, gid_t * id)
472 struct idmap_entry * e;
480 if (len == 0 || len > IDMAP_MAXNAMELEN) {
481 IDMAP_DEBUG("idmap_name_to_uid: bad len\n");
486 MALLOC(namestr, char *, len + 1, M_TEMP, M_WAITOK);
487 bcopy(name, namestr, len);
491 if ((e = idmap_name_lookup(IDMAP_TYPE_GID, namestr)) == NULL) {
492 if ((error = idmap_upcall_name(IDMAP_TYPE_GID, namestr, &e)) != 0) {
493 IDMAP_DEBUG("error in upcall\n");
494 FREE(namestr, M_TEMP);
499 IDMAP_DEBUG("no error from upcall, but no data returned\n");
500 FREE(namestr, M_TEMP);
504 if (idmap_add(e) != 0) {
505 IDMAP_DEBUG("idmap_add failed\n");
510 *id = e->id_info.id_id.gid;
511 FREE(namestr, M_TEMP);