blob: c2e9cfd9e5a4d04e3c3a6a82e2f49c8deb8821e6 [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
Chuck Lever18de9732008-10-16 17:41:11 -040043/*
44 * If the kernel has IPv6 support available, always listen for
45 * both AF_INET and AF_INET6 requests.
46 */
47#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
48static const sa_family_t nfs_callback_family = AF_INET6;
49#else
50static const sa_family_t nfs_callback_family = AF_INET;
51#endif
52
David Howells7d4e2742006-08-22 20:06:07 -040053static int param_set_port(const char *val, struct kernel_param *kp)
54{
55 char *endp;
56 int num = simple_strtol(val, &endp, 0);
57 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
58 return -EINVAL;
59 *((int *)kp->arg) = num;
60 return 0;
61}
62
63module_param_call(callback_tcpport, param_set_port, param_get_int,
64 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * This is the callback kernel thread.
68 */
Jeff Laytona277e332008-02-20 08:55:30 -050069static int
70nfs_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jeff Layton06e02d62008-04-08 15:40:07 -040072 int err, preverr = 0;
Jeff Laytona277e332008-02-20 08:55:30 -050073 struct svc_rqst *rqstp = vrqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Rafael J. Wysocki83144182007-07-17 04:03:35 -070075 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jeff Laytona277e332008-02-20 08:55:30 -050077 /*
78 * FIXME: do we really need to run this under the BKL? If so, please
79 * add a comment about what it's intended to protect.
80 */
81 lock_kernel();
82 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /*
84 * Listen for a request on the socket
85 */
NeilBrown6fb2b472006-10-02 02:17:50 -070086 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
Jeff Layton06e02d62008-04-08 15:40:07 -040087 if (err == -EAGAIN || err == -EINTR) {
88 preverr = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Jeff Layton06e02d62008-04-08 15:40:07 -040091 if (err < 0) {
92 if (err != preverr) {
93 printk(KERN_WARNING "%s: unexpected error "
94 "from svc_recv (%d)\n", __func__, err);
95 preverr = err;
96 }
97 schedule_timeout_uninterruptible(HZ);
98 continue;
99 }
100 preverr = err;
NeilBrown6fb2b472006-10-02 02:17:50 -0700101 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unlock_kernel();
Jeff Laytona277e332008-02-20 08:55:30 -0500104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400108 * Bring up the callback thread if it is not already up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
110int nfs_callback_up(void)
111{
Jeff Layton8e600292008-02-11 10:00:20 -0500112 struct svc_serv *serv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int ret = 0;
114
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800115 mutex_lock(&nfs_callback_mutex);
Jeff Laytona277e332008-02-20 08:55:30 -0500116 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto out;
Chuck Levere851db52008-06-30 18:45:30 -0400118 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
Chuck Lever18de9732008-10-16 17:41:11 -0400119 nfs_callback_family, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 ret = -ENOMEM;
121 if (!serv)
122 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800123
Tom Tuckerd7c9f1e2007-12-30 21:07:44 -0600124 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
125 SVC_SOCK_ANONYMOUS);
Chuck Lever482fb942007-02-12 00:53:29 -0800126 if (ret <= 0)
Jeff Layton8e600292008-02-11 10:00:20 -0500127 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800128 nfs_callback_tcpport = ret;
Chuck Lever18de9732008-10-16 17:41:11 -0400129 dprintk("NFS: Callback listener port = %u (af %u)\n",
130 nfs_callback_tcpport, nfs_callback_family);
Chuck Lever482fb942007-02-12 00:53:29 -0800131
Jeff Layton5afc5972008-06-11 10:03:11 -0400132 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
133 if (IS_ERR(nfs_callback_info.rqst)) {
134 ret = PTR_ERR(nfs_callback_info.rqst);
135 nfs_callback_info.rqst = NULL;
Jeff Layton8e600292008-02-11 10:00:20 -0500136 goto out_err;
Jeff Laytona277e332008-02-20 08:55:30 -0500137 }
138
139 svc_sock_update_bufs(serv);
Jeff Laytona277e332008-02-20 08:55:30 -0500140
Jeff Layton5afc5972008-06-11 10:03:11 -0400141 nfs_callback_info.task = kthread_run(nfs_callback_svc,
142 nfs_callback_info.rqst,
Jeff Laytona277e332008-02-20 08:55:30 -0500143 "nfsv4-svc");
144 if (IS_ERR(nfs_callback_info.task)) {
145 ret = PTR_ERR(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400146 svc_exit_thread(nfs_callback_info.rqst);
147 nfs_callback_info.rqst = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500148 nfs_callback_info.task = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500149 goto out_err;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151out:
Jeff Layton8e600292008-02-11 10:00:20 -0500152 /*
153 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500154 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500155 * on both success and failure so that the refcount is 1 when the
156 * thread exits.
157 */
158 if (serv)
159 svc_destroy(serv);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800160 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ret;
Jeff Layton8e600292008-02-11 10:00:20 -0500162out_err:
Chuck Lever18de9732008-10-16 17:41:11 -0400163 dprintk("NFS: Couldn't create callback socket or server thread; "
164 "err = %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 nfs_callback_info.users--;
166 goto out;
167}
168
169/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400170 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400172void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800174 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500175 nfs_callback_info.users--;
Jeff Layton5afc5972008-06-11 10:03:11 -0400176 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
Jeff Laytona277e332008-02-20 08:55:30 -0500177 kthread_stop(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400178 svc_exit_thread(nfs_callback_info.rqst);
179 nfs_callback_info.rqst = NULL;
180 nfs_callback_info.task = NULL;
181 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800182 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static int nfs_callback_authenticate(struct svc_rqst *rqstp)
186{
David Howellsadfa6f92006-08-22 20:06:08 -0400187 struct nfs_client *clp;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300188 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* Don't talk to strangers */
Chuck Leverff052642007-12-10 14:58:44 -0500191 clp = nfs_find_client(svc_addr(rqstp), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (clp == NULL)
193 return SVC_DROP;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800194
Harvey Harrison3110ff82008-05-02 13:42:44 -0700195 dprintk("%s: %s NFSv4 callback!\n", __func__,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800196 svc_print_addr(rqstp, buf, sizeof(buf)));
David Howells24c8dbb2006-08-22 20:06:10 -0400197 nfs_put_client(clp);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 switch (rqstp->rq_authop->flavour) {
200 case RPC_AUTH_NULL:
201 if (rqstp->rq_proc != CB_NULL)
202 return SVC_DENIED;
203 break;
204 case RPC_AUTH_UNIX:
205 break;
206 case RPC_AUTH_GSS:
207 /* FIXME: RPCSEC_GSS handling? */
208 default:
209 return SVC_DENIED;
210 }
211 return SVC_OK;
212}
213
214/*
215 * Define NFS4 callback program
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static struct svc_version *nfs4_callback_version[] = {
218 [1] = &nfs4_callback_version1,
219};
220
221static struct svc_stat nfs4_callback_stats;
222
223static struct svc_program nfs4_callback_program = {
224 .pg_prog = NFS4_CALLBACK, /* RPC service number */
225 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
226 .pg_vers = nfs4_callback_version, /* version table */
227 .pg_name = "NFSv4 callback", /* service name */
228 .pg_class = "nfs", /* authentication class */
229 .pg_stats = &nfs4_callback_stats,
230 .pg_authenticate = nfs_callback_authenticate,
231};