]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/callback.c | |
3 | * | |
4 | * Copyright (C) 2004 Trond Myklebust | |
5 | * | |
6 | * NFSv4 callback handling | |
7 | */ | |
8 | ||
1da177e4 LT |
9 | #include <linux/completion.h> |
10 | #include <linux/ip.h> | |
11 | #include <linux/module.h> | |
1da177e4 LT |
12 | #include <linux/sunrpc/svc.h> |
13 | #include <linux/sunrpc/svcsock.h> | |
14 | #include <linux/nfs_fs.h> | |
353ab6e9 | 15 | #include <linux/mutex.h> |
83144186 | 16 | #include <linux/freezer.h> |
a277e33c | 17 | #include <linux/kthread.h> |
945b34a7 | 18 | #include <linux/sunrpc/svcauth_gss.h> |
a43cde94 | 19 | #include <linux/sunrpc/bc_xprt.h> |
14c85021 ACM |
20 | |
21 | #include <net/inet_sock.h> | |
22 | ||
4ce79717 | 23 | #include "nfs4_fs.h" |
1da177e4 | 24 | #include "callback.h" |
24c8dbbb | 25 | #include "internal.h" |
1da177e4 LT |
26 | |
27 | #define NFSDBG_FACILITY NFSDBG_CALLBACK | |
28 | ||
29 | struct nfs_callback_data { | |
30 | unsigned int users; | |
a43cde94 | 31 | struct svc_serv *serv; |
5afc597c | 32 | struct svc_rqst *rqst; |
a277e33c | 33 | struct task_struct *task; |
1da177e4 LT |
34 | }; |
35 | ||
e82dc22d | 36 | static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1]; |
353ab6e9 | 37 | static DEFINE_MUTEX(nfs_callback_mutex); |
1da177e4 LT |
38 | static struct svc_program nfs4_callback_program; |
39 | ||
f738f517 | 40 | unsigned short nfs_callback_tcpport6; |
1da177e4 LT |
41 | |
42 | /* | |
e82dc22d | 43 | * This is the NFSv4 callback kernel thread. |
1da177e4 | 44 | */ |
a277e33c | 45 | static int |
71468513 | 46 | nfs4_callback_svc(void *vrqstp) |
1da177e4 | 47 | { |
06e02d66 | 48 | int err, preverr = 0; |
a277e33c | 49 | struct svc_rqst *rqstp = vrqstp; |
1da177e4 | 50 | |
83144186 | 51 | set_freezable(); |
1da177e4 | 52 | |
a277e33c | 53 | while (!kthread_should_stop()) { |
1da177e4 LT |
54 | /* |
55 | * Listen for a request on the socket | |
56 | */ | |
6fb2b47f | 57 | err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT); |
06e02d66 JL |
58 | if (err == -EAGAIN || err == -EINTR) { |
59 | preverr = err; | |
1da177e4 | 60 | continue; |
06e02d66 | 61 | } |
1da177e4 | 62 | if (err < 0) { |
06e02d66 | 63 | if (err != preverr) { |
a030889a | 64 | printk(KERN_WARNING "NFS: %s: unexpected error " |
06e02d66 JL |
65 | "from svc_recv (%d)\n", __func__, err); |
66 | preverr = err; | |
67 | } | |
68 | schedule_timeout_uninterruptible(HZ); | |
69 | continue; | |
1da177e4 | 70 | } |
06e02d66 | 71 | preverr = err; |
6fb2b47f | 72 | svc_process(rqstp); |
1da177e4 | 73 | } |
a277e33c | 74 | return 0; |
1da177e4 LT |
75 | } |
76 | ||
77 | /* | |
71468513 | 78 | * Prepare to bring up the NFSv4 callback service |
1da177e4 | 79 | */ |
17280175 | 80 | static struct svc_rqst * |
7bb782c6 | 81 | nfs4_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt) |
1da177e4 | 82 | { |
71468513 | 83 | int ret; |
482fb94e | 84 | |
12918b10 | 85 | ret = svc_create_xprt(serv, "tcp", &init_net, PF_INET, |
9652ada3 | 86 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
482fb94e | 87 | if (ret <= 0) |
8e60029f | 88 | goto out_err; |
482fb94e | 89 | nfs_callback_tcpport = ret; |
18de9735 | 90 | dprintk("NFS: Callback listener port = %u (af %u)\n", |
26298caa | 91 | nfs_callback_tcpport, PF_INET); |
482fb94e | 92 | |
12918b10 | 93 | ret = svc_create_xprt(serv, "tcp", &init_net, PF_INET6, |
f738f517 CL |
94 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
95 | if (ret > 0) { | |
96 | nfs_callback_tcpport6 = ret; | |
97 | dprintk("NFS: Callback listener port = %u (af %u)\n", | |
98 | nfs_callback_tcpport6, PF_INET6); | |
18fc3164 CL |
99 | } else if (ret == -EAFNOSUPPORT) |
100 | ret = 0; | |
101 | else | |
f738f517 | 102 | goto out_err; |
f738f517 | 103 | |
11fd165c | 104 | return svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE); |
71468513 BH |
105 | |
106 | out_err: | |
107 | if (ret == 0) | |
108 | ret = -ENOMEM; | |
109 | return ERR_PTR(ret); | |
110 | } | |
111 | ||
a43cde94 RL |
112 | #if defined(CONFIG_NFS_V4_1) |
113 | /* | |
114 | * The callback service for NFSv4.1 callbacks | |
115 | */ | |
116 | static int | |
117 | nfs41_callback_svc(void *vrqstp) | |
118 | { | |
119 | struct svc_rqst *rqstp = vrqstp; | |
120 | struct svc_serv *serv = rqstp->rq_server; | |
121 | struct rpc_rqst *req; | |
122 | int error; | |
123 | DEFINE_WAIT(wq); | |
124 | ||
125 | set_freezable(); | |
126 | ||
a43cde94 RL |
127 | while (!kthread_should_stop()) { |
128 | prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE); | |
129 | spin_lock_bh(&serv->sv_cb_lock); | |
130 | if (!list_empty(&serv->sv_cb_list)) { | |
131 | req = list_first_entry(&serv->sv_cb_list, | |
132 | struct rpc_rqst, rq_bc_list); | |
133 | list_del(&req->rq_bc_list); | |
134 | spin_unlock_bh(&serv->sv_cb_lock); | |
135 | dprintk("Invoking bc_svc_process()\n"); | |
136 | error = bc_svc_process(serv, req, rqstp); | |
137 | dprintk("bc_svc_process() returned w/ error code= %d\n", | |
138 | error); | |
139 | } else { | |
140 | spin_unlock_bh(&serv->sv_cb_lock); | |
141 | schedule(); | |
142 | } | |
143 | finish_wait(&serv->sv_cb_waitq, &wq); | |
144 | } | |
a43cde94 RL |
145 | return 0; |
146 | } | |
147 | ||
148 | /* | |
149 | * Bring up the NFSv4.1 callback service | |
150 | */ | |
17280175 | 151 | static struct svc_rqst * |
a43cde94 RL |
152 | nfs41_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt) |
153 | { | |
01c9a0bc AA |
154 | struct svc_rqst *rqstp; |
155 | int ret; | |
8f975242 | 156 | |
01c9a0bc AA |
157 | /* |
158 | * Create an svc_sock for the back channel service that shares the | |
159 | * fore channel connection. | |
160 | * Returns the input port (0) and sets the svc_serv bc_xprt on success | |
161 | */ | |
12918b10 | 162 | ret = svc_create_xprt(serv, "tcp-bc", &init_net, PF_INET, 0, |
01c9a0bc AA |
163 | SVC_SOCK_ANONYMOUS); |
164 | if (ret < 0) { | |
165 | rqstp = ERR_PTR(ret); | |
8f975242 | 166 | goto out; |
01c9a0bc | 167 | } |
8f975242 | 168 | |
a43cde94 RL |
169 | /* |
170 | * Save the svc_serv in the transport so that it can | |
171 | * be referenced when the session backchannel is initialized | |
172 | */ | |
173 | xprt->bc_serv = serv; | |
174 | ||
175 | INIT_LIST_HEAD(&serv->sv_cb_list); | |
176 | spin_lock_init(&serv->sv_cb_lock); | |
177 | init_waitqueue_head(&serv->sv_cb_waitq); | |
11fd165c | 178 | rqstp = svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE); |
01c9a0bc | 179 | if (IS_ERR(rqstp)) { |
4a19de0f AA |
180 | svc_xprt_put(serv->sv_bc_xprt); |
181 | serv->sv_bc_xprt = NULL; | |
01c9a0bc | 182 | } |
8f975242 | 183 | out: |
01c9a0bc AA |
184 | dprintk("--> %s return %ld\n", __func__, |
185 | IS_ERR(rqstp) ? PTR_ERR(rqstp) : 0); | |
8f975242 | 186 | return rqstp; |
a43cde94 RL |
187 | } |
188 | ||
189 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, | |
190 | struct svc_serv *serv, struct rpc_xprt *xprt, | |
191 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) | |
192 | { | |
193 | if (minorversion) { | |
194 | *rqstpp = nfs41_callback_up(serv, xprt); | |
195 | *callback_svc = nfs41_callback_svc; | |
196 | } | |
197 | return minorversion; | |
198 | } | |
199 | ||
200 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, | |
201 | struct nfs_callback_data *cb_info) | |
202 | { | |
203 | if (minorversion) | |
204 | xprt->bc_serv = cb_info->serv; | |
205 | } | |
206 | #else | |
207 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, | |
208 | struct svc_serv *serv, struct rpc_xprt *xprt, | |
209 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) | |
210 | { | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, | |
215 | struct nfs_callback_data *cb_info) | |
216 | { | |
217 | } | |
218 | #endif /* CONFIG_NFS_V4_1 */ | |
219 | ||
71468513 BH |
220 | /* |
221 | * Bring up the callback thread if it is not already up. | |
222 | */ | |
223 | int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) | |
224 | { | |
225 | struct svc_serv *serv = NULL; | |
226 | struct svc_rqst *rqstp; | |
227 | int (*callback_svc)(void *vrqstp); | |
e82dc22d | 228 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
71468513 BH |
229 | char svc_name[12]; |
230 | int ret = 0; | |
a43cde94 | 231 | int minorversion_setup; |
12918b10 | 232 | struct net *net = &init_net; |
71468513 BH |
233 | |
234 | mutex_lock(&nfs_callback_mutex); | |
e82dc22d AA |
235 | if (cb_info->users++ || cb_info->task != NULL) { |
236 | nfs_callback_bc_serv(minorversion, xprt, cb_info); | |
71468513 | 237 | goto out; |
a43cde94 | 238 | } |
71468513 BH |
239 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL); |
240 | if (!serv) { | |
241 | ret = -ENOMEM; | |
242 | goto out_err; | |
243 | } | |
b042414f N |
244 | /* As there is only one thread we need to over-ride the |
245 | * default maximum of 80 connections | |
246 | */ | |
247 | serv->sv_maxconn = 1024; | |
71468513 | 248 | |
9793f7c8 SK |
249 | ret = svc_bind(serv, net); |
250 | if (ret < 0) { | |
251 | printk(KERN_WARNING "NFS: bind callback service failed\n"); | |
252 | goto out_err; | |
253 | } | |
254 | ||
a43cde94 RL |
255 | minorversion_setup = nfs_minorversion_callback_svc_setup(minorversion, |
256 | serv, xprt, &rqstp, &callback_svc); | |
257 | if (!minorversion_setup) { | |
258 | /* v4.0 callback setup */ | |
7bb782c6 | 259 | rqstp = nfs4_callback_up(serv, xprt); |
71468513 | 260 | callback_svc = nfs4_callback_svc; |
71468513 BH |
261 | } |
262 | ||
263 | if (IS_ERR(rqstp)) { | |
264 | ret = PTR_ERR(rqstp); | |
8e60029f | 265 | goto out_err; |
a277e33c JL |
266 | } |
267 | ||
268 | svc_sock_update_bufs(serv); | |
a277e33c | 269 | |
71468513 | 270 | sprintf(svc_name, "nfsv4.%u-svc", minorversion); |
e82dc22d AA |
271 | cb_info->serv = serv; |
272 | cb_info->rqst = rqstp; | |
273 | cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name); | |
274 | if (IS_ERR(cb_info->task)) { | |
275 | ret = PTR_ERR(cb_info->task); | |
276 | svc_exit_thread(cb_info->rqst); | |
277 | cb_info->rqst = NULL; | |
278 | cb_info->task = NULL; | |
a277e33c JL |
279 | goto out_err; |
280 | } | |
1da177e4 | 281 | out: |
8e60029f JL |
282 | /* |
283 | * svc_create creates the svc_serv with sv_nrthreads == 1, and then | |
a277e33c | 284 | * svc_prepare_thread increments that. So we need to call svc_destroy |
8e60029f JL |
285 | * on both success and failure so that the refcount is 1 when the |
286 | * thread exits. | |
287 | */ | |
288 | if (serv) | |
289 | svc_destroy(serv); | |
353ab6e9 | 290 | mutex_unlock(&nfs_callback_mutex); |
1da177e4 | 291 | return ret; |
8e60029f | 292 | out_err: |
18de9735 CL |
293 | dprintk("NFS: Couldn't create callback socket or server thread; " |
294 | "err = %d\n", ret); | |
e82dc22d | 295 | cb_info->users--; |
786185b5 SK |
296 | if (serv) |
297 | svc_shutdown_net(serv, net); | |
1da177e4 LT |
298 | goto out; |
299 | } | |
300 | ||
301 | /* | |
5afc597c | 302 | * Kill the callback thread if it's no longer being used. |
1da177e4 | 303 | */ |
e82dc22d | 304 | void nfs_callback_down(int minorversion) |
1da177e4 | 305 | { |
e82dc22d AA |
306 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
307 | ||
353ab6e9 | 308 | mutex_lock(&nfs_callback_mutex); |
e82dc22d AA |
309 | cb_info->users--; |
310 | if (cb_info->users == 0 && cb_info->task != NULL) { | |
311 | kthread_stop(cb_info->task); | |
12918b10 | 312 | svc_shutdown_net(cb_info->serv, &init_net); |
e82dc22d AA |
313 | svc_exit_thread(cb_info->rqst); |
314 | cb_info->serv = NULL; | |
315 | cb_info->rqst = NULL; | |
316 | cb_info->task = NULL; | |
5afc597c | 317 | } |
353ab6e9 | 318 | mutex_unlock(&nfs_callback_mutex); |
1da177e4 LT |
319 | } |
320 | ||
778be232 AA |
321 | /* Boolean check of RPC_AUTH_GSS principal */ |
322 | int | |
323 | check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp) | |
945b34a7 | 324 | { |
03a4e1f6 | 325 | char *p = rqstp->rq_cred.cr_principal; |
945b34a7 | 326 | |
778be232 AA |
327 | if (rqstp->rq_authop->flavour != RPC_AUTH_GSS) |
328 | return 1; | |
329 | ||
ece0de63 AA |
330 | /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */ |
331 | if (clp->cl_minorversion != 0) | |
778be232 | 332 | return 0; |
945b34a7 OK |
333 | /* |
334 | * It might just be a normal user principal, in which case | |
335 | * userspace won't bother to tell us the name at all. | |
336 | */ | |
337 | if (p == NULL) | |
778be232 | 338 | return 0; |
945b34a7 OK |
339 | |
340 | /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */ | |
341 | ||
342 | if (memcmp(p, "nfs@", 4) != 0) | |
778be232 | 343 | return 0; |
945b34a7 | 344 | p += 4; |
4e0038b6 | 345 | if (strcmp(p, clp->cl_hostname) != 0) |
778be232 AA |
346 | return 0; |
347 | return 1; | |
945b34a7 OK |
348 | } |
349 | ||
778be232 AA |
350 | /* |
351 | * pg_authenticate method for nfsv4 callback threads. | |
352 | * | |
353 | * The authflavor has been negotiated, so an incorrect flavor is a server | |
354 | * bug. Drop packets with incorrect authflavor. | |
355 | * | |
356 | * All other checking done after NFS decoding where the nfs_client can be | |
357 | * found in nfs4_callback_compound | |
358 | */ | |
1da177e4 LT |
359 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
360 | { | |
1da177e4 | 361 | switch (rqstp->rq_authop->flavour) { |
778be232 AA |
362 | case RPC_AUTH_NULL: |
363 | if (rqstp->rq_proc != CB_NULL) | |
364 | return SVC_DROP; | |
365 | break; | |
366 | case RPC_AUTH_GSS: | |
367 | /* No RPC_AUTH_GSS support yet in NFSv4.1 */ | |
368 | if (svc_is_backchannel(rqstp)) | |
369 | return SVC_DROP; | |
1da177e4 | 370 | } |
778be232 | 371 | return SVC_OK; |
1da177e4 LT |
372 | } |
373 | ||
374 | /* | |
375 | * Define NFS4 callback program | |
376 | */ | |
1da177e4 LT |
377 | static struct svc_version *nfs4_callback_version[] = { |
378 | [1] = &nfs4_callback_version1, | |
07bccc2d | 379 | [4] = &nfs4_callback_version4, |
1da177e4 LT |
380 | }; |
381 | ||
382 | static struct svc_stat nfs4_callback_stats; | |
383 | ||
384 | static struct svc_program nfs4_callback_program = { | |
385 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ | |
386 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ | |
387 | .pg_vers = nfs4_callback_version, /* version table */ | |
388 | .pg_name = "NFSv4 callback", /* service name */ | |
389 | .pg_class = "nfs", /* authentication class */ | |
390 | .pg_stats = &nfs4_callback_stats, | |
391 | .pg_authenticate = nfs_callback_authenticate, | |
392 | }; |