idmap.c 12 KB

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