blob: b278e444e2f47fdb50fcdb6d34387c5433b151d9 [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,
NeilBrown1e140562010-09-22 12:55:07 +100025#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 NFSD_Svc,
27 NFSD_Add,
28 NFSD_Del,
29 NFSD_Export,
30 NFSD_Unexport,
31 NFSD_Getfd,
32 NFSD_Getfs,
NeilBrown1e140562010-09-22 12:55:07 +100033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 NFSD_List,
J. Bruce Fieldse8e87532009-12-14 12:53:32 -050035 NFSD_Export_features,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 NFSD_Fh,
Wendy Cheng4373ea82008-01-17 11:10:12 -050037 NFSD_FO_UnlockIP,
Wendy Cheng17efa372008-01-17 11:10:12 -050038 NFSD_FO_UnlockFS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 NFSD_Threads,
Greg Bankseed29652006-10-02 02:18:02 -070040 NFSD_Pool_Threads,
Greg Banks03cf6c92009-01-13 21:26:36 +110041 NFSD_Pool_Stats,
NeilBrown70c3b762005-11-07 01:00:25 -080042 NFSD_Versions,
NeilBrown80212d52006-10-02 02:17:47 -070043 NFSD_Ports,
NeilBrown596bbe52006-10-04 02:15:48 -070044 NFSD_MaxBlkSize,
NeilBrown70c3b762005-11-07 01:00:25 -080045 /*
46 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
47 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
48 */
49#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 NFSD_Leasetime,
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050051 NFSD_Gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070052 NFSD_RecoveryDir,
NeilBrown70c3b762005-11-07 01:00:25 -080053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/*
57 * write() for these nodes.
58 */
NeilBrown1e140562010-09-22 12:55:07 +100059#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static ssize_t write_svc(struct file *file, char *buf, size_t size);
61static ssize_t write_add(struct file *file, char *buf, size_t size);
62static ssize_t write_del(struct file *file, char *buf, size_t size);
63static ssize_t write_export(struct file *file, char *buf, size_t size);
64static ssize_t write_unexport(struct file *file, char *buf, size_t size);
65static ssize_t write_getfd(struct file *file, char *buf, size_t size);
66static ssize_t write_getfs(struct file *file, char *buf, size_t size);
NeilBrown1e140562010-09-22 12:55:07 +100067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
Chuck Leverb046ccd2008-12-12 16:57:13 -050069static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
70static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static ssize_t write_threads(struct file *file, char *buf, size_t size);
Greg Bankseed29652006-10-02 02:18:02 -070072static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080073static ssize_t write_versions(struct file *file, char *buf, size_t size);
NeilBrown80212d52006-10-02 02:17:47 -070074static ssize_t write_ports(struct file *file, char *buf, size_t size);
NeilBrown596bbe52006-10-04 02:15:48 -070075static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080076#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050078static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
NeilBrown0964a3d2005-06-23 22:04:32 -070079static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static ssize_t (*write_op[])(struct file *, char *, size_t) = {
NeilBrown1e140562010-09-22 12:55:07 +100083#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 [NFSD_Svc] = write_svc,
85 [NFSD_Add] = write_add,
86 [NFSD_Del] = write_del,
87 [NFSD_Export] = write_export,
88 [NFSD_Unexport] = write_unexport,
89 [NFSD_Getfd] = write_getfd,
90 [NFSD_Getfs] = write_getfs,
NeilBrown1e140562010-09-22 12:55:07 +100091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 [NFSD_Fh] = write_filehandle,
Chuck Leverb046ccd2008-12-12 16:57:13 -050093 [NFSD_FO_UnlockIP] = write_unlock_ip,
94 [NFSD_FO_UnlockFS] = write_unlock_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 [NFSD_Threads] = write_threads,
Greg Bankseed29652006-10-02 02:18:02 -070096 [NFSD_Pool_Threads] = write_pool_threads,
NeilBrown70c3b762005-11-07 01:00:25 -080097 [NFSD_Versions] = write_versions,
NeilBrown80212d52006-10-02 02:17:47 -070098 [NFSD_Ports] = write_ports,
NeilBrown596bbe52006-10-04 02:15:48 -070099 [NFSD_MaxBlkSize] = write_maxblksize,
NeilBrown70c3b762005-11-07 01:00:25 -0800100#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 [NFSD_Leasetime] = write_leasetime,
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -0500102 [NFSD_Gracetime] = write_gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -0700103 [NFSD_RecoveryDir] = write_recoverydir,
NeilBrown70c3b762005-11-07 01:00:25 -0800104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
108{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800109 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 char *data;
111 ssize_t rv;
112
Tobias Klausere8c96f82006-03-24 03:15:34 -0800113 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EINVAL;
115
116 data = simple_transaction_get(file, buf, size);
117 if (IS_ERR(data))
118 return PTR_ERR(data);
119
120 rv = write_op[ino](file, data, size);
NeilBrown8971a102007-02-14 00:33:11 -0800121 if (rv >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 simple_transaction_set(file, rv);
123 rv = size;
124 }
125 return rv;
126}
127
NeilBrown73900222005-11-07 01:00:24 -0800128static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
129{
NeilBrownc67874f2010-09-22 12:55:07 +1000130 static int warned;
131 if (file->f_dentry->d_name.name[0] == '.' && !warned) {
132 char name[sizeof(current->comm)];
133 printk(KERN_INFO
134 "Warning: \"%s\" uses deprecated NFSD interface: %s."
135 " This will be removed in 2.6.40\n",
136 get_task_comm(name, current),
137 file->f_dentry->d_name.name);
138 warned = 1;
139 }
NeilBrown73900222005-11-07 01:00:24 -0800140 if (! file->private_data) {
141 /* An attempt to read a transaction file without writing
142 * causes a 0-byte write so that the file can return
143 * state information
144 */
145 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
146 if (rv < 0)
147 return rv;
148 }
149 return simple_transaction_read(file, buf, size, pos);
150}
151
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800152static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .write = nfsctl_transaction_write,
NeilBrown73900222005-11-07 01:00:24 -0800154 .read = nfsctl_transaction_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .release = simple_transaction_release,
156};
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int exports_open(struct inode *inode, struct file *file)
159{
160 return seq_open(file, &nfs_exports_op);
161}
162
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800163static const struct file_operations exports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .open = exports_open,
165 .read = seq_read,
166 .llseek = seq_lseek,
167 .release = seq_release,
Denis V. Lunev9ef2db22008-04-29 01:02:04 -0700168 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
J. Bruce Fieldse8e87532009-12-14 12:53:32 -0500171static int export_features_show(struct seq_file *m, void *v)
172{
173 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
174 return 0;
175}
176
177static int export_features_open(struct inode *inode, struct file *file)
178{
179 return single_open(file, export_features_show, NULL);
180}
181
182static struct file_operations export_features_operations = {
183 .open = export_features_open,
184 .read = seq_read,
185 .llseek = seq_lseek,
186 .release = single_release,
187};
188
Greg Banks03cf6c92009-01-13 21:26:36 +1100189extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900190extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
Greg Banks03cf6c92009-01-13 21:26:36 +1100191
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700192static const struct file_operations pool_stats_operations = {
Greg Banks03cf6c92009-01-13 21:26:36 +1100193 .open = nfsd_pool_stats_open,
194 .read = seq_read,
195 .llseek = seq_lseek,
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900196 .release = nfsd_pool_stats_release,
Greg Banks03cf6c92009-01-13 21:26:36 +1100197 .owner = THIS_MODULE,
198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*----------------------------------------------------------------------------*/
201/*
202 * payload - write methods
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204
NeilBrown1e140562010-09-22 12:55:07 +1000205#ifdef CONFIG_NFSD_DEPRECATED
Chuck Lever262a0982008-12-12 16:57:35 -0500206/**
207 * write_svc - Start kernel's NFSD server
208 *
209 * Deprecated. /proc/fs/nfsd/threads is preferred.
210 * Function remains to support old versions of nfs-utils.
211 *
212 * Input:
213 * buf: struct nfsctl_svc
214 * svc_port: port number of this
215 * server's listener
216 * svc_nthreads: number of threads to start
217 * size: size in bytes of passed in nfsctl_svc
218 * Output:
219 * On success: returns zero
220 * On error: return code is negative errno value
221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static ssize_t write_svc(struct file *file, char *buf, size_t size)
223{
224 struct nfsctl_svc *data;
NeilBrown82e12fe2009-06-16 11:03:07 +1000225 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (size < sizeof(*data))
227 return -EINVAL;
228 data = (struct nfsctl_svc*) buf;
NeilBrown82e12fe2009-06-16 11:03:07 +1000229 err = nfsd_svc(data->svc_port, data->svc_nthreads);
230 if (err < 0)
231 return err;
232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Chuck Lever262a0982008-12-12 16:57:35 -0500235/**
236 * write_add - Add or modify client entry in auth unix cache
237 *
238 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
239 * Function remains to support old versions of nfs-utils.
240 *
241 * Input:
242 * buf: struct nfsctl_client
243 * cl_ident: '\0'-terminated C string
244 * containing domain name
245 * of client
246 * cl_naddr: no. of items in cl_addrlist
247 * cl_addrlist: array of client addresses
248 * cl_fhkeytype: ignored
249 * cl_fhkeylen: ignored
250 * cl_fhkey: ignored
251 * size: size in bytes of passed in nfsctl_client
252 * Output:
253 * On success: returns zero
254 * On error: return code is negative errno value
255 *
256 * Note: Only AF_INET client addresses are passed in, since
257 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static ssize_t write_add(struct file *file, char *buf, size_t size)
260{
261 struct nfsctl_client *data;
262 if (size < sizeof(*data))
263 return -EINVAL;
264 data = (struct nfsctl_client *)buf;
265 return exp_addclient(data);
266}
267
Chuck Lever262a0982008-12-12 16:57:35 -0500268/**
269 * write_del - Remove client from auth unix cache
270 *
271 * Deprecated. /proc/net/rpc/auth.unix.ip is preferred.
272 * Function remains to support old versions of nfs-utils.
273 *
274 * Input:
275 * buf: struct nfsctl_client
276 * cl_ident: '\0'-terminated C string
277 * containing domain name
278 * of client
279 * cl_naddr: ignored
280 * cl_addrlist: ignored
281 * cl_fhkeytype: ignored
282 * cl_fhkeylen: ignored
283 * cl_fhkey: ignored
284 * size: size in bytes of passed in nfsctl_client
285 * Output:
286 * On success: returns zero
287 * On error: return code is negative errno value
288 *
289 * Note: Only AF_INET client addresses are passed in, since
290 * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static ssize_t write_del(struct file *file, char *buf, size_t size)
293{
294 struct nfsctl_client *data;
295 if (size < sizeof(*data))
296 return -EINVAL;
297 data = (struct nfsctl_client *)buf;
298 return exp_delclient(data);
299}
300
Chuck Lever262a0982008-12-12 16:57:35 -0500301/**
302 * write_export - Export part or all of a local file system
303 *
304 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
305 * Function remains to support old versions of nfs-utils.
306 *
307 * Input:
308 * buf: struct nfsctl_export
309 * ex_client: '\0'-terminated C string
310 * containing domain name
311 * of client allowed to access
312 * this export
313 * ex_path: '\0'-terminated C string
314 * containing pathname of
315 * directory in local file system
316 * ex_dev: fsid to use for this export
317 * ex_ino: ignored
318 * ex_flags: export flags for this export
319 * ex_anon_uid: UID to use for anonymous
320 * requests
321 * ex_anon_gid: GID to use for anonymous
322 * requests
323 * size: size in bytes of passed in nfsctl_export
324 * Output:
325 * On success: returns zero
326 * On error: return code is negative errno value
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static ssize_t write_export(struct file *file, char *buf, size_t size)
329{
330 struct nfsctl_export *data;
331 if (size < sizeof(*data))
332 return -EINVAL;
333 data = (struct nfsctl_export*)buf;
334 return exp_export(data);
335}
336
Chuck Lever262a0982008-12-12 16:57:35 -0500337/**
338 * write_unexport - Unexport a previously exported file system
339 *
340 * Deprecated. /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
341 * Function remains to support old versions of nfs-utils.
342 *
343 * Input:
344 * buf: struct nfsctl_export
345 * ex_client: '\0'-terminated C string
346 * containing domain name
347 * of client no longer allowed
348 * to access this export
349 * ex_path: '\0'-terminated C string
350 * containing pathname of
351 * directory in local file system
352 * ex_dev: ignored
353 * ex_ino: ignored
354 * ex_flags: ignored
355 * ex_anon_uid: ignored
356 * ex_anon_gid: ignored
357 * size: size in bytes of passed in nfsctl_export
358 * Output:
359 * On success: returns zero
360 * On error: return code is negative errno value
361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static ssize_t write_unexport(struct file *file, char *buf, size_t size)
363{
364 struct nfsctl_export *data;
365
366 if (size < sizeof(*data))
367 return -EINVAL;
368 data = (struct nfsctl_export*)buf;
369 return exp_unexport(data);
370}
371
Chuck Lever262a0982008-12-12 16:57:35 -0500372/**
373 * write_getfs - Get a variable-length NFS file handle by path
374 *
375 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
376 * Function remains to support old versions of nfs-utils.
377 *
378 * Input:
379 * buf: struct nfsctl_fsparm
380 * gd_addr: socket address of client
381 * gd_path: '\0'-terminated C string
382 * containing pathname of
383 * directory in local file system
384 * gd_maxlen: maximum size of returned file
385 * handle
386 * size: size in bytes of passed in nfsctl_fsparm
387 * Output:
388 * On success: passed-in buffer filled with a knfsd_fh structure
389 * (a variable-length raw NFS file handle);
390 * return code is the size in bytes of the file handle
391 * On error: return code is negative errno value
392 *
393 * Note: Only AF_INET client addresses are passed in, since gd_addr
394 * is the same size as a struct sockaddr_in.
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static ssize_t write_getfs(struct file *file, char *buf, size_t size)
397{
398 struct nfsctl_fsparm *data;
399 struct sockaddr_in *sin;
400 struct auth_domain *clp;
401 int err = 0;
402 struct knfsd_fh *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100403 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (size < sizeof(*data))
406 return -EINVAL;
407 data = (struct nfsctl_fsparm*)buf;
408 err = -EPROTONOSUPPORT;
409 if (data->gd_addr.sa_family != AF_INET)
410 goto out;
411 sin = (struct sockaddr_in *)&data->gd_addr;
412 if (data->gd_maxlen > NFS3_FHSIZE)
413 data->gd_maxlen = NFS3_FHSIZE;
414
415 res = (struct knfsd_fh*)buf;
416
417 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100418
419 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
420
421 clp = auth_unix_lookup(&in6);
422 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 err = -EPERM;
424 else {
425 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
426 auth_domain_put(clp);
427 }
428 exp_readunlock();
429 if (err == 0)
Andrew Morton12127492007-07-17 04:04:34 -0700430 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 out:
432 return err;
433}
434
Chuck Lever262a0982008-12-12 16:57:35 -0500435/**
436 * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
437 *
438 * Deprecated. /proc/fs/nfsd/filehandle is preferred.
439 * Function remains to support old versions of nfs-utils.
440 *
441 * Input:
442 * buf: struct nfsctl_fdparm
443 * gd_addr: socket address of client
444 * gd_path: '\0'-terminated C string
445 * containing pathname of
446 * directory in local file system
447 * gd_version: fdparm structure version
448 * size: size in bytes of passed in nfsctl_fdparm
449 * Output:
450 * On success: passed-in buffer filled with nfsctl_res
451 * (a fixed-length raw NFS file handle);
452 * return code is the size in bytes of the file handle
453 * On error: return code is negative errno value
454 *
455 * Note: Only AF_INET client addresses are passed in, since gd_addr
456 * is the same size as a struct sockaddr_in.
457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static ssize_t write_getfd(struct file *file, char *buf, size_t size)
459{
460 struct nfsctl_fdparm *data;
461 struct sockaddr_in *sin;
462 struct auth_domain *clp;
463 int err = 0;
464 struct knfsd_fh fh;
465 char *res;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100466 struct in6_addr in6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (size < sizeof(*data))
469 return -EINVAL;
470 data = (struct nfsctl_fdparm*)buf;
471 err = -EPROTONOSUPPORT;
472 if (data->gd_addr.sa_family != AF_INET)
473 goto out;
474 err = -EINVAL;
475 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
476 goto out;
477
478 res = buf;
479 sin = (struct sockaddr_in *)&data->gd_addr;
480 exp_readlock();
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100481
482 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
483
484 clp = auth_unix_lookup(&in6);
485 if (!clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 err = -EPERM;
487 else {
488 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
489 auth_domain_put(clp);
490 }
491 exp_readunlock();
492
493 if (err == 0) {
494 memset(res,0, NFS_FHSIZE);
495 memcpy(res, &fh.fh_base, fh.fh_size);
496 err = NFS_FHSIZE;
497 }
498 out:
499 return err;
500}
NeilBrown1e140562010-09-22 12:55:07 +1000501#endif /* CONFIG_NFSD_DEPRECATED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Chuck Lever262a0982008-12-12 16:57:35 -0500503/**
504 * write_unlock_ip - Release all locks used by a client
505 *
506 * Experimental.
507 *
508 * Input:
509 * buf: '\n'-terminated C string containing a
Chuck Lever41160922009-08-09 15:09:40 -0400510 * presentation format IP address
Chuck Lever262a0982008-12-12 16:57:35 -0500511 * size: length of C string in @buf
512 * Output:
513 * On success: returns zero if all specified locks were released;
514 * returns one if one or more locks were not released
515 * On error: return code is negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500516 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500517static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500518{
Chuck Lever41160922009-08-09 15:09:40 -0400519 struct sockaddr_storage address;
520 struct sockaddr *sap = (struct sockaddr *)&address;
521 size_t salen = sizeof(address);
Chuck Lever367c8c72008-06-30 18:58:14 -0400522 char *fo_path;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500523
524 /* sanity check */
525 if (size == 0)
526 return -EINVAL;
527
528 if (buf[size-1] != '\n')
529 return -EINVAL;
530
531 fo_path = buf;
532 if (qword_get(&buf, fo_path, size) < 0)
533 return -EINVAL;
534
Chuck Lever41160922009-08-09 15:09:40 -0400535 if (rpc_pton(fo_path, size, sap, salen) == 0)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500536 return -EINVAL;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500537
Chuck Lever41160922009-08-09 15:09:40 -0400538 return nlmsvc_unlock_all_by_ip(sap);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500539}
540
Chuck Lever262a0982008-12-12 16:57:35 -0500541/**
542 * write_unlock_fs - Release all locks on a local file system
543 *
544 * Experimental.
545 *
546 * Input:
547 * buf: '\n'-terminated C string containing the
548 * absolute pathname of a local file system
549 * size: length of C string in @buf
550 * Output:
551 * On success: returns zero if all specified locks were released;
552 * returns one if one or more locks were not released
553 * On error: return code is negative errno value
554 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500555static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
Wendy Cheng17efa372008-01-17 11:10:12 -0500556{
Al Viroa63bb992008-08-02 01:03:36 -0400557 struct path path;
Wendy Cheng17efa372008-01-17 11:10:12 -0500558 char *fo_path;
559 int error;
560
561 /* sanity check */
562 if (size == 0)
563 return -EINVAL;
564
565 if (buf[size-1] != '\n')
566 return -EINVAL;
567
568 fo_path = buf;
569 if (qword_get(&buf, fo_path, size) < 0)
570 return -EINVAL;
571
Al Viroa63bb992008-08-02 01:03:36 -0400572 error = kern_path(fo_path, 0, &path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500573 if (error)
574 return error;
575
Chuck Lever262a0982008-12-12 16:57:35 -0500576 /*
577 * XXX: Needs better sanity checking. Otherwise we could end up
578 * releasing locks on the wrong file system.
579 *
580 * For example:
581 * 1. Does the path refer to a directory?
582 * 2. Is that directory a mount point, or
583 * 3. Is that directory the root of an exported file system?
584 */
Al Viroa63bb992008-08-02 01:03:36 -0400585 error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
Wendy Cheng17efa372008-01-17 11:10:12 -0500586
Al Viroa63bb992008-08-02 01:03:36 -0400587 path_put(&path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500588 return error;
589}
590
Chuck Lever262a0982008-12-12 16:57:35 -0500591/**
592 * write_filehandle - Get a variable-length NFS file handle by path
593 *
594 * On input, the buffer contains a '\n'-terminated C string comprised of
595 * three alphanumeric words separated by whitespace. The string may
596 * contain escape sequences.
597 *
598 * Input:
599 * buf:
600 * domain: client domain name
601 * path: export pathname
602 * maxsize: numeric maximum size of
603 * @buf
604 * size: length of C string in @buf
605 * Output:
606 * On success: passed-in buffer filled with '\n'-terminated C
607 * string containing a ASCII hex text version
608 * of the NFS file handle;
609 * return code is the size in bytes of the string
610 * On error: return code is negative errno value
611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 char *dname, *path;
Andrew Morton246d95b2007-08-09 00:53:50 -0700615 int uninitialized_var(maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 char *mesg = buf;
617 int len;
618 struct auth_domain *dom;
619 struct knfsd_fh fh;
620
J. Bruce Fields87d26ea2008-01-22 17:40:42 -0500621 if (size == 0)
622 return -EINVAL;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (buf[size-1] != '\n')
625 return -EINVAL;
626 buf[size-1] = 0;
627
628 dname = mesg;
629 len = qword_get(&mesg, dname, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500630 if (len <= 0)
631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 path = dname+len+1;
634 len = qword_get(&mesg, path, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500635 if (len <= 0)
636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 len = get_int(&mesg, &maxsize);
639 if (len)
640 return len;
641
642 if (maxsize < NFS_FHSIZE)
643 return -EINVAL;
644 if (maxsize > NFS3_FHSIZE)
645 maxsize = NFS3_FHSIZE;
646
647 if (qword_get(&mesg, mesg, size)>0)
648 return -EINVAL;
649
650 /* we have all the words, they are in buf.. */
651 dom = unix_domain_find(dname);
652 if (!dom)
653 return -ENOMEM;
654
655 len = exp_rootfh(dom, path, &fh, maxsize);
656 auth_domain_put(dom);
657 if (len)
658 return len;
659
Chuck Lever54224f02008-12-12 16:57:20 -0500660 mesg = buf;
661 len = SIMPLE_TRANSACTION_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
663 mesg[-1] = '\n';
664 return mesg - buf;
665}
666
Chuck Lever262a0982008-12-12 16:57:35 -0500667/**
668 * write_threads - Start NFSD, or report the current number of running threads
669 *
670 * Input:
671 * buf: ignored
672 * size: zero
673 * Output:
674 * On success: passed-in buffer filled with '\n'-terminated C
675 * string numeric value representing the number of
676 * running NFSD threads;
677 * return code is the size in bytes of the string
678 * On error: return code is zero
679 *
680 * OR
681 *
682 * Input:
683 * buf: C string containing an unsigned
684 * integer value representing the
685 * number of NFSD threads to start
686 * size: non-zero length of C string in @buf
687 * Output:
688 * On success: NFS service is started;
689 * passed-in buffer filled with '\n'-terminated C
690 * string numeric value representing the number of
691 * running NFSD threads;
692 * return code is the size in bytes of the string
693 * On error: return code is zero or a negative errno value
694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static ssize_t write_threads(struct file *file, char *buf, size_t size)
696{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 char *mesg = buf;
698 int rv;
699 if (size > 0) {
700 int newthreads;
701 rv = get_int(&mesg, &newthreads);
702 if (rv)
703 return rv;
Chuck Lever9e074852008-12-12 16:57:27 -0500704 if (newthreads < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EINVAL;
Chuck Lever9e074852008-12-12 16:57:27 -0500706 rv = nfsd_svc(NFS_PORT, newthreads);
NeilBrown82e12fe2009-06-16 11:03:07 +1000707 if (rv < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return rv;
NeilBrown82e12fe2009-06-16 11:03:07 +1000709 } else
710 rv = nfsd_nrthreads();
Chuck Levere06b6402009-04-23 19:33:25 -0400711
NeilBrown82e12fe2009-06-16 11:03:07 +1000712 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Chuck Lever262a0982008-12-12 16:57:35 -0500715/**
716 * write_pool_threads - Set or report the current number of threads per pool
717 *
718 * Input:
719 * buf: ignored
720 * size: zero
721 *
722 * OR
723 *
724 * Input:
725 * buf: C string containing whitespace-
726 * separated unsigned integer values
727 * representing the number of NFSD
728 * threads to start in each pool
729 * size: non-zero length of C string in @buf
730 * Output:
731 * On success: passed-in buffer filled with '\n'-terminated C
732 * string containing integer values representing the
733 * number of NFSD threads in each pool;
734 * return code is the size in bytes of the string
735 * On error: return code is zero or a negative errno value
736 */
Greg Bankseed29652006-10-02 02:18:02 -0700737static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
738{
739 /* if size > 0, look for an array of number of threads per node
740 * and apply them then write out number of threads per node as reply
741 */
742 char *mesg = buf;
743 int i;
744 int rv;
745 int len;
Neil Brownbedbdd82008-06-10 08:40:35 -0400746 int npools;
Greg Bankseed29652006-10-02 02:18:02 -0700747 int *nthreads;
748
Neil Brownbedbdd82008-06-10 08:40:35 -0400749 mutex_lock(&nfsd_mutex);
750 npools = nfsd_nrpools();
Greg Bankseed29652006-10-02 02:18:02 -0700751 if (npools == 0) {
752 /*
753 * NFS is shut down. The admin can start it by
754 * writing to the threads file but NOT the pool_threads
755 * file, sorry. Report zero threads.
756 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400757 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700758 strcpy(buf, "0\n");
759 return strlen(buf);
760 }
761
762 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
Neil Brownbedbdd82008-06-10 08:40:35 -0400763 rv = -ENOMEM;
Greg Bankseed29652006-10-02 02:18:02 -0700764 if (nthreads == NULL)
Neil Brownbedbdd82008-06-10 08:40:35 -0400765 goto out_free;
Greg Bankseed29652006-10-02 02:18:02 -0700766
767 if (size > 0) {
768 for (i = 0; i < npools; i++) {
769 rv = get_int(&mesg, &nthreads[i]);
770 if (rv == -ENOENT)
771 break; /* fewer numbers than pools */
772 if (rv)
773 goto out_free; /* syntax error */
774 rv = -EINVAL;
775 if (nthreads[i] < 0)
776 goto out_free;
777 }
778 rv = nfsd_set_nrthreads(i, nthreads);
779 if (rv)
780 goto out_free;
781 }
782
783 rv = nfsd_get_nrthreads(npools, nthreads);
784 if (rv)
785 goto out_free;
786
787 mesg = buf;
788 size = SIMPLE_TRANSACTION_LIMIT;
789 for (i = 0; i < npools && size > 0; i++) {
790 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
791 len = strlen(mesg);
792 size -= len;
793 mesg += len;
794 }
J. Bruce Fields413d63d2009-07-28 11:37:25 -0400795 rv = mesg - buf;
Greg Bankseed29652006-10-02 02:18:02 -0700796out_free:
797 kfree(nthreads);
Neil Brownbedbdd82008-06-10 08:40:35 -0400798 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700799 return rv;
800}
801
Jeff Layton3dd98a32008-06-10 08:40:36 -0400802static ssize_t __write_versions(struct file *file, char *buf, size_t size)
NeilBrown70c3b762005-11-07 01:00:25 -0800803{
NeilBrown70c3b762005-11-07 01:00:25 -0800804 char *mesg = buf;
Benny Halevy8daf2202009-04-03 08:28:59 +0300805 char *vers, *minorp, sign;
Chuck Lever261758b2009-04-23 19:33:18 -0400806 int len, num, remaining;
Benny Halevy8daf2202009-04-03 08:28:59 +0300807 unsigned minor;
NeilBrown70c3b762005-11-07 01:00:25 -0800808 ssize_t tlen = 0;
809 char *sep;
810
811 if (size>0) {
812 if (nfsd_serv)
NeilBrown6658d3a2006-10-02 02:17:46 -0700813 /* Cannot change versions without updating
814 * nfsd_serv->sv_xdrsize, and reallocing
815 * rq_argp and rq_resp
816 */
NeilBrown70c3b762005-11-07 01:00:25 -0800817 return -EBUSY;
818 if (buf[size-1] != '\n')
819 return -EINVAL;
820 buf[size-1] = 0;
821
822 vers = mesg;
823 len = qword_get(&mesg, vers, size);
824 if (len <= 0) return -EINVAL;
825 do {
826 sign = *vers;
827 if (sign == '+' || sign == '-')
Benny Halevy8daf2202009-04-03 08:28:59 +0300828 num = simple_strtol((vers+1), &minorp, 0);
NeilBrown70c3b762005-11-07 01:00:25 -0800829 else
Benny Halevy8daf2202009-04-03 08:28:59 +0300830 num = simple_strtol(vers, &minorp, 0);
831 if (*minorp == '.') {
832 if (num < 4)
833 return -EINVAL;
834 minor = simple_strtoul(minorp+1, NULL, 0);
835 if (minor == 0)
836 return -EINVAL;
837 if (nfsd_minorversion(minor, sign == '-' ?
838 NFSD_CLEAR : NFSD_SET) < 0)
839 return -EINVAL;
840 goto next;
841 }
NeilBrown70c3b762005-11-07 01:00:25 -0800842 switch(num) {
843 case 2:
844 case 3:
845 case 4:
NeilBrown6658d3a2006-10-02 02:17:46 -0700846 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
NeilBrown70c3b762005-11-07 01:00:25 -0800847 break;
848 default:
849 return -EINVAL;
850 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300851 next:
NeilBrown70c3b762005-11-07 01:00:25 -0800852 vers += len + 1;
NeilBrown70c3b762005-11-07 01:00:25 -0800853 } while ((len = qword_get(&mesg, vers, size)) > 0);
854 /* If all get turned off, turn them back on, as
855 * having no versions is BAD
856 */
NeilBrown6658d3a2006-10-02 02:17:46 -0700857 nfsd_reset_versions();
NeilBrown70c3b762005-11-07 01:00:25 -0800858 }
Chuck Lever261758b2009-04-23 19:33:18 -0400859
NeilBrown70c3b762005-11-07 01:00:25 -0800860 /* Now write current state into reply buffer */
861 len = 0;
862 sep = "";
Chuck Lever261758b2009-04-23 19:33:18 -0400863 remaining = SIMPLE_TRANSACTION_LIMIT;
NeilBrown70c3b762005-11-07 01:00:25 -0800864 for (num=2 ; num <= 4 ; num++)
NeilBrown6658d3a2006-10-02 02:17:46 -0700865 if (nfsd_vers(num, NFSD_AVAIL)) {
Chuck Lever261758b2009-04-23 19:33:18 -0400866 len = snprintf(buf, remaining, "%s%c%d", sep,
NeilBrown6658d3a2006-10-02 02:17:46 -0700867 nfsd_vers(num, NFSD_TEST)?'+':'-',
NeilBrown70c3b762005-11-07 01:00:25 -0800868 num);
869 sep = " ";
Chuck Lever261758b2009-04-23 19:33:18 -0400870
871 if (len > remaining)
872 break;
873 remaining -= len;
874 buf += len;
875 tlen += len;
NeilBrown70c3b762005-11-07 01:00:25 -0800876 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300877 if (nfsd_vers(4, NFSD_AVAIL))
Chuck Lever261758b2009-04-23 19:33:18 -0400878 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
879 minor++) {
880 len = snprintf(buf, remaining, " %c4.%u",
Benny Halevy8daf2202009-04-03 08:28:59 +0300881 (nfsd_vers(4, NFSD_TEST) &&
882 nfsd_minorversion(minor, NFSD_TEST)) ?
883 '+' : '-',
884 minor);
Chuck Lever261758b2009-04-23 19:33:18 -0400885
886 if (len > remaining)
887 break;
888 remaining -= len;
889 buf += len;
890 tlen += len;
891 }
892
893 len = snprintf(buf, remaining, "\n");
894 if (len > remaining)
895 return -EINVAL;
896 return tlen + len;
NeilBrown70c3b762005-11-07 01:00:25 -0800897}
898
Chuck Lever262a0982008-12-12 16:57:35 -0500899/**
900 * write_versions - Set or report the available NFS protocol versions
901 *
902 * Input:
903 * buf: ignored
904 * size: zero
905 * Output:
906 * On success: passed-in buffer filled with '\n'-terminated C
907 * string containing positive or negative integer
908 * values representing the current status of each
909 * protocol version;
910 * return code is the size in bytes of the string
911 * On error: return code is zero or a negative errno value
912 *
913 * OR
914 *
915 * Input:
916 * buf: C string containing whitespace-
917 * separated positive or negative
918 * integer values representing NFS
919 * protocol versions to enable ("+n")
920 * or disable ("-n")
921 * size: non-zero length of C string in @buf
922 * Output:
923 * On success: status of zero or more protocol versions has
924 * been updated; passed-in buffer filled with
925 * '\n'-terminated C string containing positive
926 * or negative integer values representing the
927 * current status of each protocol version;
928 * return code is the size in bytes of the string
929 * On error: return code is zero or a negative errno value
930 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400931static ssize_t write_versions(struct file *file, char *buf, size_t size)
932{
933 ssize_t rv;
934
935 mutex_lock(&nfsd_mutex);
936 rv = __write_versions(file, buf, size);
937 mutex_unlock(&nfsd_mutex);
938 return rv;
939}
940
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400941/*
Chuck Lever0a5372d2009-04-23 19:32:10 -0400942 * Zero-length write. Return a list of NFSD's current listener
943 * transports.
944 */
945static ssize_t __write_ports_names(char *buf)
946{
947 if (nfsd_serv == NULL)
948 return 0;
Chuck Lever335c54b2009-04-23 19:32:25 -0400949 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Lever0a5372d2009-04-23 19:32:10 -0400950}
951
952/*
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400953 * A single 'fd' number was written, in which case it must be for
954 * a socket of a supported family/protocol, and we use it as an
955 * nfsd listener.
956 */
957static ssize_t __write_ports_addfd(char *buf)
958{
959 char *mesg = buf;
960 int fd, err;
961
962 err = get_int(&mesg, &fd);
963 if (err != 0 || fd < 0)
964 return -EINVAL;
965
966 err = nfsd_create_serv();
967 if (err != 0)
968 return err;
969
Chuck Leverbfba9ab2009-04-23 19:32:33 -0400970 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400971 if (err < 0) {
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400972 svc_destroy(nfsd_serv);
973 return err;
974 }
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400975
Chuck Leverea068ba2009-04-23 19:32:18 -0400976 /* Decrease the count, but don't shut down the service */
977 nfsd_serv->sv_nrthreads--;
978 return err;
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400979}
980
981/*
Chuck Lever82d56592009-04-23 19:31:48 -0400982 * A '-' followed by the 'name' of a socket means we close the socket.
983 */
984static ssize_t __write_ports_delfd(char *buf)
985{
986 char *toclose;
987 int len = 0;
988
989 toclose = kstrdup(buf + 1, GFP_KERNEL);
990 if (toclose == NULL)
991 return -ENOMEM;
992
993 if (nfsd_serv != NULL)
Chuck Lever8435d342009-04-23 19:32:40 -0400994 len = svc_sock_names(nfsd_serv, buf,
995 SIMPLE_TRANSACTION_LIMIT, toclose);
Chuck Lever82d56592009-04-23 19:31:48 -0400996 kfree(toclose);
997 return len;
998}
999
1000/*
Chuck Lever4eb68c22009-04-23 19:31:40 -04001001 * A transport listener is added by writing it's transport name and
1002 * a port number.
1003 */
1004static ssize_t __write_ports_addxprt(char *buf)
1005{
1006 char transport[16];
Chuck Lever37498292010-01-26 14:04:22 -05001007 struct svc_xprt *xprt;
Chuck Lever4eb68c22009-04-23 19:31:40 -04001008 int port, err;
1009
1010 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
1011 return -EINVAL;
1012
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001013 if (port < 1 || port > USHRT_MAX)
Chuck Lever4eb68c22009-04-23 19:31:40 -04001014 return -EINVAL;
1015
1016 err = nfsd_create_serv();
1017 if (err != 0)
1018 return err;
1019
1020 err = svc_create_xprt(nfsd_serv, transport,
1021 PF_INET, port, SVC_SOCK_ANONYMOUS);
Chuck Lever68717902010-01-26 14:04:13 -05001022 if (err < 0)
Chuck Lever37498292010-01-26 14:04:22 -05001023 goto out_err;
1024
1025 err = svc_create_xprt(nfsd_serv, transport,
1026 PF_INET6, port, SVC_SOCK_ANONYMOUS);
1027 if (err < 0 && err != -EAFNOSUPPORT)
1028 goto out_close;
Jeff Layton0cd14a02010-07-19 16:50:06 -04001029
1030 /* Decrease the count, but don't shut down the service */
1031 nfsd_serv->sv_nrthreads--;
Chuck Lever4eb68c22009-04-23 19:31:40 -04001032 return 0;
Chuck Lever37498292010-01-26 14:04:22 -05001033out_close:
1034 xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
1035 if (xprt != NULL) {
1036 svc_close_xprt(xprt);
1037 svc_xprt_put(xprt);
1038 }
1039out_err:
Jeff Layton0cd14a02010-07-19 16:50:06 -04001040 svc_destroy(nfsd_serv);
Chuck Lever37498292010-01-26 14:04:22 -05001041 return err;
Chuck Lever4eb68c22009-04-23 19:31:40 -04001042}
1043
1044/*
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001045 * A transport listener is removed by writing a "-", it's transport
1046 * name, and it's port number.
1047 */
1048static ssize_t __write_ports_delxprt(char *buf)
1049{
1050 struct svc_xprt *xprt;
1051 char transport[16];
1052 int port;
1053
1054 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1055 return -EINVAL;
1056
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001057 if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001058 return -EINVAL;
1059
1060 xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1061 if (xprt == NULL)
1062 return -ENOTCONN;
1063
1064 svc_close_xprt(xprt);
1065 svc_xprt_put(xprt);
1066 return 0;
1067}
1068
Neil Brownbedbdd82008-06-10 08:40:35 -04001069static ssize_t __write_ports(struct file *file, char *buf, size_t size)
NeilBrown80212d52006-10-02 02:17:47 -07001070{
Chuck Lever0a5372d2009-04-23 19:32:10 -04001071 if (size == 0)
1072 return __write_ports_names(buf);
Chuck Lever0b7c2f62009-04-23 19:31:55 -04001073
1074 if (isdigit(buf[0]))
1075 return __write_ports_addfd(buf);
Chuck Lever82d56592009-04-23 19:31:48 -04001076
1077 if (buf[0] == '-' && isdigit(buf[1]))
1078 return __write_ports_delfd(buf);
Chuck Lever4eb68c22009-04-23 19:31:40 -04001079
1080 if (isalpha(buf[0]))
1081 return __write_ports_addxprt(buf);
Chuck Lever4cd5dc72009-04-23 19:31:32 -04001082
1083 if (buf[0] == '-' && isalpha(buf[1]))
1084 return __write_ports_delxprt(buf);
1085
NeilBrownb41b66d2006-10-02 02:17:48 -07001086 return -EINVAL;
NeilBrown80212d52006-10-02 02:17:47 -07001087}
1088
Chuck Lever262a0982008-12-12 16:57:35 -05001089/**
1090 * write_ports - Pass a socket file descriptor or transport name to listen on
1091 *
1092 * Input:
1093 * buf: ignored
1094 * size: zero
1095 * Output:
1096 * On success: passed-in buffer filled with a '\n'-terminated C
1097 * string containing a whitespace-separated list of
1098 * named NFSD listeners;
1099 * return code is the size in bytes of the string
1100 * On error: return code is zero or a negative errno value
1101 *
1102 * OR
1103 *
1104 * Input:
1105 * buf: C string containing an unsigned
1106 * integer value representing a bound
1107 * but unconnected socket that is to be
Chuck Leverc71206a2009-04-23 19:32:03 -04001108 * used as an NFSD listener; listen(3)
1109 * must be called for a SOCK_STREAM
1110 * socket, otherwise it is ignored
Chuck Lever262a0982008-12-12 16:57:35 -05001111 * size: non-zero length of C string in @buf
1112 * Output:
1113 * On success: NFS service is started;
1114 * passed-in buffer filled with a '\n'-terminated C
1115 * string containing a unique alphanumeric name of
1116 * the listener;
1117 * return code is the size in bytes of the string
1118 * On error: return code is a negative errno value
1119 *
1120 * OR
1121 *
1122 * Input:
1123 * buf: C string containing a "-" followed
1124 * by an integer value representing a
1125 * previously passed in socket file
1126 * descriptor
1127 * size: non-zero length of C string in @buf
1128 * Output:
1129 * On success: NFS service no longer listens on that socket;
1130 * passed-in buffer filled with a '\n'-terminated C
1131 * string containing a unique name of the listener;
1132 * return code is the size in bytes of the string
1133 * On error: return code is a negative errno value
1134 *
1135 * OR
1136 *
1137 * Input:
1138 * buf: C string containing a transport
1139 * name and an unsigned integer value
1140 * representing the port to listen on,
1141 * separated by whitespace
1142 * size: non-zero length of C string in @buf
1143 * Output:
1144 * On success: returns zero; NFS service is started
1145 * On error: return code is a negative errno value
1146 *
1147 * OR
1148 *
1149 * Input:
1150 * buf: C string containing a "-" followed
1151 * by a transport name and an unsigned
1152 * integer value representing the port
1153 * to listen on, separated by whitespace
1154 * size: non-zero length of C string in @buf
1155 * Output:
1156 * On success: returns zero; NFS service no longer listens
1157 * on that transport
1158 * On error: return code is a negative errno value
1159 */
Neil Brownbedbdd82008-06-10 08:40:35 -04001160static ssize_t write_ports(struct file *file, char *buf, size_t size)
1161{
1162 ssize_t rv;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001163
Neil Brownbedbdd82008-06-10 08:40:35 -04001164 mutex_lock(&nfsd_mutex);
1165 rv = __write_ports(file, buf, size);
1166 mutex_unlock(&nfsd_mutex);
1167 return rv;
1168}
1169
1170
NeilBrown596bbe52006-10-04 02:15:48 -07001171int nfsd_max_blksize;
1172
Chuck Lever262a0982008-12-12 16:57:35 -05001173/**
1174 * write_maxblksize - Set or report the current NFS blksize
1175 *
1176 * Input:
1177 * buf: ignored
1178 * size: zero
1179 *
1180 * OR
1181 *
1182 * Input:
1183 * buf: C string containing an unsigned
1184 * integer value representing the new
1185 * NFS blksize
1186 * size: non-zero length of C string in @buf
1187 * Output:
1188 * On success: passed-in buffer filled with '\n'-terminated C string
1189 * containing numeric value of the current NFS blksize
1190 * setting;
1191 * return code is the size in bytes of the string
1192 * On error: return code is zero or a negative errno value
1193 */
NeilBrown596bbe52006-10-04 02:15:48 -07001194static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1195{
1196 char *mesg = buf;
1197 if (size > 0) {
1198 int bsize;
1199 int rv = get_int(&mesg, &bsize);
1200 if (rv)
1201 return rv;
1202 /* force bsize into allowed range and
1203 * required alignment.
1204 */
1205 if (bsize < 1024)
1206 bsize = 1024;
1207 if (bsize > NFSSVC_MAXBLKSIZE)
1208 bsize = NFSSVC_MAXBLKSIZE;
1209 bsize &= ~(1024-1);
Neil Brownbedbdd82008-06-10 08:40:35 -04001210 mutex_lock(&nfsd_mutex);
J. Bruce Fields7fa53cc2010-08-06 18:00:33 -04001211 if (nfsd_serv) {
Neil Brownbedbdd82008-06-10 08:40:35 -04001212 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001213 return -EBUSY;
1214 }
1215 nfsd_max_blksize = bsize;
Neil Brownbedbdd82008-06-10 08:40:35 -04001216 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -07001217 }
Chuck Levere06b6402009-04-23 19:33:25 -04001218
1219 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1220 nfsd_max_blksize);
NeilBrown596bbe52006-10-04 02:15:48 -07001221}
1222
NeilBrown70c3b762005-11-07 01:00:25 -08001223#ifdef CONFIG_NFSD_V4
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001224static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 char *mesg = buf;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001227 int rv, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (size > 0) {
Jeff Layton3dd98a32008-06-10 08:40:36 -04001230 if (nfsd_serv)
1231 return -EBUSY;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001232 rv = get_int(&mesg, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (rv)
1234 return rv;
J. Bruce Fieldse7b184f2010-03-02 11:18:40 -05001235 /*
1236 * Some sanity checking. We don't have a reason for
1237 * these particular numbers, but problems with the
1238 * extremes are:
1239 * - Too short: the briefest network outage may
1240 * cause clients to lose all their locks. Also,
1241 * the frequent polling may be wasteful.
1242 * - Too long: do you really want reboot recovery
1243 * to take more than an hour? Or to make other
1244 * clients wait an hour before being able to
1245 * revoke a dead client's locks?
1246 */
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001247 if (i < 10 || i > 3600)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EINVAL;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001249 *time = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Chuck Levere06b6402009-04-23 19:33:25 -04001251
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001252 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
1253}
1254
1255static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1256{
1257 ssize_t rv;
1258
1259 mutex_lock(&nfsd_mutex);
1260 rv = __nfsd4_write_time(file, buf, size, time);
1261 mutex_unlock(&nfsd_mutex);
1262 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Chuck Lever262a0982008-12-12 16:57:35 -05001265/**
1266 * write_leasetime - Set or report the current NFSv4 lease time
1267 *
1268 * Input:
1269 * buf: ignored
1270 * size: zero
1271 *
1272 * OR
1273 *
1274 * Input:
1275 * buf: C string containing an unsigned
1276 * integer value representing the new
1277 * NFSv4 lease expiry time
1278 * size: non-zero length of C string in @buf
1279 * Output:
1280 * On success: passed-in buffer filled with '\n'-terminated C
1281 * string containing unsigned integer value of the
1282 * current lease expiry time;
1283 * return code is the size in bytes of the string
1284 * On error: return code is zero or a negative errno value
1285 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001286static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1287{
J. Bruce Fieldsf0135742010-03-01 19:32:36 -05001288 return nfsd4_write_time(file, buf, size, &nfsd4_lease);
Jeff Layton3dd98a32008-06-10 08:40:36 -04001289}
1290
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -05001291/**
1292 * write_gracetime - Set or report current NFSv4 grace period time
1293 *
1294 * As above, but sets the time of the NFSv4 grace period.
1295 *
1296 * Note this should never be set to less than the *previous*
1297 * lease-period time, but we don't try to enforce this. (In the common
1298 * case (a new boot), we don't know what the previous lease time was
1299 * anyway.)
1300 */
1301static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1302{
1303 return nfsd4_write_time(file, buf, size, &nfsd4_grace);
1304}
1305
Jeff Layton3dd98a32008-06-10 08:40:36 -04001306extern char *nfs4_recoverydir(void);
1307
1308static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
NeilBrown0964a3d2005-06-23 22:04:32 -07001309{
1310 char *mesg = buf;
1311 char *recdir;
1312 int len, status;
1313
Jeff Layton3dd98a32008-06-10 08:40:36 -04001314 if (size > 0) {
1315 if (nfsd_serv)
1316 return -EBUSY;
1317 if (size > PATH_MAX || buf[size-1] != '\n')
1318 return -EINVAL;
1319 buf[size-1] = 0;
NeilBrown0964a3d2005-06-23 22:04:32 -07001320
Jeff Layton3dd98a32008-06-10 08:40:36 -04001321 recdir = mesg;
1322 len = qword_get(&mesg, recdir, size);
1323 if (len <= 0)
1324 return -EINVAL;
NeilBrown0964a3d2005-06-23 22:04:32 -07001325
Jeff Layton3dd98a32008-06-10 08:40:36 -04001326 status = nfs4_reset_recoverydir(recdir);
Andi Kleen69049962010-07-20 15:24:27 -07001327 if (status)
1328 return status;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001329 }
Chuck Lever3d72ab82009-04-23 19:33:10 -04001330
1331 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1332 nfs4_recoverydir());
NeilBrown0964a3d2005-06-23 22:04:32 -07001333}
Jeff Layton3dd98a32008-06-10 08:40:36 -04001334
Chuck Lever262a0982008-12-12 16:57:35 -05001335/**
1336 * write_recoverydir - Set or report the pathname of the recovery directory
1337 *
1338 * Input:
1339 * buf: ignored
1340 * size: zero
1341 *
1342 * OR
1343 *
1344 * Input:
1345 * buf: C string containing the pathname
1346 * of the directory on a local file
1347 * system containing permanent NFSv4
1348 * recovery data
1349 * size: non-zero length of C string in @buf
1350 * Output:
1351 * On success: passed-in buffer filled with '\n'-terminated C string
1352 * containing the current recovery pathname setting;
1353 * return code is the size in bytes of the string
1354 * On error: return code is zero or a negative errno value
1355 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001356static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1357{
1358 ssize_t rv;
1359
1360 mutex_lock(&nfsd_mutex);
1361 rv = __write_recoverydir(file, buf, size);
1362 mutex_unlock(&nfsd_mutex);
1363 return rv;
1364}
1365
NeilBrown70c3b762005-11-07 01:00:25 -08001366#endif
NeilBrown0964a3d2005-06-23 22:04:32 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/*----------------------------------------------------------------------------*/
1369/*
1370 * populating the filesystem.
1371 */
1372
1373static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1374{
1375 static struct tree_descr nfsd_files[] = {
NeilBrown1e140562010-09-22 12:55:07 +10001376#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1378 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1379 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1380 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1381 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1382 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1383 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown1e140562010-09-22 12:55:07 +10001384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
J. Bruce Fieldse8e87532009-12-14 12:53:32 -05001386 [NFSD_Export_features] = {"export_features",
1387 &export_features_operations, S_IRUGO},
Wendy Cheng4373ea82008-01-17 11:10:12 -05001388 [NFSD_FO_UnlockIP] = {"unlock_ip",
1389 &transaction_ops, S_IWUSR|S_IRUSR},
Wendy Cheng17efa372008-01-17 11:10:12 -05001390 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1391 &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1393 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Bankseed29652006-10-02 02:18:02 -07001394 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Banks03cf6c92009-01-13 21:26:36 +11001395 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
NeilBrown70c3b762005-11-07 01:00:25 -08001396 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown80212d52006-10-02 02:17:47 -07001397 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
NeilBrown596bbe52006-10-04 02:15:48 -07001398 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399#ifdef CONFIG_NFSD_V4
1400 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -05001401 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown0964a3d2005-06-23 22:04:32 -07001402 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif
1404 /* last one */ {""}
1405 };
1406 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1407}
1408
David Howells454e2392006-06-23 02:02:57 -07001409static int nfsd_get_sb(struct file_system_type *fs_type,
1410 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
David Howells454e2392006-06-23 02:02:57 -07001412 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static struct file_system_type nfsd_fs_type = {
1416 .owner = THIS_MODULE,
1417 .name = "nfsd",
1418 .get_sb = nfsd_get_sb,
1419 .kill_sb = kill_litter_super,
1420};
1421
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001422#ifdef CONFIG_PROC_FS
1423static int create_proc_exports_entry(void)
1424{
1425 struct proc_dir_entry *entry;
1426
1427 entry = proc_mkdir("fs/nfs", NULL);
1428 if (!entry)
1429 return -ENOMEM;
Denis V. Lunev9ef2db22008-04-29 01:02:04 -07001430 entry = proc_create("exports", 0, entry, &exports_operations);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001431 if (!entry)
1432 return -ENOMEM;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001433 return 0;
1434}
1435#else /* CONFIG_PROC_FS */
1436static int create_proc_exports_entry(void)
1437{
1438 return 0;
1439}
1440#endif
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442static int __init init_nfsd(void)
1443{
1444 int retval;
1445 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1446
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001447 retval = nfs4_state_init(); /* nfs4 locking state */
1448 if (retval)
1449 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 nfsd_stat_init(); /* Statistics */
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001451 retval = nfsd_reply_cache_init();
1452 if (retval)
1453 goto out_free_stat;
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001454 retval = nfsd_export_init();
1455 if (retval)
1456 goto out_free_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 nfsd_lockd_init(); /* lockd->nfsd callbacks */
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001458 retval = nfsd_idmap_init();
1459 if (retval)
1460 goto out_free_lockd;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001461 retval = create_proc_exports_entry();
1462 if (retval)
1463 goto out_free_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 retval = register_filesystem(&nfsd_fs_type);
J. Bruce Fields26808d32007-11-09 13:44:06 -05001465 if (retval)
1466 goto out_free_all;
1467 return 0;
1468out_free_all:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001469 remove_proc_entry("fs/nfs/exports", NULL);
1470 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001471out_free_idmap:
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001472 nfsd_idmap_shutdown();
1473out_free_lockd:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001474 nfsd_lockd_shutdown();
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001475 nfsd_export_shutdown();
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001476out_free_cache:
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001477 nfsd_reply_cache_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001478out_free_stat:
1479 nfsd_stat_shutdown();
J. Bruce Fields26808d32007-11-09 13:44:06 -05001480 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return retval;
1482}
1483
1484static void __exit exit_nfsd(void)
1485{
1486 nfsd_export_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001487 nfsd_reply_cache_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 remove_proc_entry("fs/nfs/exports", NULL);
1489 remove_proc_entry("fs/nfs", NULL);
1490 nfsd_stat_shutdown();
1491 nfsd_lockd_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 nfsd_idmap_shutdown();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001493 nfsd4_free_slabs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unregister_filesystem(&nfsd_fs_type);
1495}
1496
1497MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1498MODULE_LICENSE("GPL");
1499module_init(init_nfsd)
1500module_exit(exit_nfsd)