idmap.c 12 KB

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