pci-acpi: add flag to indicate query had been done
[linux-2.6/mini2440.git] / fs / nfs / callback.c
blobc1e7c83006292c5085d2b2cb5b54f6d46fb0e2fa
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>
18 #include <linux/kthread.h>
20 #include <net/inet_sock.h>
22 #include "nfs4_fs.h"
23 #include "callback.h"
24 #include "internal.h"
26 #define NFSDBG_FACILITY NFSDBG_CALLBACK
28 struct nfs_callback_data {
29 unsigned int users;
30 struct svc_serv *serv;
31 struct task_struct *task;
34 static struct nfs_callback_data nfs_callback_info;
35 static DEFINE_MUTEX(nfs_callback_mutex);
36 static struct svc_program nfs4_callback_program;
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;
43 static int param_set_port(const char *val, struct kernel_param *kp)
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;
53 module_param_call(callback_tcpport, param_set_port, param_get_int,
54 &nfs_callback_set_tcpport, 0644);
57 * This is the callback kernel thread.
59 static int
60 nfs_callback_svc(void *vrqstp)
62 int err, preverr = 0;
63 struct svc_rqst *rqstp = vrqstp;
65 set_freezable();
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.
71 lock_kernel();
72 while (!kthread_should_stop()) {
74 * Listen for a request on the socket
76 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
77 if (err == -EAGAIN || err == -EINTR) {
78 preverr = err;
79 continue;
81 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;
87 schedule_timeout_uninterruptible(HZ);
88 continue;
90 preverr = err;
91 svc_process(rqstp);
93 unlock_kernel();
94 nfs_callback_info.task = NULL;
95 svc_exit_thread(rqstp);
96 return 0;
100 * Bring up the server process if it is not already up.
102 int nfs_callback_up(void)
104 struct svc_serv *serv = NULL;
105 struct svc_rqst *rqstp;
106 int ret = 0;
108 lock_kernel();
109 mutex_lock(&nfs_callback_mutex);
110 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
111 goto out;
112 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
113 ret = -ENOMEM;
114 if (!serv)
115 goto out_err;
117 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
118 SVC_SOCK_ANONYMOUS);
119 if (ret <= 0)
120 goto out_err;
121 nfs_callback_tcpport = ret;
122 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
124 rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
125 if (IS_ERR(rqstp)) {
126 ret = PTR_ERR(rqstp);
127 goto out_err;
130 svc_sock_update_bufs(serv);
131 nfs_callback_info.serv = serv;
133 nfs_callback_info.task = kthread_run(nfs_callback_svc, rqstp,
134 "nfsv4-svc");
135 if (IS_ERR(nfs_callback_info.task)) {
136 ret = PTR_ERR(nfs_callback_info.task);
137 nfs_callback_info.serv = NULL;
138 nfs_callback_info.task = NULL;
139 svc_exit_thread(rqstp);
140 goto out_err;
142 out:
144 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
145 * svc_prepare_thread increments that. So we need to call svc_destroy
146 * on both success and failure so that the refcount is 1 when the
147 * thread exits.
149 if (serv)
150 svc_destroy(serv);
151 mutex_unlock(&nfs_callback_mutex);
152 unlock_kernel();
153 return ret;
154 out_err:
155 dprintk("Couldn't create callback socket or server thread; err = %d\n",
156 ret);
157 nfs_callback_info.users--;
158 goto out;
162 * Kill the server process if it is not already down.
164 void nfs_callback_down(void)
166 lock_kernel();
167 mutex_lock(&nfs_callback_mutex);
168 nfs_callback_info.users--;
169 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL)
170 kthread_stop(nfs_callback_info.task);
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 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
180 /* Don't talk to strangers */
181 clp = nfs_find_client(svc_addr(rqstp), 4);
182 if (clp == NULL)
183 return SVC_DROP;
185 dprintk("%s: %s NFSv4 callback!\n", __func__,
186 svc_print_addr(rqstp, buf, sizeof(buf)));
187 nfs_put_client(clp);
189 switch (rqstp->rq_authop->flavour) {
190 case RPC_AUTH_NULL:
191 if (rqstp->rq_proc != CB_NULL)
192 return SVC_DENIED;
193 break;
194 case RPC_AUTH_UNIX:
195 break;
196 case RPC_AUTH_GSS:
197 /* FIXME: RPCSEC_GSS handling? */
198 default:
199 return SVC_DENIED;
201 return SVC_OK;
205 * Define NFS4 callback program
207 static struct svc_version *nfs4_callback_version[] = {
208 [1] = &nfs4_callback_version1,
211 static struct svc_stat nfs4_callback_stats;
213 static struct svc_program nfs4_callback_program = {
214 .pg_prog = NFS4_CALLBACK, /* RPC service number */
215 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
216 .pg_vers = nfs4_callback_version, /* version table */
217 .pg_name = "NFSv4 callback", /* service name */
218 .pg_class = "nfs", /* authentication class */
219 .pg_stats = &nfs4_callback_stats,
220 .pg_authenticate = nfs_callback_authenticate,