idmap.c 19 KB

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