GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / nfsd / nfsctl.c
blobd90a3dadf8e17731687fd9537eaa82c73ffb0336
1 /*
2 * Syscall interface to knfsd.
4 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
5 */
7 #include <linux/slab.h>
8 #include <linux/namei.h>
9 #include <linux/ctype.h>
11 #include <linux/nfsd_idmap.h>
12 #include <linux/sunrpc/svcsock.h>
13 #include <linux/nfsd/syscall.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/clnt.h>
17 #include "nfsd.h"
18 #include "cache.h"
21 * We have a single directory with 9 nodes in it.
23 enum {
24 NFSD_Root = 1,
25 NFSD_Svc,
26 NFSD_Add,
27 NFSD_Del,
28 NFSD_Export,
29 NFSD_Unexport,
30 NFSD_Getfd,
31 NFSD_Getfs,
32 NFSD_List,
33 NFSD_Export_features,
34 NFSD_Fh,
35 NFSD_FO_UnlockIP,
36 NFSD_FO_UnlockFS,
37 NFSD_Threads,
38 NFSD_Pool_Threads,
39 NFSD_Pool_Stats,
40 NFSD_Versions,
41 NFSD_Ports,
42 NFSD_MaxBlkSize,
44 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
45 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
47 #ifdef CONFIG_NFSD_V4
48 NFSD_Leasetime,
49 NFSD_Gracetime,
50 NFSD_RecoveryDir,
51 #endif
55 * write() for these nodes.
57 static ssize_t write_svc(struct file *file, char *buf, size_t size);
58 static ssize_t write_add(struct file *file, char *buf, size_t size);
59 static ssize_t write_del(struct file *file, char *buf, size_t size);
60 static ssize_t write_export(struct file *file, char *buf, size_t size);
61 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
62 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
63 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
64 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
65 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
66 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
67 static ssize_t write_threads(struct file *file, char *buf, size_t size);
68 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
69 static ssize_t write_versions(struct file *file, char *buf, size_t size);
70 static ssize_t write_ports(struct file *file, char *buf, size_t size);
71 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
72 #ifdef CONFIG_NFSD_V4
73 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
74 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
75 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
76 #endif
78 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
79 [NFSD_Svc] = write_svc,
80 [NFSD_Add] = write_add,
81 [NFSD_Del] = write_del,
82 [NFSD_Export] = write_export,
83 [NFSD_Unexport] = write_unexport,
84 [NFSD_Getfd] = write_getfd,
85 [NFSD_Getfs] = write_getfs,
86 [NFSD_Fh] = write_filehandle,
87 [NFSD_FO_UnlockIP] = write_unlock_ip,
88 [NFSD_FO_UnlockFS] = write_unlock_fs,
89 [NFSD_Threads] = write_threads,
90 [NFSD_Pool_Threads] = write_pool_threads,
91 [NFSD_Versions] = write_versions,
92 [NFSD_Ports] = write_ports,
93 [NFSD_MaxBlkSize] = write_maxblksize,
94 #ifdef CONFIG_NFSD_V4
95 [NFSD_Leasetime] = write_leasetime,
96 [NFSD_Gracetime] = write_gracetime,
97 [NFSD_RecoveryDir] = write_recoverydir,
98 #endif
101 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
103 ino_t ino = file->f_path.dentry->d_inode->i_ino;
104 char *data;
105 ssize_t rv;
107 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
108 return -EINVAL;
110 data = simple_transaction_get(file, buf, size);
111 if (IS_ERR(data))
112 return PTR_ERR(data);
114 rv = write_op[ino](file, data, size);
115 if (rv >= 0) {
116 simple_transaction_set(file, rv);
117 rv = size;
119 return rv;
122 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
124 if (! file->private_data) {
125 /* An attempt to read a transaction file without writing
126 * causes a 0-byte write so that the file can return
127 * state information
129 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
130 if (rv < 0)
131 return rv;
133 return simple_transaction_read(file, buf, size, pos);
136 static const struct file_operations transaction_ops = {
137 .write = nfsctl_transaction_write,
138 .read = nfsctl_transaction_read,
139 .release = simple_transaction_release,
142 static int exports_open(struct inode *inode, struct file *file)
144 return seq_open(file, &nfs_exports_op);
147 static const struct file_operations exports_operations = {
148 .open = exports_open,
149 .read = seq_read,
150 .llseek = seq_lseek,
151 .release = seq_release,
152 .owner = THIS_MODULE,
155 static int export_features_show(struct seq_file *m, void *v)
157 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
158 return 0;
161 static int export_features_open(struct inode *inode, struct file *file)
163 return single_open(file, export_features_show, NULL);
166 static struct file_operations export_features_operations = {
167 .open = export_features_open,
168 .read = seq_read,
169 .llseek = seq_lseek,
170 .release = single_release,
173 extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
174 extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
176 static const struct file_operations pool_stats_operations = {
177 .open = nfsd_pool_stats_open,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = nfsd_pool_stats_release,
181 .owner = THIS_MODULE,
184 /*----------------------------------------------------------------------------*/
186 * payload - write methods
190 * write_svc - Start kernel's NFSD server
192 * Deprecated. /proc/fs/nfsd/threads is preferred.
193 * Function remains to support old versions of nfs-utils.
195 * Input:
196 * buf: struct nfsctl_svc
197 * svc_port: port number of this
198 * server's listener
199 * svc_nthreads: number of threads to start
200 * size: size in bytes of passed in nfsctl_svc
201 * Output:
202 * On success: returns zero
203 * On error: return code is negative errno value
205 static ssize_t write_svc(struct file *file, char *buf, size_t size)
207 struct nfsctl_svc *data;
208 int err;
209 if (size < sizeof(*data))
210 return -EINVAL;
211 data = (struct nfsctl_svc*) buf;
212 err = nfsd_svc(data->svc_port, data->svc_nthreads);
213 if (err < 0)
214 return err;
215 return 0;
219 * write_add - Add or modify client entry in auth unix cache
221 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
222 * Function remains to support old versions of nfs-utils.
224 * Input:
225 * buf: struct nfsctl_client
226 * cl_ident: '\0'-terminated C string
227 * containing domain name
228 * of client
229 * cl_naddr: no. of items in cl_addrlist
230 * cl_addrlist: array of client addresses
231 * cl_fhkeytype: ignored
232 * cl_fhkeylen: ignored
233 * cl_fhkey: ignored
234 * size: size in bytes of passed in nfsctl_client
235 * Output:
236 * On success: returns zero
237 * On error: return code is negative errno value
239 * Note: Only AF_INET client addresses are passed in, since
240 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
242 static ssize_t write_add(struct file *file, char *buf, size_t size)
244 struct nfsctl_client *data;
245 if (size < sizeof(*data))
246 return -EINVAL;
247 data = (struct nfsctl_client *)buf;
248 return exp_addclient(data);
252 * write_del - Remove client from auth unix cache
254 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
255 * Function remains to support old versions of nfs-utils.
257 * Input:
258 * buf: struct nfsctl_client
259 * cl_ident: '\0'-terminated C string
260 * containing domain name
261 * of client
262 * cl_naddr: ignored
263 * cl_addrlist: ignored
264 * cl_fhkeytype: ignored
265 * cl_fhkeylen: ignored
266 * cl_fhkey: ignored
267 * size: size in bytes of passed in nfsctl_client
268 * Output:
269 * On success: returns zero
270 * On error: return code is negative errno value
272 * Note: Only AF_INET client addresses are passed in, since
273 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
275 static ssize_t write_del(struct file *file, char *buf, size_t size)
277 struct nfsctl_client *data;
278 if (size < sizeof(*data))
279 return -EINVAL;
280 data = (struct nfsctl_client *)buf;
281 return exp_delclient(data);
285 * write_export - Export part or all of a local file system
287 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
288 * Function remains to support old versions of nfs-utils.
290 * Input:
291 * buf: struct nfsctl_export
292 * ex_client: '\0'-terminated C string
293 * containing domain name
294 * of client allowed to access
295 * this export
296 * ex_path: '\0'-terminated C string
297 * containing pathname of
298 * directory in local file system
299 * ex_dev: fsid to use for this export
300 * ex_ino: ignored
301 * ex_flags: export flags for this export
302 * ex_anon_uid: UID to use for anonymous
303 * requests
304 * ex_anon_gid: GID to use for anonymous
305 * requests
306 * size: size in bytes of passed in nfsctl_export
307 * Output:
308 * On success: returns zero
309 * On error: return code is negative errno value
311 static ssize_t write_export(struct file *file, char *buf, size_t size)
313 struct nfsctl_export *data;
314 if (size < sizeof(*data))
315 return -EINVAL;
316 data = (struct nfsctl_export*)buf;
317 return exp_export(data);
321 * write_unexport - Unexport a previously exported file system
323 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
324 * Function remains to support old versions of nfs-utils.
326 * Input:
327 * buf: struct nfsctl_export
328 * ex_client: '\0'-terminated C string
329 * containing domain name
330 * of client no longer allowed
331 * to access this export
332 * ex_path: '\0'-terminated C string
333 * containing pathname of
334 * directory in local file system
335 * ex_dev: ignored
336 * ex_ino: ignored
337 * ex_flags: ignored
338 * ex_anon_uid: ignored
339 * ex_anon_gid: ignored
340 * size: size in bytes of passed in nfsctl_export
341 * Output:
342 * On success: returns zero
343 * On error: return code is negative errno value
345 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
347 struct nfsctl_export *data;
349 if (size < sizeof(*data))
350 return -EINVAL;
351 data = (struct nfsctl_export*)buf;
352 return exp_unexport(data);
356 * write_getfs - Get a variable-length NFS file handle by path
358 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
359 * Function remains to support old versions of nfs-utils.
361 * Input:
362 * buf: struct nfsctl_fsparm
363 * gd_addr: socket address of client
364 * gd_path: '\0'-terminated C string
365 * containing pathname of
366 * directory in local file system
367 * gd_maxlen: maximum size of returned file
368 * handle
369 * size: size in bytes of passed in nfsctl_fsparm
370 * Output:
371 * On success: passed-in buffer filled with a knfsd_fh structure
372 * (a variable-length raw NFS file handle);
373 * return code is the size in bytes of the file handle
374 * On error: return code is negative errno value
376 * Note: Only AF_INET client addresses are passed in, since gd_addr
377 * is the same size as a struct sockaddr_in.
379 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
381 struct nfsctl_fsparm *data;
382 struct sockaddr_in *sin;
383 struct auth_domain *clp;
384 int err = 0;
385 struct knfsd_fh *res;
386 struct in6_addr in6;
388 if (size < sizeof(*data))
389 return -EINVAL;
390 data = (struct nfsctl_fsparm*)buf;
391 err = -EPROTONOSUPPORT;
392 if (data->gd_addr.sa_family != AF_INET)
393 goto out;
394 sin = (struct sockaddr_in *)&data->gd_addr;
395 if (data->gd_maxlen > NFS3_FHSIZE)
396 data->gd_maxlen = NFS3_FHSIZE;
398 res = (struct knfsd_fh*)buf;
400 exp_readlock();
402 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
404 clp = auth_unix_lookup(&in6);
405 if (!clp)
406 err = -EPERM;
407 else {
408 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
409 auth_domain_put(clp);
411 exp_readunlock();
412 if (err == 0)
413 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
414 out:
415 return err;
419 * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
421 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
422 * Function remains to support old versions of nfs-utils.
424 * Input:
425 * buf: struct nfsctl_fdparm
426 * gd_addr: socket address of client
427 * gd_path: '\0'-terminated C string
428 * containing pathname of
429 * directory in local file system
430 * gd_version: fdparm structure version
431 * size: size in bytes of passed in nfsctl_fdparm
432 * Output:
433 * On success: passed-in buffer filled with nfsctl_res
434 * (a fixed-length raw NFS file handle);
435 * return code is the size in bytes of the file handle
436 * On error: return code is negative errno value
438 * Note: Only AF_INET client addresses are passed in, since gd_addr
439 * is the same size as a struct sockaddr_in.
441 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
443 struct nfsctl_fdparm *data;
444 struct sockaddr_in *sin;
445 struct auth_domain *clp;
446 int err = 0;
447 struct knfsd_fh fh;
448 char *res;
449 struct in6_addr in6;
451 if (size < sizeof(*data))
452 return -EINVAL;
453 data = (struct nfsctl_fdparm*)buf;
454 err = -EPROTONOSUPPORT;
455 if (data->gd_addr.sa_family != AF_INET)
456 goto out;
457 err = -EINVAL;
458 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
459 goto out;
461 res = buf;
462 sin = (struct sockaddr_in *)&data->gd_addr;
463 exp_readlock();
465 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
467 clp = auth_unix_lookup(&in6);
468 if (!clp)
469 err = -EPERM;
470 else {
471 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
472 auth_domain_put(clp);
474 exp_readunlock();
476 if (err == 0) {
477 memset(res,0, NFS_FHSIZE);
478 memcpy(res, &fh.fh_base, fh.fh_size);
479 err = NFS_FHSIZE;
481 out:
482 return err;
486 * write_unlock_ip - Release all locks used by a client
488 * Experimental.
490 * Input:
491 * buf: '\n'-terminated C string containing a
492 * presentation format IP address
493 * size: length of C string in @buf
494 * Output:
495 * On success: returns zero if all specified locks were released;
496 * returns one if one or more locks were not released
497 * On error: return code is negative errno value
499 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
501 struct sockaddr_storage address;
502 struct sockaddr *sap = (struct sockaddr *)&address;
503 size_t salen = sizeof(address);
504 char *fo_path;
506 /* sanity check */
507 if (size == 0)
508 return -EINVAL;
510 if (buf[size-1] != '\n')
511 return -EINVAL;
513 fo_path = buf;
514 if (qword_get(&buf, fo_path, size) < 0)
515 return -EINVAL;
517 if (rpc_pton(fo_path, size, sap, salen) == 0)
518 return -EINVAL;
520 return nlmsvc_unlock_all_by_ip(sap);
524 * write_unlock_fs - Release all locks on a local file system
526 * Experimental.
528 * Input:
529 * buf: '\n'-terminated C string containing the
530 * absolute pathname of a local file system
531 * size: length of C string in @buf
532 * Output:
533 * On success: returns zero if all specified locks were released;
534 * returns one if one or more locks were not released
535 * On error: return code is negative errno value
537 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
539 struct path path;
540 char *fo_path;
541 int error;
543 /* sanity check */
544 if (size == 0)
545 return -EINVAL;
547 if (buf[size-1] != '\n')
548 return -EINVAL;
550 fo_path = buf;
551 if (qword_get(&buf, fo_path, size) < 0)
552 return -EINVAL;
554 error = kern_path(fo_path, 0, &path);
555 if (error)
556 return error;
558 error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
560 path_put(&path);
561 return error;
565 * write_filehandle - Get a variable-length NFS file handle by path
567 * On input, the buffer contains a '\n'-terminated C string comprised of
568 * three alphanumeric words separated by whitespace. The string may
569 * contain escape sequences.
571 * Input:
572 * buf:
573 * domain: client domain name
574 * path: export pathname
575 * maxsize: numeric maximum size of
576 * @buf
577 * size: length of C string in @buf
578 * Output:
579 * On success: passed-in buffer filled with '\n'-terminated C
580 * string containing a ASCII hex text version
581 * of the NFS file handle;
582 * return code is the size in bytes of the string
583 * On error: return code is negative errno value
585 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
587 char *dname, *path;
588 int uninitialized_var(maxsize);
589 char *mesg = buf;
590 int len;
591 struct auth_domain *dom;
592 struct knfsd_fh fh;
594 if (size == 0)
595 return -EINVAL;
597 if (buf[size-1] != '\n')
598 return -EINVAL;
599 buf[size-1] = 0;
601 dname = mesg;
602 len = qword_get(&mesg, dname, size);
603 if (len <= 0)
604 return -EINVAL;
606 path = dname+len+1;
607 len = qword_get(&mesg, path, size);
608 if (len <= 0)
609 return -EINVAL;
611 len = get_int(&mesg, &maxsize);
612 if (len)
613 return len;
615 if (maxsize < NFS_FHSIZE)
616 return -EINVAL;
617 if (maxsize > NFS3_FHSIZE)
618 maxsize = NFS3_FHSIZE;
620 if (qword_get(&mesg, mesg, size)>0)
621 return -EINVAL;
623 /* we have all the words, they are in buf.. */
624 dom = unix_domain_find(dname);
625 if (!dom)
626 return -ENOMEM;
628 len = exp_rootfh(dom, path, &fh, maxsize);
629 auth_domain_put(dom);
630 if (len)
631 return len;
633 mesg = buf;
634 len = SIMPLE_TRANSACTION_LIMIT;
635 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
636 mesg[-1] = '\n';
637 return mesg - buf;
641 * write_threads - Start NFSD, or report the current number of running threads
643 * Input:
644 * buf: ignored
645 * size: zero
646 * Output:
647 * On success: passed-in buffer filled with '\n'-terminated C
648 * string numeric value representing the number of
649 * running NFSD threads;
650 * return code is the size in bytes of the string
651 * On error: return code is zero
653 * OR
655 * Input:
656 * buf: C string containing an unsigned
657 * integer value representing the
658 * number of NFSD threads to start
659 * size: non-zero length of C string in @buf
660 * Output:
661 * On success: NFS service is started;
662 * passed-in buffer filled with '\n'-terminated C
663 * string numeric value representing the number of
664 * running NFSD threads;
665 * return code is the size in bytes of the string
666 * On error: return code is zero or a negative errno value
668 static ssize_t write_threads(struct file *file, char *buf, size_t size)
670 char *mesg = buf;
671 int rv;
672 if (size > 0) {
673 int newthreads;
674 rv = get_int(&mesg, &newthreads);
675 if (rv)
676 return rv;
677 if (newthreads < 0)
678 return -EINVAL;
679 rv = nfsd_svc(NFS_PORT, newthreads);
680 if (rv < 0)
681 return rv;
682 } else
683 rv = nfsd_nrthreads();
685 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
689 * write_pool_threads - Set or report the current number of threads per pool
691 * Input:
692 * buf: ignored
693 * size: zero
695 * OR
697 * Input:
698 * buf: C string containing whitespace-
699 * separated unsigned integer values
700 * representing the number of NFSD
701 * threads to start in each pool
702 * size: non-zero length of C string in @buf
703 * Output:
704 * On success: passed-in buffer filled with '\n'-terminated C
705 * string containing integer values representing the
706 * number of NFSD threads in each pool;
707 * return code is the size in bytes of the string
708 * On error: return code is zero or a negative errno value
710 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
712 /* if size > 0, look for an array of number of threads per node
713 * and apply them then write out number of threads per node as reply
715 char *mesg = buf;
716 int i;
717 int rv;
718 int len;
719 int npools;
720 int *nthreads;
722 mutex_lock(&nfsd_mutex);
723 npools = nfsd_nrpools();
724 if (npools == 0) {
726 * NFS is shut down. The admin can start it by
727 * writing to the threads file but NOT the pool_threads
728 * file, sorry. Report zero threads.
730 mutex_unlock(&nfsd_mutex);
731 strcpy(buf, "0\n");
732 return strlen(buf);
735 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
736 rv = -ENOMEM;
737 if (nthreads == NULL)
738 goto out_free;
740 if (size > 0) {
741 for (i = 0; i < npools; i++) {
742 rv = get_int(&mesg, &nthreads[i]);
743 if (rv == -ENOENT)
744 break; /* fewer numbers than pools */
745 if (rv)
746 goto out_free; /* syntax error */
747 rv = -EINVAL;
748 if (nthreads[i] < 0)
749 goto out_free;
751 rv = nfsd_set_nrthreads(i, nthreads);
752 if (rv)
753 goto out_free;
756 rv = nfsd_get_nrthreads(npools, nthreads);
757 if (rv)
758 goto out_free;
760 mesg = buf;
761 size = SIMPLE_TRANSACTION_LIMIT;
762 for (i = 0; i < npools && size > 0; i++) {
763 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
764 len = strlen(mesg);
765 size -= len;
766 mesg += len;
768 rv = mesg - buf;
769 out_free:
770 kfree(nthreads);
771 mutex_unlock(&nfsd_mutex);
772 return rv;
775 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
777 char *mesg = buf;
778 char *vers, *minorp, sign;
779 int len, num, remaining;
780 unsigned minor;
781 ssize_t tlen = 0;
782 char *sep;
784 if (size>0) {
785 if (nfsd_serv)
786 /* Cannot change versions without updating
787 * nfsd_serv->sv_xdrsize, and reallocing
788 * rq_argp and rq_resp
790 return -EBUSY;
791 if (buf[size-1] != '\n')
792 return -EINVAL;
793 buf[size-1] = 0;
795 vers = mesg;
796 len = qword_get(&mesg, vers, size);
797 if (len <= 0) return -EINVAL;
798 do {
799 sign = *vers;
800 if (sign == '+' || sign == '-')
801 num = simple_strtol((vers+1), &minorp, 0);
802 else
803 num = simple_strtol(vers, &minorp, 0);
804 if (*minorp == '.') {
805 if (num < 4)
806 return -EINVAL;
807 minor = simple_strtoul(minorp+1, NULL, 0);
808 if (minor == 0)
809 return -EINVAL;
810 if (nfsd_minorversion(minor, sign == '-' ?
811 NFSD_CLEAR : NFSD_SET) < 0)
812 return -EINVAL;
813 goto next;
815 switch(num) {
816 case 2:
817 case 3:
818 case 4:
819 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
820 break;
821 default:
822 return -EINVAL;
824 next:
825 vers += len + 1;
826 } while ((len = qword_get(&mesg, vers, size)) > 0);
827 /* If all get turned off, turn them back on, as
828 * having no versions is BAD
830 nfsd_reset_versions();
833 /* Now write current state into reply buffer */
834 len = 0;
835 sep = "";
836 remaining = SIMPLE_TRANSACTION_LIMIT;
837 for (num=2 ; num <= 4 ; num++)
838 if (nfsd_vers(num, NFSD_AVAIL)) {
839 len = snprintf(buf, remaining, "%s%c%d", sep,
840 nfsd_vers(num, NFSD_TEST)?'+':'-',
841 num);
842 sep = " ";
844 if (len > remaining)
845 break;
846 remaining -= len;
847 buf += len;
848 tlen += len;
850 if (nfsd_vers(4, NFSD_AVAIL))
851 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
852 minor++) {
853 len = snprintf(buf, remaining, " %c4.%u",
854 (nfsd_vers(4, NFSD_TEST) &&
855 nfsd_minorversion(minor, NFSD_TEST)) ?
856 '+' : '-',
857 minor);
859 if (len > remaining)
860 break;
861 remaining -= len;
862 buf += len;
863 tlen += len;
866 len = snprintf(buf, remaining, "\n");
867 if (len > remaining)
868 return -EINVAL;
869 return tlen + len;
873 * write_versions - Set or report the available NFS protocol versions
875 * Input:
876 * buf: ignored
877 * size: zero
878 * Output:
879 * On success: passed-in buffer filled with '\n'-terminated C
880 * string containing positive or negative integer
881 * values representing the current status of each
882 * protocol version;
883 * return code is the size in bytes of the string
884 * On error: return code is zero or a negative errno value
886 * OR
888 * Input:
889 * buf: C string containing whitespace-
890 * separated positive or negative
891 * integer values representing NFS
892 * protocol versions to enable ("+n")
893 * or disable ("-n")
894 * size: non-zero length of C string in @buf
895 * Output:
896 * On success: status of zero or more protocol versions has
897 * been updated; passed-in buffer filled with
898 * '\n'-terminated C string containing positive
899 * or negative integer values representing the
900 * current status of each protocol version;
901 * return code is the size in bytes of the string
902 * On error: return code is zero or a negative errno value
904 static ssize_t write_versions(struct file *file, char *buf, size_t size)
906 ssize_t rv;
908 mutex_lock(&nfsd_mutex);
909 rv = __write_versions(file, buf, size);
910 mutex_unlock(&nfsd_mutex);
911 return rv;
915 * Zero-length write. Return a list of NFSD's current listener
916 * transports.
918 static ssize_t __write_ports_names(char *buf)
920 if (nfsd_serv == NULL)
921 return 0;
922 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
926 * A single 'fd' number was written, in which case it must be for
927 * a socket of a supported family/protocol, and we use it as an
928 * nfsd listener.
930 static ssize_t __write_ports_addfd(char *buf)
932 char *mesg = buf;
933 int fd, err;
935 err = get_int(&mesg, &fd);
936 if (err != 0 || fd < 0)
937 return -EINVAL;
939 err = nfsd_create_serv();
940 if (err != 0)
941 return err;
943 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
944 if (err < 0) {
945 svc_destroy(nfsd_serv);
946 return err;
949 /* Decrease the count, but don't shut down the service */
950 nfsd_serv->sv_nrthreads--;
951 return err;
955 * A '-' followed by the 'name' of a socket means we close the socket.
957 static ssize_t __write_ports_delfd(char *buf)
959 char *toclose;
960 int len = 0;
962 toclose = kstrdup(buf + 1, GFP_KERNEL);
963 if (toclose == NULL)
964 return -ENOMEM;
966 if (nfsd_serv != NULL)
967 len = svc_sock_names(nfsd_serv, buf,
968 SIMPLE_TRANSACTION_LIMIT, toclose);
969 kfree(toclose);
970 return len;
974 * A transport listener is added by writing it's transport name and
975 * a port number.
977 static ssize_t __write_ports_addxprt(char *buf)
979 char transport[16];
980 struct svc_xprt *xprt;
981 int port, err;
983 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
984 return -EINVAL;
986 if (port < 1 || port > USHRT_MAX)
987 return -EINVAL;
989 err = nfsd_create_serv();
990 if (err != 0)
991 return err;
993 err = svc_create_xprt(nfsd_serv, transport,
994 PF_INET, port, SVC_SOCK_ANONYMOUS);
995 if (err < 0)
996 goto out_err;
998 err = svc_create_xprt(nfsd_serv, transport,
999 PF_INET6, port, SVC_SOCK_ANONYMOUS);
1000 if (err < 0 && err != -EAFNOSUPPORT)
1001 goto out_close;
1003 /* Decrease the count, but don't shut down the service */
1004 nfsd_serv->sv_nrthreads--;
1005 return 0;
1006 out_close:
1007 xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
1008 if (xprt != NULL) {
1009 svc_close_xprt(xprt);
1010 svc_xprt_put(xprt);
1012 out_err:
1013 svc_destroy(nfsd_serv);
1014 return err;
1018 * A transport listener is removed by writing a "-", it's transport
1019 * name, and it's port number.
1021 static ssize_t __write_ports_delxprt(char *buf)
1023 struct svc_xprt *xprt;
1024 char transport[16];
1025 int port;
1027 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1028 return -EINVAL;
1030 if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
1031 return -EINVAL;
1033 xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1034 if (xprt == NULL)
1035 return -ENOTCONN;
1037 svc_close_xprt(xprt);
1038 svc_xprt_put(xprt);
1039 return 0;
1042 static ssize_t __write_ports(struct file *file, char *buf, size_t size)
1044 if (size == 0)
1045 return __write_ports_names(buf);
1047 if (isdigit(buf[0]))
1048 return __write_ports_addfd(buf);
1050 if (buf[0] == '-' && isdigit(buf[1]))
1051 return __write_ports_delfd(buf);
1053 if (isalpha(buf[0]))
1054 return __write_ports_addxprt(buf);
1056 if (buf[0] == '-' && isalpha(buf[1]))
1057 return __write_ports_delxprt(buf);
1059 return -EINVAL;
1063 * write_ports - Pass a socket file descriptor or transport name to listen on
1065 * Input:
1066 * buf: ignored
1067 * size: zero
1068 * Output:
1069 * On success: passed-in buffer filled with a '\n'-terminated C
1070 * string containing a whitespace-separated list of
1071 * named NFSD listeners;
1072 * return code is the size in bytes of the string
1073 * On error: return code is zero or a negative errno value
1075 * OR
1077 * Input:
1078 * buf: C string containing an unsigned
1079 * integer value representing a bound
1080 * but unconnected socket that is to be
1081 * used as an NFSD listener; listen(3)
1082 * must be called for a SOCK_STREAM
1083 * socket, otherwise it is ignored
1084 * size: non-zero length of C string in @buf
1085 * Output:
1086 * On success: NFS service is started;
1087 * passed-in buffer filled with a '\n'-terminated C
1088 * string containing a unique alphanumeric name of
1089 * the listener;
1090 * return code is the size in bytes of the string
1091 * On error: return code is a negative errno value
1093 * OR
1095 * Input:
1096 * buf: C string containing a "-" followed
1097 * by an integer value representing a
1098 * previously passed in socket file
1099 * descriptor
1100 * size: non-zero length of C string in @buf
1101 * Output:
1102 * On success: NFS service no longer listens on that socket;
1103 * passed-in buffer filled with a '\n'-terminated C
1104 * string containing a unique name of the listener;
1105 * return code is the size in bytes of the string
1106 * On error: return code is a negative errno value
1108 * OR
1110 * Input:
1111 * buf: C string containing a transport
1112 * name and an unsigned integer value
1113 * representing the port to listen on,
1114 * separated by whitespace
1115 * size: non-zero length of C string in @buf
1116 * Output:
1117 * On success: returns zero; NFS service is started
1118 * On error: return code is a negative errno value
1120 * OR
1122 * Input:
1123 * buf: C string containing a "-" followed
1124 * by a transport name and an unsigned
1125 * integer value representing the port
1126 * to listen on, separated by whitespace
1127 * size: non-zero length of C string in @buf
1128 * Output:
1129 * On success: returns zero; NFS service no longer listens
1130 * on that transport
1131 * On error: return code is a negative errno value
1133 static ssize_t write_ports(struct file *file, char *buf, size_t size)
1135 ssize_t rv;
1137 mutex_lock(&nfsd_mutex);
1138 rv = __write_ports(file, buf, size);
1139 mutex_unlock(&nfsd_mutex);
1140 return rv;
1144 int nfsd_max_blksize;
1147 * write_maxblksize - Set or report the current NFS blksize
1149 * Input:
1150 * buf: ignored
1151 * size: zero
1153 * OR
1155 * Input:
1156 * buf: C string containing an unsigned
1157 * integer value representing the new
1158 * NFS blksize
1159 * size: non-zero length of C string in @buf
1160 * Output:
1161 * On success: passed-in buffer filled with '\n'-terminated C string
1162 * containing numeric value of the current NFS blksize
1163 * setting;
1164 * return code is the size in bytes of the string
1165 * On error: return code is zero or a negative errno value
1167 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1169 char *mesg = buf;
1170 if (size > 0) {
1171 int bsize;
1172 int rv = get_int(&mesg, &bsize);
1173 if (rv)
1174 return rv;
1175 /* force bsize into allowed range and
1176 * required alignment.
1178 if (bsize < 1024)
1179 bsize = 1024;
1180 if (bsize > NFSSVC_MAXBLKSIZE)
1181 bsize = NFSSVC_MAXBLKSIZE;
1182 bsize &= ~(1024-1);
1183 mutex_lock(&nfsd_mutex);
1184 if (nfsd_serv) {
1185 mutex_unlock(&nfsd_mutex);
1186 return -EBUSY;
1188 nfsd_max_blksize = bsize;
1189 mutex_unlock(&nfsd_mutex);
1192 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1193 nfsd_max_blksize);
1196 #ifdef CONFIG_NFSD_V4
1197 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1199 char *mesg = buf;
1200 int rv, i;
1202 if (size > 0) {
1203 if (nfsd_serv)
1204 return -EBUSY;
1205 rv = get_int(&mesg, &i);
1206 if (rv)
1207 return rv;
1209 * Some sanity checking. We don't have a reason for
1210 * these particular numbers, but problems with the
1211 * extremes are:
1212 * - Too short: the briefest network outage may
1213 * cause clients to lose all their locks. Also,
1214 * the frequent polling may be wasteful.
1215 * - Too long: do you really want reboot recovery
1216 * to take more than an hour? Or to make other
1217 * clients wait an hour before being able to
1218 * revoke a dead client's locks?
1220 if (i < 10 || i > 3600)
1221 return -EINVAL;
1222 *time = i;
1225 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
1228 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1230 ssize_t rv;
1232 mutex_lock(&nfsd_mutex);
1233 rv = __nfsd4_write_time(file, buf, size, time);
1234 mutex_unlock(&nfsd_mutex);
1235 return rv;
1239 * write_leasetime - Set or report the current NFSv4 lease time
1241 * Input:
1242 * buf: ignored
1243 * size: zero
1245 * OR
1247 * Input:
1248 * buf: C string containing an unsigned
1249 * integer value representing the new
1250 * NFSv4 lease expiry time
1251 * size: non-zero length of C string in @buf
1252 * Output:
1253 * On success: passed-in buffer filled with '\n'-terminated C
1254 * string containing unsigned integer value of the
1255 * current lease expiry time;
1256 * return code is the size in bytes of the string
1257 * On error: return code is zero or a negative errno value
1259 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1261 return nfsd4_write_time(file, buf, size, &nfsd4_lease);
1265 * write_gracetime - Set or report current NFSv4 grace period time
1267 * As above, but sets the time of the NFSv4 grace period.
1269 * Note this should never be set to less than the *previous*
1270 * lease-period time, but we don't try to enforce this. (In the common
1271 * case (a new boot), we don't know what the previous lease time was
1272 * anyway.)
1274 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1276 return nfsd4_write_time(file, buf, size, &nfsd4_grace);
1279 extern char *nfs4_recoverydir(void);
1281 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
1283 char *mesg = buf;
1284 char *recdir;
1285 int len, status;
1287 if (size > 0) {
1288 if (nfsd_serv)
1289 return -EBUSY;
1290 if (size > PATH_MAX || buf[size-1] != '\n')
1291 return -EINVAL;
1292 buf[size-1] = 0;
1294 recdir = mesg;
1295 len = qword_get(&mesg, recdir, size);
1296 if (len <= 0)
1297 return -EINVAL;
1299 status = nfs4_reset_recoverydir(recdir);
1300 if (status)
1301 return status;
1304 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1305 nfs4_recoverydir());
1309 * write_recoverydir - Set or report the pathname of the recovery directory
1311 * Input:
1312 * buf: ignored
1313 * size: zero
1315 * OR
1317 * Input:
1318 * buf: C string containing the pathname
1319 * of the directory on a local file
1320 * system containing permanent NFSv4
1321 * recovery data
1322 * size: non-zero length of C string in @buf
1323 * Output:
1324 * On success: passed-in buffer filled with '\n'-terminated C string
1325 * containing the current recovery pathname setting;
1326 * return code is the size in bytes of the string
1327 * On error: return code is zero or a negative errno value
1329 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1331 ssize_t rv;
1333 mutex_lock(&nfsd_mutex);
1334 rv = __write_recoverydir(file, buf, size);
1335 mutex_unlock(&nfsd_mutex);
1336 return rv;
1339 #endif
1341 /*----------------------------------------------------------------------------*/
1343 * populating the filesystem.
1346 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1348 static struct tree_descr nfsd_files[] = {
1349 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1350 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1351 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1352 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1353 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1354 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1355 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
1356 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
1357 [NFSD_Export_features] = {"export_features",
1358 &export_features_operations, S_IRUGO},
1359 [NFSD_FO_UnlockIP] = {"unlock_ip",
1360 &transaction_ops, S_IWUSR|S_IRUSR},
1361 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1362 &transaction_ops, S_IWUSR|S_IRUSR},
1363 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1364 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1365 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1366 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1367 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1368 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1369 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1370 #ifdef CONFIG_NFSD_V4
1371 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1372 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1373 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1374 #endif
1375 /* last one */ {""}
1377 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1380 static int nfsd_get_sb(struct file_system_type *fs_type,
1381 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1383 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
1386 static struct file_system_type nfsd_fs_type = {
1387 .owner = THIS_MODULE,
1388 .name = "nfsd",
1389 .get_sb = nfsd_get_sb,
1390 .kill_sb = kill_litter_super,
1393 #ifdef CONFIG_PROC_FS
1394 static int create_proc_exports_entry(void)
1396 struct proc_dir_entry *entry;
1398 entry = proc_mkdir("fs/nfs", NULL);
1399 if (!entry)
1400 return -ENOMEM;
1401 entry = proc_create("exports", 0, entry, &exports_operations);
1402 if (!entry)
1403 return -ENOMEM;
1404 return 0;
1406 #else /* CONFIG_PROC_FS */
1407 static int create_proc_exports_entry(void)
1409 return 0;
1411 #endif
1413 static int __init init_nfsd(void)
1415 int retval;
1416 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1418 retval = nfs4_state_init(); /* nfs4 locking state */
1419 if (retval)
1420 return retval;
1421 nfsd_stat_init(); /* Statistics */
1422 retval = nfsd_reply_cache_init();
1423 if (retval)
1424 goto out_free_stat;
1425 retval = nfsd_export_init();
1426 if (retval)
1427 goto out_free_cache;
1428 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1429 retval = nfsd_idmap_init();
1430 if (retval)
1431 goto out_free_lockd;
1432 retval = create_proc_exports_entry();
1433 if (retval)
1434 goto out_free_idmap;
1435 retval = register_filesystem(&nfsd_fs_type);
1436 if (retval)
1437 goto out_free_all;
1438 return 0;
1439 out_free_all:
1440 remove_proc_entry("fs/nfs/exports", NULL);
1441 remove_proc_entry("fs/nfs", NULL);
1442 out_free_idmap:
1443 nfsd_idmap_shutdown();
1444 out_free_lockd:
1445 nfsd_lockd_shutdown();
1446 nfsd_export_shutdown();
1447 out_free_cache:
1448 nfsd_reply_cache_shutdown();
1449 out_free_stat:
1450 nfsd_stat_shutdown();
1451 nfsd4_free_slabs();
1452 return retval;
1455 static void __exit exit_nfsd(void)
1457 nfsd_export_shutdown();
1458 nfsd_reply_cache_shutdown();
1459 remove_proc_entry("fs/nfs/exports", NULL);
1460 remove_proc_entry("fs/nfs", NULL);
1461 nfsd_stat_shutdown();
1462 nfsd_lockd_shutdown();
1463 nfsd_idmap_shutdown();
1464 nfsd4_free_slabs();
1465 unregister_filesystem(&nfsd_fs_type);
1468 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1469 MODULE_LICENSE("GPL");
1470 module_init(init_nfsd)
1471 module_exit(exit_nfsd)