blob: d68cd056b281b4ecde7830e2641168cca63d52b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/time.h>
14#include <linux/errno.h>
15#include <linux/nfs.h>
16#include <linux/in.h>
17#include <linux/uio.h>
18#include <linux/unistd.h>
19#include <linux/slab.h>
20#include <linux/smp.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070021#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/fs_struct.h>
Jeff Layton9867d762008-06-10 08:40:38 -040023#include <linux/kthread.h>
Andy Adamsonc3d06f92009-04-03 08:28:18 +030024#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <linux/sunrpc/types.h>
27#include <linux/sunrpc/stats.h>
28#include <linux/sunrpc/svc.h>
29#include <linux/sunrpc/svcsock.h>
30#include <linux/sunrpc/cache.h>
31#include <linux/nfsd/nfsd.h>
32#include <linux/nfsd/stats.h>
33#include <linux/nfsd/cache.h>
NeilBrown70c3b762005-11-07 01:00:25 -080034#include <linux/nfsd/syscall.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/lockd/bind.h>
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000036#include <linux/nfsacl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define NFSDDBG_FACILITY NFSDDBG_SVC
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040extern struct svc_program nfsd_program;
Jeff Layton9867d762008-06-10 08:40:38 -040041static int nfsd(void *vrqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct timeval nfssvc_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Neil Brownbedbdd82008-06-10 08:40:35 -040044/*
45 * nfsd_mutex protects nfsd_serv -- both the pointer itself and the members
46 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
47 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
48 *
49 * If (out side the lock) nfsd_serv is non-NULL, then it must point to a
50 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
51 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
52 * entry of ->sv_pools[].
53 *
54 * Transitions of the thread count between zero and non-zero are of particular
55 * interest since the svc_serv needs to be created and initialized at that
56 * point, or freed.
Jeff Layton3dd98a32008-06-10 08:40:36 -040057 *
58 * Finally, the nfsd_mutex also protects some of the global variables that are
59 * accessed when nfsd starts and that are settable via the write_* routines in
60 * nfsctl.c. In particular:
61 *
62 * user_recovery_dirname
63 * user_lease_time
64 * nfsd_versions
Neil Brownbedbdd82008-06-10 08:40:35 -040065 */
66DEFINE_MUTEX(nfsd_mutex);
67struct svc_serv *nfsd_serv;
68
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040069/*
70 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
71 * nfsd_drc_max_pages limits the total amount of memory available for
72 * version 4.1 DRC caches.
73 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
74 */
75spinlock_t nfsd_drc_lock;
Andy Adamson0c193052009-07-27 19:09:19 -040076unsigned int nfsd_drc_max_mem;
77unsigned int nfsd_drc_mem_used;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040078
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080079#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
80static struct svc_stat nfsd_acl_svcstats;
81static struct svc_version * nfsd_acl_version[] = {
82 [2] = &nfsd_acl_version2,
83 [3] = &nfsd_acl_version3,
84};
85
86#define NFSD_ACL_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -080087#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080088static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
89
90static struct svc_program nfsd_acl_program = {
91 .pg_prog = NFS_ACL_PROGRAM,
92 .pg_nvers = NFSD_ACL_NRVERS,
93 .pg_vers = nfsd_acl_versions,
NeilBrown1a8eff62007-01-26 00:56:58 -080094 .pg_name = "nfsacl",
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080095 .pg_class = "nfsd",
96 .pg_stats = &nfsd_acl_svcstats,
97 .pg_authenticate = &svc_set_client,
98};
99
100static struct svc_stat nfsd_acl_svcstats = {
101 .program = &nfsd_acl_program,
102};
103#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
104
NeilBrown70c3b762005-11-07 01:00:25 -0800105static struct svc_version * nfsd_version[] = {
106 [2] = &nfsd_version2,
107#if defined(CONFIG_NFSD_V3)
108 [3] = &nfsd_version3,
109#endif
110#if defined(CONFIG_NFSD_V4)
111 [4] = &nfsd_version4,
112#endif
113};
114
115#define NFSD_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -0800116#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
NeilBrown70c3b762005-11-07 01:00:25 -0800117static struct svc_version *nfsd_versions[NFSD_NRVERS];
118
119struct svc_program nfsd_program = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800120#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
121 .pg_next = &nfsd_acl_program,
122#endif
NeilBrown70c3b762005-11-07 01:00:25 -0800123 .pg_prog = NFS_PROGRAM, /* program number */
124 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
125 .pg_vers = nfsd_versions, /* version table */
126 .pg_name = "nfsd", /* program name */
127 .pg_class = "nfsd", /* authentication class */
128 .pg_stats = &nfsd_svcstats, /* version table */
129 .pg_authenticate = &svc_set_client, /* export authentication */
130
131};
132
Benny Halevy8daf2202009-04-03 08:28:59 +0300133u32 nfsd_supported_minorversion;
134
NeilBrown6658d3a2006-10-02 02:17:46 -0700135int nfsd_vers(int vers, enum vers_op change)
136{
137 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
138 return -1;
139 switch(change) {
140 case NFSD_SET:
141 nfsd_versions[vers] = nfsd_version[vers];
NeilBrown6658d3a2006-10-02 02:17:46 -0700142#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
143 if (vers < NFSD_ACL_NRVERS)
NeilBrown1a8eff62007-01-26 00:56:58 -0800144 nfsd_acl_versions[vers] = nfsd_acl_version[vers];
NeilBrown6658d3a2006-10-02 02:17:46 -0700145#endif
NeilBrown1a8eff62007-01-26 00:56:58 -0800146 break;
NeilBrown6658d3a2006-10-02 02:17:46 -0700147 case NFSD_CLEAR:
148 nfsd_versions[vers] = NULL;
149#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
150 if (vers < NFSD_ACL_NRVERS)
NeilBrown1a8eff62007-01-26 00:56:58 -0800151 nfsd_acl_versions[vers] = NULL;
NeilBrown6658d3a2006-10-02 02:17:46 -0700152#endif
153 break;
154 case NFSD_TEST:
155 return nfsd_versions[vers] != NULL;
156 case NFSD_AVAIL:
157 return nfsd_version[vers] != NULL;
158 }
159 return 0;
160}
Benny Halevy8daf2202009-04-03 08:28:59 +0300161
162int nfsd_minorversion(u32 minorversion, enum vers_op change)
163{
164 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION)
165 return -1;
166 switch(change) {
167 case NFSD_SET:
168 nfsd_supported_minorversion = minorversion;
169 break;
170 case NFSD_CLEAR:
171 if (minorversion == 0)
172 return -1;
173 nfsd_supported_minorversion = minorversion - 1;
174 break;
175 case NFSD_TEST:
176 return minorversion <= nfsd_supported_minorversion;
177 case NFSD_AVAIL:
178 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION;
179 }
180 return 0;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * Maximum number of nfsd processes
185 */
186#define NFSD_MAXSERVS 8192
187
188int nfsd_nrthreads(void)
189{
Neil Brownc7d106c2008-06-12 13:38:42 +1000190 int rv = 0;
191 mutex_lock(&nfsd_mutex);
192 if (nfsd_serv)
193 rv = nfsd_serv->sv_nrthreads;
194 mutex_unlock(&nfsd_mutex);
195 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
NeilBrownbc591cc2006-10-02 02:17:44 -0700198static void nfsd_last_thread(struct svc_serv *serv)
199{
200 /* When last nfsd thread exits we need to do some clean-up */
Tom Tucker7a182082007-12-30 21:07:53 -0600201 struct svc_xprt *xprt;
202 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
NeilBrown24e36662006-10-02 02:17:45 -0700203 lockd_down();
NeilBrownbc591cc2006-10-02 02:17:44 -0700204 nfsd_serv = NULL;
205 nfsd_racache_shutdown();
206 nfs4_state_shutdown();
207
Jeff Laytone096bbc2008-06-10 08:40:37 -0400208 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
209 "cache\n");
210 nfsd_export_flush();
NeilBrownbc591cc2006-10-02 02:17:44 -0700211}
NeilBrown6658d3a2006-10-02 02:17:46 -0700212
213void nfsd_reset_versions(void)
214{
215 int found_one = 0;
216 int i;
217
218 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
219 if (nfsd_program.pg_vers[i])
220 found_one = 1;
221 }
222
223 if (!found_one) {
224 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
225 nfsd_program.pg_vers[i] = nfsd_version[i];
226#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
227 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
228 nfsd_acl_program.pg_vers[i] =
229 nfsd_acl_version[i];
230#endif
231 }
232}
233
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300234/*
235 * Each session guarantees a negotiated per slot memory cache for replies
236 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
237 * NFSv4.1 server might want to use more memory for a DRC than a machine
238 * with mutiple services.
239 *
240 * Impose a hard limit on the number of pages for the DRC which varies
241 * according to the machines free pages. This is of course only a default.
242 *
243 * For now this is a #defined shift which could be under admin control
244 * in the future.
245 */
246static void set_max_drc(void)
247{
Andy Adamson6a14dd12009-07-27 19:06:45 -0400248 #define NFSD_DRC_SIZE_SHIFT 10
Andy Adamson0c193052009-07-27 19:09:19 -0400249 nfsd_drc_max_mem = (nr_free_buffer_pages()
250 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
251 nfsd_drc_mem_used = 0;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -0400252 spin_lock_init(&nfsd_drc_lock);
Andy Adamson0c193052009-07-27 19:09:19 -0400253 dprintk("%s nfsd_drc_max_mem %u \n", __func__, nfsd_drc_max_mem);
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300254}
Neil Brownbedbdd82008-06-10 08:40:35 -0400255
NeilBrownb41b66d2006-10-02 02:17:48 -0700256int nfsd_create_serv(void)
NeilBrown02a375f2006-10-02 02:17:46 -0700257{
258 int err = 0;
Neil Brownbedbdd82008-06-10 08:40:35 -0400259
260 WARN_ON(!mutex_is_locked(&nfsd_mutex));
NeilBrown02a375f2006-10-02 02:17:46 -0700261 if (nfsd_serv) {
Greg Banks9a24ab52006-10-02 02:17:58 -0700262 svc_get(nfsd_serv);
NeilBrown02a375f2006-10-02 02:17:46 -0700263 return 0;
264 }
NeilBrown596bbe52006-10-04 02:15:48 -0700265 if (nfsd_max_blksize == 0) {
266 /* choose a suitable default */
267 struct sysinfo i;
268 si_meminfo(&i);
269 /* Aim for 1/4096 of memory per thread
270 * This gives 1MB on 4Gig machines
271 * But only uses 32K on 128M machines.
272 * Bottom out at 8K on 32M and smaller.
273 * Of course, this is only a default.
274 */
275 nfsd_max_blksize = NFSSVC_MAXBLKSIZE;
NeilBrown44c55602006-10-04 02:16:15 -0700276 i.totalram <<= PAGE_SHIFT - 12;
NeilBrown596bbe52006-10-04 02:15:48 -0700277 while (nfsd_max_blksize > i.totalram &&
278 nfsd_max_blksize >= 8*1024*2)
279 nfsd_max_blksize /= 2;
280 }
NeilBrown02a375f2006-10-02 02:17:46 -0700281
Jeff Laytone096bbc2008-06-10 08:40:37 -0400282 nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
Jeff Laytona75c5d02008-06-10 08:40:39 -0400283 nfsd_last_thread, nfsd, THIS_MODULE);
NeilBrown02a375f2006-10-02 02:17:46 -0700284 if (nfsd_serv == NULL)
285 err = -ENOMEM;
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300286 else
287 set_max_drc();
Neil Brownbedbdd82008-06-10 08:40:35 -0400288
NeilBrown02a375f2006-10-02 02:17:46 -0700289 do_gettimeofday(&nfssvc_boot); /* record boot time */
290 return err;
291}
292
293static int nfsd_init_socks(int port)
294{
295 int error;
296 if (!list_empty(&nfsd_serv->sv_permsocks))
297 return 0;
298
Chuck Lever9652ada2009-03-18 20:46:21 -0400299 error = svc_create_xprt(nfsd_serv, "udp", PF_INET, port,
Chuck Lever482fb942007-02-12 00:53:29 -0800300 SVC_SOCK_DEFAULTS);
NeilBrown02a375f2006-10-02 02:17:46 -0700301 if (error < 0)
302 return error;
303
Chuck Lever1cd9cd12008-10-22 13:12:36 -0400304 error = lockd_up();
305 if (error < 0)
306 return error;
307
Chuck Lever9652ada2009-03-18 20:46:21 -0400308 error = svc_create_xprt(nfsd_serv, "tcp", PF_INET, port,
Chuck Lever482fb942007-02-12 00:53:29 -0800309 SVC_SOCK_DEFAULTS);
NeilBrown02a375f2006-10-02 02:17:46 -0700310 if (error < 0)
311 return error;
Chuck Lever26a41402008-10-03 17:15:30 -0400312
313 error = lockd_up();
314 if (error < 0)
315 return error;
316
NeilBrown02a375f2006-10-02 02:17:46 -0700317 return 0;
318}
319
Greg Bankseed29652006-10-02 02:18:02 -0700320int nfsd_nrpools(void)
321{
322 if (nfsd_serv == NULL)
323 return 0;
324 else
325 return nfsd_serv->sv_nrpools;
326}
327
328int nfsd_get_nrthreads(int n, int *nthreads)
329{
330 int i = 0;
331
332 if (nfsd_serv != NULL) {
333 for (i = 0; i < nfsd_serv->sv_nrpools && i < n; i++)
334 nthreads[i] = nfsd_serv->sv_pools[i].sp_nrthreads;
335 }
336
337 return 0;
338}
339
340int nfsd_set_nrthreads(int n, int *nthreads)
341{
342 int i = 0;
343 int tot = 0;
344 int err = 0;
345
Neil Brownbedbdd82008-06-10 08:40:35 -0400346 WARN_ON(!mutex_is_locked(&nfsd_mutex));
347
Greg Bankseed29652006-10-02 02:18:02 -0700348 if (nfsd_serv == NULL || n <= 0)
349 return 0;
350
351 if (n > nfsd_serv->sv_nrpools)
352 n = nfsd_serv->sv_nrpools;
353
354 /* enforce a global maximum number of threads */
355 tot = 0;
356 for (i = 0; i < n; i++) {
357 if (nthreads[i] > NFSD_MAXSERVS)
358 nthreads[i] = NFSD_MAXSERVS;
359 tot += nthreads[i];
360 }
361 if (tot > NFSD_MAXSERVS) {
362 /* total too large: scale down requested numbers */
363 for (i = 0; i < n && tot > 0; i++) {
364 int new = nthreads[i] * NFSD_MAXSERVS / tot;
365 tot -= (nthreads[i] - new);
366 nthreads[i] = new;
367 }
368 for (i = 0; i < n && tot > 0; i++) {
369 nthreads[i]--;
370 tot--;
371 }
372 }
373
374 /*
375 * There must always be a thread in pool 0; the admin
376 * can't shut down NFS completely using pool_threads.
377 */
378 if (nthreads[0] == 0)
379 nthreads[0] = 1;
380
381 /* apply the new numbers */
Greg Bankseed29652006-10-02 02:18:02 -0700382 svc_get(nfsd_serv);
383 for (i = 0; i < n; i++) {
384 err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
385 nthreads[i]);
386 if (err)
387 break;
388 }
389 svc_destroy(nfsd_serv);
Greg Bankseed29652006-10-02 02:18:02 -0700390
391 return err;
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394int
395nfsd_svc(unsigned short port, int nrservs)
396{
397 int error;
Neil Brownbedbdd82008-06-10 08:40:35 -0400398
399 mutex_lock(&nfsd_mutex);
NeilBrown6658d3a2006-10-02 02:17:46 -0700400 dprintk("nfsd: creating service\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (nrservs <= 0)
402 nrservs = 0;
403 if (nrservs > NFSD_MAXSERVS)
404 nrservs = NFSD_MAXSERVS;
NeilBrown671e1fc2009-06-16 11:03:20 +1000405 error = 0;
406 if (nrservs == 0 && nfsd_serv == NULL)
407 goto out;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Readahead param cache - will no-op if it already exists */
410 error = nfsd_racache_init(2*nrservs);
411 if (error<0)
412 goto out;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -0400413 nfs4_state_start();
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800414
NeilBrown02a375f2006-10-02 02:17:46 -0700415 nfsd_reset_versions();
416
417 error = nfsd_create_serv();
418
419 if (error)
420 goto out;
421 error = nfsd_init_socks(port);
422 if (error)
423 goto failure;
424
Greg Bankseec09662006-10-02 02:18:00 -0700425 error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
NeilBrown82e12fe2009-06-16 11:03:07 +1000426 if (error == 0)
427 /* We are holding a reference to nfsd_serv which
428 * we don't want to count in the return value,
429 * so subtract 1
430 */
431 error = nfsd_serv->sv_nrthreads - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 svc_destroy(nfsd_serv); /* Release server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 out:
Neil Brownbedbdd82008-06-10 08:40:35 -0400435 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return error;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/*
441 * This is the NFS server kernel thread
442 */
Jeff Layton9867d762008-06-10 08:40:38 -0400443static int
444nfsd(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Jeff Layton9867d762008-06-10 08:40:38 -0400446 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
Jeff Layton9867d762008-06-10 08:40:38 -0400447 int err, preverr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Lock module and set up kernel thread */
Neil Brownbedbdd82008-06-10 08:40:35 -0400450 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jeff Layton9867d762008-06-10 08:40:38 -0400452 /* At this point, the thread shares current->fs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * with the init process. We need to create files with a
454 * umask of 0 instead of init's umask. */
Al Viro3e93cd62009-03-29 19:00:13 -0400455 if (unshare_fs_struct() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk("Unable to start nfsd thread: out of memory\n");
457 goto out;
458 }
Al Viro3e93cd62009-03-29 19:00:13 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 current->fs->umask = 0;
461
Jeff Layton9867d762008-06-10 08:40:38 -0400462 /*
463 * thread is spawned with all signals set to SIG_IGN, re-enable
Jeff Layton100766f2008-06-30 14:09:46 -0400464 * the ones that will bring down the thread
Jeff Layton9867d762008-06-10 08:40:38 -0400465 */
Jeff Layton100766f2008-06-30 14:09:46 -0400466 allow_signal(SIGKILL);
467 allow_signal(SIGHUP);
468 allow_signal(SIGINT);
469 allow_signal(SIGQUIT);
Neil Brownbedbdd82008-06-10 08:40:35 -0400470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 nfsdstats.th_cnt++;
Neil Brownbedbdd82008-06-10 08:40:35 -0400472 mutex_unlock(&nfsd_mutex);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * We want less throttling in balance_dirty_pages() so that nfs to
476 * localhost doesn't cause nfsd to lock up due to all the client's
477 * dirty pages.
478 */
479 current->flags |= PF_LESS_THROTTLE;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700480 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * The main request loop
484 */
485 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Find a socket with data available and call its
488 * recvfrom routine.
489 */
NeilBrown6fb2b472006-10-02 02:17:50 -0700490 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 ;
Jeff Layton9867d762008-06-10 08:40:38 -0400492 if (err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
Jeff Layton9867d762008-06-10 08:40:38 -0400494 else if (err < 0) {
495 if (err != preverr) {
496 printk(KERN_WARNING "%s: unexpected error "
497 "from svc_recv (%d)\n", __func__, -err);
498 preverr = err;
499 }
500 schedule_timeout_uninterruptible(HZ);
501 continue;
502 }
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Lock the export hash tables for reading. */
506 exp_readlock();
507
NeilBrown6fb2b472006-10-02 02:17:50 -0700508 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* Unlock export hash tables */
511 exp_readunlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
NeilBrown24e36662006-10-02 02:17:45 -0700514 /* Clear signals before calling svc_exit_thread() */
NeilBrown9e4160522005-04-16 15:26:37 -0700515 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Neil Brownbedbdd82008-06-10 08:40:35 -0400517 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 nfsdstats.th_cnt --;
519
520out:
521 /* Release the thread */
522 svc_exit_thread(rqstp);
523
524 /* Release module */
Neil Brownbedbdd82008-06-10 08:40:35 -0400525 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 module_put_and_exit(0);
Jeff Layton9867d762008-06-10 08:40:38 -0400527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Andy Adamson32c1eb02007-07-17 04:04:48 -0700530static __be32 map_new_errors(u32 vers, __be32 nfserr)
531{
532 if (nfserr == nfserr_jukebox && vers == 2)
533 return nfserr_dropit;
534 if (nfserr == nfserr_wrongsec && vers < 4)
535 return nfserr_acces;
536 return nfserr;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539int
Al Viroc7afef12006-10-19 23:29:02 -0700540nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct svc_procedure *proc;
543 kxdrproc_t xdr;
Al Viroad451d32006-10-19 23:28:55 -0700544 __be32 nfserr;
545 __be32 *nfserrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 dprintk("nfsd_dispatch: vers %d proc %d\n",
548 rqstp->rq_vers, rqstp->rq_proc);
549 proc = rqstp->rq_procinfo;
550
551 /* Check whether we have this call in the cache. */
552 switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
553 case RC_INTR:
554 case RC_DROPIT:
555 return 0;
556 case RC_REPLY:
557 return 1;
558 case RC_DOIT:;
559 /* do it */
560 }
561
562 /* Decode arguments */
563 xdr = proc->pc_decode;
Al Viroad451d32006-10-19 23:28:55 -0700564 if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rqstp->rq_argp)) {
566 dprintk("nfsd: failed to decode arguments!\n");
567 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
568 *statp = rpc_garbage_args;
569 return 1;
570 }
571
572 /* need to grab the location to store the status, as
573 * nfsv4 does some encoding while processing
574 */
575 nfserrp = rqstp->rq_res.head[0].iov_base
576 + rqstp->rq_res.head[0].iov_len;
Al Viroad451d32006-10-19 23:28:55 -0700577 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Andy Adamson074fe892009-04-03 08:28:15 +0300579 /* NFSv4.1 DRC requires statp */
580 if (rqstp->rq_vers == 4)
581 nfsd4_set_statp(rqstp, statp);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Now call the procedure handler, and encode NFS status. */
584 nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
Andy Adamson32c1eb02007-07-17 04:04:48 -0700585 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (nfserr == nfserr_dropit) {
J. Bruce Fields45457e092007-06-22 17:26:32 -0400587 dprintk("nfsd: Dropping request; may be revisited later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
589 return 0;
590 }
591
592 if (rqstp->rq_proc != 0)
593 *nfserrp++ = nfserr;
594
595 /* Encode result.
596 * For NFSv2, additional info is never returned in case of an error.
597 */
598 if (!(nfserr && rqstp->rq_vers == 2)) {
599 xdr = proc->pc_encode;
600 if (xdr && !xdr(rqstp, nfserrp,
601 rqstp->rq_resp)) {
602 /* Failed to encode result. Release cache entry */
603 dprintk("nfsd: failed to encode result!\n");
604 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
605 *statp = rpc_system_err;
606 return 1;
607 }
608 }
609
610 /* Store reply in cache. */
611 nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
612 return 1;
613}
Greg Banks03cf6c92009-01-13 21:26:36 +1100614
615int nfsd_pool_stats_open(struct inode *inode, struct file *file)
616{
617 if (nfsd_serv == NULL)
618 return -ENODEV;
619 return svc_pool_stats_open(nfsd_serv, file);
620}