idmap.c 13 KB

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