blob: 0415680d3f58a9b43ede349bd65846fe8b6d1291 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/nfsctl.c
3 *
4 * Syscall interface to knfsd.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
Al Viro3f8206d2008-07-26 03:46:43 -04009#include <linux/namei.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070010#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/nfsd_idmap.h>
NeilBrown80212d52006-10-02 02:17:47 -070013#include <linux/sunrpc/svcsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/nfsd/syscall.h>
Wendy Cheng4373ea82008-01-17 11:10:12 -050015#include <linux/lockd/lockd.h>
Chuck Lever41160922009-08-09 15:09:40 -040016#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Boaz Harrosh9a74af22009-12-03 20:30:56 +020018#include "nfsd.h"
19#include "cache.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
22 * We have a single directory with 9 nodes in it.
23 */
24enum {
25 NFSD_Root = 1,
26 NFSD_Svc,
27 NFSD_Add,
28 NFSD_Del,
29 NFSD_Export,
30 NFSD_Unexport,
31 NFSD_Getfd,
32 NFSD_Getfs,
33 NFSD_List,
34 NFSD_Fh,
Wendy Cheng4373ea82008-01-17 11:10:12 -050035 NFSD_FO_UnlockIP,
Wendy Cheng17efa372008-01-17 11:10:12 -050036 NFSD_FO_UnlockFS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 NFSD_Threads,
Greg Bankseed29652006-10-02 02:18:02 -070038 NFSD_Pool_Threads,
Greg Banks03cf6c92009-01-13 21:26:36 +110039 NFSD_Pool_Stats,
NeilBrown70c3b762005-11-07 01:00:25 -080040 NFSD_Versions,
NeilBrown80212d52006-10-02 02:17:47 -070041 NFSD_Ports,
NeilBrown596bbe52006-10-04 02:15:48 -070042 NFSD_MaxBlkSize,
NeilBrown70c3b762005-11-07 01:00:25 -080043 /*
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
46 */
47#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 NFSD_Leasetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070049 NFSD_RecoveryDir,
NeilBrown70c3b762005-11-07 01:00:25 -080050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/*
54 * write() for these nodes.
55 */
56static ssize_t write_svc(struct file *file, char *buf, size_t size);
57static ssize_t write_add(struct file *file, char *buf, size_t size);
58static ssize_t write_del(struct file *file, char *buf, size_t size);
59static ssize_t write_export(struct file *file, char *buf, size_t size);
60static ssize_t write_unexport(struct file *file, char *buf, size_t size);
61static ssize_t write_getfd(struct file *file, char *buf, size_t size);
62static ssize_t write_getfs(struct file *file, char *buf, size_t size);
63static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
Chuck Leverb046ccd2008-12-12 16:57:13 -050064static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
65static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static ssize_t write_threads(struct file *file, char *buf, size_t size);
Greg Bankseed29652006-10-02 02:18:02 -070067static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080068static ssize_t write_versions(struct file *file, char *buf, size_t size);
NeilBrown80212d52006-10-02 02:17:47 -070069static ssize_t write_ports(struct file *file, char *buf, size_t size);
NeilBrown596bbe52006-10-04 02:15:48 -070070static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080071#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
NeilBrown0964a3d2005-06-23 22:04:32 -070073static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static ssize_t (*write_op[])(struct file *, char *, size_t) = {
77 [NFSD_Svc] = write_svc,
78 [NFSD_Add] = write_add,
79 [NFSD_Del] = write_del,
80 [NFSD_Export] = write_export,
81 [NFSD_Unexport] = write_unexport,
82 [NFSD_Getfd] = write_getfd,
83 [NFSD_Getfs] = write_getfs,
84 [NFSD_Fh] = write_filehandle,
Chuck Leverb046ccd2008-12-12 16:57:13 -050085 [NFSD_FO_UnlockIP] = write_unlock_ip,
86 [NFSD_FO_UnlockFS] = write_unlock_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 [NFSD_Threads] = write_threads,
Greg Bankseed29652006-10-02 02:18:02 -070088 [NFSD_Pool_Threads] = write_pool_threads,
NeilBrown70c3b762005-11-07 01:00:25 -080089 [NFSD_Versions] = write_versions,
NeilBrown80212d52006-10-02 02:17:47 -070090 [NFSD_Ports] = write_ports,
NeilBrown596bbe52006-10-04 02:15:48 -070091 [NFSD_MaxBlkSize] = write_maxblksize,
NeilBrown70c3b762005-11-07 01:00:25 -080092#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 [NFSD_Leasetime] = write_leasetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070094 [NFSD_RecoveryDir] = write_recoverydir,
NeilBrown70c3b762005-11-07 01:00:25 -080095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
99{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800100 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 char *data;
102 ssize_t rv;
103
Tobias Klausere8c96f82006-03-24 03:15:34 -0800104 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EINVAL;
106
107 data = simple_transaction_get(file, buf, size);
108 if (IS_ERR(data))
109 return PTR_ERR(data);
110
111 rv = write_op[ino](file, data, size);
NeilBrown8971a102007-02-14 00:33:11 -0800112 if (rv >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 simple_transaction_set(file, rv);
114 rv = size;
115 }
116 return rv;
117}
118
NeilBrown73900222005-11-07 01:00:24 -0800119static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
120{
121 if (! file->private_data) {
122 /* An attempt to read a transaction file without writing
123 * causes a 0-byte write so that the file can return
124 * state information
125 */
126 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
127 if (rv < 0)
128 return rv;
129 }
130 return simple_transaction_read(file, buf, size, pos);
131}
132
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800133static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .write = nfsctl_transaction_write,
NeilBrown73900222005-11-07 01:00:24 -0800135 .read = nfsctl_transaction_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .release = simple_transaction_release,
137};
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int exports_open(struct inode *inode, struct file *file)
140{
141 return seq_open(file, &nfs_exports_op);
142}
143
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800144static const struct file_operations exports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .open = exports_open,
146 .read = seq_read,
147 .llseek = seq_lseek,
148 .release = seq_release,
Denis V. Lunev9ef2db22008-04-29 01:02:04 -0700149 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Greg Banks03cf6c92009-01-13 21:26:36 +1100152extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900153extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
Greg Banks03cf6c92009-01-13 21:26:36 +1100154
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700155static const struct file_operations pool_stats_operations = {
Greg Banks03cf6c92009-01-13 21:26:36 +1100156 .open = nfsd_pool_stats_open,
157 .read = seq_read,
158 .llseek = seq_lseek,
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900159 .release = nfsd_pool_stats_release,
Greg Banks03cf6c92009-01-13 21:26:36 +1100160 .owner = THIS_MODULE,
161};
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*----------------------------------------------------------------------------*/
164/*
165 * payload - write methods
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167
Chuck Lever262a0982008-12-12 16:57:35 -0500168/**
169 * write_svc - Start kernel's NFSD server
170 *
171 * Deprecated. /proc/fs/nfsd/threads is preferred.
172 * Function remains to support old versions of nfs-utils.
173 *
174 * Input:
175 * buf: struct nfsctl_svc
176 * svc_port: port number of this
177 * server's listener
178 * svc_nthreads: number of threads to start
179 * size: size in bytes of passed in nfsctl_svc
180 * Output:
181 * On success: returns zero
182 * On error: return code is negative errno value
183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static ssize_t write_svc(struct file *file, char *buf, size_t size)
185{
186 struct nfsctl_svc *data;
NeilBrown82e12fe2009-06-16 11:03:07 +1000187 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (size < sizeof(*data))
189 return -EINVAL;
190 data = (struct nfsctl_svc*) buf;
NeilBrown82e12fe2009-06-16 11:03:07 +1000191 err = nfsd_svc(data->svc_port, data->svc_nthreads);
192 if (err < 0)
193 return err;
194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Chuck Lever262a0982008-12-12 16:57:35 -0500197/**
198 * write_add - Add or modify client entry in auth unix cache
199 *
200 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
201 * Function remains to support old versions of nfs-utils.
202 *
203 * Input:
204 * buf: struct nfsctl_client
205 * cl_ident: '\0'-terminated C string
206 * containing domain name
207 * of client
208 * cl_naddr: no. of items in cl_addrlist
209 * cl_addrlist: array of client addresses
210 * cl_fhkeytype: ignored
211 * cl_fhkeylen: ignored
212 * cl_fhkey: ignored
213 * size: size in bytes of passed in nfsctl_client
214 * Output:
215 * On success: returns zero
216 * On error: return code is negative errno value
217 *
218 * Note: Only AF_INET client addresses are passed in, since
219 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static ssize_t write_add(struct file *file, char *buf, size_t size)
222{
223 struct nfsctl_client *data;
224 if (size < sizeof(*data))
225 return -EINVAL;
226 data = (struct nfsctl_client *)buf;
227 return exp_addclient(data);
228}
229
Chuck Lever262a0982008-12-12 16:57:35 -0500230/**
231 * write_del - Remove client from auth unix cache
232 *
233 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
234 * Function remains to support old versions of nfs-utils.
235 *
236 * Input:
237 * buf: struct nfsctl_client
238 * cl_ident: '\0'-terminated C string
239 * containing domain name
240 * of client
241 * cl_naddr: ignored
242 * cl_addrlist: ignored
243 * cl_fhkeytype: ignored
244 * cl_fhkeylen: ignored
245 * cl_fhkey: ignored
246 * size: size in bytes of passed in nfsctl_client
247 * Output:
248 * On success: returns zero
249 * On error: return code is negative errno value
250 *
251 * Note: Only AF_INET client addresses are passed in, since
252 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static ssize_t write_del(struct file *file, char *buf, size_t size)
255{
256 struct nfsctl_client *data;
257 if (size < sizeof(*data))
258 return -EINVAL;
259 data = (struct nfsctl_client *)buf;
260 return exp_delclient(data);
261}
262
Chuck Lever262a0982008-12-12 16:57:35 -0500263/**
264 * write_export - Export part or all of a local file system
265 *
266 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
267 * Function remains to support old versions of nfs-utils.
268 *
269 * Input:
270 * buf: struct nfsctl_export
271 * ex_client: '\0'-terminated C string
272 * containing domain name
273 * of client allowed to access
274 * this export
275 * ex_path: '\0'-terminated C string
276 * containing pathname of
277 * directory in local file system
278 * ex_dev: fsid to use for this export
279 * ex_ino: ignored
280 * ex_flags: export flags for this export
281 * ex_anon_uid: UID to use for anonymous
282 * requests
283 * ex_anon_gid: GID to use for anonymous
284 * requests
285 * size: size in bytes of passed in nfsctl_export
286 * Output:
287 * On success: returns zero
288 * On error: return code is negative errno value
289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static ssize_t write_export(struct file *file, char *buf, size_t size)
291{
292 struct nfsctl_export *data;
293 if (size < sizeof(*data))
294 return -EINVAL;
295 data = (struct nfsctl_export*)buf;
296 return exp_export(data);
297}
298
Chuck Lever262a0982008-12-12 16:57:35 -0500299/**
300 * write_unexport - Unexport a previously exported file system
301 *
302 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
303 * Function remains to support old versions of nfs-utils.
304 *
305 * Input:
306 * buf: struct nfsctl_export
307 * ex_client: '\0'-terminated C string
308 * containing domain name
309 * of client no longer allowed
310 * to access this export
311 * ex_path: '\0'-terminated C string
312 * containing pathname of
313 * directory in local file system
314 * ex_dev: ignored
315 * ex_ino: ignored
316 * ex_flags: ignored
317 * ex_anon_uid: ignored
318 * ex_anon_gid: ignored
319 * size: size in bytes of passed in nfsctl_export
320 * Output:
321 * On success: returns zero
322 * On error: return code is negative errno value
323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static ssize_t write_unexport(struct file *file, char *buf, size_t size)
325{
326 struct nfsctl_export *data;
327
328 if (size < sizeof(*data))
329 return -EINVAL;
330 data = (struct nfsctl_export*)buf;
331 return exp_unexport(data);
332}
333
Chuck Lever262a0982008-12-12 16:57:35 -0500334/**
335 * write_getfs - Get a variable-length NFS file handle by path
336 *
337 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
338 * Function remains to support old versions of nfs-utils.
339 *
340 * Input:
341 * buf: struct nfsctl_fsparm
342 * gd_addr: socket address of client
343 * gd_path: '\0'-terminated C string
344 * containing pathname of
345 * directory in local file system
346 * gd_maxlen: maximum size of returned file
347 * handle
348 * size: size in bytes of passed in nfsctl_fsparm
349 * Output:
350 * On success: passed-in buffer filled with a knfsd_fh structure
351 * (a variable-length raw NFS file handle);
352 * return code is the size in bytes of the file handle
353 * On error: return code is negative errno value
354 *
355 * Note: Only AF_INET client addresses are passed in, since gd_addr
356 * is the same size as a struct sockaddr_in.
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static ssize_t write_getfs(struct file *file, char *buf, size_t size)
359{
360 struct nfsctl_fsparm *data;
361 struct sockaddr_in *sin;
362 struct auth_domain *clp;
363 int err = 0;
364 struct knfsd_fh *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100365 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (size < sizeof(*data))
368 return -EINVAL;
369 data = (struct nfsctl_fsparm*)buf;
370 err = -EPROTONOSUPPORT;
371 if (data->gd_addr.sa_family != AF_INET)
372 goto out;
373 sin = (struct sockaddr_in *)&data->gd_addr;
374 if (data->gd_maxlen > NFS3_FHSIZE)
375 data->gd_maxlen = NFS3_FHSIZE;
376
377 res = (struct knfsd_fh*)buf;
378
379 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100380
381 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
382
383 clp = auth_unix_lookup(&in6);
384 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 err = -EPERM;
386 else {
387 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
388 auth_domain_put(clp);
389 }
390 exp_readunlock();
391 if (err == 0)
Andrew Morton12127492007-07-17 04:04:34 -0700392 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 out:
394 return err;
395}
396
Chuck Lever262a0982008-12-12 16:57:35 -0500397/**
398 * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
399 *
400 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
401 * Function remains to support old versions of nfs-utils.
402 *
403 * Input:
404 * buf: struct nfsctl_fdparm
405 * gd_addr: socket address of client
406 * gd_path: '\0'-terminated C string
407 * containing pathname of
408 * directory in local file system
409 * gd_version: fdparm structure version
410 * size: size in bytes of passed in nfsctl_fdparm
411 * Output:
412 * On success: passed-in buffer filled with nfsctl_res
413 * (a fixed-length raw NFS file handle);
414 * return code is the size in bytes of the file handle
415 * On error: return code is negative errno value
416 *
417 * Note: Only AF_INET client addresses are passed in, since gd_addr
418 * is the same size as a struct sockaddr_in.
419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static ssize_t write_getfd(struct file *file, char *buf, size_t size)
421{
422 struct nfsctl_fdparm *data;
423 struct sockaddr_in *sin;
424 struct auth_domain *clp;
425 int err = 0;
426 struct knfsd_fh fh;
427 char *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100428 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (size < sizeof(*data))
431 return -EINVAL;
432 data = (struct nfsctl_fdparm*)buf;
433 err = -EPROTONOSUPPORT;
434 if (data->gd_addr.sa_family != AF_INET)
435 goto out;
436 err = -EINVAL;
437 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
438 goto out;
439
440 res = buf;
441 sin = (struct sockaddr_in *)&data->gd_addr;
442 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100443
444 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
445
446 clp = auth_unix_lookup(&in6);
447 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 err = -EPERM;
449 else {
450 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
451 auth_domain_put(clp);
452 }
453 exp_readunlock();
454
455 if (err == 0) {
456 memset(res,0, NFS_FHSIZE);
457 memcpy(res, &fh.fh_base, fh.fh_size);
458 err = NFS_FHSIZE;
459 }
460 out:
461 return err;
462}
463
Chuck Lever262a0982008-12-12 16:57:35 -0500464/**
465 * write_unlock_ip - Release all locks used by a client
466 *
467 * Experimental.
468 *
469 * Input:
470 * buf: '\n'-terminated C string containing a
Chuck Lever41160922009-08-09 15:09:40 -0400471 * presentation format IP address
Chuck Lever262a0982008-12-12 16:57:35 -0500472 * size: length of C string in @buf
473 * Output:
474 * On success: returns zero if all specified locks were released;
475 * returns one if one or more locks were not released
476 * On error: return code is negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500477 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500478static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500479{
Chuck Lever41160922009-08-09 15:09:40 -0400480 struct sockaddr_storage address;
481 struct sockaddr *sap = (struct sockaddr *)&address;
482 size_t salen = sizeof(address);
Chuck Lever367c8c72008-06-30 18:58:14 -0400483 char *fo_path;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500484
485 /* sanity check */
486 if (size == 0)
487 return -EINVAL;
488
489 if (buf[size-1] != '\n')
490 return -EINVAL;
491
492 fo_path = buf;
493 if (qword_get(&buf, fo_path, size) < 0)
494 return -EINVAL;
495
Chuck Lever41160922009-08-09 15:09:40 -0400496 if (rpc_pton(fo_path, size, sap, salen) == 0)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500497 return -EINVAL;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500498
Chuck Lever41160922009-08-09 15:09:40 -0400499 return nlmsvc_unlock_all_by_ip(sap);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500500}
501
Chuck Lever262a0982008-12-12 16:57:35 -0500502/**
503 * write_unlock_fs - Release all locks on a local file system
504 *
505 * Experimental.
506 *
507 * Input:
508 * buf: '\n'-terminated C string containing the
509 * absolute pathname of a local file system
510 * size: length of C string in @buf
511 * Output:
512 * On success: returns zero if all specified locks were released;
513 * returns one if one or more locks were not released
514 * On error: return code is negative errno value
515 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500516static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
Wendy Cheng17efa372008-01-17 11:10:12 -0500517{
Al Viroa63bb992008-08-02 01:03:36 -0400518 struct path path;
Wendy Cheng17efa372008-01-17 11:10:12 -0500519 char *fo_path;
520 int error;
521
522 /* sanity check */
523 if (size == 0)
524 return -EINVAL;
525
526 if (buf[size-1] != '\n')
527 return -EINVAL;
528
529 fo_path = buf;
530 if (qword_get(&buf, fo_path, size) < 0)
531 return -EINVAL;
532
Al Viroa63bb992008-08-02 01:03:36 -0400533 error = kern_path(fo_path, 0, &path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500534 if (error)
535 return error;
536
Chuck Lever262a0982008-12-12 16:57:35 -0500537 /*
538 * XXX: Needs better sanity checking. Otherwise we could end up
539 * releasing locks on the wrong file system.
540 *
541 * For example:
542 * 1. Does the path refer to a directory?
543 * 2. Is that directory a mount point, or
544 * 3. Is that directory the root of an exported file system?
545 */
Al Viroa63bb992008-08-02 01:03:36 -0400546 error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
Wendy Cheng17efa372008-01-17 11:10:12 -0500547
Al Viroa63bb992008-08-02 01:03:36 -0400548 path_put(&path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500549 return error;
550}
551
Chuck Lever262a0982008-12-12 16:57:35 -0500552/**
553 * write_filehandle - Get a variable-length NFS file handle by path
554 *
555 * On input, the buffer contains a '\n'-terminated C string comprised of
556 * three alphanumeric words separated by whitespace. The string may
557 * contain escape sequences.
558 *
559 * Input:
560 * buf:
561 * domain: client domain name
562 * path: export pathname
563 * maxsize: numeric maximum size of
564 * @buf
565 * size: length of C string in @buf
566 * Output:
567 * On success: passed-in buffer filled with '\n'-terminated C
568 * string containing a ASCII hex text version
569 * of the NFS file handle;
570 * return code is the size in bytes of the string
571 * On error: return code is negative errno value
572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 char *dname, *path;
Andrew Morton246d95b2007-08-09 00:53:50 -0700576 int uninitialized_var(maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 char *mesg = buf;
578 int len;
579 struct auth_domain *dom;
580 struct knfsd_fh fh;
581
J. Bruce Fields87d26ea2008-01-22 17:40:42 -0500582 if (size == 0)
583 return -EINVAL;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (buf[size-1] != '\n')
586 return -EINVAL;
587 buf[size-1] = 0;
588
589 dname = mesg;
590 len = qword_get(&mesg, dname, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500591 if (len <= 0)
592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 path = dname+len+1;
595 len = qword_get(&mesg, path, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500596 if (len <= 0)
597 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 len = get_int(&mesg, &maxsize);
600 if (len)
601 return len;
602
603 if (maxsize < NFS_FHSIZE)
604 return -EINVAL;
605 if (maxsize > NFS3_FHSIZE)
606 maxsize = NFS3_FHSIZE;
607
608 if (qword_get(&mesg, mesg, size)>0)
609 return -EINVAL;
610
611 /* we have all the words, they are in buf.. */
612 dom = unix_domain_find(dname);
613 if (!dom)
614 return -ENOMEM;
615
616 len = exp_rootfh(dom, path, &fh, maxsize);
617 auth_domain_put(dom);
618 if (len)
619 return len;
620
Chuck Lever54224f02008-12-12 16:57:20 -0500621 mesg = buf;
622 len = SIMPLE_TRANSACTION_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
624 mesg[-1] = '\n';
625 return mesg - buf;
626}
627
Chuck Lever262a0982008-12-12 16:57:35 -0500628/**
629 * write_threads - Start NFSD, or report the current number of running threads
630 *
631 * Input:
632 * buf: ignored
633 * size: zero
634 * Output:
635 * On success: passed-in buffer filled with '\n'-terminated C
636 * string numeric value representing the number of
637 * running NFSD threads;
638 * return code is the size in bytes of the string
639 * On error: return code is zero
640 *
641 * OR
642 *
643 * Input:
644 * buf: C string containing an unsigned
645 * integer value representing the
646 * number of NFSD threads to start
647 * size: non-zero length of C string in @buf
648 * Output:
649 * On success: NFS service is started;
650 * passed-in buffer filled with '\n'-terminated C
651 * string numeric value representing the number of
652 * running NFSD threads;
653 * return code is the size in bytes of the string
654 * On error: return code is zero or a negative errno value
655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static ssize_t write_threads(struct file *file, char *buf, size_t size)
657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 char *mesg = buf;
659 int rv;
660 if (size > 0) {
661 int newthreads;
662 rv = get_int(&mesg, &newthreads);
663 if (rv)
664 return rv;
Chuck Lever9e074852008-12-12 16:57:27 -0500665 if (newthreads < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
Chuck Lever9e074852008-12-12 16:57:27 -0500667 rv = nfsd_svc(NFS_PORT, newthreads);
NeilBrown82e12fe2009-06-16 11:03:07 +1000668 if (rv < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return rv;
NeilBrown82e12fe2009-06-16 11:03:07 +1000670 } else
671 rv = nfsd_nrthreads();
Chuck Levere06b6402009-04-23 19:33:25 -0400672
NeilBrown82e12fe2009-06-16 11:03:07 +1000673 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Chuck Lever262a0982008-12-12 16:57:35 -0500676/**
677 * write_pool_threads - Set or report the current number of threads per pool
678 *
679 * Input:
680 * buf: ignored
681 * size: zero
682 *
683 * OR
684 *
685 * Input:
686 * buf: C string containing whitespace-
687 * separated unsigned integer values
688 * representing the number of NFSD
689 * threads to start in each pool
690 * size: non-zero length of C string in @buf
691 * Output:
692 * On success: passed-in buffer filled with '\n'-terminated C
693 * string containing integer values representing the
694 * number of NFSD threads in each pool;
695 * return code is the size in bytes of the string
696 * On error: return code is zero or a negative errno value
697 */
Greg Bankseed29652006-10-02 02:18:02 -0700698static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
699{
700 /* if size > 0, look for an array of number of threads per node
701 * and apply them then write out number of threads per node as reply
702 */
703 char *mesg = buf;
704 int i;
705 int rv;
706 int len;
Neil Brownbedbdd82008-06-10 08:40:35 -0400707 int npools;
Greg Bankseed29652006-10-02 02:18:02 -0700708 int *nthreads;
709
Neil Brownbedbdd82008-06-10 08:40:35 -0400710 mutex_lock(&nfsd_mutex);
711 npools = nfsd_nrpools();
Greg Bankseed29652006-10-02 02:18:02 -0700712 if (npools == 0) {
713 /*
714 * NFS is shut down. The admin can start it by
715 * writing to the threads file but NOT the pool_threads
716 * file, sorry. Report zero threads.
717 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400718 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700719 strcpy(buf, "0\n");
720 return strlen(buf);
721 }
722
723 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
Neil Brownbedbdd82008-06-10 08:40:35 -0400724 rv = -ENOMEM;
Greg Bankseed29652006-10-02 02:18:02 -0700725 if (nthreads == NULL)
Neil Brownbedbdd82008-06-10 08:40:35 -0400726 goto out_free;
Greg Bankseed29652006-10-02 02:18:02 -0700727
728 if (size > 0) {
729 for (i = 0; i < npools; i++) {
730 rv = get_int(&mesg, &nthreads[i]);
731 if (rv == -ENOENT)
732 break; /* fewer numbers than pools */
733 if (rv)
734 goto out_free; /* syntax error */
735 rv = -EINVAL;
736 if (nthreads[i] < 0)
737 goto out_free;
738 }
739 rv = nfsd_set_nrthreads(i, nthreads);
740 if (rv)
741 goto out_free;
742 }
743
744 rv = nfsd_get_nrthreads(npools, nthreads);
745 if (rv)
746 goto out_free;
747
748 mesg = buf;
749 size = SIMPLE_TRANSACTION_LIMIT;
750 for (i = 0; i < npools && size > 0; i++) {
751 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
752 len = strlen(mesg);
753 size -= len;
754 mesg += len;
755 }
J. Bruce Fields413d63d7102009-07-28 11:37:25 -0400756 rv = mesg - buf;
Greg Bankseed29652006-10-02 02:18:02 -0700757out_free:
758 kfree(nthreads);
Neil Brownbedbdd82008-06-10 08:40:35 -0400759 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700760 return rv;
761}
762
Jeff Layton3dd98a32008-06-10 08:40:36 -0400763static ssize_t __write_versions(struct file *file, char *buf, size_t size)
NeilBrown70c3b762005-11-07 01:00:25 -0800764{
NeilBrown70c3b762005-11-07 01:00:25 -0800765 char *mesg = buf;
Benny Halevy8daf2202009-04-03 08:28:59 +0300766 char *vers, *minorp, sign;
Chuck Lever261758b2009-04-23 19:33:18 -0400767 int len, num, remaining;
Benny Halevy8daf2202009-04-03 08:28:59 +0300768 unsigned minor;
NeilBrown70c3b762005-11-07 01:00:25 -0800769 ssize_t tlen = 0;
770 char *sep;
771
772 if (size>0) {
773 if (nfsd_serv)
NeilBrown6658d3a2006-10-02 02:17:46 -0700774 /* Cannot change versions without updating
775 * nfsd_serv->sv_xdrsize, and reallocing
776 * rq_argp and rq_resp
777 */
NeilBrown70c3b762005-11-07 01:00:25 -0800778 return -EBUSY;
779 if (buf[size-1] != '\n')
780 return -EINVAL;
781 buf[size-1] = 0;
782
783 vers = mesg;
784 len = qword_get(&mesg, vers, size);
785 if (len <= 0) return -EINVAL;
786 do {
787 sign = *vers;
788 if (sign == '+' || sign == '-')
Benny Halevy8daf2202009-04-03 08:28:59 +0300789 num = simple_strtol((vers+1), &minorp, 0);
NeilBrown70c3b762005-11-07 01:00:25 -0800790 else
Benny Halevy8daf2202009-04-03 08:28:59 +0300791 num = simple_strtol(vers, &minorp, 0);
792 if (*minorp == '.') {
793 if (num < 4)
794 return -EINVAL;
795 minor = simple_strtoul(minorp+1, NULL, 0);
796 if (minor == 0)
797 return -EINVAL;
798 if (nfsd_minorversion(minor, sign == '-' ?
799 NFSD_CLEAR : NFSD_SET) < 0)
800 return -EINVAL;
801 goto next;
802 }
NeilBrown70c3b762005-11-07 01:00:25 -0800803 switch(num) {
804 case 2:
805 case 3:
806 case 4:
NeilBrown6658d3a2006-10-02 02:17:46 -0700807 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
NeilBrown70c3b762005-11-07 01:00:25 -0800808 break;
809 default:
810 return -EINVAL;
811 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300812 next:
NeilBrown70c3b762005-11-07 01:00:25 -0800813 vers += len + 1;
NeilBrown70c3b762005-11-07 01:00:25 -0800814 } while ((len = qword_get(&mesg, vers, size)) > 0);
815 /* If all get turned off, turn them back on, as
816 * having no versions is BAD
817 */
NeilBrown6658d3a2006-10-02 02:17:46 -0700818 nfsd_reset_versions();
NeilBrown70c3b762005-11-07 01:00:25 -0800819 }
Chuck Lever261758b2009-04-23 19:33:18 -0400820
NeilBrown70c3b762005-11-07 01:00:25 -0800821 /* Now write current state into reply buffer */
822 len = 0;
823 sep = "";
Chuck Lever261758b2009-04-23 19:33:18 -0400824 remaining = SIMPLE_TRANSACTION_LIMIT;
NeilBrown70c3b762005-11-07 01:00:25 -0800825 for (num=2 ; num <= 4 ; num++)
NeilBrown6658d3a2006-10-02 02:17:46 -0700826 if (nfsd_vers(num, NFSD_AVAIL)) {
Chuck Lever261758b2009-04-23 19:33:18 -0400827 len = snprintf(buf, remaining, "%s%c%d", sep,
NeilBrown6658d3a2006-10-02 02:17:46 -0700828 nfsd_vers(num, NFSD_TEST)?'+':'-',
NeilBrown70c3b762005-11-07 01:00:25 -0800829 num);
830 sep = " ";
Chuck Lever261758b2009-04-23 19:33:18 -0400831
832 if (len > remaining)
833 break;
834 remaining -= len;
835 buf += len;
836 tlen += len;
NeilBrown70c3b762005-11-07 01:00:25 -0800837 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300838 if (nfsd_vers(4, NFSD_AVAIL))
Chuck Lever261758b2009-04-23 19:33:18 -0400839 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
840 minor++) {
841 len = snprintf(buf, remaining, " %c4.%u",
Benny Halevy8daf2202009-04-03 08:28:59 +0300842 (nfsd_vers(4, NFSD_TEST) &&
843 nfsd_minorversion(minor, NFSD_TEST)) ?
844 '+' : '-',
845 minor);
Chuck Lever261758b2009-04-23 19:33:18 -0400846
847 if (len > remaining)
848 break;
849 remaining -= len;
850 buf += len;
851 tlen += len;
852 }
853
854 len = snprintf(buf, remaining, "\n");
855 if (len > remaining)
856 return -EINVAL;
857 return tlen + len;
NeilBrown70c3b762005-11-07 01:00:25 -0800858}
859
Chuck Lever262a0982008-12-12 16:57:35 -0500860/**
861 * write_versions - Set or report the available NFS protocol versions
862 *
863 * Input:
864 * buf: ignored
865 * size: zero
866 * Output:
867 * On success: passed-in buffer filled with '\n'-terminated C
868 * string containing positive or negative integer
869 * values representing the current status of each
870 * protocol version;
871 * return code is the size in bytes of the string
872 * On error: return code is zero or a negative errno value
873 *
874 * OR
875 *
876 * Input:
877 * buf: C string containing whitespace-
878 * separated positive or negative
879 * integer values representing NFS
880 * protocol versions to enable ("+n")
881 * or disable ("-n")
882 * size: non-zero length of C string in @buf
883 * Output:
884 * On success: status of zero or more protocol versions has
885 * been updated; passed-in buffer filled with
886 * '\n'-terminated C string containing positive
887 * or negative integer values representing the
888 * current status of each protocol version;
889 * return code is the size in bytes of the string
890 * On error: return code is zero or a negative errno value
891 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400892static ssize_t write_versions(struct file *file, char *buf, size_t size)
893{
894 ssize_t rv;
895
896 mutex_lock(&nfsd_mutex);
897 rv = __write_versions(file, buf, size);
898 mutex_unlock(&nfsd_mutex);
899 return rv;
900}
901
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400902/*
Chuck Lever0a5372d2009-04-23 19:32:10 -0400903 * Zero-length write. Return a list of NFSD's current listener
904 * transports.
905 */
906static ssize_t __write_ports_names(char *buf)
907{
908 if (nfsd_serv == NULL)
909 return 0;
Chuck Lever335c54b2009-04-23 19:32:25 -0400910 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Lever0a5372d2009-04-23 19:32:10 -0400911}
912
913/*
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400914 * A single 'fd' number was written, in which case it must be for
915 * a socket of a supported family/protocol, and we use it as an
916 * nfsd listener.
917 */
918static ssize_t __write_ports_addfd(char *buf)
919{
920 char *mesg = buf;
921 int fd, err;
922
923 err = get_int(&mesg, &fd);
924 if (err != 0 || fd < 0)
925 return -EINVAL;
926
927 err = nfsd_create_serv();
928 if (err != 0)
929 return err;
930
Chuck Leverea068ba2009-04-23 19:32:18 -0400931 err = lockd_up();
932 if (err != 0)
933 goto out;
934
Chuck Leverbfba9ab2009-04-23 19:32:33 -0400935 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Leverea068ba2009-04-23 19:32:18 -0400936 if (err < 0)
937 lockd_down();
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400938
Chuck Leverea068ba2009-04-23 19:32:18 -0400939out:
940 /* Decrease the count, but don't shut down the service */
941 nfsd_serv->sv_nrthreads--;
942 return err;
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400943}
944
945/*
Chuck Lever82d56592009-04-23 19:31:48 -0400946 * A '-' followed by the 'name' of a socket means we close the socket.
947 */
948static ssize_t __write_ports_delfd(char *buf)
949{
950 char *toclose;
951 int len = 0;
952
953 toclose = kstrdup(buf + 1, GFP_KERNEL);
954 if (toclose == NULL)
955 return -ENOMEM;
956
957 if (nfsd_serv != NULL)
Chuck Lever8435d342009-04-23 19:32:40 -0400958 len = svc_sock_names(nfsd_serv, buf,
959 SIMPLE_TRANSACTION_LIMIT, toclose);
Chuck Lever82d56592009-04-23 19:31:48 -0400960 if (len >= 0)
961 lockd_down();
962
963 kfree(toclose);
964 return len;
965}
966
967/*
Chuck Lever4eb68c22009-04-23 19:31:40 -0400968 * A transport listener is added by writing it's transport name and
969 * a port number.
970 */
971static ssize_t __write_ports_addxprt(char *buf)
972{
973 char transport[16];
974 int port, err;
975
976 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
977 return -EINVAL;
978
979 if (port < 1 || port > USHORT_MAX)
980 return -EINVAL;
981
982 err = nfsd_create_serv();
983 if (err != 0)
984 return err;
985
986 err = svc_create_xprt(nfsd_serv, transport,
987 PF_INET, port, SVC_SOCK_ANONYMOUS);
988 if (err < 0) {
989 /* Give a reasonable perror msg for bad transport string */
990 if (err == -ENOENT)
991 err = -EPROTONOSUPPORT;
992 return err;
993 }
994 return 0;
995}
996
997/*
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400998 * A transport listener is removed by writing a "-", it's transport
999 * name, and it's port number.
1000 */
1001static ssize_t __write_ports_delxprt(char *buf)
1002{
1003 struct svc_xprt *xprt;
1004 char transport[16];
1005 int port;
1006
1007 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1008 return -EINVAL;
1009
1010 if (port < 1 || port > USHORT_MAX || nfsd_serv == NULL)
1011 return -EINVAL;
1012
1013 xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1014 if (xprt == NULL)
1015 return -ENOTCONN;
1016
1017 svc_close_xprt(xprt);
1018 svc_xprt_put(xprt);
1019 return 0;
1020}
1021
Neil Brownbedbdd82008-06-10 08:40:35 -04001022static ssize_t __write_ports(struct file *file, char *buf, size_t size)
NeilBrown80212d52006-10-02 02:17:47 -07001023{
Chuck Lever0a5372d2009-04-23 19:32:10 -04001024 if (size == 0)
1025 return __write_ports_names(buf);
Chuck Lever0b7c2f62009-04-23 19:31:55 -04001026
1027 if (isdigit(buf[0]))
1028 return __write_ports_addfd(buf);
Chuck Lever82d56592009-04-23 19:31:48 -04001029
1030 if (buf[0] == '-' && isdigit(buf[1]))
1031 return __write_ports_delfd(buf);
Chuck Lever4eb68c22009-04-23 19:31:40 -04001032
1033 if (isalpha(buf[0]))
1034 return __write_ports_addxprt(buf);
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001035
1036 if (buf[0] == '-' && isalpha(buf[1]))
1037 return __write_ports_delxprt(buf);
1038
NeilBrownb41b66d2006-10-02 02:17:48 -07001039 return -EINVAL;
NeilBrown80212d52006-10-02 02:17:47 -07001040}
1041
Chuck Lever262a0982008-12-12 16:57:35 -05001042/**
1043 * write_ports - Pass a socket file descriptor or transport name to listen on
1044 *
1045 * Input:
1046 * buf: ignored
1047 * size: zero
1048 * Output:
1049 * On success: passed-in buffer filled with a '\n'-terminated C
1050 * string containing a whitespace-separated list of
1051 * named NFSD listeners;
1052 * return code is the size in bytes of the string
1053 * On error: return code is zero or a negative errno value
1054 *
1055 * OR
1056 *
1057 * Input:
1058 * buf: C string containing an unsigned
1059 * integer value representing a bound
1060 * but unconnected socket that is to be
Chuck Leverc71206a2009-04-23 19:32:03 -04001061 * used as an NFSD listener; listen(3)
1062 * must be called for a SOCK_STREAM
1063 * socket, otherwise it is ignored
Chuck Lever262a0982008-12-12 16:57:35 -05001064 * size: non-zero length of C string in @buf
1065 * Output:
1066 * On success: NFS service is started;
1067 * passed-in buffer filled with a '\n'-terminated C
1068 * string containing a unique alphanumeric name of
1069 * the listener;
1070 * return code is the size in bytes of the string
1071 * On error: return code is a negative errno value
1072 *
1073 * OR
1074 *
1075 * Input:
1076 * buf: C string containing a "-" followed
1077 * by an integer value representing a
1078 * previously passed in socket file
1079 * descriptor
1080 * size: non-zero length of C string in @buf
1081 * Output:
1082 * On success: NFS service no longer listens on that socket;
1083 * passed-in buffer filled with a '\n'-terminated C
1084 * string containing a unique name of the listener;
1085 * return code is the size in bytes of the string
1086 * On error: return code is a negative errno value
1087 *
1088 * OR
1089 *
1090 * Input:
1091 * buf: C string containing a transport
1092 * name and an unsigned integer value
1093 * representing the port to listen on,
1094 * separated by whitespace
1095 * size: non-zero length of C string in @buf
1096 * Output:
1097 * On success: returns zero; NFS service is started
1098 * On error: return code is a negative errno value
1099 *
1100 * OR
1101 *
1102 * Input:
1103 * buf: C string containing a "-" followed
1104 * by a transport name and an unsigned
1105 * integer value representing the port
1106 * to listen on, separated by whitespace
1107 * size: non-zero length of C string in @buf
1108 * Output:
1109 * On success: returns zero; NFS service no longer listens
1110 * on that transport
1111 * On error: return code is a negative errno value
1112 */
Neil Brownbedbdd82008-06-10 08:40:35 -04001113static ssize_t write_ports(struct file *file, char *buf, size_t size)
1114{
1115 ssize_t rv;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001116
Neil Brownbedbdd82008-06-10 08:40:35 -04001117 mutex_lock(&nfsd_mutex);
1118 rv = __write_ports(file, buf, size);
1119 mutex_unlock(&nfsd_mutex);
1120 return rv;
1121}
1122
1123
NeilBrown596bbe52006-10-04 02:15:48 -07001124int nfsd_max_blksize;
1125
Chuck Lever262a0982008-12-12 16:57:35 -05001126/**
1127 * write_maxblksize - Set or report the current NFS blksize
1128 *
1129 * Input:
1130 * buf: ignored
1131 * size: zero
1132 *
1133 * OR
1134 *
1135 * Input:
1136 * buf: C string containing an unsigned
1137 * integer value representing the new
1138 * NFS blksize
1139 * size: non-zero length of C string in @buf
1140 * Output:
1141 * On success: passed-in buffer filled with '\n'-terminated C string
1142 * containing numeric value of the current NFS blksize
1143 * setting;
1144 * return code is the size in bytes of the string
1145 * On error: return code is zero or a negative errno value
1146 */
NeilBrown596bbe52006-10-04 02:15:48 -07001147static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1148{
1149 char *mesg = buf;
1150 if (size > 0) {
1151 int bsize;
1152 int rv = get_int(&mesg, &bsize);
1153 if (rv)
1154 return rv;
1155 /* force bsize into allowed range and
1156 * required alignment.
1157 */
1158 if (bsize < 1024)
1159 bsize = 1024;
1160 if (bsize > NFSSVC_MAXBLKSIZE)
1161 bsize = NFSSVC_MAXBLKSIZE;
1162 bsize &= ~(1024-1);
Neil Brownbedbdd82008-06-10 08:40:35 -04001163 mutex_lock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001164 if (nfsd_serv && nfsd_serv->sv_nrthreads) {
Neil Brownbedbdd82008-06-10 08:40:35 -04001165 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001166 return -EBUSY;
1167 }
1168 nfsd_max_blksize = bsize;
Neil Brownbedbdd82008-06-10 08:40:35 -04001169 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001170 }
Chuck Levere06b6402009-04-23 19:33:25 -04001171
1172 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1173 nfsd_max_blksize);
NeilBrown596bbe52006-10-04 02:15:48 -07001174}
1175
NeilBrown70c3b762005-11-07 01:00:25 -08001176#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177extern time_t nfs4_leasetime(void);
1178
Jeff Layton3dd98a32008-06-10 08:40:36 -04001179static ssize_t __write_leasetime(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 /* if size > 10 seconds, call
1182 * nfs4_reset_lease() then write out the new lease (seconds) as reply
1183 */
1184 char *mesg = buf;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001185 int rv, lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (size > 0) {
Jeff Layton3dd98a32008-06-10 08:40:36 -04001188 if (nfsd_serv)
1189 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 rv = get_int(&mesg, &lease);
1191 if (rv)
1192 return rv;
1193 if (lease < 10 || lease > 3600)
1194 return -EINVAL;
1195 nfs4_reset_lease(lease);
1196 }
Chuck Levere06b6402009-04-23 19:33:25 -04001197
1198 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n",
1199 nfs4_lease_time());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Chuck Lever262a0982008-12-12 16:57:35 -05001202/**
1203 * write_leasetime - Set or report the current NFSv4 lease time
1204 *
1205 * Input:
1206 * buf: ignored
1207 * size: zero
1208 *
1209 * OR
1210 *
1211 * Input:
1212 * buf: C string containing an unsigned
1213 * integer value representing the new
1214 * NFSv4 lease expiry time
1215 * size: non-zero length of C string in @buf
1216 * Output:
1217 * On success: passed-in buffer filled with '\n'-terminated C
1218 * string containing unsigned integer value of the
1219 * current lease expiry time;
1220 * return code is the size in bytes of the string
1221 * On error: return code is zero or a negative errno value
1222 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001223static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1224{
1225 ssize_t rv;
1226
1227 mutex_lock(&nfsd_mutex);
1228 rv = __write_leasetime(file, buf, size);
1229 mutex_unlock(&nfsd_mutex);
1230 return rv;
1231}
1232
1233extern char *nfs4_recoverydir(void);
1234
1235static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
NeilBrown0964a3d2005-06-23 22:04:32 -07001236{
1237 char *mesg = buf;
1238 char *recdir;
1239 int len, status;
1240
Jeff Layton3dd98a32008-06-10 08:40:36 -04001241 if (size > 0) {
1242 if (nfsd_serv)
1243 return -EBUSY;
1244 if (size > PATH_MAX || buf[size-1] != '\n')
1245 return -EINVAL;
1246 buf[size-1] = 0;
NeilBrown0964a3d2005-06-23 22:04:32 -07001247
Jeff Layton3dd98a32008-06-10 08:40:36 -04001248 recdir = mesg;
1249 len = qword_get(&mesg, recdir, size);
1250 if (len <= 0)
1251 return -EINVAL;
NeilBrown0964a3d2005-06-23 22:04:32 -07001252
Jeff Layton3dd98a32008-06-10 08:40:36 -04001253 status = nfs4_reset_recoverydir(recdir);
1254 }
Chuck Lever3d72ab82009-04-23 19:33:10 -04001255
1256 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1257 nfs4_recoverydir());
NeilBrown0964a3d2005-06-23 22:04:32 -07001258}
Jeff Layton3dd98a32008-06-10 08:40:36 -04001259
Chuck Lever262a0982008-12-12 16:57:35 -05001260/**
1261 * write_recoverydir - Set or report the pathname of the recovery directory
1262 *
1263 * Input:
1264 * buf: ignored
1265 * size: zero
1266 *
1267 * OR
1268 *
1269 * Input:
1270 * buf: C string containing the pathname
1271 * of the directory on a local file
1272 * system containing permanent NFSv4
1273 * recovery data
1274 * size: non-zero length of C string in @buf
1275 * Output:
1276 * On success: passed-in buffer filled with '\n'-terminated C string
1277 * containing the current recovery pathname setting;
1278 * return code is the size in bytes of the string
1279 * On error: return code is zero or a negative errno value
1280 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001281static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1282{
1283 ssize_t rv;
1284
1285 mutex_lock(&nfsd_mutex);
1286 rv = __write_recoverydir(file, buf, size);
1287 mutex_unlock(&nfsd_mutex);
1288 return rv;
1289}
1290
NeilBrown70c3b762005-11-07 01:00:25 -08001291#endif
NeilBrown0964a3d2005-06-23 22:04:32 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*----------------------------------------------------------------------------*/
1294/*
1295 * populating the filesystem.
1296 */
1297
1298static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1299{
1300 static struct tree_descr nfsd_files[] = {
1301 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1302 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1303 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1304 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1305 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1306 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1307 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
1308 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
Wendy Cheng4373ea82008-01-17 11:10:12 -05001309 [NFSD_FO_UnlockIP] = {"unlock_ip",
1310 &transaction_ops, S_IWUSR|S_IRUSR},
Wendy Cheng17efa372008-01-17 11:10:12 -05001311 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1312 &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1314 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Bankseed29652006-10-02 02:18:02 -07001315 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Banks03cf6c92009-01-13 21:26:36 +11001316 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
NeilBrown70c3b762005-11-07 01:00:25 -08001317 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown80212d52006-10-02 02:17:47 -07001318 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
NeilBrown596bbe52006-10-04 02:15:48 -07001319 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320#ifdef CONFIG_NFSD_V4
1321 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown0964a3d2005-06-23 22:04:32 -07001322 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323#endif
1324 /* last one */ {""}
1325 };
1326 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1327}
1328
David Howells454e2392006-06-23 02:02:57 -07001329static int nfsd_get_sb(struct file_system_type *fs_type,
1330 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
David Howells454e2392006-06-23 02:02:57 -07001332 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335static struct file_system_type nfsd_fs_type = {
1336 .owner = THIS_MODULE,
1337 .name = "nfsd",
1338 .get_sb = nfsd_get_sb,
1339 .kill_sb = kill_litter_super,
1340};
1341
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001342#ifdef CONFIG_PROC_FS
1343static int create_proc_exports_entry(void)
1344{
1345 struct proc_dir_entry *entry;
1346
1347 entry = proc_mkdir("fs/nfs", NULL);
1348 if (!entry)
1349 return -ENOMEM;
Denis V. Lunev9ef2db22008-04-29 01:02:04 -07001350 entry = proc_create("exports", 0, entry, &exports_operations);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001351 if (!entry)
1352 return -ENOMEM;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001353 return 0;
1354}
1355#else /* CONFIG_PROC_FS */
1356static int create_proc_exports_entry(void)
1357{
1358 return 0;
1359}
1360#endif
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static int __init init_nfsd(void)
1363{
1364 int retval;
1365 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1366
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001367 retval = nfs4_state_init(); /* nfs4 locking state */
1368 if (retval)
1369 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 nfsd_stat_init(); /* Statistics */
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001371 retval = nfsd_reply_cache_init();
1372 if (retval)
1373 goto out_free_stat;
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001374 retval = nfsd_export_init();
1375 if (retval)
1376 goto out_free_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 nfsd_lockd_init(); /* lockd->nfsd callbacks */
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001378 retval = nfsd_idmap_init();
1379 if (retval)
1380 goto out_free_lockd;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001381 retval = create_proc_exports_entry();
1382 if (retval)
1383 goto out_free_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 retval = register_filesystem(&nfsd_fs_type);
J. Bruce Fields26808d32007-11-09 13:44:06 -05001385 if (retval)
1386 goto out_free_all;
1387 return 0;
1388out_free_all:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001389 remove_proc_entry("fs/nfs/exports", NULL);
1390 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001391out_free_idmap:
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001392 nfsd_idmap_shutdown();
1393out_free_lockd:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001394 nfsd_lockd_shutdown();
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001395 nfsd_export_shutdown();
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001396out_free_cache:
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001397 nfsd_reply_cache_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001398out_free_stat:
1399 nfsd_stat_shutdown();
J. Bruce Fields26808d32007-11-09 13:44:06 -05001400 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return retval;
1402}
1403
1404static void __exit exit_nfsd(void)
1405{
1406 nfsd_export_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001407 nfsd_reply_cache_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 remove_proc_entry("fs/nfs/exports", NULL);
1409 remove_proc_entry("fs/nfs", NULL);
1410 nfsd_stat_shutdown();
1411 nfsd_lockd_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 nfsd_idmap_shutdown();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001413 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 unregister_filesystem(&nfsd_fs_type);
1415}
1416
1417MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1418MODULE_LICENSE("GPL");
1419module_init(init_nfsd)
1420module_exit(exit_nfsd)