blob: 2c042f8d70b5835a33b4b7992e6f1d10bda43b02 [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
9#include <linux/config.h>
10#include <linux/completion.h>
11#include <linux/ip.h>
12#include <linux/module.h>
13#include <linux/smp_lock.h>
14#include <linux/sunrpc/svc.h>
15#include <linux/sunrpc/svcsock.h>
16#include <linux/nfs_fs.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020017
18#include <net/inet_sock.h>
19
Trond Myklebust4ce79712005-06-22 17:16:21 +000020#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "callback.h"
22
23#define NFSDBG_FACILITY NFSDBG_CALLBACK
24
25struct nfs_callback_data {
26 unsigned int users;
27 struct svc_serv *serv;
28 pid_t pid;
29 struct completion started;
30 struct completion stopped;
31};
32
33static struct nfs_callback_data nfs_callback_info;
34static DECLARE_MUTEX(nfs_callback_sema);
35static struct svc_program nfs4_callback_program;
36
Trond Myklebusta72b4422006-01-03 09:55:41 +010037unsigned int nfs_callback_set_tcpport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038unsigned short nfs_callback_tcpport;
39
40/*
41 * This is the callback kernel thread.
42 */
43static void nfs_callback_svc(struct svc_rqst *rqstp)
44{
45 struct svc_serv *serv = rqstp->rq_server;
46 int err;
47
48 __module_get(THIS_MODULE);
49 lock_kernel();
50
51 nfs_callback_info.pid = current->pid;
52 daemonize("nfsv4-svc");
53 /* Process request with signals blocked, but allow SIGKILL. */
54 allow_signal(SIGKILL);
55
56 complete(&nfs_callback_info.started);
57
58 while (nfs_callback_info.users != 0 || !signalled()) {
59 /*
60 * Listen for a request on the socket
61 */
62 err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT);
63 if (err == -EAGAIN || err == -EINTR)
64 continue;
65 if (err < 0) {
66 printk(KERN_WARNING
67 "%s: terminating on error %d\n",
68 __FUNCTION__, -err);
69 break;
70 }
71 dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__,
72 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr));
73 svc_process(serv, rqstp);
74 }
75
Trond Myklebustf25bc342006-03-20 13:44:46 -050076 svc_exit_thread(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nfs_callback_info.pid = 0;
78 complete(&nfs_callback_info.stopped);
79 unlock_kernel();
80 module_put_and_exit(0);
81}
82
83/*
84 * Bring up the server process if it is not already up.
85 */
86int nfs_callback_up(void)
87{
88 struct svc_serv *serv;
89 struct svc_sock *svsk;
90 int ret = 0;
91
92 lock_kernel();
93 down(&nfs_callback_sema);
94 if (nfs_callback_info.users++ || nfs_callback_info.pid != 0)
95 goto out;
96 init_completion(&nfs_callback_info.started);
97 init_completion(&nfs_callback_info.stopped);
98 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE);
99 ret = -ENOMEM;
100 if (!serv)
101 goto out_err;
102 /* FIXME: We don't want to register this socket with the portmapper */
Trond Myklebusta72b4422006-01-03 09:55:41 +0100103 ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (ret < 0)
105 goto out_destroy;
106 if (!list_empty(&serv->sv_permsocks)) {
107 svsk = list_entry(serv->sv_permsocks.next,
108 struct svc_sock, sk_list);
109 nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport);
110 dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport);
111 } else
112 BUG();
113 ret = svc_create_thread(nfs_callback_svc, serv);
114 if (ret < 0)
115 goto out_destroy;
116 nfs_callback_info.serv = serv;
117 wait_for_completion(&nfs_callback_info.started);
118out:
119 up(&nfs_callback_sema);
120 unlock_kernel();
121 return ret;
122out_destroy:
123 svc_destroy(serv);
124out_err:
125 nfs_callback_info.users--;
126 goto out;
127}
128
129/*
130 * Kill the server process if it is not already up.
131 */
132int nfs_callback_down(void)
133{
134 int ret = 0;
135
136 lock_kernel();
137 down(&nfs_callback_sema);
138 if (--nfs_callback_info.users || nfs_callback_info.pid == 0)
139 goto out;
140 kill_proc(nfs_callback_info.pid, SIGKILL, 1);
141 wait_for_completion(&nfs_callback_info.stopped);
142out:
143 up(&nfs_callback_sema);
144 unlock_kernel();
145 return ret;
146}
147
148static int nfs_callback_authenticate(struct svc_rqst *rqstp)
149{
150 struct in_addr *addr = &rqstp->rq_addr.sin_addr;
151 struct nfs4_client *clp;
152
153 /* Don't talk to strangers */
154 clp = nfs4_find_client(addr);
155 if (clp == NULL)
156 return SVC_DROP;
157 dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr));
158 nfs4_put_client(clp);
159 switch (rqstp->rq_authop->flavour) {
160 case RPC_AUTH_NULL:
161 if (rqstp->rq_proc != CB_NULL)
162 return SVC_DENIED;
163 break;
164 case RPC_AUTH_UNIX:
165 break;
166 case RPC_AUTH_GSS:
167 /* FIXME: RPCSEC_GSS handling? */
168 default:
169 return SVC_DENIED;
170 }
171 return SVC_OK;
172}
173
174/*
175 * Define NFS4 callback program
176 */
177extern struct svc_version nfs4_callback_version1;
178
179static struct svc_version *nfs4_callback_version[] = {
180 [1] = &nfs4_callback_version1,
181};
182
183static struct svc_stat nfs4_callback_stats;
184
185static struct svc_program nfs4_callback_program = {
186 .pg_prog = NFS4_CALLBACK, /* RPC service number */
187 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
188 .pg_vers = nfs4_callback_version, /* version table */
189 .pg_name = "NFSv4 callback", /* service name */
190 .pg_class = "nfs", /* authentication class */
191 .pg_stats = &nfs4_callback_stats,
192 .pg_authenticate = nfs_callback_authenticate,
193};