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