callback.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <net/inet_sock.h>
  17. #include "nfs4_fs.h"
  18. #include "callback.h"
  19. #include "internal.h"
  20. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  21. struct nfs_callback_data {
  22. unsigned int users;
  23. struct svc_serv *serv;
  24. pid_t pid;
  25. struct completion started;
  26. struct completion stopped;
  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 void nfs_callback_svc(struct svc_rqst *rqstp)
  50. {
  51. struct svc_serv *serv = rqstp->rq_server;
  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. complete(&nfs_callback_info.started);
  60. for(;;) {
  61. if (signalled()) {
  62. if (nfs_callback_info.users == 0)
  63. break;
  64. flush_signals(current);
  65. }
  66. /*
  67. * Listen for a request on the socket
  68. */
  69. err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
  70. if (err == -EAGAIN || err == -EINTR)
  71. continue;
  72. if (err < 0) {
  73. printk(KERN_WARNING
  74. "%s: terminating on error %d\n",
  75. __FUNCTION__, -err);
  76. break;
  77. }
  78. dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
  79. NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
  80. svc_process(serv, rqstp);
  81. }
  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;
  94. struct svc_sock *svsk;
  95. int ret = 0;
  96. lock_kernel();
  97. mutex_lock(&nfs_callback_mutex);
  98. if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
  99. goto out;
  100. init_completion(&nfs_callback_info.started);
  101. init_completion(&nfs_callback_info.stopped);
  102. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
  103. ret = -ENOMEM;
  104. if (!serv)
  105. goto out_err;
  106. /* FIXME: We don't want to register this socket with the portmapper */
  107. ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
  108. if (ret < 0)
  109. goto out_destroy;
  110. if (!list_empty(&serv->sv_permsocks)) {
  111. svsk = list_entry(serv->sv_permsocks.next,
  112. struct svc_sock, sk_list);
  113. nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
  114. dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport);
  115. } else
  116. BUG();
  117. ret = svc_create_thread(nfs_callback_svc, serv);
  118. if (ret < 0)
  119. goto out_destroy;
  120. nfs_callback_info.serv = serv;
  121. wait_for_completion(&nfs_callback_info.started);
  122. out:
  123. mutex_unlock(&nfs_callback_mutex);
  124. unlock_kernel();
  125. return ret;
  126. out_destroy:
  127. svc_destroy(serv);
  128. out_err:
  129. nfs_callback_info.users--;
  130. goto out;
  131. }
  132. /*
  133. * Kill the server process if it is not already up.
  134. */
  135. void nfs_callback_down(void)
  136. {
  137. lock_kernel();
  138. mutex_lock(&nfs_callback_mutex);
  139. nfs_callback_info.users--;
  140. do {
  141. if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
  142. break;
  143. if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
  144. break;
  145. } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
  146. mutex_unlock(&nfs_callback_mutex);
  147. unlock_kernel();
  148. }
  149. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  150. {
  151. struct sockaddr_in *addr = &rqstp->rq_addr;
  152. struct nfs_client *clp;
  153. /* Don't talk to strangers */
  154. clp = nfs_find_client(addr, 4);
  155. if (clp == NULL)
  156. return SVC_DROP;
  157. dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr->sin_addr));
  158. nfs_put_client(clp);
  159. switch (rqstp->rq_authop->flavour) {
  160. case RPC_AUTH_NULL:
  161. if (rqstp->rq_proc != CB_NULL)
  162. return SVC_DENIED;
  163. break;
  164. case RPC_AUTH_UNIX:
  165. break;
  166. case RPC_AUTH_GSS:
  167. /* FIXME: RPCSEC_GSS handling? */
  168. default:
  169. return SVC_DENIED;
  170. }
  171. return SVC_OK;
  172. }
  173. /*
  174. * Define NFS4 callback program
  175. */
  176. static struct svc_version *nfs4_callback_version[] = {
  177. [1] = &nfs4_callback_version1,
  178. };
  179. static struct svc_stat nfs4_callback_stats;
  180. static struct svc_program nfs4_callback_program = {
  181. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  182. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  183. .pg_vers = nfs4_callback_version, /* version table */
  184. .pg_name = "NFSv4 callback", /* service name */
  185. .pg_class = "nfs", /* authentication class */
  186. .pg_stats = &nfs4_callback_stats,
  187. .pg_authenticate = nfs_callback_authenticate,
  188. };