blob: 06fa87e52e8243cc46a41d5f861aa91388581d7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Syscall interface to knfsd.
3 *
4 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
5 */
6
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Al Viro3f8206d2008-07-26 03:46:43 -04008#include <linux/namei.h>
NeilBrownb41b66d2006-10-02 02:17:48 -07009#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/nfsd_idmap.h>
NeilBrown80212d52006-10-02 02:17:47 -070012#include <linux/sunrpc/svcsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/nfsd/syscall.h>
Wendy Cheng4373ea82008-01-17 11:10:12 -050014#include <linux/lockd/lockd.h>
Chuck Lever41160922009-08-09 15:09:40 -040015#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Boaz Harrosh9a74af22009-12-03 20:30:56 +020017#include "nfsd.h"
18#include "cache.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*
21 * We have a single directory with 9 nodes in it.
22 */
23enum {
24 NFSD_Root = 1,
25 NFSD_Svc,
26 NFSD_Add,
27 NFSD_Del,
28 NFSD_Export,
29 NFSD_Unexport,
30 NFSD_Getfd,
31 NFSD_Getfs,
32 NFSD_List,
J. Bruce Fieldse8e87532009-12-14 12:53:32 -050033 NFSD_Export_features,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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,
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050049 NFSD_Gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070050 NFSD_RecoveryDir,
NeilBrown70c3b762005-11-07 01:00:25 -080051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54/*
55 * write() for these nodes.
56 */
57static ssize_t write_svc(struct file *file, char *buf, size_t size);
58static ssize_t write_add(struct file *file, char *buf, size_t size);
59static ssize_t write_del(struct file *file, char *buf, size_t size);
60static ssize_t write_export(struct file *file, char *buf, size_t size);
61static ssize_t write_unexport(struct file *file, char *buf, size_t size);
62static ssize_t write_getfd(struct file *file, char *buf, size_t size);
63static ssize_t write_getfs(struct file *file, char *buf, size_t size);
64static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
Chuck Leverb046ccd2008-12-12 16:57:13 -050065static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
66static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static ssize_t write_threads(struct file *file, char *buf, size_t size);
Greg Bankseed29652006-10-02 02:18:02 -070068static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080069static ssize_t write_versions(struct file *file, char *buf, size_t size);
NeilBrown80212d52006-10-02 02:17:47 -070070static ssize_t write_ports(struct file *file, char *buf, size_t size);
NeilBrown596bbe52006-10-04 02:15:48 -070071static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080072#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050074static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
NeilBrown0964a3d2005-06-23 22:04:32 -070075static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static ssize_t (*write_op[])(struct file *, char *, size_t) = {
79 [NFSD_Svc] = write_svc,
80 [NFSD_Add] = write_add,
81 [NFSD_Del] = write_del,
82 [NFSD_Export] = write_export,
83 [NFSD_Unexport] = write_unexport,
84 [NFSD_Getfd] = write_getfd,
85 [NFSD_Getfs] = write_getfs,
86 [NFSD_Fh] = write_filehandle,
Chuck Leverb046ccd2008-12-12 16:57:13 -050087 [NFSD_FO_UnlockIP] = write_unlock_ip,
88 [NFSD_FO_UnlockFS] = write_unlock_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 [NFSD_Threads] = write_threads,
Greg Bankseed29652006-10-02 02:18:02 -070090 [NFSD_Pool_Threads] = write_pool_threads,
NeilBrown70c3b762005-11-07 01:00:25 -080091 [NFSD_Versions] = write_versions,
NeilBrown80212d52006-10-02 02:17:47 -070092 [NFSD_Ports] = write_ports,
NeilBrown596bbe52006-10-04 02:15:48 -070093 [NFSD_MaxBlkSize] = write_maxblksize,
NeilBrown70c3b762005-11-07 01:00:25 -080094#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 [NFSD_Leasetime] = write_leasetime,
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050096 [NFSD_Gracetime] = write_gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070097 [NFSD_RecoveryDir] = write_recoverydir,
NeilBrown70c3b762005-11-07 01:00:25 -080098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
102{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800103 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 char *data;
105 ssize_t rv;
106
Tobias Klausere8c96f82006-03-24 03:15:34 -0800107 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EINVAL;
109
110 data = simple_transaction_get(file, buf, size);
111 if (IS_ERR(data))
112 return PTR_ERR(data);
113
114 rv = write_op[ino](file, data, size);
NeilBrown8971a102007-02-14 00:33:11 -0800115 if (rv >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 simple_transaction_set(file, rv);
117 rv = size;
118 }
119 return rv;
120}
121
NeilBrown73900222005-11-07 01:00:24 -0800122static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
123{
124 if (! file->private_data) {
125 /* An attempt to read a transaction file without writing
126 * causes a 0-byte write so that the file can return
127 * state information
128 */
129 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
130 if (rv < 0)
131 return rv;
132 }
133 return simple_transaction_read(file, buf, size, pos);
134}
135
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800136static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .write = nfsctl_transaction_write,
NeilBrown73900222005-11-07 01:00:24 -0800138 .read = nfsctl_transaction_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .release = simple_transaction_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200140 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int exports_open(struct inode *inode, struct file *file)
144{
145 return seq_open(file, &nfs_exports_op);
146}
147
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800148static const struct file_operations exports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .open = exports_open,
150 .read = seq_read,
151 .llseek = seq_lseek,
152 .release = seq_release,
Denis V. Lunev9ef2db22008-04-29 01:02:04 -0700153 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
J. Bruce Fieldse8e87532009-12-14 12:53:32 -0500156static int export_features_show(struct seq_file *m, void *v)
157{
158 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
159 return 0;
160}
161
162static int export_features_open(struct inode *inode, struct file *file)
163{
164 return single_open(file, export_features_show, NULL);
165}
166
167static struct file_operations export_features_operations = {
168 .open = export_features_open,
169 .read = seq_read,
170 .llseek = seq_lseek,
171 .release = single_release,
172};
173
Greg Banks03cf6c92009-01-13 21:26:36 +1100174extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900175extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
Greg Banks03cf6c92009-01-13 21:26:36 +1100176
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700177static const struct file_operations pool_stats_operations = {
Greg Banks03cf6c92009-01-13 21:26:36 +1100178 .open = nfsd_pool_stats_open,
179 .read = seq_read,
180 .llseek = seq_lseek,
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900181 .release = nfsd_pool_stats_release,
Greg Banks03cf6c92009-01-13 21:26:36 +1100182 .owner = THIS_MODULE,
183};
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*----------------------------------------------------------------------------*/
186/*
187 * payload - write methods
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189
Chuck Lever262a0982008-12-12 16:57:35 -0500190/**
191 * write_svc - Start kernel's NFSD server
192 *
193 * Deprecated. /proc/fs/nfsd/threads is preferred.
194 * Function remains to support old versions of nfs-utils.
195 *
196 * Input:
197 * buf: struct nfsctl_svc
198 * svc_port: port number of this
199 * server's listener
200 * svc_nthreads: number of threads to start
201 * size: size in bytes of passed in nfsctl_svc
202 * Output:
203 * On success: returns zero
204 * On error: return code is negative errno value
205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static ssize_t write_svc(struct file *file, char *buf, size_t size)
207{
208 struct nfsctl_svc *data;
NeilBrown82e12fe2009-06-16 11:03:07 +1000209 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (size < sizeof(*data))
211 return -EINVAL;
212 data = (struct nfsctl_svc*) buf;
NeilBrown82e12fe2009-06-16 11:03:07 +1000213 err = nfsd_svc(data->svc_port, data->svc_nthreads);
214 if (err < 0)
215 return err;
216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Chuck Lever262a0982008-12-12 16:57:35 -0500219/**
220 * write_add - Add or modify client entry in auth unix cache
221 *
222 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
223 * Function remains to support old versions of nfs-utils.
224 *
225 * Input:
226 * buf: struct nfsctl_client
227 * cl_ident: '\0'-terminated C string
228 * containing domain name
229 * of client
230 * cl_naddr: no. of items in cl_addrlist
231 * cl_addrlist: array of client addresses
232 * cl_fhkeytype: ignored
233 * cl_fhkeylen: ignored
234 * cl_fhkey: ignored
235 * size: size in bytes of passed in nfsctl_client
236 * Output:
237 * On success: returns zero
238 * On error: return code is negative errno value
239 *
240 * Note: Only AF_INET client addresses are passed in, since
241 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static ssize_t write_add(struct file *file, char *buf, size_t size)
244{
245 struct nfsctl_client *data;
246 if (size < sizeof(*data))
247 return -EINVAL;
248 data = (struct nfsctl_client *)buf;
249 return exp_addclient(data);
250}
251
Chuck Lever262a0982008-12-12 16:57:35 -0500252/**
253 * write_del - Remove client from auth unix cache
254 *
255 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
256 * Function remains to support old versions of nfs-utils.
257 *
258 * Input:
259 * buf: struct nfsctl_client
260 * cl_ident: '\0'-terminated C string
261 * containing domain name
262 * of client
263 * cl_naddr: ignored
264 * cl_addrlist: ignored
265 * cl_fhkeytype: ignored
266 * cl_fhkeylen: ignored
267 * cl_fhkey: ignored
268 * size: size in bytes of passed in nfsctl_client
269 * Output:
270 * On success: returns zero
271 * On error: return code is negative errno value
272 *
273 * Note: Only AF_INET client addresses are passed in, since
274 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static ssize_t write_del(struct file *file, char *buf, size_t size)
277{
278 struct nfsctl_client *data;
279 if (size < sizeof(*data))
280 return -EINVAL;
281 data = (struct nfsctl_client *)buf;
282 return exp_delclient(data);
283}
284
Chuck Lever262a0982008-12-12 16:57:35 -0500285/**
286 * write_export - Export part or all of a local file system
287 *
288 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
289 * Function remains to support old versions of nfs-utils.
290 *
291 * Input:
292 * buf: struct nfsctl_export
293 * ex_client: '\0'-terminated C string
294 * containing domain name
295 * of client allowed to access
296 * this export
297 * ex_path: '\0'-terminated C string
298 * containing pathname of
299 * directory in local file system
300 * ex_dev: fsid to use for this export
301 * ex_ino: ignored
302 * ex_flags: export flags for this export
303 * ex_anon_uid: UID to use for anonymous
304 * requests
305 * ex_anon_gid: GID to use for anonymous
306 * requests
307 * size: size in bytes of passed in nfsctl_export
308 * Output:
309 * On success: returns zero
310 * On error: return code is negative errno value
311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static ssize_t write_export(struct file *file, char *buf, size_t size)
313{
314 struct nfsctl_export *data;
315 if (size < sizeof(*data))
316 return -EINVAL;
317 data = (struct nfsctl_export*)buf;
318 return exp_export(data);
319}
320
Chuck Lever262a0982008-12-12 16:57:35 -0500321/**
322 * write_unexport - Unexport a previously exported file system
323 *
324 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
325 * Function remains to support old versions of nfs-utils.
326 *
327 * Input:
328 * buf: struct nfsctl_export
329 * ex_client: '\0'-terminated C string
330 * containing domain name
331 * of client no longer allowed
332 * to access this export
333 * ex_path: '\0'-terminated C string
334 * containing pathname of
335 * directory in local file system
336 * ex_dev: ignored
337 * ex_ino: ignored
338 * ex_flags: ignored
339 * ex_anon_uid: ignored
340 * ex_anon_gid: ignored
341 * size: size in bytes of passed in nfsctl_export
342 * Output:
343 * On success: returns zero
344 * On error: return code is negative errno value
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static ssize_t write_unexport(struct file *file, char *buf, size_t size)
347{
348 struct nfsctl_export *data;
349
350 if (size < sizeof(*data))
351 return -EINVAL;
352 data = (struct nfsctl_export*)buf;
353 return exp_unexport(data);
354}
355
Chuck Lever262a0982008-12-12 16:57:35 -0500356/**
357 * write_getfs - Get a variable-length NFS file handle by path
358 *
359 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
360 * Function remains to support old versions of nfs-utils.
361 *
362 * Input:
363 * buf: struct nfsctl_fsparm
364 * gd_addr: socket address of client
365 * gd_path: '\0'-terminated C string
366 * containing pathname of
367 * directory in local file system
368 * gd_maxlen: maximum size of returned file
369 * handle
370 * size: size in bytes of passed in nfsctl_fsparm
371 * Output:
372 * On success: passed-in buffer filled with a knfsd_fh structure
373 * (a variable-length raw NFS file handle);
374 * return code is the size in bytes of the file handle
375 * On error: return code is negative errno value
376 *
377 * Note: Only AF_INET client addresses are passed in, since gd_addr
378 * is the same size as a struct sockaddr_in.
379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static ssize_t write_getfs(struct file *file, char *buf, size_t size)
381{
382 struct nfsctl_fsparm *data;
383 struct sockaddr_in *sin;
384 struct auth_domain *clp;
385 int err = 0;
386 struct knfsd_fh *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100387 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (size < sizeof(*data))
390 return -EINVAL;
391 data = (struct nfsctl_fsparm*)buf;
392 err = -EPROTONOSUPPORT;
393 if (data->gd_addr.sa_family != AF_INET)
394 goto out;
395 sin = (struct sockaddr_in *)&data->gd_addr;
396 if (data->gd_maxlen > NFS3_FHSIZE)
397 data->gd_maxlen = NFS3_FHSIZE;
398
399 res = (struct knfsd_fh*)buf;
400
401 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100402
403 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
404
405 clp = auth_unix_lookup(&in6);
406 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 err = -EPERM;
408 else {
409 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
410 auth_domain_put(clp);
411 }
412 exp_readunlock();
413 if (err == 0)
Andrew Morton12127492007-07-17 04:04:34 -0700414 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 out:
416 return err;
417}
418
Chuck Lever262a0982008-12-12 16:57:35 -0500419/**
420 * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
421 *
422 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
423 * Function remains to support old versions of nfs-utils.
424 *
425 * Input:
426 * buf: struct nfsctl_fdparm
427 * gd_addr: socket address of client
428 * gd_path: '\0'-terminated C string
429 * containing pathname of
430 * directory in local file system
431 * gd_version: fdparm structure version
432 * size: size in bytes of passed in nfsctl_fdparm
433 * Output:
434 * On success: passed-in buffer filled with nfsctl_res
435 * (a fixed-length raw NFS file handle);
436 * return code is the size in bytes of the file handle
437 * On error: return code is negative errno value
438 *
439 * Note: Only AF_INET client addresses are passed in, since gd_addr
440 * is the same size as a struct sockaddr_in.
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static ssize_t write_getfd(struct file *file, char *buf, size_t size)
443{
444 struct nfsctl_fdparm *data;
445 struct sockaddr_in *sin;
446 struct auth_domain *clp;
447 int err = 0;
448 struct knfsd_fh fh;
449 char *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100450 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (size < sizeof(*data))
453 return -EINVAL;
454 data = (struct nfsctl_fdparm*)buf;
455 err = -EPROTONOSUPPORT;
456 if (data->gd_addr.sa_family != AF_INET)
457 goto out;
458 err = -EINVAL;
459 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
460 goto out;
461
462 res = buf;
463 sin = (struct sockaddr_in *)&data->gd_addr;
464 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100465
466 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
467
468 clp = auth_unix_lookup(&in6);
469 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err = -EPERM;
471 else {
472 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
473 auth_domain_put(clp);
474 }
475 exp_readunlock();
476
477 if (err == 0) {
478 memset(res,0, NFS_FHSIZE);
479 memcpy(res, &fh.fh_base, fh.fh_size);
480 err = NFS_FHSIZE;
481 }
482 out:
483 return err;
484}
485
Chuck Lever262a0982008-12-12 16:57:35 -0500486/**
487 * write_unlock_ip - Release all locks used by a client
488 *
489 * Experimental.
490 *
491 * Input:
492 * buf: '\n'-terminated C string containing a
Chuck Lever41160922009-08-09 15:09:40 -0400493 * presentation format IP address
Chuck Lever262a0982008-12-12 16:57:35 -0500494 * size: length of C string in @buf
495 * Output:
496 * On success: returns zero if all specified locks were released;
497 * returns one if one or more locks were not released
498 * On error: return code is negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500499 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500500static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500501{
Chuck Lever41160922009-08-09 15:09:40 -0400502 struct sockaddr_storage address;
503 struct sockaddr *sap = (struct sockaddr *)&address;
504 size_t salen = sizeof(address);
Chuck Lever367c8c72008-06-30 18:58:14 -0400505 char *fo_path;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500506
507 /* sanity check */
508 if (size == 0)
509 return -EINVAL;
510
511 if (buf[size-1] != '\n')
512 return -EINVAL;
513
514 fo_path = buf;
515 if (qword_get(&buf, fo_path, size) < 0)
516 return -EINVAL;
517
Chuck Lever41160922009-08-09 15:09:40 -0400518 if (rpc_pton(fo_path, size, sap, salen) == 0)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500519 return -EINVAL;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500520
Chuck Lever41160922009-08-09 15:09:40 -0400521 return nlmsvc_unlock_all_by_ip(sap);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500522}
523
Chuck Lever262a0982008-12-12 16:57:35 -0500524/**
525 * write_unlock_fs - Release all locks on a local file system
526 *
527 * Experimental.
528 *
529 * Input:
530 * buf: '\n'-terminated C string containing the
531 * absolute pathname of a local file system
532 * size: length of C string in @buf
533 * Output:
534 * On success: returns zero if all specified locks were released;
535 * returns one if one or more locks were not released
536 * On error: return code is negative errno value
537 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500538static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
Wendy Cheng17efa372008-01-17 11:10:12 -0500539{
Al Viroa63bb992008-08-02 01:03:36 -0400540 struct path path;
Wendy Cheng17efa372008-01-17 11:10:12 -0500541 char *fo_path;
542 int error;
543
544 /* sanity check */
545 if (size == 0)
546 return -EINVAL;
547
548 if (buf[size-1] != '\n')
549 return -EINVAL;
550
551 fo_path = buf;
552 if (qword_get(&buf, fo_path, size) < 0)
553 return -EINVAL;
554
Al Viroa63bb992008-08-02 01:03:36 -0400555 error = kern_path(fo_path, 0, &path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500556 if (error)
557 return error;
558
Chuck Lever262a0982008-12-12 16:57:35 -0500559 /*
560 * XXX: Needs better sanity checking. Otherwise we could end up
561 * releasing locks on the wrong file system.
562 *
563 * For example:
564 * 1. Does the path refer to a directory?
565 * 2. Is that directory a mount point, or
566 * 3. Is that directory the root of an exported file system?
567 */
Al Viroa63bb992008-08-02 01:03:36 -0400568 error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
Wendy Cheng17efa372008-01-17 11:10:12 -0500569
Al Viroa63bb992008-08-02 01:03:36 -0400570 path_put(&path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500571 return error;
572}
573
Chuck Lever262a0982008-12-12 16:57:35 -0500574/**
575 * write_filehandle - Get a variable-length NFS file handle by path
576 *
577 * On input, the buffer contains a '\n'-terminated C string comprised of
578 * three alphanumeric words separated by whitespace. The string may
579 * contain escape sequences.
580 *
581 * Input:
582 * buf:
583 * domain: client domain name
584 * path: export pathname
585 * maxsize: numeric maximum size of
586 * @buf
587 * size: length of C string in @buf
588 * Output:
589 * On success: passed-in buffer filled with '\n'-terminated C
590 * string containing a ASCII hex text version
591 * of the NFS file handle;
592 * return code is the size in bytes of the string
593 * On error: return code is negative errno value
594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 char *dname, *path;
Andrew Morton246d95b2007-08-09 00:53:50 -0700598 int uninitialized_var(maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 char *mesg = buf;
600 int len;
601 struct auth_domain *dom;
602 struct knfsd_fh fh;
603
J. Bruce Fields87d26ea2008-01-22 17:40:42 -0500604 if (size == 0)
605 return -EINVAL;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (buf[size-1] != '\n')
608 return -EINVAL;
609 buf[size-1] = 0;
610
611 dname = mesg;
612 len = qword_get(&mesg, dname, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500613 if (len <= 0)
614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 path = dname+len+1;
617 len = qword_get(&mesg, path, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500618 if (len <= 0)
619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 len = get_int(&mesg, &maxsize);
622 if (len)
623 return len;
624
625 if (maxsize < NFS_FHSIZE)
626 return -EINVAL;
627 if (maxsize > NFS3_FHSIZE)
628 maxsize = NFS3_FHSIZE;
629
630 if (qword_get(&mesg, mesg, size)>0)
631 return -EINVAL;
632
633 /* we have all the words, they are in buf.. */
634 dom = unix_domain_find(dname);
635 if (!dom)
636 return -ENOMEM;
637
638 len = exp_rootfh(dom, path, &fh, maxsize);
639 auth_domain_put(dom);
640 if (len)
641 return len;
642
Chuck Lever54224f02008-12-12 16:57:20 -0500643 mesg = buf;
644 len = SIMPLE_TRANSACTION_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
646 mesg[-1] = '\n';
647 return mesg - buf;
648}
649
Chuck Lever262a0982008-12-12 16:57:35 -0500650/**
651 * write_threads - Start NFSD, or report the current number of running threads
652 *
653 * Input:
654 * buf: ignored
655 * size: zero
656 * Output:
657 * On success: passed-in buffer filled with '\n'-terminated C
658 * string numeric value representing the number of
659 * running NFSD threads;
660 * return code is the size in bytes of the string
661 * On error: return code is zero
662 *
663 * OR
664 *
665 * Input:
666 * buf: C string containing an unsigned
667 * integer value representing the
668 * number of NFSD threads to start
669 * size: non-zero length of C string in @buf
670 * Output:
671 * On success: NFS service is started;
672 * passed-in buffer filled with '\n'-terminated C
673 * string numeric value representing the number of
674 * running NFSD threads;
675 * return code is the size in bytes of the string
676 * On error: return code is zero or a negative errno value
677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static ssize_t write_threads(struct file *file, char *buf, size_t size)
679{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 char *mesg = buf;
681 int rv;
682 if (size > 0) {
683 int newthreads;
684 rv = get_int(&mesg, &newthreads);
685 if (rv)
686 return rv;
Chuck Lever9e074852008-12-12 16:57:27 -0500687 if (newthreads < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -EINVAL;
Chuck Lever9e074852008-12-12 16:57:27 -0500689 rv = nfsd_svc(NFS_PORT, newthreads);
NeilBrown82e12fe2009-06-16 11:03:07 +1000690 if (rv < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return rv;
NeilBrown82e12fe2009-06-16 11:03:07 +1000692 } else
693 rv = nfsd_nrthreads();
Chuck Levere06b6402009-04-23 19:33:25 -0400694
NeilBrown82e12fe2009-06-16 11:03:07 +1000695 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Chuck Lever262a0982008-12-12 16:57:35 -0500698/**
699 * write_pool_threads - Set or report the current number of threads per pool
700 *
701 * Input:
702 * buf: ignored
703 * size: zero
704 *
705 * OR
706 *
707 * Input:
708 * buf: C string containing whitespace-
709 * separated unsigned integer values
710 * representing the number of NFSD
711 * threads to start in each pool
712 * size: non-zero length of C string in @buf
713 * Output:
714 * On success: passed-in buffer filled with '\n'-terminated C
715 * string containing integer values representing the
716 * number of NFSD threads in each pool;
717 * return code is the size in bytes of the string
718 * On error: return code is zero or a negative errno value
719 */
Greg Bankseed29652006-10-02 02:18:02 -0700720static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
721{
722 /* if size > 0, look for an array of number of threads per node
723 * and apply them then write out number of threads per node as reply
724 */
725 char *mesg = buf;
726 int i;
727 int rv;
728 int len;
Neil Brownbedbdd82008-06-10 08:40:35 -0400729 int npools;
Greg Bankseed29652006-10-02 02:18:02 -0700730 int *nthreads;
731
Neil Brownbedbdd82008-06-10 08:40:35 -0400732 mutex_lock(&nfsd_mutex);
733 npools = nfsd_nrpools();
Greg Bankseed29652006-10-02 02:18:02 -0700734 if (npools == 0) {
735 /*
736 * NFS is shut down. The admin can start it by
737 * writing to the threads file but NOT the pool_threads
738 * file, sorry. Report zero threads.
739 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400740 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700741 strcpy(buf, "0\n");
742 return strlen(buf);
743 }
744
745 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
Neil Brownbedbdd82008-06-10 08:40:35 -0400746 rv = -ENOMEM;
Greg Bankseed29652006-10-02 02:18:02 -0700747 if (nthreads == NULL)
Neil Brownbedbdd82008-06-10 08:40:35 -0400748 goto out_free;
Greg Bankseed29652006-10-02 02:18:02 -0700749
750 if (size > 0) {
751 for (i = 0; i < npools; i++) {
752 rv = get_int(&mesg, &nthreads[i]);
753 if (rv == -ENOENT)
754 break; /* fewer numbers than pools */
755 if (rv)
756 goto out_free; /* syntax error */
757 rv = -EINVAL;
758 if (nthreads[i] < 0)
759 goto out_free;
760 }
761 rv = nfsd_set_nrthreads(i, nthreads);
762 if (rv)
763 goto out_free;
764 }
765
766 rv = nfsd_get_nrthreads(npools, nthreads);
767 if (rv)
768 goto out_free;
769
770 mesg = buf;
771 size = SIMPLE_TRANSACTION_LIMIT;
772 for (i = 0; i < npools && size > 0; i++) {
773 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
774 len = strlen(mesg);
775 size -= len;
776 mesg += len;
777 }
J. Bruce Fields413d63d7102009-07-28 11:37:25 -0400778 rv = mesg - buf;
Greg Bankseed29652006-10-02 02:18:02 -0700779out_free:
780 kfree(nthreads);
Neil Brownbedbdd82008-06-10 08:40:35 -0400781 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700782 return rv;
783}
784
Jeff Layton3dd98a32008-06-10 08:40:36 -0400785static ssize_t __write_versions(struct file *file, char *buf, size_t size)
NeilBrown70c3b762005-11-07 01:00:25 -0800786{
NeilBrown70c3b762005-11-07 01:00:25 -0800787 char *mesg = buf;
Benny Halevy8daf2202009-04-03 08:28:59 +0300788 char *vers, *minorp, sign;
Chuck Lever261758b2009-04-23 19:33:18 -0400789 int len, num, remaining;
Benny Halevy8daf2202009-04-03 08:28:59 +0300790 unsigned minor;
NeilBrown70c3b762005-11-07 01:00:25 -0800791 ssize_t tlen = 0;
792 char *sep;
793
794 if (size>0) {
795 if (nfsd_serv)
NeilBrown6658d3a2006-10-02 02:17:46 -0700796 /* Cannot change versions without updating
797 * nfsd_serv->sv_xdrsize, and reallocing
798 * rq_argp and rq_resp
799 */
NeilBrown70c3b762005-11-07 01:00:25 -0800800 return -EBUSY;
801 if (buf[size-1] != '\n')
802 return -EINVAL;
803 buf[size-1] = 0;
804
805 vers = mesg;
806 len = qword_get(&mesg, vers, size);
807 if (len <= 0) return -EINVAL;
808 do {
809 sign = *vers;
810 if (sign == '+' || sign == '-')
Benny Halevy8daf2202009-04-03 08:28:59 +0300811 num = simple_strtol((vers+1), &minorp, 0);
NeilBrown70c3b762005-11-07 01:00:25 -0800812 else
Benny Halevy8daf2202009-04-03 08:28:59 +0300813 num = simple_strtol(vers, &minorp, 0);
814 if (*minorp == '.') {
815 if (num < 4)
816 return -EINVAL;
817 minor = simple_strtoul(minorp+1, NULL, 0);
818 if (minor == 0)
819 return -EINVAL;
820 if (nfsd_minorversion(minor, sign == '-' ?
821 NFSD_CLEAR : NFSD_SET) < 0)
822 return -EINVAL;
823 goto next;
824 }
NeilBrown70c3b762005-11-07 01:00:25 -0800825 switch(num) {
826 case 2:
827 case 3:
828 case 4:
NeilBrown6658d3a2006-10-02 02:17:46 -0700829 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
NeilBrown70c3b762005-11-07 01:00:25 -0800830 break;
831 default:
832 return -EINVAL;
833 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300834 next:
NeilBrown70c3b762005-11-07 01:00:25 -0800835 vers += len + 1;
NeilBrown70c3b762005-11-07 01:00:25 -0800836 } while ((len = qword_get(&mesg, vers, size)) > 0);
837 /* If all get turned off, turn them back on, as
838 * having no versions is BAD
839 */
NeilBrown6658d3a2006-10-02 02:17:46 -0700840 nfsd_reset_versions();
NeilBrown70c3b762005-11-07 01:00:25 -0800841 }
Chuck Lever261758b2009-04-23 19:33:18 -0400842
NeilBrown70c3b762005-11-07 01:00:25 -0800843 /* Now write current state into reply buffer */
844 len = 0;
845 sep = "";
Chuck Lever261758b2009-04-23 19:33:18 -0400846 remaining = SIMPLE_TRANSACTION_LIMIT;
NeilBrown70c3b762005-11-07 01:00:25 -0800847 for (num=2 ; num <= 4 ; num++)
NeilBrown6658d3a2006-10-02 02:17:46 -0700848 if (nfsd_vers(num, NFSD_AVAIL)) {
Chuck Lever261758b2009-04-23 19:33:18 -0400849 len = snprintf(buf, remaining, "%s%c%d", sep,
NeilBrown6658d3a2006-10-02 02:17:46 -0700850 nfsd_vers(num, NFSD_TEST)?'+':'-',
NeilBrown70c3b762005-11-07 01:00:25 -0800851 num);
852 sep = " ";
Chuck Lever261758b2009-04-23 19:33:18 -0400853
854 if (len > remaining)
855 break;
856 remaining -= len;
857 buf += len;
858 tlen += len;
NeilBrown70c3b762005-11-07 01:00:25 -0800859 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300860 if (nfsd_vers(4, NFSD_AVAIL))
Chuck Lever261758b2009-04-23 19:33:18 -0400861 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
862 minor++) {
863 len = snprintf(buf, remaining, " %c4.%u",
Benny Halevy8daf2202009-04-03 08:28:59 +0300864 (nfsd_vers(4, NFSD_TEST) &&
865 nfsd_minorversion(minor, NFSD_TEST)) ?
866 '+' : '-',
867 minor);
Chuck Lever261758b2009-04-23 19:33:18 -0400868
869 if (len > remaining)
870 break;
871 remaining -= len;
872 buf += len;
873 tlen += len;
874 }
875
876 len = snprintf(buf, remaining, "\n");
877 if (len > remaining)
878 return -EINVAL;
879 return tlen + len;
NeilBrown70c3b762005-11-07 01:00:25 -0800880}
881
Chuck Lever262a0982008-12-12 16:57:35 -0500882/**
883 * write_versions - Set or report the available NFS protocol versions
884 *
885 * Input:
886 * buf: ignored
887 * size: zero
888 * Output:
889 * On success: passed-in buffer filled with '\n'-terminated C
890 * string containing positive or negative integer
891 * values representing the current status of each
892 * protocol version;
893 * return code is the size in bytes of the string
894 * On error: return code is zero or a negative errno value
895 *
896 * OR
897 *
898 * Input:
899 * buf: C string containing whitespace-
900 * separated positive or negative
901 * integer values representing NFS
902 * protocol versions to enable ("+n")
903 * or disable ("-n")
904 * size: non-zero length of C string in @buf
905 * Output:
906 * On success: status of zero or more protocol versions has
907 * been updated; passed-in buffer filled with
908 * '\n'-terminated C string containing positive
909 * or negative integer values representing the
910 * current status of each 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
913 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400914static ssize_t write_versions(struct file *file, char *buf, size_t size)
915{
916 ssize_t rv;
917
918 mutex_lock(&nfsd_mutex);
919 rv = __write_versions(file, buf, size);
920 mutex_unlock(&nfsd_mutex);
921 return rv;
922}
923
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400924/*
Chuck Lever0a5372d2009-04-23 19:32:10 -0400925 * Zero-length write. Return a list of NFSD's current listener
926 * transports.
927 */
928static ssize_t __write_ports_names(char *buf)
929{
930 if (nfsd_serv == NULL)
931 return 0;
Chuck Lever335c54b2009-04-23 19:32:25 -0400932 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Lever0a5372d2009-04-23 19:32:10 -0400933}
934
935/*
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400936 * A single 'fd' number was written, in which case it must be for
937 * a socket of a supported family/protocol, and we use it as an
938 * nfsd listener.
939 */
940static ssize_t __write_ports_addfd(char *buf)
941{
942 char *mesg = buf;
943 int fd, err;
944
945 err = get_int(&mesg, &fd);
946 if (err != 0 || fd < 0)
947 return -EINVAL;
948
949 err = nfsd_create_serv();
950 if (err != 0)
951 return err;
952
Chuck Leverbfba9ab2009-04-23 19:32:33 -0400953 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400954 if (err < 0) {
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400955 svc_destroy(nfsd_serv);
956 return err;
957 }
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400958
Chuck Leverea068ba2009-04-23 19:32:18 -0400959 /* Decrease the count, but don't shut down the service */
960 nfsd_serv->sv_nrthreads--;
961 return err;
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400962}
963
964/*
Chuck Lever82d56592009-04-23 19:31:48 -0400965 * A '-' followed by the 'name' of a socket means we close the socket.
966 */
967static ssize_t __write_ports_delfd(char *buf)
968{
969 char *toclose;
970 int len = 0;
971
972 toclose = kstrdup(buf + 1, GFP_KERNEL);
973 if (toclose == NULL)
974 return -ENOMEM;
975
976 if (nfsd_serv != NULL)
Chuck Lever8435d342009-04-23 19:32:40 -0400977 len = svc_sock_names(nfsd_serv, buf,
978 SIMPLE_TRANSACTION_LIMIT, toclose);
Chuck Lever82d56592009-04-23 19:31:48 -0400979 kfree(toclose);
980 return len;
981}
982
983/*
Chuck Lever4eb68c22009-04-23 19:31:40 -0400984 * A transport listener is added by writing it's transport name and
985 * a port number.
986 */
987static ssize_t __write_ports_addxprt(char *buf)
988{
989 char transport[16];
Chuck Lever37498292010-01-26 14:04:22 -0500990 struct svc_xprt *xprt;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400991 int port, err;
992
993 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
994 return -EINVAL;
995
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700996 if (port < 1 || port > USHRT_MAX)
Chuck Lever4eb68c22009-04-23 19:31:40 -0400997 return -EINVAL;
998
999 err = nfsd_create_serv();
1000 if (err != 0)
1001 return err;
1002
1003 err = svc_create_xprt(nfsd_serv, transport,
1004 PF_INET, port, SVC_SOCK_ANONYMOUS);
Chuck Lever68717902010-01-26 14:04:13 -05001005 if (err < 0)
Chuck Lever37498292010-01-26 14:04:22 -05001006 goto out_err;
1007
1008 err = svc_create_xprt(nfsd_serv, transport,
1009 PF_INET6, port, SVC_SOCK_ANONYMOUS);
1010 if (err < 0 && err != -EAFNOSUPPORT)
1011 goto out_close;
Jeff Layton0cd14a02010-07-19 16:50:06 -04001012
1013 /* Decrease the count, but don't shut down the service */
1014 nfsd_serv->sv_nrthreads--;
Chuck Lever4eb68c22009-04-23 19:31:40 -04001015 return 0;
Chuck Lever37498292010-01-26 14:04:22 -05001016out_close:
1017 xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
1018 if (xprt != NULL) {
1019 svc_close_xprt(xprt);
1020 svc_xprt_put(xprt);
1021 }
1022out_err:
Jeff Layton0cd14a02010-07-19 16:50:06 -04001023 svc_destroy(nfsd_serv);
Chuck Lever37498292010-01-26 14:04:22 -05001024 return err;
Chuck Lever4eb68c22009-04-23 19:31:40 -04001025}
1026
1027/*
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001028 * A transport listener is removed by writing a "-", it's transport
1029 * name, and it's port number.
1030 */
1031static ssize_t __write_ports_delxprt(char *buf)
1032{
1033 struct svc_xprt *xprt;
1034 char transport[16];
1035 int port;
1036
1037 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1038 return -EINVAL;
1039
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001040 if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001041 return -EINVAL;
1042
1043 xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1044 if (xprt == NULL)
1045 return -ENOTCONN;
1046
1047 svc_close_xprt(xprt);
1048 svc_xprt_put(xprt);
1049 return 0;
1050}
1051
Neil Brownbedbdd82008-06-10 08:40:35 -04001052static ssize_t __write_ports(struct file *file, char *buf, size_t size)
NeilBrown80212d52006-10-02 02:17:47 -07001053{
Chuck Lever0a5372d2009-04-23 19:32:10 -04001054 if (size == 0)
1055 return __write_ports_names(buf);
Chuck Lever0b7c2f62009-04-23 19:31:55 -04001056
1057 if (isdigit(buf[0]))
1058 return __write_ports_addfd(buf);
Chuck Lever82d56592009-04-23 19:31:48 -04001059
1060 if (buf[0] == '-' && isdigit(buf[1]))
1061 return __write_ports_delfd(buf);
Chuck Lever4eb68c22009-04-23 19:31:40 -04001062
1063 if (isalpha(buf[0]))
1064 return __write_ports_addxprt(buf);
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001065
1066 if (buf[0] == '-' && isalpha(buf[1]))
1067 return __write_ports_delxprt(buf);
1068
NeilBrownb41b66d2006-10-02 02:17:48 -07001069 return -EINVAL;
NeilBrown80212d52006-10-02 02:17:47 -07001070}
1071
Chuck Lever262a0982008-12-12 16:57:35 -05001072/**
1073 * write_ports - Pass a socket file descriptor or transport name to listen on
1074 *
1075 * Input:
1076 * buf: ignored
1077 * size: zero
1078 * Output:
1079 * On success: passed-in buffer filled with a '\n'-terminated C
1080 * string containing a whitespace-separated list of
1081 * named NFSD listeners;
1082 * return code is the size in bytes of the string
1083 * On error: return code is zero or a negative errno value
1084 *
1085 * OR
1086 *
1087 * Input:
1088 * buf: C string containing an unsigned
1089 * integer value representing a bound
1090 * but unconnected socket that is to be
Chuck Leverc71206a2009-04-23 19:32:03 -04001091 * used as an NFSD listener; listen(3)
1092 * must be called for a SOCK_STREAM
1093 * socket, otherwise it is ignored
Chuck Lever262a0982008-12-12 16:57:35 -05001094 * size: non-zero length of C string in @buf
1095 * Output:
1096 * On success: NFS service is started;
1097 * passed-in buffer filled with a '\n'-terminated C
1098 * string containing a unique alphanumeric name of
1099 * the listener;
1100 * return code is the size in bytes of the string
1101 * On error: return code is a negative errno value
1102 *
1103 * OR
1104 *
1105 * Input:
1106 * buf: C string containing a "-" followed
1107 * by an integer value representing a
1108 * previously passed in socket file
1109 * descriptor
1110 * size: non-zero length of C string in @buf
1111 * Output:
1112 * On success: NFS service no longer listens on that socket;
1113 * passed-in buffer filled with a '\n'-terminated C
1114 * string containing a unique name of the listener;
1115 * return code is the size in bytes of the string
1116 * On error: return code is a negative errno value
1117 *
1118 * OR
1119 *
1120 * Input:
1121 * buf: C string containing a transport
1122 * name and an unsigned integer value
1123 * representing the port to listen on,
1124 * separated by whitespace
1125 * size: non-zero length of C string in @buf
1126 * Output:
1127 * On success: returns zero; NFS service is started
1128 * On error: return code is a negative errno value
1129 *
1130 * OR
1131 *
1132 * Input:
1133 * buf: C string containing a "-" followed
1134 * by a transport name and an unsigned
1135 * integer value representing the port
1136 * to listen on, separated by whitespace
1137 * size: non-zero length of C string in @buf
1138 * Output:
1139 * On success: returns zero; NFS service no longer listens
1140 * on that transport
1141 * On error: return code is a negative errno value
1142 */
Neil Brownbedbdd82008-06-10 08:40:35 -04001143static ssize_t write_ports(struct file *file, char *buf, size_t size)
1144{
1145 ssize_t rv;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001146
Neil Brownbedbdd82008-06-10 08:40:35 -04001147 mutex_lock(&nfsd_mutex);
1148 rv = __write_ports(file, buf, size);
1149 mutex_unlock(&nfsd_mutex);
1150 return rv;
1151}
1152
1153
NeilBrown596bbe52006-10-04 02:15:48 -07001154int nfsd_max_blksize;
1155
Chuck Lever262a0982008-12-12 16:57:35 -05001156/**
1157 * write_maxblksize - Set or report the current NFS blksize
1158 *
1159 * Input:
1160 * buf: ignored
1161 * size: zero
1162 *
1163 * OR
1164 *
1165 * Input:
1166 * buf: C string containing an unsigned
1167 * integer value representing the new
1168 * NFS blksize
1169 * size: non-zero length of C string in @buf
1170 * Output:
1171 * On success: passed-in buffer filled with '\n'-terminated C string
1172 * containing numeric value of the current NFS blksize
1173 * setting;
1174 * return code is the size in bytes of the string
1175 * On error: return code is zero or a negative errno value
1176 */
NeilBrown596bbe52006-10-04 02:15:48 -07001177static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1178{
1179 char *mesg = buf;
1180 if (size > 0) {
1181 int bsize;
1182 int rv = get_int(&mesg, &bsize);
1183 if (rv)
1184 return rv;
1185 /* force bsize into allowed range and
1186 * required alignment.
1187 */
1188 if (bsize < 1024)
1189 bsize = 1024;
1190 if (bsize > NFSSVC_MAXBLKSIZE)
1191 bsize = NFSSVC_MAXBLKSIZE;
1192 bsize &= ~(1024-1);
Neil Brownbedbdd82008-06-10 08:40:35 -04001193 mutex_lock(&nfsd_mutex);
J. Bruce Fields7fa53cc2010-08-06 18:00:33 -04001194 if (nfsd_serv) {
Neil Brownbedbdd82008-06-10 08:40:35 -04001195 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001196 return -EBUSY;
1197 }
1198 nfsd_max_blksize = bsize;
Neil Brownbedbdd82008-06-10 08:40:35 -04001199 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001200 }
Chuck Levere06b6402009-04-23 19:33:25 -04001201
1202 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1203 nfsd_max_blksize);
NeilBrown596bbe52006-10-04 02:15:48 -07001204}
1205
NeilBrown70c3b762005-11-07 01:00:25 -08001206#ifdef CONFIG_NFSD_V4
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001207static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 char *mesg = buf;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001210 int rv, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (size > 0) {
Jeff Layton3dd98a32008-06-10 08:40:36 -04001213 if (nfsd_serv)
1214 return -EBUSY;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001215 rv = get_int(&mesg, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (rv)
1217 return rv;
J. Bruce Fieldse7b184f2010-03-02 11:18:40 -05001218 /*
1219 * Some sanity checking. We don't have a reason for
1220 * these particular numbers, but problems with the
1221 * extremes are:
1222 * - Too short: the briefest network outage may
1223 * cause clients to lose all their locks. Also,
1224 * the frequent polling may be wasteful.
1225 * - Too long: do you really want reboot recovery
1226 * to take more than an hour? Or to make other
1227 * clients wait an hour before being able to
1228 * revoke a dead client's locks?
1229 */
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001230 if (i < 10 || i > 3600)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EINVAL;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001232 *time = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Chuck Levere06b6402009-04-23 19:33:25 -04001234
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001235 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
1236}
1237
1238static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1239{
1240 ssize_t rv;
1241
1242 mutex_lock(&nfsd_mutex);
1243 rv = __nfsd4_write_time(file, buf, size, time);
1244 mutex_unlock(&nfsd_mutex);
1245 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Chuck Lever262a0982008-12-12 16:57:35 -05001248/**
1249 * write_leasetime - Set or report the current NFSv4 lease time
1250 *
1251 * Input:
1252 * buf: ignored
1253 * size: zero
1254 *
1255 * OR
1256 *
1257 * Input:
1258 * buf: C string containing an unsigned
1259 * integer value representing the new
1260 * NFSv4 lease expiry time
1261 * size: non-zero length of C string in @buf
1262 * Output:
1263 * On success: passed-in buffer filled with '\n'-terminated C
1264 * string containing unsigned integer value of the
1265 * current lease expiry time;
1266 * return code is the size in bytes of the string
1267 * On error: return code is zero or a negative errno value
1268 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001269static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1270{
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001271 return nfsd4_write_time(file, buf, size, &nfsd4_lease);
Jeff Layton3dd98a32008-06-10 08:40:36 -04001272}
1273
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -05001274/**
1275 * write_gracetime - Set or report current NFSv4 grace period time
1276 *
1277 * As above, but sets the time of the NFSv4 grace period.
1278 *
1279 * Note this should never be set to less than the *previous*
1280 * lease-period time, but we don't try to enforce this. (In the common
1281 * case (a new boot), we don't know what the previous lease time was
1282 * anyway.)
1283 */
1284static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1285{
1286 return nfsd4_write_time(file, buf, size, &nfsd4_grace);
1287}
1288
Jeff Layton3dd98a32008-06-10 08:40:36 -04001289extern char *nfs4_recoverydir(void);
1290
1291static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
NeilBrown0964a3d2005-06-23 22:04:32 -07001292{
1293 char *mesg = buf;
1294 char *recdir;
1295 int len, status;
1296
Jeff Layton3dd98a32008-06-10 08:40:36 -04001297 if (size > 0) {
1298 if (nfsd_serv)
1299 return -EBUSY;
1300 if (size > PATH_MAX || buf[size-1] != '\n')
1301 return -EINVAL;
1302 buf[size-1] = 0;
NeilBrown0964a3d2005-06-23 22:04:32 -07001303
Jeff Layton3dd98a32008-06-10 08:40:36 -04001304 recdir = mesg;
1305 len = qword_get(&mesg, recdir, size);
1306 if (len <= 0)
1307 return -EINVAL;
NeilBrown0964a3d2005-06-23 22:04:32 -07001308
Jeff Layton3dd98a32008-06-10 08:40:36 -04001309 status = nfs4_reset_recoverydir(recdir);
Andi Kleen69049962010-07-20 15:24:27 -07001310 if (status)
1311 return status;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001312 }
Chuck Lever3d72ab82009-04-23 19:33:10 -04001313
1314 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1315 nfs4_recoverydir());
NeilBrown0964a3d2005-06-23 22:04:32 -07001316}
Jeff Layton3dd98a32008-06-10 08:40:36 -04001317
Chuck Lever262a0982008-12-12 16:57:35 -05001318/**
1319 * write_recoverydir - Set or report the pathname of the recovery directory
1320 *
1321 * Input:
1322 * buf: ignored
1323 * size: zero
1324 *
1325 * OR
1326 *
1327 * Input:
1328 * buf: C string containing the pathname
1329 * of the directory on a local file
1330 * system containing permanent NFSv4
1331 * recovery data
1332 * size: non-zero length of C string in @buf
1333 * Output:
1334 * On success: passed-in buffer filled with '\n'-terminated C string
1335 * containing the current recovery pathname setting;
1336 * return code is the size in bytes of the string
1337 * On error: return code is zero or a negative errno value
1338 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001339static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1340{
1341 ssize_t rv;
1342
1343 mutex_lock(&nfsd_mutex);
1344 rv = __write_recoverydir(file, buf, size);
1345 mutex_unlock(&nfsd_mutex);
1346 return rv;
1347}
1348
NeilBrown70c3b762005-11-07 01:00:25 -08001349#endif
NeilBrown0964a3d2005-06-23 22:04:32 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/*----------------------------------------------------------------------------*/
1352/*
1353 * populating the filesystem.
1354 */
1355
1356static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1357{
1358 static struct tree_descr nfsd_files[] = {
1359 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1360 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1361 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1362 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1363 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1364 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1365 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
1366 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
J. Bruce Fieldse8e87532009-12-14 12:53:32 -05001367 [NFSD_Export_features] = {"export_features",
1368 &export_features_operations, S_IRUGO},
Wendy Cheng4373ea82008-01-17 11:10:12 -05001369 [NFSD_FO_UnlockIP] = {"unlock_ip",
1370 &transaction_ops, S_IWUSR|S_IRUSR},
Wendy Cheng17efa372008-01-17 11:10:12 -05001371 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1372 &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1374 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Bankseed29652006-10-02 02:18:02 -07001375 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Banks03cf6c92009-01-13 21:26:36 +11001376 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
NeilBrown70c3b762005-11-07 01:00:25 -08001377 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown80212d52006-10-02 02:17:47 -07001378 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
NeilBrown596bbe52006-10-04 02:15:48 -07001379 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_NFSD_V4
1381 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -05001382 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown0964a3d2005-06-23 22:04:32 -07001383 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif
1385 /* last one */ {""}
1386 };
1387 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1388}
1389
David Howells454e2392006-06-23 02:02:57 -07001390static int nfsd_get_sb(struct file_system_type *fs_type,
1391 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
David Howells454e2392006-06-23 02:02:57 -07001393 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396static struct file_system_type nfsd_fs_type = {
1397 .owner = THIS_MODULE,
1398 .name = "nfsd",
1399 .get_sb = nfsd_get_sb,
1400 .kill_sb = kill_litter_super,
1401};
1402
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001403#ifdef CONFIG_PROC_FS
1404static int create_proc_exports_entry(void)
1405{
1406 struct proc_dir_entry *entry;
1407
1408 entry = proc_mkdir("fs/nfs", NULL);
1409 if (!entry)
1410 return -ENOMEM;
Denis V. Lunev9ef2db22008-04-29 01:02:04 -07001411 entry = proc_create("exports", 0, entry, &exports_operations);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001412 if (!entry)
1413 return -ENOMEM;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001414 return 0;
1415}
1416#else /* CONFIG_PROC_FS */
1417static int create_proc_exports_entry(void)
1418{
1419 return 0;
1420}
1421#endif
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423static int __init init_nfsd(void)
1424{
1425 int retval;
1426 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1427
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001428 retval = nfs4_state_init(); /* nfs4 locking state */
1429 if (retval)
1430 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 nfsd_stat_init(); /* Statistics */
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001432 retval = nfsd_reply_cache_init();
1433 if (retval)
1434 goto out_free_stat;
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001435 retval = nfsd_export_init();
1436 if (retval)
1437 goto out_free_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 nfsd_lockd_init(); /* lockd->nfsd callbacks */
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001439 retval = nfsd_idmap_init();
1440 if (retval)
1441 goto out_free_lockd;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001442 retval = create_proc_exports_entry();
1443 if (retval)
1444 goto out_free_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 retval = register_filesystem(&nfsd_fs_type);
J. Bruce Fields26808d32007-11-09 13:44:06 -05001446 if (retval)
1447 goto out_free_all;
1448 return 0;
1449out_free_all:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001450 remove_proc_entry("fs/nfs/exports", NULL);
1451 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001452out_free_idmap:
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001453 nfsd_idmap_shutdown();
1454out_free_lockd:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001455 nfsd_lockd_shutdown();
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001456 nfsd_export_shutdown();
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001457out_free_cache:
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001458 nfsd_reply_cache_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001459out_free_stat:
1460 nfsd_stat_shutdown();
J. Bruce Fields26808d32007-11-09 13:44:06 -05001461 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return retval;
1463}
1464
1465static void __exit exit_nfsd(void)
1466{
1467 nfsd_export_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001468 nfsd_reply_cache_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 remove_proc_entry("fs/nfs/exports", NULL);
1470 remove_proc_entry("fs/nfs", NULL);
1471 nfsd_stat_shutdown();
1472 nfsd_lockd_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 nfsd_idmap_shutdown();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001474 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unregister_filesystem(&nfsd_fs_type);
1476}
1477
1478MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1479MODULE_LICENSE("GPL");
1480module_init(init_nfsd)
1481module_exit(exit_nfsd)