idmap.c 23 KB

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