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>
18 #include <net/inet_sock.h>
24 #define NFSDBG_FACILITY NFSDBG_CALLBACK
26 struct nfs_callback_data {
28 struct svc_serv *serv;
30 struct completion started;
31 struct completion stopped;
34 static struct nfs_callback_data nfs_callback_info;
35 static DEFINE_MUTEX(nfs_callback_mutex);
36 static struct svc_program nfs4_callback_program;
38 unsigned int nfs_callback_set_tcpport;
39 unsigned short nfs_callback_tcpport;
40 static const int nfs_set_port_min = 0;
41 static const int nfs_set_port_max = 65535;
43 static int param_set_port(const char *val, struct kernel_param *kp)
46 int num = simple_strtol(val, &endp, 0);
47 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
49 *((int *)kp->arg) = num;
53 module_param_call(callback_tcpport, param_set_port, param_get_int,
54 &nfs_callback_set_tcpport, 0644);
57 * This is the callback kernel thread.
59 static void nfs_callback_svc(struct svc_rqst *rqstp)
63 __module_get(THIS_MODULE);
66 nfs_callback_info.pid = current->pid;
67 daemonize("nfsv4-svc");
68 /* Process request with signals blocked, but allow SIGKILL. */
69 allow_signal(SIGKILL);
71 complete(&nfs_callback_info.started);
75 if (nfs_callback_info.users == 0)
77 flush_signals(current);
80 * Listen for a request on the socket
82 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
83 if (err == -EAGAIN || err == -EINTR)
87 "%s: terminating on error %d\n",
91 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
92 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
96 svc_exit_thread(rqstp);
97 nfs_callback_info.pid = 0;
98 complete(&nfs_callback_info.stopped);
100 module_put_and_exit(0);
104 * Bring up the server process if it is not already up.
106 int nfs_callback_up(void)
108 struct svc_serv *serv;
109 struct svc_sock *svsk;
113 mutex_lock(&nfs_callback_mutex);
114 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
116 init_completion(&nfs_callback_info.started);
117 init_completion(&nfs_callback_info.stopped);
118 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
122 /* FIXME: We don't want to register this socket with the portmapper */
123 ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
126 if (!list_empty(&serv->sv_permsocks)) {
127 svsk = list_entry(serv->sv_permsocks.next,
128 struct svc_sock, sk_list);
129 nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
130 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);
145 nfs_callback_info.users--;
150 * Kill the server process if it is not already up.
152 void nfs_callback_down(void)
155 mutex_lock(&nfs_callback_mutex);
156 nfs_callback_info.users--;
158 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
160 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
162 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
163 mutex_unlock(&nfs_callback_mutex);
167 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
169 struct sockaddr_in *addr = &rqstp->rq_addr;
170 struct nfs_client *clp;
172 /* Don't talk to strangers */
173 clp = nfs_find_client(addr, 4);
176 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr->sin_addr));
178 switch (rqstp->rq_authop->flavour) {
180 if (rqstp->rq_proc != CB_NULL)
186 /* FIXME: RPCSEC_GSS handling? */
194 * Define NFS4 callback program
196 static struct svc_version *nfs4_callback_version[] = {
197 [1] = &nfs4_callback_version1,
200 static struct svc_stat nfs4_callback_stats;
202 static struct svc_program nfs4_callback_program = {
203 .pg_prog = NFS4_CALLBACK, /* RPC service number */
204 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
205 .pg_vers = nfs4_callback_version, /* version table */
206 .pg_name = "NFSv4 callback", /* service name */
207 .pg_class = "nfs", /* authentication class */
208 .pg_stats = &nfs4_callback_stats,
209 .pg_authenticate = nfs_callback_authenticate,