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