Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / nfs / callback.c
blob50bf913802ebdc43978982c694879b917bfc40e8
1 /*
2 * linux/fs/nfs/callback.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback handling
7 */
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>
19 #include <net/inet_sock.h>
21 #include "nfs4_fs.h"
22 #include "callback.h"
23 #include "internal.h"
25 #define NFSDBG_FACILITY NFSDBG_CALLBACK
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;
35 static struct nfs_callback_data nfs_callback_info;
36 static DEFINE_MUTEX(nfs_callback_mutex);
37 static struct svc_program nfs4_callback_program;
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;
44 static int param_set_port(const char *val, struct kernel_param *kp)
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;
54 module_param_call(callback_tcpport, param_set_port, param_get_int,
55 &nfs_callback_set_tcpport, 0644);
58 * This is the callback kernel thread.
60 static void nfs_callback_svc(struct svc_rqst *rqstp)
62 int err;
64 __module_get(THIS_MODULE);
65 lock_kernel();
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();
73 complete(&nfs_callback_info.started);
75 for(;;) {
76 if (signalled()) {
77 if (nfs_callback_info.users == 0)
78 break;
79 flush_signals(current);
82 * Listen for a request on the socket
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;
93 svc_process(rqstp);
96 <<<<<<< HEAD:fs/nfs/callback.c
97 =======
98 flush_signals(current);
99 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfs/callback.c
100 svc_exit_thread(rqstp);
101 nfs_callback_info.pid = 0;
102 complete(&nfs_callback_info.stopped);
103 unlock_kernel();
104 module_put_and_exit(0);
108 * Bring up the server process if it is not already up.
110 int nfs_callback_up(void)
112 struct svc_serv *serv = NULL;
113 int ret = 0;
115 lock_kernel();
116 mutex_lock(&nfs_callback_mutex);
117 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
118 goto out;
119 init_completion(&nfs_callback_info.started);
120 init_completion(&nfs_callback_info.stopped);
121 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
122 ret = -ENOMEM;
123 if (!serv)
124 goto out_err;
126 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
127 SVC_SOCK_ANONYMOUS);
128 if (ret <= 0)
129 goto out_err;
130 nfs_callback_tcpport = ret;
131 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
133 ret = svc_create_thread(nfs_callback_svc, serv);
134 if (ret < 0)
135 goto out_err;
136 nfs_callback_info.serv = serv;
137 wait_for_completion(&nfs_callback_info.started);
138 out:
140 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
141 * svc_create_thread increments that. So we need to call svc_destroy
142 * on both success and failure so that the refcount is 1 when the
143 * thread exits.
145 if (serv)
146 svc_destroy(serv);
147 mutex_unlock(&nfs_callback_mutex);
148 unlock_kernel();
149 return ret;
150 out_err:
151 dprintk("Couldn't create callback socket or server thread; err = %d\n",
152 ret);
153 nfs_callback_info.users--;
154 goto out;
158 * Kill the server process if it is not already up.
160 void nfs_callback_down(void)
162 lock_kernel();
163 mutex_lock(&nfs_callback_mutex);
164 nfs_callback_info.users--;
165 do {
166 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
167 break;
168 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
169 break;
170 } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
171 mutex_unlock(&nfs_callback_mutex);
172 unlock_kernel();
175 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
177 struct nfs_client *clp;
178 <<<<<<< HEAD:fs/nfs/callback.c
179 char buf[RPC_MAX_ADDRBUFLEN];
180 =======
181 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
182 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfs/callback.c
184 /* Don't talk to strangers */
185 clp = nfs_find_client(svc_addr(rqstp), 4);
186 if (clp == NULL)
187 return SVC_DROP;
189 dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__,
190 svc_print_addr(rqstp, buf, sizeof(buf)));
191 nfs_put_client(clp);
193 switch (rqstp->rq_authop->flavour) {
194 case RPC_AUTH_NULL:
195 if (rqstp->rq_proc != CB_NULL)
196 return SVC_DENIED;
197 break;
198 case RPC_AUTH_UNIX:
199 break;
200 case RPC_AUTH_GSS:
201 /* FIXME: RPCSEC_GSS handling? */
202 default:
203 return SVC_DENIED;
205 return SVC_OK;
209 * Define NFS4 callback program
211 static struct svc_version *nfs4_callback_version[] = {
212 [1] = &nfs4_callback_version1,
215 static struct svc_stat nfs4_callback_stats;
217 static struct svc_program nfs4_callback_program = {
218 .pg_prog = NFS4_CALLBACK, /* RPC service number */
219 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
220 .pg_vers = nfs4_callback_version, /* version table */
221 .pg_name = "NFSv4 callback", /* service name */
222 .pg_class = "nfs", /* authentication class */
223 .pg_stats = &nfs4_callback_stats,
224 .pg_authenticate = nfs_callback_authenticate,