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>
19 #include <linux/sunrpc/svcauth_gss.h>
21 #include <net/inet_sock.h>
27 #define NFSDBG_FACILITY NFSDBG_CALLBACK
29 struct nfs_callback_data
{
31 struct svc_rqst
*rqst
;
32 struct task_struct
*task
;
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 unsigned short nfs_callback_tcpport6
;
42 static const int nfs_set_port_min
= 0;
43 static const int nfs_set_port_max
= 65535;
45 static int param_set_port(const char *val
, struct kernel_param
*kp
)
48 int num
= simple_strtol(val
, &endp
, 0);
49 if (endp
== val
|| *endp
|| num
< nfs_set_port_min
|| num
> nfs_set_port_max
)
51 *((int *)kp
->arg
) = num
;
55 module_param_call(callback_tcpport
, param_set_port
, param_get_int
,
56 &nfs_callback_set_tcpport
, 0644);
59 * This is the callback kernel thread.
62 nfs_callback_svc(void *vrqstp
)
65 struct svc_rqst
*rqstp
= vrqstp
;
70 * FIXME: do we really need to run this under the BKL? If so, please
71 * add a comment about what it's intended to protect.
74 while (!kthread_should_stop()) {
76 * Listen for a request on the socket
78 err
= svc_recv(rqstp
, MAX_SCHEDULE_TIMEOUT
);
79 if (err
== -EAGAIN
|| err
== -EINTR
) {
85 printk(KERN_WARNING
"%s: unexpected error "
86 "from svc_recv (%d)\n", __func__
, err
);
89 schedule_timeout_uninterruptible(HZ
);
100 * Bring up the callback thread if it is not already up.
102 int nfs_callback_up(void)
104 struct svc_serv
*serv
= NULL
;
107 mutex_lock(&nfs_callback_mutex
);
108 if (nfs_callback_info
.users
++ || nfs_callback_info
.task
!= NULL
)
110 serv
= svc_create(&nfs4_callback_program
, NFS4_CALLBACK_BUFSIZE
, NULL
);
115 ret
= svc_create_xprt(serv
, "tcp", PF_INET
,
116 nfs_callback_set_tcpport
, SVC_SOCK_ANONYMOUS
);
119 nfs_callback_tcpport
= ret
;
120 dprintk("NFS: Callback listener port = %u (af %u)\n",
121 nfs_callback_tcpport
, PF_INET
);
123 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
124 ret
= svc_create_xprt(serv
, "tcp", PF_INET6
,
125 nfs_callback_set_tcpport
, SVC_SOCK_ANONYMOUS
);
127 nfs_callback_tcpport6
= ret
;
128 dprintk("NFS: Callback listener port = %u (af %u)\n",
129 nfs_callback_tcpport6
, PF_INET6
);
130 } else if (ret
== -EAFNOSUPPORT
)
134 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
136 nfs_callback_info
.rqst
= svc_prepare_thread(serv
, &serv
->sv_pools
[0]);
137 if (IS_ERR(nfs_callback_info
.rqst
)) {
138 ret
= PTR_ERR(nfs_callback_info
.rqst
);
139 nfs_callback_info
.rqst
= NULL
;
143 svc_sock_update_bufs(serv
);
145 nfs_callback_info
.task
= kthread_run(nfs_callback_svc
,
146 nfs_callback_info
.rqst
,
148 if (IS_ERR(nfs_callback_info
.task
)) {
149 ret
= PTR_ERR(nfs_callback_info
.task
);
150 svc_exit_thread(nfs_callback_info
.rqst
);
151 nfs_callback_info
.rqst
= NULL
;
152 nfs_callback_info
.task
= NULL
;
157 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
158 * svc_prepare_thread increments that. So we need to call svc_destroy
159 * on both success and failure so that the refcount is 1 when the
164 mutex_unlock(&nfs_callback_mutex
);
167 dprintk("NFS: Couldn't create callback socket or server thread; "
169 nfs_callback_info
.users
--;
174 * Kill the callback thread if it's no longer being used.
176 void nfs_callback_down(void)
178 mutex_lock(&nfs_callback_mutex
);
179 nfs_callback_info
.users
--;
180 if (nfs_callback_info
.users
== 0 && nfs_callback_info
.task
!= NULL
) {
181 kthread_stop(nfs_callback_info
.task
);
182 svc_exit_thread(nfs_callback_info
.rqst
);
183 nfs_callback_info
.rqst
= NULL
;
184 nfs_callback_info
.task
= NULL
;
186 mutex_unlock(&nfs_callback_mutex
);
189 static int check_gss_callback_principal(struct nfs_client
*clp
,
190 struct svc_rqst
*rqstp
)
192 struct rpc_clnt
*r
= clp
->cl_rpcclient
;
193 char *p
= svc_gss_principal(rqstp
);
196 * It might just be a normal user principal, in which case
197 * userspace won't bother to tell us the name at all.
202 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
204 if (memcmp(p
, "nfs@", 4) != 0)
207 if (strcmp(p
, r
->cl_server
) != 0)
212 static int nfs_callback_authenticate(struct svc_rqst
*rqstp
)
214 struct nfs_client
*clp
;
215 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
218 /* Don't talk to strangers */
219 clp
= nfs_find_client(svc_addr(rqstp
), 4);
223 dprintk("%s: %s NFSv4 callback!\n", __func__
,
224 svc_print_addr(rqstp
, buf
, sizeof(buf
)));
226 switch (rqstp
->rq_authop
->flavour
) {
228 if (rqstp
->rq_proc
!= CB_NULL
)
234 ret
= check_gss_callback_principal(clp
, rqstp
);
244 * Define NFS4 callback program
246 static struct svc_version
*nfs4_callback_version
[] = {
247 [1] = &nfs4_callback_version1
,
250 static struct svc_stat nfs4_callback_stats
;
252 static struct svc_program nfs4_callback_program
= {
253 .pg_prog
= NFS4_CALLBACK
, /* RPC service number */
254 .pg_nvers
= ARRAY_SIZE(nfs4_callback_version
), /* Number of entries */
255 .pg_vers
= nfs4_callback_version
, /* version table */
256 .pg_name
= "NFSv4 callback", /* service name */
257 .pg_class
= "nfs", /* authentication class */
258 .pg_stats
= &nfs4_callback_stats
,
259 .pg_authenticate
= nfs_callback_authenticate
,