idmap.c 18 KB

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