idmap.c 24 KB

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