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