Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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