blob: 6a9de59d9633ed26feefc7494ea7549809ac2a60 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Syscall interface to knfsd.
4 *
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Al Viro3f8206d2008-07-26 03:46:43 -04009#include <linux/namei.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070010#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
NeilBrown80212d52006-10-02 02:17:47 -070012#include <linux/sunrpc/svcsock.h>
Wendy Cheng4373ea82008-01-17 11:10:12 -050013#include <linux/lockd/lockd.h>
Jeff Layton59766872013-02-04 12:50:00 -050014#include <linux/sunrpc/addr.h>
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050015#include <linux/sunrpc/gss_api.h>
J. Bruce Fieldsb084f592011-05-31 12:24:58 -040016#include <linux/sunrpc/gss_krb5_enctypes.h>
Jeff Layton813fd322012-03-21 09:52:08 -040017#include <linux/sunrpc/rpc_pipe_fs.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050020#include "idmap.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020021#include "nfsd.h"
22#include "cache.h"
Bryan Schumakerf3c75212012-11-27 09:35:10 -050023#include "state.h"
Jeff Layton7ea34ac2012-03-21 09:52:05 -040024#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020025#include "pnfs.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050028 * We have a single directory with several nodes in it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30enum {
31 NFSD_Root = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 NFSD_List,
J. Bruce Fieldse8e87532009-12-14 12:53:32 -050033 NFSD_Export_features,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 NFSD_Fh,
Wendy Cheng4373ea82008-01-17 11:10:12 -050035 NFSD_FO_UnlockIP,
Wendy Cheng17efa372008-01-17 11:10:12 -050036 NFSD_FO_UnlockFS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 NFSD_Threads,
Greg Bankseed29652006-10-02 02:18:02 -070038 NFSD_Pool_Threads,
Greg Banks03cf6c92009-01-13 21:26:36 +110039 NFSD_Pool_Stats,
Jeff Laytona2f999a2013-03-27 10:15:38 -040040 NFSD_Reply_Cache_Stats,
NeilBrown70c3b762005-11-07 01:00:25 -080041 NFSD_Versions,
NeilBrown80212d52006-10-02 02:17:47 -070042 NFSD_Ports,
NeilBrown596bbe52006-10-04 02:15:48 -070043 NFSD_MaxBlkSize,
Jeff Layton5b8db002014-07-02 16:11:22 -040044 NFSD_MaxConnections,
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -050045 NFSD_SupportedEnctypes,
NeilBrown70c3b762005-11-07 01:00:25 -080046 /*
47 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
48 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
49 */
50#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 NFSD_Leasetime,
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050052 NFSD_Gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070053 NFSD_RecoveryDir,
Jeff Layton7f5ef2e2014-09-12 16:40:21 -040054 NFSD_V4EndGrace,
NeilBrown70c3b762005-11-07 01:00:25 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/*
59 * write() for these nodes.
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
Chuck Leverb046ccd2008-12-12 16:57:13 -050062static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
63static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static ssize_t write_threads(struct file *file, char *buf, size_t size);
Greg Bankseed29652006-10-02 02:18:02 -070065static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080066static ssize_t write_versions(struct file *file, char *buf, size_t size);
NeilBrown80212d52006-10-02 02:17:47 -070067static ssize_t write_ports(struct file *file, char *buf, size_t size);
NeilBrown596bbe52006-10-04 02:15:48 -070068static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
Jeff Layton5b8db002014-07-02 16:11:22 -040069static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080070#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050072static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
NeilBrown0964a3d2005-06-23 22:04:32 -070073static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
Jeff Layton7f5ef2e2014-09-12 16:40:21 -040074static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
NeilBrown70c3b762005-11-07 01:00:25 -080075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Biggersc2cdc2a2018-07-17 11:01:25 -070077static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 [NFSD_Fh] = write_filehandle,
Chuck Leverb046ccd2008-12-12 16:57:13 -050079 [NFSD_FO_UnlockIP] = write_unlock_ip,
80 [NFSD_FO_UnlockFS] = write_unlock_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 [NFSD_Threads] = write_threads,
Greg Bankseed29652006-10-02 02:18:02 -070082 [NFSD_Pool_Threads] = write_pool_threads,
NeilBrown70c3b762005-11-07 01:00:25 -080083 [NFSD_Versions] = write_versions,
NeilBrown80212d52006-10-02 02:17:47 -070084 [NFSD_Ports] = write_ports,
NeilBrown596bbe52006-10-04 02:15:48 -070085 [NFSD_MaxBlkSize] = write_maxblksize,
Jeff Layton5b8db002014-07-02 16:11:22 -040086 [NFSD_MaxConnections] = write_maxconn,
NeilBrown70c3b762005-11-07 01:00:25 -080087#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 [NFSD_Leasetime] = write_leasetime,
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050089 [NFSD_Gracetime] = write_gracetime,
NeilBrown0964a3d2005-06-23 22:04:32 -070090 [NFSD_RecoveryDir] = write_recoverydir,
Jeff Layton7f5ef2e2014-09-12 16:40:21 -040091 [NFSD_V4EndGrace] = write_v4_end_grace,
NeilBrown70c3b762005-11-07 01:00:25 -080092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
96{
Al Viro496ad9a2013-01-23 17:07:38 -050097 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 char *data;
99 ssize_t rv;
100
Tobias Klausere8c96f82006-03-24 03:15:34 -0800101 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -EINVAL;
103
104 data = simple_transaction_get(file, buf, size);
105 if (IS_ERR(data))
106 return PTR_ERR(data);
107
108 rv = write_op[ino](file, data, size);
NeilBrown8971a102007-02-14 00:33:11 -0800109 if (rv >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 simple_transaction_set(file, rv);
111 rv = size;
112 }
113 return rv;
114}
115
NeilBrown73900222005-11-07 01:00:24 -0800116static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
117{
118 if (! file->private_data) {
119 /* An attempt to read a transaction file without writing
120 * causes a 0-byte write so that the file can return
121 * state information
122 */
123 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
124 if (rv < 0)
125 return rv;
126 }
127 return simple_transaction_read(file, buf, size, pos);
128}
129
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800130static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .write = nfsctl_transaction_write,
NeilBrown73900222005-11-07 01:00:24 -0800132 .read = nfsctl_transaction_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .release = simple_transaction_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200134 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +0300137static int exports_net_open(struct net *net, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Stanislav Kinsburskyf2c7ea12012-03-28 19:09:29 +0400139 int err;
140 struct seq_file *seq;
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +0300141 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskyf2c7ea12012-03-28 19:09:29 +0400142
143 err = seq_open(file, &nfs_exports_op);
144 if (err)
145 return err;
146
147 seq = file->private_data;
Stanislav Kinsburskye5f06f72012-04-11 15:13:28 +0400148 seq->private = nn->svc_export_cache;
Stanislav Kinsburskyf2c7ea12012-03-28 19:09:29 +0400149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +0300152static int exports_proc_open(struct inode *inode, struct file *file)
153{
154 return exports_net_open(current->nsproxy->net_ns, file);
155}
156
157static const struct file_operations exports_proc_operations = {
158 .open = exports_proc_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = seq_release,
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +0300162};
163
164static int exports_nfsd_open(struct inode *inode, struct file *file)
165{
166 return exports_net_open(inode->i_sb->s_fs_info, file);
167}
168
169static const struct file_operations exports_nfsd_operations = {
170 .open = exports_nfsd_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .read = seq_read,
172 .llseek = seq_lseek,
173 .release = seq_release,
174};
175
J. Bruce Fieldse8e87532009-12-14 12:53:32 -0500176static int export_features_show(struct seq_file *m, void *v)
177{
178 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
179 return 0;
180}
181
182static int export_features_open(struct inode *inode, struct file *file)
183{
184 return single_open(file, export_features_show, NULL);
185}
186
Al Viro75ef9de2013-04-04 19:09:41 -0400187static const struct file_operations export_features_operations = {
J. Bruce Fieldse8e87532009-12-14 12:53:32 -0500188 .open = export_features_open,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .release = single_release,
192};
193
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400194#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500195static int supported_enctypes_show(struct seq_file *m, void *v)
196{
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400197 seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500198 return 0;
199}
200
201static int supported_enctypes_open(struct inode *inode, struct file *file)
202{
203 return single_open(file, supported_enctypes_show, NULL);
204}
205
Al Viro75ef9de2013-04-04 19:09:41 -0400206static const struct file_operations supported_enctypes_ops = {
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500207 .open = supported_enctypes_open,
208 .read = seq_read,
209 .llseek = seq_lseek,
210 .release = single_release,
211};
J. Bruce Fieldsb084f592011-05-31 12:24:58 -0400212#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -0500213
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700214static const struct file_operations pool_stats_operations = {
Greg Banks03cf6c92009-01-13 21:26:36 +1100215 .open = nfsd_pool_stats_open,
216 .read = seq_read,
217 .llseek = seq_lseek,
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900218 .release = nfsd_pool_stats_release,
Greg Banks03cf6c92009-01-13 21:26:36 +1100219};
220
Julia Lawall7ba630f2016-08-28 22:36:55 +0200221static const struct file_operations reply_cache_stats_operations = {
Jeff Laytona2f999a2013-03-27 10:15:38 -0400222 .open = nfsd_reply_cache_stats_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*----------------------------------------------------------------------------*/
229/*
230 * payload - write methods
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232
Al Viro244c7d42014-10-21 20:19:11 -0400233static inline struct net *netns(struct file *file)
234{
235 return file_inode(file)->i_sb->s_fs_info;
236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Chuck Lever262a0982008-12-12 16:57:35 -0500238/**
239 * write_unlock_ip - Release all locks used by a client
240 *
241 * Experimental.
242 *
243 * Input:
244 * buf: '\n'-terminated C string containing a
Chuck Lever41160922009-08-09 15:09:40 -0400245 * presentation format IP address
Chuck Lever262a0982008-12-12 16:57:35 -0500246 * size: length of C string in @buf
247 * Output:
248 * On success: returns zero if all specified locks were released;
249 * returns one if one or more locks were not released
250 * On error: return code is negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500251 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500252static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500253{
Chuck Lever41160922009-08-09 15:09:40 -0400254 struct sockaddr_storage address;
255 struct sockaddr *sap = (struct sockaddr *)&address;
256 size_t salen = sizeof(address);
Chuck Lever367c8c72008-06-30 18:58:14 -0400257 char *fo_path;
Al Viro244c7d42014-10-21 20:19:11 -0400258 struct net *net = netns(file);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500259
260 /* sanity check */
261 if (size == 0)
262 return -EINVAL;
263
264 if (buf[size-1] != '\n')
265 return -EINVAL;
266
267 fo_path = buf;
268 if (qword_get(&buf, fo_path, size) < 0)
269 return -EINVAL;
270
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +0300271 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
Wendy Cheng4373ea82008-01-17 11:10:12 -0500272 return -EINVAL;
Wendy Cheng4373ea82008-01-17 11:10:12 -0500273
Chuck Lever41160922009-08-09 15:09:40 -0400274 return nlmsvc_unlock_all_by_ip(sap);
Wendy Cheng4373ea82008-01-17 11:10:12 -0500275}
276
Chuck Lever262a0982008-12-12 16:57:35 -0500277/**
278 * write_unlock_fs - Release all locks on a local file system
279 *
280 * Experimental.
281 *
282 * Input:
283 * buf: '\n'-terminated C string containing the
284 * absolute pathname of a local file system
285 * size: length of C string in @buf
286 * Output:
287 * On success: returns zero if all specified locks were released;
288 * returns one if one or more locks were not released
289 * On error: return code is negative errno value
290 */
Chuck Leverb046ccd2008-12-12 16:57:13 -0500291static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
Wendy Cheng17efa372008-01-17 11:10:12 -0500292{
Al Viroa63bb992008-08-02 01:03:36 -0400293 struct path path;
Wendy Cheng17efa372008-01-17 11:10:12 -0500294 char *fo_path;
295 int error;
296
297 /* sanity check */
298 if (size == 0)
299 return -EINVAL;
300
301 if (buf[size-1] != '\n')
302 return -EINVAL;
303
304 fo_path = buf;
305 if (qword_get(&buf, fo_path, size) < 0)
306 return -EINVAL;
307
Al Viroa63bb992008-08-02 01:03:36 -0400308 error = kern_path(fo_path, 0, &path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500309 if (error)
310 return error;
311
Chuck Lever262a0982008-12-12 16:57:35 -0500312 /*
313 * XXX: Needs better sanity checking. Otherwise we could end up
314 * releasing locks on the wrong file system.
315 *
316 * For example:
317 * 1. Does the path refer to a directory?
318 * 2. Is that directory a mount point, or
319 * 3. Is that directory the root of an exported file system?
320 */
Al Virod8c95842011-12-07 18:16:57 -0500321 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
Wendy Cheng17efa372008-01-17 11:10:12 -0500322
Al Viroa63bb992008-08-02 01:03:36 -0400323 path_put(&path);
Wendy Cheng17efa372008-01-17 11:10:12 -0500324 return error;
325}
326
Chuck Lever262a0982008-12-12 16:57:35 -0500327/**
328 * write_filehandle - Get a variable-length NFS file handle by path
329 *
330 * On input, the buffer contains a '\n'-terminated C string comprised of
331 * three alphanumeric words separated by whitespace. The string may
332 * contain escape sequences.
333 *
334 * Input:
335 * buf:
336 * domain: client domain name
337 * path: export pathname
338 * maxsize: numeric maximum size of
339 * @buf
340 * size: length of C string in @buf
341 * Output:
342 * On success: passed-in buffer filled with '\n'-terminated C
343 * string containing a ASCII hex text version
344 * of the NFS file handle;
345 * return code is the size in bytes of the string
346 * On error: return code is negative errno value
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 char *dname, *path;
Andrew Morton246d95b2007-08-09 00:53:50 -0700351 int uninitialized_var(maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 char *mesg = buf;
353 int len;
354 struct auth_domain *dom;
355 struct knfsd_fh fh;
356
J. Bruce Fields87d26ea2008-01-22 17:40:42 -0500357 if (size == 0)
358 return -EINVAL;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (buf[size-1] != '\n')
361 return -EINVAL;
362 buf[size-1] = 0;
363
364 dname = mesg;
365 len = qword_get(&mesg, dname, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500366 if (len <= 0)
367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 path = dname+len+1;
370 len = qword_get(&mesg, path, size);
Chuck Lever54224f02008-12-12 16:57:20 -0500371 if (len <= 0)
372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 len = get_int(&mesg, &maxsize);
375 if (len)
376 return len;
377
378 if (maxsize < NFS_FHSIZE)
379 return -EINVAL;
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800380 maxsize = min(maxsize, NFS3_FHSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (qword_get(&mesg, mesg, size)>0)
383 return -EINVAL;
384
385 /* we have all the words, they are in buf.. */
386 dom = unix_domain_find(dname);
387 if (!dom)
388 return -ENOMEM;
389
Al Viro244c7d42014-10-21 20:19:11 -0400390 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 auth_domain_put(dom);
392 if (len)
393 return len;
394
Chuck Lever54224f02008-12-12 16:57:20 -0500395 mesg = buf;
396 len = SIMPLE_TRANSACTION_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
398 mesg[-1] = '\n';
399 return mesg - buf;
400}
401
Chuck Lever262a0982008-12-12 16:57:35 -0500402/**
403 * write_threads - Start NFSD, or report the current number of running threads
404 *
405 * Input:
406 * buf: ignored
407 * size: zero
408 * Output:
409 * On success: passed-in buffer filled with '\n'-terminated C
410 * string numeric value representing the number of
411 * running NFSD threads;
412 * return code is the size in bytes of the string
413 * On error: return code is zero
414 *
415 * OR
416 *
417 * Input:
418 * buf: C string containing an unsigned
419 * integer value representing the
420 * number of NFSD threads to start
421 * size: non-zero length of C string in @buf
422 * Output:
423 * On success: NFS service is started;
424 * passed-in buffer filled with '\n'-terminated C
425 * string numeric value representing the number of
426 * running NFSD threads;
427 * return code is the size in bytes of the string
428 * On error: return code is zero or a negative errno value
429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static ssize_t write_threads(struct file *file, char *buf, size_t size)
431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 char *mesg = buf;
433 int rv;
Al Viro244c7d42014-10-21 20:19:11 -0400434 struct net *net = netns(file);
Stanislav Kinsburskyd41a9412012-12-10 12:19:25 +0300435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (size > 0) {
437 int newthreads;
438 rv = get_int(&mesg, &newthreads);
439 if (rv)
440 return rv;
Chuck Lever9e074852008-12-12 16:57:27 -0500441 if (newthreads < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -EINVAL;
Trond Myklebust4df493a2019-04-09 12:13:37 -0400443 rv = nfsd_svc(newthreads, net, file->f_cred);
NeilBrown82e12fe2009-06-16 11:03:07 +1000444 if (rv < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return rv;
NeilBrown82e12fe2009-06-16 11:03:07 +1000446 } else
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300447 rv = nfsd_nrthreads(net);
Chuck Levere06b6402009-04-23 19:33:25 -0400448
NeilBrown82e12fe2009-06-16 11:03:07 +1000449 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Chuck Lever262a0982008-12-12 16:57:35 -0500452/**
453 * write_pool_threads - Set or report the current number of threads per pool
454 *
455 * Input:
456 * buf: ignored
457 * size: zero
458 *
459 * OR
460 *
461 * Input:
462 * buf: C string containing whitespace-
463 * separated unsigned integer values
464 * representing the number of NFSD
465 * threads to start in each pool
466 * size: non-zero length of C string in @buf
467 * Output:
468 * On success: passed-in buffer filled with '\n'-terminated C
469 * string containing integer values representing the
470 * number of NFSD threads in each pool;
471 * return code is the size in bytes of the string
472 * On error: return code is zero or a negative errno value
473 */
Greg Bankseed29652006-10-02 02:18:02 -0700474static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
475{
476 /* if size > 0, look for an array of number of threads per node
477 * and apply them then write out number of threads per node as reply
478 */
479 char *mesg = buf;
480 int i;
481 int rv;
482 int len;
Neil Brownbedbdd82008-06-10 08:40:35 -0400483 int npools;
Greg Bankseed29652006-10-02 02:18:02 -0700484 int *nthreads;
Al Viro244c7d42014-10-21 20:19:11 -0400485 struct net *net = netns(file);
Greg Bankseed29652006-10-02 02:18:02 -0700486
Neil Brownbedbdd82008-06-10 08:40:35 -0400487 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300488 npools = nfsd_nrpools(net);
Greg Bankseed29652006-10-02 02:18:02 -0700489 if (npools == 0) {
490 /*
491 * NFS is shut down. The admin can start it by
492 * writing to the threads file but NOT the pool_threads
493 * file, sorry. Report zero threads.
494 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400495 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700496 strcpy(buf, "0\n");
497 return strlen(buf);
498 }
499
500 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
Neil Brownbedbdd82008-06-10 08:40:35 -0400501 rv = -ENOMEM;
Greg Bankseed29652006-10-02 02:18:02 -0700502 if (nthreads == NULL)
Neil Brownbedbdd82008-06-10 08:40:35 -0400503 goto out_free;
Greg Bankseed29652006-10-02 02:18:02 -0700504
505 if (size > 0) {
506 for (i = 0; i < npools; i++) {
507 rv = get_int(&mesg, &nthreads[i]);
508 if (rv == -ENOENT)
509 break; /* fewer numbers than pools */
510 if (rv)
511 goto out_free; /* syntax error */
512 rv = -EINVAL;
513 if (nthreads[i] < 0)
514 goto out_free;
515 }
Stanislav Kinsbursky3938a0d2012-12-10 12:19:30 +0300516 rv = nfsd_set_nrthreads(i, nthreads, net);
Greg Bankseed29652006-10-02 02:18:02 -0700517 if (rv)
518 goto out_free;
519 }
520
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300521 rv = nfsd_get_nrthreads(npools, nthreads, net);
Greg Bankseed29652006-10-02 02:18:02 -0700522 if (rv)
523 goto out_free;
524
525 mesg = buf;
526 size = SIMPLE_TRANSACTION_LIMIT;
527 for (i = 0; i < npools && size > 0; i++) {
528 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
529 len = strlen(mesg);
530 size -= len;
531 mesg += len;
532 }
J. Bruce Fields413d63d7102009-07-28 11:37:25 -0400533 rv = mesg - buf;
Greg Bankseed29652006-10-02 02:18:02 -0700534out_free:
535 kfree(nthreads);
Neil Brownbedbdd82008-06-10 08:40:35 -0400536 mutex_unlock(&nfsd_mutex);
Greg Bankseed29652006-10-02 02:18:02 -0700537 return rv;
538}
539
Trond Myklebustff7d1172017-02-22 18:35:33 -0500540static ssize_t
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400541nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
542 const char *sep, unsigned vers, int minor)
Trond Myklebustff7d1172017-02-22 18:35:33 -0500543{
NeilBrownabcb4da2017-03-10 11:36:39 +1100544 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400545 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
Trond Myklebustff7d1172017-02-22 18:35:33 -0500546
NeilBrownabcb4da2017-03-10 11:36:39 +1100547 if (vers == 4 && minor >= 0 &&
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400548 !nfsd_minorversion(nn, minor, NFSD_TEST))
Trond Myklebustff7d1172017-02-22 18:35:33 -0500549 supported = false;
NeilBrownabcb4da2017-03-10 11:36:39 +1100550 if (minor == 0 && supported)
551 /*
552 * special case for backward compatability.
553 * +4.0 is never reported, it is implied by
554 * +4, unless -4.0 is present.
555 */
556 return 0;
Trond Myklebustff7d1172017-02-22 18:35:33 -0500557 return snprintf(buf, remaining, format, sep,
558 supported ? '+' : '-', vers, minor);
559}
560
Jeff Layton3dd98a32008-06-10 08:40:36 -0400561static ssize_t __write_versions(struct file *file, char *buf, size_t size)
NeilBrown70c3b762005-11-07 01:00:25 -0800562{
NeilBrown70c3b762005-11-07 01:00:25 -0800563 char *mesg = buf;
Benny Halevy8daf2202009-04-03 08:28:59 +0300564 char *vers, *minorp, sign;
Chuck Lever261758b2009-04-23 19:33:18 -0400565 int len, num, remaining;
NeilBrown70c3b762005-11-07 01:00:25 -0800566 ssize_t tlen = 0;
567 char *sep;
Al Viro244c7d42014-10-21 20:19:11 -0400568 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
NeilBrown70c3b762005-11-07 01:00:25 -0800569
570 if (size>0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300571 if (nn->nfsd_serv)
NeilBrown6658d3a2006-10-02 02:17:46 -0700572 /* Cannot change versions without updating
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300573 * nn->nfsd_serv->sv_xdrsize, and reallocing
NeilBrown6658d3a2006-10-02 02:17:46 -0700574 * rq_argp and rq_resp
575 */
NeilBrown70c3b762005-11-07 01:00:25 -0800576 return -EBUSY;
577 if (buf[size-1] != '\n')
578 return -EINVAL;
579 buf[size-1] = 0;
580
581 vers = mesg;
582 len = qword_get(&mesg, vers, size);
583 if (len <= 0) return -EINVAL;
584 do {
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500585 enum vers_op cmd;
NeilBrownabcb4da2017-03-10 11:36:39 +1100586 unsigned minor;
NeilBrown70c3b762005-11-07 01:00:25 -0800587 sign = *vers;
588 if (sign == '+' || sign == '-')
Benny Halevy8daf2202009-04-03 08:28:59 +0300589 num = simple_strtol((vers+1), &minorp, 0);
NeilBrown70c3b762005-11-07 01:00:25 -0800590 else
Benny Halevy8daf2202009-04-03 08:28:59 +0300591 num = simple_strtol(vers, &minorp, 0);
592 if (*minorp == '.') {
J. Bruce Fieldsff89be82013-01-23 18:25:01 -0500593 if (num != 4)
Benny Halevy8daf2202009-04-03 08:28:59 +0300594 return -EINVAL;
NeilBrowne35659f2016-12-21 14:32:19 +1100595 if (kstrtouint(minorp+1, 0, &minor) < 0)
Benny Halevy8daf2202009-04-03 08:28:59 +0300596 return -EINVAL;
NeilBrownabcb4da2017-03-10 11:36:39 +1100597 }
598
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500599 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
NeilBrown70c3b762005-11-07 01:00:25 -0800600 switch(num) {
601 case 2:
602 case 3:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400603 nfsd_vers(nn, num, cmd);
NeilBrown70c3b762005-11-07 01:00:25 -0800604 break;
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500605 case 4:
NeilBrownabcb4da2017-03-10 11:36:39 +1100606 if (*minorp == '.') {
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400607 if (nfsd_minorversion(nn, minor, cmd) < 0)
NeilBrownabcb4da2017-03-10 11:36:39 +1100608 return -EINVAL;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400609 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
NeilBrownabcb4da2017-03-10 11:36:39 +1100610 /*
611 * Either we have +4 and no minors are enabled,
612 * or we have -4 and at least one minor is enabled.
613 * In either case, propagate 'cmd' to all minors.
614 */
615 minor = 0;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400616 while (nfsd_minorversion(nn, minor, cmd) >= 0)
NeilBrownabcb4da2017-03-10 11:36:39 +1100617 minor++;
618 }
619 break;
NeilBrown70c3b762005-11-07 01:00:25 -0800620 default:
621 return -EINVAL;
622 }
623 vers += len + 1;
NeilBrown70c3b762005-11-07 01:00:25 -0800624 } while ((len = qword_get(&mesg, vers, size)) > 0);
625 /* If all get turned off, turn them back on, as
626 * having no versions is BAD
627 */
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400628 nfsd_reset_versions(nn);
NeilBrown70c3b762005-11-07 01:00:25 -0800629 }
Chuck Lever261758b2009-04-23 19:33:18 -0400630
NeilBrown70c3b762005-11-07 01:00:25 -0800631 /* Now write current state into reply buffer */
632 len = 0;
633 sep = "";
Chuck Lever261758b2009-04-23 19:33:18 -0400634 remaining = SIMPLE_TRANSACTION_LIMIT;
Trond Myklebustff7d1172017-02-22 18:35:33 -0500635 for (num=2 ; num <= 4 ; num++) {
NeilBrownabcb4da2017-03-10 11:36:39 +1100636 int minor;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400637 if (!nfsd_vers(nn, num, NFSD_AVAIL))
Trond Myklebustff7d1172017-02-22 18:35:33 -0500638 continue;
NeilBrownabcb4da2017-03-10 11:36:39 +1100639
640 minor = -1;
Trond Myklebustff7d1172017-02-22 18:35:33 -0500641 do {
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400642 len = nfsd_print_version_support(nn, buf, remaining,
Trond Myklebustff7d1172017-02-22 18:35:33 -0500643 sep, num, minor);
644 if (len >= remaining)
645 goto out;
646 remaining -= len;
647 buf += len;
648 tlen += len;
649 minor++;
NeilBrownabcb4da2017-03-10 11:36:39 +1100650 if (len)
651 sep = " ";
Trond Myklebustff7d1172017-02-22 18:35:33 -0500652 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
653 }
654out:
Chuck Lever261758b2009-04-23 19:33:18 -0400655 len = snprintf(buf, remaining, "\n");
Dan Carpenter818f2f52014-11-27 18:58:54 +0300656 if (len >= remaining)
Chuck Lever261758b2009-04-23 19:33:18 -0400657 return -EINVAL;
658 return tlen + len;
NeilBrown70c3b762005-11-07 01:00:25 -0800659}
660
Chuck Lever262a0982008-12-12 16:57:35 -0500661/**
662 * write_versions - Set or report the available NFS protocol versions
663 *
664 * Input:
665 * buf: ignored
666 * size: zero
667 * Output:
668 * On success: passed-in buffer filled with '\n'-terminated C
669 * string containing positive or negative integer
670 * values representing the current status of each
671 * protocol version;
672 * return code is the size in bytes of the string
673 * On error: return code is zero or a negative errno value
674 *
675 * OR
676 *
677 * Input:
678 * buf: C string containing whitespace-
679 * separated positive or negative
680 * integer values representing NFS
681 * protocol versions to enable ("+n")
682 * or disable ("-n")
683 * size: non-zero length of C string in @buf
684 * Output:
685 * On success: status of zero or more protocol versions has
686 * been updated; passed-in buffer filled with
687 * '\n'-terminated C string containing positive
688 * or negative integer values representing the
689 * current status of each protocol version;
690 * return code is the size in bytes of the string
691 * On error: return code is zero or a negative errno value
692 */
Jeff Layton3dd98a32008-06-10 08:40:36 -0400693static ssize_t write_versions(struct file *file, char *buf, size_t size)
694{
695 ssize_t rv;
696
697 mutex_lock(&nfsd_mutex);
698 rv = __write_versions(file, buf, size);
699 mutex_unlock(&nfsd_mutex);
700 return rv;
701}
702
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400703/*
Chuck Lever0a5372d2009-04-23 19:32:10 -0400704 * Zero-length write. Return a list of NFSD's current listener
705 * transports.
706 */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300707static ssize_t __write_ports_names(char *buf, struct net *net)
Chuck Lever0a5372d2009-04-23 19:32:10 -0400708{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300709 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
710
711 if (nn->nfsd_serv == NULL)
Chuck Lever0a5372d2009-04-23 19:32:10 -0400712 return 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300713 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
Chuck Lever0a5372d2009-04-23 19:32:10 -0400714}
715
716/*
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400717 * A single 'fd' number was written, in which case it must be for
718 * a socket of a supported family/protocol, and we use it as an
719 * nfsd listener.
720 */
Trond Myklebust4df493a2019-04-09 12:13:37 -0400721static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400722{
723 char *mesg = buf;
724 int fd, err;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300725 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400726
727 err = get_int(&mesg, &fd);
728 if (err != 0 || fd < 0)
729 return -EINVAL;
730
Stanislav Kinsbursky30646392014-02-26 16:50:01 +0300731 if (svc_alien_sock(net, fd)) {
732 printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
733 return -EINVAL;
734 }
735
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300736 err = nfsd_create_serv(net);
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400737 if (err != 0)
738 return err;
739
Trond Myklebust4df493a2019-04-09 12:13:37 -0400740 err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400741 if (err < 0) {
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400742 nfsd_destroy(net);
Jeff Layton78a8d7c2010-07-19 16:50:05 -0400743 return err;
744 }
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400745
Chuck Leverea068ba2009-04-23 19:32:18 -0400746 /* Decrease the count, but don't shut down the service */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300747 nn->nfsd_serv->sv_nrthreads--;
Chuck Leverea068ba2009-04-23 19:32:18 -0400748 return err;
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400749}
750
751/*
Chuck Lever4eb68c22009-04-23 19:31:40 -0400752 * A transport listener is added by writing it's transport name and
753 * a port number.
754 */
Trond Myklebust4df493a2019-04-09 12:13:37 -0400755static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
Chuck Lever4eb68c22009-04-23 19:31:40 -0400756{
757 char transport[16];
Chuck Lever37498292010-01-26 14:04:22 -0500758 struct svc_xprt *xprt;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400759 int port, err;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300760 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Chuck Lever4eb68c22009-04-23 19:31:40 -0400761
J. Bruce Fieldsa10fded2012-08-14 17:48:39 -0400762 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
Chuck Lever4eb68c22009-04-23 19:31:40 -0400763 return -EINVAL;
764
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700765 if (port < 1 || port > USHRT_MAX)
Chuck Lever4eb68c22009-04-23 19:31:40 -0400766 return -EINVAL;
767
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300768 err = nfsd_create_serv(net);
Chuck Lever4eb68c22009-04-23 19:31:40 -0400769 if (err != 0)
770 return err;
771
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300772 err = svc_create_xprt(nn->nfsd_serv, transport, net,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400773 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
Chuck Lever68717902010-01-26 14:04:13 -0500774 if (err < 0)
Chuck Lever37498292010-01-26 14:04:22 -0500775 goto out_err;
776
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300777 err = svc_create_xprt(nn->nfsd_serv, transport, net,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400778 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
Chuck Lever37498292010-01-26 14:04:22 -0500779 if (err < 0 && err != -EAFNOSUPPORT)
780 goto out_close;
Jeff Layton0cd14a02010-07-19 16:50:06 -0400781
782 /* Decrease the count, but don't shut down the service */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300783 nn->nfsd_serv->sv_nrthreads--;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400784 return 0;
Chuck Lever37498292010-01-26 14:04:22 -0500785out_close:
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300786 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
Chuck Lever37498292010-01-26 14:04:22 -0500787 if (xprt != NULL) {
788 svc_close_xprt(xprt);
789 svc_xprt_put(xprt);
790 }
791out_err:
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400792 nfsd_destroy(net);
Chuck Lever37498292010-01-26 14:04:22 -0500793 return err;
Chuck Lever4eb68c22009-04-23 19:31:40 -0400794}
795
Stanislav Kinsbursky08160352012-12-10 12:19:35 +0300796static ssize_t __write_ports(struct file *file, char *buf, size_t size,
797 struct net *net)
NeilBrown80212d52006-10-02 02:17:47 -0700798{
Chuck Lever0a5372d2009-04-23 19:32:10 -0400799 if (size == 0)
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300800 return __write_ports_names(buf, net);
Chuck Lever0b7c2f62009-04-23 19:31:55 -0400801
802 if (isdigit(buf[0]))
Trond Myklebust4df493a2019-04-09 12:13:37 -0400803 return __write_ports_addfd(buf, net, file->f_cred);
Chuck Lever82d56592009-04-23 19:31:48 -0400804
Chuck Lever4eb68c22009-04-23 19:31:40 -0400805 if (isalpha(buf[0]))
Trond Myklebust4df493a2019-04-09 12:13:37 -0400806 return __write_ports_addxprt(buf, net, file->f_cred);
Chuck Lever4cd5dc72009-04-23 19:31:32 -0400807
NeilBrownb41b66d2006-10-02 02:17:48 -0700808 return -EINVAL;
NeilBrown80212d52006-10-02 02:17:47 -0700809}
810
Chuck Lever262a0982008-12-12 16:57:35 -0500811/**
812 * write_ports - Pass a socket file descriptor or transport name to listen on
813 *
814 * Input:
815 * buf: ignored
816 * size: zero
817 * Output:
818 * On success: passed-in buffer filled with a '\n'-terminated C
819 * string containing a whitespace-separated list of
820 * named NFSD listeners;
821 * return code is the size in bytes of the string
822 * On error: return code is zero or a negative errno value
823 *
824 * OR
825 *
826 * Input:
827 * buf: C string containing an unsigned
828 * integer value representing a bound
829 * but unconnected socket that is to be
Chuck Leverc71206a2009-04-23 19:32:03 -0400830 * used as an NFSD listener; listen(3)
831 * must be called for a SOCK_STREAM
832 * socket, otherwise it is ignored
Chuck Lever262a0982008-12-12 16:57:35 -0500833 * size: non-zero length of C string in @buf
834 * Output:
835 * On success: NFS service is started;
836 * passed-in buffer filled with a '\n'-terminated C
837 * string containing a unique alphanumeric name of
838 * the listener;
839 * return code is the size in bytes of the string
840 * On error: return code is a negative errno value
841 *
842 * OR
843 *
844 * Input:
Chuck Lever262a0982008-12-12 16:57:35 -0500845 * buf: C string containing a transport
846 * name and an unsigned integer value
847 * representing the port to listen on,
848 * separated by whitespace
849 * size: non-zero length of C string in @buf
850 * Output:
851 * On success: returns zero; NFS service is started
852 * On error: return code is a negative errno value
Chuck Lever262a0982008-12-12 16:57:35 -0500853 */
Neil Brownbedbdd82008-06-10 08:40:35 -0400854static ssize_t write_ports(struct file *file, char *buf, size_t size)
855{
856 ssize_t rv;
Jeff Layton3dd98a32008-06-10 08:40:36 -0400857
Neil Brownbedbdd82008-06-10 08:40:35 -0400858 mutex_lock(&nfsd_mutex);
Al Viro244c7d42014-10-21 20:19:11 -0400859 rv = __write_ports(file, buf, size, netns(file));
Neil Brownbedbdd82008-06-10 08:40:35 -0400860 mutex_unlock(&nfsd_mutex);
861 return rv;
862}
863
864
NeilBrown596bbe52006-10-04 02:15:48 -0700865int nfsd_max_blksize;
866
Chuck Lever262a0982008-12-12 16:57:35 -0500867/**
868 * write_maxblksize - Set or report the current NFS blksize
869 *
870 * Input:
871 * buf: ignored
872 * size: zero
873 *
874 * OR
875 *
876 * Input:
877 * buf: C string containing an unsigned
878 * integer value representing the new
879 * NFS blksize
880 * size: non-zero length of C string in @buf
881 * Output:
882 * On success: passed-in buffer filled with '\n'-terminated C string
883 * containing numeric value of the current NFS blksize
884 * setting;
885 * return code is the size in bytes of the string
886 * On error: return code is zero or a negative errno value
887 */
NeilBrown596bbe52006-10-04 02:15:48 -0700888static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
889{
890 char *mesg = buf;
Al Viro244c7d42014-10-21 20:19:11 -0400891 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300892
NeilBrown596bbe52006-10-04 02:15:48 -0700893 if (size > 0) {
894 int bsize;
895 int rv = get_int(&mesg, &bsize);
896 if (rv)
897 return rv;
898 /* force bsize into allowed range and
899 * required alignment.
900 */
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800901 bsize = max_t(int, bsize, 1024);
902 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
NeilBrown596bbe52006-10-04 02:15:48 -0700903 bsize &= ~(1024-1);
Neil Brownbedbdd82008-06-10 08:40:35 -0400904 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300905 if (nn->nfsd_serv) {
Neil Brownbedbdd82008-06-10 08:40:35 -0400906 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -0700907 return -EBUSY;
908 }
909 nfsd_max_blksize = bsize;
Neil Brownbedbdd82008-06-10 08:40:35 -0400910 mutex_unlock(&nfsd_mutex);
NeilBrown596bbe52006-10-04 02:15:48 -0700911 }
Chuck Levere06b6402009-04-23 19:33:25 -0400912
913 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
914 nfsd_max_blksize);
NeilBrown596bbe52006-10-04 02:15:48 -0700915}
916
Jeff Layton5b8db002014-07-02 16:11:22 -0400917/**
918 * write_maxconn - Set or report the current max number of connections
919 *
920 * Input:
921 * buf: ignored
922 * size: zero
923 * OR
924 *
925 * Input:
926 * buf: C string containing an unsigned
927 * integer value representing the new
928 * number of max connections
929 * size: non-zero length of C string in @buf
930 * Output:
931 * On success: passed-in buffer filled with '\n'-terminated C string
932 * containing numeric value of max_connections setting
933 * for this net namespace;
934 * return code is the size in bytes of the string
935 * On error: return code is zero or a negative errno value
936 */
937static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
938{
939 char *mesg = buf;
Al Viro244c7d42014-10-21 20:19:11 -0400940 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Jeff Layton5b8db002014-07-02 16:11:22 -0400941 unsigned int maxconn = nn->max_connections;
942
943 if (size > 0) {
944 int rv = get_uint(&mesg, &maxconn);
945
946 if (rv)
947 return rv;
948 nn->max_connections = maxconn;
949 }
950
951 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
952}
953
NeilBrown70c3b762005-11-07 01:00:25 -0800954#ifdef CONFIG_NFSD_V4
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300955static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
956 time_t *time, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 char *mesg = buf;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500959 int rv, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (size > 0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300962 if (nn->nfsd_serv)
Jeff Layton3dd98a32008-06-10 08:40:36 -0400963 return -EBUSY;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500964 rv = get_int(&mesg, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (rv)
966 return rv;
J. Bruce Fieldse7b184f2010-03-02 11:18:40 -0500967 /*
968 * Some sanity checking. We don't have a reason for
969 * these particular numbers, but problems with the
970 * extremes are:
971 * - Too short: the briefest network outage may
972 * cause clients to lose all their locks. Also,
973 * the frequent polling may be wasteful.
974 * - Too long: do you really want reboot recovery
975 * to take more than an hour? Or to make other
976 * clients wait an hour before being able to
977 * revoke a dead client's locks?
978 */
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500979 if (i < 10 || i > 3600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -EINVAL;
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500981 *time = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Chuck Levere06b6402009-04-23 19:33:25 -0400983
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500984 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
985}
986
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300987static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
988 time_t *time, struct nfsd_net *nn)
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500989{
990 ssize_t rv;
991
992 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300993 rv = __nfsd4_write_time(file, buf, size, time, nn);
J. Bruce Fieldsf0135742010-03-01 19:32:36 -0500994 mutex_unlock(&nfsd_mutex);
995 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Chuck Lever262a0982008-12-12 16:57:35 -0500998/**
999 * write_leasetime - Set or report the current NFSv4 lease time
1000 *
1001 * Input:
1002 * buf: ignored
1003 * size: zero
1004 *
1005 * OR
1006 *
1007 * Input:
1008 * buf: C string containing an unsigned
1009 * integer value representing the new
1010 * NFSv4 lease expiry time
1011 * size: non-zero length of C string in @buf
1012 * Output:
1013 * On success: passed-in buffer filled with '\n'-terminated C
1014 * string containing unsigned integer value of the
1015 * current lease expiry time;
1016 * return code is the size in bytes of the string
1017 * On error: return code is zero or a negative errno value
1018 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001019static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1020{
Al Viro244c7d42014-10-21 20:19:11 -04001021 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001022 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
Jeff Layton3dd98a32008-06-10 08:40:36 -04001023}
1024
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -05001025/**
1026 * write_gracetime - Set or report current NFSv4 grace period time
1027 *
1028 * As above, but sets the time of the NFSv4 grace period.
1029 *
1030 * Note this should never be set to less than the *previous*
1031 * lease-period time, but we don't try to enforce this. (In the common
1032 * case (a new boot), we don't know what the previous lease time was
1033 * anyway.)
1034 */
1035static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1036{
Al Viro244c7d42014-10-21 20:19:11 -04001037 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001038 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -05001039}
1040
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001041static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1042 struct nfsd_net *nn)
NeilBrown0964a3d2005-06-23 22:04:32 -07001043{
1044 char *mesg = buf;
1045 char *recdir;
1046 int len, status;
1047
Jeff Layton3dd98a32008-06-10 08:40:36 -04001048 if (size > 0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001049 if (nn->nfsd_serv)
Jeff Layton3dd98a32008-06-10 08:40:36 -04001050 return -EBUSY;
1051 if (size > PATH_MAX || buf[size-1] != '\n')
1052 return -EINVAL;
1053 buf[size-1] = 0;
NeilBrown0964a3d2005-06-23 22:04:32 -07001054
Jeff Layton3dd98a32008-06-10 08:40:36 -04001055 recdir = mesg;
1056 len = qword_get(&mesg, recdir, size);
1057 if (len <= 0)
1058 return -EINVAL;
NeilBrown0964a3d2005-06-23 22:04:32 -07001059
Jeff Layton3dd98a32008-06-10 08:40:36 -04001060 status = nfs4_reset_recoverydir(recdir);
Andi Kleen69049962010-07-20 15:24:27 -07001061 if (status)
1062 return status;
Jeff Layton3dd98a32008-06-10 08:40:36 -04001063 }
Chuck Lever3d72ab82009-04-23 19:33:10 -04001064
1065 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1066 nfs4_recoverydir());
NeilBrown0964a3d2005-06-23 22:04:32 -07001067}
Jeff Layton3dd98a32008-06-10 08:40:36 -04001068
Chuck Lever262a0982008-12-12 16:57:35 -05001069/**
1070 * write_recoverydir - Set or report the pathname of the recovery directory
1071 *
1072 * Input:
1073 * buf: ignored
1074 * size: zero
1075 *
1076 * OR
1077 *
1078 * Input:
1079 * buf: C string containing the pathname
1080 * of the directory on a local file
1081 * system containing permanent NFSv4
1082 * recovery data
1083 * size: non-zero length of C string in @buf
1084 * Output:
1085 * On success: passed-in buffer filled with '\n'-terminated C string
1086 * containing the current recovery pathname setting;
1087 * return code is the size in bytes of the string
1088 * On error: return code is zero or a negative errno value
1089 */
Jeff Layton3dd98a32008-06-10 08:40:36 -04001090static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1091{
1092 ssize_t rv;
Al Viro244c7d42014-10-21 20:19:11 -04001093 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Jeff Layton3dd98a32008-06-10 08:40:36 -04001094
1095 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001096 rv = __write_recoverydir(file, buf, size, nn);
Jeff Layton3dd98a32008-06-10 08:40:36 -04001097 mutex_unlock(&nfsd_mutex);
1098 return rv;
1099}
1100
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04001101/**
1102 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1103 *
1104 * Input:
1105 * buf: ignored
1106 * size: zero
1107 * OR
1108 *
1109 * Input:
1110 * buf: any value
1111 * size: non-zero length of C string in @buf
1112 * Output:
1113 * passed-in buffer filled with "Y" or "N" with a newline
1114 * and NULL-terminated C string. This indicates whether
1115 * the grace period has ended in the current net
1116 * namespace. Return code is the size in bytes of the
1117 * string. Writing a string that starts with 'Y', 'y', or
1118 * '1' to the file will end the grace period for nfsd's v4
1119 * lock manager.
1120 */
1121static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1122{
Al Viro244c7d42014-10-21 20:19:11 -04001123 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04001124
1125 if (size > 0) {
1126 switch(buf[0]) {
1127 case 'Y':
1128 case 'y':
1129 case '1':
Yihao Wudd838822019-03-06 21:03:50 +08001130 if (!nn->nfsd_serv)
J. Bruce Fields62a063b2018-11-27 15:54:17 -05001131 return -EBUSY;
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04001132 nfsd4_end_grace(nn);
1133 break;
1134 default:
1135 return -EINVAL;
1136 }
1137 }
1138
1139 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1140 nn->grace_ended ? 'Y' : 'N');
1141}
1142
NeilBrown70c3b762005-11-07 01:00:25 -08001143#endif
NeilBrown0964a3d2005-06-23 22:04:32 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*----------------------------------------------------------------------------*/
1146/*
1147 * populating the filesystem.
1148 */
1149
1150static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1151{
Eric Biggerscda37122017-03-25 21:15:37 -07001152 static const struct tree_descr nfsd_files[] = {
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +03001153 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
J. Bruce Fieldse8e87532009-12-14 12:53:32 -05001154 [NFSD_Export_features] = {"export_features",
1155 &export_features_operations, S_IRUGO},
Wendy Cheng4373ea82008-01-17 11:10:12 -05001156 [NFSD_FO_UnlockIP] = {"unlock_ip",
1157 &transaction_ops, S_IWUSR|S_IRUSR},
Wendy Cheng17efa372008-01-17 11:10:12 -05001158 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1159 &transaction_ops, S_IWUSR|S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1161 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Bankseed29652006-10-02 02:18:02 -07001162 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
Greg Banks03cf6c92009-01-13 21:26:36 +11001163 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
Jeff Laytona2f999a2013-03-27 10:15:38 -04001164 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
NeilBrown70c3b762005-11-07 01:00:25 -08001165 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown80212d52006-10-02 02:17:47 -07001166 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
NeilBrown596bbe52006-10-04 02:15:48 -07001167 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
Jeff Layton5b8db002014-07-02 16:11:22 -04001168 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
J. Bruce Fieldsb084f592011-05-31 12:24:58 -04001169#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
Kevin Coffmanb0b0c0a2011-03-02 19:51:42 -05001170 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
J. Bruce Fieldsb084f592011-05-31 12:24:58 -04001171#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#ifdef CONFIG_NFSD_V4
1173 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -05001174 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
NeilBrown0964a3d2005-06-23 22:04:32 -07001175 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04001176 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#endif
1178 /* last one */ {""}
1179 };
Eric W. Biedermand91ee872016-05-23 14:51:59 -05001180 get_net(sb->s_fs_info);
1181 return simple_fill_super(sb, 0x6e667364, nfsd_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Al Virofc14f2f2010-07-25 01:48:30 +04001184static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1185 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Eric W. Biedermand91ee872016-05-23 14:51:59 -05001187 struct net *net = current->nsproxy->net_ns;
1188 return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super);
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001189}
1190
1191static void nfsd_umount(struct super_block *sb)
1192{
1193 struct net *net = sb->s_fs_info;
1194
1195 kill_litter_super(sb);
1196 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static struct file_system_type nfsd_fs_type = {
1200 .owner = THIS_MODULE,
1201 .name = "nfsd",
Al Virofc14f2f2010-07-25 01:48:30 +04001202 .mount = nfsd_mount,
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001203 .kill_sb = nfsd_umount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001205MODULE_ALIAS_FS("nfsd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001207#ifdef CONFIG_PROC_FS
1208static int create_proc_exports_entry(void)
1209{
1210 struct proc_dir_entry *entry;
1211
1212 entry = proc_mkdir("fs/nfs", NULL);
1213 if (!entry)
1214 return -ENOMEM;
Stanislav Kinsbursky96d851c2013-02-01 15:56:17 +03001215 entry = proc_create("exports", 0, entry,
1216 &exports_proc_operations);
fanchaotingff7c4b32013-03-27 16:31:18 +08001217 if (!entry) {
1218 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001219 return -ENOMEM;
fanchaotingff7c4b32013-03-27 16:31:18 +08001220 }
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001221 return 0;
1222}
1223#else /* CONFIG_PROC_FS */
1224static int create_proc_exports_entry(void)
1225{
1226 return 0;
1227}
1228#endif
1229
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +03001230unsigned int nfsd_net_id;
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001231
1232static __net_init int nfsd_init_net(struct net *net)
1233{
1234 int retval;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03001235 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001236
1237 retval = nfsd_export_init(net);
1238 if (retval)
1239 goto out_export_error;
Stanislav Kinsburskyf69adb22012-04-11 17:33:05 +04001240 retval = nfsd_idmap_init(net);
1241 if (retval)
1242 goto out_idmap_error;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04001243 nn->nfsd_versions = NULL;
1244 nn->nfsd4_minorversions = NULL;
J. Bruce Fields3bf6b572019-02-14 12:33:19 -05001245 nn->nfsd4_lease = 90; /* default lease time */
1246 nn->nfsd4_grace = 90;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04001247 nn->somebody_reclaimed = false;
Scott Mayhew362063a2019-03-26 18:06:28 -04001248 nn->track_reclaim_completes = false;
J. Bruce Fieldsebd7c722016-09-12 16:00:47 -04001249 nn->clverifier_counter = prandom_u32();
1250 nn->clientid_counter = prandom_u32();
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001251 nn->s2s_cp_cl_id = nn->clientid_counter++;
Vasily Averin2317dc52017-11-10 10:19:35 +03001252
1253 atomic_set(&nn->ntf_refcnt, 0);
1254 init_waitqueue_head(&nn->ntf_wq);
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001255 return 0;
1256
Stanislav Kinsburskyf69adb22012-04-11 17:33:05 +04001257out_idmap_error:
1258 nfsd_export_shutdown(net);
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001259out_export_error:
1260 return retval;
1261}
1262
1263static __net_exit void nfsd_exit_net(struct net *net)
1264{
Stanislav Kinsburskyf69adb22012-04-11 17:33:05 +04001265 nfsd_idmap_shutdown(net);
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001266 nfsd_export_shutdown(net);
Trond Myklebuste333f3b2019-04-09 11:46:19 -04001267 nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001268}
1269
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001270static struct pernet_operations nfsd_net_ops = {
Stanislav Kinsbursky5717e012012-04-11 15:13:35 +04001271 .init = nfsd_init_net,
1272 .exit = nfsd_exit_net,
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001273 .id = &nfsd_net_id,
1274 .size = sizeof(struct nfsd_net),
1275};
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277static int __init init_nfsd(void)
1278{
1279 int retval;
1280 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1281
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001282 retval = register_pernet_subsys(&nfsd_net_ops);
1283 if (retval < 0)
Giuseppe Cantavenerabb7ffbf2015-04-20 18:00:08 +02001284 return retval;
1285 retval = register_cld_notifier();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001286 if (retval)
Jeff Layton7ea34ac2012-03-21 09:52:05 -04001287 goto out_unregister_pernet;
Giuseppe Cantavenerabb7ffbf2015-04-20 18:00:08 +02001288 retval = nfsd4_init_slabs();
1289 if (retval)
1290 goto out_unregister_notifier;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001291 retval = nfsd4_init_pnfs();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001292 if (retval)
1293 goto out_free_slabs;
Greg Kroah-Hartman15b6ff92019-06-12 17:26:03 +02001294 nfsd_fault_inject_init(); /* nfsd fault injection controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 nfsd_stat_init(); /* Statistics */
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001296 retval = nfsd_reply_cache_init();
1297 if (retval)
1298 goto out_free_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 nfsd_lockd_init(); /* lockd->nfsd callbacks */
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001300 retval = create_proc_exports_entry();
1301 if (retval)
Stanislav Kinsburskyf69adb22012-04-11 17:33:05 +04001302 goto out_free_lockd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 retval = register_filesystem(&nfsd_fs_type);
J. Bruce Fields26808d32007-11-09 13:44:06 -05001304 if (retval)
1305 goto out_free_all;
1306 return 0;
1307out_free_all:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001308 remove_proc_entry("fs/nfs/exports", NULL);
1309 remove_proc_entry("fs/nfs", NULL);
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -05001310out_free_lockd:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001311 nfsd_lockd_shutdown();
J. Bruce Fieldse331f602007-11-12 17:32:21 -05001312 nfsd_reply_cache_shutdown();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001313out_free_stat:
1314 nfsd_stat_shutdown();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001315 nfsd_fault_inject_cleanup();
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001316 nfsd4_exit_pnfs();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001317out_free_slabs:
J. Bruce Fields26808d32007-11-09 13:44:06 -05001318 nfsd4_free_slabs();
Jeff Layton813fd322012-03-21 09:52:08 -04001319out_unregister_notifier:
Jeff Layton797a9d72012-03-29 07:52:49 -04001320 unregister_cld_notifier();
Giuseppe Cantavenerabb7ffbf2015-04-20 18:00:08 +02001321out_unregister_pernet:
1322 unregister_pernet_subsys(&nfsd_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return retval;
1324}
1325
1326static void __exit exit_nfsd(void)
1327{
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -05001328 nfsd_reply_cache_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 remove_proc_entry("fs/nfs/exports", NULL);
1330 remove_proc_entry("fs/nfs", NULL);
1331 nfsd_stat_shutdown();
1332 nfsd_lockd_shutdown();
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001333 nfsd4_free_slabs();
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001334 nfsd4_exit_pnfs();
Bryan Schumaker65178db2011-11-01 13:35:21 -04001335 nfsd_fault_inject_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 unregister_filesystem(&nfsd_fs_type);
Jeff Layton797a9d72012-03-29 07:52:49 -04001337 unregister_cld_notifier();
Giuseppe Cantavenerabb7ffbf2015-04-20 18:00:08 +02001338 unregister_pernet_subsys(&nfsd_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1342MODULE_LICENSE("GPL");
1343module_init(init_nfsd)
1344module_exit(exit_nfsd)