idmap.c 21 KB

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