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