blob: bd185a572a23aeb0d015b701d0786dab359f260a [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>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070017#include <linux/freezer.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"
David Howells24c8dbb2006-08-22 20:06:10 -040023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define NFSDBG_FACILITY NFSDBG_CALLBACK
26
27struct nfs_callback_data {
28 unsigned int users;
29 struct svc_serv *serv;
30 pid_t pid;
31 struct completion started;
32 struct completion stopped;
33};
34
35static struct nfs_callback_data nfs_callback_info;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080036static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct svc_program nfs4_callback_program;
38
Trond Myklebusta72b4422006-01-03 09:55:41 +010039unsigned int nfs_callback_set_tcpport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040unsigned short nfs_callback_tcpport;
David Howells7d4e2742006-08-22 20:06:07 -040041static const int nfs_set_port_min = 0;
42static const int nfs_set_port_max = 65535;
43
44static int param_set_port(const char *val, struct kernel_param *kp)
45{
46 char *endp;
47 int num = simple_strtol(val, &endp, 0);
48 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
49 return -EINVAL;
50 *((int *)kp->arg) = num;
51 return 0;
52}
53
54module_param_call(callback_tcpport, param_set_port, param_get_int,
55 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * This is the callback kernel thread.
59 */
60static void nfs_callback_svc(struct svc_rqst *rqstp)
61{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int err;
63
64 __module_get(THIS_MODULE);
65 lock_kernel();
66
67 nfs_callback_info.pid = current->pid;
68 daemonize("nfsv4-svc");
69 /* Process request with signals blocked, but allow SIGKILL. */
70 allow_signal(SIGKILL);
Rafael J. Wysocki83144182007-07-17 04:03:35 -070071 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 complete(&nfs_callback_info.started);
74
Trond Myklebust1dd761e2006-03-20 13:44:49 -050075 for(;;) {
76 if (signalled()) {
77 if (nfs_callback_info.users == 0)
78 break;
79 flush_signals(current);
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /*
82 * Listen for a request on the socket
83 */
NeilBrown6fb2b472006-10-02 02:17:50 -070084 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (err == -EAGAIN || err == -EINTR)
86 continue;
87 if (err < 0) {
88 printk(KERN_WARNING
89 "%s: terminating on error %d\n",
90 __FUNCTION__, -err);
91 break;
92 }
NeilBrown6fb2b472006-10-02 02:17:50 -070093 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int ret = 0;
110
111 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800112 mutex_lock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
114 goto out;
115 init_completion(&nfs_callback_info.started);
116 init_completion(&nfs_callback_info.stopped);
NeilBrownbc591cc2006-10-02 02:17:44 -0700117 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 ret = -ENOMEM;
119 if (!serv)
120 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800121
Tom Tuckerd7c9f1e2007-12-30 21:07:44 -0600122 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
123 SVC_SOCK_ANONYMOUS);
Chuck Lever482fb942007-02-12 00:53:29 -0800124 if (ret <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto out_destroy;
Chuck Lever482fb942007-02-12 00:53:29 -0800126 nfs_callback_tcpport = ret;
127 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 ret = svc_create_thread(nfs_callback_svc, serv);
130 if (ret < 0)
131 goto out_destroy;
132 nfs_callback_info.serv = serv;
133 wait_for_completion(&nfs_callback_info.started);
134out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800135 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unlock_kernel();
137 return ret;
138out_destroy:
Chuck Lever482fb942007-02-12 00:53:29 -0800139 dprintk("Couldn't create callback socket or server thread; err = %d\n",
140 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 svc_destroy(serv);
142out_err:
143 nfs_callback_info.users--;
144 goto out;
145}
146
147/*
148 * Kill the server process if it is not already up.
149 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400150void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 lock_kernel();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800153 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500154 nfs_callback_info.users--;
155 do {
156 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
157 break;
158 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
159 break;
160 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800161 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static int nfs_callback_authenticate(struct svc_rqst *rqstp)
166{
David Howellsadfa6f92006-08-22 20:06:08 -0400167 struct nfs_client *clp;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800168 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Don't talk to strangers */
Chuck Leverff052642007-12-10 14:58:44 -0500171 clp = nfs_find_client(svc_addr(rqstp), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (clp == NULL)
173 return SVC_DROP;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800174
175 dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
176 svc_print_addr(rqstp, buf, sizeof(buf)));
David Howells24c8dbb2006-08-22 20:06:10 -0400177 nfs_put_client(clp);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 switch (rqstp->rq_authop->flavour) {
180 case RPC_AUTH_NULL:
181 if (rqstp->rq_proc != CB_NULL)
182 return SVC_DENIED;
183 break;
184 case RPC_AUTH_UNIX:
185 break;
186 case RPC_AUTH_GSS:
187 /* FIXME: RPCSEC_GSS handling? */
188 default:
189 return SVC_DENIED;
190 }
191 return SVC_OK;
192}
193
194/*
195 * Define NFS4 callback program
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static struct svc_version *nfs4_callback_version[] = {
198 [1] = &nfs4_callback_version1,
199};
200
201static struct svc_stat nfs4_callback_stats;
202
203static struct svc_program nfs4_callback_program = {
204 .pg_prog = NFS4_CALLBACK, /* RPC service number */
205 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
206 .pg_vers = nfs4_callback_version, /* version table */
207 .pg_name = "NFSv4 callback", /* service name */
208 .pg_class = "nfs", /* authentication class */
209 .pg_stats = &nfs4_callback_stats,
210 .pg_authenticate = nfs_callback_authenticate,
211};