idmap.c 13 KB

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