idmap.c 13 KB

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