callback.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * linux/fs/nfs/callback.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback handling
  7. */
  8. #include <linux/config.h>
  9. #include <linux/completion.h>
  10. #include <linux/ip.h>
  11. #include <linux/module.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/sunrpc/svcsock.h>
  15. #include <linux/nfs_fs.h>
  16. #include <net/inet_sock.h>
  17. #include "nfs4_fs.h"
  18. #include "callback.h"
  19. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  20. struct nfs_callback_data {
  21. unsigned int users;
  22. struct svc_serv *serv;
  23. pid_t pid;
  24. struct completion started;
  25. struct completion stopped;
  26. };
  27. static struct nfs_callback_data nfs_callback_info;
  28. static DECLARE_MUTEX(nfs_callback_sema);
  29. static struct svc_program nfs4_callback_program;
  30. unsigned int nfs_callback_set_tcpport;
  31. unsigned short nfs_callback_tcpport;
  32. /*
  33. * This is the callback kernel thread.
  34. */
  35. static void nfs_callback_svc(struct svc_rqst *rqstp)
  36. {
  37. struct svc_serv *serv = rqstp->rq_server;
  38. int err;
  39. __module_get(THIS_MODULE);
  40. lock_kernel();
  41. nfs_callback_info.pid = current->pid;
  42. daemonize("nfsv4-svc");
  43. /* Process request with signals blocked, but allow SIGKILL. */
  44. allow_signal(SIGKILL);
  45. complete(&nfs_callback_info.started);
  46. while (nfs_callback_info.users != 0 || !signalled()) {
  47. /*
  48. * Listen for a request on the socket
  49. */
  50. err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
  51. if (err == -EAGAIN || err == -EINTR)
  52. continue;
  53. if (err < 0) {
  54. printk(KERN_WARNING
  55. "%s: terminating on error %d\n",
  56. __FUNCTION__, -err);
  57. break;
  58. }
  59. dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
  60. NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
  61. svc_process(serv, rqstp);
  62. }
  63. nfs_callback_info.pid = 0;
  64. complete(&nfs_callback_info.stopped);
  65. unlock_kernel();
  66. module_put_and_exit(0);
  67. }
  68. /*
  69. * Bring up the server process if it is not already up.
  70. */
  71. int nfs_callback_up(void)
  72. {
  73. struct svc_serv *serv;
  74. struct svc_sock *svsk;
  75. int ret = 0;
  76. lock_kernel();
  77. down(&nfs_callback_sema);
  78. if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
  79. goto out;
  80. init_completion(&nfs_callback_info.started);
  81. init_completion(&nfs_callback_info.stopped);
  82. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
  83. ret = -ENOMEM;
  84. if (!serv)
  85. goto out_err;
  86. /* FIXME: We don't want to register this socket with the portmapper */
  87. ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
  88. if (ret < 0)
  89. goto out_destroy;
  90. if (!list_empty(&serv->sv_permsocks)) {
  91. svsk = list_entry(serv->sv_permsocks.next,
  92. struct svc_sock, sk_list);
  93. nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
  94. dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport);
  95. } else
  96. BUG();
  97. ret = svc_create_thread(nfs_callback_svc, serv);
  98. if (ret < 0)
  99. goto out_destroy;
  100. nfs_callback_info.serv = serv;
  101. wait_for_completion(&nfs_callback_info.started);
  102. out:
  103. up(&nfs_callback_sema);
  104. unlock_kernel();
  105. return ret;
  106. out_destroy:
  107. svc_destroy(serv);
  108. out_err:
  109. nfs_callback_info.users--;
  110. goto out;
  111. }
  112. /*
  113. * Kill the server process if it is not already up.
  114. */
  115. int nfs_callback_down(void)
  116. {
  117. int ret = 0;
  118. lock_kernel();
  119. down(&nfs_callback_sema);
  120. if (--nfs_callback_info.users || nfs_callback_info.pid == 0)
  121. goto out;
  122. kill_proc(nfs_callback_info.pid, SIGKILL, 1);
  123. wait_for_completion(&nfs_callback_info.stopped);
  124. out:
  125. up(&nfs_callback_sema);
  126. unlock_kernel();
  127. return ret;
  128. }
  129. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  130. {
  131. struct in_addr *addr = &rqstp->rq_addr.sin_addr;
  132. struct nfs4_client *clp;
  133. /* Don't talk to strangers */
  134. clp = nfs4_find_client(addr);
  135. if (clp == NULL)
  136. return SVC_DROP;
  137. dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr));
  138. nfs4_put_client(clp);
  139. switch (rqstp->rq_authop->flavour) {
  140. case RPC_AUTH_NULL:
  141. if (rqstp->rq_proc != CB_NULL)
  142. return SVC_DENIED;
  143. break;
  144. case RPC_AUTH_UNIX:
  145. break;
  146. case RPC_AUTH_GSS:
  147. /* FIXME: RPCSEC_GSS handling? */
  148. default:
  149. return SVC_DENIED;
  150. }
  151. return SVC_OK;
  152. }
  153. /*
  154. * Define NFS4 callback program
  155. */
  156. extern struct svc_version nfs4_callback_version1;
  157. static struct svc_version *nfs4_callback_version[] = {
  158. [1] = &nfs4_callback_version1,
  159. };
  160. static struct svc_stat nfs4_callback_stats;
  161. static struct svc_program nfs4_callback_program = {
  162. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  163. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  164. .pg_vers = nfs4_callback_version, /* version table */
  165. .pg_name = "NFSv4 callback", /* service name */
  166. .pg_class = "nfs", /* authentication class */
  167. .pg_stats = &nfs4_callback_stats,
  168. .pg_authenticate = nfs_callback_authenticate,
  169. };