~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfsd/nfsctl.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php