idmap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * fs/nfs/idmap.c
  3. *
  4. * UID and GID to name mapping for clients.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Marius Aamodt Eriksen <marius@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  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.
  23. *
  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.
  35. */
  36. #ifdef CONFIG_NFS_USE_NEW_IDMAPPER
  37. #include <linux/slab.h>
  38. #include <linux/cred.h>
  39. #include <linux/nfs_idmap.h>
  40. #include <linux/keyctl.h>
  41. #include <linux/key-type.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/kernel.h>
  44. #include <linux/err.h>
  45. #include <keys/user-type.h>
  46. #define NFS_UINT_MAXLEN 11
  47. const struct cred *id_resolver_cache;
  48. struct key_type key_type_id_resolver = {
  49. .name = "id_resolver",
  50. .instantiate = user_instantiate,
  51. .match = user_match,
  52. .revoke = user_revoke,
  53. .destroy = user_destroy,
  54. .describe = user_describe,
  55. .read = user_read,
  56. };
  57. int nfs_idmap_init(void)
  58. {
  59. struct cred *cred;
  60. struct key *keyring;
  61. int ret = 0;
  62. printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
  63. cred = prepare_kernel_cred(NULL);
  64. if (!cred)
  65. return -ENOMEM;
  66. keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
  67. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  68. KEY_USR_VIEW | KEY_USR_READ,
  69. KEY_ALLOC_NOT_IN_QUOTA);
  70. if (IS_ERR(keyring)) {
  71. ret = PTR_ERR(keyring);
  72. goto failed_put_cred;
  73. }
  74. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  75. if (ret < 0)
  76. goto failed_put_key;
  77. ret = register_key_type(&key_type_id_resolver);
  78. if (ret < 0)
  79. goto failed_put_key;
  80. cred->thread_keyring = keyring;
  81. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  82. id_resolver_cache = cred;
  83. return 0;
  84. failed_put_key:
  85. key_put(keyring);
  86. failed_put_cred:
  87. put_cred(cred);
  88. return ret;
  89. }
  90. void nfs_idmap_quit(void)
  91. {
  92. key_revoke(id_resolver_cache->thread_keyring);
  93. unregister_key_type(&key_type_id_resolver);
  94. put_cred(id_resolver_cache);
  95. }
  96. /*
  97. * Assemble the description to pass to request_key()
  98. * This function will allocate a new string and update dest to point
  99. * at it. The caller is responsible for freeing dest.
  100. *
  101. * On error 0 is returned. Otherwise, the length of dest is returned.
  102. */
  103. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  104. const char *type, size_t typelen, char **desc)
  105. {
  106. char *cp;
  107. size_t desclen = typelen + namelen + 2;
  108. *desc = kmalloc(desclen, GFP_KERNEL);
  109. if (!*desc)
  110. return -ENOMEM;
  111. cp = *desc;
  112. memcpy(cp, type, typelen);
  113. cp += typelen;
  114. *cp++ = ':';
  115. memcpy(cp, name, namelen);
  116. cp += namelen;
  117. *cp = '\0';
  118. return desclen;
  119. }
  120. static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
  121. const char *type, void *data, size_t data_size)
  122. {
  123. const struct cred *saved_cred;
  124. struct key *rkey;
  125. char *desc;
  126. struct user_key_payload *payload;
  127. ssize_t ret;
  128. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  129. if (ret <= 0)
  130. goto out;
  131. saved_cred = override_creds(id_resolver_cache);
  132. rkey = request_key(&key_type_id_resolver, desc, "");
  133. revert_creds(saved_cred);
  134. kfree(desc);
  135. if (IS_ERR(rkey)) {
  136. ret = PTR_ERR(rkey);
  137. goto out;
  138. }
  139. rcu_read_lock();
  140. rkey->perm |= KEY_USR_VIEW;
  141. ret = key_validate(rkey);
  142. if (ret < 0)
  143. goto out_up;
  144. payload = rcu_dereference(rkey->payload.data);
  145. if (IS_ERR_OR_NULL(payload)) {
  146. ret = PTR_ERR(payload);
  147. goto out_up;
  148. }
  149. ret = payload->datalen;
  150. if (ret > 0 && ret <= data_size)
  151. memcpy(data, payload->data, ret);
  152. else
  153. ret = -EINVAL;
  154. out_up:
  155. rcu_read_unlock();
  156. key_put(rkey);
  157. out:
  158. return ret;
  159. }
  160. /* ID -> Name */
  161. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
  162. {
  163. char id_str[NFS_UINT_MAXLEN];
  164. int id_len;
  165. ssize_t ret;
  166. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  167. ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
  168. if (ret < 0)
  169. return -EINVAL;
  170. return ret;
  171. }
  172. /* Name -> ID */
  173. static int nfs_idmap_lookup_id(const char *name, size_t namelen,
  174. const char *type, __u32 *id)
  175. {
  176. char id_str[NFS_UINT_MAXLEN];
  177. long id_long;
  178. ssize_t data_size;
  179. int ret = 0;
  180. data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
  181. if (data_size <= 0) {
  182. ret = -EINVAL;
  183. } else {
  184. ret = strict_strtol(id_str, 10, &id_long);
  185. *id = (__u32)id_long;
  186. }
  187. return ret;
  188. }
  189. int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
  190. {
  191. return nfs_idmap_lookup_id(name, namelen, "uid", uid);
  192. }
  193. int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *gid)
  194. {
  195. return nfs_idmap_lookup_id(name, namelen, "gid", gid);
  196. }
  197. int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
  198. {
  199. return nfs_idmap_lookup_name(uid, "user", buf, buflen);
  200. }
  201. int nfs_map_gid_to_group(struct nfs_client *clp, __u32 gid, char *buf, size_t buflen)
  202. {
  203. return nfs_idmap_lookup_name(gid, "group", buf, buflen);
  204. }
  205. #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
  206. #include <linux/module.h>
  207. #include <linux/mutex.h>
  208. #include <linux/init.h>
  209. #include <linux/types.h>
  210. #include <linux/slab.h>
  211. #include <linux/socket.h>
  212. #include <linux/in.h>
  213. #include <linux/sched.h>
  214. #include <linux/sunrpc/clnt.h>
  215. #include <linux/workqueue.h>
  216. #include <linux/sunrpc/rpc_pipe_fs.h>
  217. #include <linux/nfs_fs.h>
  218. #include <linux/nfs_idmap.h>
  219. #include "nfs4_fs.h"
  220. #define IDMAP_HASH_SZ 128
  221. /* Default cache timeout is 10 minutes */
  222. unsigned int nfs_idmap_cache_timeout = 600 * HZ;
  223. static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
  224. {
  225. char *endp;
  226. int num = simple_strtol(val, &endp, 0);
  227. int jif = num * HZ;
  228. if (endp == val || *endp || num < 0 || jif < num)
  229. return -EINVAL;
  230. *((int *)kp->arg) = jif;
  231. return 0;
  232. }
  233. module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
  234. &nfs_idmap_cache_timeout, 0644);
  235. struct idmap_hashent {
  236. unsigned long ih_expires;
  237. __u32 ih_id;
  238. size_t ih_namelen;
  239. char ih_name[IDMAP_NAMESZ];
  240. };
  241. struct idmap_hashtable {
  242. __u8 h_type;
  243. struct idmap_hashent h_entries[IDMAP_HASH_SZ];
  244. };
  245. struct idmap {
  246. struct dentry *idmap_dentry;
  247. wait_queue_head_t idmap_wq;
  248. struct idmap_msg idmap_im;
  249. struct mutex idmap_lock; /* Serializes upcalls */
  250. struct mutex idmap_im_lock; /* Protects the hashtable */
  251. struct idmap_hashtable idmap_user_hash;
  252. struct idmap_hashtable idmap_group_hash;
  253. };
  254. static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
  255. char __user *, size_t);
  256. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  257. size_t);
  258. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  259. static unsigned int fnvhash32(const void *, size_t);
  260. static const struct rpc_pipe_ops idmap_upcall_ops = {
  261. .upcall = idmap_pipe_upcall,
  262. .downcall = idmap_pipe_downcall,
  263. .destroy_msg = idmap_pipe_destroy_msg,
  264. };
  265. int
  266. nfs_idmap_new(struct nfs_client *clp)
  267. {
  268. struct idmap *idmap;
  269. int error;
  270. BUG_ON(clp->cl_idmap != NULL);
  271. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  272. if (idmap == NULL)
  273. return -ENOMEM;
  274. idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
  275. "idmap", idmap, &idmap_upcall_ops, 0);
  276. if (IS_ERR(idmap->idmap_dentry)) {
  277. error = PTR_ERR(idmap->idmap_dentry);
  278. kfree(idmap);
  279. return error;
  280. }
  281. mutex_init(&idmap->idmap_lock);
  282. mutex_init(&idmap->idmap_im_lock);
  283. init_waitqueue_head(&idmap->idmap_wq);
  284. idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
  285. idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
  286. clp->cl_idmap = idmap;
  287. return 0;
  288. }
  289. void
  290. nfs_idmap_delete(struct nfs_client *clp)
  291. {
  292. struct idmap *idmap = clp->cl_idmap;
  293. if (!idmap)
  294. return;
  295. rpc_unlink(idmap->idmap_dentry);
  296. clp->cl_idmap = NULL;
  297. kfree(idmap);
  298. }
  299. /*
  300. * Helper routines for manipulating the hashtable
  301. */
  302. static inline struct idmap_hashent *
  303. idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
  304. {
  305. return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
  306. }
  307. static struct idmap_hashent *
  308. idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
  309. {
  310. struct idmap_hashent *he = idmap_name_hash(h, name, len);
  311. if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
  312. return NULL;
  313. if (time_after(jiffies, he->ih_expires))
  314. return NULL;
  315. return he;
  316. }
  317. static inline struct idmap_hashent *
  318. idmap_id_hash(struct idmap_hashtable* h, __u32 id)
  319. {
  320. return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
  321. }
  322. static struct idmap_hashent *
  323. idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
  324. {
  325. struct idmap_hashent *he = idmap_id_hash(h, id);
  326. if (he->ih_id != id || he->ih_namelen == 0)
  327. return NULL;
  328. if (time_after(jiffies, he->ih_expires))
  329. return NULL;
  330. return he;
  331. }
  332. /*
  333. * Routines for allocating new entries in the hashtable.
  334. * For now, we just have 1 entry per bucket, so it's all
  335. * pretty trivial.
  336. */
  337. static inline struct idmap_hashent *
  338. idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
  339. {
  340. return idmap_name_hash(h, name, len);
  341. }
  342. static inline struct idmap_hashent *
  343. idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
  344. {
  345. return idmap_id_hash(h, id);
  346. }
  347. static void
  348. idmap_update_entry(struct idmap_hashent *he, const char *name,
  349. size_t namelen, __u32 id)
  350. {
  351. he->ih_id = id;
  352. memcpy(he->ih_name, name, namelen);
  353. he->ih_name[namelen] = '\0';
  354. he->ih_namelen = namelen;
  355. he->ih_expires = jiffies + nfs_idmap_cache_timeout;
  356. }
  357. /*
  358. * Name -> ID
  359. */
  360. static int
  361. nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
  362. const char *name, size_t namelen, __u32 *id)
  363. {
  364. struct rpc_pipe_msg msg;
  365. struct idmap_msg *im;
  366. struct idmap_hashent *he;
  367. DECLARE_WAITQUEUE(wq, current);
  368. int ret = -EIO;
  369. im = &idmap->idmap_im;
  370. /*
  371. * String sanity checks
  372. * Note that the userland daemon expects NUL terminated strings
  373. */
  374. for (;;) {
  375. if (namelen == 0)
  376. return -EINVAL;
  377. if (name[namelen-1] != '\0')
  378. break;
  379. namelen--;
  380. }
  381. if (namelen >= IDMAP_NAMESZ)
  382. return -EINVAL;
  383. mutex_lock(&idmap->idmap_lock);
  384. mutex_lock(&idmap->idmap_im_lock);
  385. he = idmap_lookup_name(h, name, namelen);
  386. if (he != NULL) {
  387. *id = he->ih_id;
  388. ret = 0;
  389. goto out;
  390. }
  391. memset(im, 0, sizeof(*im));
  392. memcpy(im->im_name, name, namelen);
  393. im->im_type = h->h_type;
  394. im->im_conv = IDMAP_CONV_NAMETOID;
  395. memset(&msg, 0, sizeof(msg));
  396. msg.data = im;
  397. msg.len = sizeof(*im);
  398. add_wait_queue(&idmap->idmap_wq, &wq);
  399. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  400. remove_wait_queue(&idmap->idmap_wq, &wq);
  401. goto out;
  402. }
  403. set_current_state(TASK_UNINTERRUPTIBLE);
  404. mutex_unlock(&idmap->idmap_im_lock);
  405. schedule();
  406. __set_current_state(TASK_RUNNING);
  407. remove_wait_queue(&idmap->idmap_wq, &wq);
  408. mutex_lock(&idmap->idmap_im_lock);
  409. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  410. *id = im->im_id;
  411. ret = 0;
  412. }
  413. out:
  414. memset(im, 0, sizeof(*im));
  415. mutex_unlock(&idmap->idmap_im_lock);
  416. mutex_unlock(&idmap->idmap_lock);
  417. return ret;
  418. }
  419. /*
  420. * ID -> Name
  421. */
  422. static int
  423. nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
  424. __u32 id, char *name)
  425. {
  426. struct rpc_pipe_msg msg;
  427. struct idmap_msg *im;
  428. struct idmap_hashent *he;
  429. DECLARE_WAITQUEUE(wq, current);
  430. int ret = -EIO;
  431. unsigned int len;
  432. im = &idmap->idmap_im;
  433. mutex_lock(&idmap->idmap_lock);
  434. mutex_lock(&idmap->idmap_im_lock);
  435. he = idmap_lookup_id(h, id);
  436. if (he) {
  437. memcpy(name, he->ih_name, he->ih_namelen);
  438. ret = he->ih_namelen;
  439. goto out;
  440. }
  441. memset(im, 0, sizeof(*im));
  442. im->im_type = h->h_type;
  443. im->im_conv = IDMAP_CONV_IDTONAME;
  444. im->im_id = id;
  445. memset(&msg, 0, sizeof(msg));
  446. msg.data = im;
  447. msg.len = sizeof(*im);
  448. add_wait_queue(&idmap->idmap_wq, &wq);
  449. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  450. remove_wait_queue(&idmap->idmap_wq, &wq);
  451. goto out;
  452. }
  453. set_current_state(TASK_UNINTERRUPTIBLE);
  454. mutex_unlock(&idmap->idmap_im_lock);
  455. schedule();
  456. __set_current_state(TASK_RUNNING);
  457. remove_wait_queue(&idmap->idmap_wq, &wq);
  458. mutex_lock(&idmap->idmap_im_lock);
  459. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  460. if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
  461. goto out;
  462. memcpy(name, im->im_name, len);
  463. ret = len;
  464. }
  465. out:
  466. memset(im, 0, sizeof(*im));
  467. mutex_unlock(&idmap->idmap_im_lock);
  468. mutex_unlock(&idmap->idmap_lock);
  469. return ret;
  470. }
  471. /* RPC pipefs upcall/downcall routines */
  472. static ssize_t
  473. idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  474. char __user *dst, size_t buflen)
  475. {
  476. char *data = (char *)msg->data + msg->copied;
  477. size_t mlen = min(msg->len, buflen);
  478. unsigned long left;
  479. left = copy_to_user(dst, data, mlen);
  480. if (left == mlen) {
  481. msg->errno = -EFAULT;
  482. return -EFAULT;
  483. }
  484. mlen -= left;
  485. msg->copied += mlen;
  486. msg->errno = 0;
  487. return mlen;
  488. }
  489. static ssize_t
  490. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  491. {
  492. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  493. struct idmap *idmap = (struct idmap *)rpci->private;
  494. struct idmap_msg im_in, *im = &idmap->idmap_im;
  495. struct idmap_hashtable *h;
  496. struct idmap_hashent *he = NULL;
  497. size_t namelen_in;
  498. int ret;
  499. if (mlen != sizeof(im_in))
  500. return -ENOSPC;
  501. if (copy_from_user(&im_in, src, mlen) != 0)
  502. return -EFAULT;
  503. mutex_lock(&idmap->idmap_im_lock);
  504. ret = mlen;
  505. im->im_status = im_in.im_status;
  506. /* If we got an error, terminate now, and wake up pending upcalls */
  507. if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
  508. wake_up(&idmap->idmap_wq);
  509. goto out;
  510. }
  511. /* Sanity checking of strings */
  512. ret = -EINVAL;
  513. namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
  514. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
  515. goto out;
  516. switch (im_in.im_type) {
  517. case IDMAP_TYPE_USER:
  518. h = &idmap->idmap_user_hash;
  519. break;
  520. case IDMAP_TYPE_GROUP:
  521. h = &idmap->idmap_group_hash;
  522. break;
  523. default:
  524. goto out;
  525. }
  526. switch (im_in.im_conv) {
  527. case IDMAP_CONV_IDTONAME:
  528. /* Did we match the current upcall? */
  529. if (im->im_conv == IDMAP_CONV_IDTONAME
  530. && im->im_type == im_in.im_type
  531. && im->im_id == im_in.im_id) {
  532. /* Yes: copy string, including the terminating '\0' */
  533. memcpy(im->im_name, im_in.im_name, namelen_in);
  534. im->im_name[namelen_in] = '\0';
  535. wake_up(&idmap->idmap_wq);
  536. }
  537. he = idmap_alloc_id(h, im_in.im_id);
  538. break;
  539. case IDMAP_CONV_NAMETOID:
  540. /* Did we match the current upcall? */
  541. if (im->im_conv == IDMAP_CONV_NAMETOID
  542. && im->im_type == im_in.im_type
  543. && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
  544. && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
  545. im->im_id = im_in.im_id;
  546. wake_up(&idmap->idmap_wq);
  547. }
  548. he = idmap_alloc_name(h, im_in.im_name, namelen_in);
  549. break;
  550. default:
  551. goto out;
  552. }
  553. /* If the entry is valid, also copy it to the cache */
  554. if (he != NULL)
  555. idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
  556. ret = mlen;
  557. out:
  558. mutex_unlock(&idmap->idmap_im_lock);
  559. return ret;
  560. }
  561. static void
  562. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  563. {
  564. struct idmap_msg *im = msg->data;
  565. struct idmap *idmap = container_of(im, struct idmap, idmap_im);
  566. if (msg->errno >= 0)
  567. return;
  568. mutex_lock(&idmap->idmap_im_lock);
  569. im->im_status = IDMAP_STATUS_LOOKUPFAIL;
  570. wake_up(&idmap->idmap_wq);
  571. mutex_unlock(&idmap->idmap_im_lock);
  572. }
  573. /*
  574. * Fowler/Noll/Vo hash
  575. * http://www.isthe.com/chongo/tech/comp/fnv/
  576. */
  577. #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
  578. #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
  579. static unsigned int fnvhash32(const void *buf, size_t buflen)
  580. {
  581. const unsigned char *p, *end = (const unsigned char *)buf + buflen;
  582. unsigned int hash = FNV_1_32;
  583. for (p = buf; p < end; p++) {
  584. hash *= FNV_P_32;
  585. hash ^= (unsigned int)*p;
  586. }
  587. return hash;
  588. }
  589. int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
  590. {
  591. struct idmap *idmap = clp->cl_idmap;
  592. return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
  593. }
  594. int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
  595. {
  596. struct idmap *idmap = clp->cl_idmap;
  597. return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
  598. }
  599. int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
  600. {
  601. struct idmap *idmap = clp->cl_idmap;
  602. return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
  603. }
  604. int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
  605. {
  606. struct idmap *idmap = clp->cl_idmap;
  607. return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
  608. }
  609. #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */