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>
20 #define NFSDBG_FACILITY NFSDBG_CALLBACK
22 struct nfs_callback_data {
24 struct svc_serv *serv;
26 struct completion started;
27 struct completion stopped;
30 static struct nfs_callback_data nfs_callback_info;
31 static DECLARE_MUTEX(nfs_callback_sema);
32 static struct svc_program nfs4_callback_program;
34 unsigned short nfs_callback_tcpport;
37 * This is the callback kernel thread.
39 static void nfs_callback_svc(struct svc_rqst *rqstp)
41 struct svc_serv *serv = rqstp->rq_server;
44 __module_get(THIS_MODULE);
47 nfs_callback_info.pid = current->pid;
48 daemonize("nfsv4-svc");
49 /* Process request with signals blocked, but allow SIGKILL. */
50 allow_signal(SIGKILL);
52 complete(&nfs_callback_info.started);
54 while (nfs_callback_info.users != 0 || !signalled()) {
56 * Listen for a request on the socket
58 err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
59 if (err == -EAGAIN || err == -EINTR)
63 "%s: terminating on error %d\n",
67 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
68 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
69 svc_process(serv, rqstp);
72 nfs_callback_info.pid = 0;
73 complete(&nfs_callback_info.stopped);
75 module_put_and_exit(0);
79 * Bring up the server process if it is not already up.
81 int nfs_callback_up(void)
83 struct svc_serv *serv;
84 struct svc_sock *svsk;
88 down(&nfs_callback_sema);
89 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
91 init_completion(&nfs_callback_info.started);
92 init_completion(&nfs_callback_info.stopped);
93 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
97 /* FIXME: We don't want to register this socket with the portmapper */
98 ret = svc_makesock(serv, IPPROTO_TCP, 0);
101 if (!list_empty(&serv->sv_permsocks)) {
102 svsk = list_entry(serv->sv_permsocks.next,
103 struct svc_sock, sk_list);
104 nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
105 dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport);
108 ret = svc_create_thread(nfs_callback_svc, serv);
111 nfs_callback_info.serv = serv;
112 wait_for_completion(&nfs_callback_info.started);
114 up(&nfs_callback_sema);
120 nfs_callback_info.users--;
125 * Kill the server process if it is not already up.
127 int nfs_callback_down(void)
132 down(&nfs_callback_sema);
133 if (--nfs_callback_info.users || nfs_callback_info.pid == 0)
135 kill_proc(nfs_callback_info.pid, SIGKILL, 1);
136 wait_for_completion(&nfs_callback_info.stopped);
138 up(&nfs_callback_sema);
143 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
145 struct in_addr *addr = &rqstp->rq_addr.sin_addr;
146 struct nfs4_client *clp;
148 /* Don't talk to strangers */
149 clp = nfs4_find_client(addr);
152 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr));
153 nfs4_put_client(clp);
154 switch (rqstp->rq_authop->flavour) {
156 if (rqstp->rq_proc != CB_NULL)
162 /* FIXME: RPCSEC_GSS handling? */
170 * Define NFS4 callback program
172 extern struct svc_version nfs4_callback_version1;
174 static struct svc_version *nfs4_callback_version[] = {
175 [1] = &nfs4_callback_version1,
178 static struct svc_stat nfs4_callback_stats;
180 static struct svc_program nfs4_callback_program = {
181 .pg_prog = NFS4_CALLBACK, /* RPC service number */
182 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
183 .pg_vers = nfs4_callback_version, /* version table */
184 .pg_name = "NFSv4 callback", /* service name */
185 .pg_class = "nfs", /* authentication class */
186 .pg_stats = &nfs4_callback_stats,
187 .pg_authenticate = nfs_callback_authenticate,