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/sunrpc/svc.h>
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/mutex.h>
16 #include <linux/freezer.h>
17 #include <linux/kthread.h>
18 #include <linux/sunrpc/svcauth_gss.h>
19 #include <linux/sunrpc/bc_xprt.h>
20 #include <linux/nsproxy.h>
22 #include <net/inet_sock.h>
28 #define NFSDBG_FACILITY NFSDBG_CALLBACK
30 struct nfs_callback_data
{
32 struct svc_serv
*serv
;
33 struct svc_rqst
*rqst
;
34 struct task_struct
*task
;
37 static struct nfs_callback_data nfs_callback_info
[NFS4_MAX_MINOR_VERSION
+ 1];
38 static DEFINE_MUTEX(nfs_callback_mutex
);
39 static struct svc_program nfs4_callback_program
;
41 unsigned int nfs_callback_set_tcpport
;
42 unsigned short nfs_callback_tcpport
;
43 unsigned short nfs_callback_tcpport6
;
44 #define NFS_CALLBACK_MAXPORTNR (65535U)
46 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
53 ret
= strict_strtoul(val
, 0, &num
);
54 if (ret
== -EINVAL
|| num
> NFS_CALLBACK_MAXPORTNR
)
56 *((unsigned int *)kp
->arg
) = num
;
59 static struct kernel_param_ops param_ops_portnr
= {
60 .set
= param_set_portnr
,
61 .get
= param_get_uint
,
63 #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
65 module_param_named(callback_tcpport
, nfs_callback_set_tcpport
, portnr
, 0644);
68 * This is the NFSv4 callback kernel thread.
71 nfs4_callback_svc(void *vrqstp
)
74 struct svc_rqst
*rqstp
= vrqstp
;
78 while (!kthread_should_stop()) {
80 * Listen for a request on the socket
82 err
= svc_recv(rqstp
, MAX_SCHEDULE_TIMEOUT
);
83 if (err
== -EAGAIN
|| err
== -EINTR
) {
89 printk(KERN_WARNING
"NFS: %s: unexpected error "
90 "from svc_recv (%d)\n", __func__
, err
);
93 schedule_timeout_uninterruptible(HZ
);
103 * Prepare to bring up the NFSv4 callback service
105 static struct svc_rqst
*
106 nfs4_callback_up(struct svc_serv
*serv
, struct rpc_xprt
*xprt
)
110 ret
= svc_create_xprt(serv
, "tcp", xprt
->xprt_net
, PF_INET
,
111 nfs_callback_set_tcpport
, SVC_SOCK_ANONYMOUS
);
114 nfs_callback_tcpport
= ret
;
115 dprintk("NFS: Callback listener port = %u (af %u)\n",
116 nfs_callback_tcpport
, PF_INET
);
118 ret
= svc_create_xprt(serv
, "tcp", xprt
->xprt_net
, PF_INET6
,
119 nfs_callback_set_tcpport
, SVC_SOCK_ANONYMOUS
);
121 nfs_callback_tcpport6
= ret
;
122 dprintk("NFS: Callback listener port = %u (af %u)\n",
123 nfs_callback_tcpport6
, PF_INET6
);
124 } else if (ret
== -EAFNOSUPPORT
)
129 return svc_prepare_thread(serv
, &serv
->sv_pools
[0], NUMA_NO_NODE
);
137 #if defined(CONFIG_NFS_V4_1)
139 * The callback service for NFSv4.1 callbacks
142 nfs41_callback_svc(void *vrqstp
)
144 struct svc_rqst
*rqstp
= vrqstp
;
145 struct svc_serv
*serv
= rqstp
->rq_server
;
146 struct rpc_rqst
*req
;
152 while (!kthread_should_stop()) {
153 prepare_to_wait(&serv
->sv_cb_waitq
, &wq
, TASK_INTERRUPTIBLE
);
154 spin_lock_bh(&serv
->sv_cb_lock
);
155 if (!list_empty(&serv
->sv_cb_list
)) {
156 req
= list_first_entry(&serv
->sv_cb_list
,
157 struct rpc_rqst
, rq_bc_list
);
158 list_del(&req
->rq_bc_list
);
159 spin_unlock_bh(&serv
->sv_cb_lock
);
160 dprintk("Invoking bc_svc_process()\n");
161 error
= bc_svc_process(serv
, req
, rqstp
);
162 dprintk("bc_svc_process() returned w/ error code= %d\n",
165 spin_unlock_bh(&serv
->sv_cb_lock
);
168 finish_wait(&serv
->sv_cb_waitq
, &wq
);
174 * Bring up the NFSv4.1 callback service
176 static struct svc_rqst
*
177 nfs41_callback_up(struct svc_serv
*serv
, struct rpc_xprt
*xprt
)
179 struct svc_rqst
*rqstp
;
183 * Create an svc_sock for the back channel service that shares the
184 * fore channel connection.
185 * Returns the input port (0) and sets the svc_serv bc_xprt on success
187 ret
= svc_create_xprt(serv
, "tcp-bc", xprt
->xprt_net
, PF_INET
, 0,
190 rqstp
= ERR_PTR(ret
);
195 * Save the svc_serv in the transport so that it can
196 * be referenced when the session backchannel is initialized
198 xprt
->bc_serv
= serv
;
200 INIT_LIST_HEAD(&serv
->sv_cb_list
);
201 spin_lock_init(&serv
->sv_cb_lock
);
202 init_waitqueue_head(&serv
->sv_cb_waitq
);
203 rqstp
= svc_prepare_thread(serv
, &serv
->sv_pools
[0], NUMA_NO_NODE
);
205 svc_xprt_put(serv
->sv_bc_xprt
);
206 serv
->sv_bc_xprt
= NULL
;
209 dprintk("--> %s return %ld\n", __func__
,
210 IS_ERR(rqstp
) ? PTR_ERR(rqstp
) : 0);
214 static inline int nfs_minorversion_callback_svc_setup(u32 minorversion
,
215 struct svc_serv
*serv
, struct rpc_xprt
*xprt
,
216 struct svc_rqst
**rqstpp
, int (**callback_svc
)(void *vrqstp
))
219 *rqstpp
= nfs41_callback_up(serv
, xprt
);
220 *callback_svc
= nfs41_callback_svc
;
225 static inline void nfs_callback_bc_serv(u32 minorversion
, struct rpc_xprt
*xprt
,
226 struct nfs_callback_data
*cb_info
)
229 xprt
->bc_serv
= cb_info
->serv
;
232 static inline int nfs_minorversion_callback_svc_setup(u32 minorversion
,
233 struct svc_serv
*serv
, struct rpc_xprt
*xprt
,
234 struct svc_rqst
**rqstpp
, int (**callback_svc
)(void *vrqstp
))
239 static inline void nfs_callback_bc_serv(u32 minorversion
, struct rpc_xprt
*xprt
,
240 struct nfs_callback_data
*cb_info
)
243 #endif /* CONFIG_NFS_V4_1 */
246 * Bring up the callback thread if it is not already up.
248 int nfs_callback_up(u32 minorversion
, struct rpc_xprt
*xprt
)
250 struct svc_serv
*serv
= NULL
;
251 struct svc_rqst
*rqstp
;
252 int (*callback_svc
)(void *vrqstp
);
253 struct nfs_callback_data
*cb_info
= &nfs_callback_info
[minorversion
];
256 int minorversion_setup
;
257 struct net
*net
= current
->nsproxy
->net_ns
;
259 mutex_lock(&nfs_callback_mutex
);
260 if (cb_info
->users
++ || cb_info
->task
!= NULL
) {
261 nfs_callback_bc_serv(minorversion
, xprt
, cb_info
);
264 serv
= svc_create(&nfs4_callback_program
, NFS4_CALLBACK_BUFSIZE
, NULL
);
270 ret
= svc_bind(serv
, net
);
272 printk(KERN_WARNING
"NFS: bind callback service failed\n");
276 minorversion_setup
= nfs_minorversion_callback_svc_setup(minorversion
,
277 serv
, xprt
, &rqstp
, &callback_svc
);
278 if (!minorversion_setup
) {
279 /* v4.0 callback setup */
280 rqstp
= nfs4_callback_up(serv
, xprt
);
281 callback_svc
= nfs4_callback_svc
;
285 ret
= PTR_ERR(rqstp
);
289 svc_sock_update_bufs(serv
);
291 sprintf(svc_name
, "nfsv4.%u-svc", minorversion
);
292 cb_info
->serv
= serv
;
293 cb_info
->rqst
= rqstp
;
294 cb_info
->task
= kthread_run(callback_svc
, cb_info
->rqst
, svc_name
);
295 if (IS_ERR(cb_info
->task
)) {
296 ret
= PTR_ERR(cb_info
->task
);
297 svc_exit_thread(cb_info
->rqst
);
298 cb_info
->rqst
= NULL
;
299 cb_info
->task
= NULL
;
304 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
305 * svc_prepare_thread increments that. So we need to call svc_destroy
306 * on both success and failure so that the refcount is 1 when the
311 mutex_unlock(&nfs_callback_mutex
);
314 dprintk("NFS: Couldn't create callback socket or server thread; "
318 svc_shutdown_net(serv
, net
);
323 * Kill the callback thread if it's no longer being used.
325 void nfs_callback_down(int minorversion
)
327 struct nfs_callback_data
*cb_info
= &nfs_callback_info
[minorversion
];
329 mutex_lock(&nfs_callback_mutex
);
331 if (cb_info
->users
== 0 && cb_info
->task
!= NULL
) {
332 kthread_stop(cb_info
->task
);
333 svc_shutdown_net(cb_info
->serv
, current
->nsproxy
->net_ns
);
334 svc_exit_thread(cb_info
->rqst
);
335 cb_info
->serv
= NULL
;
336 cb_info
->rqst
= NULL
;
337 cb_info
->task
= NULL
;
339 mutex_unlock(&nfs_callback_mutex
);
342 /* Boolean check of RPC_AUTH_GSS principal */
344 check_gss_callback_principal(struct nfs_client
*clp
, struct svc_rqst
*rqstp
)
346 char *p
= rqstp
->rq_cred
.cr_principal
;
348 if (rqstp
->rq_authop
->flavour
!= RPC_AUTH_GSS
)
351 /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
352 if (clp
->cl_minorversion
!= 0)
355 * It might just be a normal user principal, in which case
356 * userspace won't bother to tell us the name at all.
361 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
363 if (memcmp(p
, "nfs@", 4) != 0)
366 if (strcmp(p
, clp
->cl_hostname
) != 0)
372 * pg_authenticate method for nfsv4 callback threads.
374 * The authflavor has been negotiated, so an incorrect flavor is a server
375 * bug. Drop packets with incorrect authflavor.
377 * All other checking done after NFS decoding where the nfs_client can be
378 * found in nfs4_callback_compound
380 static int nfs_callback_authenticate(struct svc_rqst
*rqstp
)
382 switch (rqstp
->rq_authop
->flavour
) {
384 if (rqstp
->rq_proc
!= CB_NULL
)
388 /* No RPC_AUTH_GSS support yet in NFSv4.1 */
389 if (svc_is_backchannel(rqstp
))
396 * Define NFS4 callback program
398 static struct svc_version
*nfs4_callback_version
[] = {
399 [1] = &nfs4_callback_version1
,
400 [4] = &nfs4_callback_version4
,
403 static struct svc_stat nfs4_callback_stats
;
405 static struct svc_program nfs4_callback_program
= {
406 .pg_prog
= NFS4_CALLBACK
, /* RPC service number */
407 .pg_nvers
= ARRAY_SIZE(nfs4_callback_version
), /* Number of entries */
408 .pg_vers
= nfs4_callback_version
, /* version table */
409 .pg_name
= "NFSv4 callback", /* service name */
410 .pg_class
= "nfs", /* authentication class */
411 .pg_stats
= &nfs4_callback_stats
,
412 .pg_authenticate
= nfs_callback_authenticate
,