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