idmap.c 13 KB

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