]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/nfs/super.c
nfs: get rid of ->set_security()
[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 #define NFS_TEXT_DATA           1
73
74 #if IS_ENABLED(CONFIG_NFS_V3)
75 #define NFS_DEFAULT_VERSION 3
76 #else
77 #define NFS_DEFAULT_VERSION 2
78 #endif
79
80 #define NFS_MAX_CONNECTIONS 16
81
82 enum {
83         /* Mount options that take no arguments */
84         Opt_soft, Opt_softerr, Opt_hard,
85         Opt_posix, Opt_noposix,
86         Opt_cto, Opt_nocto,
87         Opt_ac, Opt_noac,
88         Opt_lock, Opt_nolock,
89         Opt_udp, Opt_tcp, Opt_rdma,
90         Opt_acl, Opt_noacl,
91         Opt_rdirplus, Opt_nordirplus,
92         Opt_sharecache, Opt_nosharecache,
93         Opt_resvport, Opt_noresvport,
94         Opt_fscache, Opt_nofscache,
95         Opt_migration, Opt_nomigration,
96
97         /* Mount options that take integer arguments */
98         Opt_port,
99         Opt_rsize, Opt_wsize, Opt_bsize,
100         Opt_timeo, Opt_retrans,
101         Opt_acregmin, Opt_acregmax,
102         Opt_acdirmin, Opt_acdirmax,
103         Opt_actimeo,
104         Opt_namelen,
105         Opt_mountport,
106         Opt_mountvers,
107         Opt_minorversion,
108
109         /* Mount options that take string arguments */
110         Opt_nfsvers,
111         Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
112         Opt_addr, Opt_mountaddr, Opt_clientaddr,
113         Opt_nconnect,
114         Opt_lookupcache,
115         Opt_fscache_uniq,
116         Opt_local_lock,
117
118         /* Special mount options */
119         Opt_userspace, Opt_deprecated, Opt_sloppy,
120
121         Opt_err
122 };
123
124 static const match_table_t nfs_mount_option_tokens = {
125         { Opt_userspace, "bg" },
126         { Opt_userspace, "fg" },
127         { Opt_userspace, "retry=%s" },
128
129         { Opt_sloppy, "sloppy" },
130
131         { Opt_soft, "soft" },
132         { Opt_softerr, "softerr" },
133         { Opt_hard, "hard" },
134         { Opt_deprecated, "intr" },
135         { Opt_deprecated, "nointr" },
136         { Opt_posix, "posix" },
137         { Opt_noposix, "noposix" },
138         { Opt_cto, "cto" },
139         { Opt_nocto, "nocto" },
140         { Opt_ac, "ac" },
141         { Opt_noac, "noac" },
142         { Opt_lock, "lock" },
143         { Opt_nolock, "nolock" },
144         { Opt_udp, "udp" },
145         { Opt_tcp, "tcp" },
146         { Opt_rdma, "rdma" },
147         { Opt_acl, "acl" },
148         { Opt_noacl, "noacl" },
149         { Opt_rdirplus, "rdirplus" },
150         { Opt_nordirplus, "nordirplus" },
151         { Opt_sharecache, "sharecache" },
152         { Opt_nosharecache, "nosharecache" },
153         { Opt_resvport, "resvport" },
154         { Opt_noresvport, "noresvport" },
155         { Opt_fscache, "fsc" },
156         { Opt_nofscache, "nofsc" },
157         { Opt_migration, "migration" },
158         { Opt_nomigration, "nomigration" },
159
160         { Opt_port, "port=%s" },
161         { Opt_rsize, "rsize=%s" },
162         { Opt_wsize, "wsize=%s" },
163         { Opt_bsize, "bsize=%s" },
164         { Opt_timeo, "timeo=%s" },
165         { Opt_retrans, "retrans=%s" },
166         { Opt_acregmin, "acregmin=%s" },
167         { Opt_acregmax, "acregmax=%s" },
168         { Opt_acdirmin, "acdirmin=%s" },
169         { Opt_acdirmax, "acdirmax=%s" },
170         { Opt_actimeo, "actimeo=%s" },
171         { Opt_namelen, "namlen=%s" },
172         { Opt_mountport, "mountport=%s" },
173         { Opt_mountvers, "mountvers=%s" },
174         { Opt_minorversion, "minorversion=%s" },
175
176         { Opt_nfsvers, "nfsvers=%s" },
177         { Opt_nfsvers, "vers=%s" },
178
179         { Opt_sec, "sec=%s" },
180         { Opt_proto, "proto=%s" },
181         { Opt_mountproto, "mountproto=%s" },
182         { Opt_addr, "addr=%s" },
183         { Opt_clientaddr, "clientaddr=%s" },
184         { Opt_mounthost, "mounthost=%s" },
185         { Opt_mountaddr, "mountaddr=%s" },
186
187         { Opt_nconnect, "nconnect=%s" },
188
189         { Opt_lookupcache, "lookupcache=%s" },
190         { Opt_fscache_uniq, "fsc=%s" },
191         { Opt_local_lock, "local_lock=%s" },
192
193         /* The following needs to be listed after all other options */
194         { Opt_nfsvers, "v%s" },
195
196         { Opt_err, NULL }
197 };
198
199 enum {
200         Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
201         Opt_xprt_rdma6,
202
203         Opt_xprt_err
204 };
205
206 static const match_table_t nfs_xprt_protocol_tokens = {
207         { Opt_xprt_udp, "udp" },
208         { Opt_xprt_udp6, "udp6" },
209         { Opt_xprt_tcp, "tcp" },
210         { Opt_xprt_tcp6, "tcp6" },
211         { Opt_xprt_rdma, "rdma" },
212         { Opt_xprt_rdma6, "rdma6" },
213
214         { Opt_xprt_err, NULL }
215 };
216
217 enum {
218         Opt_sec_none, Opt_sec_sys,
219         Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
220         Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
221         Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
222
223         Opt_sec_err
224 };
225
226 static const match_table_t nfs_secflavor_tokens = {
227         { Opt_sec_none, "none" },
228         { Opt_sec_none, "null" },
229         { Opt_sec_sys, "sys" },
230
231         { Opt_sec_krb5, "krb5" },
232         { Opt_sec_krb5i, "krb5i" },
233         { Opt_sec_krb5p, "krb5p" },
234
235         { Opt_sec_lkey, "lkey" },
236         { Opt_sec_lkeyi, "lkeyi" },
237         { Opt_sec_lkeyp, "lkeyp" },
238
239         { Opt_sec_spkm, "spkm3" },
240         { Opt_sec_spkmi, "spkm3i" },
241         { Opt_sec_spkmp, "spkm3p" },
242
243         { Opt_sec_err, NULL }
244 };
245
246 enum {
247         Opt_lookupcache_all, Opt_lookupcache_positive,
248         Opt_lookupcache_none,
249
250         Opt_lookupcache_err
251 };
252
253 static match_table_t nfs_lookupcache_tokens = {
254         { Opt_lookupcache_all, "all" },
255         { Opt_lookupcache_positive, "pos" },
256         { Opt_lookupcache_positive, "positive" },
257         { Opt_lookupcache_none, "none" },
258
259         { Opt_lookupcache_err, NULL }
260 };
261
262 enum {
263         Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
264         Opt_local_lock_none,
265
266         Opt_local_lock_err
267 };
268
269 static match_table_t nfs_local_lock_tokens = {
270         { Opt_local_lock_all, "all" },
271         { Opt_local_lock_flock, "flock" },
272         { Opt_local_lock_posix, "posix" },
273         { Opt_local_lock_none, "none" },
274
275         { Opt_local_lock_err, NULL }
276 };
277
278 enum {
279         Opt_vers_2, Opt_vers_3, Opt_vers_4, Opt_vers_4_0,
280         Opt_vers_4_1, Opt_vers_4_2,
281
282         Opt_vers_err
283 };
284
285 static match_table_t nfs_vers_tokens = {
286         { Opt_vers_2, "2" },
287         { Opt_vers_3, "3" },
288         { Opt_vers_4, "4" },
289         { Opt_vers_4_0, "4.0" },
290         { Opt_vers_4_1, "4.1" },
291         { Opt_vers_4_2, "4.2" },
292
293         { Opt_vers_err, NULL }
294 };
295
296 static struct dentry *nfs_prepared_mount(struct file_system_type *fs_type,
297                 int flags, const char *dev_name, void *raw_data);
298
299 struct file_system_type nfs_fs_type = {
300         .owner          = THIS_MODULE,
301         .name           = "nfs",
302         .mount          = nfs_fs_mount,
303         .kill_sb        = nfs_kill_super,
304         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
305 };
306 MODULE_ALIAS_FS("nfs");
307 EXPORT_SYMBOL_GPL(nfs_fs_type);
308
309 struct file_system_type nfs_prepared_fs_type = {
310         .owner          = THIS_MODULE,
311         .name           = "nfs",
312         .mount          = nfs_prepared_mount,
313         .kill_sb        = nfs_kill_super,
314         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
315 };
316 EXPORT_SYMBOL_GPL(nfs_prepared_fs_type);
317
318 const struct super_operations nfs_sops = {
319         .alloc_inode    = nfs_alloc_inode,
320         .free_inode     = nfs_free_inode,
321         .write_inode    = nfs_write_inode,
322         .drop_inode     = nfs_drop_inode,
323         .statfs         = nfs_statfs,
324         .evict_inode    = nfs_evict_inode,
325         .umount_begin   = nfs_umount_begin,
326         .show_options   = nfs_show_options,
327         .show_devname   = nfs_show_devname,
328         .show_path      = nfs_show_path,
329         .show_stats     = nfs_show_stats,
330         .remount_fs     = nfs_remount,
331 };
332 EXPORT_SYMBOL_GPL(nfs_sops);
333
334 #if IS_ENABLED(CONFIG_NFS_V4)
335 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *);
336 static int nfs4_validate_mount_data(void *options,
337         struct nfs_parsed_mount_data *args, const char *dev_name);
338
339 struct file_system_type nfs4_fs_type = {
340         .owner          = THIS_MODULE,
341         .name           = "nfs4",
342         .mount          = nfs_fs_mount,
343         .kill_sb        = nfs_kill_super,
344         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
345 };
346 MODULE_ALIAS_FS("nfs4");
347 MODULE_ALIAS("nfs4");
348 EXPORT_SYMBOL_GPL(nfs4_fs_type);
349
350 static int __init register_nfs4_fs(void)
351 {
352         return register_filesystem(&nfs4_fs_type);
353 }
354
355 static void unregister_nfs4_fs(void)
356 {
357         unregister_filesystem(&nfs4_fs_type);
358 }
359 #else
360 static int __init register_nfs4_fs(void)
361 {
362         return 0;
363 }
364
365 static void unregister_nfs4_fs(void)
366 {
367 }
368 #endif
369
370 static struct shrinker acl_shrinker = {
371         .count_objects  = nfs_access_cache_count,
372         .scan_objects   = nfs_access_cache_scan,
373         .seeks          = DEFAULT_SEEKS,
374 };
375
376 /*
377  * Register the NFS filesystems
378  */
379 int __init register_nfs_fs(void)
380 {
381         int ret;
382
383         ret = register_filesystem(&nfs_fs_type);
384         if (ret < 0)
385                 goto error_0;
386
387         ret = register_nfs4_fs();
388         if (ret < 0)
389                 goto error_1;
390
391         ret = nfs_register_sysctl();
392         if (ret < 0)
393                 goto error_2;
394         ret = register_shrinker(&acl_shrinker);
395         if (ret < 0)
396                 goto error_3;
397         return 0;
398 error_3:
399         nfs_unregister_sysctl();
400 error_2:
401         unregister_nfs4_fs();
402 error_1:
403         unregister_filesystem(&nfs_fs_type);
404 error_0:
405         return ret;
406 }
407
408 /*
409  * Unregister the NFS filesystems
410  */
411 void __exit unregister_nfs_fs(void)
412 {
413         unregister_shrinker(&acl_shrinker);
414         nfs_unregister_sysctl();
415         unregister_nfs4_fs();
416         unregister_filesystem(&nfs_fs_type);
417 }
418
419 bool nfs_sb_active(struct super_block *sb)
420 {
421         struct nfs_server *server = NFS_SB(sb);
422
423         if (!atomic_inc_not_zero(&sb->s_active))
424                 return false;
425         if (atomic_inc_return(&server->active) != 1)
426                 atomic_dec(&sb->s_active);
427         return true;
428 }
429 EXPORT_SYMBOL_GPL(nfs_sb_active);
430
431 void nfs_sb_deactive(struct super_block *sb)
432 {
433         struct nfs_server *server = NFS_SB(sb);
434
435         if (atomic_dec_and_test(&server->active))
436                 deactivate_super(sb);
437 }
438 EXPORT_SYMBOL_GPL(nfs_sb_deactive);
439
440 /*
441  * Deliver file system statistics to userspace
442  */
443 int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
444 {
445         struct nfs_server *server = NFS_SB(dentry->d_sb);
446         unsigned char blockbits;
447         unsigned long blockres;
448         struct nfs_fh *fh = NFS_FH(d_inode(dentry));
449         struct nfs_fsstat res;
450         int error = -ENOMEM;
451
452         res.fattr = nfs_alloc_fattr();
453         if (res.fattr == NULL)
454                 goto out_err;
455
456         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
457         if (unlikely(error == -ESTALE)) {
458                 struct dentry *pd_dentry;
459
460                 pd_dentry = dget_parent(dentry);
461                 nfs_zap_caches(d_inode(pd_dentry));
462                 dput(pd_dentry);
463         }
464         nfs_free_fattr(res.fattr);
465         if (error < 0)
466                 goto out_err;
467
468         buf->f_type = NFS_SUPER_MAGIC;
469
470         /*
471          * Current versions of glibc do not correctly handle the
472          * case where f_frsize != f_bsize.  Eventually we want to
473          * report the value of wtmult in this field.
474          */
475         buf->f_frsize = dentry->d_sb->s_blocksize;
476
477         /*
478          * On most *nix systems, f_blocks, f_bfree, and f_bavail
479          * are reported in units of f_frsize.  Linux hasn't had
480          * an f_frsize field in its statfs struct until recently,
481          * thus historically Linux's sys_statfs reports these
482          * fields in units of f_bsize.
483          */
484         buf->f_bsize = dentry->d_sb->s_blocksize;
485         blockbits = dentry->d_sb->s_blocksize_bits;
486         blockres = (1 << blockbits) - 1;
487         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
488         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
489         buf->f_bavail = (res.abytes + blockres) >> blockbits;
490
491         buf->f_files = res.tfiles;
492         buf->f_ffree = res.afiles;
493
494         buf->f_namelen = server->namelen;
495
496         return 0;
497
498  out_err:
499         dprintk("%s: statfs error = %d\n", __func__, -error);
500         return error;
501 }
502 EXPORT_SYMBOL_GPL(nfs_statfs);
503
504 /*
505  * Map the security flavour number to a name
506  */
507 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
508 {
509         static const struct {
510                 rpc_authflavor_t flavour;
511                 const char *str;
512         } sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
513                 /* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
514                 { RPC_AUTH_NULL, "null" },
515                 { RPC_AUTH_UNIX, "sys" },
516                 { RPC_AUTH_GSS_KRB5, "krb5" },
517                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
518                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
519                 { RPC_AUTH_GSS_LKEY, "lkey" },
520                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
521                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
522                 { RPC_AUTH_GSS_SPKM, "spkm" },
523                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
524                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
525                 { UINT_MAX, "unknown" }
526         };
527         int i;
528
529         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
530                 if (sec_flavours[i].flavour == flavour)
531                         break;
532         }
533         return sec_flavours[i].str;
534 }
535
536 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
537                                   int showdefaults)
538 {
539         struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
540         char *proto = NULL;
541
542         switch (sap->sa_family) {
543         case AF_INET:
544                 switch (nfss->mountd_protocol) {
545                 case IPPROTO_UDP:
546                         proto = RPCBIND_NETID_UDP;
547                         break;
548                 case IPPROTO_TCP:
549                         proto = RPCBIND_NETID_TCP;
550                         break;
551                 }
552                 break;
553         case AF_INET6:
554                 switch (nfss->mountd_protocol) {
555                 case IPPROTO_UDP:
556                         proto = RPCBIND_NETID_UDP6;
557                         break;
558                 case IPPROTO_TCP:
559                         proto = RPCBIND_NETID_TCP6;
560                         break;
561                 }
562                 break;
563         }
564         if (proto || showdefaults)
565                 seq_printf(m, ",mountproto=%s", proto ?: "auto");
566 }
567
568 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
569                                     int showdefaults)
570 {
571         struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
572
573         if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
574                 return;
575
576         switch (sap->sa_family) {
577         case AF_INET: {
578                 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
579                 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
580                 break;
581         }
582         case AF_INET6: {
583                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
584                 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
585                 break;
586         }
587         default:
588                 if (showdefaults)
589                         seq_puts(m, ",mountaddr=unspecified");
590         }
591
592         if (nfss->mountd_version || showdefaults)
593                 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
594         if ((nfss->mountd_port &&
595                 nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
596                 showdefaults)
597                 seq_printf(m, ",mountport=%u", nfss->mountd_port);
598
599         nfs_show_mountd_netid(m, nfss, showdefaults);
600 }
601
602 #if IS_ENABLED(CONFIG_NFS_V4)
603 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
604                                     int showdefaults)
605 {
606         struct nfs_client *clp = nfss->nfs_client;
607
608         seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
609 }
610 #else
611 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
612                                     int showdefaults)
613 {
614 }
615 #endif
616
617 static void nfs_show_nfs_version(struct seq_file *m,
618                 unsigned int version,
619                 unsigned int minorversion)
620 {
621         seq_printf(m, ",vers=%u", version);
622         if (version == 4)
623                 seq_printf(m, ".%u", minorversion);
624 }
625
626 /*
627  * Describe the mount options in force on this server representation
628  */
629 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
630                                    int showdefaults)
631 {
632         static const struct proc_nfs_info {
633                 int flag;
634                 const char *str;
635                 const char *nostr;
636         } nfs_info[] = {
637                 { NFS_MOUNT_SOFT, ",soft", "" },
638                 { NFS_MOUNT_SOFTERR, ",softerr", "" },
639                 { NFS_MOUNT_POSIX, ",posix", "" },
640                 { NFS_MOUNT_NOCTO, ",nocto", "" },
641                 { NFS_MOUNT_NOAC, ",noac", "" },
642                 { NFS_MOUNT_NONLM, ",nolock", "" },
643                 { NFS_MOUNT_NOACL, ",noacl", "" },
644                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
645                 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
646                 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
647                 { 0, NULL, NULL }
648         };
649         const struct proc_nfs_info *nfs_infop;
650         struct nfs_client *clp = nfss->nfs_client;
651         u32 version = clp->rpc_ops->version;
652         int local_flock, local_fcntl;
653
654         nfs_show_nfs_version(m, version, clp->cl_minorversion);
655         seq_printf(m, ",rsize=%u", nfss->rsize);
656         seq_printf(m, ",wsize=%u", nfss->wsize);
657         if (nfss->bsize != 0)
658                 seq_printf(m, ",bsize=%u", nfss->bsize);
659         seq_printf(m, ",namlen=%u", nfss->namelen);
660         if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
661                 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
662         if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
663                 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
664         if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
665                 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
666         if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
667                 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
668         if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
669                         seq_puts(m, ",hard");
670         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
671                 if (nfss->flags & nfs_infop->flag)
672                         seq_puts(m, nfs_infop->str);
673                 else
674                         seq_puts(m, nfs_infop->nostr);
675         }
676         rcu_read_lock();
677         seq_printf(m, ",proto=%s",
678                    rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
679         rcu_read_unlock();
680         if (clp->cl_nconnect > 0)
681                 seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
682         if (version == 4) {
683                 if (nfss->port != NFS_PORT)
684                         seq_printf(m, ",port=%u", nfss->port);
685         } else
686                 if (nfss->port)
687                         seq_printf(m, ",port=%u", nfss->port);
688
689         seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
690         seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
691         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
692
693         if (version != 4)
694                 nfs_show_mountd_options(m, nfss, showdefaults);
695         else
696                 nfs_show_nfsv4_options(m, nfss, showdefaults);
697
698         if (nfss->options & NFS_OPTION_FSCACHE)
699                 seq_puts(m, ",fsc");
700
701         if (nfss->options & NFS_OPTION_MIGRATION)
702                 seq_puts(m, ",migration");
703
704         if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
705                 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
706                         seq_puts(m, ",lookupcache=none");
707                 else
708                         seq_puts(m, ",lookupcache=pos");
709         }
710
711         local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
712         local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
713
714         if (!local_flock && !local_fcntl)
715                 seq_puts(m, ",local_lock=none");
716         else if (local_flock && local_fcntl)
717                 seq_puts(m, ",local_lock=all");
718         else if (local_flock)
719                 seq_puts(m, ",local_lock=flock");
720         else
721                 seq_puts(m, ",local_lock=posix");
722 }
723
724 /*
725  * Describe the mount options on this VFS mountpoint
726  */
727 int nfs_show_options(struct seq_file *m, struct dentry *root)
728 {
729         struct nfs_server *nfss = NFS_SB(root->d_sb);
730
731         nfs_show_mount_options(m, nfss, 0);
732
733         rcu_read_lock();
734         seq_printf(m, ",addr=%s",
735                         rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
736                                                         RPC_DISPLAY_ADDR));
737         rcu_read_unlock();
738
739         return 0;
740 }
741 EXPORT_SYMBOL_GPL(nfs_show_options);
742
743 #if IS_ENABLED(CONFIG_NFS_V4)
744 static void show_lease(struct seq_file *m, struct nfs_server *server)
745 {
746         struct nfs_client *clp = server->nfs_client;
747         unsigned long expire;
748
749         seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
750         expire = clp->cl_last_renewal + clp->cl_lease_time;
751         seq_printf(m, ",lease_expired=%ld",
752                    time_after(expire, jiffies) ?  0 : (jiffies - expire) / HZ);
753 }
754 #ifdef CONFIG_NFS_V4_1
755 static void show_sessions(struct seq_file *m, struct nfs_server *server)
756 {
757         if (nfs4_has_session(server->nfs_client))
758                 seq_puts(m, ",sessions");
759 }
760 #else
761 static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
762 #endif
763 #endif
764
765 #ifdef CONFIG_NFS_V4_1
766 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
767 {
768         seq_printf(m, ",pnfs=");
769         if (server->pnfs_curr_ld)
770                 seq_printf(m, "%s", server->pnfs_curr_ld->name);
771         else
772                 seq_printf(m, "not configured");
773 }
774
775 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
776 {
777         if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
778                 struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
779                 seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
780                            "date='%llu,%u'",
781                            impl_id->name, impl_id->domain,
782                            impl_id->date.seconds, impl_id->date.nseconds);
783         }
784 }
785 #else
786 #if IS_ENABLED(CONFIG_NFS_V4)
787 static void show_pnfs(struct seq_file *m, struct nfs_server *server)
788 {
789 }
790 #endif
791 static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
792 {
793 }
794 #endif
795
796 int nfs_show_devname(struct seq_file *m, struct dentry *root)
797 {
798         char *page = (char *) __get_free_page(GFP_KERNEL);
799         char *devname, *dummy;
800         int err = 0;
801         if (!page)
802                 return -ENOMEM;
803         devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
804         if (IS_ERR(devname))
805                 err = PTR_ERR(devname);
806         else
807                 seq_escape(m, devname, " \t\n\\");
808         free_page((unsigned long)page);
809         return err;
810 }
811 EXPORT_SYMBOL_GPL(nfs_show_devname);
812
813 int nfs_show_path(struct seq_file *m, struct dentry *dentry)
814 {
815         seq_puts(m, "/");
816         return 0;
817 }
818 EXPORT_SYMBOL_GPL(nfs_show_path);
819
820 /*
821  * Present statistical information for this VFS mountpoint
822  */
823 int nfs_show_stats(struct seq_file *m, struct dentry *root)
824 {
825         int i, cpu;
826         struct nfs_server *nfss = NFS_SB(root->d_sb);
827         struct rpc_auth *auth = nfss->client->cl_auth;
828         struct nfs_iostats totals = { };
829
830         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
831
832         /*
833          * Display all mount option settings
834          */
835         seq_puts(m, "\n\topts:\t");
836         seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
837         seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
838         seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
839         seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
840         nfs_show_mount_options(m, nfss, 1);
841
842         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
843
844         show_implementation_id(m, nfss);
845
846         seq_puts(m, "\n\tcaps:\t");
847         seq_printf(m, "caps=0x%x", nfss->caps);
848         seq_printf(m, ",wtmult=%u", nfss->wtmult);
849         seq_printf(m, ",dtsize=%u", nfss->dtsize);
850         seq_printf(m, ",bsize=%u", nfss->bsize);
851         seq_printf(m, ",namlen=%u", nfss->namelen);
852
853 #if IS_ENABLED(CONFIG_NFS_V4)
854         if (nfss->nfs_client->rpc_ops->version == 4) {
855                 seq_puts(m, "\n\tnfsv4:\t");
856                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
857                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
858                 seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
859                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
860                 show_sessions(m, nfss);
861                 show_pnfs(m, nfss);
862                 show_lease(m, nfss);
863         }
864 #endif
865
866         /*
867          * Display security flavor in effect for this mount
868          */
869         seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
870         if (auth->au_flavor)
871                 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
872
873         /*
874          * Display superblock I/O counters
875          */
876         for_each_possible_cpu(cpu) {
877                 struct nfs_iostats *stats;
878
879                 preempt_disable();
880                 stats = per_cpu_ptr(nfss->io_stats, cpu);
881
882                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
883                         totals.events[i] += stats->events[i];
884                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
885                         totals.bytes[i] += stats->bytes[i];
886 #ifdef CONFIG_NFS_FSCACHE
887                 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
888                         totals.fscache[i] += stats->fscache[i];
889 #endif
890
891                 preempt_enable();
892         }
893
894         seq_puts(m, "\n\tevents:\t");
895         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
896                 seq_printf(m, "%lu ", totals.events[i]);
897         seq_puts(m, "\n\tbytes:\t");
898         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
899                 seq_printf(m, "%Lu ", totals.bytes[i]);
900 #ifdef CONFIG_NFS_FSCACHE
901         if (nfss->options & NFS_OPTION_FSCACHE) {
902                 seq_puts(m, "\n\tfsc:\t");
903                 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
904                         seq_printf(m, "%Lu ", totals.fscache[i]);
905         }
906 #endif
907         seq_putc(m, '\n');
908
909         rpc_clnt_show_stats(m, nfss->client);
910
911         return 0;
912 }
913 EXPORT_SYMBOL_GPL(nfs_show_stats);
914
915 /*
916  * Begin unmount by attempting to remove all automounted mountpoints we added
917  * in response to xdev traversals and referrals
918  */
919 void nfs_umount_begin(struct super_block *sb)
920 {
921         struct nfs_server *server;
922         struct rpc_clnt *rpc;
923
924         server = NFS_SB(sb);
925         /* -EIO all pending I/O */
926         rpc = server->client_acl;
927         if (!IS_ERR(rpc))
928                 rpc_killall_tasks(rpc);
929         rpc = server->client;
930         if (!IS_ERR(rpc))
931                 rpc_killall_tasks(rpc);
932 }
933 EXPORT_SYMBOL_GPL(nfs_umount_begin);
934
935 static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(void)
936 {
937         struct nfs_parsed_mount_data *data;
938
939         data = kzalloc(sizeof(*data), GFP_KERNEL);
940         if (data) {
941                 data->timeo             = NFS_UNSPEC_TIMEO;
942                 data->retrans           = NFS_UNSPEC_RETRANS;
943                 data->acregmin          = NFS_DEF_ACREGMIN;
944                 data->acregmax          = NFS_DEF_ACREGMAX;
945                 data->acdirmin          = NFS_DEF_ACDIRMIN;
946                 data->acdirmax          = NFS_DEF_ACDIRMAX;
947                 data->mount_server.port = NFS_UNSPEC_PORT;
948                 data->nfs_server.port   = NFS_UNSPEC_PORT;
949                 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
950                 data->selected_flavor   = RPC_AUTH_MAXFLAVOR;
951                 data->minorversion      = 0;
952                 data->need_mount        = true;
953                 data->net               = current->nsproxy->net_ns;
954                 data->lsm_opts          = NULL;
955         }
956         return data;
957 }
958
959 static void nfs_free_parsed_mount_data(struct nfs_parsed_mount_data *data)
960 {
961         if (data) {
962                 kfree(data->client_address);
963                 kfree(data->mount_server.hostname);
964                 kfree(data->nfs_server.export_path);
965                 kfree(data->nfs_server.hostname);
966                 kfree(data->fscache_uniq);
967                 security_free_mnt_opts(&data->lsm_opts);
968                 kfree(data);
969         }
970 }
971
972 /*
973  * Sanity-check a server address provided by the mount command.
974  *
975  * Address family must be initialized, and address must not be
976  * the ANY address for that family.
977  */
978 static int nfs_verify_server_address(struct sockaddr *addr)
979 {
980         switch (addr->sa_family) {
981         case AF_INET: {
982                 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
983                 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
984         }
985         case AF_INET6: {
986                 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
987                 return !ipv6_addr_any(sa);
988         }
989         }
990
991         dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
992         return 0;
993 }
994
995 /*
996  * Select between a default port value and a user-specified port value.
997  * If a zero value is set, then autobind will be used.
998  */
999 static void nfs_set_port(struct sockaddr *sap, int *port,
1000                                  const unsigned short default_port)
1001 {
1002         if (*port == NFS_UNSPEC_PORT)
1003                 *port = default_port;
1004
1005         rpc_set_port(sap, *port);
1006 }
1007
1008 /*
1009  * Sanity check the NFS transport protocol.
1010  *
1011  */
1012 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
1013 {
1014         switch (mnt->nfs_server.protocol) {
1015         case XPRT_TRANSPORT_UDP:
1016         case XPRT_TRANSPORT_TCP:
1017         case XPRT_TRANSPORT_RDMA:
1018                 break;
1019         default:
1020                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1021         }
1022 }
1023
1024 /*
1025  * For text based NFSv2/v3 mounts, the mount protocol transport default
1026  * settings should depend upon the specified NFS transport.
1027  */
1028 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
1029 {
1030         nfs_validate_transport_protocol(mnt);
1031
1032         if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
1033             mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
1034                         return;
1035         switch (mnt->nfs_server.protocol) {
1036         case XPRT_TRANSPORT_UDP:
1037                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1038                 break;
1039         case XPRT_TRANSPORT_TCP:
1040         case XPRT_TRANSPORT_RDMA:
1041                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1042         }
1043 }
1044
1045 /*
1046  * Add 'flavor' to 'auth_info' if not already present.
1047  * Returns true if 'flavor' ends up in the list, false otherwise
1048  */
1049 static bool nfs_auth_info_add(struct nfs_auth_info *auth_info,
1050                               rpc_authflavor_t flavor)
1051 {
1052         unsigned int i;
1053         unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
1054
1055         /* make sure this flavor isn't already in the list */
1056         for (i = 0; i < auth_info->flavor_len; i++) {
1057                 if (flavor == auth_info->flavors[i])
1058                         return true;
1059         }
1060
1061         if (auth_info->flavor_len + 1 >= max_flavor_len) {
1062                 dfprintk(MOUNT, "NFS: too many sec= flavors\n");
1063                 return false;
1064         }
1065
1066         auth_info->flavors[auth_info->flavor_len++] = flavor;
1067         return true;
1068 }
1069
1070 /*
1071  * Return true if 'match' is in auth_info or auth_info is empty.
1072  * Return false otherwise.
1073  */
1074 bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
1075                          rpc_authflavor_t match)
1076 {
1077         int i;
1078
1079         if (!auth_info->flavor_len)
1080                 return true;
1081
1082         for (i = 0; i < auth_info->flavor_len; i++) {
1083                 if (auth_info->flavors[i] == match)
1084                         return true;
1085         }
1086         return false;
1087 }
1088 EXPORT_SYMBOL_GPL(nfs_auth_info_match);
1089
1090 /*
1091  * Parse the value of the 'sec=' option.
1092  */
1093 static int nfs_parse_security_flavors(char *value,
1094                                       struct nfs_parsed_mount_data *mnt)
1095 {
1096         substring_t args[MAX_OPT_ARGS];
1097         rpc_authflavor_t pseudoflavor;
1098         char *p;
1099
1100         dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
1101
1102         while ((p = strsep(&value, ":")) != NULL) {
1103                 switch (match_token(p, nfs_secflavor_tokens, args)) {
1104                 case Opt_sec_none:
1105                         pseudoflavor = RPC_AUTH_NULL;
1106                         break;
1107                 case Opt_sec_sys:
1108                         pseudoflavor = RPC_AUTH_UNIX;
1109                         break;
1110                 case Opt_sec_krb5:
1111                         pseudoflavor = RPC_AUTH_GSS_KRB5;
1112                         break;
1113                 case Opt_sec_krb5i:
1114                         pseudoflavor = RPC_AUTH_GSS_KRB5I;
1115                         break;
1116                 case Opt_sec_krb5p:
1117                         pseudoflavor = RPC_AUTH_GSS_KRB5P;
1118                         break;
1119                 case Opt_sec_lkey:
1120                         pseudoflavor = RPC_AUTH_GSS_LKEY;
1121                         break;
1122                 case Opt_sec_lkeyi:
1123                         pseudoflavor = RPC_AUTH_GSS_LKEYI;
1124                         break;
1125                 case Opt_sec_lkeyp:
1126                         pseudoflavor = RPC_AUTH_GSS_LKEYP;
1127                         break;
1128                 case Opt_sec_spkm:
1129                         pseudoflavor = RPC_AUTH_GSS_SPKM;
1130                         break;
1131                 case Opt_sec_spkmi:
1132                         pseudoflavor = RPC_AUTH_GSS_SPKMI;
1133                         break;
1134                 case Opt_sec_spkmp:
1135                         pseudoflavor = RPC_AUTH_GSS_SPKMP;
1136                         break;
1137                 default:
1138                         dfprintk(MOUNT,
1139                                  "NFS: sec= option '%s' not recognized\n", p);
1140                         return 0;
1141                 }
1142
1143                 if (!nfs_auth_info_add(&mnt->auth_info, pseudoflavor))
1144                         return 0;
1145         }
1146
1147         return 1;
1148 }
1149
1150 static int nfs_parse_version_string(char *string,
1151                 struct nfs_parsed_mount_data *mnt,
1152                 substring_t *args)
1153 {
1154         mnt->flags &= ~NFS_MOUNT_VER3;
1155         switch (match_token(string, nfs_vers_tokens, args)) {
1156         case Opt_vers_2:
1157                 mnt->version = 2;
1158                 break;
1159         case Opt_vers_3:
1160                 mnt->flags |= NFS_MOUNT_VER3;
1161                 mnt->version = 3;
1162                 break;
1163         case Opt_vers_4:
1164                 /* Backward compatibility option. In future,
1165                  * the mount program should always supply
1166                  * a NFSv4 minor version number.
1167                  */
1168                 mnt->version = 4;
1169                 break;
1170         case Opt_vers_4_0:
1171                 mnt->version = 4;
1172                 mnt->minorversion = 0;
1173                 break;
1174         case Opt_vers_4_1:
1175                 mnt->version = 4;
1176                 mnt->minorversion = 1;
1177                 break;
1178         case Opt_vers_4_2:
1179                 mnt->version = 4;
1180                 mnt->minorversion = 2;
1181                 break;
1182         default:
1183                 return 0;
1184         }
1185         return 1;
1186 }
1187
1188 static int nfs_get_option_str(substring_t args[], char **option)
1189 {
1190         kfree(*option);
1191         *option = match_strdup(args);
1192         return !*option;
1193 }
1194
1195 static int nfs_get_option_ul(substring_t args[], unsigned long *option)
1196 {
1197         int rc;
1198         char *string;
1199
1200         string = match_strdup(args);
1201         if (string == NULL)
1202                 return -ENOMEM;
1203         rc = kstrtoul(string, 10, option);
1204         kfree(string);
1205
1206         return rc;
1207 }
1208
1209 static int nfs_get_option_ul_bound(substring_t args[], unsigned long *option,
1210                 unsigned long l_bound, unsigned long u_bound)
1211 {
1212         int ret;
1213
1214         ret = nfs_get_option_ul(args, option);
1215         if (ret != 0)
1216                 return ret;
1217         if (*option < l_bound || *option > u_bound)
1218                 return -ERANGE;
1219         return 0;
1220 }
1221
1222 /*
1223  * Error-check and convert a string of mount options from user space into
1224  * a data structure.  The whole mount string is processed; bad options are
1225  * skipped as they are encountered.  If there were no errors, return 1;
1226  * otherwise return 0 (zero).
1227  */
1228 static int nfs_parse_mount_options(char *raw,
1229                                    struct nfs_parsed_mount_data *mnt)
1230 {
1231         char *p, *string;
1232         int rc, sloppy = 0, invalid_option = 0;
1233         unsigned short protofamily = AF_UNSPEC;
1234         unsigned short mountfamily = AF_UNSPEC;
1235
1236         if (!raw) {
1237                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
1238                 return 1;
1239         }
1240         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
1241
1242         rc = security_sb_eat_lsm_opts(raw, &mnt->lsm_opts);
1243         if (rc)
1244                 goto out_security_failure;
1245
1246         while ((p = strsep(&raw, ",")) != NULL) {
1247                 substring_t args[MAX_OPT_ARGS];
1248                 unsigned long option;
1249                 int token;
1250
1251                 if (!*p)
1252                         continue;
1253
1254                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
1255
1256                 token = match_token(p, nfs_mount_option_tokens, args);
1257                 switch (token) {
1258
1259                 /*
1260                  * boolean options:  foo/nofoo
1261                  */
1262                 case Opt_soft:
1263                         mnt->flags |= NFS_MOUNT_SOFT;
1264                         mnt->flags &= ~NFS_MOUNT_SOFTERR;
1265                         break;
1266                 case Opt_softerr:
1267                         mnt->flags |= NFS_MOUNT_SOFTERR;
1268                         mnt->flags &= ~NFS_MOUNT_SOFT;
1269                         break;
1270                 case Opt_hard:
1271                         mnt->flags &= ~(NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR);
1272                         break;
1273                 case Opt_posix:
1274                         mnt->flags |= NFS_MOUNT_POSIX;
1275                         break;
1276                 case Opt_noposix:
1277                         mnt->flags &= ~NFS_MOUNT_POSIX;
1278                         break;
1279                 case Opt_cto:
1280                         mnt->flags &= ~NFS_MOUNT_NOCTO;
1281                         break;
1282                 case Opt_nocto:
1283                         mnt->flags |= NFS_MOUNT_NOCTO;
1284                         break;
1285                 case Opt_ac:
1286                         mnt->flags &= ~NFS_MOUNT_NOAC;
1287                         break;
1288                 case Opt_noac:
1289                         mnt->flags |= NFS_MOUNT_NOAC;
1290                         break;
1291                 case Opt_lock:
1292                         mnt->flags &= ~NFS_MOUNT_NONLM;
1293                         mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1294                                         NFS_MOUNT_LOCAL_FCNTL);
1295                         break;
1296                 case Opt_nolock:
1297                         mnt->flags |= NFS_MOUNT_NONLM;
1298                         mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1299                                        NFS_MOUNT_LOCAL_FCNTL);
1300                         break;
1301                 case Opt_udp:
1302                         mnt->flags &= ~NFS_MOUNT_TCP;
1303                         mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1304                         break;
1305                 case Opt_tcp:
1306                         mnt->flags |= NFS_MOUNT_TCP;
1307                         mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1308                         break;
1309                 case Opt_rdma:
1310                         mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1311                         mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1312                         xprt_load_transport(p);
1313                         break;
1314                 case Opt_acl:
1315                         mnt->flags &= ~NFS_MOUNT_NOACL;
1316                         break;
1317                 case Opt_noacl:
1318                         mnt->flags |= NFS_MOUNT_NOACL;
1319                         break;
1320                 case Opt_rdirplus:
1321                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1322                         break;
1323                 case Opt_nordirplus:
1324                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1325                         break;
1326                 case Opt_sharecache:
1327                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
1328                         break;
1329                 case Opt_nosharecache:
1330                         mnt->flags |= NFS_MOUNT_UNSHARED;
1331                         break;
1332                 case Opt_resvport:
1333                         mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1334                         break;
1335                 case Opt_noresvport:
1336                         mnt->flags |= NFS_MOUNT_NORESVPORT;
1337                         break;
1338                 case Opt_fscache:
1339                         mnt->options |= NFS_OPTION_FSCACHE;
1340                         kfree(mnt->fscache_uniq);
1341                         mnt->fscache_uniq = NULL;
1342                         break;
1343                 case Opt_nofscache:
1344                         mnt->options &= ~NFS_OPTION_FSCACHE;
1345                         kfree(mnt->fscache_uniq);
1346                         mnt->fscache_uniq = NULL;
1347                         break;
1348                 case Opt_migration:
1349                         mnt->options |= NFS_OPTION_MIGRATION;
1350                         break;
1351                 case Opt_nomigration:
1352                         mnt->options &= ~NFS_OPTION_MIGRATION;
1353                         break;
1354
1355                 /*
1356                  * options that take numeric values
1357                  */
1358                 case Opt_port:
1359                         if (nfs_get_option_ul(args, &option) ||
1360                             option > USHRT_MAX)
1361                                 goto out_invalid_value;
1362                         mnt->nfs_server.port = option;
1363                         break;
1364                 case Opt_rsize:
1365                         if (nfs_get_option_ul(args, &option))
1366                                 goto out_invalid_value;
1367                         mnt->rsize = option;
1368                         break;
1369                 case Opt_wsize:
1370                         if (nfs_get_option_ul(args, &option))
1371                                 goto out_invalid_value;
1372                         mnt->wsize = option;
1373                         break;
1374                 case Opt_bsize:
1375                         if (nfs_get_option_ul(args, &option))
1376                                 goto out_invalid_value;
1377                         mnt->bsize = option;
1378                         break;
1379                 case Opt_timeo:
1380                         if (nfs_get_option_ul_bound(args, &option, 1, INT_MAX))
1381                                 goto out_invalid_value;
1382                         mnt->timeo = option;
1383                         break;
1384                 case Opt_retrans:
1385                         if (nfs_get_option_ul_bound(args, &option, 0, INT_MAX))
1386                                 goto out_invalid_value;
1387                         mnt->retrans = option;
1388                         break;
1389                 case Opt_acregmin:
1390                         if (nfs_get_option_ul(args, &option))
1391                                 goto out_invalid_value;
1392                         mnt->acregmin = option;
1393                         break;
1394                 case Opt_acregmax:
1395                         if (nfs_get_option_ul(args, &option))
1396                                 goto out_invalid_value;
1397                         mnt->acregmax = option;
1398                         break;
1399                 case Opt_acdirmin:
1400                         if (nfs_get_option_ul(args, &option))
1401                                 goto out_invalid_value;
1402                         mnt->acdirmin = option;
1403                         break;
1404                 case Opt_acdirmax:
1405                         if (nfs_get_option_ul(args, &option))
1406                                 goto out_invalid_value;
1407                         mnt->acdirmax = option;
1408                         break;
1409                 case Opt_actimeo:
1410                         if (nfs_get_option_ul(args, &option))
1411                                 goto out_invalid_value;
1412                         mnt->acregmin = mnt->acregmax =
1413                         mnt->acdirmin = mnt->acdirmax = option;
1414                         break;
1415                 case Opt_namelen:
1416                         if (nfs_get_option_ul(args, &option))
1417                                 goto out_invalid_value;
1418                         mnt->namlen = option;
1419                         break;
1420                 case Opt_mountport:
1421                         if (nfs_get_option_ul(args, &option) ||
1422                             option > USHRT_MAX)
1423                                 goto out_invalid_value;
1424                         mnt->mount_server.port = option;
1425                         break;
1426                 case Opt_mountvers:
1427                         if (nfs_get_option_ul(args, &option) ||
1428                             option < NFS_MNT_VERSION ||
1429                             option > NFS_MNT3_VERSION)
1430                                 goto out_invalid_value;
1431                         mnt->mount_server.version = option;
1432                         break;
1433                 case Opt_minorversion:
1434                         if (nfs_get_option_ul(args, &option))
1435                                 goto out_invalid_value;
1436                         if (option > NFS4_MAX_MINOR_VERSION)
1437                                 goto out_invalid_value;
1438                         mnt->minorversion = option;
1439                         break;
1440
1441                 /*
1442                  * options that take text values
1443                  */
1444                 case Opt_nfsvers:
1445                         string = match_strdup(args);
1446                         if (string == NULL)
1447                                 goto out_nomem;
1448                         rc = nfs_parse_version_string(string, mnt, args);
1449                         kfree(string);
1450                         if (!rc)
1451                                 goto out_invalid_value;
1452                         break;
1453                 case Opt_sec:
1454                         string = match_strdup(args);
1455                         if (string == NULL)
1456                                 goto out_nomem;
1457                         rc = nfs_parse_security_flavors(string, mnt);
1458                         kfree(string);
1459                         if (!rc) {
1460                                 dfprintk(MOUNT, "NFS:   unrecognized "
1461                                                 "security flavor\n");
1462                                 return 0;
1463                         }
1464                         break;
1465                 case Opt_proto:
1466                         string = match_strdup(args);
1467                         if (string == NULL)
1468                                 goto out_nomem;
1469                         token = match_token(string,
1470                                             nfs_xprt_protocol_tokens, args);
1471
1472                         protofamily = AF_INET;
1473                         switch (token) {
1474                         case Opt_xprt_udp6:
1475                                 protofamily = AF_INET6;
1476                                 /* fall through */
1477                         case Opt_xprt_udp:
1478                                 mnt->flags &= ~NFS_MOUNT_TCP;
1479                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1480                                 break;
1481                         case Opt_xprt_tcp6:
1482                                 protofamily = AF_INET6;
1483                                 /* fall through */
1484                         case Opt_xprt_tcp:
1485                                 mnt->flags |= NFS_MOUNT_TCP;
1486                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1487                                 break;
1488                         case Opt_xprt_rdma6:
1489                                 protofamily = AF_INET6;
1490                                 /* fall through */
1491                         case Opt_xprt_rdma:
1492                                 /* vector side protocols to TCP */
1493                                 mnt->flags |= NFS_MOUNT_TCP;
1494                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1495                                 xprt_load_transport(string);
1496                                 break;
1497                         default:
1498                                 dfprintk(MOUNT, "NFS:   unrecognized "
1499                                                 "transport protocol\n");
1500                                 kfree(string);
1501                                 return 0;
1502                         }
1503                         kfree(string);
1504                         break;
1505                 case Opt_mountproto:
1506                         string = match_strdup(args);
1507                         if (string == NULL)
1508                                 goto out_nomem;
1509                         token = match_token(string,
1510                                             nfs_xprt_protocol_tokens, args);
1511                         kfree(string);
1512
1513                         mountfamily = AF_INET;
1514                         switch (token) {
1515                         case Opt_xprt_udp6:
1516                                 mountfamily = AF_INET6;
1517                                 /* fall through */
1518                         case Opt_xprt_udp:
1519                                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1520                                 break;
1521                         case Opt_xprt_tcp6:
1522                                 mountfamily = AF_INET6;
1523                                 /* fall through */
1524                         case Opt_xprt_tcp:
1525                                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1526                                 break;
1527                         case Opt_xprt_rdma: /* not used for side protocols */
1528                         default:
1529                                 dfprintk(MOUNT, "NFS:   unrecognized "
1530                                                 "transport protocol\n");
1531                                 return 0;
1532                         }
1533                         break;
1534                 case Opt_addr:
1535                         string = match_strdup(args);
1536                         if (string == NULL)
1537                                 goto out_nomem;
1538                         mnt->nfs_server.addrlen =
1539                                 rpc_pton(mnt->net, string, strlen(string),
1540                                         (struct sockaddr *)
1541                                         &mnt->nfs_server.address,
1542                                         sizeof(mnt->nfs_server.address));
1543                         kfree(string);
1544                         if (mnt->nfs_server.addrlen == 0)
1545                                 goto out_invalid_address;
1546                         break;
1547                 case Opt_clientaddr:
1548                         if (nfs_get_option_str(args, &mnt->client_address))
1549                                 goto out_nomem;
1550                         break;
1551                 case Opt_mounthost:
1552                         if (nfs_get_option_str(args,
1553                                                &mnt->mount_server.hostname))
1554                                 goto out_nomem;
1555                         break;
1556                 case Opt_mountaddr:
1557                         string = match_strdup(args);
1558                         if (string == NULL)
1559                                 goto out_nomem;
1560                         mnt->mount_server.addrlen =
1561                                 rpc_pton(mnt->net, string, strlen(string),
1562                                         (struct sockaddr *)
1563                                         &mnt->mount_server.address,
1564                                         sizeof(mnt->mount_server.address));
1565                         kfree(string);
1566                         if (mnt->mount_server.addrlen == 0)
1567                                 goto out_invalid_address;
1568                         break;
1569                 case Opt_nconnect:
1570                         if (nfs_get_option_ul_bound(args, &option, 1, NFS_MAX_CONNECTIONS))
1571                                 goto out_invalid_value;
1572                         mnt->nfs_server.nconnect = option;
1573                         break;
1574                 case Opt_lookupcache:
1575                         string = match_strdup(args);
1576                         if (string == NULL)
1577                                 goto out_nomem;
1578                         token = match_token(string,
1579                                         nfs_lookupcache_tokens, args);
1580                         kfree(string);
1581                         switch (token) {
1582                                 case Opt_lookupcache_all:
1583                                         mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1584                                         break;
1585                                 case Opt_lookupcache_positive:
1586                                         mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1587                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1588                                         break;
1589                                 case Opt_lookupcache_none:
1590                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1591                                         break;
1592                                 default:
1593                                         dfprintk(MOUNT, "NFS:   invalid "
1594                                                         "lookupcache argument\n");
1595                                         return 0;
1596                         }
1597                         break;
1598                 case Opt_fscache_uniq:
1599                         if (nfs_get_option_str(args, &mnt->fscache_uniq))
1600                                 goto out_nomem;
1601                         mnt->options |= NFS_OPTION_FSCACHE;
1602                         break;
1603                 case Opt_local_lock:
1604                         string = match_strdup(args);
1605                         if (string == NULL)
1606                                 goto out_nomem;
1607                         token = match_token(string, nfs_local_lock_tokens,
1608                                         args);
1609                         kfree(string);
1610                         switch (token) {
1611                         case Opt_local_lock_all:
1612                                 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1613                                                NFS_MOUNT_LOCAL_FCNTL);
1614                                 break;
1615                         case Opt_local_lock_flock:
1616                                 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1617                                 break;
1618                         case Opt_local_lock_posix:
1619                                 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1620                                 break;
1621                         case Opt_local_lock_none:
1622                                 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1623                                                 NFS_MOUNT_LOCAL_FCNTL);
1624                                 break;
1625                         default:
1626                                 dfprintk(MOUNT, "NFS:   invalid "
1627                                                 "local_lock argument\n");
1628                                 return 0;
1629                         }
1630                         break;
1631
1632                 /*
1633                  * Special options
1634                  */
1635                 case Opt_sloppy:
1636                         sloppy = 1;
1637                         dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1638                         break;
1639                 case Opt_userspace:
1640                 case Opt_deprecated:
1641                         dfprintk(MOUNT, "NFS:   ignoring mount option "
1642                                         "'%s'\n", p);
1643                         break;
1644
1645                 default:
1646                         invalid_option = 1;
1647                         dfprintk(MOUNT, "NFS:   unrecognized mount option "
1648                                         "'%s'\n", p);
1649                 }
1650         }
1651
1652         if (!sloppy && invalid_option)
1653                 return 0;
1654
1655         if (mnt->minorversion && mnt->version != 4)
1656                 goto out_minorversion_mismatch;
1657
1658         if (mnt->options & NFS_OPTION_MIGRATION &&
1659             (mnt->version != 4 || mnt->minorversion != 0))
1660                 goto out_migration_misuse;
1661
1662         /*
1663          * verify that any proto=/mountproto= options match the address
1664          * families in the addr=/mountaddr= options.
1665          */
1666         if (protofamily != AF_UNSPEC &&
1667             protofamily != mnt->nfs_server.address.ss_family)
1668                 goto out_proto_mismatch;
1669
1670         if (mountfamily != AF_UNSPEC) {
1671                 if (mnt->mount_server.addrlen) {
1672                         if (mountfamily != mnt->mount_server.address.ss_family)
1673                                 goto out_mountproto_mismatch;
1674                 } else {
1675                         if (mountfamily != mnt->nfs_server.address.ss_family)
1676                                 goto out_mountproto_mismatch;
1677                 }
1678         }
1679
1680         return 1;
1681
1682 out_mountproto_mismatch:
1683         printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1684                          "option\n");
1685         return 0;
1686 out_proto_mismatch:
1687         printk(KERN_INFO "NFS: server address does not match proto= option\n");
1688         return 0;
1689 out_invalid_address:
1690         printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1691         return 0;
1692 out_invalid_value:
1693         printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
1694         return 0;
1695 out_minorversion_mismatch:
1696         printk(KERN_INFO "NFS: mount option vers=%u does not support "
1697                          "minorversion=%u\n", mnt->version, mnt->minorversion);
1698         return 0;
1699 out_migration_misuse:
1700         printk(KERN_INFO
1701                 "NFS: 'migration' not supported for this NFS version\n");
1702         return 0;
1703 out_nomem:
1704         printk(KERN_INFO "NFS: not enough memory to parse option\n");
1705         return 0;
1706 out_security_failure:
1707         printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1708         return 0;
1709 }
1710
1711 /*
1712  * Ensure that a specified authtype in args->auth_info is supported by
1713  * the server. Returns 0 and sets args->selected_flavor if it's ok, and
1714  * -EACCES if not.
1715  */
1716 static int nfs_verify_authflavors(struct nfs_parsed_mount_data *args,
1717                         rpc_authflavor_t *server_authlist, unsigned int count)
1718 {
1719         rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
1720         bool found_auth_null = false;
1721         unsigned int i;
1722
1723         /*
1724          * If the sec= mount option is used, the specified flavor or AUTH_NULL
1725          * must be in the list returned by the server.
1726          *
1727          * AUTH_NULL has a special meaning when it's in the server list - it
1728          * means that the server will ignore the rpc creds, so any flavor
1729          * can be used but still use the sec= that was specified.
1730          *
1731          * Note also that the MNT procedure in MNTv1 does not return a list
1732          * of supported security flavors. In this case, nfs_mount() fabricates
1733          * a security flavor list containing just AUTH_NULL.
1734          */
1735         for (i = 0; i < count; i++) {
1736                 flavor = server_authlist[i];
1737
1738                 if (nfs_auth_info_match(&args->auth_info, flavor))
1739                         goto out;
1740
1741                 if (flavor == RPC_AUTH_NULL)
1742                         found_auth_null = true;
1743         }
1744
1745         if (found_auth_null) {
1746                 flavor = args->auth_info.flavors[0];
1747                 goto out;
1748         }
1749
1750         dfprintk(MOUNT,
1751                  "NFS: specified auth flavors not supported by server\n");
1752         return -EACCES;
1753
1754 out:
1755         args->selected_flavor = flavor;
1756         dfprintk(MOUNT, "NFS: using auth flavor %u\n", args->selected_flavor);
1757         return 0;
1758 }
1759
1760 /*
1761  * Use the remote server's MOUNT service to request the NFS file handle
1762  * corresponding to the provided path.
1763  */
1764 static int nfs_request_mount(struct nfs_parsed_mount_data *args,
1765                              struct nfs_fh *root_fh,
1766                              rpc_authflavor_t *server_authlist,
1767                              unsigned int *server_authlist_len)
1768 {
1769         struct nfs_mount_request request = {
1770                 .sap            = (struct sockaddr *)
1771                                                 &args->mount_server.address,
1772                 .dirpath        = args->nfs_server.export_path,
1773                 .protocol       = args->mount_server.protocol,
1774                 .fh             = root_fh,
1775                 .noresvport     = args->flags & NFS_MOUNT_NORESVPORT,
1776                 .auth_flav_len  = server_authlist_len,
1777                 .auth_flavs     = server_authlist,
1778                 .net            = args->net,
1779         };
1780         int status;
1781
1782         if (args->mount_server.version == 0) {
1783                 switch (args->version) {
1784                         default:
1785                                 args->mount_server.version = NFS_MNT3_VERSION;
1786                                 break;
1787                         case 2:
1788                                 args->mount_server.version = NFS_MNT_VERSION;
1789                 }
1790         }
1791         request.version = args->mount_server.version;
1792
1793         if (args->mount_server.hostname)
1794                 request.hostname = args->mount_server.hostname;
1795         else
1796                 request.hostname = args->nfs_server.hostname;
1797
1798         /*
1799          * Construct the mount server's address.
1800          */
1801         if (args->mount_server.address.ss_family == AF_UNSPEC) {
1802                 memcpy(request.sap, &args->nfs_server.address,
1803                        args->nfs_server.addrlen);
1804                 args->mount_server.addrlen = args->nfs_server.addrlen;
1805         }
1806         request.salen = args->mount_server.addrlen;
1807         nfs_set_port(request.sap, &args->mount_server.port, 0);
1808
1809         /*
1810          * Now ask the mount server to map our export path
1811          * to a file handle.
1812          */
1813         status = nfs_mount(&request);
1814         if (status != 0) {
1815                 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1816                                 request.hostname, status);
1817                 return status;
1818         }
1819
1820         return 0;
1821 }
1822
1823 static struct nfs_server *nfs_try_mount_request(struct nfs_mount_info *mount_info)
1824 {
1825         int status;
1826         unsigned int i;
1827         bool tried_auth_unix = false;
1828         bool auth_null_in_list = false;
1829         struct nfs_server *server = ERR_PTR(-EACCES);
1830         struct nfs_parsed_mount_data *args = mount_info->parsed;
1831         rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
1832         unsigned int authlist_len = ARRAY_SIZE(authlist);
1833         struct nfs_subversion *nfs_mod = mount_info->nfs_mod;
1834
1835         status = nfs_request_mount(args, mount_info->mntfh, authlist,
1836                                         &authlist_len);
1837         if (status)
1838                 return ERR_PTR(status);
1839
1840         /*
1841          * Was a sec= authflavor specified in the options? First, verify
1842          * whether the server supports it, and then just try to use it if so.
1843          */
1844         if (args->auth_info.flavor_len > 0) {
1845                 status = nfs_verify_authflavors(args, authlist, authlist_len);
1846                 dfprintk(MOUNT, "NFS: using auth flavor %u\n",
1847                          args->selected_flavor);
1848                 if (status)
1849                         return ERR_PTR(status);
1850                 return nfs_mod->rpc_ops->create_server(mount_info);
1851         }
1852
1853         /*
1854          * No sec= option was provided. RFC 2623, section 2.7 suggests we
1855          * SHOULD prefer the flavor listed first. However, some servers list
1856          * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
1857          */
1858         for (i = 0; i < authlist_len; ++i) {
1859                 rpc_authflavor_t flavor;
1860                 struct rpcsec_gss_info info;
1861
1862                 flavor = authlist[i];
1863                 switch (flavor) {
1864                 case RPC_AUTH_UNIX:
1865                         tried_auth_unix = true;
1866                         break;
1867                 case RPC_AUTH_NULL:
1868                         auth_null_in_list = true;
1869                         continue;
1870                 default:
1871                         if (rpcauth_get_gssinfo(flavor, &info) != 0)
1872                                 continue;
1873                         /* Fallthrough */
1874                 }
1875                 dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
1876                 args->selected_flavor = flavor;
1877                 server = nfs_mod->rpc_ops->create_server(mount_info);
1878                 if (!IS_ERR(server))
1879                         return server;
1880         }
1881
1882         /*
1883          * Nothing we tried so far worked. At this point, give up if we've
1884          * already tried AUTH_UNIX or if the server's list doesn't contain
1885          * AUTH_NULL
1886          */
1887         if (tried_auth_unix || !auth_null_in_list)
1888                 return server;
1889
1890         /* Last chance! Try AUTH_UNIX */
1891         dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
1892         args->selected_flavor = RPC_AUTH_UNIX;
1893         return nfs_mod->rpc_ops->create_server(mount_info);
1894 }
1895
1896 static struct dentry *nfs_fs_mount_common(int, const char *, struct nfs_mount_info *);
1897
1898 struct dentry *nfs_try_mount(int flags, const char *dev_name,
1899                              struct nfs_mount_info *mount_info)
1900 {
1901         struct nfs_subversion *nfs_mod = mount_info->nfs_mod;
1902         if (mount_info->parsed->need_mount)
1903                 mount_info->server = nfs_try_mount_request(mount_info);
1904         else
1905                 mount_info->server = nfs_mod->rpc_ops->create_server(mount_info);
1906
1907         return nfs_fs_mount_common(flags, dev_name, mount_info);
1908 }
1909 EXPORT_SYMBOL_GPL(nfs_try_mount);
1910
1911 /*
1912  * Split "dev_name" into "hostname:export_path".
1913  *
1914  * The leftmost colon demarks the split between the server's hostname
1915  * and the export path.  If the hostname starts with a left square
1916  * bracket, then it may contain colons.
1917  *
1918  * Note: caller frees hostname and export path, even on error.
1919  */
1920 static int nfs_parse_devname(const char *dev_name,
1921                              char **hostname, size_t maxnamlen,
1922                              char **export_path, size_t maxpathlen)
1923 {
1924         size_t len;
1925         char *end;
1926
1927         if (unlikely(!dev_name || !*dev_name)) {
1928                 dfprintk(MOUNT, "NFS: device name not specified\n");
1929                 return -EINVAL;
1930         }
1931
1932         /* Is the host name protected with square brakcets? */
1933         if (*dev_name == '[') {
1934                 end = strchr(++dev_name, ']');
1935                 if (end == NULL || end[1] != ':')
1936                         goto out_bad_devname;
1937
1938                 len = end - dev_name;
1939                 end++;
1940         } else {
1941                 char *comma;
1942
1943                 end = strchr(dev_name, ':');
1944                 if (end == NULL)
1945                         goto out_bad_devname;
1946                 len = end - dev_name;
1947
1948                 /* kill possible hostname list: not supported */
1949                 comma = strchr(dev_name, ',');
1950                 if (comma != NULL && comma < end)
1951                         len = comma - dev_name;
1952         }
1953
1954         if (len > maxnamlen)
1955                 goto out_hostname;
1956
1957         /* N.B. caller will free nfs_server.hostname in all cases */
1958         *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1959         if (*hostname == NULL)
1960                 goto out_nomem;
1961         len = strlen(++end);
1962         if (len > maxpathlen)
1963                 goto out_path;
1964         *export_path = kstrndup(end, len, GFP_KERNEL);
1965         if (!*export_path)
1966                 goto out_nomem;
1967
1968         dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1969         return 0;
1970
1971 out_bad_devname:
1972         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1973         return -EINVAL;
1974
1975 out_nomem:
1976         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1977         return -ENOMEM;
1978
1979 out_hostname:
1980         dfprintk(MOUNT, "NFS: server hostname too long\n");
1981         return -ENAMETOOLONG;
1982
1983 out_path:
1984         dfprintk(MOUNT, "NFS: export pathname too long\n");
1985         return -ENAMETOOLONG;
1986 }
1987
1988 /*
1989  * Validate the NFS2/NFS3 mount data
1990  * - fills in the mount root filehandle
1991  *
1992  * For option strings, user space handles the following behaviors:
1993  *
1994  * + DNS: mapping server host name to IP address ("addr=" option)
1995  *
1996  * + failure mode: how to behave if a mount request can't be handled
1997  *   immediately ("fg/bg" option)
1998  *
1999  * + retry: how often to retry a mount request ("retry=" option)
2000  *
2001  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
2002  *   mountproto=tcp after mountproto=udp, and so on
2003  */
2004 static int nfs23_validate_mount_data(void *options,
2005                                      struct nfs_parsed_mount_data *args,
2006                                      struct nfs_fh *mntfh,
2007                                      const char *dev_name)
2008 {
2009         struct nfs_mount_data *data = (struct nfs_mount_data *)options;
2010         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2011         int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
2012
2013         if (data == NULL)
2014                 goto out_no_data;
2015
2016         args->version = NFS_DEFAULT_VERSION;
2017         switch (data->version) {
2018         case 1:
2019                 data->namlen = 0; /* fall through */
2020         case 2:
2021                 data->bsize = 0; /* fall through */
2022         case 3:
2023                 if (data->flags & NFS_MOUNT_VER3)
2024                         goto out_no_v3;
2025                 data->root.size = NFS2_FHSIZE;
2026                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
2027                 /* Turn off security negotiation */
2028                 extra_flags |= NFS_MOUNT_SECFLAVOUR;
2029                 /* fall through */
2030         case 4:
2031                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
2032                         goto out_no_sec;
2033                 /* fall through */
2034         case 5:
2035                 memset(data->context, 0, sizeof(data->context));
2036                 /* fall through */
2037         case 6:
2038                 if (data->flags & NFS_MOUNT_VER3) {
2039                         if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
2040                                 goto out_invalid_fh;
2041                         mntfh->size = data->root.size;
2042                         args->version = 3;
2043                 } else {
2044                         mntfh->size = NFS2_FHSIZE;
2045                         args->version = 2;
2046                 }
2047
2048
2049                 memcpy(mntfh->data, data->root.data, mntfh->size);
2050                 if (mntfh->size < sizeof(mntfh->data))
2051                         memset(mntfh->data + mntfh->size, 0,
2052                                sizeof(mntfh->data) - mntfh->size);
2053
2054                 /*
2055                  * Translate to nfs_parsed_mount_data, which nfs_fill_super
2056                  * can deal with.
2057                  */
2058                 args->flags             = data->flags & NFS_MOUNT_FLAGMASK;
2059                 args->flags             |= extra_flags;
2060                 args->rsize             = data->rsize;
2061                 args->wsize             = data->wsize;
2062                 args->timeo             = data->timeo;
2063                 args->retrans           = data->retrans;
2064                 args->acregmin          = data->acregmin;
2065                 args->acregmax          = data->acregmax;
2066                 args->acdirmin          = data->acdirmin;
2067                 args->acdirmax          = data->acdirmax;
2068                 args->need_mount        = false;
2069
2070                 memcpy(sap, &data->addr, sizeof(data->addr));
2071                 args->nfs_server.addrlen = sizeof(data->addr);
2072                 args->nfs_server.port = ntohs(data->addr.sin_port);
2073                 if (sap->sa_family != AF_INET ||
2074                     !nfs_verify_server_address(sap))
2075                         goto out_no_address;
2076
2077                 if (!(data->flags & NFS_MOUNT_TCP))
2078                         args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
2079                 /* N.B. caller will free nfs_server.hostname in all cases */
2080                 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
2081                 args->namlen            = data->namlen;
2082                 args->bsize             = data->bsize;
2083
2084                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
2085                         args->selected_flavor = data->pseudoflavor;
2086                 else
2087                         args->selected_flavor = RPC_AUTH_UNIX;
2088                 if (!args->nfs_server.hostname)
2089                         goto out_nomem;
2090
2091                 if (!(data->flags & NFS_MOUNT_NONLM))
2092                         args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
2093                                          NFS_MOUNT_LOCAL_FCNTL);
2094                 else
2095                         args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
2096                                         NFS_MOUNT_LOCAL_FCNTL);
2097                 /*
2098                  * The legacy version 6 binary mount data from userspace has a
2099                  * field used only to transport selinux information into the
2100                  * the kernel.  To continue to support that functionality we
2101                  * have a touch of selinux knowledge here in the NFS code. The
2102                  * userspace code converted context=blah to just blah so we are
2103                  * converting back to the full string selinux understands.
2104                  */
2105                 if (data->context[0]){
2106 #ifdef CONFIG_SECURITY_SELINUX
2107                         int rc;
2108                         data->context[NFS_MAX_CONTEXT_LEN] = '\0';
2109                         rc = security_add_mnt_opt("context", data->context,
2110                                         strlen(data->context), &args->lsm_opts);
2111                         if (rc)
2112                                 return rc;
2113 #else
2114                         return -EINVAL;
2115 #endif
2116                 }
2117
2118                 break;
2119         default:
2120                 return NFS_TEXT_DATA;
2121         }
2122
2123         return 0;
2124
2125 out_no_data:
2126         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
2127         return -EINVAL;
2128
2129 out_no_v3:
2130         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
2131                  data->version);
2132         return -EINVAL;
2133
2134 out_no_sec:
2135         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
2136         return -EINVAL;
2137
2138 out_nomem:
2139         dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
2140         return -ENOMEM;
2141
2142 out_no_address:
2143         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2144         return -EINVAL;
2145
2146 out_invalid_fh:
2147         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
2148         return -EINVAL;
2149 }
2150
2151 #if IS_ENABLED(CONFIG_NFS_V4)
2152 static int nfs_validate_mount_data(struct file_system_type *fs_type,
2153                                    void *options,
2154                                    struct nfs_parsed_mount_data *args,
2155                                    struct nfs_fh *mntfh,
2156                                    const char *dev_name)
2157 {
2158         if (fs_type == &nfs_fs_type)
2159                 return nfs23_validate_mount_data(options, args, mntfh, dev_name);
2160         return nfs4_validate_mount_data(options, args, dev_name);
2161 }
2162 #else
2163 static int nfs_validate_mount_data(struct file_system_type *fs_type,
2164                                    void *options,
2165                                    struct nfs_parsed_mount_data *args,
2166                                    struct nfs_fh *mntfh,
2167                                    const char *dev_name)
2168 {
2169         return nfs23_validate_mount_data(options, args, mntfh, dev_name);
2170 }
2171 #endif
2172
2173 static int nfs_validate_text_mount_data(void *options,
2174                                         struct nfs_parsed_mount_data *args,
2175                                         const char *dev_name)
2176 {
2177         int port = 0;
2178         int max_namelen = PAGE_SIZE;
2179         int max_pathlen = NFS_MAXPATHLEN;
2180         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2181
2182         if (nfs_parse_mount_options((char *)options, args) == 0)
2183                 return -EINVAL;
2184
2185         if (!nfs_verify_server_address(sap))
2186                 goto out_no_address;
2187
2188         if (args->version == 4) {
2189 #if IS_ENABLED(CONFIG_NFS_V4)
2190                 if (args->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
2191                         port = NFS_RDMA_PORT;
2192                 else
2193                         port = NFS_PORT;
2194                 max_namelen = NFS4_MAXNAMLEN;
2195                 max_pathlen = NFS4_MAXPATHLEN;
2196                 nfs_validate_transport_protocol(args);
2197                 if (args->nfs_server.protocol == XPRT_TRANSPORT_UDP)
2198                         goto out_invalid_transport_udp;
2199                 nfs4_validate_mount_flags(args);
2200 #else
2201                 goto out_v4_not_compiled;
2202 #endif /* CONFIG_NFS_V4 */
2203         } else {
2204                 nfs_set_mount_transport_protocol(args);
2205                 if (args->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
2206                         port = NFS_RDMA_PORT;
2207         }
2208
2209         nfs_set_port(sap, &args->nfs_server.port, port);
2210
2211         return nfs_parse_devname(dev_name,
2212                                    &args->nfs_server.hostname,
2213                                    max_namelen,
2214                                    &args->nfs_server.export_path,
2215                                    max_pathlen);
2216
2217 #if !IS_ENABLED(CONFIG_NFS_V4)
2218 out_v4_not_compiled:
2219         dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
2220         return -EPROTONOSUPPORT;
2221 #else
2222 out_invalid_transport_udp:
2223         dfprintk(MOUNT, "NFSv4: Unsupported transport protocol udp\n");
2224         return -EINVAL;
2225 #endif /* !CONFIG_NFS_V4 */
2226
2227 out_no_address:
2228         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2229         return -EINVAL;
2230 }
2231
2232 #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
2233                 | NFS_MOUNT_SECURE \
2234                 | NFS_MOUNT_TCP \
2235                 | NFS_MOUNT_VER3 \
2236                 | NFS_MOUNT_KERBEROS \
2237                 | NFS_MOUNT_NONLM \
2238                 | NFS_MOUNT_BROKEN_SUID \
2239                 | NFS_MOUNT_STRICTLOCK \
2240                 | NFS_MOUNT_LEGACY_INTERFACE)
2241
2242 #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
2243                 ~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
2244
2245 static int
2246 nfs_compare_remount_data(struct nfs_server *nfss,
2247                          struct nfs_parsed_mount_data *data)
2248 {
2249         if ((data->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
2250             data->rsize != nfss->rsize ||
2251             data->wsize != nfss->wsize ||
2252             data->version != nfss->nfs_client->rpc_ops->version ||
2253             data->minorversion != nfss->nfs_client->cl_minorversion ||
2254             data->retrans != nfss->client->cl_timeout->to_retries ||
2255             !nfs_auth_info_match(&data->auth_info, nfss->client->cl_auth->au_flavor) ||
2256             data->acregmin != nfss->acregmin / HZ ||
2257             data->acregmax != nfss->acregmax / HZ ||
2258             data->acdirmin != nfss->acdirmin / HZ ||
2259             data->acdirmax != nfss->acdirmax / HZ ||
2260             data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
2261             (data->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
2262             data->nfs_server.port != nfss->port ||
2263             data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
2264             !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2265                           (struct sockaddr *)&nfss->nfs_client->cl_addr))
2266                 return -EINVAL;
2267
2268         return 0;
2269 }
2270
2271 int
2272 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2273 {
2274         int error;
2275         struct nfs_server *nfss = sb->s_fs_info;
2276         struct nfs_parsed_mount_data *data;
2277         struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2278         struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
2279         u32 nfsvers = nfss->nfs_client->rpc_ops->version;
2280
2281         sync_filesystem(sb);
2282
2283         /*
2284          * Userspace mount programs that send binary options generally send
2285          * them populated with default values. We have no way to know which
2286          * ones were explicitly specified. Fall back to legacy behavior and
2287          * just return success.
2288          */
2289         if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2290             (nfsvers <= 3 && (!options || (options->version >= 1 &&
2291                                            options->version <= 6))))
2292                 return 0;
2293
2294         data = nfs_alloc_parsed_mount_data();
2295         if (data == NULL)
2296                 return -ENOMEM;
2297
2298         /* fill out struct with values from existing mount */
2299         data->flags = nfss->flags;
2300         data->rsize = nfss->rsize;
2301         data->wsize = nfss->wsize;
2302         data->retrans = nfss->client->cl_timeout->to_retries;
2303         data->selected_flavor = nfss->client->cl_auth->au_flavor;
2304         data->acregmin = nfss->acregmin / HZ;
2305         data->acregmax = nfss->acregmax / HZ;
2306         data->acdirmin = nfss->acdirmin / HZ;
2307         data->acdirmax = nfss->acdirmax / HZ;
2308         data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
2309         data->nfs_server.port = nfss->port;
2310         data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2311         data->version = nfsvers;
2312         data->minorversion = nfss->nfs_client->cl_minorversion;
2313         data->net = current->nsproxy->net_ns;
2314         memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2315                 data->nfs_server.addrlen);
2316
2317         /* overwrite those values with any that were specified */
2318         error = -EINVAL;
2319         if (!nfs_parse_mount_options((char *)options, data))
2320                 goto out;
2321
2322         /*
2323          * noac is a special case. It implies -o sync, but that's not
2324          * necessarily reflected in the mtab options. do_remount_sb
2325          * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
2326          * remount options, so we have to explicitly reset it.
2327          */
2328         if (data->flags & NFS_MOUNT_NOAC)
2329                 *flags |= SB_SYNCHRONOUS;
2330
2331         /* compare new mount options with old ones */
2332         error = nfs_compare_remount_data(nfss, data);
2333         if (!error)
2334                 error = security_sb_remount(sb, data->lsm_opts);
2335 out:
2336         nfs_free_parsed_mount_data(data);
2337         return error;
2338 }
2339 EXPORT_SYMBOL_GPL(nfs_remount);
2340
2341 /*
2342  * Finish setting up an NFS superblock
2343  */
2344 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_info *mount_info)
2345 {
2346         struct nfs_parsed_mount_data *data = mount_info->parsed;
2347         struct nfs_server *server = NFS_SB(sb);
2348
2349         sb->s_blocksize_bits = 0;
2350         sb->s_blocksize = 0;
2351         sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
2352         sb->s_op = server->nfs_client->cl_nfs_mod->sops;
2353         if (data && data->bsize)
2354                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2355
2356         if (server->nfs_client->rpc_ops->version != 2) {
2357                 /* The VFS shouldn't apply the umask to mode bits. We will do
2358                  * so ourselves when necessary.
2359                  */
2360                 sb->s_flags |= SB_POSIXACL;
2361                 sb->s_time_gran = 1;
2362                 sb->s_export_op = &nfs_export_ops;
2363         } else
2364                 sb->s_time_gran = 1000;
2365
2366         if (server->nfs_client->rpc_ops->version != 4) {
2367                 sb->s_time_min = 0;
2368                 sb->s_time_max = U32_MAX;
2369         } else {
2370                 sb->s_time_min = S64_MIN;
2371                 sb->s_time_max = S64_MAX;
2372         }
2373
2374         sb->s_magic = NFS_SUPER_MAGIC;
2375
2376         /* We probably want something more informative here */
2377         snprintf(sb->s_id, sizeof(sb->s_id),
2378                  "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2379
2380         if (sb->s_blocksize == 0)
2381                 sb->s_blocksize = nfs_block_bits(server->wsize,
2382                                                  &sb->s_blocksize_bits);
2383
2384         nfs_super_set_maxbytes(sb, server->maxfilesize);
2385 }
2386
2387 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2388 {
2389         const struct nfs_server *a = s->s_fs_info;
2390         const struct rpc_clnt *clnt_a = a->client;
2391         const struct rpc_clnt *clnt_b = b->client;
2392
2393         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2394                 goto Ebusy;
2395         if (a->nfs_client != b->nfs_client)
2396                 goto Ebusy;
2397         if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
2398                 goto Ebusy;
2399         if (a->wsize != b->wsize)
2400                 goto Ebusy;
2401         if (a->rsize != b->rsize)
2402                 goto Ebusy;
2403         if (a->acregmin != b->acregmin)
2404                 goto Ebusy;
2405         if (a->acregmax != b->acregmax)
2406                 goto Ebusy;
2407         if (a->acdirmin != b->acdirmin)
2408                 goto Ebusy;
2409         if (a->acdirmax != b->acdirmax)
2410                 goto Ebusy;
2411         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2412                 goto Ebusy;
2413         return 1;
2414 Ebusy:
2415         return 0;
2416 }
2417
2418 struct nfs_sb_mountdata {
2419         struct nfs_server *server;
2420         int mntflags;
2421 };
2422
2423 static int nfs_set_super(struct super_block *s, void *data)
2424 {
2425         struct nfs_sb_mountdata *sb_mntdata = data;
2426         struct nfs_server *server = sb_mntdata->server;
2427         int ret;
2428
2429         s->s_flags = sb_mntdata->mntflags;
2430         s->s_fs_info = server;
2431         s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
2432         ret = set_anon_super(s, server);
2433         if (ret == 0)
2434                 server->s_dev = s->s_dev;
2435         return ret;
2436 }
2437
2438 static int nfs_compare_super_address(struct nfs_server *server1,
2439                                      struct nfs_server *server2)
2440 {
2441         struct sockaddr *sap1, *sap2;
2442         struct rpc_xprt *xprt1 = server1->client->cl_xprt;
2443         struct rpc_xprt *xprt2 = server2->client->cl_xprt;
2444
2445         if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
2446                 return 0;
2447
2448         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2449         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2450
2451         if (sap1->sa_family != sap2->sa_family)
2452                 return 0;
2453
2454         switch (sap1->sa_family) {
2455         case AF_INET: {
2456                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2457                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2458                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2459                         return 0;
2460                 if (sin1->sin_port != sin2->sin_port)
2461                         return 0;
2462                 break;
2463         }
2464         case AF_INET6: {
2465                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2466                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2467                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2468                         return 0;
2469                 if (sin1->sin6_port != sin2->sin6_port)
2470                         return 0;
2471                 break;
2472         }
2473         default:
2474                 return 0;
2475         }
2476
2477         return 1;
2478 }
2479
2480 static int nfs_compare_userns(const struct nfs_server *old,
2481                 const struct nfs_server *new)
2482 {
2483         const struct user_namespace *oldns = &init_user_ns;
2484         const struct user_namespace *newns = &init_user_ns;
2485
2486         if (old->client && old->client->cl_cred)
2487                 oldns = old->client->cl_cred->user_ns;
2488         if (new->client && new->client->cl_cred)
2489                 newns = new->client->cl_cred->user_ns;
2490         if (oldns != newns)
2491                 return 0;
2492         return 1;
2493 }
2494
2495 static int nfs_compare_super(struct super_block *sb, void *data)
2496 {
2497         struct nfs_sb_mountdata *sb_mntdata = data;
2498         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2499         int mntflags = sb_mntdata->mntflags;
2500
2501         if (!nfs_compare_super_address(old, server))
2502                 return 0;
2503         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2504         if (old->flags & NFS_MOUNT_UNSHARED)
2505                 return 0;
2506         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2507                 return 0;
2508         if (!nfs_compare_userns(old, server))
2509                 return 0;
2510         return nfs_compare_mount_options(sb, server, mntflags);
2511 }
2512
2513 #ifdef CONFIG_NFS_FSCACHE
2514 static void nfs_get_cache_cookie(struct super_block *sb,
2515                                  struct nfs_parsed_mount_data *parsed,
2516                                  struct nfs_clone_mount *cloned)
2517 {
2518         struct nfs_server *nfss = NFS_SB(sb);
2519         char *uniq = NULL;
2520         int ulen = 0;
2521
2522         nfss->fscache_key = NULL;
2523         nfss->fscache = NULL;
2524
2525         if (parsed) {
2526                 if (!(parsed->options & NFS_OPTION_FSCACHE))
2527                         return;
2528                 if (parsed->fscache_uniq) {
2529                         uniq = parsed->fscache_uniq;
2530                         ulen = strlen(parsed->fscache_uniq);
2531                 }
2532         } else if (cloned) {
2533                 struct nfs_server *mnt_s = NFS_SB(cloned->sb);
2534                 if (!(mnt_s->options & NFS_OPTION_FSCACHE))
2535                         return;
2536                 if (mnt_s->fscache_key) {
2537                         uniq = mnt_s->fscache_key->key.uniquifier;
2538                         ulen = mnt_s->fscache_key->key.uniq_len;
2539                 }
2540         } else
2541                 return;
2542
2543         nfs_fscache_get_super_cookie(sb, uniq, ulen);
2544 }
2545 #else
2546 static void nfs_get_cache_cookie(struct super_block *sb,
2547                                  struct nfs_parsed_mount_data *parsed,
2548                                  struct nfs_clone_mount *cloned)
2549 {
2550 }
2551 #endif
2552
2553 static void nfs_set_readahead(struct backing_dev_info *bdi,
2554                               unsigned long iomax_pages)
2555 {
2556         bdi->ra_pages = VM_READAHEAD_PAGES;
2557         bdi->io_pages = iomax_pages;
2558 }
2559
2560 static struct dentry *nfs_fs_mount_common(int flags, const char *dev_name,
2561                                    struct nfs_mount_info *mount_info)
2562 {
2563         struct super_block *s;
2564         struct dentry *mntroot = ERR_PTR(-ENOMEM);
2565         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2566         struct nfs_server *server = mount_info->server;
2567         unsigned long kflags = 0, kflags_out = 0;
2568         struct nfs_sb_mountdata sb_mntdata = {
2569                 .mntflags = flags,
2570                 .server = server,
2571         };
2572         int error;
2573
2574         mount_info->server = NULL;
2575         if (IS_ERR(server))
2576                 return ERR_CAST(server);
2577
2578         if (server->flags & NFS_MOUNT_UNSHARED)
2579                 compare_super = NULL;
2580
2581         /* -o noac implies -o sync */
2582         if (server->flags & NFS_MOUNT_NOAC)
2583                 sb_mntdata.mntflags |= SB_SYNCHRONOUS;
2584
2585         if (mount_info->cloned != NULL && mount_info->cloned->sb != NULL)
2586                 if (mount_info->cloned->sb->s_flags & SB_SYNCHRONOUS)
2587                         sb_mntdata.mntflags |= SB_SYNCHRONOUS;
2588
2589         /* Get a superblock - note that we may end up sharing one that already exists */
2590         s = sget(mount_info->nfs_mod->nfs_fs, compare_super, nfs_set_super,
2591                  flags, &sb_mntdata);
2592         if (IS_ERR(s)) {
2593                 mntroot = ERR_CAST(s);
2594                 goto out_err_nosb;
2595         }
2596
2597         if (s->s_fs_info != server) {
2598                 nfs_free_server(server);
2599                 server = NULL;
2600         } else {
2601                 error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
2602                                              MINOR(server->s_dev));
2603                 if (error) {
2604                         mntroot = ERR_PTR(error);
2605                         goto error_splat_super;
2606                 }
2607                 nfs_set_readahead(s->s_bdi, server->rpages);
2608                 server->super = s;
2609         }
2610
2611         if (!s->s_root) {
2612                 unsigned bsize = mount_info->inherited_bsize;
2613                 /* initial superblock/root creation */
2614                 nfs_fill_super(s, mount_info);
2615                 if (bsize) {
2616                         s->s_blocksize_bits = bsize;
2617                         s->s_blocksize = 1U << bsize;
2618                 }
2619                 nfs_get_cache_cookie(s, mount_info->parsed, mount_info->cloned);
2620                 if (!(server->flags & NFS_MOUNT_UNSHARED))
2621                         s->s_iflags |= SB_I_MULTIROOT;
2622         }
2623
2624         mntroot = nfs_get_root(s, mount_info->mntfh, dev_name);
2625         if (IS_ERR(mntroot))
2626                 goto error_splat_super;
2627
2628
2629         if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
2630                 kflags |= SECURITY_LSM_NATIVE_LABELS;
2631         if (mount_info->cloned) {
2632                 if (d_inode(mntroot)->i_fop != &nfs_dir_operations) {
2633                         error = -ESTALE;
2634                         goto error_splat_root;
2635                 }
2636                 /* clone any lsm security options from the parent to the new sb */
2637                 error = security_sb_clone_mnt_opts(mount_info->cloned->sb, s, kflags,
2638                                 &kflags_out);
2639         } else {
2640                 error = security_sb_set_mnt_opts(s, mount_info->parsed->lsm_opts,
2641                                                         kflags, &kflags_out);
2642         }
2643         if (error)
2644                 goto error_splat_root;
2645         if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
2646                 !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
2647                 NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
2648         if (error)
2649                 goto error_splat_root;
2650
2651         s->s_flags |= SB_ACTIVE;
2652
2653 out:
2654         return mntroot;
2655
2656 out_err_nosb:
2657         nfs_free_server(server);
2658         goto out;
2659
2660 error_splat_root:
2661         dput(mntroot);
2662         mntroot = ERR_PTR(error);
2663 error_splat_super:
2664         deactivate_locked_super(s);
2665         goto out;
2666 }
2667
2668 struct dentry *nfs_fs_mount(struct file_system_type *fs_type,
2669         int flags, const char *dev_name, void *raw_data)
2670 {
2671         struct nfs_mount_info mount_info = {
2672         };
2673         struct dentry *mntroot = ERR_PTR(-ENOMEM);
2674         struct nfs_subversion *nfs_mod;
2675         int error;
2676
2677         mount_info.parsed = nfs_alloc_parsed_mount_data();
2678         mount_info.mntfh = nfs_alloc_fhandle();
2679         if (mount_info.parsed == NULL || mount_info.mntfh == NULL)
2680                 goto out;
2681
2682         /* Validate the mount data */
2683         error = nfs_validate_mount_data(fs_type, raw_data, mount_info.parsed, mount_info.mntfh, dev_name);
2684         if (error == NFS_TEXT_DATA)
2685                 error = nfs_validate_text_mount_data(raw_data, mount_info.parsed, dev_name);
2686         if (error < 0) {
2687                 mntroot = ERR_PTR(error);
2688                 goto out;
2689         }
2690
2691         nfs_mod = get_nfs_version(mount_info.parsed->version);
2692         if (IS_ERR(nfs_mod)) {
2693                 mntroot = ERR_CAST(nfs_mod);
2694                 goto out;
2695         }
2696         mount_info.nfs_mod = nfs_mod;
2697
2698         mntroot = nfs_mod->rpc_ops->try_mount(flags, dev_name, &mount_info);
2699
2700         put_nfs_version(nfs_mod);
2701 out:
2702         nfs_free_parsed_mount_data(mount_info.parsed);
2703         nfs_free_fhandle(mount_info.mntfh);
2704         return mntroot;
2705 }
2706 EXPORT_SYMBOL_GPL(nfs_fs_mount);
2707
2708 /*
2709  * Destroy an NFS2/3 superblock
2710  */
2711 void nfs_kill_super(struct super_block *s)
2712 {
2713         struct nfs_server *server = NFS_SB(s);
2714         dev_t dev = s->s_dev;
2715
2716         generic_shutdown_super(s);
2717
2718         nfs_fscache_release_super_cookie(s);
2719
2720         nfs_free_server(server);
2721         free_anon_bdev(dev);
2722 }
2723 EXPORT_SYMBOL_GPL(nfs_kill_super);
2724
2725 /*
2726  * Internal use only: mount_info is already set up by caller.
2727  * Used for mountpoint crossings and for nfs4 root.
2728  */
2729 static struct dentry *
2730 nfs_prepared_mount(struct file_system_type *fs_type, int flags,
2731                    const char *dev_name, void *raw_data)
2732 {
2733         return nfs_fs_mount_common(flags, dev_name, raw_data);
2734 }
2735
2736 #if IS_ENABLED(CONFIG_NFS_V4)
2737
2738 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2739 {
2740         args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2741                          NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
2742 }
2743
2744 /*
2745  * Validate NFSv4 mount options
2746  */
2747 static int nfs4_validate_mount_data(void *options,
2748                                     struct nfs_parsed_mount_data *args,
2749                                     const char *dev_name)
2750 {
2751         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2752         struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2753         char *c;
2754
2755         if (data == NULL)
2756                 goto out_no_data;
2757
2758         args->version = 4;
2759
2760         switch (data->version) {
2761         case 1:
2762                 if (data->host_addrlen > sizeof(args->nfs_server.address))
2763                         goto out_no_address;
2764                 if (data->host_addrlen == 0)
2765                         goto out_no_address;
2766                 args->nfs_server.addrlen = data->host_addrlen;
2767                 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
2768                         return -EFAULT;
2769                 if (!nfs_verify_server_address(sap))
2770                         goto out_no_address;
2771                 args->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2772
2773                 if (data->auth_flavourlen) {
2774                         rpc_authflavor_t pseudoflavor;
2775                         if (data->auth_flavourlen > 1)
2776                                 goto out_inval_auth;
2777                         if (copy_from_user(&pseudoflavor,
2778                                            data->auth_flavours,
2779                                            sizeof(pseudoflavor)))
2780                                 return -EFAULT;
2781                         args->selected_flavor = pseudoflavor;
2782                 } else
2783                         args->selected_flavor = RPC_AUTH_UNIX;
2784
2785                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2786                 if (IS_ERR(c))
2787                         return PTR_ERR(c);
2788                 args->nfs_server.hostname = c;
2789
2790                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2791                 if (IS_ERR(c))
2792                         return PTR_ERR(c);
2793                 args->nfs_server.export_path = c;
2794                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2795
2796                 c = strndup_user(data->client_addr.data, 16);
2797                 if (IS_ERR(c))
2798                         return PTR_ERR(c);
2799                 args->client_address = c;
2800
2801                 /*
2802                  * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2803                  * can deal with.
2804                  */
2805
2806                 args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
2807                 args->rsize     = data->rsize;
2808                 args->wsize     = data->wsize;
2809                 args->timeo     = data->timeo;
2810                 args->retrans   = data->retrans;
2811                 args->acregmin  = data->acregmin;
2812                 args->acregmax  = data->acregmax;
2813                 args->acdirmin  = data->acdirmin;
2814                 args->acdirmax  = data->acdirmax;
2815                 args->nfs_server.protocol = data->proto;
2816                 nfs_validate_transport_protocol(args);
2817                 if (args->nfs_server.protocol == XPRT_TRANSPORT_UDP)
2818                         goto out_invalid_transport_udp;
2819
2820                 break;
2821         default:
2822                 return NFS_TEXT_DATA;
2823         }
2824
2825         return 0;
2826
2827 out_no_data:
2828         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2829         return -EINVAL;
2830
2831 out_inval_auth:
2832         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2833                  data->auth_flavourlen);
2834         return -EINVAL;
2835
2836 out_no_address:
2837         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2838         return -EINVAL;
2839
2840 out_invalid_transport_udp:
2841         dfprintk(MOUNT, "NFSv4: Unsupported transport protocol udp\n");
2842         return -EINVAL;
2843 }
2844
2845 /*
2846  * NFS v4 module parameters need to stay in the
2847  * NFS client for backwards compatibility
2848  */
2849 unsigned int nfs_callback_set_tcpport;
2850 unsigned short nfs_callback_nr_threads;
2851 /* Default cache timeout is 10 minutes */
2852 unsigned int nfs_idmap_cache_timeout = 600;
2853 /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
2854 bool nfs4_disable_idmapping = true;
2855 unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
2856 unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
2857 unsigned short send_implementation_id = 1;
2858 char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
2859 bool recover_lost_locks = false;
2860
2861 EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
2862 EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
2863 EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
2864 EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
2865 EXPORT_SYMBOL_GPL(max_session_slots);
2866 EXPORT_SYMBOL_GPL(max_session_cb_slots);
2867 EXPORT_SYMBOL_GPL(send_implementation_id);
2868 EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
2869 EXPORT_SYMBOL_GPL(recover_lost_locks);
2870
2871 #define NFS_CALLBACK_MAXPORTNR (65535U)
2872
2873 static int param_set_portnr(const char *val, const struct kernel_param *kp)
2874 {
2875         unsigned long num;
2876         int ret;
2877
2878         if (!val)
2879                 return -EINVAL;
2880         ret = kstrtoul(val, 0, &num);
2881         if (ret || num > NFS_CALLBACK_MAXPORTNR)
2882                 return -EINVAL;
2883         *((unsigned int *)kp->arg) = num;
2884         return 0;
2885 }
2886 static const struct kernel_param_ops param_ops_portnr = {
2887         .set = param_set_portnr,
2888         .get = param_get_uint,
2889 };
2890 #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
2891
2892 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
2893 module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
2894 MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
2895                 "assigned to the NFSv4 callback channels.");
2896 module_param(nfs_idmap_cache_timeout, int, 0644);
2897 module_param(nfs4_disable_idmapping, bool, 0644);
2898 module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
2899                         NFS4_CLIENT_ID_UNIQ_LEN, 0600);
2900 MODULE_PARM_DESC(nfs4_disable_idmapping,
2901                 "Turn off NFSv4 idmapping when using 'sec=sys'");
2902 module_param(max_session_slots, ushort, 0644);
2903 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
2904                 "requests the client will negotiate");
2905 module_param(max_session_cb_slots, ushort, 0644);
2906 MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
2907                 "callbacks the client will process for a given server");
2908 module_param(send_implementation_id, ushort, 0644);
2909 MODULE_PARM_DESC(send_implementation_id,
2910                 "Send implementation ID with NFSv4.1 exchange_id");
2911 MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
2912
2913 module_param(recover_lost_locks, bool, 0644);
2914 MODULE_PARM_DESC(recover_lost_locks,
2915                  "If the server reports that a lock might be lost, "
2916                  "try to recover it risking data corruption.");
2917
2918
2919 #endif /* CONFIG_NFS_V4 */