blob: 6a09760c5960fd427935c2306f5080caa66bce3b [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>
Jeff Laytona277e332008-02-20 08:55:30 -050018#include <linux/kthread.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019
20#include <net/inet_sock.h>
21
Trond Myklebust4ce79712005-06-22 17:16:21 +000022#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "callback.h"
David Howells24c8dbb2006-08-22 20:06:10 -040024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define NFSDBG_FACILITY NFSDBG_CALLBACK
27
28struct nfs_callback_data {
29 unsigned int users;
Jeff Layton5afc5972008-06-11 10:03:11 -040030 struct svc_rqst *rqst;
Jeff Laytona277e332008-02-20 08:55:30 -050031 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
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;
David Howells7d4e2742006-08-22 20:06:07 -040040static const int nfs_set_port_min = 0;
41static const int nfs_set_port_max = 65535;
42
43static int param_set_port(const char *val, struct kernel_param *kp)
44{
45 char *endp;
46 int num = simple_strtol(val, &endp, 0);
47 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
48 return -EINVAL;
49 *((int *)kp->arg) = num;
50 return 0;
51}
52
53module_param_call(callback_tcpport, param_set_port, param_get_int,
54 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * This is the callback kernel thread.
58 */
Jeff Laytona277e332008-02-20 08:55:30 -050059static int
60nfs_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jeff Layton06e02d62008-04-08 15:40:07 -040062 int err, preverr = 0;
Jeff Laytona277e332008-02-20 08:55:30 -050063 struct svc_rqst *rqstp = vrqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Rafael J. Wysocki83144182007-07-17 04:03:35 -070065 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Laytona277e332008-02-20 08:55:30 -050067 /*
68 * FIXME: do we really need to run this under the BKL? If so, please
69 * add a comment about what it's intended to protect.
70 */
71 lock_kernel();
72 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /*
74 * Listen for a request on the socket
75 */
NeilBrown6fb2b472006-10-02 02:17:50 -070076 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
Jeff Layton06e02d62008-04-08 15:40:07 -040077 if (err == -EAGAIN || err == -EINTR) {
78 preverr = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Jeff Layton06e02d62008-04-08 15:40:07 -040081 if (err < 0) {
82 if (err != preverr) {
83 printk(KERN_WARNING "%s: unexpected error "
84 "from svc_recv (%d)\n", __func__, err);
85 preverr = err;
86 }
87 schedule_timeout_uninterruptible(HZ);
88 continue;
89 }
90 preverr = err;
NeilBrown6fb2b472006-10-02 02:17:50 -070091 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unlock_kernel();
Jeff Laytona277e332008-02-20 08:55:30 -050094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/*
Jeff Layton5afc5972008-06-11 10:03:11 -040098 * Bring up the callback thread if it is not already up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100int nfs_callback_up(void)
101{
Jeff Layton8e600292008-02-11 10:00:20 -0500102 struct svc_serv *serv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int ret = 0;
104
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800105 mutex_lock(&nfs_callback_mutex);
Jeff Laytona277e332008-02-20 08:55:30 -0500106 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto out;
Chuck Levere851db52008-06-30 18:45:30 -0400108 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
109 AF_INET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 ret = -ENOMEM;
111 if (!serv)
112 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800113
Tom Tuckerd7c9f1e2007-12-30 21:07:44 -0600114 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
115 SVC_SOCK_ANONYMOUS);
Chuck Lever482fb942007-02-12 00:53:29 -0800116 if (ret <= 0)
Jeff Layton8e600292008-02-11 10:00:20 -0500117 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800118 nfs_callback_tcpport = ret;
119 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
120
Jeff Layton5afc5972008-06-11 10:03:11 -0400121 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
122 if (IS_ERR(nfs_callback_info.rqst)) {
123 ret = PTR_ERR(nfs_callback_info.rqst);
124 nfs_callback_info.rqst = NULL;
Jeff Layton8e600292008-02-11 10:00:20 -0500125 goto out_err;
Jeff Laytona277e332008-02-20 08:55:30 -0500126 }
127
128 svc_sock_update_bufs(serv);
Jeff Laytona277e332008-02-20 08:55:30 -0500129
Jeff Layton5afc5972008-06-11 10:03:11 -0400130 nfs_callback_info.task = kthread_run(nfs_callback_svc,
131 nfs_callback_info.rqst,
Jeff Laytona277e332008-02-20 08:55:30 -0500132 "nfsv4-svc");
133 if (IS_ERR(nfs_callback_info.task)) {
134 ret = PTR_ERR(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400135 svc_exit_thread(nfs_callback_info.rqst);
136 nfs_callback_info.rqst = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500137 nfs_callback_info.task = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500138 goto out_err;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140out:
Jeff Layton8e600292008-02-11 10:00:20 -0500141 /*
142 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500143 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500144 * on both success and failure so that the refcount is 1 when the
145 * thread exits.
146 */
147 if (serv)
148 svc_destroy(serv);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800149 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ret;
Jeff Layton8e600292008-02-11 10:00:20 -0500151out_err:
Chuck Lever482fb942007-02-12 00:53:29 -0800152 dprintk("Couldn't create callback socket or server thread; err = %d\n",
153 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 nfs_callback_info.users--;
155 goto out;
156}
157
158/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400159 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400161void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800163 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500164 nfs_callback_info.users--;
Jeff Layton5afc5972008-06-11 10:03:11 -0400165 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
Jeff Laytona277e332008-02-20 08:55:30 -0500166 kthread_stop(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400167 svc_exit_thread(nfs_callback_info.rqst);
168 nfs_callback_info.rqst = NULL;
169 nfs_callback_info.task = NULL;
170 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800171 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static int nfs_callback_authenticate(struct svc_rqst *rqstp)
175{
David Howellsadfa6f92006-08-22 20:06:08 -0400176 struct nfs_client *clp;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300177 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* Don't talk to strangers */
Chuck Leverff052642007-12-10 14:58:44 -0500180 clp = nfs_find_client(svc_addr(rqstp), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (clp == NULL)
182 return SVC_DROP;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800183
Harvey Harrison3110ff82008-05-02 13:42:44 -0700184 dprintk("%s: %s NFSv4 callback!\n", __func__,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800185 svc_print_addr(rqstp, buf, sizeof(buf)));
David Howells24c8dbb2006-08-22 20:06:10 -0400186 nfs_put_client(clp);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 switch (rqstp->rq_authop->flavour) {
189 case RPC_AUTH_NULL:
190 if (rqstp->rq_proc != CB_NULL)
191 return SVC_DENIED;
192 break;
193 case RPC_AUTH_UNIX:
194 break;
195 case RPC_AUTH_GSS:
196 /* FIXME: RPCSEC_GSS handling? */
197 default:
198 return SVC_DENIED;
199 }
200 return SVC_OK;
201}
202
203/*
204 * Define NFS4 callback program
205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct svc_version *nfs4_callback_version[] = {
207 [1] = &nfs4_callback_version1,
208};
209
210static struct svc_stat nfs4_callback_stats;
211
212static struct svc_program nfs4_callback_program = {
213 .pg_prog = NFS4_CALLBACK, /* RPC service number */
214 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
215 .pg_vers = nfs4_callback_version, /* version table */
216 .pg_name = "NFSv4 callback", /* service name */
217 .pg_class = "nfs", /* authentication class */
218 .pg_stats = &nfs4_callback_stats,
219 .pg_authenticate = nfs_callback_authenticate,
220};