diff options
Diffstat (limited to 'fs/nfsd/nfssvc.c')
-rw-r--r-- | fs/nfsd/nfssvc.c | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c new file mode 100644 index 000000000000..39551657e656 --- /dev/null +++ b/fs/nfsd/nfssvc.c | |||
@@ -0,0 +1,385 @@ | |||
1 | /* | ||
2 | * linux/fs/nfsd/nfssvc.c | ||
3 | * | ||
4 | * Central processing for nfsd. | ||
5 | * | ||
6 | * Authors: Olaf Kirch (okir@monad.swb.de) | ||
7 | * | ||
8 | * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> | ||
9 | */ | ||
10 | |||
11 | #include <linux/config.h> | ||
12 | #include <linux/module.h> | ||
13 | |||
14 | #include <linux/time.h> | ||
15 | #include <linux/errno.h> | ||
16 | #include <linux/nfs.h> | ||
17 | #include <linux/in.h> | ||
18 | #include <linux/uio.h> | ||
19 | #include <linux/unistd.h> | ||
20 | #include <linux/slab.h> | ||
21 | #include <linux/smp.h> | ||
22 | #include <linux/smp_lock.h> | ||
23 | #include <linux/fs_struct.h> | ||
24 | |||
25 | #include <linux/sunrpc/types.h> | ||
26 | #include <linux/sunrpc/stats.h> | ||
27 | #include <linux/sunrpc/svc.h> | ||
28 | #include <linux/sunrpc/svcsock.h> | ||
29 | #include <linux/sunrpc/cache.h> | ||
30 | #include <linux/nfsd/nfsd.h> | ||
31 | #include <linux/nfsd/stats.h> | ||
32 | #include <linux/nfsd/cache.h> | ||
33 | #include <linux/lockd/bind.h> | ||
34 | |||
35 | #define NFSDDBG_FACILITY NFSDDBG_SVC | ||
36 | |||
37 | /* these signals will be delivered to an nfsd thread | ||
38 | * when handling a request | ||
39 | */ | ||
40 | #define ALLOWED_SIGS (sigmask(SIGKILL)) | ||
41 | /* these signals will be delivered to an nfsd thread | ||
42 | * when not handling a request. i.e. when waiting | ||
43 | */ | ||
44 | #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT)) | ||
45 | /* if the last thread dies with SIGHUP, then the exports table is | ||
46 | * left unchanged ( like 2.4-{0-9} ). Any other signal will clear | ||
47 | * the exports table (like 2.2). | ||
48 | */ | ||
49 | #define SIG_NOCLEAN SIGHUP | ||
50 | |||
51 | extern struct svc_program nfsd_program; | ||
52 | static void nfsd(struct svc_rqst *rqstp); | ||
53 | struct timeval nfssvc_boot; | ||
54 | static struct svc_serv *nfsd_serv; | ||
55 | static atomic_t nfsd_busy; | ||
56 | static unsigned long nfsd_last_call; | ||
57 | static DEFINE_SPINLOCK(nfsd_call_lock); | ||
58 | |||
59 | struct nfsd_list { | ||
60 | struct list_head list; | ||
61 | struct task_struct *task; | ||
62 | }; | ||
63 | static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list); | ||
64 | |||
65 | /* | ||
66 | * Maximum number of nfsd processes | ||
67 | */ | ||
68 | #define NFSD_MAXSERVS 8192 | ||
69 | |||
70 | int nfsd_nrthreads(void) | ||
71 | { | ||
72 | if (nfsd_serv == NULL) | ||
73 | return 0; | ||
74 | else | ||
75 | return nfsd_serv->sv_nrthreads; | ||
76 | } | ||
77 | |||
78 | int | ||
79 | nfsd_svc(unsigned short port, int nrservs) | ||
80 | { | ||
81 | int error; | ||
82 | int none_left; | ||
83 | struct list_head *victim; | ||
84 | |||
85 | lock_kernel(); | ||
86 | dprintk("nfsd: creating service\n"); | ||
87 | error = -EINVAL; | ||
88 | if (nrservs <= 0) | ||
89 | nrservs = 0; | ||
90 | if (nrservs > NFSD_MAXSERVS) | ||
91 | nrservs = NFSD_MAXSERVS; | ||
92 | |||
93 | /* Readahead param cache - will no-op if it already exists */ | ||
94 | error = nfsd_racache_init(2*nrservs); | ||
95 | if (error<0) | ||
96 | goto out; | ||
97 | error = nfs4_state_init(); | ||
98 | if (error<0) | ||
99 | goto out; | ||
100 | if (!nfsd_serv) { | ||
101 | atomic_set(&nfsd_busy, 0); | ||
102 | error = -ENOMEM; | ||
103 | nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE); | ||
104 | if (nfsd_serv == NULL) | ||
105 | goto out; | ||
106 | error = svc_makesock(nfsd_serv, IPPROTO_UDP, port); | ||
107 | if (error < 0) | ||
108 | goto failure; | ||
109 | |||
110 | #ifdef CONFIG_NFSD_TCP | ||
111 | error = svc_makesock(nfsd_serv, IPPROTO_TCP, port); | ||
112 | if (error < 0) | ||
113 | goto failure; | ||
114 | #endif | ||
115 | do_gettimeofday(&nfssvc_boot); /* record boot time */ | ||
116 | } else | ||
117 | nfsd_serv->sv_nrthreads++; | ||
118 | nrservs -= (nfsd_serv->sv_nrthreads-1); | ||
119 | while (nrservs > 0) { | ||
120 | nrservs--; | ||
121 | __module_get(THIS_MODULE); | ||
122 | error = svc_create_thread(nfsd, nfsd_serv); | ||
123 | if (error < 0) { | ||
124 | module_put(THIS_MODULE); | ||
125 | break; | ||
126 | } | ||
127 | } | ||
128 | victim = nfsd_list.next; | ||
129 | while (nrservs < 0 && victim != &nfsd_list) { | ||
130 | struct nfsd_list *nl = | ||
131 | list_entry(victim,struct nfsd_list, list); | ||
132 | victim = victim->next; | ||
133 | send_sig(SIG_NOCLEAN, nl->task, 1); | ||
134 | nrservs++; | ||
135 | } | ||
136 | failure: | ||
137 | none_left = (nfsd_serv->sv_nrthreads == 1); | ||
138 | svc_destroy(nfsd_serv); /* Release server */ | ||
139 | if (none_left) { | ||
140 | nfsd_serv = NULL; | ||
141 | nfsd_racache_shutdown(); | ||
142 | nfs4_state_shutdown(); | ||
143 | } | ||
144 | out: | ||
145 | unlock_kernel(); | ||
146 | return error; | ||
147 | } | ||
148 | |||
149 | static inline void | ||
150 | update_thread_usage(int busy_threads) | ||
151 | { | ||
152 | unsigned long prev_call; | ||
153 | unsigned long diff; | ||
154 | int decile; | ||
155 | |||
156 | spin_lock(&nfsd_call_lock); | ||
157 | prev_call = nfsd_last_call; | ||
158 | nfsd_last_call = jiffies; | ||
159 | decile = busy_threads*10/nfsdstats.th_cnt; | ||
160 | if (decile>0 && decile <= 10) { | ||
161 | diff = nfsd_last_call - prev_call; | ||
162 | if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP) | ||
163 | nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP; | ||
164 | if (decile == 10) | ||
165 | nfsdstats.th_fullcnt++; | ||
166 | } | ||
167 | spin_unlock(&nfsd_call_lock); | ||
168 | } | ||
169 | |||
170 | /* | ||
171 | * This is the NFS server kernel thread | ||
172 | */ | ||
173 | static void | ||
174 | nfsd(struct svc_rqst *rqstp) | ||
175 | { | ||
176 | struct svc_serv *serv = rqstp->rq_server; | ||
177 | struct fs_struct *fsp; | ||
178 | int err; | ||
179 | struct nfsd_list me; | ||
180 | sigset_t shutdown_mask, allowed_mask; | ||
181 | |||
182 | /* Lock module and set up kernel thread */ | ||
183 | lock_kernel(); | ||
184 | daemonize("nfsd"); | ||
185 | |||
186 | /* After daemonize() this kernel thread shares current->fs | ||
187 | * with the init process. We need to create files with a | ||
188 | * umask of 0 instead of init's umask. */ | ||
189 | fsp = copy_fs_struct(current->fs); | ||
190 | if (!fsp) { | ||
191 | printk("Unable to start nfsd thread: out of memory\n"); | ||
192 | goto out; | ||
193 | } | ||
194 | exit_fs(current); | ||
195 | current->fs = fsp; | ||
196 | current->fs->umask = 0; | ||
197 | |||
198 | siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS); | ||
199 | siginitsetinv(&allowed_mask, ALLOWED_SIGS); | ||
200 | |||
201 | nfsdstats.th_cnt++; | ||
202 | |||
203 | lockd_up(); /* start lockd */ | ||
204 | |||
205 | me.task = current; | ||
206 | list_add(&me.list, &nfsd_list); | ||
207 | |||
208 | unlock_kernel(); | ||
209 | |||
210 | /* | ||
211 | * We want less throttling in balance_dirty_pages() so that nfs to | ||
212 | * localhost doesn't cause nfsd to lock up due to all the client's | ||
213 | * dirty pages. | ||
214 | */ | ||
215 | current->flags |= PF_LESS_THROTTLE; | ||
216 | |||
217 | /* | ||
218 | * The main request loop | ||
219 | */ | ||
220 | for (;;) { | ||
221 | /* Block all but the shutdown signals */ | ||
222 | sigprocmask(SIG_SETMASK, &shutdown_mask, NULL); | ||
223 | |||
224 | /* | ||
225 | * Find a socket with data available and call its | ||
226 | * recvfrom routine. | ||
227 | */ | ||
228 | while ((err = svc_recv(serv, rqstp, | ||
229 | 60*60*HZ)) == -EAGAIN) | ||
230 | ; | ||
231 | if (err < 0) | ||
232 | break; | ||
233 | update_thread_usage(atomic_read(&nfsd_busy)); | ||
234 | atomic_inc(&nfsd_busy); | ||
235 | |||
236 | /* Lock the export hash tables for reading. */ | ||
237 | exp_readlock(); | ||
238 | |||
239 | /* Process request with signals blocked. */ | ||
240 | sigprocmask(SIG_SETMASK, &allowed_mask, NULL); | ||
241 | |||
242 | svc_process(serv, rqstp); | ||
243 | |||
244 | /* Unlock export hash tables */ | ||
245 | exp_readunlock(); | ||
246 | update_thread_usage(atomic_read(&nfsd_busy)); | ||
247 | atomic_dec(&nfsd_busy); | ||
248 | } | ||
249 | |||
250 | if (err != -EINTR) { | ||
251 | printk(KERN_WARNING "nfsd: terminating on error %d\n", -err); | ||
252 | } else { | ||
253 | unsigned int signo; | ||
254 | |||
255 | for (signo = 1; signo <= _NSIG; signo++) | ||
256 | if (sigismember(¤t->pending.signal, signo) && | ||
257 | !sigismember(¤t->blocked, signo)) | ||
258 | break; | ||
259 | err = signo; | ||
260 | } | ||
261 | |||
262 | lock_kernel(); | ||
263 | |||
264 | /* Release lockd */ | ||
265 | lockd_down(); | ||
266 | |||
267 | /* Check if this is last thread */ | ||
268 | if (serv->sv_nrthreads==1) { | ||
269 | |||
270 | printk(KERN_WARNING "nfsd: last server has exited\n"); | ||
271 | if (err != SIG_NOCLEAN) { | ||
272 | printk(KERN_WARNING "nfsd: unexporting all filesystems\n"); | ||
273 | nfsd_export_flush(); | ||
274 | } | ||
275 | nfsd_serv = NULL; | ||
276 | nfsd_racache_shutdown(); /* release read-ahead cache */ | ||
277 | nfs4_state_shutdown(); | ||
278 | } | ||
279 | list_del(&me.list); | ||
280 | nfsdstats.th_cnt --; | ||
281 | |||
282 | out: | ||
283 | /* Release the thread */ | ||
284 | svc_exit_thread(rqstp); | ||
285 | |||
286 | /* Release module */ | ||
287 | module_put_and_exit(0); | ||
288 | } | ||
289 | |||
290 | int | ||
291 | nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp) | ||
292 | { | ||
293 | struct svc_procedure *proc; | ||
294 | kxdrproc_t xdr; | ||
295 | u32 nfserr; | ||
296 | u32 *nfserrp; | ||
297 | |||
298 | dprintk("nfsd_dispatch: vers %d proc %d\n", | ||
299 | rqstp->rq_vers, rqstp->rq_proc); | ||
300 | proc = rqstp->rq_procinfo; | ||
301 | |||
302 | /* Check whether we have this call in the cache. */ | ||
303 | switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) { | ||
304 | case RC_INTR: | ||
305 | case RC_DROPIT: | ||
306 | return 0; | ||
307 | case RC_REPLY: | ||
308 | return 1; | ||
309 | case RC_DOIT:; | ||
310 | /* do it */ | ||
311 | } | ||
312 | |||
313 | /* Decode arguments */ | ||
314 | xdr = proc->pc_decode; | ||
315 | if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base, | ||
316 | rqstp->rq_argp)) { | ||
317 | dprintk("nfsd: failed to decode arguments!\n"); | ||
318 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); | ||
319 | *statp = rpc_garbage_args; | ||
320 | return 1; | ||
321 | } | ||
322 | |||
323 | /* need to grab the location to store the status, as | ||
324 | * nfsv4 does some encoding while processing | ||
325 | */ | ||
326 | nfserrp = rqstp->rq_res.head[0].iov_base | ||
327 | + rqstp->rq_res.head[0].iov_len; | ||
328 | rqstp->rq_res.head[0].iov_len += sizeof(u32); | ||
329 | |||
330 | /* Now call the procedure handler, and encode NFS status. */ | ||
331 | nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); | ||
332 | if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2) | ||
333 | nfserr = nfserr_dropit; | ||
334 | if (nfserr == nfserr_dropit) { | ||
335 | dprintk("nfsd: Dropping request due to malloc failure!\n"); | ||
336 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); | ||
337 | return 0; | ||
338 | } | ||
339 | |||
340 | if (rqstp->rq_proc != 0) | ||
341 | *nfserrp++ = nfserr; | ||
342 | |||
343 | /* Encode result. | ||
344 | * For NFSv2, additional info is never returned in case of an error. | ||
345 | */ | ||
346 | if (!(nfserr && rqstp->rq_vers == 2)) { | ||
347 | xdr = proc->pc_encode; | ||
348 | if (xdr && !xdr(rqstp, nfserrp, | ||
349 | rqstp->rq_resp)) { | ||
350 | /* Failed to encode result. Release cache entry */ | ||
351 | dprintk("nfsd: failed to encode result!\n"); | ||
352 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); | ||
353 | *statp = rpc_system_err; | ||
354 | return 1; | ||
355 | } | ||
356 | } | ||
357 | |||
358 | /* Store reply in cache. */ | ||
359 | nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1); | ||
360 | return 1; | ||
361 | } | ||
362 | |||
363 | extern struct svc_version nfsd_version2, nfsd_version3, nfsd_version4; | ||
364 | |||
365 | static struct svc_version * nfsd_version[] = { | ||
366 | [2] = &nfsd_version2, | ||
367 | #if defined(CONFIG_NFSD_V3) | ||
368 | [3] = &nfsd_version3, | ||
369 | #endif | ||
370 | #if defined(CONFIG_NFSD_V4) | ||
371 | [4] = &nfsd_version4, | ||
372 | #endif | ||
373 | }; | ||
374 | |||
375 | #define NFSD_NRVERS (sizeof(nfsd_version)/sizeof(nfsd_version[0])) | ||
376 | struct svc_program nfsd_program = { | ||
377 | .pg_prog = NFS_PROGRAM, /* program number */ | ||
378 | .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */ | ||
379 | .pg_vers = nfsd_version, /* version table */ | ||
380 | .pg_name = "nfsd", /* program name */ | ||
381 | .pg_class = "nfsd", /* authentication class */ | ||
382 | .pg_stats = &nfsd_svcstats, /* version table */ | ||
383 | .pg_authenticate = &svc_set_client, /* export authentication */ | ||
384 | |||
385 | }; | ||