blob: b1f7dc4153928af1221e563d82ab7261661f74e9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
10#include <linux/ip.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>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080016#include <linux/mutex.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020017
18#include <net/inet_sock.h>
19
Trond Myklebust4ce79712005-06-22 17:16:21 +000020#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "callback.h"
22
23#define NFSDBG_FACILITY NFSDBG_CALLBACK
24
25struct nfs_callback_data {
26 unsigned int users;
27 struct svc_serv *serv;
28 pid_t pid;
29 struct completion started;
30 struct completion stopped;
31};
32
33static struct nfs_callback_data nfs_callback_info;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080034static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct svc_program nfs4_callback_program;
36
Trond Myklebusta72b4422006-01-03 09:55:41 +010037unsigned int nfs_callback_set_tcpport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038unsigned short nfs_callback_tcpport;
David Howells7d4e2742006-08-22 20:06:07 -040039static const int nfs_set_port_min = 0;
40static const int nfs_set_port_max = 65535;
41
42static int param_set_port(const char *val, struct kernel_param *kp)
43{
44 char *endp;
45 int num = simple_strtol(val, &endp, 0);
46 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
47 return -EINVAL;
48 *((int *)kp->arg) = num;
49 return 0;
50}
51
52module_param_call(callback_tcpport, param_set_port, param_get_int,
53 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * This is the callback kernel thread.
57 */
58static void nfs_callback_svc(struct svc_rqst *rqstp)
59{
60 struct svc_serv *serv = rqstp->rq_server;
61 int err;
62
63 __module_get(THIS_MODULE);
64 lock_kernel();
65
66 nfs_callback_info.pid = current->pid;
67 daemonize("nfsv4-svc");
68 /* Process request with signals blocked, but allow SIGKILL. */
69 allow_signal(SIGKILL);
70
71 complete(&nfs_callback_info.started);
72
Trond Myklebust1dd761e2006-03-20 13:44:49 -050073 for(;;) {
74 if (signalled()) {
75 if (nfs_callback_info.users == 0)
76 break;
77 flush_signals(current);
78 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /*
80 * Listen for a request on the socket
81 */
82 err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
83 if (err == -EAGAIN || err == -EINTR)
84 continue;
85 if (err < 0) {
86 printk(KERN_WARNING
87 "%s: terminating on error %d\n",
88 __FUNCTION__, -err);
89 break;
90 }
91 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
92 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
93 svc_process(serv, rqstp);
94 }
95
Trond Myklebustf25bc342006-03-20 13:44:46 -050096 svc_exit_thread(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 nfs_callback_info.pid = 0;
98 complete(&nfs_callback_info.stopped);
99 unlock_kernel();
100 module_put_and_exit(0);
101}
102
103/*
104 * Bring up the server process if it is not already up.
105 */
106int nfs_callback_up(void)
107{
108 struct svc_serv *serv;
109 struct svc_sock *svsk;
110 int ret = 0;
111
112 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800113 mutex_lock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
115 goto out;
116 init_completion(&nfs_callback_info.started);
117 init_completion(&nfs_callback_info.stopped);
118 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
119 ret = -ENOMEM;
120 if (!serv)
121 goto out_err;
122 /* FIXME: We don't want to register this socket with the portmapper */
Trond Myklebusta72b4422006-01-03 09:55:41 +0100123 ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (ret < 0)
125 goto out_destroy;
126 if (!list_empty(&serv->sv_permsocks)) {
127 svsk = list_entry(serv->sv_permsocks.next,
128 struct svc_sock, sk_list);
129 nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
130 dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport);
131 } else
132 BUG();
133 ret = svc_create_thread(nfs_callback_svc, serv);
134 if (ret < 0)
135 goto out_destroy;
136 nfs_callback_info.serv = serv;
137 wait_for_completion(&nfs_callback_info.started);
138out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800139 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unlock_kernel();
141 return ret;
142out_destroy:
143 svc_destroy(serv);
144out_err:
145 nfs_callback_info.users--;
146 goto out;
147}
148
149/*
150 * Kill the server process if it is not already up.
151 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400152void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800155 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500156 nfs_callback_info.users--;
157 do {
158 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
159 break;
160 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
161 break;
162 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800163 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static int nfs_callback_authenticate(struct svc_rqst *rqstp)
168{
169 struct in_addr *addr = &rqstp->rq_addr.sin_addr;
170 struct nfs4_client *clp;
171
172 /* Don't talk to strangers */
173 clp = nfs4_find_client(addr);
174 if (clp == NULL)
175 return SVC_DROP;
176 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr));
177 nfs4_put_client(clp);
178 switch (rqstp->rq_authop->flavour) {
179 case RPC_AUTH_NULL:
180 if (rqstp->rq_proc != CB_NULL)
181 return SVC_DENIED;
182 break;
183 case RPC_AUTH_UNIX:
184 break;
185 case RPC_AUTH_GSS:
186 /* FIXME: RPCSEC_GSS handling? */
187 default:
188 return SVC_DENIED;
189 }
190 return SVC_OK;
191}
192
193/*
194 * Define NFS4 callback program
195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static struct svc_version *nfs4_callback_version[] = {
197 [1] = &nfs4_callback_version1,
198};
199
200static struct svc_stat nfs4_callback_stats;
201
202static struct svc_program nfs4_callback_program = {
203 .pg_prog = NFS4_CALLBACK, /* RPC service number */
204 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
205 .pg_vers = nfs4_callback_version, /* version table */
206 .pg_name = "NFSv4 callback", /* service name */
207 .pg_class = "nfs", /* authentication class */
208 .pg_stats = &nfs4_callback_stats,
209 .pg_authenticate = nfs_callback_authenticate,
210};