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_rqst *rqst;
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;
44 * If the kernel has IPv6 support available, always listen for
45 * both AF_INET and AF_INET6 requests.
47 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
48 static const sa_family_t nfs_callback_family = AF_INET6;
50 static const sa_family_t nfs_callback_family = AF_INET;
53 static int param_set_port(const char *val, struct kernel_param *kp)
56 int num = simple_strtol(val, &endp, 0);
57 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
59 *((int *)kp->arg) = num;
63 module_param_call(callback_tcpport, param_set_port, param_get_int,
64 &nfs_callback_set_tcpport, 0644);
67 * This is the callback kernel thread.
70 nfs_callback_svc(void *vrqstp)
73 struct svc_rqst *rqstp = vrqstp;
78 * FIXME: do we really need to run this under the BKL? If so, please
79 * add a comment about what it's intended to protect.
82 while (!kthread_should_stop()) {
84 * Listen for a request on the socket
86 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
87 if (err == -EAGAIN || err == -EINTR) {
93 printk(KERN_WARNING "%s: unexpected error "
94 "from svc_recv (%d)\n", __func__, err);
97 schedule_timeout_uninterruptible(HZ);
108 * Bring up the callback thread if it is not already up.
110 int nfs_callback_up(void)
112 struct svc_serv *serv = NULL;
115 mutex_lock(&nfs_callback_mutex);
116 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
118 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
119 nfs_callback_family, NULL);
124 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
128 nfs_callback_tcpport = ret;
129 dprintk("NFS: Callback listener port = %u (af %u)\n",
130 nfs_callback_tcpport, nfs_callback_family);
132 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
133 if (IS_ERR(nfs_callback_info.rqst)) {
134 ret = PTR_ERR(nfs_callback_info.rqst);
135 nfs_callback_info.rqst = NULL;
139 svc_sock_update_bufs(serv);
141 nfs_callback_info.task = kthread_run(nfs_callback_svc,
142 nfs_callback_info.rqst,
144 if (IS_ERR(nfs_callback_info.task)) {
145 ret = PTR_ERR(nfs_callback_info.task);
146 svc_exit_thread(nfs_callback_info.rqst);
147 nfs_callback_info.rqst = NULL;
148 nfs_callback_info.task = NULL;
153 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
154 * svc_prepare_thread increments that. So we need to call svc_destroy
155 * on both success and failure so that the refcount is 1 when the
160 mutex_unlock(&nfs_callback_mutex);
163 dprintk("NFS: Couldn't create callback socket or server thread; "
165 nfs_callback_info.users--;
170 * Kill the callback thread if it's no longer being used.
172 void nfs_callback_down(void)
174 mutex_lock(&nfs_callback_mutex);
175 nfs_callback_info.users--;
176 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
177 kthread_stop(nfs_callback_info.task);
178 svc_exit_thread(nfs_callback_info.rqst);
179 nfs_callback_info.rqst = NULL;
180 nfs_callback_info.task = NULL;
182 mutex_unlock(&nfs_callback_mutex);
185 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
187 struct nfs_client *clp;
188 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
190 /* Don't talk to strangers */
191 clp = nfs_find_client(svc_addr(rqstp), 4);
195 dprintk("%s: %s NFSv4 callback!\n", __func__,
196 svc_print_addr(rqstp, buf, sizeof(buf)));
199 switch (rqstp->rq_authop->flavour) {
201 if (rqstp->rq_proc != CB_NULL)
207 /* FIXME: RPCSEC_GSS handling? */
215 * Define NFS4 callback program
217 static struct svc_version *nfs4_callback_version[] = {
218 [1] = &nfs4_callback_version1,
221 static struct svc_stat nfs4_callback_stats;
223 static struct svc_program nfs4_callback_program = {
224 .pg_prog = NFS4_CALLBACK, /* RPC service number */
225 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
226 .pg_vers = nfs4_callback_version, /* version table */
227 .pg_name = "NFSv4 callback", /* service name */
228 .pg_class = "nfs", /* authentication class */
229 .pg_stats = &nfs4_callback_stats,
230 .pg_authenticate = nfs_callback_authenticate,