xref: /openbmc/linux/fs/nfs/callback.c (revision a1e58bbd)
1 /*
2  * linux/fs/nfs/callback.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback handling
7  */
8 
9 #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>
16 #include <linux/mutex.h>
17 #include <linux/freezer.h>
18 
19 #include <net/inet_sock.h>
20 
21 #include "nfs4_fs.h"
22 #include "callback.h"
23 #include "internal.h"
24 
25 #define NFSDBG_FACILITY NFSDBG_CALLBACK
26 
27 struct 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 
35 static struct nfs_callback_data nfs_callback_info;
36 static DEFINE_MUTEX(nfs_callback_mutex);
37 static struct svc_program nfs4_callback_program;
38 
39 unsigned int nfs_callback_set_tcpport;
40 unsigned short nfs_callback_tcpport;
41 static const int nfs_set_port_min = 0;
42 static const int nfs_set_port_max = 65535;
43 
44 static 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 
54 module_param_call(callback_tcpport, param_set_port, param_get_int,
55 		 &nfs_callback_set_tcpport, 0644);
56 
57 /*
58  * This is the callback kernel thread.
59  */
60 static void nfs_callback_svc(struct svc_rqst *rqstp)
61 {
62 	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);
71 	set_freezable();
72 
73 	complete(&nfs_callback_info.started);
74 
75 	for(;;) {
76 		if (signalled()) {
77 			if (nfs_callback_info.users == 0)
78 				break;
79 			flush_signals(current);
80 		}
81 		/*
82 		 * Listen for a request on the socket
83 		 */
84 		err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
85 		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 		}
93 		svc_process(rqstp);
94 	}
95 
96 	flush_signals(current);
97 	svc_exit_thread(rqstp);
98 	nfs_callback_info.pid = 0;
99 	complete(&nfs_callback_info.stopped);
100 	unlock_kernel();
101 	module_put_and_exit(0);
102 }
103 
104 /*
105  * Bring up the server process if it is not already up.
106  */
107 int nfs_callback_up(void)
108 {
109 	struct svc_serv *serv = NULL;
110 	int ret = 0;
111 
112 	lock_kernel();
113 	mutex_lock(&nfs_callback_mutex);
114 	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, NULL);
119 	ret = -ENOMEM;
120 	if (!serv)
121 		goto out_err;
122 
123 	ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
124 			      SVC_SOCK_ANONYMOUS);
125 	if (ret <= 0)
126 		goto out_err;
127 	nfs_callback_tcpport = ret;
128 	dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
129 
130 	ret = svc_create_thread(nfs_callback_svc, serv);
131 	if (ret < 0)
132 		goto out_err;
133 	nfs_callback_info.serv = serv;
134 	wait_for_completion(&nfs_callback_info.started);
135 out:
136 	/*
137 	 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
138 	 * svc_create_thread increments that. So we need to call svc_destroy
139 	 * on both success and failure so that the refcount is 1 when the
140 	 * thread exits.
141 	 */
142 	if (serv)
143 		svc_destroy(serv);
144 	mutex_unlock(&nfs_callback_mutex);
145 	unlock_kernel();
146 	return ret;
147 out_err:
148 	dprintk("Couldn't create callback socket or server thread; err = %d\n",
149 		ret);
150 	nfs_callback_info.users--;
151 	goto out;
152 }
153 
154 /*
155  * Kill the server process if it is not already up.
156  */
157 void nfs_callback_down(void)
158 {
159 	lock_kernel();
160 	mutex_lock(&nfs_callback_mutex);
161 	nfs_callback_info.users--;
162 	do {
163 		if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
164 			break;
165 		if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
166 			break;
167 	} while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
168 	mutex_unlock(&nfs_callback_mutex);
169 	unlock_kernel();
170 }
171 
172 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
173 {
174 	struct nfs_client *clp;
175 	RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
176 
177 	/* Don't talk to strangers */
178 	clp = nfs_find_client(svc_addr(rqstp), 4);
179 	if (clp == NULL)
180 		return SVC_DROP;
181 
182 	dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
183 			svc_print_addr(rqstp, buf, sizeof(buf)));
184 	nfs_put_client(clp);
185 
186 	switch (rqstp->rq_authop->flavour) {
187 		case RPC_AUTH_NULL:
188 			if (rqstp->rq_proc != CB_NULL)
189 				return SVC_DENIED;
190 			break;
191 		case RPC_AUTH_UNIX:
192 			break;
193 		case RPC_AUTH_GSS:
194 			/* FIXME: RPCSEC_GSS handling? */
195 		default:
196 			return SVC_DENIED;
197 	}
198 	return SVC_OK;
199 }
200 
201 /*
202  * Define NFS4 callback program
203  */
204 static struct svc_version *nfs4_callback_version[] = {
205 	[1] = &nfs4_callback_version1,
206 };
207 
208 static struct svc_stat nfs4_callback_stats;
209 
210 static struct svc_program nfs4_callback_program = {
211 	.pg_prog = NFS4_CALLBACK,			/* RPC service number */
212 	.pg_nvers = ARRAY_SIZE(nfs4_callback_version),	/* Number of entries */
213 	.pg_vers = nfs4_callback_version,		/* version table */
214 	.pg_name = "NFSv4 callback",			/* service name */
215 	.pg_class = "nfs",				/* authentication class */
216 	.pg_stats = &nfs4_callback_stats,
217 	.pg_authenticate = nfs_callback_authenticate,
218 };
219