1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Syscall interface to knfsd.
4 *
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6 */
7
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/fs_context.h>
12
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/lockd/bind.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/rpc_pipe_fs.h>
18 #include <linux/sunrpc/svc.h>
19 #include <linux/module.h>
20 #include <linux/fsnotify.h>
21 #include <linux/nfslocalio.h>
22
23 #include "idmap.h"
24 #include "nfsd.h"
25 #include "cache.h"
26 #include "state.h"
27 #include "netns.h"
28 #include "pnfs.h"
29 #include "filecache.h"
30 #include "trace.h"
31 #include "netlink.h"
32
33 /*
34 * We have a single directory with several nodes in it.
35 */
36 enum {
37 NFSD_Root = 1,
38 NFSD_List,
39 NFSD_Export_Stats,
40 NFSD_Export_features,
41 NFSD_Fh,
42 NFSD_FO_UnlockIP,
43 NFSD_FO_UnlockFS,
44 NFSD_Threads,
45 NFSD_Pool_Threads,
46 NFSD_Pool_Stats,
47 NFSD_Reply_Cache_Stats,
48 NFSD_Versions,
49 NFSD_Ports,
50 NFSD_MaxBlkSize,
51 NFSD_Filecache,
52 NFSD_Leasetime,
53 NFSD_Gracetime,
54 NFSD_RecoveryDir,
55 NFSD_V4EndGrace,
56 NFSD_MaxReserved
57 };
58
59 /*
60 * write() for these nodes.
61 */
62 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
63 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
64 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
65 static ssize_t write_threads(struct file *file, char *buf, size_t size);
66 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
67 static ssize_t write_versions(struct file *file, char *buf, size_t size);
68 static ssize_t write_ports(struct file *file, char *buf, size_t size);
69 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
70 #ifdef CONFIG_NFSD_V4
71 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
72 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
73 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
74 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
75 #endif
76 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
77 #endif
78
79 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
80 [NFSD_Fh] = write_filehandle,
81 [NFSD_FO_UnlockIP] = write_unlock_ip,
82 [NFSD_FO_UnlockFS] = write_unlock_fs,
83 [NFSD_Threads] = write_threads,
84 [NFSD_Pool_Threads] = write_pool_threads,
85 [NFSD_Versions] = write_versions,
86 [NFSD_Ports] = write_ports,
87 [NFSD_MaxBlkSize] = write_maxblksize,
88 #ifdef CONFIG_NFSD_V4
89 [NFSD_Leasetime] = write_leasetime,
90 [NFSD_Gracetime] = write_gracetime,
91 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
92 [NFSD_RecoveryDir] = write_recoverydir,
93 #endif
94 [NFSD_V4EndGrace] = write_v4_end_grace,
95 #endif
96 };
97
nfsctl_transaction_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)98 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
99 {
100 ino_t ino = file_inode(file)->i_ino;
101 char *data;
102 ssize_t rv;
103
104 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
105 return -EINVAL;
106
107 data = simple_transaction_get(file, buf, size);
108 if (IS_ERR(data))
109 return PTR_ERR(data);
110
111 rv = write_op[ino](file, data, size);
112 if (rv < 0)
113 return rv;
114
115 simple_transaction_set(file, rv);
116 return size;
117 }
118
nfsctl_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)119 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
120 {
121 if (! file->private_data) {
122 /* An attempt to read a transaction file without writing
123 * causes a 0-byte write so that the file can return
124 * state information
125 */
126 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
127 if (rv < 0)
128 return rv;
129 }
130 return simple_transaction_read(file, buf, size, pos);
131 }
132
133 static const struct file_operations transaction_ops = {
134 .write = nfsctl_transaction_write,
135 .read = nfsctl_transaction_read,
136 .release = simple_transaction_release,
137 .llseek = default_llseek,
138 };
139
exports_net_open(struct net * net,struct file * file)140 static int exports_net_open(struct net *net, struct file *file)
141 {
142 int err;
143 struct seq_file *seq;
144 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
145
146 err = seq_open(file, &nfs_exports_op);
147 if (err)
148 return err;
149
150 seq = file->private_data;
151 seq->private = nn->svc_export_cache;
152 get_net(net);
153 return 0;
154 }
155
exports_release(struct inode * inode,struct file * file)156 static int exports_release(struct inode *inode, struct file *file)
157 {
158 struct seq_file *seq = file->private_data;
159 struct cache_detail *cd = seq->private;
160
161 put_net(cd->net);
162 return seq_release(inode, file);
163 }
164
exports_nfsd_open(struct inode * inode,struct file * file)165 static int exports_nfsd_open(struct inode *inode, struct file *file)
166 {
167 return exports_net_open(inode->i_sb->s_fs_info, file);
168 }
169
170 static const struct file_operations exports_nfsd_operations = {
171 .open = exports_nfsd_open,
172 .read = seq_read,
173 .llseek = seq_lseek,
174 .release = exports_release,
175 };
176
export_features_show(struct seq_file * m,void * v)177 static int export_features_show(struct seq_file *m, void *v)
178 {
179 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
180 return 0;
181 }
182
183 DEFINE_SHOW_ATTRIBUTE(export_features);
184
nfsd_pool_stats_open(struct inode * inode,struct file * file)185 static int nfsd_pool_stats_open(struct inode *inode, struct file *file)
186 {
187 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
188
189 return svc_pool_stats_open(&nn->nfsd_info, file);
190 }
191
192 static const struct file_operations pool_stats_operations = {
193 .open = nfsd_pool_stats_open,
194 .read = seq_read,
195 .llseek = seq_lseek,
196 .release = seq_release,
197 };
198
199 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
200
201 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
202
203 /*----------------------------------------------------------------------------*/
204 /*
205 * payload - write methods
206 */
207
netns(struct file * file)208 static inline struct net *netns(struct file *file)
209 {
210 return file_inode(file)->i_sb->s_fs_info;
211 }
212
213 /*
214 * write_unlock_ip - Release all locks used by a client
215 *
216 * Experimental.
217 *
218 * Input:
219 * buf: '\n'-terminated C string containing a
220 * presentation format IP address
221 * size: length of C string in @buf
222 * Output:
223 * On success: returns zero if all specified locks were released;
224 * returns one if one or more locks were not released
225 * On error: return code is negative errno value
226 */
write_unlock_ip(struct file * file,char * buf,size_t size)227 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
228 {
229 struct sockaddr_storage address;
230 struct sockaddr *sap = (struct sockaddr *)&address;
231 size_t salen = sizeof(address);
232 char *fo_path;
233 struct net *net = netns(file);
234
235 /* sanity check */
236 if (size == 0)
237 return -EINVAL;
238
239 if (buf[size-1] != '\n')
240 return -EINVAL;
241
242 fo_path = buf;
243 if (qword_get(&buf, fo_path, size) < 0)
244 return -EINVAL;
245
246 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
247 return -EINVAL;
248
249 trace_nfsd_ctl_unlock_ip(net, buf);
250 return nlmsvc_unlock_all_by_ip(sap);
251 }
252
253 /*
254 * write_unlock_fs - Release all locks on a local file system
255 *
256 * Experimental.
257 *
258 * Input:
259 * buf: '\n'-terminated C string containing the
260 * absolute pathname of a local file system
261 * size: length of C string in @buf
262 * Output:
263 * On success: returns zero if all specified locks were released;
264 * returns one if one or more locks were not released
265 * On error: return code is negative errno value
266 */
write_unlock_fs(struct file * file,char * buf,size_t size)267 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
268 {
269 struct path path;
270 char *fo_path;
271 int error;
272 struct nfsd_net *nn;
273
274 /* sanity check */
275 if (size == 0)
276 return -EINVAL;
277
278 if (buf[size-1] != '\n')
279 return -EINVAL;
280
281 fo_path = buf;
282 if (qword_get(&buf, fo_path, size) < 0)
283 return -EINVAL;
284 trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
285 error = kern_path(fo_path, 0, &path);
286 if (error)
287 return error;
288
289 /*
290 * XXX: Needs better sanity checking. Otherwise we could end up
291 * releasing locks on the wrong file system.
292 *
293 * For example:
294 * 1. Does the path refer to a directory?
295 * 2. Is that directory a mount point, or
296 * 3. Is that directory the root of an exported file system?
297 */
298 nfsd4_cancel_copy_by_sb(netns(file), path.dentry->d_sb);
299 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
300 mutex_lock(&nfsd_mutex);
301 nn = net_generic(netns(file), nfsd_net_id);
302 if (nn->nfsd_serv)
303 nfsd4_revoke_states(nn, path.dentry->d_sb);
304 else
305 error = -EINVAL;
306 mutex_unlock(&nfsd_mutex);
307
308 path_put(&path);
309 return error;
310 }
311
312 /*
313 * write_filehandle - Get a variable-length NFS file handle by path
314 *
315 * On input, the buffer contains a '\n'-terminated C string comprised of
316 * three alphanumeric words separated by whitespace. The string may
317 * contain escape sequences.
318 *
319 * Input:
320 * buf:
321 * domain: client domain name
322 * path: export pathname
323 * maxsize: numeric maximum size of
324 * @buf
325 * size: length of C string in @buf
326 * Output:
327 * On success: passed-in buffer filled with '\n'-terminated C
328 * string containing a ASCII hex text version
329 * of the NFS file handle;
330 * return code is the size in bytes of the string
331 * On error: return code is negative errno value
332 */
write_filehandle(struct file * file,char * buf,size_t size)333 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
334 {
335 char *dname, *path;
336 int maxsize;
337 char *mesg = buf;
338 int len;
339 struct auth_domain *dom;
340 struct knfsd_fh fh;
341
342 if (size == 0)
343 return -EINVAL;
344
345 if (buf[size-1] != '\n')
346 return -EINVAL;
347 buf[size-1] = 0;
348
349 dname = mesg;
350 len = qword_get(&mesg, dname, size);
351 if (len <= 0)
352 return -EINVAL;
353
354 path = dname+len+1;
355 len = qword_get(&mesg, path, size);
356 if (len <= 0)
357 return -EINVAL;
358
359 len = get_int(&mesg, &maxsize);
360 if (len)
361 return len;
362
363 if (maxsize < NFS_FHSIZE)
364 return -EINVAL;
365 maxsize = min(maxsize, NFS3_FHSIZE);
366
367 if (qword_get(&mesg, mesg, size) > 0)
368 return -EINVAL;
369
370 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
371
372 /* we have all the words, they are in buf.. */
373 dom = unix_domain_find(dname);
374 if (!dom)
375 return -ENOMEM;
376
377 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
378 auth_domain_put(dom);
379 if (len)
380 return len;
381
382 mesg = buf;
383 len = SIMPLE_TRANSACTION_LIMIT;
384 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
385 mesg[-1] = '\n';
386 return mesg - buf;
387 }
388
389 /*
390 * write_threads - Start NFSD, or report the configured number of threads
391 *
392 * Input:
393 * buf: ignored
394 * size: zero
395 * Output:
396 * On success: passed-in buffer filled with '\n'-terminated C
397 * string numeric value representing the configured
398 * number of NFSD threads;
399 * return code is the size in bytes of the string
400 * On error: return code is zero
401 *
402 * OR
403 *
404 * Input:
405 * buf: C string containing an unsigned
406 * integer value representing the
407 * number of NFSD threads to start
408 * size: non-zero length of C string in @buf
409 * Output:
410 * On success: NFS service is started;
411 * passed-in buffer filled with '\n'-terminated C
412 * string numeric value representing the configured
413 * number of NFSD threads;
414 * return code is the size in bytes of the string
415 * On error: return code is zero or a negative errno value
416 */
write_threads(struct file * file,char * buf,size_t size)417 static ssize_t write_threads(struct file *file, char *buf, size_t size)
418 {
419 char *mesg = buf;
420 int rv;
421 struct net *net = netns(file);
422
423 if (size > 0) {
424 int newthreads;
425 rv = get_int(&mesg, &newthreads);
426 if (rv)
427 return rv;
428 if (newthreads < 0)
429 return -EINVAL;
430 trace_nfsd_ctl_threads(net, newthreads);
431 mutex_lock(&nfsd_mutex);
432 rv = nfsd_svc(1, &newthreads, net, file->f_cred, NULL);
433 mutex_unlock(&nfsd_mutex);
434 if (rv < 0)
435 return rv;
436 } else
437 rv = nfsd_nrthreads(net);
438
439 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
440 }
441
442 /*
443 * write_pool_threads - Set or report the configured number of threads per pool
444 *
445 * Input:
446 * buf: ignored
447 * size: zero
448 *
449 * OR
450 *
451 * Input:
452 * buf: C string containing whitespace-
453 * separated unsigned integer values
454 * representing the number of NFSD
455 * threads to start in each pool
456 * size: non-zero length of C string in @buf
457 * Output:
458 * On success: passed-in buffer filled with '\n'-terminated C
459 * string containing integer values representing the
460 * configured number of NFSD threads in each pool;
461 * return code is the size in bytes of the string
462 * On error: return code is zero or a negative errno value
463 */
write_pool_threads(struct file * file,char * buf,size_t size)464 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
465 {
466 /* if size > 0, look for an array of number of threads per node
467 * and apply them then write out number of threads per node as reply
468 */
469 char *mesg = buf;
470 int i;
471 int rv;
472 int len;
473 int npools;
474 int *nthreads;
475 struct net *net = netns(file);
476
477 mutex_lock(&nfsd_mutex);
478 npools = nfsd_nrpools(net);
479 if (npools == 0) {
480 /*
481 * NFS is shut down. The admin can start it by
482 * writing to the threads file but NOT the pool_threads
483 * file, sorry. Report zero threads.
484 */
485 mutex_unlock(&nfsd_mutex);
486 strcpy(buf, "0\n");
487 return strlen(buf);
488 }
489
490 nthreads = kzalloc_objs(int, npools);
491 rv = -ENOMEM;
492 if (nthreads == NULL)
493 goto out_free;
494
495 if (size > 0) {
496 for (i = 0; i < npools; i++) {
497 rv = get_int(&mesg, &nthreads[i]);
498 if (rv == -ENOENT)
499 break; /* fewer numbers than pools */
500 if (rv)
501 goto out_free; /* syntax error */
502 rv = -EINVAL;
503 if (nthreads[i] < 0)
504 goto out_free;
505 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
506 }
507
508 /*
509 * There must always be a thread in pool 0; the admin
510 * can't shut down NFS completely using pool_threads.
511 */
512 if (nthreads[0] == 0)
513 nthreads[0] = 1;
514
515 rv = nfsd_set_nrthreads(i, nthreads, net);
516 if (rv)
517 goto out_free;
518 }
519
520 rv = nfsd_get_nrthreads(npools, nthreads, net);
521 if (rv)
522 goto out_free;
523
524 mesg = buf;
525 size = SIMPLE_TRANSACTION_LIMIT;
526 for (i = 0; i < npools && size > 0; i++) {
527 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
528 len = strlen(mesg);
529 size -= len;
530 mesg += len;
531 }
532 rv = mesg - buf;
533 out_free:
534 kfree(nthreads);
535 mutex_unlock(&nfsd_mutex);
536 return rv;
537 }
538
539 static ssize_t
nfsd_print_version_support(struct nfsd_net * nn,char * buf,int remaining,const char * sep,unsigned vers,int minor)540 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
541 const char *sep, unsigned vers, int minor)
542 {
543 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
544 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
545
546 if (vers == 4 && minor >= 0 &&
547 !nfsd_minorversion(nn, minor, NFSD_TEST))
548 supported = false;
549 if (minor == 0 && supported)
550 /*
551 * special case for backward compatability.
552 * +4.0 is never reported, it is implied by
553 * +4, unless -4.0 is present.
554 */
555 return 0;
556 return snprintf(buf, remaining, format, sep,
557 supported ? '+' : '-', vers, minor);
558 }
559
__write_versions(struct file * file,char * buf,size_t size)560 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
561 {
562 char *mesg = buf;
563 char *vers, *minorp, sign;
564 int len, num, remaining;
565 ssize_t tlen = 0;
566 char *sep;
567 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
568
569 if (size > 0) {
570 if (nn->nfsd_serv)
571 /* Cannot change versions without updating
572 * nn->nfsd_serv->sv_xdrsize, and reallocing
573 * rq_argp and rq_resp
574 */
575 return -EBUSY;
576 if (buf[size-1] != '\n')
577 return -EINVAL;
578 buf[size-1] = 0;
579 trace_nfsd_ctl_version(netns(file), buf);
580
581 vers = mesg;
582 len = qword_get(&mesg, vers, size);
583 if (len <= 0) return -EINVAL;
584 do {
585 enum vers_op cmd;
586 unsigned minor;
587 sign = *vers;
588 if (sign == '+' || sign == '-')
589 num = simple_strtol((vers+1), &minorp, 0);
590 else
591 num = simple_strtol(vers, &minorp, 0);
592 if (*minorp == '.') {
593 if (num != 4)
594 return -EINVAL;
595 if (kstrtouint(minorp+1, 0, &minor) < 0)
596 return -EINVAL;
597 }
598
599 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
600 switch(num) {
601 #ifdef CONFIG_NFSD_V2
602 case 2:
603 #endif
604 case 3:
605 nfsd_vers(nn, num, cmd);
606 break;
607 case 4:
608 if (*minorp == '.') {
609 if (nfsd_minorversion(nn, minor, cmd) < 0)
610 return -EINVAL;
611 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
612 /*
613 * Either we have +4 and no minors are enabled,
614 * or we have -4 and at least one minor is enabled.
615 * In either case, propagate 'cmd' to all minors.
616 */
617 minor = 0;
618 while (nfsd_minorversion(nn, minor, cmd) >= 0)
619 minor++;
620 }
621 break;
622 default:
623 /* Ignore requests to disable non-existent versions */
624 if (cmd == NFSD_SET)
625 return -EINVAL;
626 }
627 vers += len + 1;
628 } while ((len = qword_get(&mesg, vers, size)) > 0);
629 /* If all get turned off, turn them back on, as
630 * having no versions is BAD
631 */
632 nfsd_reset_versions(nn);
633 }
634
635 /* Now write current state into reply buffer */
636 sep = "";
637 remaining = SIMPLE_TRANSACTION_LIMIT;
638 for (num=2 ; num <= 4 ; num++) {
639 int minor;
640 if (!nfsd_vers(nn, num, NFSD_AVAIL))
641 continue;
642
643 minor = -1;
644 do {
645 len = nfsd_print_version_support(nn, buf, remaining,
646 sep, num, minor);
647 if (len >= remaining)
648 goto out;
649 remaining -= len;
650 buf += len;
651 tlen += len;
652 minor++;
653 if (len)
654 sep = " ";
655 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
656 }
657 out:
658 len = snprintf(buf, remaining, "\n");
659 if (len >= remaining)
660 return -EINVAL;
661 return tlen + len;
662 }
663
664 /*
665 * write_versions - Set or report the available NFS protocol versions
666 *
667 * Input:
668 * buf: ignored
669 * size: zero
670 * Output:
671 * On success: passed-in buffer filled with '\n'-terminated C
672 * string containing positive or negative integer
673 * values representing the current status of each
674 * protocol version;
675 * return code is the size in bytes of the string
676 * On error: return code is zero or a negative errno value
677 *
678 * OR
679 *
680 * Input:
681 * buf: C string containing whitespace-
682 * separated positive or negative
683 * integer values representing NFS
684 * protocol versions to enable ("+n")
685 * or disable ("-n")
686 * size: non-zero length of C string in @buf
687 * Output:
688 * On success: status of zero or more protocol versions has
689 * been updated; passed-in buffer filled with
690 * '\n'-terminated C string containing positive
691 * or negative integer values representing the
692 * current status of each protocol version;
693 * return code is the size in bytes of the string
694 * On error: return code is zero or a negative errno value
695 */
write_versions(struct file * file,char * buf,size_t size)696 static ssize_t write_versions(struct file *file, char *buf, size_t size)
697 {
698 ssize_t rv;
699
700 mutex_lock(&nfsd_mutex);
701 rv = __write_versions(file, buf, size);
702 mutex_unlock(&nfsd_mutex);
703 return rv;
704 }
705
706 /*
707 * Zero-length write. Return a list of NFSD's current listener
708 * transports.
709 */
__write_ports_names(char * buf,struct net * net)710 static ssize_t __write_ports_names(char *buf, struct net *net)
711 {
712 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
713
714 if (nn->nfsd_serv == NULL)
715 return 0;
716 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
717 }
718
719 /*
720 * A single 'fd' number was written, in which case it must be for
721 * a socket of a supported family/protocol, and we use it as an
722 * nfsd listener.
723 */
__write_ports_addfd(char * buf,struct net * net,const struct cred * cred)724 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
725 {
726 char *mesg = buf;
727 int fd, err;
728 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
729 struct svc_serv *serv;
730
731 err = get_int(&mesg, &fd);
732 if (err != 0 || fd < 0)
733 return -EINVAL;
734 trace_nfsd_ctl_ports_addfd(net, fd);
735
736 err = nfsd_create_serv(net);
737 if (err != 0)
738 return err;
739
740 serv = nn->nfsd_serv;
741 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
742
743 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
744 nfsd_destroy_serv(net);
745
746 return err;
747 }
748
749 /*
750 * A transport listener is added by writing its transport name and
751 * a port number.
752 */
__write_ports_addxprt(char * buf,struct net * net,const struct cred * cred)753 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
754 {
755 char transport[16];
756 struct svc_xprt *xprt;
757 int port, err;
758 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
759 struct svc_serv *serv;
760
761 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
762 return -EINVAL;
763
764 if (port < 1 || port > USHRT_MAX)
765 return -EINVAL;
766 trace_nfsd_ctl_ports_addxprt(net, transport, port);
767
768 err = nfsd_create_serv(net);
769 if (err != 0)
770 return err;
771
772 serv = nn->nfsd_serv;
773 err = svc_xprt_create(serv, transport, net,
774 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
775 if (err < 0)
776 goto out_err;
777
778 err = svc_xprt_create(serv, transport, net,
779 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
780 if (err < 0 && err != -EAFNOSUPPORT)
781 goto out_close;
782
783 return 0;
784 out_close:
785 xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
786 if (xprt != NULL) {
787 svc_xprt_close(xprt);
788 svc_xprt_put(xprt);
789 }
790 out_err:
791 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
792 nfsd_destroy_serv(net);
793
794 return err;
795 }
796
__write_ports(struct file * file,char * buf,size_t size,struct net * net)797 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
798 struct net *net)
799 {
800 if (size == 0)
801 return __write_ports_names(buf, net);
802
803 if (isdigit(buf[0]))
804 return __write_ports_addfd(buf, net, file->f_cred);
805
806 if (isalpha(buf[0]))
807 return __write_ports_addxprt(buf, net, file->f_cred);
808
809 return -EINVAL;
810 }
811
812 /*
813 * write_ports - Pass a socket file descriptor or transport name to listen on
814 *
815 * Input:
816 * buf: ignored
817 * size: zero
818 * Output:
819 * On success: passed-in buffer filled with a '\n'-terminated C
820 * string containing a whitespace-separated list of
821 * named NFSD listeners;
822 * return code is the size in bytes of the string
823 * On error: return code is zero or a negative errno value
824 *
825 * OR
826 *
827 * Input:
828 * buf: C string containing an unsigned
829 * integer value representing a bound
830 * but unconnected socket that is to be
831 * used as an NFSD listener; listen(3)
832 * must be called for a SOCK_STREAM
833 * socket, otherwise it is ignored
834 * size: non-zero length of C string in @buf
835 * Output:
836 * On success: NFS service is started;
837 * passed-in buffer filled with a '\n'-terminated C
838 * string containing a unique alphanumeric name of
839 * the listener;
840 * return code is the size in bytes of the string
841 * On error: return code is a negative errno value
842 *
843 * OR
844 *
845 * Input:
846 * buf: C string containing a transport
847 * name and an unsigned integer value
848 * representing the port to listen on,
849 * separated by whitespace
850 * size: non-zero length of C string in @buf
851 * Output:
852 * On success: returns zero; NFS service is started
853 * On error: return code is a negative errno value
854 */
write_ports(struct file * file,char * buf,size_t size)855 static ssize_t write_ports(struct file *file, char *buf, size_t size)
856 {
857 ssize_t rv;
858
859 mutex_lock(&nfsd_mutex);
860 rv = __write_ports(file, buf, size, netns(file));
861 mutex_unlock(&nfsd_mutex);
862 return rv;
863 }
864
865
866 int nfsd_max_blksize;
867
868 /*
869 * write_maxblksize - Set or report the current NFS blksize
870 *
871 * Input:
872 * buf: ignored
873 * size: zero
874 *
875 * OR
876 *
877 * Input:
878 * buf: C string containing an unsigned
879 * integer value representing the new
880 * NFS blksize
881 * size: non-zero length of C string in @buf
882 * Output:
883 * On success: passed-in buffer filled with '\n'-terminated C string
884 * containing numeric value of the current NFS blksize
885 * setting;
886 * return code is the size in bytes of the string
887 * On error: return code is zero or a negative errno value
888 */
write_maxblksize(struct file * file,char * buf,size_t size)889 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
890 {
891 char *mesg = buf;
892 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
893
894 if (size > 0) {
895 int bsize;
896 int rv = get_int(&mesg, &bsize);
897 if (rv)
898 return rv;
899 trace_nfsd_ctl_maxblksize(netns(file), bsize);
900
901 /* force bsize into allowed range and
902 * required alignment.
903 */
904 bsize = max_t(int, bsize, 1024);
905 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
906 bsize &= ~(1024-1);
907 mutex_lock(&nfsd_mutex);
908 if (nn->nfsd_serv) {
909 mutex_unlock(&nfsd_mutex);
910 return -EBUSY;
911 }
912 nfsd_max_blksize = bsize;
913 mutex_unlock(&nfsd_mutex);
914 }
915
916 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
917 nfsd_max_blksize);
918 }
919
920 #ifdef CONFIG_NFSD_V4
__nfsd4_write_time(struct file * file,char * buf,size_t size,time64_t * time,struct nfsd_net * nn)921 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
922 time64_t *time, struct nfsd_net *nn)
923 {
924 struct dentry *dentry = file_dentry(file);
925 char *mesg = buf;
926 int rv, i;
927
928 if (size > 0) {
929 if (nn->nfsd_serv)
930 return -EBUSY;
931 rv = get_int(&mesg, &i);
932 if (rv)
933 return rv;
934 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
935 dentry->d_name.len, i);
936
937 /*
938 * Some sanity checking. We don't have a reason for
939 * these particular numbers, but problems with the
940 * extremes are:
941 * - Too short: the briefest network outage may
942 * cause clients to lose all their locks. Also,
943 * the frequent polling may be wasteful.
944 * - Too long: do you really want reboot recovery
945 * to take more than an hour? Or to make other
946 * clients wait an hour before being able to
947 * revoke a dead client's locks?
948 */
949 if (i < 10 || i > 3600)
950 return -EINVAL;
951 *time = i;
952 }
953
954 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
955 }
956
nfsd4_write_time(struct file * file,char * buf,size_t size,time64_t * time,struct nfsd_net * nn)957 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
958 time64_t *time, struct nfsd_net *nn)
959 {
960 ssize_t rv;
961
962 mutex_lock(&nfsd_mutex);
963 rv = __nfsd4_write_time(file, buf, size, time, nn);
964 mutex_unlock(&nfsd_mutex);
965 return rv;
966 }
967
968 /*
969 * write_leasetime - Set or report the current NFSv4 lease time
970 *
971 * Input:
972 * buf: ignored
973 * size: zero
974 *
975 * OR
976 *
977 * Input:
978 * buf: C string containing an unsigned
979 * integer value representing the new
980 * NFSv4 lease expiry time
981 * size: non-zero length of C string in @buf
982 * Output:
983 * On success: passed-in buffer filled with '\n'-terminated C
984 * string containing unsigned integer value of the
985 * current lease expiry time;
986 * return code is the size in bytes of the string
987 * On error: return code is zero or a negative errno value
988 */
write_leasetime(struct file * file,char * buf,size_t size)989 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
990 {
991 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
992 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
993 }
994
995 /*
996 * write_gracetime - Set or report current NFSv4 grace period time
997 *
998 * As above, but sets the time of the NFSv4 grace period.
999 *
1000 * Note this should never be set to less than the *previous*
1001 * lease-period time, but we don't try to enforce this. (In the common
1002 * case (a new boot), we don't know what the previous lease time was
1003 * anyway.)
1004 */
write_gracetime(struct file * file,char * buf,size_t size)1005 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1006 {
1007 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1008 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1009 }
1010
1011 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
__write_recoverydir(struct file * file,char * buf,size_t size,struct nfsd_net * nn)1012 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1013 struct nfsd_net *nn)
1014 {
1015 char *mesg = buf;
1016 char *recdir;
1017 int len, status;
1018
1019 if (size > 0) {
1020 if (nn->nfsd_serv)
1021 return -EBUSY;
1022 if (size > PATH_MAX || buf[size-1] != '\n')
1023 return -EINVAL;
1024 buf[size-1] = 0;
1025
1026 recdir = mesg;
1027 len = qword_get(&mesg, recdir, size);
1028 if (len <= 0)
1029 return -EINVAL;
1030 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1031
1032 status = nfs4_reset_recoverydir(recdir);
1033 if (status)
1034 return status;
1035 }
1036
1037 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1038 nfs4_recoverydir());
1039 }
1040
1041 /*
1042 * write_recoverydir - Set or report the pathname of the recovery directory
1043 *
1044 * Input:
1045 * buf: ignored
1046 * size: zero
1047 *
1048 * OR
1049 *
1050 * Input:
1051 * buf: C string containing the pathname
1052 * of the directory on a local file
1053 * system containing permanent NFSv4
1054 * recovery data
1055 * size: non-zero length of C string in @buf
1056 * Output:
1057 * On success: passed-in buffer filled with '\n'-terminated C string
1058 * containing the current recovery pathname setting;
1059 * return code is the size in bytes of the string
1060 * On error: return code is zero or a negative errno value
1061 */
write_recoverydir(struct file * file,char * buf,size_t size)1062 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1063 {
1064 ssize_t rv;
1065 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1066
1067 mutex_lock(&nfsd_mutex);
1068 rv = __write_recoverydir(file, buf, size, nn);
1069 mutex_unlock(&nfsd_mutex);
1070 return rv;
1071 }
1072 #endif
1073
1074 /*
1075 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1076 *
1077 * Input:
1078 * buf: ignored
1079 * size: zero
1080 * OR
1081 *
1082 * Input:
1083 * buf: any value
1084 * size: non-zero length of C string in @buf
1085 * Output:
1086 * passed-in buffer filled with "Y" or "N" with a newline
1087 * and NULL-terminated C string. This indicates whether
1088 * the grace period has ended in the current net
1089 * namespace. Return code is the size in bytes of the
1090 * string. Writing a string that starts with 'Y', 'y', or
1091 * '1' to the file will end the grace period for nfsd's v4
1092 * lock manager.
1093 */
write_v4_end_grace(struct file * file,char * buf,size_t size)1094 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1095 {
1096 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1097
1098 if (size > 0) {
1099 switch(buf[0]) {
1100 case 'Y':
1101 case 'y':
1102 case '1':
1103 if (!nfsd4_force_end_grace(nn))
1104 return -EBUSY;
1105 trace_nfsd_end_grace(netns(file));
1106 break;
1107 default:
1108 return -EINVAL;
1109 }
1110 }
1111
1112 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1113 nn->grace_ended ? 'Y' : 'N');
1114 }
1115
1116 #endif
1117
1118 /*----------------------------------------------------------------------------*/
1119 /*
1120 * populating the filesystem.
1121 */
1122
nfsd_get_inode(struct super_block * sb,umode_t mode)1123 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1124 {
1125 struct inode *inode = new_inode(sb);
1126 if (inode) {
1127 /* Following advice from simple_fill_super documentation: */
1128 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1129 inode->i_mode = mode;
1130 simple_inode_init_ts(inode);
1131 }
1132 return inode;
1133 }
1134
nfsd_mkdir(struct dentry * parent,struct nfsdfs_client * ncl,char * name)1135 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1136 {
1137 struct inode *dir = parent->d_inode;
1138 struct dentry *dentry;
1139 struct inode *inode;
1140
1141 inode = nfsd_get_inode(parent->d_sb, S_IFDIR | 0600);
1142 if (!inode)
1143 return ERR_PTR(-ENOMEM);
1144
1145 dentry = simple_start_creating(parent, name);
1146 if (IS_ERR(dentry)) {
1147 iput(inode);
1148 return dentry;
1149 }
1150 inode->i_fop = &simple_dir_operations;
1151 inode->i_op = &simple_dir_inode_operations;
1152 inc_nlink(inode);
1153 if (ncl) {
1154 inode->i_private = ncl;
1155 kref_get(&ncl->cl_ref);
1156 }
1157 d_make_persistent(dentry, inode);
1158 inc_nlink(dir);
1159 fsnotify_mkdir(dir, dentry);
1160 simple_done_creating(dentry);
1161 return dentry; // borrowed
1162 }
1163
1164 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1165 /*
1166 * @content is assumed to be a NUL-terminated string that lives
1167 * longer than the symlink itself.
1168 */
_nfsd_symlink(struct dentry * parent,const char * name,const char * content)1169 static void _nfsd_symlink(struct dentry *parent, const char *name,
1170 const char *content)
1171 {
1172 struct inode *dir = parent->d_inode;
1173 struct inode *inode;
1174 struct dentry *dentry;
1175
1176 inode = nfsd_get_inode(dir->i_sb, S_IFLNK | 0777);
1177 if (!inode)
1178 return;
1179
1180 dentry = simple_start_creating(parent, name);
1181 if (IS_ERR(dentry)) {
1182 iput(inode);
1183 return;
1184 }
1185
1186 inode->i_op = &simple_symlink_inode_operations;
1187 inode->i_link = (char *)content;
1188 inode->i_size = strlen(content);
1189
1190 d_make_persistent(dentry, inode);
1191 fsnotify_create(dir, dentry);
1192 simple_done_creating(dentry);
1193 }
1194 #else
_nfsd_symlink(struct dentry * parent,const char * name,const char * content)1195 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1196 const char *content)
1197 {
1198 }
1199
1200 #endif
1201
clear_ncl(struct dentry * dentry)1202 static void clear_ncl(struct dentry *dentry)
1203 {
1204 struct inode *inode = d_inode(dentry);
1205 struct nfsdfs_client *ncl = inode->i_private;
1206
1207 spin_lock(&inode->i_lock);
1208 inode->i_private = NULL;
1209 spin_unlock(&inode->i_lock);
1210 kref_put(&ncl->cl_ref, ncl->cl_release);
1211 }
1212
get_nfsdfs_client(struct inode * inode)1213 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1214 {
1215 struct nfsdfs_client *nc;
1216
1217 spin_lock(&inode->i_lock);
1218 nc = inode->i_private;
1219 if (nc)
1220 kref_get(&nc->cl_ref);
1221 spin_unlock(&inode->i_lock);
1222 return nc;
1223 }
1224
1225 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1226 * code instead. */
nfsdfs_create_files(struct dentry * root,const struct tree_descr * files,struct nfsdfs_client * ncl,struct dentry ** fdentries)1227 static int nfsdfs_create_files(struct dentry *root,
1228 const struct tree_descr *files,
1229 struct nfsdfs_client *ncl,
1230 struct dentry **fdentries)
1231 {
1232 struct inode *dir = d_inode(root);
1233 struct dentry *dentry;
1234
1235 for (int i = 0; files->name && files->name[0]; i++, files++) {
1236 struct inode *inode = nfsd_get_inode(root->d_sb,
1237 S_IFREG | files->mode);
1238 if (!inode)
1239 return -ENOMEM;
1240 dentry = simple_start_creating(root, files->name);
1241 if (IS_ERR(dentry)) {
1242 iput(inode);
1243 return PTR_ERR(dentry);
1244 }
1245 kref_get(&ncl->cl_ref);
1246 inode->i_fop = files->ops;
1247 inode->i_private = ncl;
1248 d_make_persistent(dentry, inode);
1249 fsnotify_create(dir, dentry);
1250 if (fdentries)
1251 fdentries[i] = dentry; // borrowed
1252 simple_done_creating(dentry);
1253 }
1254 return 0;
1255 }
1256
1257 /* on success, returns positive number unique to that client. */
nfsd_client_mkdir(struct nfsd_net * nn,struct nfsdfs_client * ncl,u32 id,const struct tree_descr * files,struct dentry ** fdentries)1258 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1259 struct nfsdfs_client *ncl, u32 id,
1260 const struct tree_descr *files,
1261 struct dentry **fdentries)
1262 {
1263 struct dentry *dentry;
1264 char name[11];
1265 int ret;
1266
1267 sprintf(name, "%u", id);
1268
1269 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1270 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1271 return NULL;
1272 ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1273 if (ret) {
1274 nfsd_client_rmdir(dentry);
1275 return NULL;
1276 }
1277 return dentry;
1278 }
1279
1280 /* Taken from __rpc_rmdir: */
nfsd_client_rmdir(struct dentry * dentry)1281 void nfsd_client_rmdir(struct dentry *dentry)
1282 {
1283 simple_recursive_removal(dentry, clear_ncl);
1284 }
1285
nfsd_fill_super(struct super_block * sb,struct fs_context * fc)1286 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1287 {
1288 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1289 nfsd_net_id);
1290 struct dentry *dentry;
1291 int ret;
1292
1293 static const struct tree_descr nfsd_files[] = {
1294 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1295 /* Per-export io stats use same ops as exports file */
1296 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1297 [NFSD_Export_features] = {"export_features",
1298 &export_features_fops, S_IRUGO},
1299 [NFSD_FO_UnlockIP] = {"unlock_ip",
1300 &transaction_ops, S_IWUSR|S_IRUSR},
1301 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1302 &transaction_ops, S_IWUSR|S_IRUSR},
1303 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1304 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1305 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1306 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1307 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1308 &nfsd_reply_cache_stats_fops, S_IRUGO},
1309 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1310 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1311 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1312 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1313 #ifdef CONFIG_NFSD_V4
1314 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1315 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1316 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1317 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1318 #endif
1319 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1320 #endif
1321 /* last one */ {""}
1322 };
1323
1324 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1325 if (ret)
1326 return ret;
1327 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1328 "/proc/net/rpc/gss_krb5_enctypes");
1329 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1330 if (IS_ERR(dentry))
1331 return PTR_ERR(dentry);
1332 nn->nfsd_client_dir = dentry;
1333 return 0;
1334 }
1335
nfsd_fs_get_tree(struct fs_context * fc)1336 static int nfsd_fs_get_tree(struct fs_context *fc)
1337 {
1338 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1339 }
1340
nfsd_fs_free_fc(struct fs_context * fc)1341 static void nfsd_fs_free_fc(struct fs_context *fc)
1342 {
1343 if (fc->s_fs_info)
1344 put_net(fc->s_fs_info);
1345 }
1346
1347 static const struct fs_context_operations nfsd_fs_context_ops = {
1348 .free = nfsd_fs_free_fc,
1349 .get_tree = nfsd_fs_get_tree,
1350 };
1351
nfsd_init_fs_context(struct fs_context * fc)1352 static int nfsd_init_fs_context(struct fs_context *fc)
1353 {
1354 put_user_ns(fc->user_ns);
1355 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1356 fc->ops = &nfsd_fs_context_ops;
1357 return 0;
1358 }
1359
nfsd_umount(struct super_block * sb)1360 static void nfsd_umount(struct super_block *sb)
1361 {
1362 struct net *net = sb->s_fs_info;
1363
1364 nfsd_shutdown_threads(net);
1365
1366 kill_anon_super(sb);
1367 put_net(net);
1368 }
1369
1370 static struct file_system_type nfsd_fs_type = {
1371 .owner = THIS_MODULE,
1372 .name = "nfsd",
1373 .init_fs_context = nfsd_init_fs_context,
1374 .kill_sb = nfsd_umount,
1375 };
1376 MODULE_ALIAS_FS("nfsd");
1377
1378 #ifdef CONFIG_PROC_FS
1379
exports_proc_open(struct inode * inode,struct file * file)1380 static int exports_proc_open(struct inode *inode, struct file *file)
1381 {
1382 return exports_net_open(current->nsproxy->net_ns, file);
1383 }
1384
1385 static const struct proc_ops exports_proc_ops = {
1386 .proc_open = exports_proc_open,
1387 .proc_read = seq_read,
1388 .proc_lseek = seq_lseek,
1389 .proc_release = exports_release,
1390 };
1391
create_proc_exports_entry(void)1392 static int create_proc_exports_entry(void)
1393 {
1394 struct proc_dir_entry *entry;
1395
1396 entry = proc_mkdir("fs/nfs", NULL);
1397 if (!entry)
1398 return -ENOMEM;
1399 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1400 if (!entry) {
1401 remove_proc_entry("fs/nfs", NULL);
1402 return -ENOMEM;
1403 }
1404 return 0;
1405 }
1406 #else /* CONFIG_PROC_FS */
create_proc_exports_entry(void)1407 static int create_proc_exports_entry(void)
1408 {
1409 return 0;
1410 }
1411 #endif
1412
1413 unsigned int nfsd_net_id;
1414
nfsd_genl_rpc_status_compose_msg(struct sk_buff * skb,struct netlink_callback * cb,struct nfsd_genl_rqstp * genl_rqstp)1415 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1416 struct netlink_callback *cb,
1417 struct nfsd_genl_rqstp *genl_rqstp)
1418 {
1419 void *hdr;
1420 u32 i;
1421
1422 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1423 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1424 if (!hdr)
1425 return -ENOBUFS;
1426
1427 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, genl_rqstp->rq_xid) ||
1428 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, genl_rqstp->rq_flags) ||
1429 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, genl_rqstp->rq_prog) ||
1430 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, genl_rqstp->rq_proc) ||
1431 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, genl_rqstp->rq_vers) ||
1432 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1433 ktime_to_us(genl_rqstp->rq_stime),
1434 NFSD_A_RPC_STATUS_PAD))
1435 return -ENOBUFS;
1436
1437 switch (genl_rqstp->rq_saddr.sa_family) {
1438 case AF_INET: {
1439 const struct sockaddr_in *s_in, *d_in;
1440
1441 s_in = (const struct sockaddr_in *)&genl_rqstp->rq_saddr;
1442 d_in = (const struct sockaddr_in *)&genl_rqstp->rq_daddr;
1443 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1444 s_in->sin_addr.s_addr) ||
1445 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1446 d_in->sin_addr.s_addr) ||
1447 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1448 s_in->sin_port) ||
1449 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1450 d_in->sin_port))
1451 return -ENOBUFS;
1452 break;
1453 }
1454 case AF_INET6: {
1455 const struct sockaddr_in6 *s_in, *d_in;
1456
1457 s_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_saddr;
1458 d_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_daddr;
1459 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1460 &s_in->sin6_addr) ||
1461 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1462 &d_in->sin6_addr) ||
1463 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1464 s_in->sin6_port) ||
1465 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1466 d_in->sin6_port))
1467 return -ENOBUFS;
1468 break;
1469 }
1470 }
1471
1472 for (i = 0; i < genl_rqstp->rq_opcnt; i++)
1473 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1474 genl_rqstp->rq_opnum[i]))
1475 return -ENOBUFS;
1476
1477 genlmsg_end(skb, hdr);
1478 return 0;
1479 }
1480
1481 /**
1482 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1483 * @skb: reply buffer
1484 * @cb: netlink metadata and command arguments
1485 *
1486 * Returns the size of the reply or a negative errno.
1487 */
nfsd_nl_rpc_status_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)1488 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1489 struct netlink_callback *cb)
1490 {
1491 int i, ret, rqstp_index = 0;
1492 struct nfsd_net *nn;
1493
1494 mutex_lock(&nfsd_mutex);
1495
1496 nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1497 if (!nn->nfsd_serv) {
1498 ret = -ENODEV;
1499 goto out_unlock;
1500 }
1501
1502 rcu_read_lock();
1503
1504 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1505 struct svc_rqst *rqstp;
1506
1507 if (i < cb->args[0]) /* already consumed */
1508 continue;
1509
1510 rqstp_index = 0;
1511 list_for_each_entry_rcu(rqstp,
1512 &nn->nfsd_serv->sv_pools[i].sp_all_threads,
1513 rq_all) {
1514 struct nfsd_genl_rqstp genl_rqstp;
1515 unsigned int status_counter;
1516
1517 if (rqstp_index++ < cb->args[1]) /* already consumed */
1518 continue;
1519 /*
1520 * Acquire rq_status_counter before parsing the rqst
1521 * fields. rq_status_counter is set to an odd value in
1522 * order to notify the consumers the rqstp fields are
1523 * meaningful.
1524 */
1525 status_counter =
1526 smp_load_acquire(&rqstp->rq_status_counter);
1527 if (!(status_counter & 1))
1528 continue;
1529
1530 genl_rqstp.rq_xid = rqstp->rq_xid;
1531 genl_rqstp.rq_flags = rqstp->rq_flags;
1532 genl_rqstp.rq_vers = rqstp->rq_vers;
1533 genl_rqstp.rq_prog = rqstp->rq_prog;
1534 genl_rqstp.rq_proc = rqstp->rq_proc;
1535 genl_rqstp.rq_stime = rqstp->rq_stime;
1536 genl_rqstp.rq_opcnt = 0;
1537 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1538 sizeof(struct sockaddr));
1539 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1540 sizeof(struct sockaddr));
1541
1542 #ifdef CONFIG_NFSD_V4
1543 if (rqstp->rq_vers == NFS4_VERSION &&
1544 rqstp->rq_proc == NFSPROC4_COMPOUND) {
1545 /* NFSv4 compound */
1546 struct nfsd4_compoundargs *args;
1547 int j;
1548
1549 args = rqstp->rq_argp;
1550 genl_rqstp.rq_opcnt = min_t(u32, args->opcnt,
1551 ARRAY_SIZE(genl_rqstp.rq_opnum));
1552 for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1553 genl_rqstp.rq_opnum[j] =
1554 args->ops[j].opnum;
1555 }
1556 #endif /* CONFIG_NFSD_V4 */
1557
1558 /*
1559 * Acquire rq_status_counter before reporting the rqst
1560 * fields to the user.
1561 */
1562 if (smp_load_acquire(&rqstp->rq_status_counter) !=
1563 status_counter)
1564 continue;
1565
1566 ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1567 &genl_rqstp);
1568 if (ret)
1569 goto out;
1570 }
1571 }
1572
1573 cb->args[0] = i;
1574 cb->args[1] = rqstp_index;
1575 ret = skb->len;
1576 out:
1577 rcu_read_unlock();
1578 out_unlock:
1579 mutex_unlock(&nfsd_mutex);
1580
1581 return ret;
1582 }
1583
1584 /**
1585 * nfsd_nl_fh_key_set - helper to copy fh_key from userspace
1586 * @attr: nlattr NFSD_A_SERVER_FH_KEY
1587 * @nn: nfsd_net
1588 *
1589 * Callers should hold nfsd_mutex, returns 0 on success or negative errno.
1590 * Callers must ensure the server is shut down (sv_nrthreads == 0),
1591 * userspace documentation asserts the key may only be set when the server
1592 * is not running.
1593 */
nfsd_nl_fh_key_set(const struct nlattr * attr,struct nfsd_net * nn)1594 static int nfsd_nl_fh_key_set(const struct nlattr *attr, struct nfsd_net *nn)
1595 {
1596 siphash_key_t *fh_key = nn->fh_key;
1597 u64 k0, k1;
1598 bool changed;
1599
1600 k0 = get_unaligned_le64(nla_data(attr));
1601 k1 = get_unaligned_le64(nla_data(attr) + 8);
1602
1603 if (!fh_key) {
1604 fh_key = kmalloc(sizeof(siphash_key_t), GFP_KERNEL);
1605 if (!fh_key) {
1606 trace_nfsd_ctl_fh_key_set(false, -ENOMEM);
1607 return -ENOMEM;
1608 }
1609 nn->fh_key = fh_key;
1610 changed = true;
1611 } else {
1612 changed = fh_key->key[0] != k0 || fh_key->key[1] != k1;
1613 }
1614
1615 fh_key->key[0] = k0;
1616 fh_key->key[1] = k1;
1617 trace_nfsd_ctl_fh_key_set(changed, 0);
1618 return 0;
1619 }
1620
1621 /**
1622 * nfsd_nl_threads_set_doit - set the number of running threads
1623 * @skb: reply buffer
1624 * @info: netlink metadata and command arguments
1625 *
1626 * Return 0 on success or a negative errno.
1627 */
nfsd_nl_threads_set_doit(struct sk_buff * skb,struct genl_info * info)1628 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1629 {
1630 int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem;
1631 struct net *net = genl_info_net(info);
1632 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1633 const struct nlattr *attr;
1634 const char *scope = NULL;
1635
1636 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1637 return -EINVAL;
1638
1639 /* count number of SERVER_THREADS values */
1640 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
1641 GENL_HDRLEN, rem)
1642 nrpools++;
1643
1644 mutex_lock(&nfsd_mutex);
1645
1646 nthreads = kzalloc_objs(int, nrpools);
1647 if (!nthreads) {
1648 ret = -ENOMEM;
1649 goto out_unlock;
1650 }
1651
1652 i = 0;
1653 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
1654 GENL_HDRLEN, rem) {
1655 nthreads[i++] = nla_get_u32(attr);
1656 if (i >= nrpools)
1657 break;
1658 }
1659
1660 if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1661 info->attrs[NFSD_A_SERVER_LEASETIME] ||
1662 info->attrs[NFSD_A_SERVER_SCOPE] ||
1663 info->attrs[NFSD_A_SERVER_FH_KEY]) {
1664 ret = -EBUSY;
1665 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1666 goto out_unlock;
1667
1668 ret = -EINVAL;
1669 attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1670 if (attr) {
1671 u32 gracetime = nla_get_u32(attr);
1672
1673 if (gracetime < 10 || gracetime > 3600)
1674 goto out_unlock;
1675
1676 nn->nfsd4_grace = gracetime;
1677 }
1678
1679 attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1680 if (attr) {
1681 u32 leasetime = nla_get_u32(attr);
1682
1683 if (leasetime < 10 || leasetime > 3600)
1684 goto out_unlock;
1685
1686 nn->nfsd4_lease = leasetime;
1687 }
1688
1689 attr = info->attrs[NFSD_A_SERVER_SCOPE];
1690 if (attr)
1691 scope = nla_data(attr);
1692
1693 attr = info->attrs[NFSD_A_SERVER_FH_KEY];
1694 if (attr) {
1695 ret = nfsd_nl_fh_key_set(attr, nn);
1696 if (ret)
1697 goto out_unlock;
1698 }
1699 }
1700
1701 attr = info->attrs[NFSD_A_SERVER_MIN_THREADS];
1702 if (attr)
1703 nn->min_threads = nla_get_u32(attr);
1704
1705 ret = nfsd_svc(nrpools, nthreads, net, current_cred(), scope);
1706 if (ret > 0)
1707 ret = 0;
1708 out_unlock:
1709 mutex_unlock(&nfsd_mutex);
1710 kfree(nthreads);
1711 return ret;
1712 }
1713
1714 /**
1715 * nfsd_nl_threads_get_doit - get the maximum number of running threads
1716 * @skb: reply buffer
1717 * @info: netlink metadata and command arguments
1718 *
1719 * Return 0 on success or a negative errno.
1720 */
nfsd_nl_threads_get_doit(struct sk_buff * skb,struct genl_info * info)1721 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1722 {
1723 struct net *net = genl_info_net(info);
1724 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1725 void *hdr;
1726 int err;
1727
1728 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1729 if (!skb)
1730 return -ENOMEM;
1731
1732 hdr = genlmsg_iput(skb, info);
1733 if (!hdr) {
1734 err = -EMSGSIZE;
1735 goto err_free_msg;
1736 }
1737
1738 mutex_lock(&nfsd_mutex);
1739
1740 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1741 nn->nfsd4_grace) ||
1742 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1743 nn->nfsd4_lease) ||
1744 nla_put_u32(skb, NFSD_A_SERVER_MIN_THREADS,
1745 nn->min_threads) ||
1746 nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1747 nn->nfsd_name);
1748 if (err)
1749 goto err_unlock;
1750
1751 if (nn->nfsd_serv) {
1752 int i;
1753
1754 for (i = 0; i < nfsd_nrpools(net); ++i) {
1755 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1756
1757 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1758 sp->sp_nrthrmax);
1759 if (err)
1760 goto err_unlock;
1761 }
1762 } else {
1763 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1764 if (err)
1765 goto err_unlock;
1766 }
1767
1768 mutex_unlock(&nfsd_mutex);
1769
1770 genlmsg_end(skb, hdr);
1771
1772 return genlmsg_reply(skb, info);
1773
1774 err_unlock:
1775 mutex_unlock(&nfsd_mutex);
1776 err_free_msg:
1777 nlmsg_free(skb);
1778
1779 return err;
1780 }
1781
1782 /**
1783 * nfsd_nl_version_set_doit - set the nfs enabled versions
1784 * @skb: reply buffer
1785 * @info: netlink metadata and command arguments
1786 *
1787 * Return 0 on success or a negative errno.
1788 */
nfsd_nl_version_set_doit(struct sk_buff * skb,struct genl_info * info)1789 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1790 {
1791 const struct nlattr *attr;
1792 struct nfsd_net *nn;
1793 int i, rem;
1794
1795 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1796 return -EINVAL;
1797
1798 mutex_lock(&nfsd_mutex);
1799
1800 nn = net_generic(genl_info_net(info), nfsd_net_id);
1801 if (nn->nfsd_serv) {
1802 mutex_unlock(&nfsd_mutex);
1803 return -EBUSY;
1804 }
1805
1806 /* clear current supported versions. */
1807 nfsd_vers(nn, 2, NFSD_CLEAR);
1808 nfsd_vers(nn, 3, NFSD_CLEAR);
1809 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1810 nfsd_minorversion(nn, i, NFSD_CLEAR);
1811
1812 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_PROTO_VERSION, info->nlhdr,
1813 GENL_HDRLEN, rem) {
1814 struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1815 u32 major, minor = 0;
1816 bool enabled;
1817
1818 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1819 nfsd_version_nl_policy, info->extack) < 0)
1820 continue;
1821
1822 if (!tb[NFSD_A_VERSION_MAJOR])
1823 continue;
1824
1825 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1826 if (tb[NFSD_A_VERSION_MINOR])
1827 minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1828
1829 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1830
1831 switch (major) {
1832 case 4:
1833 nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1834 break;
1835 case 3:
1836 case 2:
1837 if (!minor)
1838 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1839 break;
1840 default:
1841 break;
1842 }
1843 }
1844
1845 mutex_unlock(&nfsd_mutex);
1846
1847 return 0;
1848 }
1849
1850 /**
1851 * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1852 * @skb: reply buffer
1853 * @info: netlink metadata and command arguments
1854 *
1855 * Return 0 on success or a negative errno.
1856 */
nfsd_nl_version_get_doit(struct sk_buff * skb,struct genl_info * info)1857 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1858 {
1859 struct nfsd_net *nn;
1860 int i, err;
1861 void *hdr;
1862
1863 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1864 if (!skb)
1865 return -ENOMEM;
1866
1867 hdr = genlmsg_iput(skb, info);
1868 if (!hdr) {
1869 err = -EMSGSIZE;
1870 goto err_free_msg;
1871 }
1872
1873 mutex_lock(&nfsd_mutex);
1874 nn = net_generic(genl_info_net(info), nfsd_net_id);
1875
1876 for (i = 2; i <= 4; i++) {
1877 int j;
1878
1879 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1880 struct nlattr *attr;
1881
1882 /* Don't record any versions the kernel doesn't have
1883 * compiled in
1884 */
1885 if (!nfsd_support_version(i))
1886 continue;
1887
1888 /* NFSv{2,3} does not support minor numbers */
1889 if (i < 4 && j)
1890 continue;
1891
1892 attr = nla_nest_start(skb,
1893 NFSD_A_SERVER_PROTO_VERSION);
1894 if (!attr) {
1895 err = -EINVAL;
1896 goto err_nfsd_unlock;
1897 }
1898
1899 if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1900 nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1901 err = -EINVAL;
1902 goto err_nfsd_unlock;
1903 }
1904
1905 /* Set the enabled flag if the version is enabled */
1906 if (nfsd_vers(nn, i, NFSD_TEST) &&
1907 (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1908 nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1909 err = -EINVAL;
1910 goto err_nfsd_unlock;
1911 }
1912
1913 nla_nest_end(skb, attr);
1914 }
1915 }
1916
1917 mutex_unlock(&nfsd_mutex);
1918 genlmsg_end(skb, hdr);
1919
1920 return genlmsg_reply(skb, info);
1921
1922 err_nfsd_unlock:
1923 mutex_unlock(&nfsd_mutex);
1924 err_free_msg:
1925 nlmsg_free(skb);
1926
1927 return err;
1928 }
1929
1930 /**
1931 * nfsd_nl_listener_set_doit - set the nfs running sockets
1932 * @skb: reply buffer
1933 * @info: netlink metadata and command arguments
1934 *
1935 * Return 0 on success or a negative errno.
1936 */
nfsd_nl_listener_set_doit(struct sk_buff * skb,struct genl_info * info)1937 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1938 {
1939 struct net *net = genl_info_net(info);
1940 struct svc_xprt *xprt, *tmp;
1941 const struct nlattr *attr;
1942 struct svc_serv *serv;
1943 LIST_HEAD(permsocks);
1944 struct nfsd_net *nn;
1945 bool delete = false;
1946 int err, rem;
1947
1948 mutex_lock(&nfsd_mutex);
1949
1950 err = nfsd_create_serv(net);
1951 if (err) {
1952 mutex_unlock(&nfsd_mutex);
1953 return err;
1954 }
1955
1956 nn = net_generic(net, nfsd_net_id);
1957 serv = nn->nfsd_serv;
1958
1959 spin_lock_bh(&serv->sv_lock);
1960
1961 /* Move all of the old listener sockets to a temp list */
1962 list_splice_init(&serv->sv_permsocks, &permsocks);
1963
1964 /*
1965 * Walk the list of server_socks from userland and move any that match
1966 * back to sv_permsocks
1967 */
1968 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
1969 GENL_HDRLEN, rem) {
1970 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1971 const char *xcl_name;
1972 struct sockaddr *sa;
1973
1974 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1975 nfsd_sock_nl_policy, info->extack) < 0)
1976 continue;
1977
1978 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
1979 continue;
1980
1981 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
1982 continue;
1983
1984 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
1985 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
1986
1987 /* Put back any matching sockets */
1988 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
1989 /* This shouldn't be possible */
1990 if (WARN_ON_ONCE(xprt->xpt_net != net)) {
1991 list_move(&xprt->xpt_list, &serv->sv_permsocks);
1992 continue;
1993 }
1994
1995 /* If everything matches, put it back */
1996 if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
1997 rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
1998 list_move(&xprt->xpt_list, &serv->sv_permsocks);
1999 break;
2000 }
2001 }
2002 }
2003
2004 /*
2005 * If there are listener transports remaining on the permsocks list,
2006 * it means we were asked to remove a listener.
2007 */
2008 if (!list_empty(&permsocks)) {
2009 list_splice_init(&permsocks, &serv->sv_permsocks);
2010 delete = true;
2011 }
2012 spin_unlock_bh(&serv->sv_lock);
2013
2014 /* Do not remove listeners while there are active threads. */
2015 if (serv->sv_nrthreads) {
2016 err = -EBUSY;
2017 goto out_unlock_mtx;
2018 }
2019
2020 /*
2021 * Since we can't delete an arbitrary llist entry, destroy the
2022 * remaining listeners and recreate the list.
2023 */
2024 if (delete)
2025 svc_xprt_destroy_all(serv, net, false);
2026
2027 /* walk list of addrs again, open any that still don't exist */
2028 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
2029 GENL_HDRLEN, rem) {
2030 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2031 const char *xcl_name;
2032 struct sockaddr *sa;
2033 int ret;
2034
2035 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2036 nfsd_sock_nl_policy, info->extack) < 0)
2037 continue;
2038
2039 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2040 continue;
2041
2042 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2043 continue;
2044
2045 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2046 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2047
2048 xprt = svc_find_listener(serv, xcl_name, net, sa);
2049 if (xprt) {
2050 if (delete)
2051 WARN_ONCE(1, "Transport type=%s already exists\n",
2052 xcl_name);
2053 svc_xprt_put(xprt);
2054 continue;
2055 }
2056
2057 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, 0,
2058 current_cred());
2059 /* always save the latest error */
2060 if (ret < 0)
2061 err = ret;
2062 }
2063
2064 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2065 nfsd_destroy_serv(net);
2066
2067 out_unlock_mtx:
2068 mutex_unlock(&nfsd_mutex);
2069
2070 return err;
2071 }
2072
2073 /**
2074 * nfsd_nl_listener_get_doit - get the nfs running listeners
2075 * @skb: reply buffer
2076 * @info: netlink metadata and command arguments
2077 *
2078 * Return 0 on success or a negative errno.
2079 */
nfsd_nl_listener_get_doit(struct sk_buff * skb,struct genl_info * info)2080 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2081 {
2082 struct svc_xprt *xprt;
2083 struct svc_serv *serv;
2084 struct nfsd_net *nn;
2085 void *hdr;
2086 int err;
2087
2088 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2089 if (!skb)
2090 return -ENOMEM;
2091
2092 hdr = genlmsg_iput(skb, info);
2093 if (!hdr) {
2094 err = -EMSGSIZE;
2095 goto err_free_msg;
2096 }
2097
2098 mutex_lock(&nfsd_mutex);
2099 nn = net_generic(genl_info_net(info), nfsd_net_id);
2100
2101 /* no nfs server? Just send empty socket list */
2102 if (!nn->nfsd_serv)
2103 goto out_unlock_mtx;
2104
2105 serv = nn->nfsd_serv;
2106 spin_lock_bh(&serv->sv_lock);
2107 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2108 struct nlattr *attr;
2109
2110 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2111 if (!attr) {
2112 err = -EINVAL;
2113 goto err_serv_unlock;
2114 }
2115
2116 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2117 xprt->xpt_class->xcl_name) ||
2118 nla_put(skb, NFSD_A_SOCK_ADDR,
2119 sizeof(struct sockaddr_storage),
2120 &xprt->xpt_local)) {
2121 err = -EINVAL;
2122 goto err_serv_unlock;
2123 }
2124
2125 nla_nest_end(skb, attr);
2126 }
2127 spin_unlock_bh(&serv->sv_lock);
2128 out_unlock_mtx:
2129 mutex_unlock(&nfsd_mutex);
2130 genlmsg_end(skb, hdr);
2131
2132 return genlmsg_reply(skb, info);
2133
2134 err_serv_unlock:
2135 spin_unlock_bh(&serv->sv_lock);
2136 mutex_unlock(&nfsd_mutex);
2137 err_free_msg:
2138 nlmsg_free(skb);
2139
2140 return err;
2141 }
2142
2143 /**
2144 * nfsd_nl_pool_mode_set_doit - set the number of running threads
2145 * @skb: reply buffer
2146 * @info: netlink metadata and command arguments
2147 *
2148 * Return 0 on success or a negative errno.
2149 */
nfsd_nl_pool_mode_set_doit(struct sk_buff * skb,struct genl_info * info)2150 int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info)
2151 {
2152 const struct nlattr *attr;
2153
2154 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE))
2155 return -EINVAL;
2156
2157 attr = info->attrs[NFSD_A_POOL_MODE_MODE];
2158 return sunrpc_set_pool_mode(nla_data(attr));
2159 }
2160
2161 /**
2162 * nfsd_nl_pool_mode_get_doit - get info about pool_mode
2163 * @skb: reply buffer
2164 * @info: netlink metadata and command arguments
2165 *
2166 * Return 0 on success or a negative errno.
2167 */
nfsd_nl_pool_mode_get_doit(struct sk_buff * skb,struct genl_info * info)2168 int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info)
2169 {
2170 struct net *net = genl_info_net(info);
2171 char buf[16];
2172 void *hdr;
2173 int err;
2174
2175 if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
2176 return -ERANGE;
2177
2178 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2179 if (!skb)
2180 return -ENOMEM;
2181
2182 err = -EMSGSIZE;
2183 hdr = genlmsg_iput(skb, info);
2184 if (!hdr)
2185 goto err_free_msg;
2186
2187 err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) |
2188 nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net));
2189 if (err)
2190 goto err_free_msg;
2191
2192 genlmsg_end(skb, hdr);
2193 return genlmsg_reply(skb, info);
2194
2195 err_free_msg:
2196 nlmsg_free(skb);
2197 return err;
2198 }
2199
2200 /**
2201 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2202 * @net: a freshly-created network namespace
2203 *
2204 * This information stays around as long as the network namespace is
2205 * alive whether or not there is an NFSD instance running in the
2206 * namespace.
2207 *
2208 * Returns zero on success, or a negative errno otherwise.
2209 */
nfsd_net_init(struct net * net)2210 static __net_init int nfsd_net_init(struct net *net)
2211 {
2212 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2213 int retval;
2214 int i;
2215
2216 retval = nfsd_net_cb_init(nn);
2217 if (retval)
2218 return retval;
2219 retval = nfsd_export_init(net);
2220 if (retval)
2221 goto out_export_error;
2222 retval = nfsd_idmap_init(net);
2223 if (retval)
2224 goto out_idmap_error;
2225 retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2226 NFSD_STATS_COUNTERS_NUM);
2227 if (retval)
2228 goto out_repcache_error;
2229
2230 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2231 nn->nfsd_svcstats.program = &nfsd_programs[0];
2232 if (!nfsd_proc_stat_init(net)) {
2233 retval = -ENOMEM;
2234 goto out_proc_error;
2235 }
2236
2237 for (i = 0; i < sizeof(nn->nfsd_versions); i++)
2238 nn->nfsd_versions[i] = nfsd_support_version(i);
2239 for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++)
2240 nn->nfsd4_minorversions[i] = nfsd_support_version(4);
2241 nn->nfsd_info.mutex = &nfsd_mutex;
2242 nn->nfsd_serv = NULL;
2243 nfsd4_init_leases_net(nn);
2244 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2245 seqlock_init(&nn->writeverf_lock);
2246 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2247 spin_lock_init(&nn->local_clients_lock);
2248 INIT_LIST_HEAD(&nn->local_clients);
2249 #endif
2250 return 0;
2251
2252 out_proc_error:
2253 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2254 out_repcache_error:
2255 nfsd_idmap_shutdown(net);
2256 out_idmap_error:
2257 nfsd_export_shutdown(net);
2258 out_export_error:
2259 nfsd_net_cb_shutdown(nn);
2260 return retval;
2261 }
2262
2263 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2264 /**
2265 * nfsd_net_pre_exit - Disconnect localio clients from net namespace
2266 * @net: a network namespace that is about to be destroyed
2267 *
2268 * This invalidates ->net pointers held by localio clients
2269 * while they can still safely access nn->counter.
2270 */
nfsd_net_pre_exit(struct net * net)2271 static __net_exit void nfsd_net_pre_exit(struct net *net)
2272 {
2273 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2274
2275 nfs_localio_invalidate_clients(&nn->local_clients,
2276 &nn->local_clients_lock);
2277 }
2278 #endif
2279
2280 /**
2281 * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2282 * @net: a network namespace that is about to be destroyed
2283 *
2284 */
nfsd_net_exit(struct net * net)2285 static __net_exit void nfsd_net_exit(struct net *net)
2286 {
2287 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2288
2289 kfree_sensitive(nn->fh_key);
2290 nfsd_net_cb_shutdown(nn);
2291 nfsd_proc_stat_shutdown(net);
2292 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2293 nfsd_idmap_shutdown(net);
2294 nfsd_export_shutdown(net);
2295 }
2296
2297 static struct pernet_operations nfsd_net_ops = {
2298 .init = nfsd_net_init,
2299 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2300 .pre_exit = nfsd_net_pre_exit,
2301 #endif
2302 .exit = nfsd_net_exit,
2303 .id = &nfsd_net_id,
2304 .size = sizeof(struct nfsd_net),
2305 };
2306
init_nfsd(void)2307 static int __init init_nfsd(void)
2308 {
2309 int retval;
2310
2311 nfsd_debugfs_init();
2312
2313 retval = nfsd4_init_slabs();
2314 if (retval)
2315 return retval;
2316 retval = nfsd4_init_pnfs();
2317 if (retval)
2318 goto out_free_slabs;
2319 retval = nfsd_drc_slab_create();
2320 if (retval)
2321 goto out_free_pnfs;
2322 nfsd_lockd_init(); /* lockd->nfsd callbacks */
2323 retval = nfsd_export_wq_init();
2324 if (retval)
2325 goto out_free_lockd;
2326 retval = register_pernet_subsys(&nfsd_net_ops);
2327 if (retval < 0)
2328 goto out_free_export_wq;
2329 retval = register_cld_notifier();
2330 if (retval)
2331 goto out_free_subsys;
2332 retval = nfsd4_create_laundry_wq();
2333 if (retval)
2334 goto out_free_cld;
2335 retval = register_filesystem(&nfsd_fs_type);
2336 if (retval)
2337 goto out_free_nfsd4;
2338 retval = genl_register_family(&nfsd_nl_family);
2339 if (retval)
2340 goto out_free_filesystem;
2341 retval = create_proc_exports_entry();
2342 if (retval)
2343 goto out_free_all;
2344 nfsd_localio_ops_init();
2345
2346 return 0;
2347 out_free_all:
2348 genl_unregister_family(&nfsd_nl_family);
2349 out_free_filesystem:
2350 unregister_filesystem(&nfsd_fs_type);
2351 out_free_nfsd4:
2352 nfsd4_destroy_laundry_wq();
2353 out_free_cld:
2354 unregister_cld_notifier();
2355 out_free_subsys:
2356 unregister_pernet_subsys(&nfsd_net_ops);
2357 out_free_export_wq:
2358 nfsd_export_wq_shutdown();
2359 out_free_lockd:
2360 nfsd_lockd_shutdown();
2361 nfsd_drc_slab_free();
2362 out_free_pnfs:
2363 nfsd4_exit_pnfs();
2364 out_free_slabs:
2365 nfsd4_free_slabs();
2366 nfsd_debugfs_exit();
2367 return retval;
2368 }
2369
exit_nfsd(void)2370 static void __exit exit_nfsd(void)
2371 {
2372 remove_proc_entry("fs/nfs/exports", NULL);
2373 remove_proc_entry("fs/nfs", NULL);
2374 genl_unregister_family(&nfsd_nl_family);
2375 unregister_filesystem(&nfsd_fs_type);
2376 nfsd4_destroy_laundry_wq();
2377 unregister_cld_notifier();
2378 unregister_pernet_subsys(&nfsd_net_ops);
2379 nfsd_export_wq_shutdown();
2380 nfsd_drc_slab_free();
2381 nfsd_lockd_shutdown();
2382 nfsd4_free_slabs();
2383 nfsd4_exit_pnfs();
2384 nfsd_debugfs_exit();
2385 }
2386
2387 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2388 MODULE_DESCRIPTION("In-kernel NFS server");
2389 MODULE_LICENSE("GPL");
2390 module_init(init_nfsd)
2391 module_exit(exit_nfsd)
2392