idmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/module.h>
  37. #include <linux/mutex.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/socket.h>
  42. #include <linux/in.h>
  43. #include <linux/sched.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/sunrpc/rpc_pipe_fs.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_idmap.h>
  49. #include "nfs4_fs.h"
  50. #define IDMAP_HASH_SZ 128
  51. /* Default cache timeout is 10 minutes */
  52. unsigned int nfs_idmap_cache_timeout = 600 * HZ;
  53. static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
  54. {
  55. char *endp;
  56. int num = simple_strtol(val, &endp, 0);
  57. int jif = num * HZ;
  58. if (endp == val || *endp || num < 0 || jif < num)
  59. return -EINVAL;
  60. *((int *)kp->arg) = jif;
  61. return 0;
  62. }
  63. module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
  64. &nfs_idmap_cache_timeout, 0644);
  65. struct idmap_hashent {
  66. unsigned long ih_expires;
  67. __u32 ih_id;
  68. int ih_namelen;
  69. char ih_name[IDMAP_NAMESZ];
  70. };
  71. struct idmap_hashtable {
  72. __u8 h_type;
  73. struct idmap_hashent h_entries[IDMAP_HASH_SZ];
  74. };
  75. struct idmap {
  76. struct dentry *idmap_dentry;
  77. wait_queue_head_t idmap_wq;
  78. struct idmap_msg idmap_im;
  79. struct mutex idmap_lock; /* Serializes upcalls */
  80. struct mutex idmap_im_lock; /* Protects the hashtable */
  81. struct idmap_hashtable idmap_user_hash;
  82. struct idmap_hashtable idmap_group_hash;
  83. };
  84. static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
  85. char __user *, size_t);
  86. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  87. size_t);
  88. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  89. static unsigned int fnvhash32(const void *, size_t);
  90. static struct rpc_pipe_ops idmap_upcall_ops = {
  91. .upcall = idmap_pipe_upcall,
  92. .downcall = idmap_pipe_downcall,
  93. .destroy_msg = idmap_pipe_destroy_msg,
  94. };
  95. int
  96. nfs_idmap_new(struct nfs_client *clp)
  97. {
  98. struct idmap *idmap;
  99. int error;
  100. BUG_ON(clp->cl_idmap != NULL);
  101. if ((idmap = kzalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
  102. return -ENOMEM;
  103. idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_dentry, "idmap",
  104. idmap, &idmap_upcall_ops, 0);
  105. if (IS_ERR(idmap->idmap_dentry)) {
  106. error = PTR_ERR(idmap->idmap_dentry);
  107. kfree(idmap);
  108. return error;
  109. }
  110. mutex_init(&idmap->idmap_lock);
  111. mutex_init(&idmap->idmap_im_lock);
  112. init_waitqueue_head(&idmap->idmap_wq);
  113. idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
  114. idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
  115. clp->cl_idmap = idmap;
  116. return 0;
  117. }
  118. void
  119. nfs_idmap_delete(struct nfs_client *clp)
  120. {
  121. struct idmap *idmap = clp->cl_idmap;
  122. if (!idmap)
  123. return;
  124. rpc_unlink(idmap->idmap_dentry);
  125. clp->cl_idmap = NULL;
  126. kfree(idmap);
  127. }
  128. /*
  129. * Helper routines for manipulating the hashtable
  130. */
  131. static inline struct idmap_hashent *
  132. idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
  133. {
  134. return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
  135. }
  136. static struct idmap_hashent *
  137. idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
  138. {
  139. struct idmap_hashent *he = idmap_name_hash(h, name, len);
  140. if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
  141. return NULL;
  142. if (time_after(jiffies, he->ih_expires))
  143. return NULL;
  144. return he;
  145. }
  146. static inline struct idmap_hashent *
  147. idmap_id_hash(struct idmap_hashtable* h, __u32 id)
  148. {
  149. return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
  150. }
  151. static struct idmap_hashent *
  152. idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
  153. {
  154. struct idmap_hashent *he = idmap_id_hash(h, id);
  155. if (he->ih_id != id || he->ih_namelen == 0)
  156. return NULL;
  157. if (time_after(jiffies, he->ih_expires))
  158. return NULL;
  159. return he;
  160. }
  161. /*
  162. * Routines for allocating new entries in the hashtable.
  163. * For now, we just have 1 entry per bucket, so it's all
  164. * pretty trivial.
  165. */
  166. static inline struct idmap_hashent *
  167. idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
  168. {
  169. return idmap_name_hash(h, name, len);
  170. }
  171. static inline struct idmap_hashent *
  172. idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
  173. {
  174. return idmap_id_hash(h, id);
  175. }
  176. static void
  177. idmap_update_entry(struct idmap_hashent *he, const char *name,
  178. size_t namelen, __u32 id)
  179. {
  180. he->ih_id = id;
  181. memcpy(he->ih_name, name, namelen);
  182. he->ih_name[namelen] = '\0';
  183. he->ih_namelen = namelen;
  184. he->ih_expires = jiffies + nfs_idmap_cache_timeout;
  185. }
  186. /*
  187. * Name -> ID
  188. */
  189. static int
  190. nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
  191. const char *name, size_t namelen, __u32 *id)
  192. {
  193. struct rpc_pipe_msg msg;
  194. struct idmap_msg *im;
  195. struct idmap_hashent *he;
  196. DECLARE_WAITQUEUE(wq, current);
  197. int ret = -EIO;
  198. im = &idmap->idmap_im;
  199. /*
  200. * String sanity checks
  201. * Note that the userland daemon expects NUL terminated strings
  202. */
  203. for (;;) {
  204. if (namelen == 0)
  205. return -EINVAL;
  206. if (name[namelen-1] != '\0')
  207. break;
  208. namelen--;
  209. }
  210. if (namelen >= IDMAP_NAMESZ)
  211. return -EINVAL;
  212. mutex_lock(&idmap->idmap_lock);
  213. mutex_lock(&idmap->idmap_im_lock);
  214. he = idmap_lookup_name(h, name, namelen);
  215. if (he != NULL) {
  216. *id = he->ih_id;
  217. ret = 0;
  218. goto out;
  219. }
  220. memset(im, 0, sizeof(*im));
  221. memcpy(im->im_name, name, namelen);
  222. im->im_type = h->h_type;
  223. im->im_conv = IDMAP_CONV_NAMETOID;
  224. memset(&msg, 0, sizeof(msg));
  225. msg.data = im;
  226. msg.len = sizeof(*im);
  227. add_wait_queue(&idmap->idmap_wq, &wq);
  228. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  229. remove_wait_queue(&idmap->idmap_wq, &wq);
  230. goto out;
  231. }
  232. set_current_state(TASK_UNINTERRUPTIBLE);
  233. mutex_unlock(&idmap->idmap_im_lock);
  234. schedule();
  235. current->state = TASK_RUNNING;
  236. remove_wait_queue(&idmap->idmap_wq, &wq);
  237. mutex_lock(&idmap->idmap_im_lock);
  238. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  239. *id = im->im_id;
  240. ret = 0;
  241. }
  242. out:
  243. memset(im, 0, sizeof(*im));
  244. mutex_unlock(&idmap->idmap_im_lock);
  245. mutex_unlock(&idmap->idmap_lock);
  246. return (ret);
  247. }
  248. /*
  249. * ID -> Name
  250. */
  251. static int
  252. nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
  253. __u32 id, char *name)
  254. {
  255. struct rpc_pipe_msg msg;
  256. struct idmap_msg *im;
  257. struct idmap_hashent *he;
  258. DECLARE_WAITQUEUE(wq, current);
  259. int ret = -EIO;
  260. unsigned int len;
  261. im = &idmap->idmap_im;
  262. mutex_lock(&idmap->idmap_lock);
  263. mutex_lock(&idmap->idmap_im_lock);
  264. he = idmap_lookup_id(h, id);
  265. if (he != 0) {
  266. memcpy(name, he->ih_name, he->ih_namelen);
  267. ret = he->ih_namelen;
  268. goto out;
  269. }
  270. memset(im, 0, sizeof(*im));
  271. im->im_type = h->h_type;
  272. im->im_conv = IDMAP_CONV_IDTONAME;
  273. im->im_id = id;
  274. memset(&msg, 0, sizeof(msg));
  275. msg.data = im;
  276. msg.len = sizeof(*im);
  277. add_wait_queue(&idmap->idmap_wq, &wq);
  278. if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
  279. remove_wait_queue(&idmap->idmap_wq, &wq);
  280. goto out;
  281. }
  282. set_current_state(TASK_UNINTERRUPTIBLE);
  283. mutex_unlock(&idmap->idmap_im_lock);
  284. schedule();
  285. current->state = TASK_RUNNING;
  286. remove_wait_queue(&idmap->idmap_wq, &wq);
  287. mutex_lock(&idmap->idmap_im_lock);
  288. if (im->im_status & IDMAP_STATUS_SUCCESS) {
  289. if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
  290. goto out;
  291. memcpy(name, im->im_name, len);
  292. ret = len;
  293. }
  294. out:
  295. memset(im, 0, sizeof(*im));
  296. mutex_unlock(&idmap->idmap_im_lock);
  297. mutex_unlock(&idmap->idmap_lock);
  298. return ret;
  299. }
  300. /* RPC pipefs upcall/downcall routines */
  301. static ssize_t
  302. idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  303. char __user *dst, size_t buflen)
  304. {
  305. char *data = (char *)msg->data + msg->copied;
  306. ssize_t mlen = msg->len - msg->copied;
  307. ssize_t left;
  308. if (mlen > buflen)
  309. mlen = buflen;
  310. left = copy_to_user(dst, data, mlen);
  311. if (left < 0) {
  312. msg->errno = left;
  313. return left;
  314. }
  315. mlen -= left;
  316. msg->copied += mlen;
  317. msg->errno = 0;
  318. return mlen;
  319. }
  320. static ssize_t
  321. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  322. {
  323. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  324. struct idmap *idmap = (struct idmap *)rpci->private;
  325. struct idmap_msg im_in, *im = &idmap->idmap_im;
  326. struct idmap_hashtable *h;
  327. struct idmap_hashent *he = NULL;
  328. int namelen_in;
  329. int ret;
  330. if (mlen != sizeof(im_in))
  331. return (-ENOSPC);
  332. if (copy_from_user(&im_in, src, mlen) != 0)
  333. return (-EFAULT);
  334. mutex_lock(&idmap->idmap_im_lock);
  335. ret = mlen;
  336. im->im_status = im_in.im_status;
  337. /* If we got an error, terminate now, and wake up pending upcalls */
  338. if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
  339. wake_up(&idmap->idmap_wq);
  340. goto out;
  341. }
  342. /* Sanity checking of strings */
  343. ret = -EINVAL;
  344. namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
  345. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
  346. goto out;
  347. switch (im_in.im_type) {
  348. case IDMAP_TYPE_USER:
  349. h = &idmap->idmap_user_hash;
  350. break;
  351. case IDMAP_TYPE_GROUP:
  352. h = &idmap->idmap_group_hash;
  353. break;
  354. default:
  355. goto out;
  356. }
  357. switch (im_in.im_conv) {
  358. case IDMAP_CONV_IDTONAME:
  359. /* Did we match the current upcall? */
  360. if (im->im_conv == IDMAP_CONV_IDTONAME
  361. && im->im_type == im_in.im_type
  362. && im->im_id == im_in.im_id) {
  363. /* Yes: copy string, including the terminating '\0' */
  364. memcpy(im->im_name, im_in.im_name, namelen_in);
  365. im->im_name[namelen_in] = '\0';
  366. wake_up(&idmap->idmap_wq);
  367. }
  368. he = idmap_alloc_id(h, im_in.im_id);
  369. break;
  370. case IDMAP_CONV_NAMETOID:
  371. /* Did we match the current upcall? */
  372. if (im->im_conv == IDMAP_CONV_NAMETOID
  373. && im->im_type == im_in.im_type
  374. && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
  375. && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
  376. im->im_id = im_in.im_id;
  377. wake_up(&idmap->idmap_wq);
  378. }
  379. he = idmap_alloc_name(h, im_in.im_name, namelen_in);
  380. break;
  381. default:
  382. goto out;
  383. }
  384. /* If the entry is valid, also copy it to the cache */
  385. if (he != NULL)
  386. idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
  387. ret = mlen;
  388. out:
  389. mutex_unlock(&idmap->idmap_im_lock);
  390. return ret;
  391. }
  392. static void
  393. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  394. {
  395. struct idmap_msg *im = msg->data;
  396. struct idmap *idmap = container_of(im, struct idmap, idmap_im);
  397. if (msg->errno >= 0)
  398. return;
  399. mutex_lock(&idmap->idmap_im_lock);
  400. im->im_status = IDMAP_STATUS_LOOKUPFAIL;
  401. wake_up(&idmap->idmap_wq);
  402. mutex_unlock(&idmap->idmap_im_lock);
  403. }
  404. /*
  405. * Fowler/Noll/Vo hash
  406. * http://www.isthe.com/chongo/tech/comp/fnv/
  407. */
  408. #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
  409. #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
  410. static unsigned int fnvhash32(const void *buf, size_t buflen)
  411. {
  412. const unsigned char *p, *end = (const unsigned char *)buf + buflen;
  413. unsigned int hash = FNV_1_32;
  414. for (p = buf; p < end; p++) {
  415. hash *= FNV_P_32;
  416. hash ^= (unsigned int)*p;
  417. }
  418. return (hash);
  419. }
  420. int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
  421. {
  422. struct idmap *idmap = clp->cl_idmap;
  423. return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
  424. }
  425. int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
  426. {
  427. struct idmap *idmap = clp->cl_idmap;
  428. return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
  429. }
  430. int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
  431. {
  432. struct idmap *idmap = clp->cl_idmap;
  433. return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
  434. }
  435. int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
  436. {
  437. struct idmap *idmap = clp->cl_idmap;
  438. return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
  439. }