idmap.c 21 KB

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