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>
18 #include <linux/kthread.h>
20 #include <net/inet_sock.h>
26 #define NFSDBG_FACILITY NFSDBG_CALLBACK
28 struct nfs_callback_data {
30 struct svc_serv *serv;
31 struct task_struct *task;
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.
60 nfs_callback_svc(void *vrqstp)
63 struct svc_rqst *rqstp = vrqstp;
68 * FIXME: do we really need to run this under the BKL? If so, please
69 * add a comment about what it's intended to protect.
72 while (!kthread_should_stop()) {
74 * Listen for a request on the socket
76 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
77 if (err == -EAGAIN || err == -EINTR)
81 "%s: terminating on error %d\n",
88 nfs_callback_info.task = NULL;
89 svc_exit_thread(rqstp);
94 * Bring up the server process if it is not already up.
96 int nfs_callback_up(void)
98 struct svc_serv *serv = NULL;
99 struct svc_rqst *rqstp;
103 mutex_lock(&nfs_callback_mutex);
104 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
106 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
111 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
115 nfs_callback_tcpport = ret;
116 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
118 rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
120 ret = PTR_ERR(rqstp);
124 svc_sock_update_bufs(serv);
125 nfs_callback_info.serv = serv;
127 nfs_callback_info.task = kthread_run(nfs_callback_svc, rqstp,
129 if (IS_ERR(nfs_callback_info.task)) {
130 ret = PTR_ERR(nfs_callback_info.task);
131 nfs_callback_info.serv = NULL;
132 nfs_callback_info.task = NULL;
133 svc_exit_thread(rqstp);
138 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
139 * svc_prepare_thread increments that. So we need to call svc_destroy
140 * on both success and failure so that the refcount is 1 when the
145 mutex_unlock(&nfs_callback_mutex);
149 dprintk("Couldn't create callback socket or server thread; err = %d\n",
151 nfs_callback_info.users--;
156 * Kill the server process if it is not already down.
158 void nfs_callback_down(void)
161 mutex_lock(&nfs_callback_mutex);
162 nfs_callback_info.users--;
163 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL)
164 kthread_stop(nfs_callback_info.task);
165 mutex_unlock(&nfs_callback_mutex);
169 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
171 struct nfs_client *clp;
172 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
174 /* Don't talk to strangers */
175 clp = nfs_find_client(svc_addr(rqstp), 4);
179 dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
180 svc_print_addr(rqstp, buf, sizeof(buf)));
183 switch (rqstp->rq_authop->flavour) {
185 if (rqstp->rq_proc != CB_NULL)
191 /* FIXME: RPCSEC_GSS handling? */
199 * Define NFS4 callback program
201 static struct svc_version *nfs4_callback_version[] = {
202 [1] = &nfs4_callback_version1,
205 static struct svc_stat nfs4_callback_stats;
207 static struct svc_program nfs4_callback_program = {
208 .pg_prog = NFS4_CALLBACK, /* RPC service number */
209 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
210 .pg_vers = nfs4_callback_version, /* version table */
211 .pg_name = "NFSv4 callback", /* service name */
212 .pg_class = "nfs", /* authentication class */
213 .pg_stats = &nfs4_callback_stats,
214 .pg_authenticate = nfs_callback_authenticate,