2 * linux/fs/nfs/callback.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback handling
9 #include <linux/config.h>
10 #include <linux/completion.h>
12 #include <linux/module.h>
13 #include <linux/smp_lock.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/sunrpc/svcsock.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/mutex.h>
19 #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
;
42 * This is the callback kernel thread.
44 static void nfs_callback_svc(struct svc_rqst
*rqstp
)
46 struct svc_serv
*serv
= rqstp
->rq_server
;
49 __module_get(THIS_MODULE
);
52 nfs_callback_info
.pid
= current
->pid
;
53 daemonize("nfsv4-svc");
54 /* Process request with signals blocked, but allow SIGKILL. */
55 allow_signal(SIGKILL
);
57 complete(&nfs_callback_info
.started
);
61 if (nfs_callback_info
.users
== 0)
63 flush_signals(current
);
66 * Listen for a request on the socket
68 err
= svc_recv(serv
, rqstp
, MAX_SCHEDULE_TIMEOUT
);
69 if (err
== -EAGAIN
|| err
== -EINTR
)
73 "%s: terminating on error %d\n",
77 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__
,
78 NIPQUAD(rqstp
->rq_addr
.sin_addr
.s_addr
));
79 svc_process(serv
, rqstp
);
82 svc_exit_thread(rqstp
);
83 nfs_callback_info
.pid
= 0;
84 complete(&nfs_callback_info
.stopped
);
86 module_put_and_exit(0);
90 * Bring up the server process if it is not already up.
92 int nfs_callback_up(void)
94 struct svc_serv
*serv
;
95 struct svc_sock
*svsk
;
99 mutex_lock(&nfs_callback_mutex
);
100 if (nfs_callback_info
.users
++ || nfs_callback_info
.pid
!= 0)
102 init_completion(&nfs_callback_info
.started
);
103 init_completion(&nfs_callback_info
.stopped
);
104 serv
= svc_create(&nfs4_callback_program
, NFS4_CALLBACK_BUFSIZE
);
108 /* FIXME: We don't want to register this socket with the portmapper */
109 ret
= svc_makesock(serv
, IPPROTO_TCP
, nfs_callback_set_tcpport
);
112 if (!list_empty(&serv
->sv_permsocks
)) {
113 svsk
= list_entry(serv
->sv_permsocks
.next
,
114 struct svc_sock
, sk_list
);
115 nfs_callback_tcpport
= ntohs(inet_sk(svsk
->sk_sk
)->sport
);
116 dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport
);
119 ret
= svc_create_thread(nfs_callback_svc
, serv
);
122 nfs_callback_info
.serv
= serv
;
123 wait_for_completion(&nfs_callback_info
.started
);
125 mutex_unlock(&nfs_callback_mutex
);
131 nfs_callback_info
.users
--;
136 * Kill the server process if it is not already up.
138 int nfs_callback_down(void)
143 mutex_lock(&nfs_callback_mutex
);
144 nfs_callback_info
.users
--;
146 if (nfs_callback_info
.users
!= 0 || nfs_callback_info
.pid
== 0)
148 if (kill_proc(nfs_callback_info
.pid
, SIGKILL
, 1) < 0)
150 } while (wait_for_completion_timeout(&nfs_callback_info
.stopped
, 5*HZ
) == 0);
151 mutex_unlock(&nfs_callback_mutex
);
156 static int nfs_callback_authenticate(struct svc_rqst
*rqstp
)
158 struct in_addr
*addr
= &rqstp
->rq_addr
.sin_addr
;
159 struct nfs4_client
*clp
;
161 /* Don't talk to strangers */
162 clp
= nfs4_find_client(addr
);
165 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__
, NIPQUAD(addr
));
166 nfs4_put_client(clp
);
167 switch (rqstp
->rq_authop
->flavour
) {
169 if (rqstp
->rq_proc
!= CB_NULL
)
175 /* FIXME: RPCSEC_GSS handling? */
183 * Define NFS4 callback program
185 extern struct svc_version nfs4_callback_version1
;
187 static struct svc_version
*nfs4_callback_version
[] = {
188 [1] = &nfs4_callback_version1
,
191 static struct svc_stat nfs4_callback_stats
;
193 static struct svc_program nfs4_callback_program
= {
194 .pg_prog
= NFS4_CALLBACK
, /* RPC service number */
195 .pg_nvers
= ARRAY_SIZE(nfs4_callback_version
), /* Number of entries */
196 .pg_vers
= nfs4_callback_version
, /* version table */
197 .pg_name
= "NFSv4 callback", /* service name */
198 .pg_class
= "nfs", /* authentication class */
199 .pg_stats
= &nfs4_callback_stats
,
200 .pg_authenticate
= nfs_callback_authenticate
,