bio: add support for inlining a number of bio_vecs inside the bio
[deliverable/linux.git] / fs / nfs / callback.c
CommitLineData
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>
12#include <linux/smp_lock.h>
13#include <linux/sunrpc/svc.h>
14#include <linux/sunrpc/svcsock.h>
15#include <linux/nfs_fs.h>
353ab6e9 16#include <linux/mutex.h>
83144186 17#include <linux/freezer.h>
a277e33c 18#include <linux/kthread.h>
14c85021
ACM
19
20#include <net/inet_sock.h>
21
4ce79717 22#include "nfs4_fs.h"
1da177e4 23#include "callback.h"
24c8dbbb 24#include "internal.h"
1da177e4
LT
25
26#define NFSDBG_FACILITY NFSDBG_CALLBACK
27
28struct nfs_callback_data {
29 unsigned int users;
5afc597c 30 struct svc_rqst *rqst;
a277e33c 31 struct task_struct *task;
1da177e4
LT
32};
33
34static struct nfs_callback_data nfs_callback_info;
353ab6e9 35static DEFINE_MUTEX(nfs_callback_mutex);
1da177e4
LT
36static struct svc_program nfs4_callback_program;
37
a72b4422 38unsigned int nfs_callback_set_tcpport;
1da177e4 39unsigned short nfs_callback_tcpport;
7d4e2747
DH
40static const int nfs_set_port_min = 0;
41static const int nfs_set_port_max = 65535;
42
18de9735
CL
43/*
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
7d4e2747
DH
53static 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);
1da177e4
LT
65
66/*
67 * This is the callback kernel thread.
68 */
a277e33c
JL
69static int
70nfs_callback_svc(void *vrqstp)
1da177e4 71{
06e02d66 72 int err, preverr = 0;
a277e33c 73 struct svc_rqst *rqstp = vrqstp;
1da177e4 74
83144186 75 set_freezable();
1da177e4 76
a277e33c
JL
77 /*
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()) {
1da177e4
LT
83 /*
84 * Listen for a request on the socket
85 */
6fb2b47f 86 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
06e02d66
JL
87 if (err == -EAGAIN || err == -EINTR) {
88 preverr = err;
1da177e4 89 continue;
06e02d66 90 }
1da177e4 91 if (err < 0) {
06e02d66
JL
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;
1da177e4 99 }
06e02d66 100 preverr = err;
6fb2b47f 101 svc_process(rqstp);
1da177e4 102 }
1da177e4 103 unlock_kernel();
a277e33c 104 return 0;
1da177e4
LT
105}
106
107/*
5afc597c 108 * Bring up the callback thread if it is not already up.
1da177e4
LT
109 */
110int nfs_callback_up(void)
111{
8e60029f 112 struct svc_serv *serv = NULL;
1da177e4
LT
113 int ret = 0;
114
353ab6e9 115 mutex_lock(&nfs_callback_mutex);
a277e33c 116 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
1da177e4 117 goto out;
e851db5b 118 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
18de9735 119 nfs_callback_family, NULL);
1da177e4
LT
120 ret = -ENOMEM;
121 if (!serv)
122 goto out_err;
482fb94e 123
d7c9f1ed
TT
124 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
125 SVC_SOCK_ANONYMOUS);
482fb94e 126 if (ret <= 0)
8e60029f 127 goto out_err;
482fb94e 128 nfs_callback_tcpport = ret;
18de9735
CL
129 dprintk("NFS: Callback listener port = %u (af %u)\n",
130 nfs_callback_tcpport, nfs_callback_family);
482fb94e 131
5afc597c
JL
132 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;
8e60029f 136 goto out_err;
a277e33c
JL
137 }
138
139 svc_sock_update_bufs(serv);
a277e33c 140
5afc597c
JL
141 nfs_callback_info.task = kthread_run(nfs_callback_svc,
142 nfs_callback_info.rqst,
a277e33c
JL
143 "nfsv4-svc");
144 if (IS_ERR(nfs_callback_info.task)) {
145 ret = PTR_ERR(nfs_callback_info.task);
5afc597c
JL
146 svc_exit_thread(nfs_callback_info.rqst);
147 nfs_callback_info.rqst = NULL;
a277e33c 148 nfs_callback_info.task = NULL;
a277e33c
JL
149 goto out_err;
150 }
1da177e4 151out:
8e60029f
JL
152 /*
153 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
a277e33c 154 * svc_prepare_thread increments that. So we need to call svc_destroy
8e60029f
JL
155 * on both success and failure so that the refcount is 1 when the
156 * thread exits.
157 */
158 if (serv)
159 svc_destroy(serv);
353ab6e9 160 mutex_unlock(&nfs_callback_mutex);
1da177e4 161 return ret;
8e60029f 162out_err:
18de9735
CL
163 dprintk("NFS: Couldn't create callback socket or server thread; "
164 "err = %d\n", ret);
1da177e4
LT
165 nfs_callback_info.users--;
166 goto out;
167}
168
169/*
5afc597c 170 * Kill the callback thread if it's no longer being used.
1da177e4 171 */
5ae1fbce 172void nfs_callback_down(void)
1da177e4 173{
353ab6e9 174 mutex_lock(&nfs_callback_mutex);
1dd761e9 175 nfs_callback_info.users--;
5afc597c 176 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
a277e33c 177 kthread_stop(nfs_callback_info.task);
5afc597c
JL
178 svc_exit_thread(nfs_callback_info.rqst);
179 nfs_callback_info.rqst = NULL;
180 nfs_callback_info.task = NULL;
181 }
353ab6e9 182 mutex_unlock(&nfs_callback_mutex);
1da177e4
LT
183}
184
185static int nfs_callback_authenticate(struct svc_rqst *rqstp)
186{
adfa6f98 187 struct nfs_client *clp;
5216a8e7 188 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1da177e4
LT
189
190 /* Don't talk to strangers */
ff052645 191 clp = nfs_find_client(svc_addr(rqstp), 4);
1da177e4
LT
192 if (clp == NULL)
193 return SVC_DROP;
ad06e4bd 194
3110ff80 195 dprintk("%s: %s NFSv4 callback!\n", __func__,
ad06e4bd 196 svc_print_addr(rqstp, buf, sizeof(buf)));
24c8dbbb 197 nfs_put_client(clp);
ad06e4bd 198
1da177e4
LT
199 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 */
1da177e4
LT
217static 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};
This page took 0.414773 seconds and 5 git commands to generate.