NFS: Move mount parameterisation bits into their own file
[linux-block.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 args->auth_info is supported by
730  * the server. Returns 0 and sets args->selected_flavor if it's ok, and
731  * -EACCES if not.
732  */
733 static int nfs_verify_authflavors(struct nfs_parsed_mount_data *args,
734                         rpc_authflavor_t *server_authlist, unsigned int count)
735 {
736         rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
737         bool found_auth_null = false;
738         unsigned int i;
739
740         /*
741          * If the sec= mount option is used, the specified flavor or AUTH_NULL
742          * must be in the list returned by the server.
743          *
744          * AUTH_NULL has a special meaning when it's in the server list - it
745          * means that the server will ignore the rpc creds, so any flavor
746          * can be used but still use the sec= that was specified.
747          *
748          * Note also that the MNT procedure in MNTv1 does not return a list
749          * of supported security flavors. In this case, nfs_mount() fabricates
750          * a security flavor list containing just AUTH_NULL.
751          */
752         for (i = 0; i < count; i++) {
753                 flavor = server_authlist[i];
754
755                 if (nfs_auth_info_match(&args->auth_info, flavor))
756                         goto out;
757
758                 if (flavor == RPC_AUTH_NULL)
759                         found_auth_null = true;
760         }
761
762         if (found_auth_null) {
763                 flavor = args->auth_info.flavors[0];
764                 goto out;
765         }
766
767         dfprintk(MOUNT,
768                  "NFS: specified auth flavors not supported by server\n");
769         return -EACCES;
770
771 out:
772         args->selected_flavor = flavor;
773         dfprintk(MOUNT, "NFS: using auth flavor %u\n", args->selected_flavor);
774         return 0;
775 }
776
777 /*
778  * Use the remote server's MOUNT service to request the NFS file handle
779  * corresponding to the provided path.
780  */
781 static int nfs_request_mount(struct nfs_parsed_mount_data *args,
782                              struct nfs_fh *root_fh,
783                              rpc_authflavor_t *server_authlist,
784                              unsigned int *server_authlist_len)
785 {
786         struct nfs_mount_request request = {
787                 .sap            = (struct sockaddr *)
788                                                 &args->mount_server.address,
789                 .dirpath        = args->nfs_server.export_path,
790                 .protocol       = args->mount_server.protocol,
791                 .fh             = root_fh,
792                 .noresvport     = args->flags & NFS_MOUNT_NORESVPORT,
793                 .auth_flav_len  = server_authlist_len,
794                 .auth_flavs     = server_authlist,
795                 .net            = args->net,
796         };
797         int status;
798
799         if (args->mount_server.version == 0) {
800                 switch (args->version) {
801                         default:
802                                 args->mount_server.version = NFS_MNT3_VERSION;
803                                 break;
804                         case 2:
805                                 args->mount_server.version = NFS_MNT_VERSION;
806                 }
807         }
808         request.version = args->mount_server.version;
809
810         if (args->mount_server.hostname)
811                 request.hostname = args->mount_server.hostname;
812         else
813                 request.hostname = args->nfs_server.hostname;
814
815         /*
816          * Construct the mount server's address.
817          */
818         if (args->mount_server.address.ss_family == AF_UNSPEC) {
819                 memcpy(request.sap, &args->nfs_server.address,
820                        args->nfs_server.addrlen);
821                 args->mount_server.addrlen = args->nfs_server.addrlen;
822         }
823         request.salen = args->mount_server.addrlen;
824         nfs_set_port(request.sap, &args->mount_server.port, 0);
825
826         /*
827          * Now ask the mount server to map our export path
828          * to a file handle.
829          */
830         status = nfs_mount(&request);
831         if (status != 0) {
832                 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
833                                 request.hostname, status);
834                 return status;
835         }
836
837         return 0;
838 }
839
840 static struct nfs_server *nfs_try_mount_request(struct nfs_mount_info *mount_info)
841 {
842         int status;
843         unsigned int i;
844         bool tried_auth_unix = false;
845         bool auth_null_in_list = false;
846         struct nfs_server *server = ERR_PTR(-EACCES);
847         struct nfs_parsed_mount_data *args = mount_info->parsed;
848         rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
849         unsigned int authlist_len = ARRAY_SIZE(authlist);
850         struct nfs_subversion *nfs_mod = mount_info->nfs_mod;
851
852         status = nfs_request_mount(args, mount_info->mntfh, authlist,
853                                         &authlist_len);
854         if (status)
855                 return ERR_PTR(status);
856
857         /*
858          * Was a sec= authflavor specified in the options? First, verify
859          * whether the server supports it, and then just try to use it if so.
860          */
861         if (args->auth_info.flavor_len > 0) {
862                 status = nfs_verify_authflavors(args, authlist, authlist_len);
863                 dfprintk(MOUNT, "NFS: using auth flavor %u\n",
864                          args->selected_flavor);
865                 if (status)
866                         return ERR_PTR(status);
867                 return nfs_mod->rpc_ops->create_server(mount_info);
868         }
869
870         /*
871          * No sec= option was provided. RFC 2623, section 2.7 suggests we
872          * SHOULD prefer the flavor listed first. However, some servers list
873          * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
874          */
875         for (i = 0; i < authlist_len; ++i) {
876                 rpc_authflavor_t flavor;
877                 struct rpcsec_gss_info info;
878
879                 flavor = authlist[i];
880                 switch (flavor) {
881                 case RPC_AUTH_UNIX:
882                         tried_auth_unix = true;
883                         break;
884                 case RPC_AUTH_NULL:
885                         auth_null_in_list = true;
886                         continue;
887                 default:
888                         if (rpcauth_get_gssinfo(flavor, &info) != 0)
889                                 continue;
890                         /* Fallthrough */
891                 }
892                 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
893                 args->selected_flavor = flavor;
894                 server = nfs_mod->rpc_ops->create_server(mount_info);
895                 if (!IS_ERR(server))
896                         return server;
897         }
898
899         /*
900          * Nothing we tried so far worked. At this point, give up if we've
901          * already tried AUTH_UNIX or if the server's list doesn't contain
902          * AUTH_NULL
903          */
904         if (tried_auth_unix || !auth_null_in_list)
905                 return server;
906
907         /* Last chance! Try AUTH_UNIX */
908         dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
909         args->selected_flavor = RPC_AUTH_UNIX;
910         return nfs_mod->rpc_ops->create_server(mount_info);
911 }
912
913 static struct dentry *nfs_fs_mount_common(int, const char *, struct nfs_mount_info *);
914
915 struct dentry *nfs_try_mount(int flags, const char *dev_name,
916                              struct nfs_mount_info *mount_info)
917 {
918         struct nfs_subversion *nfs_mod = mount_info->nfs_mod;
919         if (mount_info->parsed->need_mount)
920                 mount_info->server = nfs_try_mount_request(mount_info);
921         else
922                 mount_info->server = nfs_mod->rpc_ops->create_server(mount_info);
923
924         return nfs_fs_mount_common(flags, dev_name, mount_info);
925 }
926 EXPORT_SYMBOL_GPL(nfs_try_mount);
927
928 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
929                 | NFS_MOUNT_SECURE \
930                 | NFS_MOUNT_TCP \
931                 | NFS_MOUNT_VER3 \
932                 | NFS_MOUNT_KERBEROS \
933                 | NFS_MOUNT_NONLM \
934                 | NFS_MOUNT_BROKEN_SUID \
935                 | NFS_MOUNT_STRICTLOCK \
936                 | NFS_MOUNT_LEGACY_INTERFACE)
937
938 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
939                 ~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
940
941 static int
942 nfs_compare_remount_data(struct nfs_server *nfss,
943                          struct nfs_parsed_mount_data *data)
944 {
945         if ((data->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
946             data->rsize != nfss->rsize ||
947             data->wsize != nfss->wsize ||
948             data->version != nfss->nfs_client->rpc_ops->version ||
949             data->minorversion != nfss->nfs_client->cl_minorversion ||
950             data->retrans != nfss->client->cl_timeout->to_retries ||
951             !nfs_auth_info_match(&data->auth_info, nfss->client->cl_auth->au_flavor) ||
952             data->acregmin != nfss->acregmin / HZ ||
953             data->acregmax != nfss->acregmax / HZ ||
954             data->acdirmin != nfss->acdirmin / HZ ||
955             data->acdirmax != nfss->acdirmax / HZ ||
956             data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
957             (data->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
958             data->nfs_server.port != nfss->port ||
959             data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
960             !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
961                           (struct sockaddr *)&nfss->nfs_client->cl_addr))
962                 return -EINVAL;
963
964         return 0;
965 }
966
967 int
968 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
969 {
970         int error;
971         struct nfs_server *nfss = sb->s_fs_info;
972         struct nfs_parsed_mount_data *data;
973         struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
974         struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
975         u32 nfsvers = nfss->nfs_client->rpc_ops->version;
976
977         sync_filesystem(sb);
978
979         /*
980          * Userspace mount programs that send binary options generally send
981          * them populated with default values. We have no way to know which
982          * ones were explicitly specified. Fall back to legacy behavior and
983          * just return success.
984          */
985         if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
986             (nfsvers <= 3 && (!options || (options->version >= 1 &&
987                                            options->version <= 6))))
988                 return 0;
989
990         data = nfs_alloc_parsed_mount_data();
991         if (data == NULL)
992                 return -ENOMEM;
993
994         /* fill out struct with values from existing mount */
995         data->flags = nfss->flags;
996         data->rsize = nfss->rsize;
997         data->wsize = nfss->wsize;
998         data->retrans = nfss->client->cl_timeout->to_retries;
999         data->selected_flavor = nfss->client->cl_auth->au_flavor;
1000         data->acregmin = nfss->acregmin / HZ;
1001         data->acregmax = nfss->acregmax / HZ;
1002         data->acdirmin = nfss->acdirmin / HZ;
1003         data->acdirmax = nfss->acdirmax / HZ;
1004         data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1005         data->nfs_server.port = nfss->port;
1006         data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1007         data->version = nfsvers;
1008         data->minorversion = nfss->nfs_client->cl_minorversion;
1009         data->net = current->nsproxy->net_ns;
1010         memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1011                 data->nfs_server.addrlen);
1012
1013         /* overwrite those values with any that were specified */
1014         error = -EINVAL;
1015         if (!nfs_parse_mount_options((char *)options, data))
1016                 goto out;
1017
1018         /*
1019          * noac is a special case. It implies -o sync, but that's not
1020          * necessarily reflected in the mtab options. do_remount_sb
1021          * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
1022          * remount options, so we have to explicitly reset it.
1023          */
1024         if (data->flags & NFS_MOUNT_NOAC)
1025                 *flags |= SB_SYNCHRONOUS;
1026
1027         /* compare new mount options with old ones */
1028         error = nfs_compare_remount_data(nfss, data);
1029         if (!error)
1030                 error = security_sb_remount(sb, data->lsm_opts);
1031 out:
1032         nfs_free_parsed_mount_data(data);
1033         return error;
1034 }
1035 EXPORT_SYMBOL_GPL(nfs_remount);
1036
1037 /*
1038  * Finish setting up an NFS superblock
1039  */
1040 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_info *mount_info)
1041 {
1042         struct nfs_parsed_mount_data *data = mount_info->parsed;
1043         struct nfs_server *server = NFS_SB(sb);
1044
1045         sb->s_blocksize_bits = 0;
1046         sb->s_blocksize = 0;
1047         sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
1048         sb->s_op = server->nfs_client->cl_nfs_mod->sops;
1049         if (data && data->bsize)
1050                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1051
1052         if (server->nfs_client->rpc_ops->version != 2) {
1053                 /* The VFS shouldn't apply the umask to mode bits. We will do
1054                  * so ourselves when necessary.
1055                  */
1056                 sb->s_flags |= SB_POSIXACL;
1057                 sb->s_time_gran = 1;
1058                 sb->s_export_op = &nfs_export_ops;
1059         } else
1060                 sb->s_time_gran = 1000;
1061
1062         if (server->nfs_client->rpc_ops->version != 4) {
1063                 sb->s_time_min = 0;
1064                 sb->s_time_max = U32_MAX;
1065         } else {
1066                 sb->s_time_min = S64_MIN;
1067                 sb->s_time_max = S64_MAX;
1068         }
1069
1070         sb->s_magic = NFS_SUPER_MAGIC;
1071
1072         /* We probably want something more informative here */
1073         snprintf(sb->s_id, sizeof(sb->s_id),
1074                  "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1075
1076         if (sb->s_blocksize == 0)
1077                 sb->s_blocksize = nfs_block_bits(server->wsize,
1078                                                  &sb->s_blocksize_bits);
1079
1080         nfs_super_set_maxbytes(sb, server->maxfilesize);
1081 }
1082
1083 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1084 {
1085         const struct nfs_server *a = s->s_fs_info;
1086         const struct rpc_clnt *clnt_a = a->client;
1087         const struct rpc_clnt *clnt_b = b->client;
1088
1089         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1090                 goto Ebusy;
1091         if (a->nfs_client != b->nfs_client)
1092                 goto Ebusy;
1093         if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
1094                 goto Ebusy;
1095         if (a->wsize != b->wsize)
1096                 goto Ebusy;
1097         if (a->rsize != b->rsize)
1098                 goto Ebusy;
1099         if (a->acregmin != b->acregmin)
1100                 goto Ebusy;
1101         if (a->acregmax != b->acregmax)
1102                 goto Ebusy;
1103         if (a->acdirmin != b->acdirmin)
1104                 goto Ebusy;
1105         if (a->acdirmax != b->acdirmax)
1106                 goto Ebusy;
1107         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1108                 goto Ebusy;
1109         return 1;
1110 Ebusy:
1111         return 0;
1112 }
1113
1114 struct nfs_sb_mountdata {
1115         struct nfs_server *server;
1116         int mntflags;
1117 };
1118
1119 static int nfs_set_super(struct super_block *s, void *data)
1120 {
1121         struct nfs_sb_mountdata *sb_mntdata = data;
1122         struct nfs_server *server = sb_mntdata->server;
1123         int ret;
1124
1125         s->s_flags = sb_mntdata->mntflags;
1126         s->s_fs_info = server;
1127         s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
1128         ret = set_anon_super(s, server);
1129         if (ret == 0)
1130                 server->s_dev = s->s_dev;
1131         return ret;
1132 }
1133
1134 static int nfs_compare_super_address(struct nfs_server *server1,
1135                                      struct nfs_server *server2)
1136 {
1137         struct sockaddr *sap1, *sap2;
1138         struct rpc_xprt *xprt1 = server1->client->cl_xprt;
1139         struct rpc_xprt *xprt2 = server2->client->cl_xprt;
1140
1141         if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
1142                 return 0;
1143
1144         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1145         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1146
1147         if (sap1->sa_family != sap2->sa_family)
1148                 return 0;
1149
1150         switch (sap1->sa_family) {
1151         case AF_INET: {
1152                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1153                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1154                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1155                         return 0;
1156                 if (sin1->sin_port != sin2->sin_port)
1157                         return 0;
1158                 break;
1159         }
1160         case AF_INET6: {
1161                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1162                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1163                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1164                         return 0;
1165                 if (sin1->sin6_port != sin2->sin6_port)
1166                         return 0;
1167                 break;
1168         }
1169         default:
1170                 return 0;
1171         }
1172
1173         return 1;
1174 }
1175
1176 static int nfs_compare_userns(const struct nfs_server *old,
1177                 const struct nfs_server *new)
1178 {
1179         const struct user_namespace *oldns = &init_user_ns;
1180         const struct user_namespace *newns = &init_user_ns;
1181
1182         if (old->client && old->client->cl_cred)
1183                 oldns = old->client->cl_cred->user_ns;
1184         if (new->client && new->client->cl_cred)
1185                 newns = new->client->cl_cred->user_ns;
1186         if (oldns != newns)
1187                 return 0;
1188         return 1;
1189 }
1190
1191 static int nfs_compare_super(struct super_block *sb, void *data)
1192 {
1193         struct nfs_sb_mountdata *sb_mntdata = data;
1194         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1195         int mntflags = sb_mntdata->mntflags;
1196
1197         if (!nfs_compare_super_address(old, server))
1198                 return 0;
1199         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1200         if (old->flags & NFS_MOUNT_UNSHARED)
1201                 return 0;
1202         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1203                 return 0;
1204         if (!nfs_compare_userns(old, server))
1205                 return 0;
1206         return nfs_compare_mount_options(sb, server, mntflags);
1207 }
1208
1209 #ifdef CONFIG_NFS_FSCACHE
1210 static void nfs_get_cache_cookie(struct super_block *sb,
1211                                  struct nfs_parsed_mount_data *parsed,
1212                                  struct nfs_clone_mount *cloned)
1213 {
1214         struct nfs_server *nfss = NFS_SB(sb);
1215         char *uniq = NULL;
1216         int ulen = 0;
1217
1218         nfss->fscache_key = NULL;
1219         nfss->fscache = NULL;
1220
1221         if (parsed) {
1222                 if (!(parsed->options & NFS_OPTION_FSCACHE))
1223                         return;
1224                 if (parsed->fscache_uniq) {
1225                         uniq = parsed->fscache_uniq;
1226                         ulen = strlen(parsed->fscache_uniq);
1227                 }
1228         } else if (cloned) {
1229                 struct nfs_server *mnt_s = NFS_SB(cloned->sb);
1230                 if (!(mnt_s->options & NFS_OPTION_FSCACHE))
1231                         return;
1232                 if (mnt_s->fscache_key) {
1233                         uniq = mnt_s->fscache_key->key.uniquifier;
1234                         ulen = mnt_s->fscache_key->key.uniq_len;
1235                 }
1236         } else
1237                 return;
1238
1239         nfs_fscache_get_super_cookie(sb, uniq, ulen);
1240 }
1241 #else
1242 static void nfs_get_cache_cookie(struct super_block *sb,
1243                                  struct nfs_parsed_mount_data *parsed,
1244                                  struct nfs_clone_mount *cloned)
1245 {
1246 }
1247 #endif
1248
1249 static void nfs_set_readahead(struct backing_dev_info *bdi,
1250                               unsigned long iomax_pages)
1251 {
1252         bdi->ra_pages = VM_READAHEAD_PAGES;
1253         bdi->io_pages = iomax_pages;
1254 }
1255
1256 static struct dentry *nfs_fs_mount_common(int flags, const char *dev_name,
1257                                    struct nfs_mount_info *mount_info)
1258 {
1259         struct super_block *s;
1260         struct dentry *mntroot = ERR_PTR(-ENOMEM);
1261         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1262         struct nfs_server *server = mount_info->server;
1263         unsigned long kflags = 0, kflags_out = 0;
1264         struct nfs_sb_mountdata sb_mntdata = {
1265                 .mntflags = flags,
1266                 .server = server,
1267         };
1268         int error;
1269
1270         mount_info->server = NULL;
1271         if (IS_ERR(server))
1272                 return ERR_CAST(server);
1273
1274         if (server->flags & NFS_MOUNT_UNSHARED)
1275                 compare_super = NULL;
1276
1277         /* -o noac implies -o sync */
1278         if (server->flags & NFS_MOUNT_NOAC)
1279                 sb_mntdata.mntflags |= SB_SYNCHRONOUS;
1280
1281         if (mount_info->cloned != NULL && mount_info->cloned->sb != NULL)
1282                 if (mount_info->cloned->sb->s_flags & SB_SYNCHRONOUS)
1283                         sb_mntdata.mntflags |= SB_SYNCHRONOUS;
1284
1285         /* Get a superblock - note that we may end up sharing one that already exists */
1286         s = sget(mount_info->nfs_mod->nfs_fs, compare_super, nfs_set_super,
1287                  flags, &sb_mntdata);
1288         if (IS_ERR(s)) {
1289                 mntroot = ERR_CAST(s);
1290                 goto out_err_nosb;
1291         }
1292
1293         if (s->s_fs_info != server) {
1294                 nfs_free_server(server);
1295                 server = NULL;
1296         } else {
1297                 error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
1298                                              MINOR(server->s_dev));
1299                 if (error) {
1300                         mntroot = ERR_PTR(error);
1301                         goto error_splat_super;
1302                 }
1303                 nfs_set_readahead(s->s_bdi, server->rpages);
1304                 server->super = s;
1305         }
1306
1307         if (!s->s_root) {
1308                 unsigned bsize = mount_info->inherited_bsize;
1309                 /* initial superblock/root creation */
1310                 nfs_fill_super(s, mount_info);
1311                 if (bsize) {
1312                         s->s_blocksize_bits = bsize;
1313                         s->s_blocksize = 1U << bsize;
1314                 }
1315                 nfs_get_cache_cookie(s, mount_info->parsed, mount_info->cloned);
1316                 if (!(server->flags & NFS_MOUNT_UNSHARED))
1317                         s->s_iflags |= SB_I_MULTIROOT;
1318         }
1319
1320         mntroot = nfs_get_root(s, mount_info->mntfh, dev_name);
1321         if (IS_ERR(mntroot))
1322                 goto error_splat_super;
1323
1324
1325         if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
1326                 kflags |= SECURITY_LSM_NATIVE_LABELS;
1327         if (mount_info->cloned) {
1328                 if (d_inode(mntroot)->i_fop != &nfs_dir_operations) {
1329                         error = -ESTALE;
1330                         goto error_splat_root;
1331                 }
1332                 /* clone any lsm security options from the parent to the new sb */
1333                 error = security_sb_clone_mnt_opts(mount_info->cloned->sb, s, kflags,
1334                                 &kflags_out);
1335         } else {
1336                 error = security_sb_set_mnt_opts(s, mount_info->parsed->lsm_opts,
1337                                                         kflags, &kflags_out);
1338         }
1339         if (error)
1340                 goto error_splat_root;
1341         if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
1342                 !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
1343                 NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
1344         if (error)
1345                 goto error_splat_root;
1346
1347         s->s_flags |= SB_ACTIVE;
1348
1349 out:
1350         return mntroot;
1351
1352 out_err_nosb:
1353         nfs_free_server(server);
1354         goto out;
1355
1356 error_splat_root:
1357         dput(mntroot);
1358         mntroot = ERR_PTR(error);
1359 error_splat_super:
1360         deactivate_locked_super(s);
1361         goto out;
1362 }
1363
1364 struct dentry *nfs_fs_mount(struct file_system_type *fs_type,
1365         int flags, const char *dev_name, void *raw_data)
1366 {
1367         struct nfs_mount_info mount_info = {
1368         };
1369         struct dentry *mntroot = ERR_PTR(-ENOMEM);
1370         struct nfs_subversion *nfs_mod;
1371         int error;
1372
1373         mount_info.parsed = nfs_alloc_parsed_mount_data();
1374         mount_info.mntfh = nfs_alloc_fhandle();
1375         if (mount_info.parsed == NULL || mount_info.mntfh == NULL)
1376                 goto out;
1377
1378         /* Validate the mount data */
1379         error = nfs_validate_mount_data(fs_type, raw_data, mount_info.parsed, mount_info.mntfh, dev_name);
1380         if (error == NFS_TEXT_DATA)
1381                 error = nfs_validate_text_mount_data(raw_data, mount_info.parsed, dev_name);
1382         if (error < 0) {
1383                 mntroot = ERR_PTR(error);
1384                 goto out;
1385         }
1386
1387         nfs_mod = get_nfs_version(mount_info.parsed->version);
1388         if (IS_ERR(nfs_mod)) {
1389                 mntroot = ERR_CAST(nfs_mod);
1390                 goto out;
1391         }
1392         mount_info.nfs_mod = nfs_mod;
1393
1394         mntroot = nfs_mod->rpc_ops->try_mount(flags, dev_name, &mount_info);
1395
1396         put_nfs_version(nfs_mod);
1397 out:
1398         nfs_free_parsed_mount_data(mount_info.parsed);
1399         nfs_free_fhandle(mount_info.mntfh);
1400         return mntroot;
1401 }
1402 EXPORT_SYMBOL_GPL(nfs_fs_mount);
1403
1404 /*
1405  * Destroy an NFS2/3 superblock
1406  */
1407 void nfs_kill_super(struct super_block *s)
1408 {
1409         struct nfs_server *server = NFS_SB(s);
1410         dev_t dev = s->s_dev;
1411
1412         generic_shutdown_super(s);
1413
1414         nfs_fscache_release_super_cookie(s);
1415
1416         nfs_free_server(server);
1417         free_anon_bdev(dev);
1418 }
1419 EXPORT_SYMBOL_GPL(nfs_kill_super);
1420
1421 /*
1422  * Internal use only: mount_info is already set up by caller.
1423  * Used for mountpoint crossings and for nfs4 root.
1424  */
1425 static struct dentry *
1426 nfs_prepared_mount(struct file_system_type *fs_type, int flags,
1427                    const char *dev_name, void *raw_data)
1428 {
1429         return nfs_fs_mount_common(flags, dev_name, raw_data);
1430 }
1431
1432 #if IS_ENABLED(CONFIG_NFS_V4)
1433
1434 /*
1435  * NFS v4 module parameters need to stay in the
1436  * NFS client for backwards compatibility
1437  */
1438 unsigned int nfs_callback_set_tcpport;
1439 unsigned short nfs_callback_nr_threads;
1440 /* Default cache timeout is 10 minutes */
1441 unsigned int nfs_idmap_cache_timeout = 600;
1442 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
1443 bool nfs4_disable_idmapping = true;
1444 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
1445 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
1446 unsigned short send_implementation_id = 1;
1447 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
1448 bool recover_lost_locks = false;
1449
1450 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
1451 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
1452 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
1453 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
1454 EXPORT_SYMBOL_GPL(max_session_slots);
1455 EXPORT_SYMBOL_GPL(max_session_cb_slots);
1456 EXPORT_SYMBOL_GPL(send_implementation_id);
1457 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
1458 EXPORT_SYMBOL_GPL(recover_lost_locks);
1459
1460 #define NFS_CALLBACK_MAXPORTNR (65535U)
1461
1462 static int param_set_portnr(const char *val, const struct kernel_param *kp)
1463 {
1464         unsigned long num;
1465         int ret;
1466
1467         if (!val)
1468                 return -EINVAL;
1469         ret = kstrtoul(val, 0, &num);
1470         if (ret || num > NFS_CALLBACK_MAXPORTNR)
1471                 return -EINVAL;
1472         *((unsigned int *)kp->arg) = num;
1473         return 0;
1474 }
1475 static const struct kernel_param_ops param_ops_portnr = {
1476         .set = param_set_portnr,
1477         .get = param_get_uint,
1478 };
1479 #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
1480
1481 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
1482 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
1483 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
1484                 "assigned to the NFSv4 callback channels.");
1485 module_param(nfs_idmap_cache_timeout, int, 0644);
1486 module_param(nfs4_disable_idmapping, bool, 0644);
1487 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
1488                         NFS4_CLIENT_ID_UNIQ_LEN, 0600);
1489 MODULE_PARM_DESC(nfs4_disable_idmapping,
1490                 "Turn off NFSv4 idmapping when using 'sec=sys'");
1491 module_param(max_session_slots, ushort, 0644);
1492 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
1493                 "requests the client will negotiate");
1494 module_param(max_session_cb_slots, ushort, 0644);
1495 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
1496                 "callbacks the client will process for a given server");
1497 module_param(send_implementation_id, ushort, 0644);
1498 MODULE_PARM_DESC(send_implementation_id,
1499                 "Send implementation ID with NFSv4.1 exchange_id");
1500 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
1501
1502 module_param(recover_lost_locks, bool, 0644);
1503 MODULE_PARM_DESC(recover_lost_locks,
1504                  "If the server reports that a lock might be lost, "
1505                  "try to recover it risking data corruption.");
1506
1507
1508 #endif /* CONFIG_NFS_V4 */