staging: brcm80211: cleanup mac80211 callback bss_info_changed
[zen-stable.git] / fs / nfsd / nfsctl.c
blob33b3e2b06779da530630fbb43800b3587c2728d9
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/sunrpc/svcsock.h>
12 #include <linux/nfsd/syscall.h>
13 #include <linux/lockd/lockd.h>
14 #include <linux/sunrpc/clnt.h>
16 #include "idmap.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 #ifdef CONFIG_NFSD_DEPRECATED
26 NFSD_Svc,
27 NFSD_Add,
28 NFSD_Del,
29 NFSD_Export,
30 NFSD_Unexport,
31 NFSD_Getfd,
32 NFSD_Getfs,
33 #endif
34 NFSD_List,
35 NFSD_Export_features,
36 NFSD_Fh,
37 NFSD_FO_UnlockIP,
38 NFSD_FO_UnlockFS,
39 NFSD_Threads,
40 NFSD_Pool_Threads,
41 NFSD_Pool_Stats,
42 NFSD_Versions,
43 NFSD_Ports,
44 NFSD_MaxBlkSize,
46 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
47 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
49 #ifdef CONFIG_NFSD_V4
50 NFSD_Leasetime,
51 NFSD_Gracetime,
52 NFSD_RecoveryDir,
53 #endif
57 * write() for these nodes.
59 #ifdef CONFIG_NFSD_DEPRECATED
60 static ssize_t write_svc(struct file *file, char *buf, size_t size);
61 static ssize_t write_add(struct file *file, char *buf, size_t size);
62 static ssize_t write_del(struct file *file, char *buf, size_t size);
63 static ssize_t write_export(struct file *file, char *buf, size_t size);
64 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
65 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
66 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
67 #endif
68 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
69 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
70 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
71 static ssize_t write_threads(struct file *file, char *buf, size_t size);
72 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
73 static ssize_t write_versions(struct file *file, char *buf, size_t size);
74 static ssize_t write_ports(struct file *file, char *buf, size_t size);
75 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
76 #ifdef CONFIG_NFSD_V4
77 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
78 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
79 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
80 #endif
82 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
83 #ifdef CONFIG_NFSD_DEPRECATED
84 [NFSD_Svc] = write_svc,
85 [NFSD_Add] = write_add,
86 [NFSD_Del] = write_del,
87 [NFSD_Export] = write_export,
88 [NFSD_Unexport] = write_unexport,
89 [NFSD_Getfd] = write_getfd,
90 [NFSD_Getfs] = write_getfs,
91 #endif
92 [NFSD_Fh] = write_filehandle,
93 [NFSD_FO_UnlockIP] = write_unlock_ip,
94 [NFSD_FO_UnlockFS] = write_unlock_fs,
95 [NFSD_Threads] = write_threads,
96 [NFSD_Pool_Threads] = write_pool_threads,
97 [NFSD_Versions] = write_versions,
98 [NFSD_Ports] = write_ports,
99 [NFSD_MaxBlkSize] = write_maxblksize,
100 #ifdef CONFIG_NFSD_V4
101 [NFSD_Leasetime] = write_leasetime,
102 [NFSD_Gracetime] = write_gracetime,
103 [NFSD_RecoveryDir] = write_recoverydir,
104 #endif
107 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
109 ino_t ino = file->f_path.dentry->d_inode->i_ino;
110 char *data;
111 ssize_t rv;
113 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
114 return -EINVAL;
116 data = simple_transaction_get(file, buf, size);
117 if (IS_ERR(data))
118 return PTR_ERR(data);
120 rv = write_op[ino](file, data, size);
121 if (rv >= 0) {
122 simple_transaction_set(file, rv);
123 rv = size;
125 return rv;
128 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
130 #ifdef CONFIG_NFSD_DEPRECATED
131 static int warned;
132 if (file->f_dentry->d_name.name[0] == '.' && !warned) {
133 printk(KERN_INFO
134 "Warning: \"%s\" uses deprecated NFSD interface: %s."
135 " This will be removed in 2.6.40\n",
136 current->comm, file->f_dentry->d_name.name);
137 warned = 1;
139 #endif
140 if (! file->private_data) {
141 /* An attempt to read a transaction file without writing
142 * causes a 0-byte write so that the file can return
143 * state information
145 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
146 if (rv < 0)
147 return rv;
149 return simple_transaction_read(file, buf, size, pos);
152 static const struct file_operations transaction_ops = {
153 .write = nfsctl_transaction_write,
154 .read = nfsctl_transaction_read,
155 .release = simple_transaction_release,
156 .llseek = default_llseek,
159 static int exports_open(struct inode *inode, struct file *file)
161 return seq_open(file, &nfs_exports_op);
164 static const struct file_operations exports_operations = {
165 .open = exports_open,
166 .read = seq_read,
167 .llseek = seq_lseek,
168 .release = seq_release,
169 .owner = THIS_MODULE,
172 static int export_features_show(struct seq_file *m, void *v)
174 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
175 return 0;
178 static int export_features_open(struct inode *inode, struct file *file)
180 return single_open(file, export_features_show, NULL);
183 static struct file_operations export_features_operations = {
184 .open = export_features_open,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = single_release,
190 extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
191 extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
193 static const struct file_operations pool_stats_operations = {
194 .open = nfsd_pool_stats_open,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = nfsd_pool_stats_release,
198 .owner = THIS_MODULE,
201 /*----------------------------------------------------------------------------*/
203 * payload - write methods
206 #ifdef CONFIG_NFSD_DEPRECATED
208 * write_svc - Start kernel's NFSD server
210 * Deprecated. /proc/fs/nfsd/threads is preferred.
211 * Function remains to support old versions of nfs-utils.
213 * Input:
214 * buf: struct nfsctl_svc
215 * svc_port: port number of this
216 * server's listener
217 * svc_nthreads: number of threads to start
218 * size: size in bytes of passed in nfsctl_svc
219 * Output:
220 * On success: returns zero
221 * On error: return code is negative errno value
223 static ssize_t write_svc(struct file *file, char *buf, size_t size)
225 struct nfsctl_svc *data;
226 int err;
227 if (size < sizeof(*data))
228 return -EINVAL;
229 data = (struct nfsctl_svc*) buf;
230 err = nfsd_svc(data->svc_port, data->svc_nthreads);
231 if (err < 0)
232 return err;
233 return 0;
237 * write_add - Add or modify client entry in auth unix cache
239 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
240 * Function remains to support old versions of nfs-utils.
242 * Input:
243 * buf: struct nfsctl_client
244 * cl_ident: '\0'-terminated C string
245 * containing domain name
246 * of client
247 * cl_naddr: no. of items in cl_addrlist
248 * cl_addrlist: array of client addresses
249 * cl_fhkeytype: ignored
250 * cl_fhkeylen: ignored
251 * cl_fhkey: ignored
252 * size: size in bytes of passed in nfsctl_client
253 * Output:
254 * On success: returns zero
255 * On error: return code is negative errno value
257 * Note: Only AF_INET client addresses are passed in, since
258 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
260 static ssize_t write_add(struct file *file, char *buf, size_t size)
262 struct nfsctl_client *data;
263 if (size < sizeof(*data))
264 return -EINVAL;
265 data = (struct nfsctl_client *)buf;
266 return exp_addclient(data);
270 * write_del - Remove client from auth unix cache
272 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
273 * Function remains to support old versions of nfs-utils.
275 * Input:
276 * buf: struct nfsctl_client
277 * cl_ident: '\0'-terminated C string
278 * containing domain name
279 * of client
280 * cl_naddr: ignored
281 * cl_addrlist: ignored
282 * cl_fhkeytype: ignored
283 * cl_fhkeylen: ignored
284 * cl_fhkey: ignored
285 * size: size in bytes of passed in nfsctl_client
286 * Output:
287 * On success: returns zero
288 * On error: return code is negative errno value
290 * Note: Only AF_INET client addresses are passed in, since
291 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
293 static ssize_t write_del(struct file *file, char *buf, size_t size)
295 struct nfsctl_client *data;
296 if (size < sizeof(*data))
297 return -EINVAL;
298 data = (struct nfsctl_client *)buf;
299 return exp_delclient(data);
303 * write_export - Export part or all of a local file system
305 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
306 * Function remains to support old versions of nfs-utils.
308 * Input:
309 * buf: struct nfsctl_export
310 * ex_client: '\0'-terminated C string
311 * containing domain name
312 * of client allowed to access
313 * this export
314 * ex_path: '\0'-terminated C string
315 * containing pathname of
316 * directory in local file system
317 * ex_dev: fsid to use for this export
318 * ex_ino: ignored
319 * ex_flags: export flags for this export
320 * ex_anon_uid: UID to use for anonymous
321 * requests
322 * ex_anon_gid: GID to use for anonymous
323 * requests
324 * size: size in bytes of passed in nfsctl_export
325 * Output:
326 * On success: returns zero
327 * On error: return code is negative errno value
329 static ssize_t write_export(struct file *file, char *buf, size_t size)
331 struct nfsctl_export *data;
332 if (size < sizeof(*data))
333 return -EINVAL;
334 data = (struct nfsctl_export*)buf;
335 return exp_export(data);
339 * write_unexport - Unexport a previously exported file system
341 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
342 * Function remains to support old versions of nfs-utils.
344 * Input:
345 * buf: struct nfsctl_export
346 * ex_client: '\0'-terminated C string
347 * containing domain name
348 * of client no longer allowed
349 * to access this export
350 * ex_path: '\0'-terminated C string
351 * containing pathname of
352 * directory in local file system
353 * ex_dev: ignored
354 * ex_ino: ignored
355 * ex_flags: ignored
356 * ex_anon_uid: ignored
357 * ex_anon_gid: ignored
358 * size: size in bytes of passed in nfsctl_export
359 * Output:
360 * On success: returns zero
361 * On error: return code is negative errno value
363 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
365 struct nfsctl_export *data;
367 if (size < sizeof(*data))
368 return -EINVAL;
369 data = (struct nfsctl_export*)buf;
370 return exp_unexport(data);
374 * write_getfs - Get a variable-length NFS file handle by path
376 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
377 * Function remains to support old versions of nfs-utils.
379 * Input:
380 * buf: struct nfsctl_fsparm
381 * gd_addr: socket address of client
382 * gd_path: '\0'-terminated C string
383 * containing pathname of
384 * directory in local file system
385 * gd_maxlen: maximum size of returned file
386 * handle
387 * size: size in bytes of passed in nfsctl_fsparm
388 * Output:
389 * On success: passed-in buffer filled with a knfsd_fh structure
390 * (a variable-length raw NFS file handle);
391 * return code is the size in bytes of the file handle
392 * On error: return code is negative errno value
394 * Note: Only AF_INET client addresses are passed in, since gd_addr
395 * is the same size as a struct sockaddr_in.
397 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
399 struct nfsctl_fsparm *data;
400 struct sockaddr_in *sin;
401 struct auth_domain *clp;
402 int err = 0;
403 struct knfsd_fh *res;
404 struct in6_addr in6;
406 if (size < sizeof(*data))
407 return -EINVAL;
408 data = (struct nfsctl_fsparm*)buf;
409 err = -EPROTONOSUPPORT;
410 if (data->gd_addr.sa_family != AF_INET)
411 goto out;
412 sin = (struct sockaddr_in *)&data->gd_addr;
413 if (data->gd_maxlen > NFS3_FHSIZE)
414 data->gd_maxlen = NFS3_FHSIZE;
416 res = (struct knfsd_fh*)buf;
418 exp_readlock();
420 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
422 clp = auth_unix_lookup(&init_net, &in6);
423 if (!clp)
424 err = -EPERM;
425 else {
426 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
427 auth_domain_put(clp);
429 exp_readunlock();
430 if (err == 0)
431 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
432 out:
433 return err;
437 * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
439 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
440 * Function remains to support old versions of nfs-utils.
442 * Input:
443 * buf: struct nfsctl_fdparm
444 * gd_addr: socket address of client
445 * gd_path: '\0'-terminated C string
446 * containing pathname of
447 * directory in local file system
448 * gd_version: fdparm structure version
449 * size: size in bytes of passed in nfsctl_fdparm
450 * Output:
451 * On success: passed-in buffer filled with nfsctl_res
452 * (a fixed-length raw NFS file handle);
453 * return code is the size in bytes of the file handle
454 * On error: return code is negative errno value
456 * Note: Only AF_INET client addresses are passed in, since gd_addr
457 * is the same size as a struct sockaddr_in.
459 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
461 struct nfsctl_fdparm *data;
462 struct sockaddr_in *sin;
463 struct auth_domain *clp;
464 int err = 0;
465 struct knfsd_fh fh;
466 char *res;
467 struct in6_addr in6;
469 if (size < sizeof(*data))
470 return -EINVAL;
471 data = (struct nfsctl_fdparm*)buf;
472 err = -EPROTONOSUPPORT;
473 if (data->gd_addr.sa_family != AF_INET)
474 goto out;
475 err = -EINVAL;
476 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
477 goto out;
479 res = buf;
480 sin = (struct sockaddr_in *)&data->gd_addr;
481 exp_readlock();
483 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
485 clp = auth_unix_lookup(&init_net, &in6);
486 if (!clp)
487 err = -EPERM;
488 else {
489 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
490 auth_domain_put(clp);
492 exp_readunlock();
494 if (err == 0) {
495 memset(res,0, NFS_FHSIZE);
496 memcpy(res, &fh.fh_base, fh.fh_size);
497 err = NFS_FHSIZE;
499 out:
500 return err;
502 #endif /* CONFIG_NFSD_DEPRECATED */
505 * write_unlock_ip - Release all locks used by a client
507 * Experimental.
509 * Input:
510 * buf: '\n'-terminated C string containing a
511 * presentation format IP address
512 * size: length of C string in @buf
513 * Output:
514 * On success: returns zero if all specified locks were released;
515 * returns one if one or more locks were not released
516 * On error: return code is negative errno value
518 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
520 struct sockaddr_storage address;
521 struct sockaddr *sap = (struct sockaddr *)&address;
522 size_t salen = sizeof(address);
523 char *fo_path;
525 /* sanity check */
526 if (size == 0)
527 return -EINVAL;
529 if (buf[size-1] != '\n')
530 return -EINVAL;
532 fo_path = buf;
533 if (qword_get(&buf, fo_path, size) < 0)
534 return -EINVAL;
536 if (rpc_pton(fo_path, size, sap, salen) == 0)
537 return -EINVAL;
539 return nlmsvc_unlock_all_by_ip(sap);
543 * write_unlock_fs - Release all locks on a local file system
545 * Experimental.
547 * Input:
548 * buf: '\n'-terminated C string containing the
549 * absolute pathname of a local file system
550 * size: length of C string in @buf
551 * Output:
552 * On success: returns zero if all specified locks were released;
553 * returns one if one or more locks were not released
554 * On error: return code is negative errno value
556 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
558 struct path path;
559 char *fo_path;
560 int error;
562 /* sanity check */
563 if (size == 0)
564 return -EINVAL;
566 if (buf[size-1] != '\n')
567 return -EINVAL;
569 fo_path = buf;
570 if (qword_get(&buf, fo_path, size) < 0)
571 return -EINVAL;
573 error = kern_path(fo_path, 0, &path);
574 if (error)
575 return error;
578 * XXX: Needs better sanity checking. Otherwise we could end up
579 * releasing locks on the wrong file system.
581 * For example:
582 * 1. Does the path refer to a directory?
583 * 2. Is that directory a mount point, or
584 * 3. Is that directory the root of an exported file system?
586 error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
588 path_put(&path);
589 return error;
593 * write_filehandle - Get a variable-length NFS file handle by path
595 * On input, the buffer contains a '\n'-terminated C string comprised of
596 * three alphanumeric words separated by whitespace. The string may
597 * contain escape sequences.
599 * Input:
600 * buf:
601 * domain: client domain name
602 * path: export pathname
603 * maxsize: numeric maximum size of
604 * @buf
605 * size: length of C string in @buf
606 * Output:
607 * On success: passed-in buffer filled with '\n'-terminated C
608 * string containing a ASCII hex text version
609 * of the NFS file handle;
610 * return code is the size in bytes of the string
611 * On error: return code is negative errno value
613 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
615 char *dname, *path;
616 int uninitialized_var(maxsize);
617 char *mesg = buf;
618 int len;
619 struct auth_domain *dom;
620 struct knfsd_fh fh;
622 if (size == 0)
623 return -EINVAL;
625 if (buf[size-1] != '\n')
626 return -EINVAL;
627 buf[size-1] = 0;
629 dname = mesg;
630 len = qword_get(&mesg, dname, size);
631 if (len <= 0)
632 return -EINVAL;
634 path = dname+len+1;
635 len = qword_get(&mesg, path, size);
636 if (len <= 0)
637 return -EINVAL;
639 len = get_int(&mesg, &maxsize);
640 if (len)
641 return len;
643 if (maxsize < NFS_FHSIZE)
644 return -EINVAL;
645 if (maxsize > NFS3_FHSIZE)
646 maxsize = NFS3_FHSIZE;
648 if (qword_get(&mesg, mesg, size)>0)
649 return -EINVAL;
651 /* we have all the words, they are in buf.. */
652 dom = unix_domain_find(dname);
653 if (!dom)
654 return -ENOMEM;
656 len = exp_rootfh(dom, path, &fh, maxsize);
657 auth_domain_put(dom);
658 if (len)
659 return len;
661 mesg = buf;
662 len = SIMPLE_TRANSACTION_LIMIT;
663 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
664 mesg[-1] = '\n';
665 return mesg - buf;
669 * write_threads - Start NFSD, or report the current number of running threads
671 * Input:
672 * buf: ignored
673 * size: zero
674 * Output:
675 * On success: passed-in buffer filled with '\n'-terminated C
676 * string numeric value representing the number of
677 * running NFSD threads;
678 * return code is the size in bytes of the string
679 * On error: return code is zero
681 * OR
683 * Input:
684 * buf: C string containing an unsigned
685 * integer value representing the
686 * number of NFSD threads to start
687 * size: non-zero length of C string in @buf
688 * Output:
689 * On success: NFS service is started;
690 * passed-in buffer filled with '\n'-terminated C
691 * string numeric value representing the number of
692 * running NFSD threads;
693 * return code is the size in bytes of the string
694 * On error: return code is zero or a negative errno value
696 static ssize_t write_threads(struct file *file, char *buf, size_t size)
698 char *mesg = buf;
699 int rv;
700 if (size > 0) {
701 int newthreads;
702 rv = get_int(&mesg, &newthreads);
703 if (rv)
704 return rv;
705 if (newthreads < 0)
706 return -EINVAL;
707 rv = nfsd_svc(NFS_PORT, newthreads);
708 if (rv < 0)
709 return rv;
710 } else
711 rv = nfsd_nrthreads();
713 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
717 * write_pool_threads - Set or report the current number of threads per pool
719 * Input:
720 * buf: ignored
721 * size: zero
723 * OR
725 * Input:
726 * buf: C string containing whitespace-
727 * separated unsigned integer values
728 * representing the number of NFSD
729 * threads to start in each pool
730 * size: non-zero length of C string in @buf
731 * Output:
732 * On success: passed-in buffer filled with '\n'-terminated C
733 * string containing integer values representing the
734 * number of NFSD threads in each pool;
735 * return code is the size in bytes of the string
736 * On error: return code is zero or a negative errno value
738 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
740 /* if size > 0, look for an array of number of threads per node
741 * and apply them then write out number of threads per node as reply
743 char *mesg = buf;
744 int i;
745 int rv;
746 int len;
747 int npools;
748 int *nthreads;
750 mutex_lock(&nfsd_mutex);
751 npools = nfsd_nrpools();
752 if (npools == 0) {
754 * NFS is shut down. The admin can start it by
755 * writing to the threads file but NOT the pool_threads
756 * file, sorry. Report zero threads.
758 mutex_unlock(&nfsd_mutex);
759 strcpy(buf, "0\n");
760 return strlen(buf);
763 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
764 rv = -ENOMEM;
765 if (nthreads == NULL)
766 goto out_free;
768 if (size > 0) {
769 for (i = 0; i < npools; i++) {
770 rv = get_int(&mesg, &nthreads[i]);
771 if (rv == -ENOENT)
772 break; /* fewer numbers than pools */
773 if (rv)
774 goto out_free; /* syntax error */
775 rv = -EINVAL;
776 if (nthreads[i] < 0)
777 goto out_free;
779 rv = nfsd_set_nrthreads(i, nthreads);
780 if (rv)
781 goto out_free;
784 rv = nfsd_get_nrthreads(npools, nthreads);
785 if (rv)
786 goto out_free;
788 mesg = buf;
789 size = SIMPLE_TRANSACTION_LIMIT;
790 for (i = 0; i < npools && size > 0; i++) {
791 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
792 len = strlen(mesg);
793 size -= len;
794 mesg += len;
796 rv = mesg - buf;
797 out_free:
798 kfree(nthreads);
799 mutex_unlock(&nfsd_mutex);
800 return rv;
803 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
805 char *mesg = buf;
806 char *vers, *minorp, sign;
807 int len, num, remaining;
808 unsigned minor;
809 ssize_t tlen = 0;
810 char *sep;
812 if (size>0) {
813 if (nfsd_serv)
814 /* Cannot change versions without updating
815 * nfsd_serv->sv_xdrsize, and reallocing
816 * rq_argp and rq_resp
818 return -EBUSY;
819 if (buf[size-1] != '\n')
820 return -EINVAL;
821 buf[size-1] = 0;
823 vers = mesg;
824 len = qword_get(&mesg, vers, size);
825 if (len <= 0) return -EINVAL;
826 do {
827 sign = *vers;
828 if (sign == '+' || sign == '-')
829 num = simple_strtol((vers+1), &minorp, 0);
830 else
831 num = simple_strtol(vers, &minorp, 0);
832 if (*minorp == '.') {
833 if (num < 4)
834 return -EINVAL;
835 minor = simple_strtoul(minorp+1, NULL, 0);
836 if (minor == 0)
837 return -EINVAL;
838 if (nfsd_minorversion(minor, sign == '-' ?
839 NFSD_CLEAR : NFSD_SET) < 0)
840 return -EINVAL;
841 goto next;
843 switch(num) {
844 case 2:
845 case 3:
846 case 4:
847 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
848 break;
849 default:
850 return -EINVAL;
852 next:
853 vers += len + 1;
854 } while ((len = qword_get(&mesg, vers, size)) > 0);
855 /* If all get turned off, turn them back on, as
856 * having no versions is BAD
858 nfsd_reset_versions();
861 /* Now write current state into reply buffer */
862 len = 0;
863 sep = "";
864 remaining = SIMPLE_TRANSACTION_LIMIT;
865 for (num=2 ; num <= 4 ; num++)
866 if (nfsd_vers(num, NFSD_AVAIL)) {
867 len = snprintf(buf, remaining, "%s%c%d", sep,
868 nfsd_vers(num, NFSD_TEST)?'+':'-',
869 num);
870 sep = " ";
872 if (len > remaining)
873 break;
874 remaining -= len;
875 buf += len;
876 tlen += len;
878 if (nfsd_vers(4, NFSD_AVAIL))
879 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
880 minor++) {
881 len = snprintf(buf, remaining, " %c4.%u",
882 (nfsd_vers(4, NFSD_TEST) &&
883 nfsd_minorversion(minor, NFSD_TEST)) ?
884 '+' : '-',
885 minor);
887 if (len > remaining)
888 break;
889 remaining -= len;
890 buf += len;
891 tlen += len;
894 len = snprintf(buf, remaining, "\n");
895 if (len > remaining)
896 return -EINVAL;
897 return tlen + len;
901 * write_versions - Set or report the available NFS protocol versions
903 * Input:
904 * buf: ignored
905 * size: zero
906 * Output:
907 * On success: passed-in buffer filled with '\n'-terminated C
908 * string containing positive or negative integer
909 * values representing the current status of each
910 * protocol version;
911 * return code is the size in bytes of the string
912 * On error: return code is zero or a negative errno value
914 * OR
916 * Input:
917 * buf: C string containing whitespace-
918 * separated positive or negative
919 * integer values representing NFS
920 * protocol versions to enable ("+n")
921 * or disable ("-n")
922 * size: non-zero length of C string in @buf
923 * Output:
924 * On success: status of zero or more protocol versions has
925 * been updated; passed-in buffer filled with
926 * '\n'-terminated C string containing positive
927 * or negative integer values representing the
928 * current status of each protocol version;
929 * return code is the size in bytes of the string
930 * On error: return code is zero or a negative errno value
932 static ssize_t write_versions(struct file *file, char *buf, size_t size)
934 ssize_t rv;
936 mutex_lock(&nfsd_mutex);
937 rv = __write_versions(file, buf, size);
938 mutex_unlock(&nfsd_mutex);
939 return rv;
943 * Zero-length write. Return a list of NFSD's current listener
944 * transports.
946 static ssize_t __write_ports_names(char *buf)
948 if (nfsd_serv == NULL)
949 return 0;
950 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
954 * A single 'fd' number was written, in which case it must be for
955 * a socket of a supported family/protocol, and we use it as an
956 * nfsd listener.
958 static ssize_t __write_ports_addfd(char *buf)
960 char *mesg = buf;
961 int fd, err;
963 err = get_int(&mesg, &fd);
964 if (err != 0 || fd < 0)
965 return -EINVAL;
967 err = nfsd_create_serv();
968 if (err != 0)
969 return err;
971 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
972 if (err < 0) {
973 svc_destroy(nfsd_serv);
974 return err;
977 /* Decrease the count, but don't shut down the service */
978 nfsd_serv->sv_nrthreads--;
979 return err;
983 * A '-' followed by the 'name' of a socket means we close the socket.
985 static ssize_t __write_ports_delfd(char *buf)
987 char *toclose;
988 int len = 0;
990 toclose = kstrdup(buf + 1, GFP_KERNEL);
991 if (toclose == NULL)
992 return -ENOMEM;
994 if (nfsd_serv != NULL)
995 len = svc_sock_names(nfsd_serv, buf,
996 SIMPLE_TRANSACTION_LIMIT, toclose);
997 kfree(toclose);
998 return len;
1002 * A transport listener is added by writing it's transport name and
1003 * a port number.
1005 static ssize_t __write_ports_addxprt(char *buf)
1007 char transport[16];
1008 struct svc_xprt *xprt;
1009 int port, err;
1011 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
1012 return -EINVAL;
1014 if (port < 1 || port > USHRT_MAX)
1015 return -EINVAL;
1017 err = nfsd_create_serv();
1018 if (err != 0)
1019 return err;
1021 err = svc_create_xprt(nfsd_serv, transport, &init_net,
1022 PF_INET, port, SVC_SOCK_ANONYMOUS);
1023 if (err < 0)
1024 goto out_err;
1026 err = svc_create_xprt(nfsd_serv, transport, &init_net,
1027 PF_INET6, port, SVC_SOCK_ANONYMOUS);
1028 if (err < 0 && err != -EAFNOSUPPORT)
1029 goto out_close;
1031 /* Decrease the count, but don't shut down the service */
1032 nfsd_serv->sv_nrthreads--;
1033 return 0;
1034 out_close:
1035 xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
1036 if (xprt != NULL) {
1037 svc_close_xprt(xprt);
1038 svc_xprt_put(xprt);
1040 out_err:
1041 svc_destroy(nfsd_serv);
1042 return err;
1046 * A transport listener is removed by writing a "-", it's transport
1047 * name, and it's port number.
1049 static ssize_t __write_ports_delxprt(char *buf)
1051 struct svc_xprt *xprt;
1052 char transport[16];
1053 int port;
1055 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1056 return -EINVAL;
1058 if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
1059 return -EINVAL;
1061 xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1062 if (xprt == NULL)
1063 return -ENOTCONN;
1065 svc_close_xprt(xprt);
1066 svc_xprt_put(xprt);
1067 return 0;
1070 static ssize_t __write_ports(struct file *file, char *buf, size_t size)
1072 if (size == 0)
1073 return __write_ports_names(buf);
1075 if (isdigit(buf[0]))
1076 return __write_ports_addfd(buf);
1078 if (buf[0] == '-' && isdigit(buf[1]))
1079 return __write_ports_delfd(buf);
1081 if (isalpha(buf[0]))
1082 return __write_ports_addxprt(buf);
1084 if (buf[0] == '-' && isalpha(buf[1]))
1085 return __write_ports_delxprt(buf);
1087 return -EINVAL;
1091 * write_ports - Pass a socket file descriptor or transport name to listen on
1093 * Input:
1094 * buf: ignored
1095 * size: zero
1096 * Output:
1097 * On success: passed-in buffer filled with a '\n'-terminated C
1098 * string containing a whitespace-separated list of
1099 * named NFSD listeners;
1100 * return code is the size in bytes of the string
1101 * On error: return code is zero or a negative errno value
1103 * OR
1105 * Input:
1106 * buf: C string containing an unsigned
1107 * integer value representing a bound
1108 * but unconnected socket that is to be
1109 * used as an NFSD listener; listen(3)
1110 * must be called for a SOCK_STREAM
1111 * socket, otherwise it is ignored
1112 * size: non-zero length of C string in @buf
1113 * Output:
1114 * On success: NFS service is started;
1115 * passed-in buffer filled with a '\n'-terminated C
1116 * string containing a unique alphanumeric name of
1117 * the listener;
1118 * return code is the size in bytes of the string
1119 * On error: return code is a negative errno value
1121 * OR
1123 * Input:
1124 * buf: C string containing a "-" followed
1125 * by an integer value representing a
1126 * previously passed in socket file
1127 * descriptor
1128 * size: non-zero length of C string in @buf
1129 * Output:
1130 * On success: NFS service no longer listens on that socket;
1131 * passed-in buffer filled with a '\n'-terminated C
1132 * string containing a unique name of the listener;
1133 * return code is the size in bytes of the string
1134 * On error: return code is a negative errno value
1136 * OR
1138 * Input:
1139 * buf: C string containing a transport
1140 * name and an unsigned integer value
1141 * representing the port to listen on,
1142 * separated by whitespace
1143 * size: non-zero length of C string in @buf
1144 * Output:
1145 * On success: returns zero; NFS service is started
1146 * On error: return code is a negative errno value
1148 * OR
1150 * Input:
1151 * buf: C string containing a "-" followed
1152 * by a transport name and an unsigned
1153 * integer value representing the port
1154 * to listen on, separated by whitespace
1155 * size: non-zero length of C string in @buf
1156 * Output:
1157 * On success: returns zero; NFS service no longer listens
1158 * on that transport
1159 * On error: return code is a negative errno value
1161 static ssize_t write_ports(struct file *file, char *buf, size_t size)
1163 ssize_t rv;
1165 mutex_lock(&nfsd_mutex);
1166 rv = __write_ports(file, buf, size);
1167 mutex_unlock(&nfsd_mutex);
1168 return rv;
1172 int nfsd_max_blksize;
1175 * write_maxblksize - Set or report the current NFS blksize
1177 * Input:
1178 * buf: ignored
1179 * size: zero
1181 * OR
1183 * Input:
1184 * buf: C string containing an unsigned
1185 * integer value representing the new
1186 * NFS blksize
1187 * size: non-zero length of C string in @buf
1188 * Output:
1189 * On success: passed-in buffer filled with '\n'-terminated C string
1190 * containing numeric value of the current NFS blksize
1191 * setting;
1192 * return code is the size in bytes of the string
1193 * On error: return code is zero or a negative errno value
1195 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1197 char *mesg = buf;
1198 if (size > 0) {
1199 int bsize;
1200 int rv = get_int(&mesg, &bsize);
1201 if (rv)
1202 return rv;
1203 /* force bsize into allowed range and
1204 * required alignment.
1206 if (bsize < 1024)
1207 bsize = 1024;
1208 if (bsize > NFSSVC_MAXBLKSIZE)
1209 bsize = NFSSVC_MAXBLKSIZE;
1210 bsize &= ~(1024-1);
1211 mutex_lock(&nfsd_mutex);
1212 if (nfsd_serv) {
1213 mutex_unlock(&nfsd_mutex);
1214 return -EBUSY;
1216 nfsd_max_blksize = bsize;
1217 mutex_unlock(&nfsd_mutex);
1220 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1221 nfsd_max_blksize);
1224 #ifdef CONFIG_NFSD_V4
1225 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1227 char *mesg = buf;
1228 int rv, i;
1230 if (size > 0) {
1231 if (nfsd_serv)
1232 return -EBUSY;
1233 rv = get_int(&mesg, &i);
1234 if (rv)
1235 return rv;
1237 * Some sanity checking. We don't have a reason for
1238 * these particular numbers, but problems with the
1239 * extremes are:
1240 * - Too short: the briefest network outage may
1241 * cause clients to lose all their locks. Also,
1242 * the frequent polling may be wasteful.
1243 * - Too long: do you really want reboot recovery
1244 * to take more than an hour? Or to make other
1245 * clients wait an hour before being able to
1246 * revoke a dead client's locks?
1248 if (i < 10 || i > 3600)
1249 return -EINVAL;
1250 *time = i;
1253 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
1256 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1258 ssize_t rv;
1260 mutex_lock(&nfsd_mutex);
1261 rv = __nfsd4_write_time(file, buf, size, time);
1262 mutex_unlock(&nfsd_mutex);
1263 return rv;
1267 * write_leasetime - Set or report the current NFSv4 lease time
1269 * Input:
1270 * buf: ignored
1271 * size: zero
1273 * OR
1275 * Input:
1276 * buf: C string containing an unsigned
1277 * integer value representing the new
1278 * NFSv4 lease expiry time
1279 * size: non-zero length of C string in @buf
1280 * Output:
1281 * On success: passed-in buffer filled with '\n'-terminated C
1282 * string containing unsigned integer value of the
1283 * current lease expiry time;
1284 * return code is the size in bytes of the string
1285 * On error: return code is zero or a negative errno value
1287 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1289 return nfsd4_write_time(file, buf, size, &nfsd4_lease);
1293 * write_gracetime - Set or report current NFSv4 grace period time
1295 * As above, but sets the time of the NFSv4 grace period.
1297 * Note this should never be set to less than the *previous*
1298 * lease-period time, but we don't try to enforce this. (In the common
1299 * case (a new boot), we don't know what the previous lease time was
1300 * anyway.)
1302 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1304 return nfsd4_write_time(file, buf, size, &nfsd4_grace);
1307 extern char *nfs4_recoverydir(void);
1309 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
1311 char *mesg = buf;
1312 char *recdir;
1313 int len, status;
1315 if (size > 0) {
1316 if (nfsd_serv)
1317 return -EBUSY;
1318 if (size > PATH_MAX || buf[size-1] != '\n')
1319 return -EINVAL;
1320 buf[size-1] = 0;
1322 recdir = mesg;
1323 len = qword_get(&mesg, recdir, size);
1324 if (len <= 0)
1325 return -EINVAL;
1327 status = nfs4_reset_recoverydir(recdir);
1328 if (status)
1329 return status;
1332 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1333 nfs4_recoverydir());
1337 * write_recoverydir - Set or report the pathname of the recovery directory
1339 * Input:
1340 * buf: ignored
1341 * size: zero
1343 * OR
1345 * Input:
1346 * buf: C string containing the pathname
1347 * of the directory on a local file
1348 * system containing permanent NFSv4
1349 * recovery data
1350 * size: non-zero length of C string in @buf
1351 * Output:
1352 * On success: passed-in buffer filled with '\n'-terminated C string
1353 * containing the current recovery pathname setting;
1354 * return code is the size in bytes of the string
1355 * On error: return code is zero or a negative errno value
1357 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1359 ssize_t rv;
1361 mutex_lock(&nfsd_mutex);
1362 rv = __write_recoverydir(file, buf, size);
1363 mutex_unlock(&nfsd_mutex);
1364 return rv;
1367 #endif
1369 /*----------------------------------------------------------------------------*/
1371 * populating the filesystem.
1374 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1376 static struct tree_descr nfsd_files[] = {
1377 #ifdef CONFIG_NFSD_DEPRECATED
1378 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1379 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1380 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1381 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1382 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1383 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1384 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
1385 #endif
1386 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
1387 [NFSD_Export_features] = {"export_features",
1388 &export_features_operations, S_IRUGO},
1389 [NFSD_FO_UnlockIP] = {"unlock_ip",
1390 &transaction_ops, S_IWUSR|S_IRUSR},
1391 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1392 &transaction_ops, S_IWUSR|S_IRUSR},
1393 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1394 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1395 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1396 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1397 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1398 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1399 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1400 #ifdef CONFIG_NFSD_V4
1401 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1402 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1403 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1404 #endif
1405 /* last one */ {""}
1407 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1410 static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1411 int flags, const char *dev_name, void *data)
1413 return mount_single(fs_type, flags, data, nfsd_fill_super);
1416 static struct file_system_type nfsd_fs_type = {
1417 .owner = THIS_MODULE,
1418 .name = "nfsd",
1419 .mount = nfsd_mount,
1420 .kill_sb = kill_litter_super,
1423 #ifdef CONFIG_PROC_FS
1424 static int create_proc_exports_entry(void)
1426 struct proc_dir_entry *entry;
1428 entry = proc_mkdir("fs/nfs", NULL);
1429 if (!entry)
1430 return -ENOMEM;
1431 entry = proc_create("exports", 0, entry, &exports_operations);
1432 if (!entry)
1433 return -ENOMEM;
1434 return 0;
1436 #else /* CONFIG_PROC_FS */
1437 static int create_proc_exports_entry(void)
1439 return 0;
1441 #endif
1443 static int __init init_nfsd(void)
1445 int retval;
1446 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1448 retval = nfs4_state_init(); /* nfs4 locking state */
1449 if (retval)
1450 return retval;
1451 nfsd_stat_init(); /* Statistics */
1452 retval = nfsd_reply_cache_init();
1453 if (retval)
1454 goto out_free_stat;
1455 retval = nfsd_export_init();
1456 if (retval)
1457 goto out_free_cache;
1458 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1459 retval = nfsd_idmap_init();
1460 if (retval)
1461 goto out_free_lockd;
1462 retval = create_proc_exports_entry();
1463 if (retval)
1464 goto out_free_idmap;
1465 retval = register_filesystem(&nfsd_fs_type);
1466 if (retval)
1467 goto out_free_all;
1468 return 0;
1469 out_free_all:
1470 remove_proc_entry("fs/nfs/exports", NULL);
1471 remove_proc_entry("fs/nfs", NULL);
1472 out_free_idmap:
1473 nfsd_idmap_shutdown();
1474 out_free_lockd:
1475 nfsd_lockd_shutdown();
1476 nfsd_export_shutdown();
1477 out_free_cache:
1478 nfsd_reply_cache_shutdown();
1479 out_free_stat:
1480 nfsd_stat_shutdown();
1481 nfsd4_free_slabs();
1482 return retval;
1485 static void __exit exit_nfsd(void)
1487 nfsd_export_shutdown();
1488 nfsd_reply_cache_shutdown();
1489 remove_proc_entry("fs/nfs/exports", NULL);
1490 remove_proc_entry("fs/nfs", NULL);
1491 nfsd_stat_shutdown();
1492 nfsd_lockd_shutdown();
1493 nfsd_idmap_shutdown();
1494 nfsd4_free_slabs();
1495 unregister_filesystem(&nfsd_fs_type);
1498 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1499 MODULE_LICENSE("GPL");
1500 module_init(init_nfsd)
1501 module_exit(exit_nfsd)