callback.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, preverr = 0;
  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. preverr = err;
  67. continue;
  68. }
  69. if (err < 0) {
  70. if (err != preverr) {
  71. printk(KERN_WARNING "%s: unexpected error "
  72. "from svc_recv (%d)\n", __func__, err);
  73. preverr = err;
  74. }
  75. schedule_timeout_uninterruptible(HZ);
  76. continue;
  77. }
  78. preverr = err;
  79. svc_process(rqstp);
  80. }
  81. unlock_kernel();
  82. nfs_callback_info.task = NULL;
  83. svc_exit_thread(rqstp);
  84. return 0;
  85. }
  86. /*
  87. * Bring up the server process if it is not already up.
  88. */
  89. int nfs_callback_up(void)
  90. {
  91. struct svc_serv *serv = NULL;
  92. struct svc_rqst *rqstp;
  93. int ret = 0;
  94. lock_kernel();
  95. mutex_lock(&nfs_callback_mutex);
  96. if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
  97. goto out;
  98. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
  99. ret = -ENOMEM;
  100. if (!serv)
  101. goto out_err;
  102. ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
  103. SVC_SOCK_ANONYMOUS);
  104. if (ret <= 0)
  105. goto out_err;
  106. nfs_callback_tcpport = ret;
  107. dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
  108. rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
  109. if (IS_ERR(rqstp)) {
  110. ret = PTR_ERR(rqstp);
  111. goto out_err;
  112. }
  113. svc_sock_update_bufs(serv);
  114. nfs_callback_info.serv = serv;
  115. nfs_callback_info.task = kthread_run(nfs_callback_svc, rqstp,
  116. "nfsv4-svc");
  117. if (IS_ERR(nfs_callback_info.task)) {
  118. ret = PTR_ERR(nfs_callback_info.task);
  119. nfs_callback_info.serv = NULL;
  120. nfs_callback_info.task = NULL;
  121. svc_exit_thread(rqstp);
  122. goto out_err;
  123. }
  124. out:
  125. /*
  126. * svc_create creates the svc_serv with sv_nrthreads == 1, and then
  127. * svc_prepare_thread increments that. So we need to call svc_destroy
  128. * on both success and failure so that the refcount is 1 when the
  129. * thread exits.
  130. */
  131. if (serv)
  132. svc_destroy(serv);
  133. mutex_unlock(&nfs_callback_mutex);
  134. unlock_kernel();
  135. return ret;
  136. out_err:
  137. dprintk("Couldn't create callback socket or server thread; err = %d\n",
  138. ret);
  139. nfs_callback_info.users--;
  140. goto out;
  141. }
  142. /*
  143. * Kill the server process if it is not already down.
  144. */
  145. void nfs_callback_down(void)
  146. {
  147. lock_kernel();
  148. mutex_lock(&nfs_callback_mutex);
  149. nfs_callback_info.users--;
  150. if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL)
  151. kthread_stop(nfs_callback_info.task);
  152. mutex_unlock(&nfs_callback_mutex);
  153. unlock_kernel();
  154. }
  155. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  156. {
  157. struct nfs_client *clp;
  158. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  159. /* Don't talk to strangers */
  160. clp = nfs_find_client(svc_addr(rqstp), 4);
  161. if (clp == NULL)
  162. return SVC_DROP;
  163. dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
  164. svc_print_addr(rqstp, buf, sizeof(buf)));
  165. nfs_put_client(clp);
  166. switch (rqstp->rq_authop->flavour) {
  167. case RPC_AUTH_NULL:
  168. if (rqstp->rq_proc != CB_NULL)
  169. return SVC_DENIED;
  170. break;
  171. case RPC_AUTH_UNIX:
  172. break;
  173. case RPC_AUTH_GSS:
  174. /* FIXME: RPCSEC_GSS handling? */
  175. default:
  176. return SVC_DENIED;
  177. }
  178. return SVC_OK;
  179. }
  180. /*
  181. * Define NFS4 callback program
  182. */
  183. static struct svc_version *nfs4_callback_version[] = {
  184. [1] = &nfs4_callback_version1,
  185. };
  186. static struct svc_stat nfs4_callback_stats;
  187. static struct svc_program nfs4_callback_program = {
  188. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  189. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  190. .pg_vers = nfs4_callback_version, /* version table */
  191. .pg_name = "NFSv4 callback", /* service name */
  192. .pg_class = "nfs", /* authentication class */
  193. .pg_stats = &nfs4_callback_stats,
  194. .pg_authenticate = nfs_callback_authenticate,
  195. };