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>
18 #include <net/inet_sock.h>
23 #define NFSDBG_FACILITY NFSDBG_CALLBACK
25 struct nfs_callback_data
{
27 struct svc_serv
*serv
;
29 struct completion started
;
30 struct completion stopped
;
33 static struct nfs_callback_data nfs_callback_info
;
34 static DEFINE_MUTEX(nfs_callback_mutex
);
35 static struct svc_program nfs4_callback_program
;
37 unsigned int nfs_callback_set_tcpport
;
38 unsigned short nfs_callback_tcpport
;
41 * This is the callback kernel thread.
43 static void nfs_callback_svc(struct svc_rqst
*rqstp
)
45 struct svc_serv
*serv
= rqstp
->rq_server
;
48 __module_get(THIS_MODULE
);
51 nfs_callback_info
.pid
= current
->pid
;
52 daemonize("nfsv4-svc");
53 /* Process request with signals blocked, but allow SIGKILL. */
54 allow_signal(SIGKILL
);
56 complete(&nfs_callback_info
.started
);
60 if (nfs_callback_info
.users
== 0)
62 flush_signals(current
);
65 * Listen for a request on the socket
67 err
= svc_recv(serv
, rqstp
, MAX_SCHEDULE_TIMEOUT
);
68 if (err
== -EAGAIN
|| err
== -EINTR
)
72 "%s: terminating on error %d\n",
76 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__
,
77 NIPQUAD(rqstp
->rq_addr
.sin_addr
.s_addr
));
78 svc_process(serv
, rqstp
);
81 svc_exit_thread(rqstp
);
82 nfs_callback_info
.pid
= 0;
83 complete(&nfs_callback_info
.stopped
);
85 module_put_and_exit(0);
89 * Bring up the server process if it is not already up.
91 int nfs_callback_up(void)
93 struct svc_serv
*serv
;
94 struct svc_sock
*svsk
;
98 mutex_lock(&nfs_callback_mutex
);
99 if (nfs_callback_info
.users
++ || nfs_callback_info
.pid
!= 0)
101 init_completion(&nfs_callback_info
.started
);
102 init_completion(&nfs_callback_info
.stopped
);
103 serv
= svc_create(&nfs4_callback_program
, NFS4_CALLBACK_BUFSIZE
);
107 /* FIXME: We don't want to register this socket with the portmapper */
108 ret
= svc_makesock(serv
, IPPROTO_TCP
, nfs_callback_set_tcpport
);
111 if (!list_empty(&serv
->sv_permsocks
)) {
112 svsk
= list_entry(serv
->sv_permsocks
.next
,
113 struct svc_sock
, sk_list
);
114 nfs_callback_tcpport
= ntohs(inet_sk(svsk
->sk_sk
)->sport
);
115 dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport
);
118 ret
= svc_create_thread(nfs_callback_svc
, serv
);
121 nfs_callback_info
.serv
= serv
;
122 wait_for_completion(&nfs_callback_info
.started
);
124 mutex_unlock(&nfs_callback_mutex
);
130 nfs_callback_info
.users
--;
135 * Kill the server process if it is not already up.
137 int nfs_callback_down(void)
142 mutex_lock(&nfs_callback_mutex
);
143 nfs_callback_info
.users
--;
145 if (nfs_callback_info
.users
!= 0 || nfs_callback_info
.pid
== 0)
147 if (kill_proc(nfs_callback_info
.pid
, SIGKILL
, 1) < 0)
149 } while (wait_for_completion_timeout(&nfs_callback_info
.stopped
, 5*HZ
) == 0);
150 mutex_unlock(&nfs_callback_mutex
);
155 static int nfs_callback_authenticate(struct svc_rqst
*rqstp
)
157 struct in_addr
*addr
= &rqstp
->rq_addr
.sin_addr
;
158 struct nfs4_client
*clp
;
160 /* Don't talk to strangers */
161 clp
= nfs4_find_client(addr
);
164 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__
, NIPQUAD(addr
));
165 nfs4_put_client(clp
);
166 switch (rqstp
->rq_authop
->flavour
) {
168 if (rqstp
->rq_proc
!= CB_NULL
)
174 /* FIXME: RPCSEC_GSS handling? */
182 * Define NFS4 callback program
184 static struct svc_version
*nfs4_callback_version
[] = {
185 [1] = &nfs4_callback_version1
,
188 static struct svc_stat nfs4_callback_stats
;
190 static struct svc_program nfs4_callback_program
= {
191 .pg_prog
= NFS4_CALLBACK
, /* RPC service number */
192 .pg_nvers
= ARRAY_SIZE(nfs4_callback_version
), /* Number of entries */
193 .pg_vers
= nfs4_callback_version
, /* version table */
194 .pg_name
= "NFSv4 callback", /* service name */
195 .pg_class
= "nfs", /* authentication class */
196 .pg_stats
= &nfs4_callback_stats
,
197 .pg_authenticate
= nfs_callback_authenticate
,