nfsd: don't call nfsd_reply_cache_shutdown twice
[linux-2.6-block.git] / fs / nfsd / nfsctl.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
1da177e4
LT
3 * Syscall interface to knfsd.
4 *
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6 */
7
5a0e3ad6 8#include <linux/slab.h>
3f8206d4 9#include <linux/namei.h>
b41b66d6 10#include <linux/ctype.h>
1da177e4 11
80212d59 12#include <linux/sunrpc/svcsock.h>
4373ea84 13#include <linux/lockd/lockd.h>
5976687a 14#include <linux/sunrpc/addr.h>
b0b0c0a2 15#include <linux/sunrpc/gss_api.h>
b084f598 16#include <linux/sunrpc/gss_krb5_enctypes.h>
813fd320 17#include <linux/sunrpc/rpc_pipe_fs.h>
143cb494 18#include <linux/module.h>
1da177e4 19
2ca72e17 20#include "idmap.h"
9a74af21
BH
21#include "nfsd.h"
22#include "cache.h"
f3c7521f 23#include "state.h"
7ea34ac1 24#include "netns.h"
9cf514cc 25#include "pnfs.h"
9a74af21 26
1da177e4 27/*
b0b0c0a2 28 * We have a single directory with several nodes in it.
1da177e4
LT
29 */
30enum {
31 NFSD_Root = 1,
1da177e4 32 NFSD_List,
e8e8753f 33 NFSD_Export_features,
1da177e4 34 NFSD_Fh,
4373ea84 35 NFSD_FO_UnlockIP,
17efa372 36 NFSD_FO_UnlockFS,
1da177e4 37 NFSD_Threads,
eed2965a 38 NFSD_Pool_Threads,
03cf6c9f 39 NFSD_Pool_Stats,
a2f999a3 40 NFSD_Reply_Cache_Stats,
70c3b76c 41 NFSD_Versions,
80212d59 42 NFSD_Ports,
596bbe53 43 NFSD_MaxBlkSize,
5b8db00b 44 NFSD_MaxConnections,
b0b0c0a2 45 NFSD_SupportedEnctypes,
70c3b76c
N
46 /*
47 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
48 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
49 */
50#ifdef CONFIG_NFSD_V4
1da177e4 51 NFSD_Leasetime,
efc4bb4f 52 NFSD_Gracetime,
0964a3d3 53 NFSD_RecoveryDir,
7f5ef2e9 54 NFSD_V4EndGrace,
70c3b76c 55#endif
1da177e4
LT
56};
57
58/*
59 * write() for these nodes.
60 */
1da177e4 61static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
b046ccdc
CL
62static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
63static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
1da177e4 64static ssize_t write_threads(struct file *file, char *buf, size_t size);
eed2965a 65static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
70c3b76c 66static ssize_t write_versions(struct file *file, char *buf, size_t size);
80212d59 67static ssize_t write_ports(struct file *file, char *buf, size_t size);
596bbe53 68static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
5b8db00b 69static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
70c3b76c 70#ifdef CONFIG_NFSD_V4
1da177e4 71static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
efc4bb4f 72static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
0964a3d3 73static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
7f5ef2e9 74static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
70c3b76c 75#endif
1da177e4 76
c2cdc2ab 77static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
1da177e4 78 [NFSD_Fh] = write_filehandle,
b046ccdc
CL
79 [NFSD_FO_UnlockIP] = write_unlock_ip,
80 [NFSD_FO_UnlockFS] = write_unlock_fs,
1da177e4 81 [NFSD_Threads] = write_threads,
eed2965a 82 [NFSD_Pool_Threads] = write_pool_threads,
70c3b76c 83 [NFSD_Versions] = write_versions,
80212d59 84 [NFSD_Ports] = write_ports,
596bbe53 85 [NFSD_MaxBlkSize] = write_maxblksize,
5b8db00b 86 [NFSD_MaxConnections] = write_maxconn,
70c3b76c 87#ifdef CONFIG_NFSD_V4
1da177e4 88 [NFSD_Leasetime] = write_leasetime,
efc4bb4f 89 [NFSD_Gracetime] = write_gracetime,
0964a3d3 90 [NFSD_RecoveryDir] = write_recoverydir,
7f5ef2e9 91 [NFSD_V4EndGrace] = write_v4_end_grace,
70c3b76c 92#endif
1da177e4
LT
93};
94
95static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
96{
496ad9aa 97 ino_t ino = file_inode(file)->i_ino;
1da177e4
LT
98 char *data;
99 ssize_t rv;
100
e8c96f8c 101 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1da177e4
LT
102 return -EINVAL;
103
104 data = simple_transaction_get(file, buf, size);
105 if (IS_ERR(data))
106 return PTR_ERR(data);
107
108 rv = write_op[ino](file, data, size);
8971a101 109 if (rv >= 0) {
1da177e4
LT
110 simple_transaction_set(file, rv);
111 rv = size;
112 }
113 return rv;
114}
115
7390022d
N
116static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
117{
118 if (! file->private_data) {
119 /* An attempt to read a transaction file without writing
120 * causes a 0-byte write so that the file can return
121 * state information
122 */
123 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
124 if (rv < 0)
125 return rv;
126 }
127 return simple_transaction_read(file, buf, size, pos);
128}
129
4b6f5d20 130static const struct file_operations transaction_ops = {
1da177e4 131 .write = nfsctl_transaction_write,
7390022d 132 .read = nfsctl_transaction_read,
1da177e4 133 .release = simple_transaction_release,
6038f373 134 .llseek = default_llseek,
1da177e4
LT
135};
136
96d851c4 137static int exports_net_open(struct net *net, struct file *file)
1da177e4 138{
f2c7ea10
SK
139 int err;
140 struct seq_file *seq;
96d851c4 141 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
f2c7ea10
SK
142
143 err = seq_open(file, &nfs_exports_op);
144 if (err)
145 return err;
146
147 seq = file->private_data;
e5f06f72 148 seq->private = nn->svc_export_cache;
f2c7ea10 149 return 0;
1da177e4
LT
150}
151
96d851c4
SK
152static int exports_proc_open(struct inode *inode, struct file *file)
153{
154 return exports_net_open(current->nsproxy->net_ns, file);
155}
156
157static const struct file_operations exports_proc_operations = {
158 .open = exports_proc_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = seq_release,
96d851c4
SK
162};
163
164static int exports_nfsd_open(struct inode *inode, struct file *file)
165{
166 return exports_net_open(inode->i_sb->s_fs_info, file);
167}
168
169static const struct file_operations exports_nfsd_operations = {
170 .open = exports_nfsd_open,
1da177e4
LT
171 .read = seq_read,
172 .llseek = seq_lseek,
173 .release = seq_release,
174};
175
e8e8753f
BF
176static int export_features_show(struct seq_file *m, void *v)
177{
178 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
179 return 0;
180}
181
182static int export_features_open(struct inode *inode, struct file *file)
183{
184 return single_open(file, export_features_show, NULL);
185}
186
75ef9de1 187static const struct file_operations export_features_operations = {
e8e8753f
BF
188 .open = export_features_open,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .release = single_release,
192};
193
b084f598 194#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
b0b0c0a2
KC
195static int supported_enctypes_show(struct seq_file *m, void *v)
196{
b084f598 197 seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
b0b0c0a2
KC
198 return 0;
199}
200
201static int supported_enctypes_open(struct inode *inode, struct file *file)
202{
203 return single_open(file, supported_enctypes_show, NULL);
204}
205
75ef9de1 206static const struct file_operations supported_enctypes_ops = {
b0b0c0a2
KC
207 .open = supported_enctypes_open,
208 .read = seq_read,
209 .llseek = seq_lseek,
210 .release = single_release,
211};
b084f598 212#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
b0b0c0a2 213
828c0950 214static const struct file_operations pool_stats_operations = {
03cf6c9f
GB
215 .open = nfsd_pool_stats_open,
216 .read = seq_read,
217 .llseek = seq_lseek,
ed2d8aed 218 .release = nfsd_pool_stats_release,
03cf6c9f
GB
219};
220
7ba630f5 221static const struct file_operations reply_cache_stats_operations = {
a2f999a3
JL
222 .open = nfsd_reply_cache_stats_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
226};
227
1da177e4
LT
228/*----------------------------------------------------------------------------*/
229/*
230 * payload - write methods
1da177e4
LT
231 */
232
244c7d44
AV
233static inline struct net *netns(struct file *file)
234{
235 return file_inode(file)->i_sb->s_fs_info;
236}
1da177e4 237
262a0982
CL
238/**
239 * write_unlock_ip - Release all locks used by a client
240 *
241 * Experimental.
242 *
243 * Input:
244 * buf: '\n'-terminated C string containing a
4116092b 245 * presentation format IP address
262a0982
CL
246 * size: length of C string in @buf
247 * Output:
248 * On success: returns zero if all specified locks were released;
249 * returns one if one or more locks were not released
250 * On error: return code is negative errno value
262a0982 251 */
b046ccdc 252static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
4373ea84 253{
4116092b
CL
254 struct sockaddr_storage address;
255 struct sockaddr *sap = (struct sockaddr *)&address;
256 size_t salen = sizeof(address);
367c8c7b 257 char *fo_path;
244c7d44 258 struct net *net = netns(file);
4373ea84
WC
259
260 /* sanity check */
261 if (size == 0)
262 return -EINVAL;
263
264 if (buf[size-1] != '\n')
265 return -EINVAL;
266
267 fo_path = buf;
268 if (qword_get(&buf, fo_path, size) < 0)
269 return -EINVAL;
270
11f77942 271 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
4373ea84 272 return -EINVAL;
4373ea84 273
4116092b 274 return nlmsvc_unlock_all_by_ip(sap);
4373ea84
WC
275}
276
262a0982
CL
277/**
278 * write_unlock_fs - Release all locks on a local file system
279 *
280 * Experimental.
281 *
282 * Input:
283 * buf: '\n'-terminated C string containing the
284 * absolute pathname of a local file system
285 * size: length of C string in @buf
286 * Output:
287 * On success: returns zero if all specified locks were released;
288 * returns one if one or more locks were not released
289 * On error: return code is negative errno value
290 */
b046ccdc 291static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
17efa372 292{
a63bb996 293 struct path path;
17efa372
WC
294 char *fo_path;
295 int error;
296
297 /* sanity check */
298 if (size == 0)
299 return -EINVAL;
300
301 if (buf[size-1] != '\n')
302 return -EINVAL;
303
304 fo_path = buf;
305 if (qword_get(&buf, fo_path, size) < 0)
306 return -EINVAL;
307
a63bb996 308 error = kern_path(fo_path, 0, &path);
17efa372
WC
309 if (error)
310 return error;
311
262a0982
CL
312 /*
313 * XXX: Needs better sanity checking. Otherwise we could end up
314 * releasing locks on the wrong file system.
315 *
316 * For example:
317 * 1. Does the path refer to a directory?
318 * 2. Is that directory a mount point, or
319 * 3. Is that directory the root of an exported file system?
320 */
d8c9584e 321 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
17efa372 322
a63bb996 323 path_put(&path);
17efa372
WC
324 return error;
325}
326
262a0982
CL
327/**
328 * write_filehandle - Get a variable-length NFS file handle by path
329 *
330 * On input, the buffer contains a '\n'-terminated C string comprised of
331 * three alphanumeric words separated by whitespace. The string may
332 * contain escape sequences.
333 *
334 * Input:
335 * buf:
336 * domain: client domain name
337 * path: export pathname
338 * maxsize: numeric maximum size of
339 * @buf
340 * size: length of C string in @buf
341 * Output:
342 * On success: passed-in buffer filled with '\n'-terminated C
343 * string containing a ASCII hex text version
344 * of the NFS file handle;
345 * return code is the size in bytes of the string
346 * On error: return code is negative errno value
347 */
1da177e4
LT
348static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
349{
1da177e4 350 char *dname, *path;
246d95ba 351 int uninitialized_var(maxsize);
1da177e4
LT
352 char *mesg = buf;
353 int len;
354 struct auth_domain *dom;
355 struct knfsd_fh fh;
356
87d26ea7
BF
357 if (size == 0)
358 return -EINVAL;
359
1da177e4
LT
360 if (buf[size-1] != '\n')
361 return -EINVAL;
362 buf[size-1] = 0;
363
364 dname = mesg;
365 len = qword_get(&mesg, dname, size);
54224f04
CL
366 if (len <= 0)
367 return -EINVAL;
1da177e4
LT
368
369 path = dname+len+1;
370 len = qword_get(&mesg, path, size);
54224f04
CL
371 if (len <= 0)
372 return -EINVAL;
1da177e4
LT
373
374 len = get_int(&mesg, &maxsize);
375 if (len)
376 return len;
377
378 if (maxsize < NFS_FHSIZE)
379 return -EINVAL;
3c7aa15d 380 maxsize = min(maxsize, NFS3_FHSIZE);
1da177e4
LT
381
382 if (qword_get(&mesg, mesg, size)>0)
383 return -EINVAL;
384
385 /* we have all the words, they are in buf.. */
386 dom = unix_domain_find(dname);
387 if (!dom)
388 return -ENOMEM;
389
244c7d44 390 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
1da177e4
LT
391 auth_domain_put(dom);
392 if (len)
393 return len;
394
54224f04
CL
395 mesg = buf;
396 len = SIMPLE_TRANSACTION_LIMIT;
1da177e4
LT
397 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
398 mesg[-1] = '\n';
399 return mesg - buf;
400}
401
262a0982
CL
402/**
403 * write_threads - Start NFSD, or report the current number of running threads
404 *
405 * Input:
406 * buf: ignored
407 * size: zero
408 * Output:
409 * On success: passed-in buffer filled with '\n'-terminated C
410 * string numeric value representing the number of
411 * running NFSD threads;
412 * return code is the size in bytes of the string
413 * On error: return code is zero
414 *
415 * OR
416 *
417 * Input:
418 * buf: C string containing an unsigned
419 * integer value representing the
420 * number of NFSD threads to start
421 * size: non-zero length of C string in @buf
422 * Output:
423 * On success: NFS service is started;
424 * passed-in buffer filled with '\n'-terminated C
425 * string numeric value representing the number of
426 * running NFSD threads;
427 * return code is the size in bytes of the string
428 * On error: return code is zero or a negative errno value
429 */
1da177e4
LT
430static ssize_t write_threads(struct file *file, char *buf, size_t size)
431{
1da177e4
LT
432 char *mesg = buf;
433 int rv;
244c7d44 434 struct net *net = netns(file);
d41a9417 435
1da177e4
LT
436 if (size > 0) {
437 int newthreads;
438 rv = get_int(&mesg, &newthreads);
439 if (rv)
440 return rv;
9e074856 441 if (newthreads < 0)
1da177e4 442 return -EINVAL;
4df493a2 443 rv = nfsd_svc(newthreads, net, file->f_cred);
82e12fe9 444 if (rv < 0)
1da177e4 445 return rv;
82e12fe9 446 } else
9dd9845f 447 rv = nfsd_nrthreads(net);
e06b6405 448
82e12fe9 449 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
1da177e4
LT
450}
451
262a0982
CL
452/**
453 * write_pool_threads - Set or report the current number of threads per pool
454 *
455 * Input:
456 * buf: ignored
457 * size: zero
458 *
459 * OR
460 *
461 * Input:
462 * buf: C string containing whitespace-
463 * separated unsigned integer values
464 * representing the number of NFSD
465 * threads to start in each pool
466 * size: non-zero length of C string in @buf
467 * Output:
468 * On success: passed-in buffer filled with '\n'-terminated C
469 * string containing integer values representing the
470 * number of NFSD threads in each pool;
471 * return code is the size in bytes of the string
472 * On error: return code is zero or a negative errno value
473 */
eed2965a
GB
474static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
475{
476 /* if size > 0, look for an array of number of threads per node
477 * and apply them then write out number of threads per node as reply
478 */
479 char *mesg = buf;
480 int i;
481 int rv;
482 int len;
bedbdd8b 483 int npools;
eed2965a 484 int *nthreads;
244c7d44 485 struct net *net = netns(file);
eed2965a 486
bedbdd8b 487 mutex_lock(&nfsd_mutex);
9dd9845f 488 npools = nfsd_nrpools(net);
eed2965a
GB
489 if (npools == 0) {
490 /*
491 * NFS is shut down. The admin can start it by
492 * writing to the threads file but NOT the pool_threads
493 * file, sorry. Report zero threads.
494 */
bedbdd8b 495 mutex_unlock(&nfsd_mutex);
eed2965a
GB
496 strcpy(buf, "0\n");
497 return strlen(buf);
498 }
499
500 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
bedbdd8b 501 rv = -ENOMEM;
eed2965a 502 if (nthreads == NULL)
bedbdd8b 503 goto out_free;
eed2965a
GB
504
505 if (size > 0) {
506 for (i = 0; i < npools; i++) {
507 rv = get_int(&mesg, &nthreads[i]);
508 if (rv == -ENOENT)
509 break; /* fewer numbers than pools */
510 if (rv)
511 goto out_free; /* syntax error */
512 rv = -EINVAL;
513 if (nthreads[i] < 0)
514 goto out_free;
515 }
3938a0d5 516 rv = nfsd_set_nrthreads(i, nthreads, net);
eed2965a
GB
517 if (rv)
518 goto out_free;
519 }
520
9dd9845f 521 rv = nfsd_get_nrthreads(npools, nthreads, net);
eed2965a
GB
522 if (rv)
523 goto out_free;
524
525 mesg = buf;
526 size = SIMPLE_TRANSACTION_LIMIT;
527 for (i = 0; i < npools && size > 0; i++) {
528 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
529 len = strlen(mesg);
530 size -= len;
531 mesg += len;
532 }
413d63d7 533 rv = mesg - buf;
eed2965a
GB
534out_free:
535 kfree(nthreads);
bedbdd8b 536 mutex_unlock(&nfsd_mutex);
eed2965a
GB
537 return rv;
538}
539
ff7d1179 540static ssize_t
e333f3bb
TM
541nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
542 const char *sep, unsigned vers, int minor)
ff7d1179 543{
abcb4dac 544 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
e333f3bb 545 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
ff7d1179 546
abcb4dac 547 if (vers == 4 && minor >= 0 &&
e333f3bb 548 !nfsd_minorversion(nn, minor, NFSD_TEST))
ff7d1179 549 supported = false;
abcb4dac
N
550 if (minor == 0 && supported)
551 /*
552 * special case for backward compatability.
553 * +4.0 is never reported, it is implied by
554 * +4, unless -4.0 is present.
555 */
556 return 0;
ff7d1179
TM
557 return snprintf(buf, remaining, format, sep,
558 supported ? '+' : '-', vers, minor);
559}
560
3dd98a3b 561static ssize_t __write_versions(struct file *file, char *buf, size_t size)
70c3b76c 562{
70c3b76c 563 char *mesg = buf;
8daf220a 564 char *vers, *minorp, sign;
261758b5 565 int len, num, remaining;
70c3b76c
N
566 ssize_t tlen = 0;
567 char *sep;
244c7d44 568 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
70c3b76c
N
569
570 if (size>0) {
9dd9845f 571 if (nn->nfsd_serv)
6658d3a7 572 /* Cannot change versions without updating
9dd9845f 573 * nn->nfsd_serv->sv_xdrsize, and reallocing
6658d3a7
N
574 * rq_argp and rq_resp
575 */
70c3b76c
N
576 return -EBUSY;
577 if (buf[size-1] != '\n')
578 return -EINVAL;
579 buf[size-1] = 0;
580
581 vers = mesg;
582 len = qword_get(&mesg, vers, size);
583 if (len <= 0) return -EINVAL;
584 do {
d3635ff0 585 enum vers_op cmd;
abcb4dac 586 unsigned minor;
70c3b76c
N
587 sign = *vers;
588 if (sign == '+' || sign == '-')
8daf220a 589 num = simple_strtol((vers+1), &minorp, 0);
70c3b76c 590 else
8daf220a
BH
591 num = simple_strtol(vers, &minorp, 0);
592 if (*minorp == '.') {
ff89be87 593 if (num != 4)
8daf220a 594 return -EINVAL;
e35659f1 595 if (kstrtouint(minorp+1, 0, &minor) < 0)
8daf220a 596 return -EINVAL;
abcb4dac
N
597 }
598
d3635ff0 599 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
70c3b76c
N
600 switch(num) {
601 case 2:
602 case 3:
e333f3bb 603 nfsd_vers(nn, num, cmd);
70c3b76c 604 break;
d3635ff0 605 case 4:
abcb4dac 606 if (*minorp == '.') {
e333f3bb 607 if (nfsd_minorversion(nn, minor, cmd) < 0)
abcb4dac 608 return -EINVAL;
e333f3bb 609 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
abcb4dac
N
610 /*
611 * Either we have +4 and no minors are enabled,
612 * or we have -4 and at least one minor is enabled.
613 * In either case, propagate 'cmd' to all minors.
614 */
615 minor = 0;
e333f3bb 616 while (nfsd_minorversion(nn, minor, cmd) >= 0)
abcb4dac
N
617 minor++;
618 }
619 break;
70c3b76c
N
620 default:
621 return -EINVAL;
622 }
623 vers += len + 1;
70c3b76c
N
624 } while ((len = qword_get(&mesg, vers, size)) > 0);
625 /* If all get turned off, turn them back on, as
626 * having no versions is BAD
627 */
e333f3bb 628 nfsd_reset_versions(nn);
70c3b76c 629 }
261758b5 630
70c3b76c
N
631 /* Now write current state into reply buffer */
632 len = 0;
633 sep = "";
261758b5 634 remaining = SIMPLE_TRANSACTION_LIMIT;
ff7d1179 635 for (num=2 ; num <= 4 ; num++) {
abcb4dac 636 int minor;
e333f3bb 637 if (!nfsd_vers(nn, num, NFSD_AVAIL))
ff7d1179 638 continue;
abcb4dac
N
639
640 minor = -1;
ff7d1179 641 do {
e333f3bb 642 len = nfsd_print_version_support(nn, buf, remaining,
ff7d1179 643 sep, num, minor);
818f2f57 644 if (len >= remaining)
ff7d1179 645 goto out;
261758b5
CL
646 remaining -= len;
647 buf += len;
648 tlen += len;
ff7d1179 649 minor++;
abcb4dac
N
650 if (len)
651 sep = " ";
ff7d1179
TM
652 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
653 }
654out:
261758b5 655 len = snprintf(buf, remaining, "\n");
818f2f57 656 if (len >= remaining)
261758b5
CL
657 return -EINVAL;
658 return tlen + len;
70c3b76c
N
659}
660
262a0982
CL
661/**
662 * write_versions - Set or report the available NFS protocol versions
663 *
664 * Input:
665 * buf: ignored
666 * size: zero
667 * Output:
668 * On success: passed-in buffer filled with '\n'-terminated C
669 * string containing positive or negative integer
670 * values representing the current status of each
671 * protocol version;
672 * return code is the size in bytes of the string
673 * On error: return code is zero or a negative errno value
674 *
675 * OR
676 *
677 * Input:
678 * buf: C string containing whitespace-
679 * separated positive or negative
680 * integer values representing NFS
681 * protocol versions to enable ("+n")
682 * or disable ("-n")
683 * size: non-zero length of C string in @buf
684 * Output:
685 * On success: status of zero or more protocol versions has
686 * been updated; passed-in buffer filled with
687 * '\n'-terminated C string containing positive
688 * or negative integer values representing the
689 * current status of each protocol version;
690 * return code is the size in bytes of the string
691 * On error: return code is zero or a negative errno value
692 */
3dd98a3b
JL
693static ssize_t write_versions(struct file *file, char *buf, size_t size)
694{
695 ssize_t rv;
696
697 mutex_lock(&nfsd_mutex);
698 rv = __write_versions(file, buf, size);
699 mutex_unlock(&nfsd_mutex);
700 return rv;
701}
702
0a5372d8
CL
703/*
704 * Zero-length write. Return a list of NFSD's current listener
705 * transports.
706 */
9dd9845f 707static ssize_t __write_ports_names(char *buf, struct net *net)
0a5372d8 708{
9dd9845f
SK
709 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
710
711 if (nn->nfsd_serv == NULL)
0a5372d8 712 return 0;
9dd9845f 713 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
0a5372d8
CL
714}
715
0b7c2f6f
CL
716/*
717 * A single 'fd' number was written, in which case it must be for
718 * a socket of a supported family/protocol, and we use it as an
719 * nfsd listener.
720 */
4df493a2 721static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
0b7c2f6f
CL
722{
723 char *mesg = buf;
724 int fd, err;
9dd9845f 725 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
0b7c2f6f
CL
726
727 err = get_int(&mesg, &fd);
728 if (err != 0 || fd < 0)
729 return -EINVAL;
730
30646394
SK
731 if (svc_alien_sock(net, fd)) {
732 printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
733 return -EINVAL;
734 }
735
6777436b 736 err = nfsd_create_serv(net);
0b7c2f6f
CL
737 if (err != 0)
738 return err;
739
4df493a2 740 err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
78a8d7c8 741 if (err < 0) {
19f7e2ca 742 nfsd_destroy(net);
78a8d7c8
JL
743 return err;
744 }
0b7c2f6f 745
ea068bad 746 /* Decrease the count, but don't shut down the service */
9dd9845f 747 nn->nfsd_serv->sv_nrthreads--;
ea068bad 748 return err;
0b7c2f6f
CL
749}
750
4eb68c26
CL
751/*
752 * A transport listener is added by writing it's transport name and
753 * a port number.
754 */
4df493a2 755static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
4eb68c26
CL
756{
757 char transport[16];
37498292 758 struct svc_xprt *xprt;
4eb68c26 759 int port, err;
9dd9845f 760 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4eb68c26 761
a10fded1 762 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
4eb68c26
CL
763 return -EINVAL;
764
4be929be 765 if (port < 1 || port > USHRT_MAX)
4eb68c26
CL
766 return -EINVAL;
767
6777436b 768 err = nfsd_create_serv(net);
4eb68c26
CL
769 if (err != 0)
770 return err;
771
9dd9845f 772 err = svc_create_xprt(nn->nfsd_serv, transport, net,
4df493a2 773 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
68717908 774 if (err < 0)
37498292
CL
775 goto out_err;
776
9dd9845f 777 err = svc_create_xprt(nn->nfsd_serv, transport, net,
4df493a2 778 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
37498292
CL
779 if (err < 0 && err != -EAFNOSUPPORT)
780 goto out_close;
0cd14a06
JL
781
782 /* Decrease the count, but don't shut down the service */
9dd9845f 783 nn->nfsd_serv->sv_nrthreads--;
4eb68c26 784 return 0;
37498292 785out_close:
9dd9845f 786 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
37498292
CL
787 if (xprt != NULL) {
788 svc_close_xprt(xprt);
789 svc_xprt_put(xprt);
790 }
791out_err:
19f7e2ca 792 nfsd_destroy(net);
37498292 793 return err;
4eb68c26
CL
794}
795
08160352
SK
796static ssize_t __write_ports(struct file *file, char *buf, size_t size,
797 struct net *net)
80212d59 798{
0a5372d8 799 if (size == 0)
9dd9845f 800 return __write_ports_names(buf, net);
0b7c2f6f
CL
801
802 if (isdigit(buf[0]))
4df493a2 803 return __write_ports_addfd(buf, net, file->f_cred);
82d56591 804
4eb68c26 805 if (isalpha(buf[0]))
4df493a2 806 return __write_ports_addxprt(buf, net, file->f_cred);
4cd5dc75 807
b41b66d6 808 return -EINVAL;
80212d59
N
809}
810
262a0982
CL
811/**
812 * write_ports - Pass a socket file descriptor or transport name to listen on
813 *
814 * Input:
815 * buf: ignored
816 * size: zero
817 * Output:
818 * On success: passed-in buffer filled with a '\n'-terminated C
819 * string containing a whitespace-separated list of
820 * named NFSD listeners;
821 * return code is the size in bytes of the string
822 * On error: return code is zero or a negative errno value
823 *
824 * OR
825 *
826 * Input:
827 * buf: C string containing an unsigned
828 * integer value representing a bound
829 * but unconnected socket that is to be
c71206a7
CL
830 * used as an NFSD listener; listen(3)
831 * must be called for a SOCK_STREAM
832 * socket, otherwise it is ignored
262a0982
CL
833 * size: non-zero length of C string in @buf
834 * Output:
835 * On success: NFS service is started;
836 * passed-in buffer filled with a '\n'-terminated C
837 * string containing a unique alphanumeric name of
838 * the listener;
839 * return code is the size in bytes of the string
840 * On error: return code is a negative errno value
841 *
842 * OR
843 *
844 * Input:
262a0982
CL
845 * buf: C string containing a transport
846 * name and an unsigned integer value
847 * representing the port to listen on,
848 * separated by whitespace
849 * size: non-zero length of C string in @buf
850 * Output:
851 * On success: returns zero; NFS service is started
852 * On error: return code is a negative errno value
262a0982 853 */
bedbdd8b
NB
854static ssize_t write_ports(struct file *file, char *buf, size_t size)
855{
856 ssize_t rv;
3dd98a3b 857
bedbdd8b 858 mutex_lock(&nfsd_mutex);
244c7d44 859 rv = __write_ports(file, buf, size, netns(file));
bedbdd8b
NB
860 mutex_unlock(&nfsd_mutex);
861 return rv;
862}
863
864
596bbe53
N
865int nfsd_max_blksize;
866
262a0982
CL
867/**
868 * write_maxblksize - Set or report the current NFS blksize
869 *
870 * Input:
871 * buf: ignored
872 * size: zero
873 *
874 * OR
875 *
876 * Input:
877 * buf: C string containing an unsigned
878 * integer value representing the new
879 * NFS blksize
880 * size: non-zero length of C string in @buf
881 * Output:
882 * On success: passed-in buffer filled with '\n'-terminated C string
883 * containing numeric value of the current NFS blksize
884 * setting;
885 * return code is the size in bytes of the string
886 * On error: return code is zero or a negative errno value
887 */
596bbe53
N
888static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
889{
890 char *mesg = buf;
244c7d44 891 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9dd9845f 892
596bbe53
N
893 if (size > 0) {
894 int bsize;
895 int rv = get_int(&mesg, &bsize);
896 if (rv)
897 return rv;
898 /* force bsize into allowed range and
899 * required alignment.
900 */
3c7aa15d
KM
901 bsize = max_t(int, bsize, 1024);
902 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
596bbe53 903 bsize &= ~(1024-1);
bedbdd8b 904 mutex_lock(&nfsd_mutex);
9dd9845f 905 if (nn->nfsd_serv) {
bedbdd8b 906 mutex_unlock(&nfsd_mutex);
596bbe53
N
907 return -EBUSY;
908 }
909 nfsd_max_blksize = bsize;
bedbdd8b 910 mutex_unlock(&nfsd_mutex);
596bbe53 911 }
e06b6405
CL
912
913 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
914 nfsd_max_blksize);
596bbe53
N
915}
916
5b8db00b
JL
917/**
918 * write_maxconn - Set or report the current max number of connections
919 *
920 * Input:
921 * buf: ignored
922 * size: zero
923 * OR
924 *
925 * Input:
926 * buf: C string containing an unsigned
927 * integer value representing the new
928 * number of max connections
929 * size: non-zero length of C string in @buf
930 * Output:
931 * On success: passed-in buffer filled with '\n'-terminated C string
932 * containing numeric value of max_connections setting
933 * for this net namespace;
934 * return code is the size in bytes of the string
935 * On error: return code is zero or a negative errno value
936 */
937static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
938{
939 char *mesg = buf;
244c7d44 940 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
5b8db00b
JL
941 unsigned int maxconn = nn->max_connections;
942
943 if (size > 0) {
944 int rv = get_uint(&mesg, &maxconn);
945
946 if (rv)
947 return rv;
948 nn->max_connections = maxconn;
949 }
950
951 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
952}
953
70c3b76c 954#ifdef CONFIG_NFSD_V4
9dd9845f
SK
955static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
956 time_t *time, struct nfsd_net *nn)
1da177e4 957{
1da177e4 958 char *mesg = buf;
f0135740 959 int rv, i;
1da177e4
LT
960
961 if (size > 0) {
9dd9845f 962 if (nn->nfsd_serv)
3dd98a3b 963 return -EBUSY;
f0135740 964 rv = get_int(&mesg, &i);
1da177e4
LT
965 if (rv)
966 return rv;
e7b184f1
BF
967 /*
968 * Some sanity checking. We don't have a reason for
969 * these particular numbers, but problems with the
970 * extremes are:
971 * - Too short: the briefest network outage may
972 * cause clients to lose all their locks. Also,
973 * the frequent polling may be wasteful.
974 * - Too long: do you really want reboot recovery
975 * to take more than an hour? Or to make other
976 * clients wait an hour before being able to
977 * revoke a dead client's locks?
978 */
f0135740 979 if (i < 10 || i > 3600)
1da177e4 980 return -EINVAL;
f0135740 981 *time = i;
1da177e4 982 }
e06b6405 983
f0135740
BF
984 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
985}
986
9dd9845f
SK
987static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
988 time_t *time, struct nfsd_net *nn)
f0135740
BF
989{
990 ssize_t rv;
991
992 mutex_lock(&nfsd_mutex);
9dd9845f 993 rv = __nfsd4_write_time(file, buf, size, time, nn);
f0135740
BF
994 mutex_unlock(&nfsd_mutex);
995 return rv;
1da177e4
LT
996}
997
262a0982
CL
998/**
999 * write_leasetime - Set or report the current NFSv4 lease time
1000 *
1001 * Input:
1002 * buf: ignored
1003 * size: zero
1004 *
1005 * OR
1006 *
1007 * Input:
1008 * buf: C string containing an unsigned
1009 * integer value representing the new
1010 * NFSv4 lease expiry time
1011 * size: non-zero length of C string in @buf
1012 * Output:
1013 * On success: passed-in buffer filled with '\n'-terminated C
1014 * string containing unsigned integer value of the
1015 * current lease expiry time;
1016 * return code is the size in bytes of the string
1017 * On error: return code is zero or a negative errno value
1018 */
3dd98a3b
JL
1019static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1020{
244c7d44 1021 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9dd9845f 1022 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
3dd98a3b
JL
1023}
1024
efc4bb4f
BF
1025/**
1026 * write_gracetime - Set or report current NFSv4 grace period time
1027 *
1028 * As above, but sets the time of the NFSv4 grace period.
1029 *
1030 * Note this should never be set to less than the *previous*
1031 * lease-period time, but we don't try to enforce this. (In the common
1032 * case (a new boot), we don't know what the previous lease time was
1033 * anyway.)
1034 */
1035static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1036{
244c7d44 1037 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9dd9845f 1038 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
efc4bb4f
BF
1039}
1040
9dd9845f
SK
1041static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1042 struct nfsd_net *nn)
0964a3d3
N
1043{
1044 char *mesg = buf;
1045 char *recdir;
1046 int len, status;
1047
3dd98a3b 1048 if (size > 0) {
9dd9845f 1049 if (nn->nfsd_serv)
3dd98a3b
JL
1050 return -EBUSY;
1051 if (size > PATH_MAX || buf[size-1] != '\n')
1052 return -EINVAL;
1053 buf[size-1] = 0;
0964a3d3 1054
3dd98a3b
JL
1055 recdir = mesg;
1056 len = qword_get(&mesg, recdir, size);
1057 if (len <= 0)
1058 return -EINVAL;
0964a3d3 1059
3dd98a3b 1060 status = nfs4_reset_recoverydir(recdir);
69049961
AK
1061 if (status)
1062 return status;
3dd98a3b 1063 }
3d72ab8f
CL
1064
1065 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1066 nfs4_recoverydir());
0964a3d3 1067}
3dd98a3b 1068
262a0982
CL
1069/**
1070 * write_recoverydir - Set or report the pathname of the recovery directory
1071 *
1072 * Input:
1073 * buf: ignored
1074 * size: zero
1075 *
1076 * OR
1077 *
1078 * Input:
1079 * buf: C string containing the pathname
1080 * of the directory on a local file
1081 * system containing permanent NFSv4
1082 * recovery data
1083 * size: non-zero length of C string in @buf
1084 * Output:
1085 * On success: passed-in buffer filled with '\n'-terminated C string
1086 * containing the current recovery pathname setting;
1087 * return code is the size in bytes of the string
1088 * On error: return code is zero or a negative errno value
1089 */
3dd98a3b
JL
1090static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1091{
1092 ssize_t rv;
244c7d44 1093 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
3dd98a3b
JL
1094
1095 mutex_lock(&nfsd_mutex);
9dd9845f 1096 rv = __write_recoverydir(file, buf, size, nn);
3dd98a3b
JL
1097 mutex_unlock(&nfsd_mutex);
1098 return rv;
1099}
1100
7f5ef2e9
JL
1101/**
1102 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1103 *
1104 * Input:
1105 * buf: ignored
1106 * size: zero
1107 * OR
1108 *
1109 * Input:
1110 * buf: any value
1111 * size: non-zero length of C string in @buf
1112 * Output:
1113 * passed-in buffer filled with "Y" or "N" with a newline
1114 * and NULL-terminated C string. This indicates whether
1115 * the grace period has ended in the current net
1116 * namespace. Return code is the size in bytes of the
1117 * string. Writing a string that starts with 'Y', 'y', or
1118 * '1' to the file will end the grace period for nfsd's v4
1119 * lock manager.
1120 */
1121static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1122{
244c7d44 1123 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
7f5ef2e9
JL
1124
1125 if (size > 0) {
1126 switch(buf[0]) {
1127 case 'Y':
1128 case 'y':
1129 case '1':
dd838821 1130 if (!nn->nfsd_serv)
62a063b8 1131 return -EBUSY;
7f5ef2e9
JL
1132 nfsd4_end_grace(nn);
1133 break;
1134 default:
1135 return -EINVAL;
1136 }
1137 }
1138
1139 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1140 nn->grace_ended ? 'Y' : 'N');
1141}
1142
70c3b76c 1143#endif
0964a3d3 1144
1da177e4
LT
1145/*----------------------------------------------------------------------------*/
1146/*
1147 * populating the filesystem.
1148 */
1149
1150static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1151{
cda37124 1152 static const struct tree_descr nfsd_files[] = {
96d851c4 1153 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
e8e8753f
BF
1154 [NFSD_Export_features] = {"export_features",
1155 &export_features_operations, S_IRUGO},
4373ea84
WC
1156 [NFSD_FO_UnlockIP] = {"unlock_ip",
1157 &transaction_ops, S_IWUSR|S_IRUSR},
17efa372
WC
1158 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1159 &transaction_ops, S_IWUSR|S_IRUSR},
1da177e4
LT
1160 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1161 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
eed2965a 1162 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
03cf6c9f 1163 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
a2f999a3 1164 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
70c3b76c 1165 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
80212d59 1166 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
596bbe53 1167 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
5b8db00b 1168 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
b084f598 1169#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
b0b0c0a2 1170 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
b084f598 1171#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
1da177e4
LT
1172#ifdef CONFIG_NFSD_V4
1173 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
efc4bb4f 1174 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
0964a3d3 1175 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
7f5ef2e9 1176 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1da177e4
LT
1177#endif
1178 /* last one */ {""}
1179 };
d91ee87d
EB
1180 get_net(sb->s_fs_info);
1181 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1da177e4
LT
1182}
1183
fc14f2fe
AV
1184static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1185 int flags, const char *dev_name, void *data)
1da177e4 1186{
d91ee87d
EB
1187 struct net *net = current->nsproxy->net_ns;
1188 return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super);
11f77942
SK
1189}
1190
1191static void nfsd_umount(struct super_block *sb)
1192{
1193 struct net *net = sb->s_fs_info;
1194
1195 kill_litter_super(sb);
1196 put_net(net);
1da177e4
LT
1197}
1198
1199static struct file_system_type nfsd_fs_type = {
1200 .owner = THIS_MODULE,
1201 .name = "nfsd",
fc14f2fe 1202 .mount = nfsd_mount,
11f77942 1203 .kill_sb = nfsd_umount,
1da177e4 1204};
7f78e035 1205MODULE_ALIAS_FS("nfsd");
1da177e4 1206
e331f606
BF
1207#ifdef CONFIG_PROC_FS
1208static int create_proc_exports_entry(void)
1209{
1210 struct proc_dir_entry *entry;
1211
1212 entry = proc_mkdir("fs/nfs", NULL);
1213 if (!entry)
1214 return -ENOMEM;
96d851c4
SK
1215 entry = proc_create("exports", 0, entry,
1216 &exports_proc_operations);
ff7c4b36 1217 if (!entry) {
1218 remove_proc_entry("fs/nfs", NULL);
e331f606 1219 return -ENOMEM;
ff7c4b36 1220 }
e331f606
BF
1221 return 0;
1222}
1223#else /* CONFIG_PROC_FS */
1224static int create_proc_exports_entry(void)
1225{
1226 return 0;
1227}
1228#endif
1229
c7d03a00 1230unsigned int nfsd_net_id;
5717e012
SK
1231
1232static __net_init int nfsd_init_net(struct net *net)
1233{
1234 int retval;
3d733711 1235 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5717e012
SK
1236
1237 retval = nfsd_export_init(net);
1238 if (retval)
1239 goto out_export_error;
f69adb2f
SK
1240 retval = nfsd_idmap_init(net);
1241 if (retval)
1242 goto out_idmap_error;
e333f3bb
TM
1243 nn->nfsd_versions = NULL;
1244 nn->nfsd4_minorversions = NULL;
3bf6b57e
BF
1245 nn->nfsd4_lease = 90; /* default lease time */
1246 nn->nfsd4_grace = 90;
03f318ca 1247 nn->somebody_reclaimed = false;
362063a5 1248 nn->track_reclaim_completes = false;
ebd7c72c
BF
1249 nn->clverifier_counter = prandom_u32();
1250 nn->clientid_counter = prandom_u32();
e0639dc5 1251 nn->s2s_cp_cl_id = nn->clientid_counter++;
2317dc55
VA
1252
1253 atomic_set(&nn->ntf_refcnt, 0);
1254 init_waitqueue_head(&nn->ntf_wq);
5717e012
SK
1255 return 0;
1256
f69adb2f
SK
1257out_idmap_error:
1258 nfsd_export_shutdown(net);
5717e012
SK
1259out_export_error:
1260 return retval;
1261}
1262
1263static __net_exit void nfsd_exit_net(struct net *net)
1264{
f69adb2f 1265 nfsd_idmap_shutdown(net);
5717e012 1266 nfsd_export_shutdown(net);
e333f3bb 1267 nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
5717e012
SK
1268}
1269
7ea34ac1 1270static struct pernet_operations nfsd_net_ops = {
5717e012
SK
1271 .init = nfsd_init_net,
1272 .exit = nfsd_exit_net,
7ea34ac1
JL
1273 .id = &nfsd_net_id,
1274 .size = sizeof(struct nfsd_net),
1275};
1276
1da177e4
LT
1277static int __init init_nfsd(void)
1278{
1279 int retval;
1280 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1281
7ea34ac1
JL
1282 retval = register_pernet_subsys(&nfsd_net_ops);
1283 if (retval < 0)
bb7ffbf2
GC
1284 return retval;
1285 retval = register_cld_notifier();
e8ff2a84 1286 if (retval)
7ea34ac1 1287 goto out_unregister_pernet;
bb7ffbf2
GC
1288 retval = nfsd4_init_slabs();
1289 if (retval)
1290 goto out_unregister_notifier;
9cf514cc 1291 retval = nfsd4_init_pnfs();
65178db4
BS
1292 if (retval)
1293 goto out_free_slabs;
9cf514cc
CH
1294 retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
1295 if (retval)
1296 goto out_exit_pnfs;
1da177e4 1297 nfsd_stat_init(); /* Statistics */
d5c3428b
BF
1298 retval = nfsd_reply_cache_init();
1299 if (retval)
1300 goto out_free_stat;
1da177e4 1301 nfsd_lockd_init(); /* lockd->nfsd callbacks */
e331f606
BF
1302 retval = create_proc_exports_entry();
1303 if (retval)
f69adb2f 1304 goto out_free_lockd;
1da177e4 1305 retval = register_filesystem(&nfsd_fs_type);
26808d3f
BF
1306 if (retval)
1307 goto out_free_all;
1308 return 0;
1309out_free_all:
26808d3f
BF
1310 remove_proc_entry("fs/nfs/exports", NULL);
1311 remove_proc_entry("fs/nfs", NULL);
dbf847ec 1312out_free_lockd:
26808d3f 1313 nfsd_lockd_shutdown();
e331f606 1314 nfsd_reply_cache_shutdown();
d5c3428b
BF
1315out_free_stat:
1316 nfsd_stat_shutdown();
65178db4 1317 nfsd_fault_inject_cleanup();
9cf514cc
CH
1318out_exit_pnfs:
1319 nfsd4_exit_pnfs();
65178db4 1320out_free_slabs:
26808d3f 1321 nfsd4_free_slabs();
813fd320 1322out_unregister_notifier:
797a9d79 1323 unregister_cld_notifier();
bb7ffbf2
GC
1324out_unregister_pernet:
1325 unregister_pernet_subsys(&nfsd_net_ops);
1da177e4
LT
1326 return retval;
1327}
1328
1329static void __exit exit_nfsd(void)
1330{
d5c3428b 1331 nfsd_reply_cache_shutdown();
1da177e4
LT
1332 remove_proc_entry("fs/nfs/exports", NULL);
1333 remove_proc_entry("fs/nfs", NULL);
1334 nfsd_stat_shutdown();
1335 nfsd_lockd_shutdown();
e8ff2a84 1336 nfsd4_free_slabs();
9cf514cc 1337 nfsd4_exit_pnfs();
65178db4 1338 nfsd_fault_inject_cleanup();
1da177e4 1339 unregister_filesystem(&nfsd_fs_type);
797a9d79 1340 unregister_cld_notifier();
bb7ffbf2 1341 unregister_pernet_subsys(&nfsd_net_ops);
1da177e4
LT
1342}
1343
1344MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1345MODULE_LICENSE("GPL");
1346module_init(init_nfsd)
1347module_exit(exit_nfsd)