callback.c 5.1 KB

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