idmap.c 13 KB

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