idmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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/sunrpc/sched.h>
  60. #include <linux/nfs4.h>
  61. #include <linux/nfs_fs_sb.h>
  62. #include <linux/nfs_idmap.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/slab.h>
  246. #include <linux/socket.h>
  247. #include <linux/in.h>
  248. #include <linux/sched.h>
  249. #include <linux/sunrpc/clnt.h>
  250. #include <linux/workqueue.h>
  251. #include <linux/sunrpc/rpc_pipe_fs.h>
  252. #include <linux/nfs_fs.h>
  253. #include <linux/nfs_idmap.h>
  254. #include "nfs4_fs.h"
  255. #define IDMAP_HASH_SZ 128
  256. /* Default cache timeout is 10 minutes */
  257. unsigned int nfs_idmap_cache_timeout = 600 * HZ;
  258. static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
  259. {
  260. char *endp;
  261. int num = simple_strtol(val, &endp, 0);
  262. int jif = num * HZ;
  263. if (endp == val || *endp || num < 0 || jif < num)
  264. return -EINVAL;
  265. *((int *)kp->arg) = jif;
  266. return 0;
  267. }
  268. module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
  269. &nfs_idmap_cache_timeout, 0644);
  270. struct idmap_hashent {
  271. unsigned long ih_expires;
  272. __u32 ih_id;
  273. size_t ih_namelen;
  274. char ih_name[IDMAP_NAMESZ];
  275. };
  276. struct idmap_hashtable {
  277. __u8 h_type;
  278. struct idmap_hashent h_entries[IDMAP_HASH_SZ];
  279. };
  280. struct idmap {
  281. struct dentry *idmap_dentry;
  282. wait_queue_head_t idmap_wq;
  283. struct idmap_msg idmap_im;
  284. struct mutex idmap_lock; /* Serializes upcalls */
  285. struct mutex idmap_im_lock; /* Protects the hashtable */
  286. struct idmap_hashtable idmap_user_hash;
  287. struct idmap_hashtable idmap_group_hash;
  288. };
  289. static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
  290. char __user *, size_t);
  291. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  292. size_t);
  293. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  294. static unsigned int fnvhash32(const void *, size_t);
  295. static const struct rpc_pipe_ops idmap_upcall_ops = {
  296. .upcall = idmap_pipe_upcall,
  297. .downcall = idmap_pipe_downcall,
  298. .destroy_msg = idmap_pipe_destroy_msg,
  299. };
  300. int
  301. nfs_idmap_new(struct nfs_client *clp)
  302. {
  303. struct idmap *idmap;
  304. int error;
  305. BUG_ON(clp->cl_idmap != NULL);
  306. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  307. if (idmap == NULL)
  308. return -ENOMEM;
  309. idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
  310. "idmap", idmap, &idmap_upcall_ops, 0);
  311. if (IS_ERR(idmap->idmap_dentry)) {
  312. error = PTR_ERR(idmap->idmap_dentry);
  313. kfree(idmap);
  314. return error;
  315. }
  316. mutex_init(&idmap->idmap_lock);
  317. mutex_init(&idmap->idmap_im_lock);
  318. init_waitqueue_head(&idmap->idmap_wq);
  319. idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
  320. idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
  321. clp->cl_idmap = idmap;
  322. return 0;
  323. }
  324. void
  325. nfs_idmap_delete(struct nfs_client *clp)
  326. {
  327. struct idmap *idmap = clp->cl_idmap;
  328. if (!idmap)
  329. return;
  330. rpc_unlink(idmap->idmap_dentry);
  331. clp->cl_idmap = NULL;
  332. kfree(idmap);
  333. }
  334. /*
  335. * Helper routines for manipulating the hashtable
  336. */
  337. static inline struct idmap_hashent *
  338. idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
  339. {
  340. return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
  341. }
  342. static struct idmap_hashent *
  343. idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
  344. {
  345. struct idmap_hashent *he = idmap_name_hash(h, name, len);
  346. if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
  347. return NULL;
  348. if (time_after(jiffies, he->ih_expires))
  349. return NULL;
  350. return he;
  351. }
  352. static inline struct idmap_hashent *
  353. idmap_id_hash(struct idmap_hashtable* h, __u32 id)
  354. {
  355. return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
  356. }
  357. static struct idmap_hashent *
  358. idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
  359. {
  360. struct idmap_hashent *he = idmap_id_hash(h, id);
  361. if (he->ih_id != id || he->ih_namelen == 0)
  362. return NULL;
  363. if (time_after(jiffies, he->ih_expires))
  364. return NULL;
  365. return he;
  366. }
  367. /*
  368. * Routines for allocating new entries in the hashtable.
  369. * For now, we just have 1 entry per bucket, so it's all
  370. * pretty trivial.
  371. */
  372. static inline struct idmap_hashent *
  373. idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
  374. {
  375. return idmap_name_hash(h, name, len);
  376. }
  377. static inline struct idmap_hashent *
  378. idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
  379. {
  380. return idmap_id_hash(h, id);
  381. }
  382. static void
  383. idmap_update_entry(struct idmap_hashent *he, const char *name,
  384. size_t namelen, __u32 id)
  385. {
  386. he->ih_id = id;
  387. memcpy(he->ih_name, name, namelen);
  388. he->ih_name[namelen] = '\0';
  389. he->ih_namelen = namelen;
  390. he->ih_expires = jiffies + nfs_idmap_cache_timeout;
  391. }
  392. /*
  393. * Name -> ID
  394. */
  395. static int
  396. nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
  397. const char *name, size_t namelen, __u32 *id)
  398. {
  399. struct rpc_pipe_msg msg;
  400. struct idmap_msg *im;
  401. struct idmap_hashent *he;
  402. DECLARE_WAITQUEUE(wq, current);
  403. int ret = -EIO;
  404. im = &idmap->idmap_im;
  405. /*
  406. * String sanity checks
  407. * Note that the userland daemon expects NUL terminated strings
  408. */
  409. for (;;) {
  410. if (namelen == 0)
  411. return -EINVAL;
  412. if (name[namelen-1] != '\0')
  413. break;
  414. namelen--;
  415. }
  416. if (namelen >= IDMAP_NAMESZ)
  417. return -EINVAL;
  418. mutex_lock(&idmap->idmap_lock);
  419. mutex_lock(&idmap->idmap_im_lock);
  420. he = idmap_lookup_name(h, name, namelen);
  421. if (he != NULL) {
  422. *id = he->ih_id;
  423. ret = 0;
  424. goto out;
  425. }
  426. memset(im, 0, sizeof(*im));
  427. memcpy(im->im_name, name, namelen);
  428. im->im_type = h->h_type;
  429. im->im_conv = IDMAP_CONV_NAMETOID;
  430. memset(&msg, 0, sizeof(msg));
  431. msg.data = im;
  432. msg.len = sizeof(*im);
  433. add_wait_queue(&idmap->idmap_wq, &wq);
  434. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  435. remove_wait_queue(&idmap->idmap_wq, &wq);
  436. goto out;
  437. }
  438. set_current_state(TASK_UNINTERRUPTIBLE);
  439. mutex_unlock(&idmap->idmap_im_lock);
  440. schedule();
  441. __set_current_state(TASK_RUNNING);
  442. remove_wait_queue(&idmap->idmap_wq, &wq);
  443. mutex_lock(&idmap->idmap_im_lock);
  444. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  445. *id = im->im_id;
  446. ret = 0;
  447. }
  448. out:
  449. memset(im, 0, sizeof(*im));
  450. mutex_unlock(&idmap->idmap_im_lock);
  451. mutex_unlock(&idmap->idmap_lock);
  452. return ret;
  453. }
  454. /*
  455. * ID -> Name
  456. */
  457. static int
  458. nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
  459. __u32 id, char *name)
  460. {
  461. struct rpc_pipe_msg msg;
  462. struct idmap_msg *im;
  463. struct idmap_hashent *he;
  464. DECLARE_WAITQUEUE(wq, current);
  465. int ret = -EIO;
  466. unsigned int len;
  467. im = &idmap->idmap_im;
  468. mutex_lock(&idmap->idmap_lock);
  469. mutex_lock(&idmap->idmap_im_lock);
  470. he = idmap_lookup_id(h, id);
  471. if (he) {
  472. memcpy(name, he->ih_name, he->ih_namelen);
  473. ret = he->ih_namelen;
  474. goto out;
  475. }
  476. memset(im, 0, sizeof(*im));
  477. im->im_type = h->h_type;
  478. im->im_conv = IDMAP_CONV_IDTONAME;
  479. im->im_id = id;
  480. memset(&msg, 0, sizeof(msg));
  481. msg.data = im;
  482. msg.len = sizeof(*im);
  483. add_wait_queue(&idmap->idmap_wq, &wq);
  484. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  485. remove_wait_queue(&idmap->idmap_wq, &wq);
  486. goto out;
  487. }
  488. set_current_state(TASK_UNINTERRUPTIBLE);
  489. mutex_unlock(&idmap->idmap_im_lock);
  490. schedule();
  491. __set_current_state(TASK_RUNNING);
  492. remove_wait_queue(&idmap->idmap_wq, &wq);
  493. mutex_lock(&idmap->idmap_im_lock);
  494. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  495. if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
  496. goto out;
  497. memcpy(name, im->im_name, len);
  498. ret = len;
  499. }
  500. out:
  501. memset(im, 0, sizeof(*im));
  502. mutex_unlock(&idmap->idmap_im_lock);
  503. mutex_unlock(&idmap->idmap_lock);
  504. return ret;
  505. }
  506. /* RPC pipefs upcall/downcall routines */
  507. static ssize_t
  508. idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  509. char __user *dst, size_t buflen)
  510. {
  511. char *data = (char *)msg->data + msg->copied;
  512. size_t mlen = min(msg->len, buflen);
  513. unsigned long left;
  514. left = copy_to_user(dst, data, mlen);
  515. if (left == mlen) {
  516. msg->errno = -EFAULT;
  517. return -EFAULT;
  518. }
  519. mlen -= left;
  520. msg->copied += mlen;
  521. msg->errno = 0;
  522. return mlen;
  523. }
  524. static ssize_t
  525. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  526. {
  527. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  528. struct idmap *idmap = (struct idmap *)rpci->private;
  529. struct idmap_msg im_in, *im = &idmap->idmap_im;
  530. struct idmap_hashtable *h;
  531. struct idmap_hashent *he = NULL;
  532. size_t namelen_in;
  533. int ret;
  534. if (mlen != sizeof(im_in))
  535. return -ENOSPC;
  536. if (copy_from_user(&im_in, src, mlen) != 0)
  537. return -EFAULT;
  538. mutex_lock(&idmap->idmap_im_lock);
  539. ret = mlen;
  540. im->im_status = im_in.im_status;
  541. /* If we got an error, terminate now, and wake up pending upcalls */
  542. if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
  543. wake_up(&idmap->idmap_wq);
  544. goto out;
  545. }
  546. /* Sanity checking of strings */
  547. ret = -EINVAL;
  548. namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
  549. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
  550. goto out;
  551. switch (im_in.im_type) {
  552. case IDMAP_TYPE_USER:
  553. h = &idmap->idmap_user_hash;
  554. break;
  555. case IDMAP_TYPE_GROUP:
  556. h = &idmap->idmap_group_hash;
  557. break;
  558. default:
  559. goto out;
  560. }
  561. switch (im_in.im_conv) {
  562. case IDMAP_CONV_IDTONAME:
  563. /* Did we match the current upcall? */
  564. if (im->im_conv == IDMAP_CONV_IDTONAME
  565. && im->im_type == im_in.im_type
  566. && im->im_id == im_in.im_id) {
  567. /* Yes: copy string, including the terminating '\0' */
  568. memcpy(im->im_name, im_in.im_name, namelen_in);
  569. im->im_name[namelen_in] = '\0';
  570. wake_up(&idmap->idmap_wq);
  571. }
  572. he = idmap_alloc_id(h, im_in.im_id);
  573. break;
  574. case IDMAP_CONV_NAMETOID:
  575. /* Did we match the current upcall? */
  576. if (im->im_conv == IDMAP_CONV_NAMETOID
  577. && im->im_type == im_in.im_type
  578. && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
  579. && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
  580. im->im_id = im_in.im_id;
  581. wake_up(&idmap->idmap_wq);
  582. }
  583. he = idmap_alloc_name(h, im_in.im_name, namelen_in);
  584. break;
  585. default:
  586. goto out;
  587. }
  588. /* If the entry is valid, also copy it to the cache */
  589. if (he != NULL)
  590. idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
  591. ret = mlen;
  592. out:
  593. mutex_unlock(&idmap->idmap_im_lock);
  594. return ret;
  595. }
  596. static void
  597. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  598. {
  599. struct idmap_msg *im = msg->data;
  600. struct idmap *idmap = container_of(im, struct idmap, idmap_im);
  601. if (msg->errno >= 0)
  602. return;
  603. mutex_lock(&idmap->idmap_im_lock);
  604. im->im_status = IDMAP_STATUS_LOOKUPFAIL;
  605. wake_up(&idmap->idmap_wq);
  606. mutex_unlock(&idmap->idmap_im_lock);
  607. }
  608. /*
  609. * Fowler/Noll/Vo hash
  610. * http://www.isthe.com/chongo/tech/comp/fnv/
  611. */
  612. #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
  613. #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
  614. static unsigned int fnvhash32(const void *buf, size_t buflen)
  615. {
  616. const unsigned char *p, *end = (const unsigned char *)buf + buflen;
  617. unsigned int hash = FNV_1_32;
  618. for (p = buf; p < end; p++) {
  619. hash *= FNV_P_32;
  620. hash ^= (unsigned int)*p;
  621. }
  622. return hash;
  623. }
  624. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  625. {
  626. struct idmap *idmap = server->nfs_client->cl_idmap;
  627. if (nfs_map_string_to_numeric(name, namelen, uid))
  628. return 0;
  629. return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
  630. }
  631. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  632. {
  633. struct idmap *idmap = server->nfs_client->cl_idmap;
  634. if (nfs_map_string_to_numeric(name, namelen, uid))
  635. return 0;
  636. return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
  637. }
  638. int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  639. {
  640. struct idmap *idmap = server->nfs_client->cl_idmap;
  641. int ret = -EINVAL;
  642. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  643. ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
  644. if (ret < 0)
  645. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  646. return ret;
  647. }
  648. int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  649. {
  650. struct idmap *idmap = server->nfs_client->cl_idmap;
  651. int ret = -EINVAL;
  652. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  653. ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
  654. if (ret < 0)
  655. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  656. return ret;
  657. }
  658. #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */