idmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. #include "internal.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 rpc_pipe *idmap_pipe;
  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. static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
  372. {
  373. if (pipe->dentry)
  374. rpc_unlink(pipe->dentry);
  375. }
  376. static int __nfs_idmap_register(struct dentry *dir,
  377. struct idmap *idmap,
  378. struct rpc_pipe *pipe)
  379. {
  380. struct dentry *dentry;
  381. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  382. if (IS_ERR(dentry))
  383. return PTR_ERR(dentry);
  384. pipe->dentry = dentry;
  385. return 0;
  386. }
  387. static void nfs_idmap_unregister(struct nfs_client *clp,
  388. struct rpc_pipe *pipe)
  389. {
  390. struct net *net = clp->net;
  391. struct super_block *pipefs_sb;
  392. pipefs_sb = rpc_get_sb_net(net);
  393. if (pipefs_sb) {
  394. __nfs_idmap_unregister(pipe);
  395. rpc_put_sb_net(net);
  396. }
  397. }
  398. static int nfs_idmap_register(struct nfs_client *clp,
  399. struct idmap *idmap,
  400. struct rpc_pipe *pipe)
  401. {
  402. struct net *net = clp->net;
  403. struct super_block *pipefs_sb;
  404. int err = 0;
  405. pipefs_sb = rpc_get_sb_net(net);
  406. if (pipefs_sb) {
  407. if (clp->cl_rpcclient->cl_dentry)
  408. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  409. idmap, pipe);
  410. rpc_put_sb_net(net);
  411. }
  412. return err;
  413. }
  414. int
  415. nfs_idmap_new(struct nfs_client *clp)
  416. {
  417. struct idmap *idmap;
  418. struct rpc_pipe *pipe;
  419. int error;
  420. BUG_ON(clp->cl_idmap != NULL);
  421. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  422. if (idmap == NULL)
  423. return -ENOMEM;
  424. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  425. if (IS_ERR(pipe)) {
  426. error = PTR_ERR(pipe);
  427. kfree(idmap);
  428. return error;
  429. }
  430. error = nfs_idmap_register(clp, idmap, pipe);
  431. if (error) {
  432. rpc_destroy_pipe_data(pipe);
  433. kfree(idmap);
  434. return error;
  435. }
  436. idmap->idmap_pipe = pipe;
  437. mutex_init(&idmap->idmap_lock);
  438. mutex_init(&idmap->idmap_im_lock);
  439. init_waitqueue_head(&idmap->idmap_wq);
  440. idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
  441. idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
  442. clp->cl_idmap = idmap;
  443. return 0;
  444. }
  445. void
  446. nfs_idmap_delete(struct nfs_client *clp)
  447. {
  448. struct idmap *idmap = clp->cl_idmap;
  449. if (!idmap)
  450. return;
  451. nfs_idmap_unregister(clp, idmap->idmap_pipe);
  452. rpc_destroy_pipe_data(idmap->idmap_pipe);
  453. clp->cl_idmap = NULL;
  454. kfree(idmap);
  455. }
  456. static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
  457. struct super_block *sb)
  458. {
  459. int err = 0;
  460. switch (event) {
  461. case RPC_PIPEFS_MOUNT:
  462. BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
  463. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  464. clp->cl_idmap,
  465. clp->cl_idmap->idmap_pipe);
  466. break;
  467. case RPC_PIPEFS_UMOUNT:
  468. if (clp->cl_idmap->idmap_pipe) {
  469. struct dentry *parent;
  470. parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
  471. __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
  472. /*
  473. * Note: This is a dirty hack. SUNRPC hook has been
  474. * called already but simple_rmdir() call for the
  475. * directory returned with error because of idmap pipe
  476. * inside. Thus now we have to remove this directory
  477. * here.
  478. */
  479. if (rpc_rmdir(parent))
  480. printk(KERN_ERR "%s: failed to remove clnt dir!\n", __func__);
  481. }
  482. break;
  483. default:
  484. printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
  485. return -ENOTSUPP;
  486. }
  487. return err;
  488. }
  489. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  490. void *ptr)
  491. {
  492. struct super_block *sb = ptr;
  493. struct nfs_client *clp;
  494. int error = 0;
  495. spin_lock(&nfs_client_lock);
  496. list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
  497. if (clp->net != sb->s_fs_info)
  498. continue;
  499. if (clp->rpc_ops != &nfs_v4_clientops)
  500. continue;
  501. error = __rpc_pipefs_event(clp, event, sb);
  502. if (error)
  503. break;
  504. }
  505. spin_unlock(&nfs_client_lock);
  506. return error;
  507. }
  508. #define PIPEFS_NFS_PRIO 1
  509. static struct notifier_block nfs_idmap_block = {
  510. .notifier_call = rpc_pipefs_event,
  511. .priority = SUNRPC_PIPEFS_NFS_PRIO,
  512. };
  513. int nfs_idmap_init(void)
  514. {
  515. return rpc_pipefs_notifier_register(&nfs_idmap_block);
  516. }
  517. void nfs_idmap_quit(void)
  518. {
  519. rpc_pipefs_notifier_unregister(&nfs_idmap_block);
  520. }
  521. /*
  522. * Helper routines for manipulating the hashtable
  523. */
  524. static inline struct idmap_hashent *
  525. idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
  526. {
  527. return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
  528. }
  529. static struct idmap_hashent *
  530. idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
  531. {
  532. struct idmap_hashent *he = idmap_name_hash(h, name, len);
  533. if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
  534. return NULL;
  535. if (time_after(jiffies, he->ih_expires))
  536. return NULL;
  537. return he;
  538. }
  539. static inline struct idmap_hashent *
  540. idmap_id_hash(struct idmap_hashtable* h, __u32 id)
  541. {
  542. return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
  543. }
  544. static struct idmap_hashent *
  545. idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
  546. {
  547. struct idmap_hashent *he = idmap_id_hash(h, id);
  548. if (he->ih_id != id || he->ih_namelen == 0)
  549. return NULL;
  550. if (time_after(jiffies, he->ih_expires))
  551. return NULL;
  552. return he;
  553. }
  554. /*
  555. * Routines for allocating new entries in the hashtable.
  556. * For now, we just have 1 entry per bucket, so it's all
  557. * pretty trivial.
  558. */
  559. static inline struct idmap_hashent *
  560. idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
  561. {
  562. return idmap_name_hash(h, name, len);
  563. }
  564. static inline struct idmap_hashent *
  565. idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
  566. {
  567. return idmap_id_hash(h, id);
  568. }
  569. static void
  570. idmap_update_entry(struct idmap_hashent *he, const char *name,
  571. size_t namelen, __u32 id)
  572. {
  573. he->ih_id = id;
  574. memcpy(he->ih_name, name, namelen);
  575. he->ih_name[namelen] = '\0';
  576. he->ih_namelen = namelen;
  577. he->ih_expires = jiffies + nfs_idmap_cache_timeout;
  578. }
  579. /*
  580. * Name -> ID
  581. */
  582. static int
  583. nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
  584. const char *name, size_t namelen, __u32 *id)
  585. {
  586. struct rpc_pipe_msg msg;
  587. struct idmap_msg *im;
  588. struct idmap_hashent *he;
  589. DECLARE_WAITQUEUE(wq, current);
  590. int ret = -EIO;
  591. im = &idmap->idmap_im;
  592. /*
  593. * String sanity checks
  594. * Note that the userland daemon expects NUL terminated strings
  595. */
  596. for (;;) {
  597. if (namelen == 0)
  598. return -EINVAL;
  599. if (name[namelen-1] != '\0')
  600. break;
  601. namelen--;
  602. }
  603. if (namelen >= IDMAP_NAMESZ)
  604. return -EINVAL;
  605. mutex_lock(&idmap->idmap_lock);
  606. mutex_lock(&idmap->idmap_im_lock);
  607. he = idmap_lookup_name(h, name, namelen);
  608. if (he != NULL) {
  609. *id = he->ih_id;
  610. ret = 0;
  611. goto out;
  612. }
  613. memset(im, 0, sizeof(*im));
  614. memcpy(im->im_name, name, namelen);
  615. im->im_type = h->h_type;
  616. im->im_conv = IDMAP_CONV_NAMETOID;
  617. memset(&msg, 0, sizeof(msg));
  618. msg.data = im;
  619. msg.len = sizeof(*im);
  620. add_wait_queue(&idmap->idmap_wq, &wq);
  621. if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
  622. remove_wait_queue(&idmap->idmap_wq, &wq);
  623. goto out;
  624. }
  625. set_current_state(TASK_UNINTERRUPTIBLE);
  626. mutex_unlock(&idmap->idmap_im_lock);
  627. schedule();
  628. __set_current_state(TASK_RUNNING);
  629. remove_wait_queue(&idmap->idmap_wq, &wq);
  630. mutex_lock(&idmap->idmap_im_lock);
  631. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  632. *id = im->im_id;
  633. ret = 0;
  634. }
  635. out:
  636. memset(im, 0, sizeof(*im));
  637. mutex_unlock(&idmap->idmap_im_lock);
  638. mutex_unlock(&idmap->idmap_lock);
  639. return ret;
  640. }
  641. /*
  642. * ID -> Name
  643. */
  644. static int
  645. nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
  646. __u32 id, char *name)
  647. {
  648. struct rpc_pipe_msg msg;
  649. struct idmap_msg *im;
  650. struct idmap_hashent *he;
  651. DECLARE_WAITQUEUE(wq, current);
  652. int ret = -EIO;
  653. unsigned int len;
  654. im = &idmap->idmap_im;
  655. mutex_lock(&idmap->idmap_lock);
  656. mutex_lock(&idmap->idmap_im_lock);
  657. he = idmap_lookup_id(h, id);
  658. if (he) {
  659. memcpy(name, he->ih_name, he->ih_namelen);
  660. ret = he->ih_namelen;
  661. goto out;
  662. }
  663. memset(im, 0, sizeof(*im));
  664. im->im_type = h->h_type;
  665. im->im_conv = IDMAP_CONV_IDTONAME;
  666. im->im_id = id;
  667. memset(&msg, 0, sizeof(msg));
  668. msg.data = im;
  669. msg.len = sizeof(*im);
  670. add_wait_queue(&idmap->idmap_wq, &wq);
  671. if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
  672. remove_wait_queue(&idmap->idmap_wq, &wq);
  673. goto out;
  674. }
  675. set_current_state(TASK_UNINTERRUPTIBLE);
  676. mutex_unlock(&idmap->idmap_im_lock);
  677. schedule();
  678. __set_current_state(TASK_RUNNING);
  679. remove_wait_queue(&idmap->idmap_wq, &wq);
  680. mutex_lock(&idmap->idmap_im_lock);
  681. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  682. if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
  683. goto out;
  684. memcpy(name, im->im_name, len);
  685. ret = len;
  686. }
  687. out:
  688. memset(im, 0, sizeof(*im));
  689. mutex_unlock(&idmap->idmap_im_lock);
  690. mutex_unlock(&idmap->idmap_lock);
  691. return ret;
  692. }
  693. static ssize_t
  694. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  695. {
  696. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  697. struct idmap *idmap = (struct idmap *)rpci->private;
  698. struct idmap_msg im_in, *im = &idmap->idmap_im;
  699. struct idmap_hashtable *h;
  700. struct idmap_hashent *he = NULL;
  701. size_t namelen_in;
  702. int ret;
  703. if (mlen != sizeof(im_in))
  704. return -ENOSPC;
  705. if (copy_from_user(&im_in, src, mlen) != 0)
  706. return -EFAULT;
  707. mutex_lock(&idmap->idmap_im_lock);
  708. ret = mlen;
  709. im->im_status = im_in.im_status;
  710. /* If we got an error, terminate now, and wake up pending upcalls */
  711. if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
  712. wake_up(&idmap->idmap_wq);
  713. goto out;
  714. }
  715. /* Sanity checking of strings */
  716. ret = -EINVAL;
  717. namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
  718. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
  719. goto out;
  720. switch (im_in.im_type) {
  721. case IDMAP_TYPE_USER:
  722. h = &idmap->idmap_user_hash;
  723. break;
  724. case IDMAP_TYPE_GROUP:
  725. h = &idmap->idmap_group_hash;
  726. break;
  727. default:
  728. goto out;
  729. }
  730. switch (im_in.im_conv) {
  731. case IDMAP_CONV_IDTONAME:
  732. /* Did we match the current upcall? */
  733. if (im->im_conv == IDMAP_CONV_IDTONAME
  734. && im->im_type == im_in.im_type
  735. && im->im_id == im_in.im_id) {
  736. /* Yes: copy string, including the terminating '\0' */
  737. memcpy(im->im_name, im_in.im_name, namelen_in);
  738. im->im_name[namelen_in] = '\0';
  739. wake_up(&idmap->idmap_wq);
  740. }
  741. he = idmap_alloc_id(h, im_in.im_id);
  742. break;
  743. case IDMAP_CONV_NAMETOID:
  744. /* Did we match the current upcall? */
  745. if (im->im_conv == IDMAP_CONV_NAMETOID
  746. && im->im_type == im_in.im_type
  747. && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
  748. && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
  749. im->im_id = im_in.im_id;
  750. wake_up(&idmap->idmap_wq);
  751. }
  752. he = idmap_alloc_name(h, im_in.im_name, namelen_in);
  753. break;
  754. default:
  755. goto out;
  756. }
  757. /* If the entry is valid, also copy it to the cache */
  758. if (he != NULL)
  759. idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
  760. ret = mlen;
  761. out:
  762. mutex_unlock(&idmap->idmap_im_lock);
  763. return ret;
  764. }
  765. static void
  766. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  767. {
  768. struct idmap_msg *im = msg->data;
  769. struct idmap *idmap = container_of(im, struct idmap, idmap_im);
  770. if (msg->errno >= 0)
  771. return;
  772. mutex_lock(&idmap->idmap_im_lock);
  773. im->im_status = IDMAP_STATUS_LOOKUPFAIL;
  774. wake_up(&idmap->idmap_wq);
  775. mutex_unlock(&idmap->idmap_im_lock);
  776. }
  777. /*
  778. * Fowler/Noll/Vo hash
  779. * http://www.isthe.com/chongo/tech/comp/fnv/
  780. */
  781. #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
  782. #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
  783. static unsigned int fnvhash32(const void *buf, size_t buflen)
  784. {
  785. const unsigned char *p, *end = (const unsigned char *)buf + buflen;
  786. unsigned int hash = FNV_1_32;
  787. for (p = buf; p < end; p++) {
  788. hash *= FNV_P_32;
  789. hash ^= (unsigned int)*p;
  790. }
  791. return hash;
  792. }
  793. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  794. {
  795. struct idmap *idmap = server->nfs_client->cl_idmap;
  796. if (nfs_map_string_to_numeric(name, namelen, uid))
  797. return 0;
  798. return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
  799. }
  800. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  801. {
  802. struct idmap *idmap = server->nfs_client->cl_idmap;
  803. if (nfs_map_string_to_numeric(name, namelen, uid))
  804. return 0;
  805. return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
  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_name(idmap, &idmap->idmap_user_hash, uid, buf);
  813. if (ret < 0)
  814. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  815. return ret;
  816. }
  817. int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  818. {
  819. struct idmap *idmap = server->nfs_client->cl_idmap;
  820. int ret = -EINVAL;
  821. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  822. ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
  823. if (ret < 0)
  824. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  825. return ret;
  826. }
  827. #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */