2 * linux/fs/nfs/callback.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback handling
9 #include <linux/completion.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 <linux/mutex.h>
17 #include <linux/freezer.h>
19 #include <net/inet_sock.h>
25 #define NFSDBG_FACILITY NFSDBG_CALLBACK
27 struct nfs_callback_data {
29 struct svc_serv *serv;
31 struct completion started;
32 struct completion stopped;
35 static struct nfs_callback_data nfs_callback_info;
36 static DEFINE_MUTEX(nfs_callback_mutex);
37 static struct svc_program nfs4_callback_program;
39 unsigned int nfs_callback_set_tcpport;
40 unsigned short nfs_callback_tcpport;
41 static const int nfs_set_port_min = 0;
42 static const int nfs_set_port_max = 65535;
44 static int param_set_port(const char *val, struct kernel_param *kp)
47 int num = simple_strtol(val, &endp, 0);
48 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
50 *((int *)kp->arg) = num;
54 module_param_call(callback_tcpport, param_set_port, param_get_int,
55 &nfs_callback_set_tcpport, 0644);
58 * This is the callback kernel thread.
60 static void nfs_callback_svc(struct svc_rqst *rqstp)
64 __module_get(THIS_MODULE);
67 nfs_callback_info.pid = current->pid;
68 daemonize("nfsv4-svc");
69 /* Process request with signals blocked, but allow SIGKILL. */
70 allow_signal(SIGKILL);
73 complete(&nfs_callback_info.started);
76 char buf[RPC_MAX_ADDRBUFLEN];
79 if (nfs_callback_info.users == 0)
81 flush_signals(current);
84 * Listen for a request on the socket
86 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
87 if (err == -EAGAIN || err == -EINTR)
91 "%s: terminating on error %d\n",
95 dprintk("%s: request from %s\n", __FUNCTION__,
96 svc_print_addr(rqstp, buf, sizeof(buf)));
100 svc_exit_thread(rqstp);
101 nfs_callback_info.pid = 0;
102 complete(&nfs_callback_info.stopped);
104 module_put_and_exit(0);
108 * Bring up the server process if it is not already up.
110 int nfs_callback_up(void)
112 struct svc_serv *serv;
116 mutex_lock(&nfs_callback_mutex);
117 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
119 init_completion(&nfs_callback_info.started);
120 init_completion(&nfs_callback_info.stopped);
121 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
126 ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport,
130 nfs_callback_tcpport = ret;
131 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
133 ret = svc_create_thread(nfs_callback_svc, serv);
136 nfs_callback_info.serv = serv;
137 wait_for_completion(&nfs_callback_info.started);
139 mutex_unlock(&nfs_callback_mutex);
143 dprintk("Couldn't create callback socket or server thread; err = %d\n",
147 nfs_callback_info.users--;
152 * Kill the server process if it is not already up.
154 void nfs_callback_down(void)
157 mutex_lock(&nfs_callback_mutex);
158 nfs_callback_info.users--;
160 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
162 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
164 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
165 mutex_unlock(&nfs_callback_mutex);
169 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
171 struct sockaddr_in *addr = svc_addr_in(rqstp);
172 struct nfs_client *clp;
173 char buf[RPC_MAX_ADDRBUFLEN];
175 /* Don't talk to strangers */
176 clp = nfs_find_client(addr, 4);
180 dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
181 svc_print_addr(rqstp, buf, sizeof(buf)));
184 switch (rqstp->rq_authop->flavour) {
186 if (rqstp->rq_proc != CB_NULL)
192 /* FIXME: RPCSEC_GSS handling? */
200 * Define NFS4 callback program
202 static struct svc_version *nfs4_callback_version[] = {
203 [1] = &nfs4_callback_version1,
206 static struct svc_stat nfs4_callback_stats;
208 static struct svc_program nfs4_callback_program = {
209 .pg_prog = NFS4_CALLBACK, /* RPC service number */
210 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
211 .pg_vers = nfs4_callback_version, /* version table */
212 .pg_name = "NFSv4 callback", /* service name */
213 .pg_class = "nfs", /* authentication class */
214 .pg_stats = &nfs4_callback_stats,
215 .pg_authenticate = nfs_callback_authenticate,