blob: 141197e0470371750e59c539221e8b12ef138b5c [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
NeilBrown80212d52006-10-02 02:17:47 -070011#include <linux/sunrpc/svcsock.h>
Wendy Cheng4373ea82008-01-17 11:10:12 -050012#include <linux/lockd/lockd.h>
Chuck Lever41160922009-08-09 15:09:40 -040013#include <linux/sunrpc/clnt.h>
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050014#include <linux/sunrpc/gss_api.h>
J. Bruce Fieldsb084f592011-05-31 12:24:58 -040015#include <linux/sunrpc/gss_krb5_enctypes.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050018#include "idmap.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020019#include "nfsd.h"
20#include "cache.h"
Bryan Schumaker65178db2011-11-01 13:35:21 -040021#include "fault_inject.h"
Jeff Layton7ea34ac2012-03-21 09:52:05 -040022#include "netns.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050025 * We have a single directory with several nodes in it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27enum {
28 NFSD_Root = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 NFSD_List,
J. Bruce Fieldse8e87532009-12-14 12:53:32 -050030 NFSD_Export_features,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 NFSD_Fh,
Wendy Cheng4373ea82008-01-17 11:10:12 -050032 NFSD_FO_UnlockIP,
Wendy Cheng17efa372008-01-17 11:10:12 -050033 NFSD_FO_UnlockFS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 NFSD_Threads,
Greg Bankseed29652006-10-02 02:18:02 -070035 NFSD_Pool_Threads,
Greg Banks03cf6c92009-01-13 21:26:36 +110036 NFSD_Pool_Stats,
NeilBrown70c3b762005-11-07 01:00:25 -080037 NFSD_Versions,
NeilBrown80212d52006-10-02 02:17:47 -070038 NFSD_Ports,
NeilBrown596bbe52006-10-04 02:15:48 -070039 NFSD_MaxBlkSize,
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050040 NFSD_SupportedEnctypes,
NeilBrown70c3b762005-11-07 01:00:25 -080041 /*
42 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
43 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
44 */
45#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 NFSD_Leasetime,
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050047 NFSD_Gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070048 NFSD_RecoveryDir,
NeilBrown70c3b762005-11-07 01:00:25 -080049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52/*
53 * write() for these nodes.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
Chuck Leverb046ccd2008-12-12 16:57:13 -050056static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
57static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static ssize_t write_threads(struct file *file, char *buf, size_t size);
Greg Bankseed29652006-10-02 02:18:02 -070059static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080060static ssize_t write_versions(struct file *file, char *buf, size_t size);
NeilBrown80212d52006-10-02 02:17:47 -070061static ssize_t write_ports(struct file *file, char *buf, size_t size);
NeilBrown596bbe52006-10-04 02:15:48 -070062static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080063#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050065static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
NeilBrown0964a3d2005-06-23 22:04:32 -070066static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static ssize_t (*write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 [NFSD_Fh] = write_filehandle,
Chuck Leverb046ccd2008-12-12 16:57:13 -050071 [NFSD_FO_UnlockIP] = write_unlock_ip,
72 [NFSD_FO_UnlockFS] = write_unlock_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 [NFSD_Threads] = write_threads,
Greg Bankseed29652006-10-02 02:18:02 -070074 [NFSD_Pool_Threads] = write_pool_threads,
NeilBrown70c3b762005-11-07 01:00:25 -080075 [NFSD_Versions] = write_versions,
NeilBrown80212d52006-10-02 02:17:47 -070076 [NFSD_Ports] = write_ports,
NeilBrown596bbe52006-10-04 02:15:48 -070077 [NFSD_MaxBlkSize] = write_maxblksize,
NeilBrown70c3b762005-11-07 01:00:25 -080078#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 [NFSD_Leasetime] = write_leasetime,
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050080 [NFSD_Gracetime] = write_gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070081 [NFSD_RecoveryDir] = write_recoverydir,
NeilBrown70c3b762005-11-07 01:00:25 -080082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
86{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -080087 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char *data;
89 ssize_t rv;
90
Tobias Klausere8c96f82006-03-24 03:15:34 -080091 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EINVAL;
93
94 data = simple_transaction_get(file, buf, size);
95 if (IS_ERR(data))
96 return PTR_ERR(data);
97
98 rv = write_op[ino](file, data, size);
NeilBrown8971a102007-02-14 00:33:11 -080099 if (rv >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 simple_transaction_set(file, rv);
101 rv = size;
102 }
103 return rv;
104}
105
NeilBrown73900222005-11-07 01:00:24 -0800106static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
107{
108 if (! file->private_data) {
109 /* An attempt to read a transaction file without writing
110 * causes a 0-byte write so that the file can return
111 * state information
112 */
113 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
114 if (rv < 0)
115 return rv;
116 }
117 return simple_transaction_read(file, buf, size, pos);
118}
119
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800120static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .write = nfsctl_transaction_write,
NeilBrown73900222005-11-07 01:00:24 -0800122 .read = nfsctl_transaction_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .release = simple_transaction_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200124 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int exports_open(struct inode *inode, struct file *file)
128{
129 return seq_open(file, &nfs_exports_op);
130}
131
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800132static const struct file_operations exports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .open = exports_open,
134 .read = seq_read,
135 .llseek = seq_lseek,
136 .release = seq_release,
Denis V. Lunev9ef2db22008-04-29 01:02:04 -0700137 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
J. Bruce Fieldse8e87532009-12-14 12:53:32 -0500140static int export_features_show(struct seq_file *m, void *v)
141{
142 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
143 return 0;
144}
145
146static int export_features_open(struct inode *inode, struct file *file)
147{
148 return single_open(file, export_features_show, NULL);
149}
150
151static struct file_operations export_features_operations = {
152 .open = export_features_open,
153 .read = seq_read,
154 .llseek = seq_lseek,
155 .release = single_release,
156};
157
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400158#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500159static int supported_enctypes_show(struct seq_file *m, void *v)
160{
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400161 seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500162 return 0;
163}
164
165static int supported_enctypes_open(struct inode *inode, struct file *file)
166{
167 return single_open(file, supported_enctypes_show, NULL);
168}
169
170static struct file_operations supported_enctypes_ops = {
171 .open = supported_enctypes_open,
172 .read = seq_read,
173 .llseek = seq_lseek,
174 .release = single_release,
175};
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400176#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500177
Greg Banks03cf6c92009-01-13 21:26:36 +1100178extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900179extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
Greg Banks03cf6c92009-01-13 21:26:36 +1100180
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700181static const struct file_operations pool_stats_operations = {
Greg Banks03cf6c92009-01-13 21:26:36 +1100182 .open = nfsd_pool_stats_open,
183 .read = seq_read,
184 .llseek = seq_lseek,
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900185 .release = nfsd_pool_stats_release,
Greg Banks03cf6c92009-01-13 21:26:36 +1100186 .owner = THIS_MODULE,
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*----------------------------------------------------------------------------*/
190/*
191 * payload - write methods
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Chuck Lever262a0982008-12-12 16:57:35 -0500195/**
196 * write_unlock_ip - Release all locks used by a client
197 *
198 * Experimental.
199 *
200 * Input:
201 * buf: '\n'-terminated C string containing a
Chuck Lever41160922009-08-09 15:09:40 -0400202 * presentation format IP address
Chuck Lever262a0982008-12-12 16:57:35 -0500203 * size: length of C string in @buf
204 * Output:
205 * On success: returns zero if all specified locks were released;
206 * returns one if one or more locks were not released
207 * On error: return code is negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500208 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500209static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500210{
Chuck Lever41160922009-08-09 15:09:40 -0400211 struct sockaddr_storage address;
212 struct sockaddr *sap = (struct sockaddr *)&address;
213 size_t salen = sizeof(address);
Chuck Lever367c8c72008-06-30 18:58:14 -0400214 char *fo_path;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500215
216 /* sanity check */
217 if (size == 0)
218 return -EINVAL;
219
220 if (buf[size-1] != '\n')
221 return -EINVAL;
222
223 fo_path = buf;
224 if (qword_get(&buf, fo_path, size) < 0)
225 return -EINVAL;
226
Stanislav Kinsbursky90100b12012-01-13 13:09:19 +0400227 if (rpc_pton(&init_net, fo_path, size, sap, salen) == 0)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500228 return -EINVAL;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500229
Chuck Lever41160922009-08-09 15:09:40 -0400230 return nlmsvc_unlock_all_by_ip(sap);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500231}
232
Chuck Lever262a0982008-12-12 16:57:35 -0500233/**
234 * write_unlock_fs - Release all locks on a local file system
235 *
236 * Experimental.
237 *
238 * Input:
239 * buf: '\n'-terminated C string containing the
240 * absolute pathname of a local file system
241 * size: length of C string in @buf
242 * Output:
243 * On success: returns zero if all specified locks were released;
244 * returns one if one or more locks were not released
245 * On error: return code is negative errno value
246 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500247static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
Wendy Cheng17efa372008-01-17 11:10:12 -0500248{
Al Viroa63bb992008-08-02 01:03:36 -0400249 struct path path;
Wendy Cheng17efa372008-01-17 11:10:12 -0500250 char *fo_path;
251 int error;
252
253 /* sanity check */
254 if (size == 0)
255 return -EINVAL;
256
257 if (buf[size-1] != '\n')
258 return -EINVAL;
259
260 fo_path = buf;
261 if (qword_get(&buf, fo_path, size) < 0)
262 return -EINVAL;
263
Al Viroa63bb992008-08-02 01:03:36 -0400264 error = kern_path(fo_path, 0, &path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500265 if (error)
266 return error;
267
Chuck Lever262a0982008-12-12 16:57:35 -0500268 /*
269 * XXX: Needs better sanity checking. Otherwise we could end up
270 * releasing locks on the wrong file system.
271 *
272 * For example:
273 * 1. Does the path refer to a directory?
274 * 2. Is that directory a mount point, or
275 * 3. Is that directory the root of an exported file system?
276 */
Al Virod8c95842011-12-07 18:16:57 -0500277 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
Wendy Cheng17efa372008-01-17 11:10:12 -0500278
Al Viroa63bb992008-08-02 01:03:36 -0400279 path_put(&path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500280 return error;
281}
282
Chuck Lever262a0982008-12-12 16:57:35 -0500283/**
284 * write_filehandle - Get a variable-length NFS file handle by path
285 *
286 * On input, the buffer contains a '\n'-terminated C string comprised of
287 * three alphanumeric words separated by whitespace. The string may
288 * contain escape sequences.
289 *
290 * Input:
291 * buf:
292 * domain: client domain name
293 * path: export pathname
294 * maxsize: numeric maximum size of
295 * @buf
296 * size: length of C string in @buf
297 * Output:
298 * On success: passed-in buffer filled with '\n'-terminated C
299 * string containing a ASCII hex text version
300 * of the NFS file handle;
301 * return code is the size in bytes of the string
302 * On error: return code is negative errno value
303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 char *dname, *path;
Andrew Morton246d95b2007-08-09 00:53:50 -0700307 int uninitialized_var(maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 char *mesg = buf;
309 int len;
310 struct auth_domain *dom;
311 struct knfsd_fh fh;
312
J. Bruce Fields87d26ea2008-01-22 17:40:42 -0500313 if (size == 0)
314 return -EINVAL;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (buf[size-1] != '\n')
317 return -EINVAL;
318 buf[size-1] = 0;
319
320 dname = mesg;
321 len = qword_get(&mesg, dname, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500322 if (len <= 0)
323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 path = dname+len+1;
326 len = qword_get(&mesg, path, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500327 if (len <= 0)
328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 len = get_int(&mesg, &maxsize);
331 if (len)
332 return len;
333
334 if (maxsize < NFS_FHSIZE)
335 return -EINVAL;
336 if (maxsize > NFS3_FHSIZE)
337 maxsize = NFS3_FHSIZE;
338
339 if (qword_get(&mesg, mesg, size)>0)
340 return -EINVAL;
341
342 /* we have all the words, they are in buf.. */
343 dom = unix_domain_find(dname);
344 if (!dom)
345 return -ENOMEM;
346
347 len = exp_rootfh(dom, path, &fh, maxsize);
348 auth_domain_put(dom);
349 if (len)
350 return len;
351
Chuck Lever54224f02008-12-12 16:57:20 -0500352 mesg = buf;
353 len = SIMPLE_TRANSACTION_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
355 mesg[-1] = '\n';
356 return mesg - buf;
357}
358
Chuck Lever262a0982008-12-12 16:57:35 -0500359/**
360 * write_threads - Start NFSD, or report the current number of running threads
361 *
362 * Input:
363 * buf: ignored
364 * size: zero
365 * Output:
366 * On success: passed-in buffer filled with '\n'-terminated C
367 * string numeric value representing the number of
368 * running NFSD threads;
369 * return code is the size in bytes of the string
370 * On error: return code is zero
371 *
372 * OR
373 *
374 * Input:
375 * buf: C string containing an unsigned
376 * integer value representing the
377 * number of NFSD threads to start
378 * size: non-zero length of C string in @buf
379 * Output:
380 * On success: NFS service is started;
381 * passed-in buffer filled with '\n'-terminated C
382 * string numeric value representing the number of
383 * running NFSD threads;
384 * return code is the size in bytes of the string
385 * On error: return code is zero or a negative errno value
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static ssize_t write_threads(struct file *file, char *buf, size_t size)
388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 char *mesg = buf;
390 int rv;
391 if (size > 0) {
392 int newthreads;
393 rv = get_int(&mesg, &newthreads);
394 if (rv)
395 return rv;
Chuck Lever9e074852008-12-12 16:57:27 -0500396 if (newthreads < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -EINVAL;
Chuck Lever9e074852008-12-12 16:57:27 -0500398 rv = nfsd_svc(NFS_PORT, newthreads);
NeilBrown82e12fe2009-06-16 11:03:07 +1000399 if (rv < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return rv;
NeilBrown82e12fe2009-06-16 11:03:07 +1000401 } else
402 rv = nfsd_nrthreads();
Chuck Levere06b6402009-04-23 19:33:25 -0400403
NeilBrown82e12fe2009-06-16 11:03:07 +1000404 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Chuck Lever262a0982008-12-12 16:57:35 -0500407/**
408 * write_pool_threads - Set or report the current number of threads per pool
409 *
410 * Input:
411 * buf: ignored
412 * size: zero
413 *
414 * OR
415 *
416 * Input:
417 * buf: C string containing whitespace-
418 * separated unsigned integer values
419 * representing the number of NFSD
420 * threads to start in each pool
421 * size: non-zero length of C string in @buf
422 * Output:
423 * On success: passed-in buffer filled with '\n'-terminated C
424 * string containing integer values representing the
425 * number of NFSD threads in each pool;
426 * return code is the size in bytes of the string
427 * On error: return code is zero or a negative errno value
428 */
Greg Bankseed29652006-10-02 02:18:02 -0700429static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
430{
431 /* if size > 0, look for an array of number of threads per node
432 * and apply them then write out number of threads per node as reply
433 */
434 char *mesg = buf;
435 int i;
436 int rv;
437 int len;
Neil Brownbedbdd82008-06-10 08:40:35 -0400438 int npools;
Greg Bankseed29652006-10-02 02:18:02 -0700439 int *nthreads;
440
Neil Brownbedbdd82008-06-10 08:40:35 -0400441 mutex_lock(&nfsd_mutex);
442 npools = nfsd_nrpools();
Greg Bankseed29652006-10-02 02:18:02 -0700443 if (npools == 0) {
444 /*
445 * NFS is shut down. The admin can start it by
446 * writing to the threads file but NOT the pool_threads
447 * file, sorry. Report zero threads.
448 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400449 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700450 strcpy(buf, "0\n");
451 return strlen(buf);
452 }
453
454 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
Neil Brownbedbdd82008-06-10 08:40:35 -0400455 rv = -ENOMEM;
Greg Bankseed29652006-10-02 02:18:02 -0700456 if (nthreads == NULL)
Neil Brownbedbdd82008-06-10 08:40:35 -0400457 goto out_free;
Greg Bankseed29652006-10-02 02:18:02 -0700458
459 if (size > 0) {
460 for (i = 0; i < npools; i++) {
461 rv = get_int(&mesg, &nthreads[i]);
462 if (rv == -ENOENT)
463 break; /* fewer numbers than pools */
464 if (rv)
465 goto out_free; /* syntax error */
466 rv = -EINVAL;
467 if (nthreads[i] < 0)
468 goto out_free;
469 }
470 rv = nfsd_set_nrthreads(i, nthreads);
471 if (rv)
472 goto out_free;
473 }
474
475 rv = nfsd_get_nrthreads(npools, nthreads);
476 if (rv)
477 goto out_free;
478
479 mesg = buf;
480 size = SIMPLE_TRANSACTION_LIMIT;
481 for (i = 0; i < npools && size > 0; i++) {
482 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
483 len = strlen(mesg);
484 size -= len;
485 mesg += len;
486 }
J. Bruce Fields413d63d2009-07-28 11:37:25 -0400487 rv = mesg - buf;
Greg Bankseed29652006-10-02 02:18:02 -0700488out_free:
489 kfree(nthreads);
Neil Brownbedbdd82008-06-10 08:40:35 -0400490 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700491 return rv;
492}
493
Jeff Layton3dd98a32008-06-10 08:40:36 -0400494static ssize_t __write_versions(struct file *file, char *buf, size_t size)
NeilBrown70c3b762005-11-07 01:00:25 -0800495{
NeilBrown70c3b762005-11-07 01:00:25 -0800496 char *mesg = buf;
Benny Halevy8daf2202009-04-03 08:28:59 +0300497 char *vers, *minorp, sign;
Chuck Lever261758b2009-04-23 19:33:18 -0400498 int len, num, remaining;
Benny Halevy8daf2202009-04-03 08:28:59 +0300499 unsigned minor;
NeilBrown70c3b762005-11-07 01:00:25 -0800500 ssize_t tlen = 0;
501 char *sep;
502
503 if (size>0) {
504 if (nfsd_serv)
NeilBrown6658d3a2006-10-02 02:17:46 -0700505 /* Cannot change versions without updating
506 * nfsd_serv->sv_xdrsize, and reallocing
507 * rq_argp and rq_resp
508 */
NeilBrown70c3b762005-11-07 01:00:25 -0800509 return -EBUSY;
510 if (buf[size-1] != '\n')
511 return -EINVAL;
512 buf[size-1] = 0;
513
514 vers = mesg;
515 len = qword_get(&mesg, vers, size);
516 if (len <= 0) return -EINVAL;
517 do {
518 sign = *vers;
519 if (sign == '+' || sign == '-')
Benny Halevy8daf2202009-04-03 08:28:59 +0300520 num = simple_strtol((vers+1), &minorp, 0);
NeilBrown70c3b762005-11-07 01:00:25 -0800521 else
Benny Halevy8daf2202009-04-03 08:28:59 +0300522 num = simple_strtol(vers, &minorp, 0);
523 if (*minorp == '.') {
524 if (num < 4)
525 return -EINVAL;
526 minor = simple_strtoul(minorp+1, NULL, 0);
527 if (minor == 0)
528 return -EINVAL;
529 if (nfsd_minorversion(minor, sign == '-' ?
530 NFSD_CLEAR : NFSD_SET) < 0)
531 return -EINVAL;
532 goto next;
533 }
NeilBrown70c3b762005-11-07 01:00:25 -0800534 switch(num) {
535 case 2:
536 case 3:
537 case 4:
NeilBrown6658d3a2006-10-02 02:17:46 -0700538 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
NeilBrown70c3b762005-11-07 01:00:25 -0800539 break;
540 default:
541 return -EINVAL;
542 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300543 next:
NeilBrown70c3b762005-11-07 01:00:25 -0800544 vers += len + 1;
NeilBrown70c3b762005-11-07 01:00:25 -0800545 } while ((len = qword_get(&mesg, vers, size)) > 0);
546 /* If all get turned off, turn them back on, as
547 * having no versions is BAD
548 */
NeilBrown6658d3a2006-10-02 02:17:46 -0700549 nfsd_reset_versions();
NeilBrown70c3b762005-11-07 01:00:25 -0800550 }
Chuck Lever261758b2009-04-23 19:33:18 -0400551
NeilBrown70c3b762005-11-07 01:00:25 -0800552 /* Now write current state into reply buffer */
553 len = 0;
554 sep = "";
Chuck Lever261758b2009-04-23 19:33:18 -0400555 remaining = SIMPLE_TRANSACTION_LIMIT;
NeilBrown70c3b762005-11-07 01:00:25 -0800556 for (num=2 ; num <= 4 ; num++)
NeilBrown6658d3a2006-10-02 02:17:46 -0700557 if (nfsd_vers(num, NFSD_AVAIL)) {
Chuck Lever261758b2009-04-23 19:33:18 -0400558 len = snprintf(buf, remaining, "%s%c%d", sep,
NeilBrown6658d3a2006-10-02 02:17:46 -0700559 nfsd_vers(num, NFSD_TEST)?'+':'-',
NeilBrown70c3b762005-11-07 01:00:25 -0800560 num);
561 sep = " ";
Chuck Lever261758b2009-04-23 19:33:18 -0400562
563 if (len > remaining)
564 break;
565 remaining -= len;
566 buf += len;
567 tlen += len;
NeilBrown70c3b762005-11-07 01:00:25 -0800568 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300569 if (nfsd_vers(4, NFSD_AVAIL))
Chuck Lever261758b2009-04-23 19:33:18 -0400570 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
571 minor++) {
572 len = snprintf(buf, remaining, " %c4.%u",
Benny Halevy8daf2202009-04-03 08:28:59 +0300573 (nfsd_vers(4, NFSD_TEST) &&
574 nfsd_minorversion(minor, NFSD_TEST)) ?
575 '+' : '-',
576 minor);
Chuck Lever261758b2009-04-23 19:33:18 -0400577
578 if (len > remaining)
579 break;
580 remaining -= len;
581 buf += len;
582 tlen += len;
583 }
584
585 len = snprintf(buf, remaining, "\n");
586 if (len > remaining)
587 return -EINVAL;
588 return tlen + len;
NeilBrown70c3b762005-11-07 01:00:25 -0800589}
590
Chuck Lever262a0982008-12-12 16:57:35 -0500591/**
592 * write_versions - Set or report the available NFS protocol versions
593 *
594 * Input:
595 * buf: ignored
596 * size: zero
597 * Output:
598 * On success: passed-in buffer filled with '\n'-terminated C
599 * string containing positive or negative integer
600 * values representing the current status of each
601 * protocol version;
602 * return code is the size in bytes of the string
603 * On error: return code is zero or a negative errno value
604 *
605 * OR
606 *
607 * Input:
608 * buf: C string containing whitespace-
609 * separated positive or negative
610 * integer values representing NFS
611 * protocol versions to enable ("+n")
612 * or disable ("-n")
613 * size: non-zero length of C string in @buf
614 * Output:
615 * On success: status of zero or more protocol versions has
616 * been updated; passed-in buffer filled with
617 * '\n'-terminated C string containing positive
618 * or negative integer values representing the
619 * current status of each protocol version;
620 * return code is the size in bytes of the string
621 * On error: return code is zero or a negative errno value
622 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400623static ssize_t write_versions(struct file *file, char *buf, size_t size)
624{
625 ssize_t rv;
626
627 mutex_lock(&nfsd_mutex);
628 rv = __write_versions(file, buf, size);
629 mutex_unlock(&nfsd_mutex);
630 return rv;
631}
632
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400633/*
Chuck Lever0a5372d2009-04-23 19:32:10 -0400634 * Zero-length write. Return a list of NFSD's current listener
635 * transports.
636 */
637static ssize_t __write_ports_names(char *buf)
638{
639 if (nfsd_serv == NULL)
640 return 0;
Chuck Lever335c54b2009-04-23 19:32:25 -0400641 return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Lever0a5372d2009-04-23 19:32:10 -0400642}
643
644/*
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400645 * A single 'fd' number was written, in which case it must be for
646 * a socket of a supported family/protocol, and we use it as an
647 * nfsd listener.
648 */
649static ssize_t __write_ports_addfd(char *buf)
650{
651 char *mesg = buf;
652 int fd, err;
653
654 err = get_int(&mesg, &fd);
655 if (err != 0 || fd < 0)
656 return -EINVAL;
657
658 err = nfsd_create_serv();
659 if (err != 0)
660 return err;
661
Chuck Leverbfba9ab2009-04-23 19:32:33 -0400662 err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400663 if (err < 0) {
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400664 svc_destroy(nfsd_serv);
665 return err;
666 }
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400667
Chuck Leverea068ba2009-04-23 19:32:18 -0400668 /* Decrease the count, but don't shut down the service */
669 nfsd_serv->sv_nrthreads--;
670 return err;
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400671}
672
673/*
Chuck Lever82d56592009-04-23 19:31:48 -0400674 * A '-' followed by the 'name' of a socket means we close the socket.
675 */
676static ssize_t __write_ports_delfd(char *buf)
677{
678 char *toclose;
679 int len = 0;
680
681 toclose = kstrdup(buf + 1, GFP_KERNEL);
682 if (toclose == NULL)
683 return -ENOMEM;
684
685 if (nfsd_serv != NULL)
Chuck Lever8435d342009-04-23 19:32:40 -0400686 len = svc_sock_names(nfsd_serv, buf,
687 SIMPLE_TRANSACTION_LIMIT, toclose);
Chuck Lever82d56592009-04-23 19:31:48 -0400688 kfree(toclose);
689 return len;
690}
691
692/*
Chuck Lever4eb68c22009-04-23 19:31:40 -0400693 * A transport listener is added by writing it's transport name and
694 * a port number.
695 */
696static ssize_t __write_ports_addxprt(char *buf)
697{
698 char transport[16];
Chuck Lever37498292010-01-26 14:04:22 -0500699 struct svc_xprt *xprt;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400700 int port, err;
701
702 if (sscanf(buf, "%15s %4u", transport, &port) != 2)
703 return -EINVAL;
704
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700705 if (port < 1 || port > USHRT_MAX)
Chuck Lever4eb68c22009-04-23 19:31:40 -0400706 return -EINVAL;
707
708 err = nfsd_create_serv();
709 if (err != 0)
710 return err;
711
Pavel Emelyanovfc5d00b2010-09-29 16:03:50 +0400712 err = svc_create_xprt(nfsd_serv, transport, &init_net,
Chuck Lever4eb68c22009-04-23 19:31:40 -0400713 PF_INET, port, SVC_SOCK_ANONYMOUS);
Chuck Lever68717902010-01-26 14:04:13 -0500714 if (err < 0)
Chuck Lever37498292010-01-26 14:04:22 -0500715 goto out_err;
716
Pavel Emelyanovfc5d00b2010-09-29 16:03:50 +0400717 err = svc_create_xprt(nfsd_serv, transport, &init_net,
Chuck Lever37498292010-01-26 14:04:22 -0500718 PF_INET6, port, SVC_SOCK_ANONYMOUS);
719 if (err < 0 && err != -EAFNOSUPPORT)
720 goto out_close;
Jeff Layton0cd14a02010-07-19 16:50:06 -0400721
722 /* Decrease the count, but don't shut down the service */
723 nfsd_serv->sv_nrthreads--;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400724 return 0;
Chuck Lever37498292010-01-26 14:04:22 -0500725out_close:
Stanislav Kinsbursky4cb54ca2012-01-20 16:50:53 +0400726 xprt = svc_find_xprt(nfsd_serv, transport, &init_net, PF_INET, port);
Chuck Lever37498292010-01-26 14:04:22 -0500727 if (xprt != NULL) {
728 svc_close_xprt(xprt);
729 svc_xprt_put(xprt);
730 }
731out_err:
Jeff Layton0cd14a02010-07-19 16:50:06 -0400732 svc_destroy(nfsd_serv);
Chuck Lever37498292010-01-26 14:04:22 -0500733 return err;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400734}
735
736/*
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400737 * A transport listener is removed by writing a "-", it's transport
738 * name, and it's port number.
739 */
740static ssize_t __write_ports_delxprt(char *buf)
741{
742 struct svc_xprt *xprt;
743 char transport[16];
744 int port;
745
746 if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
747 return -EINVAL;
748
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700749 if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400750 return -EINVAL;
751
Stanislav Kinsbursky4cb54ca2012-01-20 16:50:53 +0400752 xprt = svc_find_xprt(nfsd_serv, transport, &init_net, AF_UNSPEC, port);
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400753 if (xprt == NULL)
754 return -ENOTCONN;
755
756 svc_close_xprt(xprt);
757 svc_xprt_put(xprt);
758 return 0;
759}
760
Neil Brownbedbdd82008-06-10 08:40:35 -0400761static ssize_t __write_ports(struct file *file, char *buf, size_t size)
NeilBrown80212d52006-10-02 02:17:47 -0700762{
Chuck Lever0a5372d2009-04-23 19:32:10 -0400763 if (size == 0)
764 return __write_ports_names(buf);
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400765
766 if (isdigit(buf[0]))
767 return __write_ports_addfd(buf);
Chuck Lever82d56592009-04-23 19:31:48 -0400768
769 if (buf[0] == '-' && isdigit(buf[1]))
770 return __write_ports_delfd(buf);
Chuck Lever4eb68c22009-04-23 19:31:40 -0400771
772 if (isalpha(buf[0]))
773 return __write_ports_addxprt(buf);
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400774
775 if (buf[0] == '-' && isalpha(buf[1]))
776 return __write_ports_delxprt(buf);
777
NeilBrownb41b66d2006-10-02 02:17:48 -0700778 return -EINVAL;
NeilBrown80212d52006-10-02 02:17:47 -0700779}
780
Chuck Lever262a0982008-12-12 16:57:35 -0500781/**
782 * write_ports - Pass a socket file descriptor or transport name to listen on
783 *
784 * Input:
785 * buf: ignored
786 * size: zero
787 * Output:
788 * On success: passed-in buffer filled with a '\n'-terminated C
789 * string containing a whitespace-separated list of
790 * named NFSD listeners;
791 * return code is the size in bytes of the string
792 * On error: return code is zero or a negative errno value
793 *
794 * OR
795 *
796 * Input:
797 * buf: C string containing an unsigned
798 * integer value representing a bound
799 * but unconnected socket that is to be
Chuck Leverc71206a2009-04-23 19:32:03 -0400800 * used as an NFSD listener; listen(3)
801 * must be called for a SOCK_STREAM
802 * socket, otherwise it is ignored
Chuck Lever262a0982008-12-12 16:57:35 -0500803 * size: non-zero length of C string in @buf
804 * Output:
805 * On success: NFS service is started;
806 * passed-in buffer filled with a '\n'-terminated C
807 * string containing a unique alphanumeric name of
808 * the listener;
809 * return code is the size in bytes of the string
810 * On error: return code is a negative errno value
811 *
812 * OR
813 *
814 * Input:
815 * buf: C string containing a "-" followed
816 * by an integer value representing a
817 * previously passed in socket file
818 * descriptor
819 * size: non-zero length of C string in @buf
820 * Output:
821 * On success: NFS service no longer listens on that socket;
822 * passed-in buffer filled with a '\n'-terminated C
823 * string containing a unique name of the listener;
824 * return code is the size in bytes of the string
825 * On error: return code is a negative errno value
826 *
827 * OR
828 *
829 * Input:
830 * buf: C string containing a transport
831 * name and an unsigned integer value
832 * representing the port to listen on,
833 * separated by whitespace
834 * size: non-zero length of C string in @buf
835 * Output:
836 * On success: returns zero; NFS service is started
837 * On error: return code is a negative errno value
838 *
839 * OR
840 *
841 * Input:
842 * buf: C string containing a "-" followed
843 * by a transport name and an unsigned
844 * integer value representing the port
845 * to listen on, separated by whitespace
846 * size: non-zero length of C string in @buf
847 * Output:
848 * On success: returns zero; NFS service no longer listens
849 * on that transport
850 * On error: return code is a negative errno value
851 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400852static ssize_t write_ports(struct file *file, char *buf, size_t size)
853{
854 ssize_t rv;
Jeff Layton3dd98a32008-06-10 08:40:36 -0400855
Neil Brownbedbdd82008-06-10 08:40:35 -0400856 mutex_lock(&nfsd_mutex);
857 rv = __write_ports(file, buf, size);
858 mutex_unlock(&nfsd_mutex);
859 return rv;
860}
861
862
NeilBrown596bbe52006-10-04 02:15:48 -0700863int nfsd_max_blksize;
864
Chuck Lever262a0982008-12-12 16:57:35 -0500865/**
866 * write_maxblksize - Set or report the current NFS blksize
867 *
868 * Input:
869 * buf: ignored
870 * size: zero
871 *
872 * OR
873 *
874 * Input:
875 * buf: C string containing an unsigned
876 * integer value representing the new
877 * NFS blksize
878 * size: non-zero length of C string in @buf
879 * Output:
880 * On success: passed-in buffer filled with '\n'-terminated C string
881 * containing numeric value of the current NFS blksize
882 * setting;
883 * return code is the size in bytes of the string
884 * On error: return code is zero or a negative errno value
885 */
NeilBrown596bbe52006-10-04 02:15:48 -0700886static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
887{
888 char *mesg = buf;
889 if (size > 0) {
890 int bsize;
891 int rv = get_int(&mesg, &bsize);
892 if (rv)
893 return rv;
894 /* force bsize into allowed range and
895 * required alignment.
896 */
897 if (bsize < 1024)
898 bsize = 1024;
899 if (bsize > NFSSVC_MAXBLKSIZE)
900 bsize = NFSSVC_MAXBLKSIZE;
901 bsize &= ~(1024-1);
Neil Brownbedbdd82008-06-10 08:40:35 -0400902 mutex_lock(&nfsd_mutex);
J. Bruce Fields7fa53cc2010-08-06 18:00:33 -0400903 if (nfsd_serv) {
Neil Brownbedbdd82008-06-10 08:40:35 -0400904 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -0700905 return -EBUSY;
906 }
907 nfsd_max_blksize = bsize;
Neil Brownbedbdd82008-06-10 08:40:35 -0400908 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -0700909 }
Chuck Levere06b6402009-04-23 19:33:25 -0400910
911 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
912 nfsd_max_blksize);
NeilBrown596bbe52006-10-04 02:15:48 -0700913}
914
NeilBrown70c3b762005-11-07 01:00:25 -0800915#ifdef CONFIG_NFSD_V4
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500916static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 char *mesg = buf;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500919 int rv, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (size > 0) {
Jeff Layton3dd98a32008-06-10 08:40:36 -0400922 if (nfsd_serv)
923 return -EBUSY;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500924 rv = get_int(&mesg, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (rv)
926 return rv;
J. Bruce Fieldse7b184f2010-03-02 11:18:40 -0500927 /*
928 * Some sanity checking. We don't have a reason for
929 * these particular numbers, but problems with the
930 * extremes are:
931 * - Too short: the briefest network outage may
932 * cause clients to lose all their locks. Also,
933 * the frequent polling may be wasteful.
934 * - Too long: do you really want reboot recovery
935 * to take more than an hour? Or to make other
936 * clients wait an hour before being able to
937 * revoke a dead client's locks?
938 */
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500939 if (i < 10 || i > 3600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EINVAL;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500941 *time = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Chuck Levere06b6402009-04-23 19:33:25 -0400943
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500944 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
945}
946
947static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
948{
949 ssize_t rv;
950
951 mutex_lock(&nfsd_mutex);
952 rv = __nfsd4_write_time(file, buf, size, time);
953 mutex_unlock(&nfsd_mutex);
954 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Chuck Lever262a0982008-12-12 16:57:35 -0500957/**
958 * write_leasetime - Set or report the current NFSv4 lease time
959 *
960 * Input:
961 * buf: ignored
962 * size: zero
963 *
964 * OR
965 *
966 * Input:
967 * buf: C string containing an unsigned
968 * integer value representing the new
969 * NFSv4 lease expiry time
970 * size: non-zero length of C string in @buf
971 * Output:
972 * On success: passed-in buffer filled with '\n'-terminated C
973 * string containing unsigned integer value of the
974 * current lease expiry time;
975 * return code is the size in bytes of the string
976 * On error: return code is zero or a negative errno value
977 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400978static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
979{
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500980 return nfsd4_write_time(file, buf, size, &nfsd4_lease);
Jeff Layton3dd98a32008-06-10 08:40:36 -0400981}
982
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -0500983/**
984 * write_gracetime - Set or report current NFSv4 grace period time
985 *
986 * As above, but sets the time of the NFSv4 grace period.
987 *
988 * Note this should never be set to less than the *previous*
989 * lease-period time, but we don't try to enforce this. (In the common
990 * case (a new boot), we don't know what the previous lease time was
991 * anyway.)
992 */
993static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
994{
995 return nfsd4_write_time(file, buf, size, &nfsd4_grace);
996}
997
Jeff Layton3dd98a32008-06-10 08:40:36 -0400998extern char *nfs4_recoverydir(void);
999
1000static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
NeilBrown0964a3d2005-06-23 22:04:32 -07001001{
1002 char *mesg = buf;
1003 char *recdir;
1004 int len, status;
1005
Jeff Layton3dd98a32008-06-10 08:40:36 -04001006 if (size > 0) {
1007 if (nfsd_serv)
1008 return -EBUSY;
1009 if (size > PATH_MAX || buf[size-1] != '\n')
1010 return -EINVAL;
1011 buf[size-1] = 0;
NeilBrown0964a3d2005-06-23 22:04:32 -07001012
Jeff Layton3dd98a32008-06-10 08:40:36 -04001013 recdir = mesg;
1014 len = qword_get(&mesg, recdir, size);
1015 if (len <= 0)
1016 return -EINVAL;
NeilBrown0964a3d2005-06-23 22:04:32 -07001017
Jeff Layton3dd98a32008-06-10 08:40:36 -04001018 status = nfs4_reset_recoverydir(recdir);
Andi Kleen69049962010-07-20 15:24:27 -07001019 if (status)
1020 return status;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001021 }
Chuck Lever3d72ab82009-04-23 19:33:10 -04001022
1023 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1024 nfs4_recoverydir());
NeilBrown0964a3d2005-06-23 22:04:32 -07001025}
Jeff Layton3dd98a32008-06-10 08:40:36 -04001026
Chuck Lever262a0982008-12-12 16:57:35 -05001027/**
1028 * write_recoverydir - Set or report the pathname of the recovery directory
1029 *
1030 * Input:
1031 * buf: ignored
1032 * size: zero
1033 *
1034 * OR
1035 *
1036 * Input:
1037 * buf: C string containing the pathname
1038 * of the directory on a local file
1039 * system containing permanent NFSv4
1040 * recovery data
1041 * size: non-zero length of C string in @buf
1042 * Output:
1043 * On success: passed-in buffer filled with '\n'-terminated C string
1044 * containing the current recovery pathname setting;
1045 * return code is the size in bytes of the string
1046 * On error: return code is zero or a negative errno value
1047 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001048static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1049{
1050 ssize_t rv;
1051
1052 mutex_lock(&nfsd_mutex);
1053 rv = __write_recoverydir(file, buf, size);
1054 mutex_unlock(&nfsd_mutex);
1055 return rv;
1056}
1057
NeilBrown70c3b762005-11-07 01:00:25 -08001058#endif
NeilBrown0964a3d2005-06-23 22:04:32 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/*----------------------------------------------------------------------------*/
1061/*
1062 * populating the filesystem.
1063 */
1064
1065static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1066{
1067 static struct tree_descr nfsd_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
J. Bruce Fieldse8e87532009-12-14 12:53:32 -05001069 [NFSD_Export_features] = {"export_features",
1070 &export_features_operations, S_IRUGO},
Wendy Cheng4373ea82008-01-17 11:10:12 -05001071 [NFSD_FO_UnlockIP] = {"unlock_ip",
1072 &transaction_ops, S_IWUSR|S_IRUSR},
Wendy Cheng17efa372008-01-17 11:10:12 -05001073 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1074 &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1076 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Bankseed29652006-10-02 02:18:02 -07001077 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Banks03cf6c92009-01-13 21:26:36 +11001078 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
NeilBrown70c3b762005-11-07 01:00:25 -08001079 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown80212d52006-10-02 02:17:47 -07001080 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
NeilBrown596bbe52006-10-04 02:15:48 -07001081 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
J. Bruce Fieldsb084f592011-05-31 12:24:58 -04001082#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -05001083 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
J. Bruce Fieldsb084f592011-05-31 12:24:58 -04001084#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085#ifdef CONFIG_NFSD_V4
1086 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -05001087 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown0964a3d2005-06-23 22:04:32 -07001088 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#endif
1090 /* last one */ {""}
1091 };
1092 return simple_fill_super(sb, 0x6e667364, nfsd_files);
1093}
1094
Al Virofc14f2f2010-07-25 01:48:30 +04001095static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1096 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Al Virofc14f2f2010-07-25 01:48:30 +04001098 return mount_single(fs_type, flags, data, nfsd_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static struct file_system_type nfsd_fs_type = {
1102 .owner = THIS_MODULE,
1103 .name = "nfsd",
Al Virofc14f2f2010-07-25 01:48:30 +04001104 .mount = nfsd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .kill_sb = kill_litter_super,
1106};
1107
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001108#ifdef CONFIG_PROC_FS
1109static int create_proc_exports_entry(void)
1110{
1111 struct proc_dir_entry *entry;
1112
1113 entry = proc_mkdir("fs/nfs", NULL);
1114 if (!entry)
1115 return -ENOMEM;
Denis V. Lunev9ef2db22008-04-29 01:02:04 -07001116 entry = proc_create("exports", 0, entry, &exports_operations);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001117 if (!entry)
1118 return -ENOMEM;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001119 return 0;
1120}
1121#else /* CONFIG_PROC_FS */
1122static int create_proc_exports_entry(void)
1123{
1124 return 0;
1125}
1126#endif
1127
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001128int nfsd_net_id;
1129static struct pernet_operations nfsd_net_ops = {
1130 .id = &nfsd_net_id,
1131 .size = sizeof(struct nfsd_net),
1132};
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134static int __init init_nfsd(void)
1135{
1136 int retval;
1137 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1138
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001139 retval = register_pernet_subsys(&nfsd_net_ops);
1140 if (retval < 0)
1141 return retval;
Bryan Schumaker72083392011-11-01 15:24:59 -04001142 retval = nfsd4_init_slabs();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001143 if (retval)
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001144 goto out_unregister_pernet;
Bryan Schumaker72083392011-11-01 15:24:59 -04001145 nfs4_state_init();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001146 retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
1147 if (retval)
1148 goto out_free_slabs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 nfsd_stat_init(); /* Statistics */
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001150 retval = nfsd_reply_cache_init();
1151 if (retval)
1152 goto out_free_stat;
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001153 retval = nfsd_export_init();
1154 if (retval)
1155 goto out_free_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 nfsd_lockd_init(); /* lockd->nfsd callbacks */
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001157 retval = nfsd_idmap_init();
1158 if (retval)
1159 goto out_free_lockd;
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001160 retval = create_proc_exports_entry();
1161 if (retval)
1162 goto out_free_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 retval = register_filesystem(&nfsd_fs_type);
J. Bruce Fields26808d32007-11-09 13:44:06 -05001164 if (retval)
1165 goto out_free_all;
1166 return 0;
1167out_free_all:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001168 remove_proc_entry("fs/nfs/exports", NULL);
1169 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001170out_free_idmap:
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001171 nfsd_idmap_shutdown();
1172out_free_lockd:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001173 nfsd_lockd_shutdown();
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001174 nfsd_export_shutdown();
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001175out_free_cache:
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001176 nfsd_reply_cache_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001177out_free_stat:
1178 nfsd_stat_shutdown();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001179 nfsd_fault_inject_cleanup();
1180out_free_slabs:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001181 nfsd4_free_slabs();
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001182out_unregister_pernet:
1183 unregister_pernet_subsys(&nfsd_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return retval;
1185}
1186
1187static void __exit exit_nfsd(void)
1188{
1189 nfsd_export_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001190 nfsd_reply_cache_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 remove_proc_entry("fs/nfs/exports", NULL);
1192 remove_proc_entry("fs/nfs", NULL);
1193 nfsd_stat_shutdown();
1194 nfsd_lockd_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 nfsd_idmap_shutdown();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001196 nfsd4_free_slabs();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001197 nfsd_fault_inject_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 unregister_filesystem(&nfsd_fs_type);
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001199 unregister_pernet_subsys(&nfsd_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1203MODULE_LICENSE("GPL");
1204module_init(init_nfsd)
1205module_exit(exit_nfsd)