idmap.c 13 KB

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