blob: d53f8c6a9ecb5483fe0467404d92e895acba5a75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/callback.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback handling
7 */
8
9#include <linux/config.h>
10#include <linux/completion.h>
11#include <linux/ip.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>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080017#include <linux/mutex.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020018
19#include <net/inet_sock.h>
20
Trond Myklebust4ce79712005-06-22 17:16:21 +000021#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "callback.h"
23
24#define NFSDBG_FACILITY NFSDBG_CALLBACK
25
26struct nfs_callback_data {
27 unsigned int users;
28 struct svc_serv *serv;
29 pid_t pid;
30 struct completion started;
31 struct completion stopped;
32};
33
34static struct nfs_callback_data nfs_callback_info;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080035static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static struct svc_program nfs4_callback_program;
37
Trond Myklebusta72b4422006-01-03 09:55:41 +010038unsigned int nfs_callback_set_tcpport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039unsigned short nfs_callback_tcpport;
40
41/*
42 * This is the callback kernel thread.
43 */
44static void nfs_callback_svc(struct svc_rqst *rqstp)
45{
46 struct svc_serv *serv = rqstp->rq_server;
47 int err;
48
49 __module_get(THIS_MODULE);
50 lock_kernel();
51
52 nfs_callback_info.pid = current->pid;
53 daemonize("nfsv4-svc");
54 /* Process request with signals blocked, but allow SIGKILL. */
55 allow_signal(SIGKILL);
56
57 complete(&nfs_callback_info.started);
58
Trond Myklebust1dd761e2006-03-20 13:44:49 -050059 for(;;) {
60 if (signalled()) {
61 if (nfs_callback_info.users == 0)
62 break;
63 flush_signals(current);
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /*
66 * Listen for a request on the socket
67 */
68 err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
69 if (err == -EAGAIN || err == -EINTR)
70 continue;
71 if (err < 0) {
72 printk(KERN_WARNING
73 "%s: terminating on error %d\n",
74 __FUNCTION__, -err);
75 break;
76 }
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);
80 }
81
Trond Myklebustf25bc342006-03-20 13:44:46 -050082 svc_exit_thread(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 nfs_callback_info.pid = 0;
84 complete(&nfs_callback_info.stopped);
85 unlock_kernel();
86 module_put_and_exit(0);
87}
88
89/*
90 * Bring up the server process if it is not already up.
91 */
92int nfs_callback_up(void)
93{
94 struct svc_serv *serv;
95 struct svc_sock *svsk;
96 int ret = 0;
97
98 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -080099 mutex_lock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
101 goto out;
102 init_completion(&nfs_callback_info.started);
103 init_completion(&nfs_callback_info.stopped);
104 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
105 ret = -ENOMEM;
106 if (!serv)
107 goto out_err;
108 /* FIXME: We don't want to register this socket with the portmapper */
Trond Myklebusta72b4422006-01-03 09:55:41 +0100109 ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (ret < 0)
111 goto out_destroy;
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);
117 } else
118 BUG();
119 ret = svc_create_thread(nfs_callback_svc, serv);
120 if (ret < 0)
121 goto out_destroy;
122 nfs_callback_info.serv = serv;
123 wait_for_completion(&nfs_callback_info.started);
124out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800125 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unlock_kernel();
127 return ret;
128out_destroy:
129 svc_destroy(serv);
130out_err:
131 nfs_callback_info.users--;
132 goto out;
133}
134
135/*
136 * Kill the server process if it is not already up.
137 */
138int nfs_callback_down(void)
139{
140 int ret = 0;
141
142 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800143 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500144 nfs_callback_info.users--;
145 do {
146 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
147 break;
148 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
149 break;
150 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800151 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unlock_kernel();
153 return ret;
154}
155
156static int nfs_callback_authenticate(struct svc_rqst *rqstp)
157{
158 struct in_addr *addr = &rqstp->rq_addr.sin_addr;
159 struct nfs4_client *clp;
160
161 /* Don't talk to strangers */
162 clp = nfs4_find_client(addr);
163 if (clp == NULL)
164 return SVC_DROP;
165 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr));
166 nfs4_put_client(clp);
167 switch (rqstp->rq_authop->flavour) {
168 case RPC_AUTH_NULL:
169 if (rqstp->rq_proc != CB_NULL)
170 return SVC_DENIED;
171 break;
172 case RPC_AUTH_UNIX:
173 break;
174 case RPC_AUTH_GSS:
175 /* FIXME: RPCSEC_GSS handling? */
176 default:
177 return SVC_DENIED;
178 }
179 return SVC_OK;
180}
181
182/*
183 * Define NFS4 callback program
184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static struct svc_version *nfs4_callback_version[] = {
186 [1] = &nfs4_callback_version1,
187};
188
189static struct svc_stat nfs4_callback_stats;
190
191static struct svc_program nfs4_callback_program = {
192 .pg_prog = NFS4_CALLBACK, /* RPC service number */
193 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
194 .pg_vers = nfs4_callback_version, /* version table */
195 .pg_name = "NFSv4 callback", /* service name */
196 .pg_class = "nfs", /* authentication class */
197 .pg_stats = &nfs4_callback_stats,
198 .pg_authenticate = nfs_callback_authenticate,
199};