4 * UID and GID to name mapping for clients.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/nfs_idmap.h>
41 #include <linux/nfs_fs.h>
42 #include <linux/cred.h>
43 #include <linux/sunrpc/sched.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs_sb.h>
46 #include <linux/keyctl.h>
47 #include <linux/key-type.h>
48 #include <linux/rcupdate.h>
49 #include <linux/err.h>
50 #include <keys/user-type.h>
52 /* include files needed by legacy idmapper */
53 #include <linux/module.h>
54 #include <linux/mutex.h>
55 #include <linux/init.h>
56 #include <linux/socket.h>
58 #include <linux/sched.h>
59 #include <linux/sunrpc/clnt.h>
60 #include <linux/workqueue.h>
61 #include <linux/sunrpc/rpc_pipe_fs.h>
62 #include <linux/nfs_fs.h>
66 #define NFS_UINT_MAXLEN 11
67 #define IDMAP_HASH_SZ 128
69 /* Default cache timeout is 10 minutes */
70 unsigned int nfs_idmap_cache_timeout
= 600 * HZ
;
71 const struct cred
*id_resolver_cache
;
75 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
76 * @fattr: fully initialised struct nfs_fattr
77 * @owner_name: owner name string cache
78 * @group_name: group name string cache
80 void nfs_fattr_init_names(struct nfs_fattr
*fattr
,
81 struct nfs4_string
*owner_name
,
82 struct nfs4_string
*group_name
)
84 fattr
->owner_name
= owner_name
;
85 fattr
->group_name
= group_name
;
88 static void nfs_fattr_free_owner_name(struct nfs_fattr
*fattr
)
90 fattr
->valid
&= ~NFS_ATTR_FATTR_OWNER_NAME
;
91 kfree(fattr
->owner_name
->data
);
94 static void nfs_fattr_free_group_name(struct nfs_fattr
*fattr
)
96 fattr
->valid
&= ~NFS_ATTR_FATTR_GROUP_NAME
;
97 kfree(fattr
->group_name
->data
);
100 static bool nfs_fattr_map_owner_name(struct nfs_server
*server
, struct nfs_fattr
*fattr
)
102 struct nfs4_string
*owner
= fattr
->owner_name
;
105 if (!(fattr
->valid
& NFS_ATTR_FATTR_OWNER_NAME
))
107 if (nfs_map_name_to_uid(server
, owner
->data
, owner
->len
, &uid
) == 0) {
109 fattr
->valid
|= NFS_ATTR_FATTR_OWNER
;
114 static bool nfs_fattr_map_group_name(struct nfs_server
*server
, struct nfs_fattr
*fattr
)
116 struct nfs4_string
*group
= fattr
->group_name
;
119 if (!(fattr
->valid
& NFS_ATTR_FATTR_GROUP_NAME
))
121 if (nfs_map_group_to_gid(server
, group
->data
, group
->len
, &gid
) == 0) {
123 fattr
->valid
|= NFS_ATTR_FATTR_GROUP
;
129 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130 * @fattr: a fully initialised nfs_fattr structure
132 void nfs_fattr_free_names(struct nfs_fattr
*fattr
)
134 if (fattr
->valid
& NFS_ATTR_FATTR_OWNER_NAME
)
135 nfs_fattr_free_owner_name(fattr
);
136 if (fattr
->valid
& NFS_ATTR_FATTR_GROUP_NAME
)
137 nfs_fattr_free_group_name(fattr
);
141 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
142 * @server: pointer to the filesystem nfs_server structure
143 * @fattr: a fully initialised nfs_fattr structure
145 * This helper maps the cached NFSv4 owner/group strings in fattr into
146 * their numeric uid/gid equivalents, and then frees the cached strings.
148 void nfs_fattr_map_and_free_names(struct nfs_server
*server
, struct nfs_fattr
*fattr
)
150 if (nfs_fattr_map_owner_name(server
, fattr
))
151 nfs_fattr_free_owner_name(fattr
);
152 if (nfs_fattr_map_group_name(server
, fattr
))
153 nfs_fattr_free_group_name(fattr
);
156 static int nfs_map_string_to_numeric(const char *name
, size_t namelen
, __u32
*res
)
161 if (memchr(name
, '@', namelen
) != NULL
|| namelen
>= sizeof(buf
))
163 memcpy(buf
, name
, namelen
);
165 if (strict_strtoul(buf
, 0, &val
) != 0)
171 static int nfs_map_numeric_to_string(__u32 id
, char *buf
, size_t buflen
)
173 return snprintf(buf
, buflen
, "%u", id
);
176 struct key_type key_type_id_resolver
= {
177 .name
= "id_resolver",
178 .instantiate
= user_instantiate
,
180 .revoke
= user_revoke
,
181 .destroy
= user_destroy
,
182 .describe
= user_describe
,
186 static int nfs_idmap_init_keyring(void)
192 printk(KERN_NOTICE
"NFS: Registering the %s key type\n",
193 key_type_id_resolver
.name
);
195 cred
= prepare_kernel_cred(NULL
);
199 keyring
= key_alloc(&key_type_keyring
, ".id_resolver", 0, 0, cred
,
200 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
201 KEY_USR_VIEW
| KEY_USR_READ
,
202 KEY_ALLOC_NOT_IN_QUOTA
);
203 if (IS_ERR(keyring
)) {
204 ret
= PTR_ERR(keyring
);
205 goto failed_put_cred
;
208 ret
= key_instantiate_and_link(keyring
, NULL
, 0, NULL
, NULL
);
212 ret
= register_key_type(&key_type_id_resolver
);
216 cred
->thread_keyring
= keyring
;
217 cred
->jit_keyring
= KEY_REQKEY_DEFL_THREAD_KEYRING
;
218 id_resolver_cache
= cred
;
228 static void nfs_idmap_quit_keyring(void)
230 key_revoke(id_resolver_cache
->thread_keyring
);
231 unregister_key_type(&key_type_id_resolver
);
232 put_cred(id_resolver_cache
);
236 * Assemble the description to pass to request_key()
237 * This function will allocate a new string and update dest to point
238 * at it. The caller is responsible for freeing dest.
240 * On error 0 is returned. Otherwise, the length of dest is returned.
242 static ssize_t
nfs_idmap_get_desc(const char *name
, size_t namelen
,
243 const char *type
, size_t typelen
, char **desc
)
246 size_t desclen
= typelen
+ namelen
+ 2;
248 *desc
= kmalloc(desclen
, GFP_KERNEL
);
253 memcpy(cp
, type
, typelen
);
257 memcpy(cp
, name
, namelen
);
263 static ssize_t
nfs_idmap_request_key(const char *name
, size_t namelen
,
264 const char *type
, void *data
, size_t data_size
)
266 const struct cred
*saved_cred
;
269 struct user_key_payload
*payload
;
272 ret
= nfs_idmap_get_desc(name
, namelen
, type
, strlen(type
), &desc
);
276 saved_cred
= override_creds(id_resolver_cache
);
277 rkey
= request_key(&key_type_id_resolver
, desc
, "");
278 revert_creds(saved_cred
);
286 rkey
->perm
|= KEY_USR_VIEW
;
288 ret
= key_validate(rkey
);
292 payload
= rcu_dereference(rkey
->payload
.data
);
293 if (IS_ERR_OR_NULL(payload
)) {
294 ret
= PTR_ERR(payload
);
298 ret
= payload
->datalen
;
299 if (ret
> 0 && ret
<= data_size
)
300 memcpy(data
, payload
->data
, ret
);
313 static ssize_t
nfs_idmap_lookup_name(__u32 id
, const char *type
, char *buf
, size_t buflen
)
315 char id_str
[NFS_UINT_MAXLEN
];
319 id_len
= snprintf(id_str
, sizeof(id_str
), "%u", id
);
320 ret
= nfs_idmap_request_key(id_str
, id_len
, type
, buf
, buflen
);
327 static int nfs_idmap_lookup_id(const char *name
, size_t namelen
,
328 const char *type
, __u32
*id
)
330 char id_str
[NFS_UINT_MAXLEN
];
335 data_size
= nfs_idmap_request_key(name
, namelen
, type
, id_str
, NFS_UINT_MAXLEN
);
336 if (data_size
<= 0) {
339 ret
= strict_strtol(id_str
, 10, &id_long
);
340 *id
= (__u32
)id_long
;
345 /* idmap classic begins here */
346 static int param_set_idmap_timeout(const char *val
, struct kernel_param
*kp
)
349 int num
= simple_strtol(val
, &endp
, 0);
351 if (endp
== val
|| *endp
|| num
< 0 || jif
< num
)
353 *((int *)kp
->arg
) = jif
;
357 module_param_call(idmap_cache_timeout
, param_set_idmap_timeout
, param_get_int
,
358 &nfs_idmap_cache_timeout
, 0644);
360 struct idmap_hashent
{
361 unsigned long ih_expires
;
364 char ih_name
[IDMAP_NAMESZ
];
367 struct idmap_hashtable
{
369 struct idmap_hashent h_entries
[IDMAP_HASH_SZ
];
373 struct rpc_pipe
*idmap_pipe
;
374 wait_queue_head_t idmap_wq
;
375 struct idmap_msg idmap_im
;
376 struct mutex idmap_lock
; /* Serializes upcalls */
377 struct mutex idmap_im_lock
; /* Protects the hashtable */
378 struct idmap_hashtable idmap_user_hash
;
379 struct idmap_hashtable idmap_group_hash
;
382 static ssize_t
idmap_pipe_downcall(struct file
*, const char __user
*,
384 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg
*);
386 static unsigned int fnvhash32(const void *, size_t);
388 static const struct rpc_pipe_ops idmap_upcall_ops
= {
389 .upcall
= rpc_pipe_generic_upcall
,
390 .downcall
= idmap_pipe_downcall
,
391 .destroy_msg
= idmap_pipe_destroy_msg
,
394 static void __nfs_idmap_unregister(struct rpc_pipe
*pipe
)
397 rpc_unlink(pipe
->dentry
);
400 static int __nfs_idmap_register(struct dentry
*dir
,
402 struct rpc_pipe
*pipe
)
404 struct dentry
*dentry
;
406 dentry
= rpc_mkpipe_dentry(dir
, "idmap", idmap
, pipe
);
408 return PTR_ERR(dentry
);
409 pipe
->dentry
= dentry
;
413 static void nfs_idmap_unregister(struct nfs_client
*clp
,
414 struct rpc_pipe
*pipe
)
416 struct net
*net
= clp
->net
;
417 struct super_block
*pipefs_sb
;
419 pipefs_sb
= rpc_get_sb_net(net
);
421 __nfs_idmap_unregister(pipe
);
426 static int nfs_idmap_register(struct nfs_client
*clp
,
428 struct rpc_pipe
*pipe
)
430 struct net
*net
= clp
->net
;
431 struct super_block
*pipefs_sb
;
434 pipefs_sb
= rpc_get_sb_net(net
);
436 if (clp
->cl_rpcclient
->cl_dentry
)
437 err
= __nfs_idmap_register(clp
->cl_rpcclient
->cl_dentry
,
445 nfs_idmap_new(struct nfs_client
*clp
)
448 struct rpc_pipe
*pipe
;
451 BUG_ON(clp
->cl_idmap
!= NULL
);
453 idmap
= kzalloc(sizeof(*idmap
), GFP_KERNEL
);
457 pipe
= rpc_mkpipe_data(&idmap_upcall_ops
, 0);
459 error
= PTR_ERR(pipe
);
463 error
= nfs_idmap_register(clp
, idmap
, pipe
);
465 rpc_destroy_pipe_data(pipe
);
469 idmap
->idmap_pipe
= pipe
;
470 mutex_init(&idmap
->idmap_lock
);
471 mutex_init(&idmap
->idmap_im_lock
);
472 init_waitqueue_head(&idmap
->idmap_wq
);
473 idmap
->idmap_user_hash
.h_type
= IDMAP_TYPE_USER
;
474 idmap
->idmap_group_hash
.h_type
= IDMAP_TYPE_GROUP
;
476 clp
->cl_idmap
= idmap
;
481 nfs_idmap_delete(struct nfs_client
*clp
)
483 struct idmap
*idmap
= clp
->cl_idmap
;
487 nfs_idmap_unregister(clp
, idmap
->idmap_pipe
);
488 rpc_destroy_pipe_data(idmap
->idmap_pipe
);
489 clp
->cl_idmap
= NULL
;
493 static int __rpc_pipefs_event(struct nfs_client
*clp
, unsigned long event
,
494 struct super_block
*sb
)
499 case RPC_PIPEFS_MOUNT
:
500 BUG_ON(clp
->cl_rpcclient
->cl_dentry
== NULL
);
501 err
= __nfs_idmap_register(clp
->cl_rpcclient
->cl_dentry
,
503 clp
->cl_idmap
->idmap_pipe
);
505 case RPC_PIPEFS_UMOUNT
:
506 if (clp
->cl_idmap
->idmap_pipe
) {
507 struct dentry
*parent
;
509 parent
= clp
->cl_idmap
->idmap_pipe
->dentry
->d_parent
;
510 __nfs_idmap_unregister(clp
->cl_idmap
->idmap_pipe
);
512 * Note: This is a dirty hack. SUNRPC hook has been
513 * called already but simple_rmdir() call for the
514 * directory returned with error because of idmap pipe
515 * inside. Thus now we have to remove this directory
518 if (rpc_rmdir(parent
))
519 printk(KERN_ERR
"NFS: %s: failed to remove "
520 "clnt dir!\n", __func__
);
524 printk(KERN_ERR
"NFS: %s: unknown event: %ld\n", __func__
,
531 static int rpc_pipefs_event(struct notifier_block
*nb
, unsigned long event
,
534 struct super_block
*sb
= ptr
;
535 struct nfs_client
*clp
;
538 spin_lock(&nfs_client_lock
);
539 list_for_each_entry(clp
, &nfs_client_list
, cl_share_link
) {
540 if (clp
->net
!= sb
->s_fs_info
)
542 if (clp
->rpc_ops
!= &nfs_v4_clientops
)
544 error
= __rpc_pipefs_event(clp
, event
, sb
);
548 spin_unlock(&nfs_client_lock
);
552 #define PIPEFS_NFS_PRIO 1
554 static struct notifier_block nfs_idmap_block
= {
555 .notifier_call
= rpc_pipefs_event
,
556 .priority
= SUNRPC_PIPEFS_NFS_PRIO
,
559 int nfs_idmap_init(void)
562 ret
= nfs_idmap_init_keyring();
565 ret
= rpc_pipefs_notifier_register(&nfs_idmap_block
);
567 nfs_idmap_quit_keyring();
572 void nfs_idmap_quit(void)
574 rpc_pipefs_notifier_unregister(&nfs_idmap_block
);
575 nfs_idmap_quit_keyring();
579 * Helper routines for manipulating the hashtable
581 static inline struct idmap_hashent
*
582 idmap_name_hash(struct idmap_hashtable
* h
, const char *name
, size_t len
)
584 return &h
->h_entries
[fnvhash32(name
, len
) % IDMAP_HASH_SZ
];
587 static struct idmap_hashent
*
588 idmap_lookup_name(struct idmap_hashtable
*h
, const char *name
, size_t len
)
590 struct idmap_hashent
*he
= idmap_name_hash(h
, name
, len
);
592 if (he
->ih_namelen
!= len
|| memcmp(he
->ih_name
, name
, len
) != 0)
594 if (time_after(jiffies
, he
->ih_expires
))
599 static inline struct idmap_hashent
*
600 idmap_id_hash(struct idmap_hashtable
* h
, __u32 id
)
602 return &h
->h_entries
[fnvhash32(&id
, sizeof(id
)) % IDMAP_HASH_SZ
];
605 static struct idmap_hashent
*
606 idmap_lookup_id(struct idmap_hashtable
*h
, __u32 id
)
608 struct idmap_hashent
*he
= idmap_id_hash(h
, id
);
609 if (he
->ih_id
!= id
|| he
->ih_namelen
== 0)
611 if (time_after(jiffies
, he
->ih_expires
))
617 * Routines for allocating new entries in the hashtable.
618 * For now, we just have 1 entry per bucket, so it's all
621 static inline struct idmap_hashent
*
622 idmap_alloc_name(struct idmap_hashtable
*h
, char *name
, size_t len
)
624 return idmap_name_hash(h
, name
, len
);
627 static inline struct idmap_hashent
*
628 idmap_alloc_id(struct idmap_hashtable
*h
, __u32 id
)
630 return idmap_id_hash(h
, id
);
634 idmap_update_entry(struct idmap_hashent
*he
, const char *name
,
635 size_t namelen
, __u32 id
)
638 memcpy(he
->ih_name
, name
, namelen
);
639 he
->ih_name
[namelen
] = '\0';
640 he
->ih_namelen
= namelen
;
641 he
->ih_expires
= jiffies
+ nfs_idmap_cache_timeout
;
648 nfs_idmap_id(struct idmap
*idmap
, struct idmap_hashtable
*h
,
649 const char *name
, size_t namelen
, __u32
*id
)
651 struct rpc_pipe_msg msg
;
652 struct idmap_msg
*im
;
653 struct idmap_hashent
*he
;
654 DECLARE_WAITQUEUE(wq
, current
);
657 im
= &idmap
->idmap_im
;
660 * String sanity checks
661 * Note that the userland daemon expects NUL terminated strings
666 if (name
[namelen
-1] != '\0')
670 if (namelen
>= IDMAP_NAMESZ
)
673 mutex_lock(&idmap
->idmap_lock
);
674 mutex_lock(&idmap
->idmap_im_lock
);
676 he
= idmap_lookup_name(h
, name
, namelen
);
683 memset(im
, 0, sizeof(*im
));
684 memcpy(im
->im_name
, name
, namelen
);
686 im
->im_type
= h
->h_type
;
687 im
->im_conv
= IDMAP_CONV_NAMETOID
;
689 memset(&msg
, 0, sizeof(msg
));
691 msg
.len
= sizeof(*im
);
693 add_wait_queue(&idmap
->idmap_wq
, &wq
);
694 if (rpc_queue_upcall(idmap
->idmap_pipe
, &msg
) < 0) {
695 remove_wait_queue(&idmap
->idmap_wq
, &wq
);
699 set_current_state(TASK_UNINTERRUPTIBLE
);
700 mutex_unlock(&idmap
->idmap_im_lock
);
702 __set_current_state(TASK_RUNNING
);
703 remove_wait_queue(&idmap
->idmap_wq
, &wq
);
704 mutex_lock(&idmap
->idmap_im_lock
);
706 if (im
->im_status
& IDMAP_STATUS_SUCCESS
) {
712 memset(im
, 0, sizeof(*im
));
713 mutex_unlock(&idmap
->idmap_im_lock
);
714 mutex_unlock(&idmap
->idmap_lock
);
722 nfs_idmap_name(struct idmap
*idmap
, struct idmap_hashtable
*h
,
723 __u32 id
, char *name
)
725 struct rpc_pipe_msg msg
;
726 struct idmap_msg
*im
;
727 struct idmap_hashent
*he
;
728 DECLARE_WAITQUEUE(wq
, current
);
732 im
= &idmap
->idmap_im
;
734 mutex_lock(&idmap
->idmap_lock
);
735 mutex_lock(&idmap
->idmap_im_lock
);
737 he
= idmap_lookup_id(h
, id
);
739 memcpy(name
, he
->ih_name
, he
->ih_namelen
);
740 ret
= he
->ih_namelen
;
744 memset(im
, 0, sizeof(*im
));
745 im
->im_type
= h
->h_type
;
746 im
->im_conv
= IDMAP_CONV_IDTONAME
;
749 memset(&msg
, 0, sizeof(msg
));
751 msg
.len
= sizeof(*im
);
753 add_wait_queue(&idmap
->idmap_wq
, &wq
);
755 if (rpc_queue_upcall(idmap
->idmap_pipe
, &msg
) < 0) {
756 remove_wait_queue(&idmap
->idmap_wq
, &wq
);
760 set_current_state(TASK_UNINTERRUPTIBLE
);
761 mutex_unlock(&idmap
->idmap_im_lock
);
763 __set_current_state(TASK_RUNNING
);
764 remove_wait_queue(&idmap
->idmap_wq
, &wq
);
765 mutex_lock(&idmap
->idmap_im_lock
);
767 if (im
->im_status
& IDMAP_STATUS_SUCCESS
) {
768 if ((len
= strnlen(im
->im_name
, IDMAP_NAMESZ
)) == 0)
770 memcpy(name
, im
->im_name
, len
);
775 memset(im
, 0, sizeof(*im
));
776 mutex_unlock(&idmap
->idmap_im_lock
);
777 mutex_unlock(&idmap
->idmap_lock
);
782 idmap_pipe_downcall(struct file
*filp
, const char __user
*src
, size_t mlen
)
784 struct rpc_inode
*rpci
= RPC_I(filp
->f_path
.dentry
->d_inode
);
785 struct idmap
*idmap
= (struct idmap
*)rpci
->private;
786 struct idmap_msg im_in
, *im
= &idmap
->idmap_im
;
787 struct idmap_hashtable
*h
;
788 struct idmap_hashent
*he
= NULL
;
792 if (mlen
!= sizeof(im_in
))
795 if (copy_from_user(&im_in
, src
, mlen
) != 0)
798 mutex_lock(&idmap
->idmap_im_lock
);
801 im
->im_status
= im_in
.im_status
;
802 /* If we got an error, terminate now, and wake up pending upcalls */
803 if (!(im_in
.im_status
& IDMAP_STATUS_SUCCESS
)) {
804 wake_up(&idmap
->idmap_wq
);
808 /* Sanity checking of strings */
810 namelen_in
= strnlen(im_in
.im_name
, IDMAP_NAMESZ
);
811 if (namelen_in
== 0 || namelen_in
== IDMAP_NAMESZ
)
814 switch (im_in
.im_type
) {
815 case IDMAP_TYPE_USER
:
816 h
= &idmap
->idmap_user_hash
;
818 case IDMAP_TYPE_GROUP
:
819 h
= &idmap
->idmap_group_hash
;
825 switch (im_in
.im_conv
) {
826 case IDMAP_CONV_IDTONAME
:
827 /* Did we match the current upcall? */
828 if (im
->im_conv
== IDMAP_CONV_IDTONAME
829 && im
->im_type
== im_in
.im_type
830 && im
->im_id
== im_in
.im_id
) {
831 /* Yes: copy string, including the terminating '\0' */
832 memcpy(im
->im_name
, im_in
.im_name
, namelen_in
);
833 im
->im_name
[namelen_in
] = '\0';
834 wake_up(&idmap
->idmap_wq
);
836 he
= idmap_alloc_id(h
, im_in
.im_id
);
838 case IDMAP_CONV_NAMETOID
:
839 /* Did we match the current upcall? */
840 if (im
->im_conv
== IDMAP_CONV_NAMETOID
841 && im
->im_type
== im_in
.im_type
842 && strnlen(im
->im_name
, IDMAP_NAMESZ
) == namelen_in
843 && memcmp(im
->im_name
, im_in
.im_name
, namelen_in
) == 0) {
844 im
->im_id
= im_in
.im_id
;
845 wake_up(&idmap
->idmap_wq
);
847 he
= idmap_alloc_name(h
, im_in
.im_name
, namelen_in
);
853 /* If the entry is valid, also copy it to the cache */
855 idmap_update_entry(he
, im_in
.im_name
, namelen_in
, im_in
.im_id
);
858 mutex_unlock(&idmap
->idmap_im_lock
);
863 idmap_pipe_destroy_msg(struct rpc_pipe_msg
*msg
)
865 struct idmap_msg
*im
= msg
->data
;
866 struct idmap
*idmap
= container_of(im
, struct idmap
, idmap_im
);
870 mutex_lock(&idmap
->idmap_im_lock
);
871 im
->im_status
= IDMAP_STATUS_LOOKUPFAIL
;
872 wake_up(&idmap
->idmap_wq
);
873 mutex_unlock(&idmap
->idmap_im_lock
);
877 * Fowler/Noll/Vo hash
878 * http://www.isthe.com/chongo/tech/comp/fnv/
881 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
882 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
884 static unsigned int fnvhash32(const void *buf
, size_t buflen
)
886 const unsigned char *p
, *end
= (const unsigned char *)buf
+ buflen
;
887 unsigned int hash
= FNV_1_32
;
889 for (p
= buf
; p
< end
; p
++) {
891 hash
^= (unsigned int)*p
;
897 int nfs_map_name_to_uid(const struct nfs_server
*server
, const char *name
, size_t namelen
, __u32
*uid
)
899 struct idmap
*idmap
= server
->nfs_client
->cl_idmap
;
902 if (nfs_map_string_to_numeric(name
, namelen
, uid
))
904 ret
= nfs_idmap_lookup_id(name
, namelen
, "uid", uid
);
906 ret
= nfs_idmap_id(idmap
, &idmap
->idmap_user_hash
, name
, namelen
, uid
);
910 int nfs_map_group_to_gid(const struct nfs_server
*server
, const char *name
, size_t namelen
, __u32
*gid
)
912 struct idmap
*idmap
= server
->nfs_client
->cl_idmap
;
915 if (nfs_map_string_to_numeric(name
, namelen
, gid
))
917 ret
= nfs_idmap_lookup_id(name
, namelen
, "gid", gid
);
919 ret
= nfs_idmap_id(idmap
, &idmap
->idmap_group_hash
, name
, namelen
, gid
);
923 int nfs_map_uid_to_name(const struct nfs_server
*server
, __u32 uid
, char *buf
, size_t buflen
)
925 struct idmap
*idmap
= server
->nfs_client
->cl_idmap
;
928 if (!(server
->caps
& NFS_CAP_UIDGID_NOMAP
)) {
929 ret
= nfs_idmap_lookup_name(uid
, "user", buf
, buflen
);
931 ret
= nfs_idmap_name(idmap
, &idmap
->idmap_user_hash
, uid
, buf
);
934 ret
= nfs_map_numeric_to_string(uid
, buf
, buflen
);
937 int nfs_map_gid_to_group(const struct nfs_server
*server
, __u32 gid
, char *buf
, size_t buflen
)
939 struct idmap
*idmap
= server
->nfs_client
->cl_idmap
;
942 if (!(server
->caps
& NFS_CAP_UIDGID_NOMAP
)) {
943 ret
= nfs_idmap_lookup_name(gid
, "group", buf
, buflen
);
945 ret
= nfs_idmap_name(idmap
, &idmap
->idmap_group_hash
, gid
, buf
);
948 ret
= nfs_map_numeric_to_string(gid
, buf
, buflen
);