idmap.c 19 KB

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