callback.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * linux/fs/nfs/callback.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback handling
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/ip.h>
  10. #include <linux/module.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/sunrpc/svc.h>
  13. #include <linux/sunrpc/svcsock.h>
  14. #include <linux/nfs_fs.h>
  15. #include <linux/mutex.h>
  16. #include <linux/freezer.h>
  17. #include <linux/kthread.h>
  18. #include <net/inet_sock.h>
  19. #include "nfs4_fs.h"
  20. #include "callback.h"
  21. #include "internal.h"
  22. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  23. struct nfs_callback_data {
  24. unsigned int users;
  25. struct svc_serv *serv;
  26. struct task_struct *task;
  27. };
  28. static struct nfs_callback_data nfs_callback_info;
  29. static DEFINE_MUTEX(nfs_callback_mutex);
  30. static struct svc_program nfs4_callback_program;
  31. unsigned int nfs_callback_set_tcpport;
  32. unsigned short nfs_callback_tcpport;
  33. static const int nfs_set_port_min = 0;
  34. static const int nfs_set_port_max = 65535;
  35. static int param_set_port(const char *val, struct kernel_param *kp)
  36. {
  37. char *endp;
  38. int num = simple_strtol(val, &endp, 0);
  39. if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
  40. return -EINVAL;
  41. *((int *)kp->arg) = num;
  42. return 0;
  43. }
  44. module_param_call(callback_tcpport, param_set_port, param_get_int,
  45. &nfs_callback_set_tcpport, 0644);
  46. /*
  47. * This is the callback kernel thread.
  48. */
  49. static int
  50. nfs_callback_svc(void *vrqstp)
  51. {
  52. int err;
  53. struct svc_rqst *rqstp = vrqstp;
  54. set_freezable();
  55. /*
  56. * FIXME: do we really need to run this under the BKL? If so, please
  57. * add a comment about what it's intended to protect.
  58. */
  59. lock_kernel();
  60. while (!kthread_should_stop()) {
  61. /*
  62. * Listen for a request on the socket
  63. */
  64. err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
  65. if (err == -EAGAIN || err == -EINTR)
  66. continue;
  67. if (err < 0) {
  68. printk(KERN_WARNING
  69. "%s: terminating on error %d\n",
  70. __FUNCTION__, -err);
  71. break;
  72. }
  73. svc_process(rqstp);
  74. }
  75. unlock_kernel();
  76. nfs_callback_info.task = NULL;
  77. svc_exit_thread(rqstp);
  78. return 0;
  79. }
  80. /*
  81. * Bring up the server process if it is not already up.
  82. */
  83. int nfs_callback_up(void)
  84. {
  85. struct svc_serv *serv = NULL;
  86. struct svc_rqst *rqstp;
  87. int ret = 0;
  88. lock_kernel();
  89. mutex_lock(&nfs_callback_mutex);
  90. if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
  91. goto out;
  92. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
  93. ret = -ENOMEM;
  94. if (!serv)
  95. goto out_err;
  96. ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
  97. SVC_SOCK_ANONYMOUS);
  98. if (ret <= 0)
  99. goto out_err;
  100. nfs_callback_tcpport = ret;
  101. dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
  102. rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
  103. if (IS_ERR(rqstp)) {
  104. ret = PTR_ERR(rqstp);
  105. goto out_err;
  106. }
  107. svc_sock_update_bufs(serv);
  108. nfs_callback_info.serv = serv;
  109. nfs_callback_info.task = kthread_run(nfs_callback_svc, rqstp,
  110. "nfsv4-svc");
  111. if (IS_ERR(nfs_callback_info.task)) {
  112. ret = PTR_ERR(nfs_callback_info.task);
  113. nfs_callback_info.serv = NULL;
  114. nfs_callback_info.task = NULL;
  115. svc_exit_thread(rqstp);
  116. goto out_err;
  117. }
  118. out:
  119. /*
  120. * svc_create creates the svc_serv with sv_nrthreads == 1, and then
  121. * svc_prepare_thread increments that. So we need to call svc_destroy
  122. * on both success and failure so that the refcount is 1 when the
  123. * thread exits.
  124. */
  125. if (serv)
  126. svc_destroy(serv);
  127. mutex_unlock(&nfs_callback_mutex);
  128. unlock_kernel();
  129. return ret;
  130. out_err:
  131. dprintk("Couldn't create callback socket or server thread; err = %d\n",
  132. ret);
  133. nfs_callback_info.users--;
  134. goto out;
  135. }
  136. /*
  137. * Kill the server process if it is not already up.
  138. */
  139. void nfs_callback_down(void)
  140. {
  141. lock_kernel();
  142. mutex_lock(&nfs_callback_mutex);
  143. nfs_callback_info.users--;
  144. if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL)
  145. kthread_stop(nfs_callback_info.task);
  146. mutex_unlock(&nfs_callback_mutex);
  147. unlock_kernel();
  148. }
  149. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  150. {
  151. struct nfs_client *clp;
  152. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  153. /* Don't talk to strangers */
  154. clp = nfs_find_client(svc_addr(rqstp), 4);
  155. if (clp == NULL)
  156. return SVC_DROP;
  157. dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
  158. svc_print_addr(rqstp, buf, sizeof(buf)));
  159. nfs_put_client(clp);
  160. switch (rqstp->rq_authop->flavour) {
  161. case RPC_AUTH_NULL:
  162. if (rqstp->rq_proc != CB_NULL)
  163. return SVC_DENIED;
  164. break;
  165. case RPC_AUTH_UNIX:
  166. break;
  167. case RPC_AUTH_GSS:
  168. /* FIXME: RPCSEC_GSS handling? */
  169. default:
  170. return SVC_DENIED;
  171. }
  172. return SVC_OK;
  173. }
  174. /*
  175. * Define NFS4 callback program
  176. */
  177. static struct svc_version *nfs4_callback_version[] = {
  178. [1] = &nfs4_callback_version1,
  179. };
  180. static struct svc_stat nfs4_callback_stats;
  181. static struct svc_program nfs4_callback_program = {
  182. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  183. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  184. .pg_vers = nfs4_callback_version, /* version table */
  185. .pg_name = "NFSv4 callback", /* service name */
  186. .pg_class = "nfs", /* authentication class */
  187. .pg_stats = &nfs4_callback_stats,
  188. .pg_authenticate = nfs_callback_authenticate,
  189. };