a070109fa6c702d101841d44da7d372c87dac318
[linux-2.6.git] / fs / nfs / callback.c
1 /*
2  * linux/fs/nfs/callback.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback handling
7  */
8
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
18 #include <net/inet_sock.h>
19
20 #include "nfs4_fs.h"
21 #include "callback.h"
22 #include "internal.h"
23
24 #define NFSDBG_FACILITY NFSDBG_CALLBACK
25
26 struct nfs_callback_data {
27         unsigned int users;
28         struct svc_serv *serv;
29         pid_t pid;
30         struct completion started;
31         struct completion stopped;
32 };
33
34 static struct nfs_callback_data nfs_callback_info;
35 static DEFINE_MUTEX(nfs_callback_mutex);
36 static struct svc_program nfs4_callback_program;
37
38 unsigned int nfs_callback_set_tcpport;
39 unsigned short nfs_callback_tcpport;
40 static const int nfs_set_port_min = 0;
41 static const int nfs_set_port_max = 65535;
42
43 static 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
53 module_param_call(callback_tcpport, param_set_port, param_get_int,
54                  &nfs_callback_set_tcpport, 0644);
55
56 /*
57  * This is the callback kernel thread.
58  */
59 static void nfs_callback_svc(struct svc_rqst *rqstp)
60 {
61         int err;
62
63         __module_get(THIS_MODULE);
64         lock_kernel();
65
66         nfs_callback_info.pid = current->pid;
67         daemonize("nfsv4-svc");
68         /* Process request with signals blocked, but allow SIGKILL.  */
69         allow_signal(SIGKILL);
70
71         complete(&nfs_callback_info.started);
72
73         for(;;) {
74                 if (signalled()) {
75                         if (nfs_callback_info.users == 0)
76                                 break;
77                         flush_signals(current);
78                 }
79                 /*
80                  * Listen for a request on the socket
81                  */
82                 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
83                 if (err == -EAGAIN || err == -EINTR)
84                         continue;
85                 if (err < 0) {
86                         printk(KERN_WARNING
87                                         "%s: terminating on error %d\n",
88                                         __FUNCTION__, -err);
89                         break;
90                 }
91                 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
92                                 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
93                 svc_process(rqstp);
94         }
95
96         svc_exit_thread(rqstp);
97         nfs_callback_info.pid = 0;
98         complete(&nfs_callback_info.stopped);
99         unlock_kernel();
100         module_put_and_exit(0);
101 }
102
103 /*
104  * Bring up the server process if it is not already up.
105  */
106 int nfs_callback_up(void)
107 {
108         struct svc_serv *serv;
109         int ret = 0;
110
111         lock_kernel();
112         mutex_lock(&nfs_callback_mutex);
113         if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
114                 goto out;
115         init_completion(&nfs_callback_info.started);
116         init_completion(&nfs_callback_info.stopped);
117         serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
118         ret = -ENOMEM;
119         if (!serv)
120                 goto out_err;
121
122         ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport,
123                                                         SVC_SOCK_ANONYMOUS);
124         if (ret <= 0)
125                 goto out_destroy;
126         nfs_callback_tcpport = ret;
127         dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
128
129         ret = svc_create_thread(nfs_callback_svc, serv);
130         if (ret < 0)
131                 goto out_destroy;
132         nfs_callback_info.serv = serv;
133         wait_for_completion(&nfs_callback_info.started);
134 out:
135         mutex_unlock(&nfs_callback_mutex);
136         unlock_kernel();
137         return ret;
138 out_destroy:
139         dprintk("Couldn't create callback socket or server thread; err = %d\n",
140                 ret);
141         svc_destroy(serv);
142 out_err:
143         nfs_callback_info.users--;
144         goto out;
145 }
146
147 /*
148  * Kill the server process if it is not already up.
149  */
150 void nfs_callback_down(void)
151 {
152         lock_kernel();
153         mutex_lock(&nfs_callback_mutex);
154         nfs_callback_info.users--;
155         do {
156                 if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0)
157                         break;
158                 if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0)
159                         break;
160         } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0);
161         mutex_unlock(&nfs_callback_mutex);
162         unlock_kernel();
163 }
164
165 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
166 {
167         struct sockaddr_in *addr = &rqstp->rq_addr;
168         struct nfs_client *clp;
169
170         /* Don't talk to strangers */
171         clp = nfs_find_client(addr, 4);
172         if (clp == NULL)
173                 return SVC_DROP;
174         dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr->sin_addr));
175         nfs_put_client(clp);
176         switch (rqstp->rq_authop->flavour) {
177                 case RPC_AUTH_NULL:
178                         if (rqstp->rq_proc != CB_NULL)
179                                 return SVC_DENIED;
180                         break;
181                 case RPC_AUTH_UNIX:
182                         break;
183                 case RPC_AUTH_GSS:
184                         /* FIXME: RPCSEC_GSS handling? */
185                 default:
186                         return SVC_DENIED;
187         }
188         return SVC_OK;
189 }
190
191 /*
192  * Define NFS4 callback program
193  */
194 static struct svc_version *nfs4_callback_version[] = {
195         [1] = &nfs4_callback_version1,
196 };
197
198 static struct svc_stat nfs4_callback_stats;
199
200 static struct svc_program nfs4_callback_program = {
201         .pg_prog = NFS4_CALLBACK,                       /* RPC service number */
202         .pg_nvers = ARRAY_SIZE(nfs4_callback_version),  /* Number of entries */
203         .pg_vers = nfs4_callback_version,               /* version table */
204         .pg_name = "NFSv4 callback",                    /* service name */
205         .pg_class = "nfs",                              /* authentication class */
206         .pg_stats = &nfs4_callback_stats,
207         .pg_authenticate = nfs_callback_authenticate,
208 };