1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1992 Rick Sladkey
7 * nfs superblock handling functions
10 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
12 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
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())
24 #include <linux/module.h>
25 #include <linux/init.h>
27 #include <linux/time.h>
28 #include <linux/kernel.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>
35 #include <linux/sunrpc/addr.h>
36 #include <linux/sunrpc/stats.h>
37 #include <linux/sunrpc/metrics.h>
38 #include <linux/sunrpc/xprtsock.h>
39 #include <linux/sunrpc/xprtrdma.h>
40 #include <linux/nfs_fs.h>
41 #include <linux/nfs_mount.h>
42 #include <linux/nfs4_mount.h>
43 #include <linux/lockd/bind.h>
44 #include <linux/seq_file.h>
45 #include <linux/mount.h>
46 #include <linux/namei.h>
47 #include <linux/vfs.h>
48 #include <linux/inet.h>
49 #include <linux/in6.h>
50 #include <linux/sched.h>
51 #include <linux/slab.h>
53 #include <linux/netdevice.h>
54 #include <linux/nfs_xdr.h>
55 #include <linux/magic.h>
56 #include <linux/parser.h>
57 #include <linux/nsproxy.h>
58 #include <linux/rcupdate.h>
60 #include <linux/uaccess.h>
61 #include <linux/nfs_ssc.h>
63 #include <uapi/linux/tls.h>
67 #include "delegation.h"
71 #include "nfs4session.h"
76 #include "nfs4idmap.h"
78 #define NFSDBG_FACILITY NFSDBG_VFS
80 const struct super_operations nfs_sops = {
81 .alloc_inode = nfs_alloc_inode,
82 .free_inode = nfs_free_inode,
83 .write_inode = nfs_write_inode,
84 .drop_inode = nfs_drop_inode,
86 .evict_inode = nfs_evict_inode,
87 .umount_begin = nfs_umount_begin,
88 .show_options = nfs_show_options,
89 .show_devname = nfs_show_devname,
90 .show_path = nfs_show_path,
91 .show_stats = nfs_show_stats,
93 EXPORT_SYMBOL_GPL(nfs_sops);
95 #ifdef CONFIG_NFS_V4_2
96 static const struct nfs_ssc_client_ops nfs_ssc_clnt_ops_tbl = {
97 .sco_sb_deactive = nfs_sb_deactive,
101 #if IS_ENABLED(CONFIG_NFS_V4)
102 static int __init register_nfs4_fs(void)
104 return register_filesystem(&nfs4_fs_type);
107 static void unregister_nfs4_fs(void)
109 unregister_filesystem(&nfs4_fs_type);
112 static int __init register_nfs4_fs(void)
117 static void unregister_nfs4_fs(void)
122 #ifdef CONFIG_NFS_V4_2
123 static void nfs_ssc_register_ops(void)
125 nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
128 static void nfs_ssc_unregister_ops(void)
130 nfs_ssc_unregister(&nfs_ssc_clnt_ops_tbl);
132 #endif /* CONFIG_NFS_V4_2 */
134 static struct shrinker *acl_shrinker;
137 * Register the NFS filesystems
139 int __init register_nfs_fs(void)
143 ret = register_filesystem(&nfs_fs_type);
147 ret = register_nfs4_fs();
151 ret = nfs_register_sysctl();
155 acl_shrinker = shrinker_alloc(0, "nfs-acl");
161 acl_shrinker->count_objects = nfs_access_cache_count;
162 acl_shrinker->scan_objects = nfs_access_cache_scan;
164 shrinker_register(acl_shrinker);
166 #ifdef CONFIG_NFS_V4_2
167 nfs_ssc_register_ops();
171 nfs_unregister_sysctl();
173 unregister_nfs4_fs();
175 unregister_filesystem(&nfs_fs_type);
181 * Unregister the NFS filesystems
183 void __exit unregister_nfs_fs(void)
185 shrinker_free(acl_shrinker);
186 nfs_unregister_sysctl();
187 unregister_nfs4_fs();
188 #ifdef CONFIG_NFS_V4_2
189 nfs_ssc_unregister_ops();
191 unregister_filesystem(&nfs_fs_type);
194 bool nfs_sb_active(struct super_block *sb)
196 struct nfs_server *server = NFS_SB(sb);
198 if (!atomic_inc_not_zero(&sb->s_active))
200 if (atomic_inc_return(&server->active) != 1)
201 atomic_dec(&sb->s_active);
204 EXPORT_SYMBOL_GPL(nfs_sb_active);
206 void nfs_sb_deactive(struct super_block *sb)
208 struct nfs_server *server = NFS_SB(sb);
210 if (atomic_dec_and_test(&server->active))
211 deactivate_super(sb);
213 EXPORT_SYMBOL_GPL(nfs_sb_deactive);
215 static int __nfs_list_for_each_server(struct list_head *head,
216 int (*fn)(struct nfs_server *, void *),
219 struct nfs_server *server, *last = NULL;
223 list_for_each_entry_rcu(server, head, client_link) {
224 if (!(server->super && nfs_sb_active(server->super)))
228 nfs_sb_deactive(last->super);
230 ret = fn(server, data);
239 nfs_sb_deactive(last->super);
243 int nfs_client_for_each_server(struct nfs_client *clp,
244 int (*fn)(struct nfs_server *, void *),
247 return __nfs_list_for_each_server(&clp->cl_superblocks, fn, data);
249 EXPORT_SYMBOL_GPL(nfs_client_for_each_server);
252 * Deliver file system statistics to userspace
254 int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
256 struct nfs_server *server = NFS_SB(dentry->d_sb);
257 unsigned char blockbits;
258 unsigned long blockres;
259 struct nfs_fh *fh = NFS_FH(d_inode(dentry));
260 struct nfs_fsstat res;
263 res.fattr = nfs_alloc_fattr();
264 if (res.fattr == NULL)
267 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
268 if (unlikely(error == -ESTALE)) {
269 struct dentry *pd_dentry;
271 pd_dentry = dget_parent(dentry);
272 nfs_zap_caches(d_inode(pd_dentry));
275 nfs_free_fattr(res.fattr);
279 buf->f_type = NFS_SUPER_MAGIC;
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.
286 buf->f_frsize = dentry->d_sb->s_blocksize;
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.
295 buf->f_bsize = dentry->d_sb->s_blocksize;
296 blockbits = dentry->d_sb->s_blocksize_bits;
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;
302 buf->f_files = res.tfiles;
303 buf->f_ffree = res.afiles;
305 buf->f_namelen = server->namelen;
310 dprintk("%s: statfs error = %d\n", __func__, -error);
313 EXPORT_SYMBOL_GPL(nfs_statfs);
316 * Map the security flavour number to a name
318 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
320 static const struct {
321 rpc_authflavor_t flavour;
323 } sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
324 /* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
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" },
336 { UINT_MAX, "unknown" }
340 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
341 if (sec_flavours[i].flavour == flavour)
344 return sec_flavours[i].str;
347 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
350 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
353 switch (sap->sa_family) {
355 switch (nfss->mountd_protocol) {
357 proto = RPCBIND_NETID_UDP;
360 proto = RPCBIND_NETID_TCP;
365 switch (nfss->mountd_protocol) {
367 proto = RPCBIND_NETID_UDP6;
370 proto = RPCBIND_NETID_TCP6;
375 if (proto || showdefaults)
376 seq_printf(m, ",mountproto=%s", proto ?: "auto");
379 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
382 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
384 if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
387 switch (sap->sa_family) {
389 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
390 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
394 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
395 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
400 seq_puts(m, ",mountaddr=unspecified");
403 if (nfss->mountd_version || showdefaults)
404 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
405 if ((nfss->mountd_port &&
406 nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
408 seq_printf(m, ",mountport=%u", nfss->mountd_port);
410 nfs_show_mountd_netid(m, nfss, showdefaults);
413 #if IS_ENABLED(CONFIG_NFS_V4)
414 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
417 struct nfs_client *clp = nfss->nfs_client;
419 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
422 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
428 static void nfs_show_nfs_version(struct seq_file *m,
429 unsigned int version,
430 unsigned int minorversion)
432 seq_printf(m, ",vers=%u", version);
434 seq_printf(m, ".%u", minorversion);
438 * Describe the mount options in force on this server representation
440 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
443 static const struct proc_nfs_info {
448 { NFS_MOUNT_SOFT, ",soft", "" },
449 { NFS_MOUNT_SOFTERR, ",softerr", "" },
450 { NFS_MOUNT_SOFTREVAL, ",softreval", "" },
451 { NFS_MOUNT_POSIX, ",posix", "" },
452 { NFS_MOUNT_NOCTO, ",nocto", "" },
453 { NFS_MOUNT_NOAC, ",noac", "" },
454 { NFS_MOUNT_NONLM, ",nolock", "" },
455 { NFS_MOUNT_NOACL, ",noacl", "" },
456 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
457 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
458 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
461 const struct proc_nfs_info *nfs_infop;
462 struct nfs_client *clp = nfss->nfs_client;
463 u32 version = clp->rpc_ops->version;
464 int local_flock, local_fcntl;
466 nfs_show_nfs_version(m, version, clp->cl_minorversion);
467 seq_printf(m, ",rsize=%u", nfss->rsize);
468 seq_printf(m, ",wsize=%u", nfss->wsize);
469 if (nfss->bsize != 0)
470 seq_printf(m, ",bsize=%u", nfss->bsize);
471 seq_printf(m, ",namlen=%u", nfss->namelen);
472 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
473 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
474 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
475 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
476 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
477 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
478 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
479 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
480 if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
481 seq_puts(m, ",hard");
482 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
483 if (nfss->flags & nfs_infop->flag)
484 seq_puts(m, nfs_infop->str);
486 seq_puts(m, nfs_infop->nostr);
489 seq_printf(m, ",proto=%s",
490 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
492 if (clp->cl_nconnect > 0)
493 seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
495 if (clp->cl_max_connect > 1)
496 seq_printf(m, ",max_connect=%u", clp->cl_max_connect);
497 if (nfss->port != NFS_PORT)
498 seq_printf(m, ",port=%u", nfss->port);
501 seq_printf(m, ",port=%u", nfss->port);
503 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
504 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
505 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
506 switch (clp->cl_xprtsec.policy) {
507 case RPC_XPRTSEC_TLS_ANON:
508 seq_puts(m, ",xprtsec=tls");
510 case RPC_XPRTSEC_TLS_X509:
511 seq_puts(m, ",xprtsec=mtls");
518 nfs_show_mountd_options(m, nfss, showdefaults);
520 nfs_show_nfsv4_options(m, nfss, showdefaults);
522 if (nfss->options & NFS_OPTION_FSCACHE) {
523 #ifdef CONFIG_NFS_FSCACHE
524 if (nfss->fscache_uniq)
525 seq_printf(m, ",fsc=%s", nfss->fscache_uniq);
533 if (nfss->options & NFS_OPTION_MIGRATION)
534 seq_puts(m, ",migration");
536 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
537 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
538 seq_puts(m, ",lookupcache=none");
540 seq_puts(m, ",lookupcache=pos");
543 local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
544 local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
546 if (!local_flock && !local_fcntl)
547 seq_puts(m, ",local_lock=none");
548 else if (local_flock && local_fcntl)
549 seq_puts(m, ",local_lock=all");
550 else if (local_flock)
551 seq_puts(m, ",local_lock=flock");
553 seq_puts(m, ",local_lock=posix");
555 if (nfss->flags & NFS_MOUNT_NO_ALIGNWRITE)
556 seq_puts(m, ",noalignwrite");
558 if (nfss->flags & NFS_MOUNT_WRITE_EAGER) {
559 if (nfss->flags & NFS_MOUNT_WRITE_WAIT)
560 seq_puts(m, ",write=wait");
562 seq_puts(m, ",write=eager");
567 * Describe the mount options on this VFS mountpoint
569 int nfs_show_options(struct seq_file *m, struct dentry *root)
571 struct nfs_server *nfss = NFS_SB(root->d_sb);
573 nfs_show_mount_options(m, nfss, 0);
576 seq_printf(m, ",addr=%s",
577 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
583 EXPORT_SYMBOL_GPL(nfs_show_options);
585 #if IS_ENABLED(CONFIG_NFS_V4)
586 static void show_lease(struct seq_file *m, struct nfs_server *server)
588 struct nfs_client *clp = server->nfs_client;
589 unsigned long expire;
591 seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
592 expire = clp->cl_last_renewal + clp->cl_lease_time;
593 seq_printf(m, ",lease_expired=%ld",
594 time_after(expire, jiffies) ? 0 : (jiffies - expire) / HZ);
596 #ifdef CONFIG_NFS_V4_1
597 static void show_sessions(struct seq_file *m, struct nfs_server *server)
599 if (nfs4_has_session(server->nfs_client))
600 seq_puts(m, ",sessions");
603 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
607 #ifdef CONFIG_NFS_V4_1
608 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
610 seq_printf(m, ",pnfs=");
611 if (server->pnfs_curr_ld)
612 seq_printf(m, "%s", server->pnfs_curr_ld->name);
614 seq_printf(m, "not configured");
617 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
619 if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
620 struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
621 seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
623 impl_id->name, impl_id->domain,
624 impl_id->date.seconds, impl_id->date.nseconds);
628 #if IS_ENABLED(CONFIG_NFS_V4)
629 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
633 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
638 int nfs_show_devname(struct seq_file *m, struct dentry *root)
640 char *page = (char *) __get_free_page(GFP_KERNEL);
641 char *devname, *dummy;
645 devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
647 err = PTR_ERR(devname);
649 seq_escape(m, devname, " \t\n\\");
650 free_page((unsigned long)page);
653 EXPORT_SYMBOL_GPL(nfs_show_devname);
655 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
660 EXPORT_SYMBOL_GPL(nfs_show_path);
663 * Present statistical information for this VFS mountpoint
665 int nfs_show_stats(struct seq_file *m, struct dentry *root)
668 struct nfs_server *nfss = NFS_SB(root->d_sb);
669 struct rpc_auth *auth = nfss->client->cl_auth;
670 struct nfs_iostats totals = { };
672 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
675 * Display all mount option settings
677 seq_puts(m, "\n\topts:\t");
678 seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
679 seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
680 seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
681 seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
682 nfs_show_mount_options(m, nfss, 1);
684 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
686 show_implementation_id(m, nfss);
688 seq_puts(m, "\n\tcaps:\t");
689 seq_printf(m, "caps=0x%x", nfss->caps);
690 seq_printf(m, ",wtmult=%u", nfss->wtmult);
691 seq_printf(m, ",dtsize=%u", nfss->dtsize);
692 seq_printf(m, ",bsize=%u", nfss->bsize);
693 seq_printf(m, ",namlen=%u", nfss->namelen);
695 #if IS_ENABLED(CONFIG_NFS_V4)
696 if (nfss->nfs_client->rpc_ops->version == 4) {
697 seq_puts(m, "\n\tnfsv4:\t");
698 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
699 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
700 seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
701 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
702 show_sessions(m, nfss);
709 * Display security flavor in effect for this mount
711 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
713 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
716 * Display superblock I/O counters
718 for_each_possible_cpu(cpu) {
719 struct nfs_iostats *stats;
722 stats = per_cpu_ptr(nfss->io_stats, cpu);
724 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
725 totals.events[i] += stats->events[i];
726 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
727 totals.bytes[i] += stats->bytes[i];
732 seq_puts(m, "\n\tevents:\t");
733 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
734 seq_printf(m, "%lu ", totals.events[i]);
735 seq_puts(m, "\n\tbytes:\t");
736 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
737 seq_printf(m, "%Lu ", totals.bytes[i]);
740 rpc_clnt_show_stats(m, nfss->client);
744 EXPORT_SYMBOL_GPL(nfs_show_stats);
747 * Begin unmount by attempting to remove all automounted mountpoints we added
748 * in response to xdev traversals and referrals
750 void nfs_umount_begin(struct super_block *sb)
752 struct nfs_server *server;
753 struct rpc_clnt *rpc;
756 /* -EIO all pending I/O */
757 rpc = server->client_acl;
759 rpc_killall_tasks(rpc);
760 rpc = server->client;
762 rpc_killall_tasks(rpc);
764 EXPORT_SYMBOL_GPL(nfs_umount_begin);
767 * Return true if 'match' is in auth_info or auth_info is empty.
768 * Return false otherwise.
770 bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
771 rpc_authflavor_t match)
775 if (!auth_info->flavor_len)
778 for (i = 0; i < auth_info->flavor_len; i++) {
779 if (auth_info->flavors[i] == match)
784 EXPORT_SYMBOL_GPL(nfs_auth_info_match);
787 * Ensure that a specified authtype in ctx->auth_info is supported by
788 * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
791 static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
792 rpc_authflavor_t *server_authlist,
795 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
796 bool found_auth_null = false;
800 * If the sec= mount option is used, the specified flavor or AUTH_NULL
801 * must be in the list returned by the server.
803 * AUTH_NULL has a special meaning when it's in the server list - it
804 * means that the server will ignore the rpc creds, so any flavor
805 * can be used but still use the sec= that was specified.
807 * Note also that the MNT procedure in MNTv1 does not return a list
808 * of supported security flavors. In this case, nfs_mount() fabricates
809 * a security flavor list containing just AUTH_NULL.
811 for (i = 0; i < count; i++) {
812 flavor = server_authlist[i];
814 if (nfs_auth_info_match(&ctx->auth_info, flavor))
817 if (flavor == RPC_AUTH_NULL)
818 found_auth_null = true;
821 if (found_auth_null) {
822 flavor = ctx->auth_info.flavors[0];
827 "NFS: specified auth flavors not supported by server\n");
831 ctx->selected_flavor = flavor;
832 dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
837 * Use the remote server's MOUNT service to request the NFS file handle
838 * corresponding to the provided path.
840 static int nfs_request_mount(struct fs_context *fc,
841 struct nfs_fh *root_fh,
842 rpc_authflavor_t *server_authlist,
843 unsigned int *server_authlist_len)
845 struct nfs_fs_context *ctx = nfs_fc2context(fc);
846 struct nfs_mount_request request = {
847 .sap = &ctx->mount_server._address,
848 .dirpath = ctx->nfs_server.export_path,
849 .protocol = ctx->mount_server.protocol,
851 .noresvport = ctx->flags & NFS_MOUNT_NORESVPORT,
852 .auth_flav_len = server_authlist_len,
853 .auth_flavs = server_authlist,
858 if (ctx->mount_server.version == 0) {
859 switch (ctx->version) {
861 ctx->mount_server.version = NFS_MNT3_VERSION;
864 ctx->mount_server.version = NFS_MNT_VERSION;
867 request.version = ctx->mount_server.version;
869 if (ctx->mount_server.hostname)
870 request.hostname = ctx->mount_server.hostname;
872 request.hostname = ctx->nfs_server.hostname;
875 * Construct the mount server's address.
877 if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
878 memcpy(request.sap, &ctx->nfs_server._address,
879 ctx->nfs_server.addrlen);
880 ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
882 request.salen = ctx->mount_server.addrlen;
883 nfs_set_port(request.sap, &ctx->mount_server.port, 0);
886 * Now ask the mount server to map our export path
889 if ((request.protocol == XPRT_TRANSPORT_UDP) ==
890 !(ctx->flags & NFS_MOUNT_TCP))
892 * NFS protocol and mount protocol are both UDP or neither UDP
893 * so timeouts are compatible. Use NFS timeouts for MOUNT
895 status = nfs_mount(&request, ctx->timeo, ctx->retrans);
897 status = nfs_mount(&request, NFS_UNSPEC_TIMEO, NFS_UNSPEC_RETRANS);
899 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
900 request.hostname, status);
907 static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
909 struct nfs_fs_context *ctx = nfs_fc2context(fc);
912 bool tried_auth_unix = false;
913 bool auth_null_in_list = false;
914 struct nfs_server *server = ERR_PTR(-EACCES);
915 rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
916 unsigned int authlist_len = ARRAY_SIZE(authlist);
918 /* make sure 'nolock'/'lock' override the 'local_lock' mount option */
919 if (ctx->lock_status) {
920 if (ctx->lock_status == NFS_LOCK_NOLOCK) {
921 ctx->flags |= NFS_MOUNT_NONLM;
922 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
924 ctx->flags &= ~NFS_MOUNT_NONLM;
925 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
928 status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
930 return ERR_PTR(status);
933 * Was a sec= authflavor specified in the options? First, verify
934 * whether the server supports it, and then just try to use it if so.
936 if (ctx->auth_info.flavor_len > 0) {
937 status = nfs_verify_authflavors(ctx, authlist, authlist_len);
938 dfprintk(MOUNT, "NFS: using auth flavor %u\n",
939 ctx->selected_flavor);
941 return ERR_PTR(status);
942 return ctx->nfs_mod->rpc_ops->create_server(fc);
946 * No sec= option was provided. RFC 2623, section 2.7 suggests we
947 * SHOULD prefer the flavor listed first. However, some servers list
948 * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
950 for (i = 0; i < authlist_len; ++i) {
951 rpc_authflavor_t flavor;
952 struct rpcsec_gss_info info;
954 flavor = authlist[i];
957 tried_auth_unix = true;
960 auth_null_in_list = true;
963 if (rpcauth_get_gssinfo(flavor, &info) != 0)
967 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
968 ctx->selected_flavor = flavor;
969 server = ctx->nfs_mod->rpc_ops->create_server(fc);
975 * Nothing we tried so far worked. At this point, give up if we've
976 * already tried AUTH_UNIX or if the server's list doesn't contain
979 if (tried_auth_unix || !auth_null_in_list)
982 /* Last chance! Try AUTH_UNIX */
983 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
984 ctx->selected_flavor = RPC_AUTH_UNIX;
985 return ctx->nfs_mod->rpc_ops->create_server(fc);
988 int nfs_try_get_tree(struct fs_context *fc)
990 struct nfs_fs_context *ctx = nfs_fc2context(fc);
993 ctx->server = nfs_try_mount_request(fc);
995 ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
997 return nfs_get_tree_common(fc);
999 EXPORT_SYMBOL_GPL(nfs_try_get_tree);
1002 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
1003 | NFS_MOUNT_SECURE \
1006 | NFS_MOUNT_KERBEROS \
1008 | NFS_MOUNT_BROKEN_SUID \
1009 | NFS_MOUNT_STRICTLOCK \
1010 | NFS_MOUNT_LEGACY_INTERFACE)
1012 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
1013 ~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
1016 nfs_compare_remount_data(struct nfs_server *nfss,
1017 struct nfs_fs_context *ctx)
1019 if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
1020 ctx->rsize != nfss->rsize ||
1021 ctx->wsize != nfss->wsize ||
1022 ctx->version != nfss->nfs_client->rpc_ops->version ||
1023 ctx->minorversion != nfss->nfs_client->cl_minorversion ||
1024 ctx->retrans != nfss->client->cl_timeout->to_retries ||
1025 !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
1026 ctx->acregmin != nfss->acregmin / HZ ||
1027 ctx->acregmax != nfss->acregmax / HZ ||
1028 ctx->acdirmin != nfss->acdirmin / HZ ||
1029 ctx->acdirmax != nfss->acdirmax / HZ ||
1030 ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1031 (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
1032 ctx->nfs_server.port != nfss->port ||
1033 ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1034 !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
1035 (struct sockaddr *)&nfss->nfs_client->cl_addr))
1041 int nfs_reconfigure(struct fs_context *fc)
1043 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1044 struct super_block *sb = fc->root->d_sb;
1045 struct nfs_server *nfss = sb->s_fs_info;
1048 sync_filesystem(sb);
1051 * Userspace mount programs that send binary options generally send
1052 * them populated with default values. We have no way to know which
1053 * ones were explicitly specified. Fall back to legacy behavior and
1054 * just return success.
1056 if (ctx->skip_reconfig_option_check)
1060 * noac is a special case. It implies -o sync, but that's not
1061 * necessarily reflected in the mtab options. reconfigure_super
1062 * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1063 * remount options, so we have to explicitly reset it.
1065 if (ctx->flags & NFS_MOUNT_NOAC) {
1066 fc->sb_flags |= SB_SYNCHRONOUS;
1067 fc->sb_flags_mask |= SB_SYNCHRONOUS;
1070 /* compare new mount options with old ones */
1071 ret = nfs_compare_remount_data(nfss, ctx);
1075 return nfs_probe_server(nfss, NFS_FH(d_inode(fc->root)));
1077 EXPORT_SYMBOL_GPL(nfs_reconfigure);
1080 * Finish setting up an NFS superblock
1082 static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
1084 struct nfs_server *server = NFS_SB(sb);
1086 sb->s_blocksize_bits = 0;
1087 sb->s_blocksize = 0;
1088 sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1089 sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1091 sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
1093 switch (server->nfs_client->rpc_ops->version) {
1095 sb->s_time_gran = 1000;
1097 sb->s_time_max = U32_MAX;
1101 * The VFS shouldn't apply the umask to mode bits.
1102 * We will do so ourselves when necessary.
1104 sb->s_flags |= SB_POSIXACL;
1105 sb->s_time_gran = 1;
1107 sb->s_time_max = U32_MAX;
1108 sb->s_export_op = &nfs_export_ops;
1111 sb->s_iflags |= SB_I_NOUMASK;
1112 sb->s_time_gran = 1;
1113 sb->s_time_min = S64_MIN;
1114 sb->s_time_max = S64_MAX;
1115 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1116 sb->s_export_op = &nfs_export_ops;
1120 sb->s_magic = NFS_SUPER_MAGIC;
1122 /* We probably want something more informative here */
1123 snprintf(sb->s_id, sizeof(sb->s_id),
1124 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1126 if (sb->s_blocksize == 0)
1127 sb->s_blocksize = nfs_block_bits(server->wsize,
1128 &sb->s_blocksize_bits);
1130 nfs_super_set_maxbytes(sb, server->maxfilesize);
1131 nfs_sysfs_move_server_to_sb(sb);
1132 server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
1135 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
1136 const struct fs_context *fc)
1138 const struct nfs_server *a = s->s_fs_info;
1139 const struct rpc_clnt *clnt_a = a->client;
1140 const struct rpc_clnt *clnt_b = b->client;
1142 if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
1144 if (a->nfs_client != b->nfs_client)
1146 if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1148 if (a->wsize != b->wsize)
1150 if (a->rsize != b->rsize)
1152 if (a->acregmin != b->acregmin)
1154 if (a->acregmax != b->acregmax)
1156 if (a->acdirmin != b->acdirmin)
1158 if (a->acdirmax != b->acdirmax)
1160 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1167 static int nfs_set_super(struct super_block *s, struct fs_context *fc)
1169 struct nfs_server *server = fc->s_fs_info;
1172 s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1173 ret = set_anon_super(s, server);
1175 server->s_dev = s->s_dev;
1179 static int nfs_compare_super_address(struct nfs_server *server1,
1180 struct nfs_server *server2)
1182 struct sockaddr *sap1, *sap2;
1183 struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1184 struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1186 if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1189 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1190 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1192 if (sap1->sa_family != sap2->sa_family)
1195 switch (sap1->sa_family) {
1197 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1198 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1199 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1201 if (sin1->sin_port != sin2->sin_port)
1206 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1207 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1208 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1210 if (sin1->sin6_port != sin2->sin6_port)
1221 static int nfs_compare_userns(const struct nfs_server *old,
1222 const struct nfs_server *new)
1224 const struct user_namespace *oldns = &init_user_ns;
1225 const struct user_namespace *newns = &init_user_ns;
1227 if (old->client && old->client->cl_cred)
1228 oldns = old->client->cl_cred->user_ns;
1229 if (new->client && new->client->cl_cred)
1230 newns = new->client->cl_cred->user_ns;
1236 static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
1238 struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
1240 if (!nfs_compare_super_address(old, server))
1242 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1243 if (old->flags & NFS_MOUNT_UNSHARED)
1245 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1247 if (!nfs_compare_userns(old, server))
1249 if ((old->has_sec_mnt_opts || fc->security) &&
1250 security_sb_mnt_opts_compat(sb, fc->security))
1252 return nfs_compare_mount_options(sb, server, fc);
1255 #ifdef CONFIG_NFS_FSCACHE
1256 static int nfs_get_cache_cookie(struct super_block *sb,
1257 struct nfs_fs_context *ctx)
1259 struct nfs_server *nfss = NFS_SB(sb);
1263 nfss->fscache = NULL;
1268 if (ctx->clone_data.sb) {
1269 struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
1270 if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1272 if (mnt_s->fscache_uniq) {
1273 uniq = mnt_s->fscache_uniq;
1274 ulen = strlen(uniq);
1277 if (!(ctx->options & NFS_OPTION_FSCACHE))
1279 if (ctx->fscache_uniq) {
1280 uniq = ctx->fscache_uniq;
1281 ulen = strlen(ctx->fscache_uniq);
1285 return nfs_fscache_get_super_cookie(sb, uniq, ulen);
1288 static int nfs_get_cache_cookie(struct super_block *sb,
1289 struct nfs_fs_context *ctx)
1295 int nfs_get_tree_common(struct fs_context *fc)
1297 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1298 struct super_block *s;
1299 int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
1300 struct nfs_server *server = ctx->server;
1305 return PTR_ERR(server);
1307 if (server->flags & NFS_MOUNT_UNSHARED)
1308 compare_super = NULL;
1310 /* -o noac implies -o sync */
1311 if (server->flags & NFS_MOUNT_NOAC)
1312 fc->sb_flags |= SB_SYNCHRONOUS;
1314 if (ctx->clone_data.sb)
1315 if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
1316 fc->sb_flags |= SB_SYNCHRONOUS;
1318 /* Get a superblock - note that we may end up sharing one that already exists */
1319 fc->s_fs_info = server;
1320 s = sget_fc(fc, compare_super, nfs_set_super);
1321 fc->s_fs_info = NULL;
1324 nfs_errorf(fc, "NFS: Couldn't get superblock");
1328 if (s->s_fs_info != server) {
1329 nfs_free_server(server);
1332 error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1333 MINOR(server->s_dev));
1335 goto error_splat_super;
1336 s->s_bdi->io_pages = server->rpages;
1341 unsigned bsize = ctx->clone_data.inherited_bsize;
1342 /* initial superblock/root creation */
1343 nfs_fill_super(s, ctx);
1345 s->s_blocksize_bits = bsize;
1346 s->s_blocksize = 1U << bsize;
1348 error = nfs_get_cache_cookie(s, ctx);
1350 goto error_splat_super;
1353 error = nfs_get_root(s, fc);
1355 nfs_errorf(fc, "NFS: Couldn't get root dentry");
1356 goto error_splat_super;
1359 s->s_flags |= SB_ACTIVE;
1366 nfs_free_server(server);
1369 deactivate_locked_super(s);
1374 * Destroy an NFS superblock
1376 void nfs_kill_super(struct super_block *s)
1378 struct nfs_server *server = NFS_SB(s);
1380 nfs_sysfs_move_sb_to_server(server);
1383 nfs_fscache_release_super_cookie(s);
1385 nfs_free_server(server);
1387 EXPORT_SYMBOL_GPL(nfs_kill_super);
1389 #if IS_ENABLED(CONFIG_NFS_V4)
1392 * NFS v4 module parameters need to stay in the
1393 * NFS client for backwards compatibility
1395 unsigned int nfs_callback_set_tcpport;
1396 unsigned short nfs_callback_nr_threads;
1397 /* Default cache timeout is 10 minutes */
1398 unsigned int nfs_idmap_cache_timeout = 600;
1399 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1400 bool nfs4_disable_idmapping = true;
1401 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1402 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1403 unsigned short send_implementation_id = 1;
1404 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1405 bool recover_lost_locks = false;
1406 short nfs_delay_retrans = -1;
1408 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1409 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1410 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1411 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1412 EXPORT_SYMBOL_GPL(max_session_slots);
1413 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1414 EXPORT_SYMBOL_GPL(send_implementation_id);
1415 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1416 EXPORT_SYMBOL_GPL(recover_lost_locks);
1417 EXPORT_SYMBOL_GPL(nfs_delay_retrans);
1419 #define NFS_CALLBACK_MAXPORTNR (65535U)
1421 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1428 ret = kstrtoul(val, 0, &num);
1429 if (ret || num > NFS_CALLBACK_MAXPORTNR)
1431 *((unsigned int *)kp->arg) = num;
1434 static const struct kernel_param_ops param_ops_portnr = {
1435 .set = param_set_portnr,
1436 .get = param_get_uint,
1438 #define param_check_portnr(name, p) __param_check(name, p, unsigned int)
1440 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1441 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1442 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1443 "assigned to the NFSv4 callback channels.");
1444 module_param(nfs_idmap_cache_timeout, int, 0644);
1445 module_param(nfs4_disable_idmapping, bool, 0644);
1446 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1447 NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1448 MODULE_PARM_DESC(nfs4_disable_idmapping,
1449 "Turn off NFSv4 idmapping when using 'sec=sys'");
1450 module_param(max_session_slots, ushort, 0644);
1451 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1452 "requests the client will negotiate");
1453 module_param(max_session_cb_slots, ushort, 0644);
1454 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1455 "callbacks the client will process for a given server");
1456 module_param(send_implementation_id, ushort, 0644);
1457 MODULE_PARM_DESC(send_implementation_id,
1458 "Send implementation ID with NFSv4.1 exchange_id");
1459 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1461 module_param(recover_lost_locks, bool, 0644);
1462 MODULE_PARM_DESC(recover_lost_locks,
1463 "If the server reports that a lock might be lost, "
1464 "try to recover it risking data corruption.");
1466 module_param_named(delay_retrans, nfs_delay_retrans, short, 0644);
1467 MODULE_PARM_DESC(delay_retrans,
1468 "Unless negative, specifies the number of times the NFSv4 "
1469 "client retries a request before returning an EAGAIN error, "
1470 "after a reply of NFS4ERR_DELAY from the server.");
1471 #endif /* CONFIG_NFS_V4 */