Linux 6.16-rc6
[linux-2.6-block.git] / fs / nfs / super.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
f7b422b1
DH
2/*
3 * linux/fs/nfs/super.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
6 *
7 * nfs superblock handling functions
8 *
526719ba 9 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
f7b422b1
DH
10 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 *
12 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
13 * J.S.Peatfield@damtp.cam.ac.uk
14 *
15 * Split from inode.c by David Howells <dhowells@redhat.com>
16 *
54ceac45
DH
17 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
18 * particular server are held in the same superblock
19 * - NFS superblocks can have several effective roots to the dentry tree
20 * - directory type roots are spliced into the tree when a path from one root reaches the root
21 * of another (see nfs_lookup())
f7b422b1
DH
22 */
23
f7b422b1
DH
24#include <linux/module.h>
25#include <linux/init.h>
26
27#include <linux/time.h>
28#include <linux/kernel.h>
29#include <linux/mm.h>
30#include <linux/string.h>
31#include <linux/stat.h>
32#include <linux/errno.h>
33#include <linux/unistd.h>
34#include <linux/sunrpc/clnt.h>
5976687a 35#include <linux/sunrpc/addr.h>
f7b422b1
DH
36#include <linux/sunrpc/stats.h>
37#include <linux/sunrpc/metrics.h>
0896a725 38#include <linux/sunrpc/xprtsock.h>
2cf7ff7a 39#include <linux/sunrpc/xprtrdma.h>
f7b422b1
DH
40#include <linux/nfs_fs.h>
41#include <linux/nfs_mount.h>
42#include <linux/nfs4_mount.h>
43#include <linux/lockd/bind.h>
f7b422b1
DH
44#include <linux/seq_file.h>
45#include <linux/mount.h>
c02d7adf 46#include <linux/namei.h>
f7b422b1
DH
47#include <linux/vfs.h>
48#include <linux/inet.h>
fd00a8ff 49#include <linux/in6.h>
a017ad13 50#include <linux/sched.h>
5a0e3ad6 51#include <linux/slab.h>
fd00a8ff 52#include <net/ipv6.h>
d8e7748a 53#include <linux/netdevice.h>
f7b422b1 54#include <linux/nfs_xdr.h>
b5d5dfbd 55#include <linux/magic.h>
bf0fd768 56#include <linux/parser.h>
e50a7a1a 57#include <linux/nsproxy.h>
2446ab60 58#include <linux/rcupdate.h>
f7b422b1 59
7c0f6ba6 60#include <linux/uaccess.h>
0cfcd405 61#include <linux/nfs_ssc.h>
f7b422b1 62
c8407f2e
CL
63#include <uapi/linux/tls.h>
64
f7b422b1
DH
65#include "nfs4_fs.h"
66#include "callback.h"
67#include "delegation.h"
68#include "iostat.h"
69#include "internal.h"
08734048 70#include "fscache.h"
76e697ba 71#include "nfs4session.h"
ae50c0b5 72#include "pnfs.h"
ab7017a3 73#include "nfs.h"
1c725118
BC
74#include "netns.h"
75#include "sysfs.h"
bedb4e60 76#include "nfs4idmap.h"
f7b422b1
DH
77
78#define NFSDBG_FACILITY NFSDBG_VFS
bf0fd768 79
6a74490d 80const struct super_operations nfs_sops = {
f7b422b1 81 .alloc_inode = nfs_alloc_inode,
ca1a199e 82 .free_inode = nfs_free_inode,
f7b422b1 83 .write_inode = nfs_write_inode,
eed99357 84 .drop_inode = nfs_drop_inode,
f7b422b1 85 .statfs = nfs_statfs,
b57922d9 86 .evict_inode = nfs_evict_inode,
f7b422b1
DH
87 .umount_begin = nfs_umount_begin,
88 .show_options = nfs_show_options,
c7f404b4
AV
89 .show_devname = nfs_show_devname,
90 .show_path = nfs_show_path,
f7b422b1
DH
91 .show_stats = nfs_show_stats,
92};
ddda8e0a 93EXPORT_SYMBOL_GPL(nfs_sops);
f7b422b1 94
02591f9f 95#ifdef CONFIG_NFS_V4_2
0cfcd405
DN
96static const struct nfs_ssc_client_ops nfs_ssc_clnt_ops_tbl = {
97 .sco_sb_deactive = nfs_sb_deactive,
98};
02591f9f 99#endif
0cfcd405 100
89d77c8f 101#if IS_ENABLED(CONFIG_NFS_V4)
1ae811ee 102static int __init register_nfs4_fs(void)
103{
104 return register_filesystem(&nfs4_fs_type);
105}
106
107static void unregister_nfs4_fs(void)
108{
109 unregister_filesystem(&nfs4_fs_type);
110}
111#else
112static int __init register_nfs4_fs(void)
113{
114 return 0;
115}
116
117static void unregister_nfs4_fs(void)
118{
119}
f7b422b1
DH
120#endif
121
02591f9f 122#ifdef CONFIG_NFS_V4_2
0cfcd405
DN
123static void nfs_ssc_register_ops(void)
124{
125 nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
126}
127
128static void nfs_ssc_unregister_ops(void)
129{
130 nfs_ssc_unregister(&nfs_ssc_clnt_ops_tbl);
131}
02591f9f 132#endif /* CONFIG_NFS_V4_2 */
0cfcd405 133
777fc8f1 134static struct shrinker *acl_shrinker;
979df72e 135
f7b422b1
DH
136/*
137 * Register the NFS filesystems
138 */
139int __init register_nfs_fs(void)
140{
141 int ret;
142
143 ret = register_filesystem(&nfs_fs_type);
144 if (ret < 0)
145 goto error_0;
146
1ae811ee 147 ret = register_nfs4_fs();
f7b422b1
DH
148 if (ret < 0)
149 goto error_1;
1ae811ee 150
151 ret = nfs_register_sysctl();
152 if (ret < 0)
153 goto error_2;
777fc8f1
QZ
154
155 acl_shrinker = shrinker_alloc(0, "nfs-acl");
156 if (!acl_shrinker) {
157 ret = -ENOMEM;
5ef8d792 158 goto error_3;
777fc8f1
QZ
159 }
160
161 acl_shrinker->count_objects = nfs_access_cache_count;
162 acl_shrinker->scan_objects = nfs_access_cache_scan;
163
164 shrinker_register(acl_shrinker);
165
02591f9f 166#ifdef CONFIG_NFS_V4_2
0cfcd405 167 nfs_ssc_register_ops();
02591f9f 168#endif
f7b422b1 169 return 0;
5ef8d792
KM
170error_3:
171 nfs_unregister_sysctl();
1ae811ee 172error_2:
173 unregister_nfs4_fs();
f7b422b1
DH
174error_1:
175 unregister_filesystem(&nfs_fs_type);
f7b422b1
DH
176error_0:
177 return ret;
178}
179
180/*
181 * Unregister the NFS filesystems
182 */
183void __exit unregister_nfs_fs(void)
184{
777fc8f1 185 shrinker_free(acl_shrinker);
49af7ee1 186 nfs_unregister_sysctl();
1ae811ee 187 unregister_nfs4_fs();
02591f9f 188#ifdef CONFIG_NFS_V4_2
0cfcd405 189 nfs_ssc_unregister_ops();
02591f9f 190#endif
f7b422b1
DH
191 unregister_filesystem(&nfs_fs_type);
192}
193
ea7c38fe 194bool nfs_sb_active(struct super_block *sb)
ef818a28 195{
1daef0a8 196 struct nfs_server *server = NFS_SB(sb);
ef818a28 197
ea7c38fe
TM
198 if (!atomic_inc_not_zero(&sb->s_active))
199 return false;
200 if (atomic_inc_return(&server->active) != 1)
201 atomic_dec(&sb->s_active);
202 return true;
ef818a28 203}
89d77c8f 204EXPORT_SYMBOL_GPL(nfs_sb_active);
ef818a28 205
1daef0a8 206void nfs_sb_deactive(struct super_block *sb)
ef818a28
SD
207{
208 struct nfs_server *server = NFS_SB(sb);
1daef0a8
TM
209
210 if (atomic_dec_and_test(&server->active))
211 deactivate_super(sb);
ef818a28 212}
89d77c8f 213EXPORT_SYMBOL_GPL(nfs_sb_deactive);
ef818a28 214
3c9e502b
TM
215static int __nfs_list_for_each_server(struct list_head *head,
216 int (*fn)(struct nfs_server *, void *),
217 void *data)
218{
219 struct nfs_server *server, *last = NULL;
220 int ret = 0;
221
222 rcu_read_lock();
223 list_for_each_entry_rcu(server, head, client_link) {
9c07b75b 224 if (!(server->super && nfs_sb_active(server->super)))
3c9e502b
TM
225 continue;
226 rcu_read_unlock();
227 if (last)
228 nfs_sb_deactive(last->super);
229 last = server;
230 ret = fn(server, data);
231 if (ret)
232 goto out;
a017ad13 233 cond_resched();
3c9e502b
TM
234 rcu_read_lock();
235 }
236 rcu_read_unlock();
237out:
238 if (last)
239 nfs_sb_deactive(last->super);
240 return ret;
241}
242
243int nfs_client_for_each_server(struct nfs_client *clp,
244 int (*fn)(struct nfs_server *, void *),
245 void *data)
246{
247 return __nfs_list_for_each_server(&clp->cl_superblocks, fn, data);
248}
249EXPORT_SYMBOL_GPL(nfs_client_for_each_server);
250
f7b422b1
DH
251/*
252 * Deliver file system statistics to userspace
253 */
fbdefd64 254int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
f7b422b1 255{
0c7d90cf 256 struct nfs_server *server = NFS_SB(dentry->d_sb);
f7b422b1
DH
257 unsigned char blockbits;
258 unsigned long blockres;
2b0143b5 259 struct nfs_fh *fh = NFS_FH(d_inode(dentry));
ca7e9a0d
TM
260 struct nfs_fsstat res;
261 int error = -ENOMEM;
262
263 res.fattr = nfs_alloc_fattr();
264 if (res.fattr == NULL)
265 goto out_err;
f7b422b1 266
8fa5c000 267 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
fbf3fdd2
MZ
268 if (unlikely(error == -ESTALE)) {
269 struct dentry *pd_dentry;
270
271 pd_dentry = dget_parent(dentry);
1cfb7072
AV
272 nfs_zap_caches(d_inode(pd_dentry));
273 dput(pd_dentry);
fbf3fdd2 274 }
ca7e9a0d 275 nfs_free_fattr(res.fattr);
f7b422b1
DH
276 if (error < 0)
277 goto out_err;
ca7e9a0d 278
1a0ba9ae 279 buf->f_type = NFS_SUPER_MAGIC;
f7b422b1
DH
280
281 /*
282 * Current versions of glibc do not correctly handle the
283 * case where f_frsize != f_bsize. Eventually we want to
284 * report the value of wtmult in this field.
285 */
0c7d90cf 286 buf->f_frsize = dentry->d_sb->s_blocksize;
f7b422b1
DH
287
288 /*
289 * On most *nix systems, f_blocks, f_bfree, and f_bavail
290 * are reported in units of f_frsize. Linux hasn't had
291 * an f_frsize field in its statfs struct until recently,
292 * thus historically Linux's sys_statfs reports these
293 * fields in units of f_bsize.
294 */
0c7d90cf
DH
295 buf->f_bsize = dentry->d_sb->s_blocksize;
296 blockbits = dentry->d_sb->s_blocksize_bits;
f7b422b1
DH
297 blockres = (1 << blockbits) - 1;
298 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
299 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
300 buf->f_bavail = (res.abytes + blockres) >> blockbits;
301
302 buf->f_files = res.tfiles;
303 buf->f_ffree = res.afiles;
304
305 buf->f_namelen = server->namelen;
1a0ba9ae 306
f7b422b1
DH
307 return 0;
308
309 out_err:
3110ff80 310 dprintk("%s: statfs error = %d\n", __func__, -error);
1a0ba9ae 311 return error;
f7b422b1 312}
89d77c8f 313EXPORT_SYMBOL_GPL(nfs_statfs);
f7b422b1 314
7d4e2747
DH
315/*
316 * Map the security flavour number to a name
317 */
81039f1f
TM
318static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
319{
7d4e2747 320 static const struct {
81039f1f
TM
321 rpc_authflavor_t flavour;
322 const char *str;
4d4b69dd
WAA
323 } sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
324 /* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
81039f1f
TM
325 { RPC_AUTH_NULL, "null" },
326 { RPC_AUTH_UNIX, "sys" },
327 { RPC_AUTH_GSS_KRB5, "krb5" },
328 { RPC_AUTH_GSS_KRB5I, "krb5i" },
329 { RPC_AUTH_GSS_KRB5P, "krb5p" },
330 { RPC_AUTH_GSS_LKEY, "lkey" },
331 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
332 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
333 { RPC_AUTH_GSS_SPKM, "spkm" },
334 { RPC_AUTH_GSS_SPKMI, "spkmi" },
335 { RPC_AUTH_GSS_SPKMP, "spkmp" },
4d81cd16 336 { UINT_MAX, "unknown" }
81039f1f
TM
337 };
338 int i;
339
4d81cd16 340 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
81039f1f
TM
341 if (sec_flavours[i].flavour == flavour)
342 break;
343 }
344 return sec_flavours[i].str;
345}
346
ee671b01
JL
347static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
348 int showdefaults)
349{
350 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
6f6e3c09 351 char *proto = NULL;
ee671b01 352
ee671b01
JL
353 switch (sap->sa_family) {
354 case AF_INET:
355 switch (nfss->mountd_protocol) {
356 case IPPROTO_UDP:
6f6e3c09 357 proto = RPCBIND_NETID_UDP;
ee671b01
JL
358 break;
359 case IPPROTO_TCP:
6f6e3c09 360 proto = RPCBIND_NETID_TCP;
ee671b01 361 break;
ee671b01
JL
362 }
363 break;
364 case AF_INET6:
365 switch (nfss->mountd_protocol) {
366 case IPPROTO_UDP:
6f6e3c09 367 proto = RPCBIND_NETID_UDP6;
ee671b01
JL
368 break;
369 case IPPROTO_TCP:
6f6e3c09 370 proto = RPCBIND_NETID_TCP6;
ee671b01 371 break;
ee671b01
JL
372 }
373 break;
ee671b01 374 }
6f6e3c09
N
375 if (proto || showdefaults)
376 seq_printf(m, ",mountproto=%s", proto ?: "auto");
ee671b01
JL
377}
378
82d101d5
CL
379static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
380 int showdefaults)
381{
382 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
383
d5eff1a3
BS
384 if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
385 return;
386
82d101d5
CL
387 switch (sap->sa_family) {
388 case AF_INET: {
389 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
be859405 390 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
82d101d5
CL
391 break;
392 }
393 case AF_INET6: {
394 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
d250e190 395 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
82d101d5
CL
396 break;
397 }
398 default:
399 if (showdefaults)
1c316e39 400 seq_puts(m, ",mountaddr=unspecified");
82d101d5
CL
401 }
402
403 if (nfss->mountd_version || showdefaults)
404 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
aa699473
SK
405 if ((nfss->mountd_port &&
406 nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
407 showdefaults)
82d101d5
CL
408 seq_printf(m, ",mountport=%u", nfss->mountd_port);
409
ee671b01 410 nfs_show_mountd_netid(m, nfss, showdefaults);
82d101d5
CL
411}
412
89d77c8f 413#if IS_ENABLED(CONFIG_NFS_V4)
0be8189f
TM
414static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
415 int showdefaults)
416{
417 struct nfs_client *clp = nfss->nfs_client;
418
419 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
0be8189f
TM
420}
421#else
422static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
423 int showdefaults)
424{
425}
426#endif
427
7bbceb6f
TM
428static void nfs_show_nfs_version(struct seq_file *m,
429 unsigned int version,
430 unsigned int minorversion)
431{
432 seq_printf(m, ",vers=%u", version);
433 if (version == 4)
434 seq_printf(m, ".%u", minorversion);
435}
436
f7b422b1
DH
437/*
438 * Describe the mount options in force on this server representation
439 */
82d101d5
CL
440static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
441 int showdefaults)
f7b422b1 442{
509de811 443 static const struct proc_nfs_info {
f7b422b1 444 int flag;
509de811
DH
445 const char *str;
446 const char *nostr;
f7b422b1 447 } nfs_info[] = {
91a575e1
TM
448 { NFS_MOUNT_SOFT, ",soft", "" },
449 { NFS_MOUNT_SOFTERR, ",softerr", "" },
c74dfe97 450 { NFS_MOUNT_SOFTREVAL, ",softreval", "" },
82d101d5 451 { NFS_MOUNT_POSIX, ",posix", "" },
f7b422b1
DH
452 { NFS_MOUNT_NOCTO, ",nocto", "" },
453 { NFS_MOUNT_NOAC, ",noac", "" },
454 { NFS_MOUNT_NONLM, ",nolock", "" },
455 { NFS_MOUNT_NOACL, ",noacl", "" },
74dd34e6 456 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
cfe1f877 457 { NFS_MOUNT_FORCE_RDIRPLUS, ",rdirplus=force", "" },
d740351b
CL
458 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
459 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
487fae09
TM
460 { NFS_MOUNT_NETUNREACH_FATAL,
461 ",fatal_neterrors=ENETDOWN:ENETUNREACH",
462 ",fatal_neterrors=none" },
f7b422b1
DH
463 { 0, NULL, NULL }
464 };
509de811 465 const struct proc_nfs_info *nfs_infop;
8fa5c000 466 struct nfs_client *clp = nfss->nfs_client;
82d101d5 467 u32 version = clp->rpc_ops->version;
7c563cc9 468 int local_flock, local_fcntl;
f7b422b1 469
7bbceb6f 470 nfs_show_nfs_version(m, version, clp->cl_minorversion);
2d767432
CL
471 seq_printf(m, ",rsize=%u", nfss->rsize);
472 seq_printf(m, ",wsize=%u", nfss->wsize);
82d101d5
CL
473 if (nfss->bsize != 0)
474 seq_printf(m, ",bsize=%u", nfss->bsize);
475 seq_printf(m, ",namlen=%u", nfss->namelen);
0e0cab74 476 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
2d767432 477 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
0e0cab74 478 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
2d767432 479 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
0e0cab74 480 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
2d767432 481 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
0e0cab74 482 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
2d767432 483 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
91a575e1
TM
484 if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
485 seq_puts(m, ",hard");
f7b422b1
DH
486 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
487 if (nfss->flags & nfs_infop->flag)
488 seq_puts(m, nfs_infop->str);
489 else
490 seq_puts(m, nfs_infop->nostr);
491 }
2446ab60 492 rcu_read_lock();
56928edd 493 seq_printf(m, ",proto=%s",
ee671b01 494 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
2446ab60 495 rcu_read_unlock();
fd87c8b7
TM
496 if (clp->cl_nconnect > 0)
497 seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
82d101d5 498 if (version == 4) {
7e134205
OK
499 if (clp->cl_max_connect > 1)
500 seq_printf(m, ",max_connect=%u", clp->cl_max_connect);
82d101d5
CL
501 if (nfss->port != NFS_PORT)
502 seq_printf(m, ",port=%u", nfss->port);
503 } else
504 if (nfss->port)
505 seq_printf(m, ",port=%u", nfss->port);
506
33170233
TM
507 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
508 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
81039f1f 509 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
c8407f2e
CL
510 switch (clp->cl_xprtsec.policy) {
511 case RPC_XPRTSEC_TLS_ANON:
512 seq_puts(m, ",xprtsec=tls");
513 break;
514 case RPC_XPRTSEC_TLS_X509:
515 seq_puts(m, ",xprtsec=mtls");
516 break;
517 default:
518 break;
519 }
82d101d5
CL
520
521 if (version != 4)
522 nfs_show_mountd_options(m, nfss, showdefaults);
0be8189f
TM
523 else
524 nfs_show_nfsv4_options(m, nfss, showdefaults);
82d101d5 525
490566ed
CH
526 if (nfss->options & NFS_OPTION_FSCACHE) {
527#ifdef CONFIG_NFS_FSCACHE
528 if (nfss->fscache_uniq)
529 seq_printf(m, ",fsc=%s", nfss->fscache_uniq);
530 else
531 seq_puts(m, ",fsc");
532#else
1c316e39 533 seq_puts(m, ",fsc");
490566ed
CH
534#endif
535 }
9b00c643 536
89652617 537 if (nfss->options & NFS_OPTION_MIGRATION)
1c316e39 538 seq_puts(m, ",migration");
89652617 539
9b00c643
PL
540 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
541 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1c316e39 542 seq_puts(m, ",lookupcache=none");
9b00c643 543 else
1c316e39 544 seq_puts(m, ",lookupcache=pos");
9b00c643 545 }
7c563cc9
SJ
546
547 local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
548 local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
549
550 if (!local_flock && !local_fcntl)
1c316e39 551 seq_puts(m, ",local_lock=none");
7c563cc9 552 else if (local_flock && local_fcntl)
1c316e39 553 seq_puts(m, ",local_lock=all");
7c563cc9 554 else if (local_flock)
1c316e39 555 seq_puts(m, ",local_lock=flock");
7c563cc9 556 else
1c316e39 557 seq_puts(m, ",local_lock=posix");
7ae017c7 558
dfb07e99
DA
559 if (nfss->flags & NFS_MOUNT_NO_ALIGNWRITE)
560 seq_puts(m, ",noalignwrite");
561
7ae017c7
TM
562 if (nfss->flags & NFS_MOUNT_WRITE_EAGER) {
563 if (nfss->flags & NFS_MOUNT_WRITE_WAIT)
564 seq_puts(m, ",write=wait");
565 else
566 seq_puts(m, ",write=eager");
567 }
f7b422b1
DH
568}
569
570/*
571 * Describe the mount options on this VFS mountpoint
572 */
fbdefd64 573int nfs_show_options(struct seq_file *m, struct dentry *root)
f7b422b1 574{
34c80b1d 575 struct nfs_server *nfss = NFS_SB(root->d_sb);
f7b422b1
DH
576
577 nfs_show_mount_options(m, nfss, 0);
578
2446ab60 579 rcu_read_lock();
5d8515ca
CL
580 seq_printf(m, ",addr=%s",
581 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
582 RPC_DISPLAY_ADDR));
2446ab60 583 rcu_read_unlock();
f7b422b1
DH
584
585 return 0;
586}
89d77c8f 587EXPORT_SYMBOL_GPL(nfs_show_options);
45402c38 588
89d77c8f 589#if IS_ENABLED(CONFIG_NFS_V4)
1a7441b2
DW
590static void show_lease(struct seq_file *m, struct nfs_server *server)
591{
592 struct nfs_client *clp = server->nfs_client;
593 unsigned long expire;
594
595 seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
596 expire = clp->cl_last_renewal + clp->cl_lease_time;
597 seq_printf(m, ",lease_expired=%ld",
598 time_after(expire, jiffies) ? 0 : (jiffies - expire) / HZ);
599}
ae50c0b5 600#ifdef CONFIG_NFS_V4_1
45402c38 601static void show_sessions(struct seq_file *m, struct nfs_server *server)
ae50c0b5
BF
602{
603 if (nfs4_has_session(server->nfs_client))
1c316e39 604 seq_puts(m, ",sessions");
ae50c0b5
BF
605}
606#else
45402c38
HS
607static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
608#endif
ae50c0b5
BF
609#endif
610
611#ifdef CONFIG_NFS_V4_1
45402c38 612static void show_pnfs(struct seq_file *m, struct nfs_server *server)
ae50c0b5
BF
613{
614 seq_printf(m, ",pnfs=");
615 if (server->pnfs_curr_ld)
616 seq_printf(m, "%s", server->pnfs_curr_ld->name);
617 else
618 seq_printf(m, "not configured");
619}
7e03b7cc
TM
620
621static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
622{
59155546
CL
623 if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
624 struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
7e03b7cc
TM
625 seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
626 "date='%llu,%u'",
627 impl_id->name, impl_id->domain,
628 impl_id->date.seconds, impl_id->date.nseconds);
629 }
630}
45402c38 631#else
89d77c8f 632#if IS_ENABLED(CONFIG_NFS_V4)
7e03b7cc
TM
633static void show_pnfs(struct seq_file *m, struct nfs_server *server)
634{
635}
45402c38 636#endif
7e03b7cc
TM
637static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
638{
639}
45402c38 640#endif
f7b422b1 641
fbdefd64 642int nfs_show_devname(struct seq_file *m, struct dentry *root)
c7f404b4
AV
643{
644 char *page = (char *) __get_free_page(GFP_KERNEL);
645 char *devname, *dummy;
646 int err = 0;
647 if (!page)
648 return -ENOMEM;
97a54868 649 devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
c7f404b4
AV
650 if (IS_ERR(devname))
651 err = PTR_ERR(devname);
652 else
653 seq_escape(m, devname, " \t\n\\");
654 free_page((unsigned long)page);
655 return err;
656}
89d77c8f 657EXPORT_SYMBOL_GPL(nfs_show_devname);
c7f404b4 658
fbdefd64 659int nfs_show_path(struct seq_file *m, struct dentry *dentry)
c7f404b4
AV
660{
661 seq_puts(m, "/");
662 return 0;
663}
89d77c8f 664EXPORT_SYMBOL_GPL(nfs_show_path);
c7f404b4 665
f7b422b1
DH
666/*
667 * Present statistical information for this VFS mountpoint
668 */
fbdefd64 669int nfs_show_stats(struct seq_file *m, struct dentry *root)
f7b422b1
DH
670{
671 int i, cpu;
64132379 672 struct nfs_server *nfss = NFS_SB(root->d_sb);
f7b422b1
DH
673 struct rpc_auth *auth = nfss->client->cl_auth;
674 struct nfs_iostats totals = { };
675
676 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
677
678 /*
679 * Display all mount option settings
680 */
1c316e39 681 seq_puts(m, "\n\topts:\t");
bc98a42c 682 seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
1751e8a6
LT
683 seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
684 seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
685 seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
f7b422b1
DH
686 nfs_show_mount_options(m, nfss, 1);
687
688 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
689
7e03b7cc 690 show_implementation_id(m, nfss);
7d2ed9ac 691
1c316e39 692 seq_puts(m, "\n\tcaps:\t");
f7b422b1 693 seq_printf(m, "caps=0x%x", nfss->caps);
2d767432
CL
694 seq_printf(m, ",wtmult=%u", nfss->wtmult);
695 seq_printf(m, ",dtsize=%u", nfss->dtsize);
696 seq_printf(m, ",bsize=%u", nfss->bsize);
697 seq_printf(m, ",namlen=%u", nfss->namelen);
f7b422b1 698
89d77c8f 699#if IS_ENABLED(CONFIG_NFS_V4)
40c55319 700 if (nfss->nfs_client->rpc_ops->version == 4) {
1c316e39 701 seq_puts(m, "\n\tnfsv4:\t");
f7b422b1
DH
702 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
703 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
a09df2ca 704 seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
f7b422b1 705 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
ae50c0b5
BF
706 show_sessions(m, nfss);
707 show_pnfs(m, nfss);
1a7441b2 708 show_lease(m, nfss);
f7b422b1
DH
709 }
710#endif
711
712 /*
713 * Display security flavor in effect for this mount
714 */
2d767432 715 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
f7b422b1 716 if (auth->au_flavor)
2d767432 717 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
f7b422b1
DH
718
719 /*
720 * Display superblock I/O counters
721 */
722 for_each_possible_cpu(cpu) {
723 struct nfs_iostats *stats;
724
725 preempt_disable();
726 stats = per_cpu_ptr(nfss->io_stats, cpu);
727
728 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
729 totals.events[i] += stats->events[i];
730 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
731 totals.bytes[i] += stats->bytes[i];
732
733 preempt_enable();
734 }
735
1c316e39 736 seq_puts(m, "\n\tevents:\t");
f7b422b1
DH
737 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
738 seq_printf(m, "%lu ", totals.events[i]);
1c316e39 739 seq_puts(m, "\n\tbytes:\t");
f7b422b1
DH
740 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
741 seq_printf(m, "%Lu ", totals.bytes[i]);
9bcaa35c 742 seq_putc(m, '\n');
f7b422b1 743
016583d7 744 rpc_clnt_show_stats(m, nfss->client);
f7b422b1
DH
745
746 return 0;
747}
89d77c8f 748EXPORT_SYMBOL_GPL(nfs_show_stats);
f7b422b1
DH
749
750/*
751 * Begin unmount by attempting to remove all automounted mountpoints we added
54ceac45 752 * in response to xdev traversals and referrals
f7b422b1 753 */
fbdefd64 754void nfs_umount_begin(struct super_block *sb)
f7b422b1 755{
67e55205 756 struct nfs_server *server;
fc6ae3cf
TM
757 struct rpc_clnt *rpc;
758
67e55205 759 server = NFS_SB(sb);
fc6ae3cf
TM
760 /* -EIO all pending I/O */
761 rpc = server->client_acl;
762 if (!IS_ERR(rpc))
763 rpc_killall_tasks(rpc);
764 rpc = server->client;
765 if (!IS_ERR(rpc))
766 rpc_killall_tasks(rpc);
f7b422b1 767}
89d77c8f 768EXPORT_SYMBOL_GPL(nfs_umount_begin);
f7b422b1 769
4d4b69dd
WAA
770/*
771 * Return true if 'match' is in auth_info or auth_info is empty.
772 * Return false otherwise.
773 */
774bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
775 rpc_authflavor_t match)
776{
777 int i;
778
779 if (!auth_info->flavor_len)
780 return true;
781
782 for (i = 0; i < auth_info->flavor_len; i++) {
783 if (auth_info->flavors[i] == match)
784 return true;
785 }
786 return false;
787}
788EXPORT_SYMBOL_GPL(nfs_auth_info_match);
789
ec88f28d 790/*
38465f5d
SM
791 * Ensure that a specified authtype in ctx->auth_info is supported by
792 * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
4d4b69dd 793 * -EACCES if not.
ec88f28d 794 */
38465f5d 795static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
5eb005ca
DH
796 rpc_authflavor_t *server_authlist,
797 unsigned int count)
ec88f28d 798{
4d4b69dd 799 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
53a75f22 800 bool found_auth_null = false;
9111c95b 801 unsigned int i;
4580a92d 802
d497ab97
WAA
803 /*
804 * If the sec= mount option is used, the specified flavor or AUTH_NULL
805 * must be in the list returned by the server.
806 *
807 * AUTH_NULL has a special meaning when it's in the server list - it
808 * means that the server will ignore the rpc creds, so any flavor
e68fd7c8 809 * can be used but still use the sec= that was specified.
53a75f22
CL
810 *
811 * Note also that the MNT procedure in MNTv1 does not return a list
812 * of supported security flavors. In this case, nfs_mount() fabricates
813 * a security flavor list containing just AUTH_NULL.
d497ab97 814 */
4580a92d 815 for (i = 0; i < count; i++) {
4d4b69dd
WAA
816 flavor = server_authlist[i];
817
38465f5d 818 if (nfs_auth_info_match(&ctx->auth_info, flavor))
9111c95b 819 goto out;
e68fd7c8
SD
820
821 if (flavor == RPC_AUTH_NULL)
53a75f22 822 found_auth_null = true;
e68fd7c8
SD
823 }
824
53a75f22 825 if (found_auth_null) {
38465f5d 826 flavor = ctx->auth_info.flavors[0];
e68fd7c8 827 goto out;
4580a92d 828 }
ec88f28d 829
4d4b69dd
WAA
830 dfprintk(MOUNT,
831 "NFS: specified auth flavors not supported by server\n");
9111c95b 832 return -EACCES;
d497ab97 833
4580a92d 834out:
38465f5d
SM
835 ctx->selected_flavor = flavor;
836 dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
d497ab97 837 return 0;
ec88f28d
CL
838}
839
0076d7b7
CL
840/*
841 * Use the remote server's MOUNT service to request the NFS file handle
842 * corresponding to the provided path.
843 */
62a55d08 844static int nfs_request_mount(struct fs_context *fc,
294ae81d
JL
845 struct nfs_fh *root_fh,
846 rpc_authflavor_t *server_authlist,
847 unsigned int *server_authlist_len)
0076d7b7 848{
62a55d08 849 struct nfs_fs_context *ctx = nfs_fc2context(fc);
c5d120f8 850 struct nfs_mount_request request = {
cf0d7e7f 851 .sap = &ctx->mount_server._address,
38465f5d
SM
852 .dirpath = ctx->nfs_server.export_path,
853 .protocol = ctx->mount_server.protocol,
c5d120f8 854 .fh = root_fh,
38465f5d 855 .noresvport = ctx->flags & NFS_MOUNT_NORESVPORT,
294ae81d 856 .auth_flav_len = server_authlist_len,
ec88f28d 857 .auth_flavs = server_authlist,
62a55d08 858 .net = fc->net_ns,
c5d120f8 859 };
4c568017 860 int status;
0076d7b7 861
38465f5d
SM
862 if (ctx->mount_server.version == 0) {
863 switch (ctx->version) {
8a6e5deb 864 default:
38465f5d 865 ctx->mount_server.version = NFS_MNT3_VERSION;
8a6e5deb
TM
866 break;
867 case 2:
38465f5d 868 ctx->mount_server.version = NFS_MNT_VERSION;
8a6e5deb 869 }
0076d7b7 870 }
38465f5d 871 request.version = ctx->mount_server.version;
0076d7b7 872
38465f5d
SM
873 if (ctx->mount_server.hostname)
874 request.hostname = ctx->mount_server.hostname;
33832034 875 else
38465f5d 876 request.hostname = ctx->nfs_server.hostname;
33832034 877
0076d7b7
CL
878 /*
879 * Construct the mount server's address.
880 */
38465f5d 881 if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
cf0d7e7f 882 memcpy(request.sap, &ctx->nfs_server._address,
38465f5d
SM
883 ctx->nfs_server.addrlen);
884 ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
4c568017 885 }
38465f5d
SM
886 request.salen = ctx->mount_server.addrlen;
887 nfs_set_port(request.sap, &ctx->mount_server.port, 0);
0076d7b7
CL
888
889 /*
890 * Now ask the mount server to map our export path
891 * to a file handle.
892 */
6e2a1034
N
893 if ((request.protocol == XPRT_TRANSPORT_UDP) ==
894 !(ctx->flags & NFS_MOUNT_TCP))
895 /*
896 * NFS protocol and mount protocol are both UDP or neither UDP
897 * so timeouts are compatible. Use NFS timeouts for MOUNT
898 */
899 status = nfs_mount(&request, ctx->timeo, ctx->retrans);
900 else
901 status = nfs_mount(&request, NFS_UNSPEC_TIMEO, NFS_UNSPEC_RETRANS);
ec88f28d
CL
902 if (status != 0) {
903 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
904 request.hostname, status);
905 return status;
906 }
0076d7b7 907
9111c95b 908 return 0;
0076d7b7
CL
909}
910
62a55d08 911static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
d17540c6 912{
62a55d08 913 struct nfs_fs_context *ctx = nfs_fc2context(fc);
d17540c6 914 int status;
9111c95b
JL
915 unsigned int i;
916 bool tried_auth_unix = false;
917 bool auth_null_in_list = false;
918 struct nfs_server *server = ERR_PTR(-EACCES);
294ae81d
JL
919 rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
920 unsigned int authlist_len = ARRAY_SIZE(authlist);
d17540c6 921
bf95f82e
CH
922 /* make sure 'nolock'/'lock' override the 'local_lock' mount option */
923 if (ctx->lock_status) {
924 if (ctx->lock_status == NFS_LOCK_NOLOCK) {
925 ctx->flags |= NFS_MOUNT_NONLM;
926 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
927 } else {
928 ctx->flags &= ~NFS_MOUNT_NONLM;
929 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
930 }
931 }
62a55d08 932 status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
d17540c6
JL
933 if (status)
934 return ERR_PTR(status);
935
9111c95b
JL
936 /*
937 * Was a sec= authflavor specified in the options? First, verify
938 * whether the server supports it, and then just try to use it if so.
939 */
5eb005ca
DH
940 if (ctx->auth_info.flavor_len > 0) {
941 status = nfs_verify_authflavors(ctx, authlist, authlist_len);
a3f73c27 942 dfprintk(MOUNT, "NFS: using auth flavor %u\n",
5eb005ca 943 ctx->selected_flavor);
9111c95b
JL
944 if (status)
945 return ERR_PTR(status);
62a55d08 946 return ctx->nfs_mod->rpc_ops->create_server(fc);
9111c95b
JL
947 }
948
949 /*
950 * No sec= option was provided. RFC 2623, section 2.7 suggests we
951 * SHOULD prefer the flavor listed first. However, some servers list
952 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
953 */
954 for (i = 0; i < authlist_len; ++i) {
955 rpc_authflavor_t flavor;
956 struct rpcsec_gss_info info;
957
958 flavor = authlist[i];
959 switch (flavor) {
960 case RPC_AUTH_UNIX:
961 tried_auth_unix = true;
962 break;
963 case RPC_AUTH_NULL:
964 auth_null_in_list = true;
965 continue;
966 default:
967 if (rpcauth_get_gssinfo(flavor, &info) != 0)
968 continue;
fb08334b 969 break;
9111c95b
JL
970 }
971 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
5eb005ca 972 ctx->selected_flavor = flavor;
62a55d08 973 server = ctx->nfs_mod->rpc_ops->create_server(fc);
9111c95b
JL
974 if (!IS_ERR(server))
975 return server;
976 }
977
978 /*
979 * Nothing we tried so far worked. At this point, give up if we've
980 * already tried AUTH_UNIX or if the server's list doesn't contain
981 * AUTH_NULL
982 */
983 if (tried_auth_unix || !auth_null_in_list)
984 return server;
985
986 /* Last chance! Try AUTH_UNIX */
987 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
5eb005ca 988 ctx->selected_flavor = RPC_AUTH_UNIX;
62a55d08 989 return ctx->nfs_mod->rpc_ops->create_server(fc);
d17540c6
JL
990}
991
f2aedb71 992int nfs_try_get_tree(struct fs_context *fc)
486aa699 993{
f2aedb71
DH
994 struct nfs_fs_context *ctx = nfs_fc2context(fc);
995
996 if (ctx->need_mount)
62a55d08 997 ctx->server = nfs_try_mount_request(fc);
d17540c6 998 else
62a55d08 999 ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
486aa699 1000
f2aedb71 1001 return nfs_get_tree_common(fc);
486aa699 1002}
f2aedb71
DH
1003EXPORT_SYMBOL_GPL(nfs_try_get_tree);
1004
486aa699 1005
71a6ec8a 1006#define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
c8e47028
SM
1007 | NFS_MOUNT_SECURE \
1008 | NFS_MOUNT_TCP \
1009 | NFS_MOUNT_VER3 \
1010 | NFS_MOUNT_KERBEROS \
1011 | NFS_MOUNT_NONLM \
1012 | NFS_MOUNT_BROKEN_SUID \
1013 | NFS_MOUNT_STRICTLOCK \
c8e47028
SM
1014 | NFS_MOUNT_LEGACY_INTERFACE)
1015
71a6ec8a
SM
1016#define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1017 ~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1018
48b605f8
JL
1019static int
1020nfs_compare_remount_data(struct nfs_server *nfss,
5eb005ca 1021 struct nfs_fs_context *ctx)
48b605f8 1022{
5eb005ca
DH
1023 if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1024 ctx->rsize != nfss->rsize ||
1025 ctx->wsize != nfss->wsize ||
1026 ctx->version != nfss->nfs_client->rpc_ops->version ||
1027 ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1028 ctx->retrans != nfss->client->cl_timeout->to_retries ||
1029 !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1030 ctx->acregmin != nfss->acregmin / HZ ||
1031 ctx->acregmax != nfss->acregmax / HZ ||
1032 ctx->acdirmin != nfss->acdirmin / HZ ||
1033 ctx->acdirmax != nfss->acdirmax / HZ ||
1034 ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1035 (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1036 ctx->nfs_server.port != nfss->port ||
1037 ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1038 !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
a1be9eee 1039 (struct sockaddr *)&nfss->nfs_client->cl_addr))
48b605f8
JL
1040 return -EINVAL;
1041
1042 return 0;
1043}
1044
f2aedb71 1045int nfs_reconfigure(struct fs_context *fc)
48b605f8 1046{
f2aedb71
DH
1047 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1048 struct super_block *sb = fc->root->d_sb;
48b605f8 1049 struct nfs_server *nfss = sb->s_fs_info;
1301ba60 1050 int ret;
48b605f8 1051
02b9984d
TT
1052 sync_filesystem(sb);
1053
80c4de6a
LL
1054 /*
1055 * The SB_RDONLY flag has been removed from the superblock during
1056 * mounts to prevent interference between different filesystems.
1057 * Similarly, it is also necessary to ignore the SB_RDONLY flag
1058 * during reconfiguration; otherwise, it may also result in the
1059 * creation of redundant superblocks when mounting a directory with
1060 * different rw and ro flags multiple times.
1061 */
1062 fc->sb_flags_mask &= ~SB_RDONLY;
1063
48b605f8
JL
1064 /*
1065 * Userspace mount programs that send binary options generally send
1066 * them populated with default values. We have no way to know which
1067 * ones were explicitly specified. Fall back to legacy behavior and
1068 * just return success.
1069 */
f2aedb71 1070 if (ctx->skip_reconfig_option_check)
48b605f8
JL
1071 return 0;
1072
26c4c170
JL
1073 /*
1074 * noac is a special case. It implies -o sync, but that's not
f2aedb71 1075 * necessarily reflected in the mtab options. reconfigure_super
1751e8a6 1076 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
26c4c170
JL
1077 * remount options, so we have to explicitly reset it.
1078 */
f2aedb71
DH
1079 if (ctx->flags & NFS_MOUNT_NOAC) {
1080 fc->sb_flags |= SB_SYNCHRONOUS;
1081 fc->sb_flags_mask |= SB_SYNCHRONOUS;
1082 }
26c4c170 1083
48b605f8 1084 /* compare new mount options with old ones */
1301ba60
AS
1085 ret = nfs_compare_remount_data(nfss, ctx);
1086 if (ret)
1087 return ret;
1088
1089 return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
48b605f8 1090}
f2aedb71 1091EXPORT_SYMBOL_GPL(nfs_reconfigure);
48b605f8 1092
f7b422b1 1093/*
ab88dca3 1094 * Finish setting up an NFS superblock
f7b422b1 1095 */
62a55d08 1096static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
f7b422b1
DH
1097{
1098 struct nfs_server *server = NFS_SB(sb);
5006a76c 1099
54ceac45
DH
1100 sb->s_blocksize_bits = 0;
1101 sb->s_blocksize = 0;
6a74490d
BS
1102 sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1103 sb->s_op = server->nfs_client->cl_nfs_mod->sops;
8c6d76a3 1104 if (ctx->bsize)
5eb005ca 1105 sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
f7b422b1 1106
2a9d683b
TM
1107 switch (server->nfs_client->rpc_ops->version) {
1108 case 2:
1109 sb->s_time_gran = 1000;
1110 sb->s_time_min = 0;
1111 sb->s_time_max = U32_MAX;
1112 break;
1113 case 3:
1114 /*
1115 * The VFS shouldn't apply the umask to mode bits.
1116 * We will do so ourselves when necessary.
54ceac45 1117 */
1751e8a6 1118 sb->s_flags |= SB_POSIXACL;
54ceac45 1119 sb->s_time_gran = 1;
1fcb79c1
DD
1120 sb->s_time_min = 0;
1121 sb->s_time_max = U32_MAX;
2a9d683b
TM
1122 sb->s_export_op = &nfs_export_ops;
1123 break;
1124 case 4:
5aa8fd9c 1125 sb->s_iflags |= SB_I_NOUMASK;
2a9d683b 1126 sb->s_time_gran = 1;
1fcb79c1
DD
1127 sb->s_time_min = S64_MIN;
1128 sb->s_time_max = S64_MAX;
2a9d683b
TM
1129 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1130 sb->s_export_op = &nfs_export_ops;
1131 break;
816724e6 1132 }
54ceac45 1133
ab88dca3 1134 sb->s_magic = NFS_SUPER_MAGIC;
f7b422b1 1135
ab88dca3
AV
1136 /* We probably want something more informative here */
1137 snprintf(sb->s_id, sizeof(sb->s_id),
1138 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1fcb79c1 1139
ab88dca3
AV
1140 if (sb->s_blocksize == 0)
1141 sb->s_blocksize = nfs_block_bits(server->wsize,
1142 &sb->s_blocksize_bits);
f7b422b1 1143
ab88dca3 1144 nfs_super_set_maxbytes(sb, server->maxfilesize);
1c725118 1145 nfs_sysfs_move_server_to_sb(sb);
ec1ade6a 1146 server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
f7b422b1
DH
1147}
1148
62a55d08
SM
1149static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1150 const struct fs_context *fc)
275a5d24
TM
1151{
1152 const struct nfs_server *a = s->s_fs_info;
1153 const struct rpc_clnt *clnt_a = a->client;
1154 const struct rpc_clnt *clnt_b = b->client;
1155
62a55d08 1156 if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
275a5d24
TM
1157 goto Ebusy;
1158 if (a->nfs_client != b->nfs_client)
1159 goto Ebusy;
0aea92bf 1160 if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
275a5d24
TM
1161 goto Ebusy;
1162 if (a->wsize != b->wsize)
1163 goto Ebusy;
1164 if (a->rsize != b->rsize)
1165 goto Ebusy;
1166 if (a->acregmin != b->acregmin)
1167 goto Ebusy;
1168 if (a->acregmax != b->acregmax)
1169 goto Ebusy;
1170 if (a->acdirmin != b->acdirmin)
1171 goto Ebusy;
1172 if (a->acdirmax != b->acdirmax)
1173 goto Ebusy;
594d1644 1174 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
275a5d24 1175 goto Ebusy;
e89a5a43 1176 return 1;
275a5d24 1177Ebusy:
e89a5a43
TM
1178 return 0;
1179}
1180
f2aedb71 1181static int nfs_set_super(struct super_block *s, struct fs_context *fc)
e89a5a43 1182{
f2aedb71 1183 struct nfs_server *server = fc->s_fs_info;
e89a5a43
TM
1184 int ret;
1185
8b244ff2 1186 s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
e89a5a43
TM
1187 ret = set_anon_super(s, server);
1188 if (ret == 0)
1189 server->s_dev = s->s_dev;
1190 return ret;
1191}
1192
fd00a8ff
CL
1193static int nfs_compare_super_address(struct nfs_server *server1,
1194 struct nfs_server *server2)
1195{
1196 struct sockaddr *sap1, *sap2;
7e3fcf61
BF
1197 struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1198 struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1199
1200 if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1201 return 0;
fd00a8ff
CL
1202
1203 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1204 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1205
1206 if (sap1->sa_family != sap2->sa_family)
1207 return 0;
1208
1209 switch (sap1->sa_family) {
1210 case AF_INET: {
1211 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1212 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1213 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1214 return 0;
1215 if (sin1->sin_port != sin2->sin_port)
1216 return 0;
1217 break;
1218 }
1219 case AF_INET6: {
1220 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1221 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1222 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1223 return 0;
1224 if (sin1->sin6_port != sin2->sin6_port)
1225 return 0;
1226 break;
1227 }
1228 default:
1229 return 0;
1230 }
1231
1232 return 1;
1233}
1234
3b7eb5e3
TM
1235static int nfs_compare_userns(const struct nfs_server *old,
1236 const struct nfs_server *new)
1237{
1238 const struct user_namespace *oldns = &init_user_ns;
1239 const struct user_namespace *newns = &init_user_ns;
1240
1241 if (old->client && old->client->cl_cred)
1242 oldns = old->client->cl_cred->user_ns;
1243 if (new->client && new->client->cl_cred)
1244 newns = new->client->cl_cred->user_ns;
1245 if (oldns != newns)
1246 return 0;
1247 return 1;
1248}
1249
f2aedb71 1250static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
e89a5a43 1251{
f2aedb71 1252 struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
e89a5a43 1253
fd00a8ff 1254 if (!nfs_compare_super_address(old, server))
e89a5a43
TM
1255 return 0;
1256 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1257 if (old->flags & NFS_MOUNT_UNSHARED)
1258 return 0;
1259 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1260 return 0;
3b7eb5e3
TM
1261 if (!nfs_compare_userns(old, server))
1262 return 0;
ec1ade6a
OK
1263 if ((old->has_sec_mnt_opts || fc->security) &&
1264 security_sb_mnt_opts_compat(sb, fc->security))
1265 return 0;
62a55d08 1266 return nfs_compare_mount_options(sb, server, fc);
275a5d24
TM
1267}
1268
39ffb921 1269#ifdef CONFIG_NFS_FSCACHE
a6b5a28e
DW
1270static int nfs_get_cache_cookie(struct super_block *sb,
1271 struct nfs_fs_context *ctx)
2311b943 1272{
c4271c6e 1273 struct nfs_server *nfss = NFS_SB(sb);
2311b943
BS
1274 char *uniq = NULL;
1275 int ulen = 0;
1276
c4271c6e
TM
1277 nfss->fscache = NULL;
1278
f2aedb71 1279 if (!ctx)
a6b5a28e 1280 return 0;
f2aedb71
DH
1281
1282 if (ctx->clone_data.sb) {
1283 struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
c4271c6e 1284 if (!(mnt_s->options & NFS_OPTION_FSCACHE))
a6b5a28e
DW
1285 return 0;
1286 if (mnt_s->fscache_uniq) {
1287 uniq = mnt_s->fscache_uniq;
1288 ulen = strlen(uniq);
9c91fa36 1289 }
f2aedb71
DH
1290 } else {
1291 if (!(ctx->options & NFS_OPTION_FSCACHE))
a6b5a28e 1292 return 0;
f2aedb71
DH
1293 if (ctx->fscache_uniq) {
1294 uniq = ctx->fscache_uniq;
1295 ulen = strlen(ctx->fscache_uniq);
1296 }
f2aedb71 1297 }
2311b943 1298
a6b5a28e 1299 return nfs_fscache_get_super_cookie(sb, uniq, ulen);
2311b943 1300}
39ffb921 1301#else
a6b5a28e
DW
1302static int nfs_get_cache_cookie(struct super_block *sb,
1303 struct nfs_fs_context *ctx)
39ffb921 1304{
a6b5a28e 1305 return 0;
39ffb921
TM
1306}
1307#endif
2311b943 1308
f2aedb71 1309int nfs_get_tree_common(struct fs_context *fc)
f7b422b1 1310{
f2aedb71 1311 struct nfs_fs_context *ctx = nfs_fc2context(fc);
f7b422b1 1312 struct super_block *s;
f2aedb71 1313 int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
62a55d08 1314 struct nfs_server *server = ctx->server;
01194981 1315 int error;
33852a1f 1316
62a55d08 1317 ctx->server = NULL;
444a5296 1318 if (IS_ERR(server))
f2aedb71 1319 return PTR_ERR(server);
444a5296 1320
8cd9b785
LL
1321 /*
1322 * When NFS_MOUNT_UNSHARED is not set, NFS forces the sharing of a
1323 * superblock among each filesystem that mounts sub-directories
1324 * belonging to a single exported root path.
1325 * To prevent interference between different filesystems, the
1326 * SB_RDONLY flag should be removed from the superblock.
1327 */
75180df2
TM
1328 if (server->flags & NFS_MOUNT_UNSHARED)
1329 compare_super = NULL;
8cd9b785
LL
1330 else
1331 fc->sb_flags &= ~SB_RDONLY;
75180df2 1332
fb2088cc
SP
1333 /* -o noac implies -o sync */
1334 if (server->flags & NFS_MOUNT_NOAC)
f2aedb71
DH
1335 fc->sb_flags |= SB_SYNCHRONOUS;
1336
1337 if (ctx->clone_data.sb)
1338 if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1339 fc->sb_flags |= SB_SYNCHRONOUS;
fb2088cc 1340
54ceac45 1341 /* Get a superblock - note that we may end up sharing one that already exists */
f2aedb71
DH
1342 fc->s_fs_info = server;
1343 s = sget_fc(fc, compare_super, nfs_set_super);
1344 fc->s_fs_info = NULL;
816724e6 1345 if (IS_ERR(s)) {
f2aedb71 1346 error = PTR_ERR(s);
ce8866f0 1347 nfs_errorf(fc, "NFS: Couldn't get superblock");
54ceac45 1348 goto out_err_nosb;
816724e6
TM
1349 }
1350
54ceac45
DH
1351 if (s->s_fs_info != server) {
1352 nfs_free_server(server);
1353 server = NULL;
fa799759 1354 } else {
9052c7cf
JK
1355 error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1356 MINOR(server->s_dev));
f2aedb71 1357 if (error)
9052c7cf 1358 goto error_splat_super;
55b2598e 1359 s->s_bdi->io_pages = server->rpages;
ce6cda18 1360 server->super = s;
54ceac45 1361 }
f7b422b1 1362
54ceac45 1363 if (!s->s_root) {
62a55d08 1364 unsigned bsize = ctx->clone_data.inherited_bsize;
54ceac45 1365 /* initial superblock/root creation */
62a55d08 1366 nfs_fill_super(s, ctx);
ab88dca3
AV
1367 if (bsize) {
1368 s->s_blocksize_bits = bsize;
1369 s->s_blocksize = 1U << bsize;
1370 }
a6b5a28e
DW
1371 error = nfs_get_cache_cookie(s, ctx);
1372 if (error < 0)
1373 goto error_splat_super;
54ceac45 1374 }
f7b422b1 1375
62a55d08
SM
1376 error = nfs_get_root(s, fc);
1377 if (error < 0) {
ce8866f0 1378 nfs_errorf(fc, "NFS: Couldn't get root dentry");
54ceac45 1379 goto error_splat_super;
f2aedb71 1380 }
adf2314f 1381
1751e8a6 1382 s->s_flags |= SB_ACTIVE;
f2aedb71 1383 error = 0;
0655960f
CL
1384
1385out:
f2aedb71 1386 return error;
816724e6 1387
54ceac45
DH
1388out_err_nosb:
1389 nfs_free_server(server);
0655960f 1390 goto out;
54ceac45 1391error_splat_super:
6f5bbff9 1392 deactivate_locked_super(s);
0655960f 1393 goto out;
f7b422b1
DH
1394}
1395
54ceac45 1396/*
1c725118 1397 * Destroy an NFS superblock
54ceac45 1398 */
fbdefd64 1399void nfs_kill_super(struct super_block *s)
f7b422b1
DH
1400{
1401 struct nfs_server *server = NFS_SB(s);
7b14a213 1402
1c725118 1403 nfs_sysfs_move_sb_to_server(server);
5069ba84 1404 kill_anon_super(s);
f7b422b1 1405
08734048 1406 nfs_fscache_release_super_cookie(s);
7b14a213 1407
54ceac45 1408 nfs_free_server(server);
f7b422b1 1409}
89d77c8f 1410EXPORT_SYMBOL_GPL(nfs_kill_super);
f7b422b1 1411
89d77c8f 1412#if IS_ENABLED(CONFIG_NFS_V4)
54ceac45 1413
fac1e8e4
BS
1414/*
1415 * NFS v4 module parameters need to stay in the
1416 * NFS client for backwards compatibility
1417 */
1418unsigned int nfs_callback_set_tcpport;
5405fc44 1419unsigned short nfs_callback_nr_threads;
fac1e8e4
BS
1420/* Default cache timeout is 10 minutes */
1421unsigned int nfs_idmap_cache_timeout = 600;
1422/* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1423bool nfs4_disable_idmapping = true;
1424unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
5405fc44 1425unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
fac1e8e4 1426unsigned short send_implementation_id = 1;
6f2ea7f2 1427char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
f6de7a39 1428bool recover_lost_locks = false;
5b9d31ae 1429short nfs_delay_retrans = -1;
fac1e8e4 1430
5405fc44 1431EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
89d77c8f 1432EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
89d77c8f
BS
1433EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1434EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1435EXPORT_SYMBOL_GPL(max_session_slots);
5405fc44 1436EXPORT_SYMBOL_GPL(max_session_cb_slots);
89d77c8f 1437EXPORT_SYMBOL_GPL(send_implementation_id);
6f2ea7f2 1438EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
f6de7a39 1439EXPORT_SYMBOL_GPL(recover_lost_locks);
5b9d31ae 1440EXPORT_SYMBOL_GPL(nfs_delay_retrans);
89d77c8f 1441
fac1e8e4
BS
1442#define NFS_CALLBACK_MAXPORTNR (65535U)
1443
1444static int param_set_portnr(const char *val, const struct kernel_param *kp)
1445{
1446 unsigned long num;
1447 int ret;
1448
1449 if (!val)
1450 return -EINVAL;
7297cb68 1451 ret = kstrtoul(val, 0, &num);
379ebf07 1452 if (ret || num > NFS_CALLBACK_MAXPORTNR)
fac1e8e4
BS
1453 return -EINVAL;
1454 *((unsigned int *)kp->arg) = num;
1455 return 0;
1456}
9c27847d 1457static const struct kernel_param_ops param_ops_portnr = {
fac1e8e4
BS
1458 .set = param_set_portnr,
1459 .get = param_get_uint,
1460};
a799b68a 1461#define param_check_portnr(name, p) __param_check(name, p, unsigned int)
fac1e8e4
BS
1462
1463module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
5405fc44
TM
1464module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1465MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1466 "assigned to the NFSv4 callback channels.");
fac1e8e4
BS
1467module_param(nfs_idmap_cache_timeout, int, 0644);
1468module_param(nfs4_disable_idmapping, bool, 0644);
6f2ea7f2
CL
1469module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1470 NFS4_CLIENT_ID_UNIQ_LEN, 0600);
fac1e8e4
BS
1471MODULE_PARM_DESC(nfs4_disable_idmapping,
1472 "Turn off NFSv4 idmapping when using 'sec=sys'");
1473module_param(max_session_slots, ushort, 0644);
1474MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1475 "requests the client will negotiate");
5405fc44 1476module_param(max_session_cb_slots, ushort, 0644);
f36ab161 1477MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
5405fc44 1478 "callbacks the client will process for a given server");
fac1e8e4
BS
1479module_param(send_implementation_id, ushort, 0644);
1480MODULE_PARM_DESC(send_implementation_id,
1481 "Send implementation ID with NFSv4.1 exchange_id");
6f2ea7f2 1482MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
425e776d 1483
f6de7a39
TM
1484module_param(recover_lost_locks, bool, 0644);
1485MODULE_PARM_DESC(recover_lost_locks,
1486 "If the server reports that a lock might be lost, "
1487 "try to recover it risking data corruption.");
1488
5b9d31ae
TM
1489module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1490MODULE_PARM_DESC(delay_retrans,
1491 "Unless negative, specifies the number of times the NFSv4 "
1492 "client retries a request before returning an EAGAIN error, "
1493 "after a reply of NFS4ERR_DELAY from the server.");
54ceac45 1494#endif /* CONFIG_NFS_V4 */