blob: 47cf83e917bea4f635bddd77fa44faef136590c3 [file] [log] [blame]
David Howellsf7b422b2006-06-09 09:34:33 -04001/*
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.Cox@linux.org>, 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 *
David Howells54ceac42006-08-22 20:06:13 -040016 * - 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())
David Howellsf7b422b2006-06-09 09:34:33 -040021 */
22
David Howellsf7b422b2006-06-09 09:34:33 -040023#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>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040036#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040037#include <linux/sunrpc/xprtrdma.h>
David Howellsf7b422b2006-06-09 09:34:33 -040038#include <linux/nfs_fs.h>
39#include <linux/nfs_mount.h>
40#include <linux/nfs4_mount.h>
41#include <linux/lockd/bind.h>
42#include <linux/smp_lock.h>
43#include <linux/seq_file.h>
44#include <linux/mount.h>
45#include <linux/nfs_idmap.h>
46#include <linux/vfs.h>
47#include <linux/inet.h>
Chuck Leverfd00a8f2007-12-10 14:57:38 -050048#include <linux/in6.h>
49#include <net/ipv6.h>
Chuck Leverd8e77482008-06-23 12:37:01 -040050#include <linux/netdevice.h>
David Howellsf7b422b2006-06-09 09:34:33 -040051#include <linux/nfs_xdr.h>
Adrian Bunkb5d5dfb2007-02-12 00:53:40 -080052#include <linux/magic.h>
Chuck Leverbf0fd762007-07-01 12:13:44 -040053#include <linux/parser.h>
David Howellsf7b422b2006-06-09 09:34:33 -040054
55#include <asm/system.h>
56#include <asm/uaccess.h>
57
58#include "nfs4_fs.h"
59#include "callback.h"
60#include "delegation.h"
61#include "iostat.h"
62#include "internal.h"
63
64#define NFSDBG_FACILITY NFSDBG_VFS
65
Chuck Leverbf0fd762007-07-01 12:13:44 -040066enum {
67 /* Mount options that take no arguments */
68 Opt_soft, Opt_hard,
Chuck Leverbf0fd762007-07-01 12:13:44 -040069 Opt_posix, Opt_noposix,
70 Opt_cto, Opt_nocto,
71 Opt_ac, Opt_noac,
72 Opt_lock, Opt_nolock,
73 Opt_v2, Opt_v3,
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040074 Opt_udp, Opt_tcp, Opt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -040075 Opt_acl, Opt_noacl,
76 Opt_rdirplus, Opt_nordirplus,
Trond Myklebust75180df2007-05-16 16:53:28 -040077 Opt_sharecache, Opt_nosharecache,
Chuck Leverbf0fd762007-07-01 12:13:44 -040078
79 /* Mount options that take integer arguments */
80 Opt_port,
81 Opt_rsize, Opt_wsize, Opt_bsize,
82 Opt_timeo, Opt_retrans,
83 Opt_acregmin, Opt_acregmax,
84 Opt_acdirmin, Opt_acdirmax,
85 Opt_actimeo,
86 Opt_namelen,
87 Opt_mountport,
Chuck Levere887cbc2007-10-26 13:32:29 -040088 Opt_mountvers,
Chuck Leverad879ce2007-10-26 13:32:24 -040089 Opt_nfsvers,
Chuck Leverbf0fd762007-07-01 12:13:44 -040090
91 /* Mount options that take string arguments */
Chuck Lever33832032007-12-10 14:59:13 -050092 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
Chuck Lever0ac83772007-09-11 18:01:04 -040093 Opt_addr, Opt_mountaddr, Opt_clientaddr,
Chuck Leverbf0fd762007-07-01 12:13:44 -040094
Chuck Leverf45663c2008-06-24 19:28:02 -040095 /* Special mount options */
96 Opt_userspace, Opt_deprecated, Opt_sloppy,
Chuck Leverbf0fd762007-07-01 12:13:44 -040097
98 Opt_err
99};
100
101static match_table_t nfs_mount_option_tokens = {
102 { Opt_userspace, "bg" },
103 { Opt_userspace, "fg" },
Chuck Leverecbb3842008-06-12 12:37:33 -0400104 { Opt_userspace, "retry=%s" },
105
Chuck Leverf45663c2008-06-24 19:28:02 -0400106 { Opt_sloppy, "sloppy" },
107
Chuck Leverbf0fd762007-07-01 12:13:44 -0400108 { Opt_soft, "soft" },
109 { Opt_hard, "hard" },
Chuck Leverd33e4df2008-06-12 12:37:41 -0400110 { Opt_deprecated, "intr" },
111 { Opt_deprecated, "nointr" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400112 { Opt_posix, "posix" },
113 { Opt_noposix, "noposix" },
114 { Opt_cto, "cto" },
115 { Opt_nocto, "nocto" },
116 { Opt_ac, "ac" },
117 { Opt_noac, "noac" },
118 { Opt_lock, "lock" },
119 { Opt_nolock, "nolock" },
120 { Opt_v2, "v2" },
121 { Opt_v3, "v3" },
122 { Opt_udp, "udp" },
123 { Opt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400124 { Opt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400125 { Opt_acl, "acl" },
126 { Opt_noacl, "noacl" },
127 { Opt_rdirplus, "rdirplus" },
128 { Opt_nordirplus, "nordirplus" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400129 { Opt_sharecache, "sharecache" },
130 { Opt_nosharecache, "nosharecache" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400131
132 { Opt_port, "port=%u" },
133 { Opt_rsize, "rsize=%u" },
134 { Opt_wsize, "wsize=%u" },
135 { Opt_bsize, "bsize=%u" },
136 { Opt_timeo, "timeo=%u" },
137 { Opt_retrans, "retrans=%u" },
138 { Opt_acregmin, "acregmin=%u" },
139 { Opt_acregmax, "acregmax=%u" },
140 { Opt_acdirmin, "acdirmin=%u" },
141 { Opt_acdirmax, "acdirmax=%u" },
142 { Opt_actimeo, "actimeo=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400143 { Opt_namelen, "namlen=%u" },
144 { Opt_mountport, "mountport=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400145 { Opt_mountvers, "mountvers=%u" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400146 { Opt_nfsvers, "nfsvers=%u" },
147 { Opt_nfsvers, "vers=%u" },
148
149 { Opt_sec, "sec=%s" },
150 { Opt_proto, "proto=%s" },
151 { Opt_mountproto, "mountproto=%s" },
152 { Opt_addr, "addr=%s" },
153 { Opt_clientaddr, "clientaddr=%s" },
Chuck Lever33832032007-12-10 14:59:13 -0500154 { Opt_mounthost, "mounthost=%s" },
Chuck Lever0ac83772007-09-11 18:01:04 -0400155 { Opt_mountaddr, "mountaddr=%s" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400156
157 { Opt_err, NULL }
158};
159
160enum {
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400161 Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
Chuck Leverbf0fd762007-07-01 12:13:44 -0400162
163 Opt_xprt_err
164};
165
166static match_table_t nfs_xprt_protocol_tokens = {
167 { Opt_xprt_udp, "udp" },
168 { Opt_xprt_tcp, "tcp" },
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400169 { Opt_xprt_rdma, "rdma" },
Chuck Leverbf0fd762007-07-01 12:13:44 -0400170
171 { Opt_xprt_err, NULL }
172};
173
174enum {
175 Opt_sec_none, Opt_sec_sys,
176 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
177 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
178 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
179
180 Opt_sec_err
181};
182
183static match_table_t nfs_secflavor_tokens = {
184 { Opt_sec_none, "none" },
185 { Opt_sec_none, "null" },
186 { Opt_sec_sys, "sys" },
187
188 { Opt_sec_krb5, "krb5" },
189 { Opt_sec_krb5i, "krb5i" },
190 { Opt_sec_krb5p, "krb5p" },
191
192 { Opt_sec_lkey, "lkey" },
193 { Opt_sec_lkeyi, "lkeyi" },
194 { Opt_sec_lkeyp, "lkeyp" },
195
Olga Kornievskaia8d042212008-02-13 16:47:06 -0500196 { Opt_sec_spkm, "spkm3" },
197 { Opt_sec_spkmi, "spkm3i" },
198 { Opt_sec_spkmp, "spkm3p" },
199
Chuck Leverbf0fd762007-07-01 12:13:44 -0400200 { Opt_sec_err, NULL }
201};
202
203
Al Viro42faad92008-04-24 07:21:56 -0400204static void nfs_umount_begin(struct super_block *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400205static int nfs_statfs(struct dentry *, struct kstatfs *);
David Howellsf7b422b2006-06-09 09:34:33 -0400206static int nfs_show_options(struct seq_file *, struct vfsmount *);
207static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebust816724e2006-06-24 08:41:41 -0400208static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
David Howells54ceac42006-08-22 20:06:13 -0400209static int nfs_xdev_get_sb(struct file_system_type *fs_type,
Trond Myklebust816724e2006-06-24 08:41:41 -0400210 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400211static void nfs_kill_super(struct super_block *);
Steve Dicksonef818a22007-11-08 04:05:04 -0500212static void nfs_put_super(struct super_block *);
Jeff Layton48b605f2008-06-10 15:38:39 -0400213static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
David Howellsf7b422b2006-06-09 09:34:33 -0400214
215static struct file_system_type nfs_fs_type = {
216 .owner = THIS_MODULE,
217 .name = "nfs",
218 .get_sb = nfs_get_sb,
219 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700220 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400221};
222
David Howells54ceac42006-08-22 20:06:13 -0400223struct file_system_type nfs_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400224 .owner = THIS_MODULE,
225 .name = "nfs",
David Howells54ceac42006-08-22 20:06:13 -0400226 .get_sb = nfs_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400227 .kill_sb = nfs_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700228 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400229};
230
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800231static const struct super_operations nfs_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400232 .alloc_inode = nfs_alloc_inode,
233 .destroy_inode = nfs_destroy_inode,
234 .write_inode = nfs_write_inode,
Steve Dicksonef818a22007-11-08 04:05:04 -0500235 .put_super = nfs_put_super,
David Howellsf7b422b2006-06-09 09:34:33 -0400236 .statfs = nfs_statfs,
237 .clear_inode = nfs_clear_inode,
238 .umount_begin = nfs_umount_begin,
239 .show_options = nfs_show_options,
240 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400241 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400242};
243
244#ifdef CONFIG_NFS_V4
Trond Myklebust816724e2006-06-24 08:41:41 -0400245static int nfs4_get_sb(struct file_system_type *fs_type,
246 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howells54ceac42006-08-22 20:06:13 -0400247static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
248 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
249static int nfs4_referral_get_sb(struct file_system_type *fs_type,
250 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400251static void nfs4_kill_super(struct super_block *sb);
252
253static struct file_system_type nfs4_fs_type = {
254 .owner = THIS_MODULE,
255 .name = "nfs4",
256 .get_sb = nfs4_get_sb,
257 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700258 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400259};
260
David Howells54ceac42006-08-22 20:06:13 -0400261struct file_system_type nfs4_xdev_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400262 .owner = THIS_MODULE,
263 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400264 .get_sb = nfs4_xdev_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400265 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700266 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400267};
268
David Howells54ceac42006-08-22 20:06:13 -0400269struct file_system_type nfs4_referral_fs_type = {
David Howellsf7b422b2006-06-09 09:34:33 -0400270 .owner = THIS_MODULE,
271 .name = "nfs4",
David Howells54ceac42006-08-22 20:06:13 -0400272 .get_sb = nfs4_referral_get_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400273 .kill_sb = nfs4_kill_super,
Mark Fasheh349457c2006-09-08 14:22:21 -0700274 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
David Howellsf7b422b2006-06-09 09:34:33 -0400275};
276
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800277static const struct super_operations nfs4_sops = {
David Howellsf7b422b2006-06-09 09:34:33 -0400278 .alloc_inode = nfs_alloc_inode,
279 .destroy_inode = nfs_destroy_inode,
280 .write_inode = nfs_write_inode,
281 .statfs = nfs_statfs,
282 .clear_inode = nfs4_clear_inode,
283 .umount_begin = nfs_umount_begin,
284 .show_options = nfs_show_options,
285 .show_stats = nfs_show_stats,
Jeff Layton48b605f2008-06-10 15:38:39 -0400286 .remount_fs = nfs_remount,
David Howellsf7b422b2006-06-09 09:34:33 -0400287};
288#endif
289
Rusty Russell8e1f9362007-07-17 04:03:17 -0700290static struct shrinker acl_shrinker = {
291 .shrink = nfs_access_cache_shrinker,
292 .seeks = DEFAULT_SEEKS,
293};
Trond Myklebust979df722006-07-25 11:28:19 -0400294
David Howellsf7b422b2006-06-09 09:34:33 -0400295/*
296 * Register the NFS filesystems
297 */
298int __init register_nfs_fs(void)
299{
300 int ret;
301
302 ret = register_filesystem(&nfs_fs_type);
303 if (ret < 0)
304 goto error_0;
305
David Howellsf7b422b2006-06-09 09:34:33 -0400306 ret = nfs_register_sysctl();
307 if (ret < 0)
308 goto error_1;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800309#ifdef CONFIG_NFS_V4
David Howellsf7b422b2006-06-09 09:34:33 -0400310 ret = register_filesystem(&nfs4_fs_type);
311 if (ret < 0)
312 goto error_2;
313#endif
Rusty Russell8e1f9362007-07-17 04:03:17 -0700314 register_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400315 return 0;
316
317#ifdef CONFIG_NFS_V4
318error_2:
319 nfs_unregister_sysctl();
Peter Zijlstra89a09142007-03-16 13:38:26 -0800320#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400321error_1:
322 unregister_filesystem(&nfs_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400323error_0:
324 return ret;
325}
326
327/*
328 * Unregister the NFS filesystems
329 */
330void __exit unregister_nfs_fs(void)
331{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700332 unregister_shrinker(&acl_shrinker);
David Howellsf7b422b2006-06-09 09:34:33 -0400333#ifdef CONFIG_NFS_V4
334 unregister_filesystem(&nfs4_fs_type);
David Howellsf7b422b2006-06-09 09:34:33 -0400335#endif
Alexey Dobriyan49af7ee2007-09-18 22:46:40 -0700336 nfs_unregister_sysctl();
David Howellsf7b422b2006-06-09 09:34:33 -0400337 unregister_filesystem(&nfs_fs_type);
338}
339
Steve Dicksonef818a22007-11-08 04:05:04 -0500340void nfs_sb_active(struct nfs_server *server)
341{
342 atomic_inc(&server->active);
343}
344
345void nfs_sb_deactive(struct nfs_server *server)
346{
347 if (atomic_dec_and_test(&server->active))
348 wake_up(&server->active_wq);
349}
350
351static void nfs_put_super(struct super_block *sb)
352{
353 struct nfs_server *server = NFS_SB(sb);
354 /*
355 * Make sure there are no outstanding ops to this server.
356 * If so, wait for them to finish before allowing the
357 * unmount to continue.
358 */
359 wait_event(server->active_wq, atomic_read(&server->active) == 0);
360}
361
David Howellsf7b422b2006-06-09 09:34:33 -0400362/*
363 * Deliver file system statistics to userspace
364 */
Trond Myklebust816724e2006-06-24 08:41:41 -0400365static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howellsf7b422b2006-06-09 09:34:33 -0400366{
David Howells0c7d90c2006-08-22 20:06:10 -0400367 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howellsf7b422b2006-06-09 09:34:33 -0400368 unsigned char blockbits;
369 unsigned long blockres;
David Howells0c7d90c2006-08-22 20:06:10 -0400370 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
David Howellsf7b422b2006-06-09 09:34:33 -0400371 struct nfs_fattr fattr;
372 struct nfs_fsstat res = {
373 .fattr = &fattr,
374 };
375 int error;
376
377 lock_kernel();
378
David Howells8fa5c002006-08-22 20:06:12 -0400379 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
David Howellsf7b422b2006-06-09 09:34:33 -0400380 if (error < 0)
381 goto out_err;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700382 buf->f_type = NFS_SUPER_MAGIC;
David Howellsf7b422b2006-06-09 09:34:33 -0400383
384 /*
385 * Current versions of glibc do not correctly handle the
386 * case where f_frsize != f_bsize. Eventually we want to
387 * report the value of wtmult in this field.
388 */
David Howells0c7d90c2006-08-22 20:06:10 -0400389 buf->f_frsize = dentry->d_sb->s_blocksize;
David Howellsf7b422b2006-06-09 09:34:33 -0400390
391 /*
392 * On most *nix systems, f_blocks, f_bfree, and f_bavail
393 * are reported in units of f_frsize. Linux hasn't had
394 * an f_frsize field in its statfs struct until recently,
395 * thus historically Linux's sys_statfs reports these
396 * fields in units of f_bsize.
397 */
David Howells0c7d90c2006-08-22 20:06:10 -0400398 buf->f_bsize = dentry->d_sb->s_blocksize;
399 blockbits = dentry->d_sb->s_blocksize_bits;
David Howellsf7b422b2006-06-09 09:34:33 -0400400 blockres = (1 << blockbits) - 1;
401 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
402 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
403 buf->f_bavail = (res.abytes + blockres) >> blockbits;
404
405 buf->f_files = res.tfiles;
406 buf->f_ffree = res.afiles;
407
408 buf->f_namelen = server->namelen;
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700409
David Howellsf7b422b2006-06-09 09:34:33 -0400410 unlock_kernel();
411 return 0;
412
413 out_err:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700414 dprintk("%s: statfs error = %d\n", __func__, -error);
Amnon Aaronsohn1a0ba9a2007-04-09 22:05:26 -0700415 unlock_kernel();
416 return error;
David Howellsf7b422b2006-06-09 09:34:33 -0400417}
418
David Howells7d4e2742006-08-22 20:06:07 -0400419/*
420 * Map the security flavour number to a name
421 */
Trond Myklebust81039f12006-06-09 09:34:34 -0400422static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
423{
David Howells7d4e2742006-08-22 20:06:07 -0400424 static const struct {
Trond Myklebust81039f12006-06-09 09:34:34 -0400425 rpc_authflavor_t flavour;
426 const char *str;
427 } sec_flavours[] = {
428 { RPC_AUTH_NULL, "null" },
429 { RPC_AUTH_UNIX, "sys" },
430 { RPC_AUTH_GSS_KRB5, "krb5" },
431 { RPC_AUTH_GSS_KRB5I, "krb5i" },
432 { RPC_AUTH_GSS_KRB5P, "krb5p" },
433 { RPC_AUTH_GSS_LKEY, "lkey" },
434 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
435 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
436 { RPC_AUTH_GSS_SPKM, "spkm" },
437 { RPC_AUTH_GSS_SPKMI, "spkmi" },
438 { RPC_AUTH_GSS_SPKMP, "spkmp" },
Chuck Lever4d81cd12007-07-01 12:12:40 -0400439 { UINT_MAX, "unknown" }
Trond Myklebust81039f12006-06-09 09:34:34 -0400440 };
441 int i;
442
Chuck Lever4d81cd12007-07-01 12:12:40 -0400443 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
Trond Myklebust81039f12006-06-09 09:34:34 -0400444 if (sec_flavours[i].flavour == flavour)
445 break;
446 }
447 return sec_flavours[i].str;
448}
449
Chuck Lever82d101d2008-03-14 14:10:37 -0400450static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
451 int showdefaults)
452{
453 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
454
455 switch (sap->sa_family) {
456 case AF_INET: {
457 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
458 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
459 NIPQUAD(sin->sin_addr.s_addr));
460 break;
461 }
462 case AF_INET6: {
463 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
464 seq_printf(m, ",mountaddr=" NIP6_FMT,
465 NIP6(sin6->sin6_addr));
466 break;
467 }
468 default:
469 if (showdefaults)
470 seq_printf(m, ",mountaddr=unspecified");
471 }
472
473 if (nfss->mountd_version || showdefaults)
474 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
475 if (nfss->mountd_port || showdefaults)
476 seq_printf(m, ",mountport=%u", nfss->mountd_port);
477
478 switch (nfss->mountd_protocol) {
479 case IPPROTO_UDP:
480 seq_printf(m, ",mountproto=udp");
481 break;
482 case IPPROTO_TCP:
483 seq_printf(m, ",mountproto=tcp");
484 break;
485 default:
486 if (showdefaults)
487 seq_printf(m, ",mountproto=auto");
488 }
489}
490
David Howellsf7b422b2006-06-09 09:34:33 -0400491/*
492 * Describe the mount options in force on this server representation
493 */
Chuck Lever82d101d2008-03-14 14:10:37 -0400494static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
495 int showdefaults)
David Howellsf7b422b2006-06-09 09:34:33 -0400496{
David Howells509de812006-08-22 20:06:11 -0400497 static const struct proc_nfs_info {
David Howellsf7b422b2006-06-09 09:34:33 -0400498 int flag;
David Howells509de812006-08-22 20:06:11 -0400499 const char *str;
500 const char *nostr;
David Howellsf7b422b2006-06-09 09:34:33 -0400501 } nfs_info[] = {
502 { NFS_MOUNT_SOFT, ",soft", ",hard" },
Chuck Lever82d101d2008-03-14 14:10:37 -0400503 { NFS_MOUNT_INTR, ",intr", ",nointr" },
504 { NFS_MOUNT_POSIX, ",posix", "" },
David Howellsf7b422b2006-06-09 09:34:33 -0400505 { NFS_MOUNT_NOCTO, ",nocto", "" },
506 { NFS_MOUNT_NOAC, ",noac", "" },
507 { NFS_MOUNT_NONLM, ",nolock", "" },
508 { NFS_MOUNT_NOACL, ",noacl", "" },
Steve Dickson74dd34e2007-04-14 17:01:15 -0400509 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
Trond Myklebust75180df2007-05-16 16:53:28 -0400510 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
David Howellsf7b422b2006-06-09 09:34:33 -0400511 { 0, NULL, NULL }
512 };
David Howells509de812006-08-22 20:06:11 -0400513 const struct proc_nfs_info *nfs_infop;
David Howells8fa5c002006-08-22 20:06:12 -0400514 struct nfs_client *clp = nfss->nfs_client;
Chuck Lever82d101d2008-03-14 14:10:37 -0400515 u32 version = clp->rpc_ops->version;
David Howellsf7b422b2006-06-09 09:34:33 -0400516
Chuck Lever82d101d2008-03-14 14:10:37 -0400517 seq_printf(m, ",vers=%u", version);
Chuck Lever2d767432008-03-14 14:10:08 -0400518 seq_printf(m, ",rsize=%u", nfss->rsize);
519 seq_printf(m, ",wsize=%u", nfss->wsize);
Chuck Lever82d101d2008-03-14 14:10:37 -0400520 if (nfss->bsize != 0)
521 seq_printf(m, ",bsize=%u", nfss->bsize);
522 seq_printf(m, ",namlen=%u", nfss->namelen);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400523 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400524 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400525 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400526 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400527 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400528 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
Chuck Lever0e0cab72008-06-26 17:47:12 -0400529 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
Chuck Lever2d767432008-03-14 14:10:08 -0400530 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howellsf7b422b2006-06-09 09:34:33 -0400531 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
532 if (nfss->flags & nfs_infop->flag)
533 seq_puts(m, nfs_infop->str);
534 else
535 seq_puts(m, nfs_infop->nostr);
536 }
\"Talpey, Thomas\56928ed2007-09-10 13:48:47 -0400537 seq_printf(m, ",proto=%s",
538 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
Chuck Lever82d101d2008-03-14 14:10:37 -0400539 if (version == 4) {
540 if (nfss->port != NFS_PORT)
541 seq_printf(m, ",port=%u", nfss->port);
542 } else
543 if (nfss->port)
544 seq_printf(m, ",port=%u", nfss->port);
545
Trond Myklebust33170232007-12-20 16:03:59 -0500546 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
547 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
Trond Myklebust81039f12006-06-09 09:34:34 -0400548 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
Chuck Lever82d101d2008-03-14 14:10:37 -0400549
550 if (version != 4)
551 nfs_show_mountd_options(m, nfss, showdefaults);
552
553#ifdef CONFIG_NFS_V4
554 if (clp->rpc_ops->version == 4)
555 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
556#endif
David Howellsf7b422b2006-06-09 09:34:33 -0400557}
558
559/*
560 * Describe the mount options on this VFS mountpoint
561 */
562static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
563{
564 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
565
566 nfs_show_mount_options(m, nfss, 0);
567
Chuck Lever5d8515c2007-12-10 14:57:16 -0500568 seq_printf(m, ",addr=%s",
569 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
570 RPC_DISPLAY_ADDR));
David Howellsf7b422b2006-06-09 09:34:33 -0400571
572 return 0;
573}
574
575/*
576 * Present statistical information for this VFS mountpoint
577 */
578static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
579{
580 int i, cpu;
581 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
582 struct rpc_auth *auth = nfss->client->cl_auth;
583 struct nfs_iostats totals = { };
584
585 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
586
587 /*
588 * Display all mount option settings
589 */
590 seq_printf(m, "\n\topts:\t");
591 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
592 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
593 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
594 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
595 nfs_show_mount_options(m, nfss, 1);
596
597 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
598
599 seq_printf(m, "\n\tcaps:\t");
600 seq_printf(m, "caps=0x%x", nfss->caps);
Chuck Lever2d767432008-03-14 14:10:08 -0400601 seq_printf(m, ",wtmult=%u", nfss->wtmult);
602 seq_printf(m, ",dtsize=%u", nfss->dtsize);
603 seq_printf(m, ",bsize=%u", nfss->bsize);
604 seq_printf(m, ",namlen=%u", nfss->namelen);
David Howellsf7b422b2006-06-09 09:34:33 -0400605
606#ifdef CONFIG_NFS_V4
Trond Myklebust40c553192007-12-14 14:56:07 -0500607 if (nfss->nfs_client->rpc_ops->version == 4) {
David Howellsf7b422b2006-06-09 09:34:33 -0400608 seq_printf(m, "\n\tnfsv4:\t");
609 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
610 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
611 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
612 }
613#endif
614
615 /*
616 * Display security flavor in effect for this mount
617 */
Chuck Lever2d767432008-03-14 14:10:08 -0400618 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400619 if (auth->au_flavor)
Chuck Lever2d767432008-03-14 14:10:08 -0400620 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howellsf7b422b2006-06-09 09:34:33 -0400621
622 /*
623 * Display superblock I/O counters
624 */
625 for_each_possible_cpu(cpu) {
626 struct nfs_iostats *stats;
627
628 preempt_disable();
629 stats = per_cpu_ptr(nfss->io_stats, cpu);
630
631 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
632 totals.events[i] += stats->events[i];
633 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
634 totals.bytes[i] += stats->bytes[i];
635
636 preempt_enable();
637 }
638
639 seq_printf(m, "\n\tevents:\t");
640 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
641 seq_printf(m, "%lu ", totals.events[i]);
642 seq_printf(m, "\n\tbytes:\t");
643 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
644 seq_printf(m, "%Lu ", totals.bytes[i]);
645 seq_printf(m, "\n");
646
647 rpc_print_iostats(m, nfss->client);
648
649 return 0;
650}
651
652/*
653 * Begin unmount by attempting to remove all automounted mountpoints we added
David Howells54ceac42006-08-22 20:06:13 -0400654 * in response to xdev traversals and referrals
David Howellsf7b422b2006-06-09 09:34:33 -0400655 */
Al Viro42faad92008-04-24 07:21:56 -0400656static void nfs_umount_begin(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -0400657{
Al Viro42faad92008-04-24 07:21:56 -0400658 struct nfs_server *server = NFS_SB(sb);
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400659 struct rpc_clnt *rpc;
660
Trond Myklebustfc6ae3c2007-06-05 19:13:47 -0400661 /* -EIO all pending I/O */
662 rpc = server->client_acl;
663 if (!IS_ERR(rpc))
664 rpc_killall_tasks(rpc);
665 rpc = server->client;
666 if (!IS_ERR(rpc))
667 rpc_killall_tasks(rpc);
David Howellsf7b422b2006-06-09 09:34:33 -0400668}
669
670/*
Chuck Lever04dcd6e2007-12-10 14:57:53 -0500671 * Set the port number in an address. Be agnostic about the address family.
672 */
673static void nfs_set_port(struct sockaddr *sap, unsigned short port)
674{
675 switch (sap->sa_family) {
676 case AF_INET: {
677 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
678 ap->sin_port = htons(port);
679 break;
680 }
681 case AF_INET6: {
682 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
683 ap->sin6_port = htons(port);
684 break;
685 }
686 }
687}
688
689/*
Chuck Levercdcd7f92007-12-10 14:57:45 -0500690 * Sanity-check a server address provided by the mount command.
691 *
692 * Address family must be initialized, and address must not be
693 * the ANY address for that family.
Chuck Leverfc50d582007-07-01 12:12:46 -0400694 */
695static int nfs_verify_server_address(struct sockaddr *addr)
696{
697 switch (addr->sa_family) {
698 case AF_INET: {
Chuck Levercdcd7f92007-12-10 14:57:45 -0500699 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
Al Viroe6f1ceb2008-03-17 22:44:53 -0700700 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
Chuck Levercdcd7f92007-12-10 14:57:45 -0500701 }
702 case AF_INET6: {
703 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
704 return !ipv6_addr_any(sa);
Chuck Leverfc50d582007-07-01 12:12:46 -0400705 }
706 }
707
708 return 0;
709}
710
Chuck Leverce3b7e12008-06-23 12:36:53 -0400711static void nfs_parse_ipv4_address(char *string, size_t str_len,
712 struct sockaddr *sap, size_t *addr_len)
Chuck Lever9412b922007-12-10 14:59:21 -0500713{
Chuck Leverce3b7e12008-06-23 12:36:53 -0400714 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
715 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
Chuck Lever9412b922007-12-10 14:59:21 -0500716
Chuck Leverce3b7e12008-06-23 12:36:53 -0400717 if (str_len <= INET_ADDRSTRLEN) {
718 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
719 (int)str_len, string);
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500720
Chuck Leverce3b7e12008-06-23 12:36:53 -0400721 sin->sin_family = AF_INET;
722 *addr_len = sizeof(*sin);
723 if (in4_pton(string, str_len, addr, '\0', NULL))
Chuck Lever3c7c7e42007-12-10 14:59:35 -0500724 return;
725 }
Chuck Lever9412b922007-12-10 14:59:21 -0500726
727 sap->sa_family = AF_UNSPEC;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400728 *addr_len = 0;
729}
730
Chuck Leverd8e77482008-06-23 12:37:01 -0400731#define IPV6_SCOPE_DELIMITER '%'
732
Chuck Leverce3b7e12008-06-23 12:36:53 -0400733#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Chuck Leverd8e77482008-06-23 12:37:01 -0400734static void nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
735 const char *delim,
736 struct sockaddr_in6 *sin6)
737{
738 char *p;
739 size_t len;
740
741 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
742 return ;
743 if (*delim != IPV6_SCOPE_DELIMITER)
744 return;
745
746 len = (string + str_len) - delim - 1;
747 p = kstrndup(delim + 1, len, GFP_KERNEL);
748 if (p) {
749 unsigned long scope_id = 0;
750 struct net_device *dev;
751
752 dev = dev_get_by_name(&init_net, p);
753 if (dev != NULL) {
754 scope_id = dev->ifindex;
755 dev_put(dev);
756 } else {
757 /* scope_id is set to zero on error */
758 strict_strtoul(p, 10, &scope_id);
759 }
760
761 kfree(p);
762 sin6->sin6_scope_id = scope_id;
763 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
764 }
765}
766
Chuck Leverce3b7e12008-06-23 12:36:53 -0400767static void nfs_parse_ipv6_address(char *string, size_t str_len,
768 struct sockaddr *sap, size_t *addr_len)
769{
770 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
771 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
Chuck Leverd8e77482008-06-23 12:37:01 -0400772 const char *delim;
Chuck Leverce3b7e12008-06-23 12:36:53 -0400773
774 if (str_len <= INET6_ADDRSTRLEN) {
775 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
776 (int)str_len, string);
777
778 sin6->sin6_family = AF_INET6;
779 *addr_len = sizeof(*sin6);
Chuck Leverd8e77482008-06-23 12:37:01 -0400780 if (in6_pton(string, str_len, addr, IPV6_SCOPE_DELIMITER, &delim)) {
781 nfs_parse_ipv6_scope_id(string, str_len, delim, sin6);
Chuck Leverce3b7e12008-06-23 12:36:53 -0400782 return;
Chuck Leverd8e77482008-06-23 12:37:01 -0400783 }
Chuck Leverce3b7e12008-06-23 12:36:53 -0400784 }
785
786 sap->sa_family = AF_UNSPEC;
787 *addr_len = 0;
788}
789#else
790static void nfs_parse_ipv6_address(char *string, size_t str_len,
791 struct sockaddr *sap, size_t *addr_len)
792{
793 sap->sa_family = AF_UNSPEC;
794 *addr_len = 0;
795}
796#endif
797
798/*
799 * Construct a sockaddr based on the contents of a string that contains
800 * an IP address in presentation format.
801 *
802 * If there is a problem constructing the new sockaddr, set the address
803 * family to AF_UNSPEC.
804 */
805static void nfs_parse_ip_address(char *string, size_t str_len,
806 struct sockaddr *sap, size_t *addr_len)
807{
808 unsigned int i, colons;
809
810 colons = 0;
811 for (i = 0; i < str_len; i++)
812 if (string[i] == ':')
813 colons++;
814
815 if (colons >= 2)
816 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
817 else
818 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
Chuck Lever9412b922007-12-10 14:59:21 -0500819}
820
821/*
Trond Myklebust259875e2008-07-02 14:43:47 -0400822 * Sanity check the NFS transport protocol.
823 *
824 */
825static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
826{
827 switch (mnt->nfs_server.protocol) {
828 case XPRT_TRANSPORT_UDP:
829 case XPRT_TRANSPORT_TCP:
830 case XPRT_TRANSPORT_RDMA:
831 break;
832 default:
833 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
834 }
835}
836
837/*
838 * For text based NFSv2/v3 mounts, the mount protocol transport default
839 * settings should depend upon the specified NFS transport.
840 */
841static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
842{
843 nfs_validate_transport_protocol(mnt);
844
845 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
846 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
847 return;
848 switch (mnt->nfs_server.protocol) {
849 case XPRT_TRANSPORT_UDP:
850 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
851 break;
852 case XPRT_TRANSPORT_TCP:
853 case XPRT_TRANSPORT_RDMA:
854 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
855 }
856}
857
858/*
Chuck Lever01060c82008-06-24 16:33:38 -0400859 * Parse the value of the 'sec=' option.
860 *
Chuck Leverdd07c942008-06-24 16:33:46 -0400861 * The flavor_len setting is for v4 mounts.
Chuck Lever01060c82008-06-24 16:33:38 -0400862 */
863static int nfs_parse_security_flavors(char *value,
864 struct nfs_parsed_mount_data *mnt)
865{
866 substring_t args[MAX_OPT_ARGS];
867
868 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
869
870 switch (match_token(value, nfs_secflavor_tokens, args)) {
871 case Opt_sec_none:
Chuck Lever01060c82008-06-24 16:33:38 -0400872 mnt->auth_flavor_len = 0;
873 mnt->auth_flavors[0] = RPC_AUTH_NULL;
874 break;
875 case Opt_sec_sys:
Chuck Lever01060c82008-06-24 16:33:38 -0400876 mnt->auth_flavor_len = 0;
877 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
878 break;
879 case Opt_sec_krb5:
Chuck Lever01060c82008-06-24 16:33:38 -0400880 mnt->auth_flavor_len = 1;
881 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
882 break;
883 case Opt_sec_krb5i:
Chuck Lever01060c82008-06-24 16:33:38 -0400884 mnt->auth_flavor_len = 1;
885 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
886 break;
887 case Opt_sec_krb5p:
Chuck Lever01060c82008-06-24 16:33:38 -0400888 mnt->auth_flavor_len = 1;
889 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
890 break;
891 case Opt_sec_lkey:
Chuck Lever01060c82008-06-24 16:33:38 -0400892 mnt->auth_flavor_len = 1;
893 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
894 break;
895 case Opt_sec_lkeyi:
Chuck Lever01060c82008-06-24 16:33:38 -0400896 mnt->auth_flavor_len = 1;
897 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
898 break;
899 case Opt_sec_lkeyp:
Chuck Lever01060c82008-06-24 16:33:38 -0400900 mnt->auth_flavor_len = 1;
901 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
902 break;
903 case Opt_sec_spkm:
Chuck Lever01060c82008-06-24 16:33:38 -0400904 mnt->auth_flavor_len = 1;
905 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
906 break;
907 case Opt_sec_spkmi:
Chuck Lever01060c82008-06-24 16:33:38 -0400908 mnt->auth_flavor_len = 1;
909 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
910 break;
911 case Opt_sec_spkmp:
Chuck Lever01060c82008-06-24 16:33:38 -0400912 mnt->auth_flavor_len = 1;
913 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
914 break;
915 default:
916 return 0;
917 }
918
919 return 1;
920}
921
Chuck Leverf45663c2008-06-24 19:28:02 -0400922static void nfs_parse_invalid_value(const char *option)
923{
924 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
925}
926
Chuck Lever01060c82008-06-24 16:33:38 -0400927/*
Chuck Leverbf0fd762007-07-01 12:13:44 -0400928 * Error-check and convert a string of mount options from user space into
Chuck Leverf45663c2008-06-24 19:28:02 -0400929 * a data structure. The whole mount string is processed; bad options are
930 * skipped as they are encountered. If there were no errors, return 1;
931 * otherwise return 0 (zero).
Chuck Leverbf0fd762007-07-01 12:13:44 -0400932 */
933static int nfs_parse_mount_options(char *raw,
934 struct nfs_parsed_mount_data *mnt)
935{
Eric Parisf9c3a382008-03-05 14:20:18 -0500936 char *p, *string, *secdata;
Chuck Leverf45663c2008-06-24 19:28:02 -0400937 int rc, sloppy = 0, errors = 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400938
939 if (!raw) {
940 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
941 return 1;
942 }
943 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
944
Eric Parisf9c3a382008-03-05 14:20:18 -0500945 secdata = alloc_secdata();
946 if (!secdata)
947 goto out_nomem;
948
949 rc = security_sb_copy_data(raw, secdata);
950 if (rc)
951 goto out_security_failure;
952
953 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
954 if (rc)
955 goto out_security_failure;
956
957 free_secdata(secdata);
958
Chuck Leverbf0fd762007-07-01 12:13:44 -0400959 while ((p = strsep(&raw, ",")) != NULL) {
960 substring_t args[MAX_OPT_ARGS];
961 int option, token;
962
963 if (!*p)
964 continue;
965
966 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
967
968 token = match_token(p, nfs_mount_option_tokens, args);
969 switch (token) {
Chuck Leverf45663c2008-06-24 19:28:02 -0400970
971 /*
972 * boolean options: foo/nofoo
973 */
Chuck Leverbf0fd762007-07-01 12:13:44 -0400974 case Opt_soft:
975 mnt->flags |= NFS_MOUNT_SOFT;
976 break;
977 case Opt_hard:
978 mnt->flags &= ~NFS_MOUNT_SOFT;
979 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -0400980 case Opt_posix:
981 mnt->flags |= NFS_MOUNT_POSIX;
982 break;
983 case Opt_noposix:
984 mnt->flags &= ~NFS_MOUNT_POSIX;
985 break;
986 case Opt_cto:
987 mnt->flags &= ~NFS_MOUNT_NOCTO;
988 break;
989 case Opt_nocto:
990 mnt->flags |= NFS_MOUNT_NOCTO;
991 break;
992 case Opt_ac:
993 mnt->flags &= ~NFS_MOUNT_NOAC;
994 break;
995 case Opt_noac:
996 mnt->flags |= NFS_MOUNT_NOAC;
997 break;
998 case Opt_lock:
999 mnt->flags &= ~NFS_MOUNT_NONLM;
1000 break;
1001 case Opt_nolock:
1002 mnt->flags |= NFS_MOUNT_NONLM;
1003 break;
1004 case Opt_v2:
1005 mnt->flags &= ~NFS_MOUNT_VER3;
1006 break;
1007 case Opt_v3:
1008 mnt->flags |= NFS_MOUNT_VER3;
1009 break;
1010 case Opt_udp:
1011 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001012 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001013 break;
1014 case Opt_tcp:
1015 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001016 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001017 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001018 case Opt_rdma:
1019 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1020 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001021 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001022 case Opt_acl:
1023 mnt->flags &= ~NFS_MOUNT_NOACL;
1024 break;
1025 case Opt_noacl:
1026 mnt->flags |= NFS_MOUNT_NOACL;
1027 break;
1028 case Opt_rdirplus:
1029 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1030 break;
1031 case Opt_nordirplus:
1032 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1033 break;
Trond Myklebust75180df2007-05-16 16:53:28 -04001034 case Opt_sharecache:
1035 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1036 break;
1037 case Opt_nosharecache:
1038 mnt->flags |= NFS_MOUNT_UNSHARED;
1039 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001040
Chuck Leverf45663c2008-06-24 19:28:02 -04001041 /*
1042 * options that take numeric values
1043 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001044 case Opt_port:
Chuck Leverf45663c2008-06-24 19:28:02 -04001045 if (match_int(args, &option) ||
1046 option < 0 || option > USHORT_MAX) {
1047 errors++;
1048 nfs_parse_invalid_value("port");
1049 } else
1050 mnt->nfs_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001051 break;
1052 case Opt_rsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001053 if (match_int(args, &option) || option < 0) {
1054 errors++;
1055 nfs_parse_invalid_value("rsize");
1056 } else
1057 mnt->rsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001058 break;
1059 case Opt_wsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001060 if (match_int(args, &option) || option < 0) {
1061 errors++;
1062 nfs_parse_invalid_value("wsize");
1063 } else
1064 mnt->wsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001065 break;
1066 case Opt_bsize:
Chuck Leverf45663c2008-06-24 19:28:02 -04001067 if (match_int(args, &option) || option < 0) {
1068 errors++;
1069 nfs_parse_invalid_value("bsize");
1070 } else
1071 mnt->bsize = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001072 break;
1073 case Opt_timeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001074 if (match_int(args, &option) || option <= 0) {
1075 errors++;
1076 nfs_parse_invalid_value("timeo");
1077 } else
1078 mnt->timeo = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001079 break;
1080 case Opt_retrans:
Chuck Leverf45663c2008-06-24 19:28:02 -04001081 if (match_int(args, &option) || option <= 0) {
1082 errors++;
1083 nfs_parse_invalid_value("retrans");
1084 } else
1085 mnt->retrans = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001086 break;
1087 case Opt_acregmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001088 if (match_int(args, &option) || option < 0) {
1089 errors++;
1090 nfs_parse_invalid_value("acregmin");
1091 } else
1092 mnt->acregmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001093 break;
1094 case Opt_acregmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001095 if (match_int(args, &option) || option < 0) {
1096 errors++;
1097 nfs_parse_invalid_value("acregmax");
1098 } else
1099 mnt->acregmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001100 break;
1101 case Opt_acdirmin:
Chuck Leverf45663c2008-06-24 19:28:02 -04001102 if (match_int(args, &option) || option < 0) {
1103 errors++;
1104 nfs_parse_invalid_value("acdirmin");
1105 } else
1106 mnt->acdirmin = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001107 break;
1108 case Opt_acdirmax:
Chuck Leverf45663c2008-06-24 19:28:02 -04001109 if (match_int(args, &option) || option < 0) {
1110 errors++;
1111 nfs_parse_invalid_value("acdirmax");
1112 } else
1113 mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001114 break;
1115 case Opt_actimeo:
Chuck Leverf45663c2008-06-24 19:28:02 -04001116 if (match_int(args, &option) || option < 0) {
1117 errors++;
1118 nfs_parse_invalid_value("actimeo");
1119 } else
1120 mnt->acregmin = mnt->acregmax =
1121 mnt->acdirmin = mnt->acdirmax = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001122 break;
1123 case Opt_namelen:
Chuck Leverf45663c2008-06-24 19:28:02 -04001124 if (match_int(args, &option) || option < 0) {
1125 errors++;
1126 nfs_parse_invalid_value("namlen");
1127 } else
1128 mnt->namlen = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001129 break;
1130 case Opt_mountport:
Chuck Leverf45663c2008-06-24 19:28:02 -04001131 if (match_int(args, &option) ||
1132 option < 0 || option > USHORT_MAX) {
1133 errors++;
1134 nfs_parse_invalid_value("mountport");
1135 } else
1136 mnt->mount_server.port = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001137 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001138 case Opt_mountvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001139 if (match_int(args, &option) ||
1140 option < NFS_MNT_VERSION ||
1141 option > NFS_MNT3_VERSION) {
1142 errors++;
1143 nfs_parse_invalid_value("mountvers");
1144 } else
1145 mnt->mount_server.version = option;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001146 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001147 case Opt_nfsvers:
Chuck Leverf45663c2008-06-24 19:28:02 -04001148 if (match_int(args, &option)) {
1149 errors++;
1150 nfs_parse_invalid_value("nfsvers");
1151 break;
1152 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001153 switch (option) {
Chuck Leverf45663c2008-06-24 19:28:02 -04001154 case NFS2_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001155 mnt->flags &= ~NFS_MOUNT_VER3;
1156 break;
Chuck Leverf45663c2008-06-24 19:28:02 -04001157 case NFS3_VERSION:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001158 mnt->flags |= NFS_MOUNT_VER3;
1159 break;
1160 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001161 errors++;
1162 nfs_parse_invalid_value("nfsvers");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001163 }
1164 break;
1165
Chuck Leverf45663c2008-06-24 19:28:02 -04001166 /*
1167 * options that take text values
1168 */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001169 case Opt_sec:
1170 string = match_strdup(args);
1171 if (string == NULL)
1172 goto out_nomem;
Chuck Lever01060c82008-06-24 16:33:38 -04001173 rc = nfs_parse_security_flavors(string, mnt);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001174 kfree(string);
Chuck Leverf45663c2008-06-24 19:28:02 -04001175 if (!rc) {
1176 errors++;
1177 dfprintk(MOUNT, "NFS: unrecognized "
1178 "security flavor\n");
1179 }
Chuck Leverbf0fd762007-07-01 12:13:44 -04001180 break;
1181 case Opt_proto:
1182 string = match_strdup(args);
1183 if (string == NULL)
1184 goto out_nomem;
1185 token = match_token(string,
1186 nfs_xprt_protocol_tokens, args);
1187 kfree(string);
1188
1189 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001190 case Opt_xprt_udp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001191 mnt->flags &= ~NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001192 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001193 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001194 case Opt_xprt_tcp:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001195 mnt->flags |= NFS_MOUNT_TCP;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001196 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001197 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001198 case Opt_xprt_rdma:
1199 /* vector side protocols to TCP */
1200 mnt->flags |= NFS_MOUNT_TCP;
1201 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001202 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001203 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001204 errors++;
1205 dfprintk(MOUNT, "NFS: unrecognized "
1206 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001207 }
1208 break;
1209 case Opt_mountproto:
1210 string = match_strdup(args);
1211 if (string == NULL)
1212 goto out_nomem;
1213 token = match_token(string,
1214 nfs_xprt_protocol_tokens, args);
1215 kfree(string);
1216
1217 switch (token) {
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001218 case Opt_xprt_udp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001219 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001220 break;
Chuck Leverfdb66ff2007-08-29 17:58:57 -04001221 case Opt_xprt_tcp:
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001222 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001223 break;
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -04001224 case Opt_xprt_rdma: /* not used for side protocols */
Chuck Leverbf0fd762007-07-01 12:13:44 -04001225 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001226 errors++;
1227 dfprintk(MOUNT, "NFS: unrecognized "
1228 "transport protocol\n");
Chuck Leverbf0fd762007-07-01 12:13:44 -04001229 }
1230 break;
1231 case Opt_addr:
1232 string = match_strdup(args);
1233 if (string == NULL)
1234 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001235 nfs_parse_ip_address(string, strlen(string),
1236 (struct sockaddr *)
1237 &mnt->nfs_server.address,
1238 &mnt->nfs_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001239 kfree(string);
1240 break;
1241 case Opt_clientaddr:
1242 string = match_strdup(args);
1243 if (string == NULL)
1244 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001245 kfree(mnt->client_address);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001246 mnt->client_address = string;
1247 break;
Chuck Lever33832032007-12-10 14:59:13 -05001248 case Opt_mounthost:
1249 string = match_strdup(args);
1250 if (string == NULL)
1251 goto out_nomem;
Chuck Leverfc601472008-01-16 16:38:10 -05001252 kfree(mnt->mount_server.hostname);
Chuck Lever33832032007-12-10 14:59:13 -05001253 mnt->mount_server.hostname = string;
1254 break;
Chuck Lever0ac83772007-09-11 18:01:04 -04001255 case Opt_mountaddr:
Chuck Leverbf0fd762007-07-01 12:13:44 -04001256 string = match_strdup(args);
1257 if (string == NULL)
1258 goto out_nomem;
Chuck Leverce3b7e12008-06-23 12:36:53 -04001259 nfs_parse_ip_address(string, strlen(string),
1260 (struct sockaddr *)
1261 &mnt->mount_server.address,
1262 &mnt->mount_server.addrlen);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001263 kfree(string);
1264 break;
1265
Chuck Leverf45663c2008-06-24 19:28:02 -04001266 /*
1267 * Special options
1268 */
1269 case Opt_sloppy:
1270 sloppy = 1;
1271 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1272 break;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001273 case Opt_userspace:
1274 case Opt_deprecated:
Chuck Leverd33e4df2008-06-12 12:37:41 -04001275 dfprintk(MOUNT, "NFS: ignoring mount option "
1276 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001277 break;
1278
1279 default:
Chuck Leverf45663c2008-06-24 19:28:02 -04001280 errors++;
1281 dfprintk(MOUNT, "NFS: unrecognized mount option "
1282 "'%s'\n", p);
Chuck Leverbf0fd762007-07-01 12:13:44 -04001283 }
1284 }
1285
1286 return 1;
1287
1288out_nomem:
1289 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1290 return 0;
Eric Parisf9c3a382008-03-05 14:20:18 -05001291out_security_failure:
1292 free_secdata(secdata);
1293 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1294 return 0;
Chuck Leverbf0fd762007-07-01 12:13:44 -04001295}
1296
1297/*
Chuck Lever0076d7b2007-07-01 12:13:49 -04001298 * Use the remote server's MOUNT service to request the NFS file handle
1299 * corresponding to the provided path.
1300 */
1301static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1302 struct nfs_fh *root_fh)
1303{
Chuck Lever4c568012007-12-10 14:59:28 -05001304 struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
Chuck Lever33832032007-12-10 14:59:13 -05001305 char *hostname;
Chuck Lever4c568012007-12-10 14:59:28 -05001306 int status;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001307
1308 if (args->mount_server.version == 0) {
1309 if (args->flags & NFS_MOUNT_VER3)
1310 args->mount_server.version = NFS_MNT3_VERSION;
1311 else
1312 args->mount_server.version = NFS_MNT_VERSION;
1313 }
1314
Chuck Lever33832032007-12-10 14:59:13 -05001315 if (args->mount_server.hostname)
1316 hostname = args->mount_server.hostname;
1317 else
1318 hostname = args->nfs_server.hostname;
1319
Chuck Lever0076d7b2007-07-01 12:13:49 -04001320 /*
1321 * Construct the mount server's address.
1322 */
Chuck Lever4c568012007-12-10 14:59:28 -05001323 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1324 memcpy(sap, &args->nfs_server.address,
1325 args->nfs_server.addrlen);
1326 args->mount_server.addrlen = args->nfs_server.addrlen;
1327 }
1328
James Lentiniaad70002007-09-24 17:32:49 -04001329 /*
1330 * autobind will be used if mount_server.port == 0
1331 */
Chuck Lever4c568012007-12-10 14:59:28 -05001332 nfs_set_port(sap, args->mount_server.port);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001333
1334 /*
1335 * Now ask the mount server to map our export path
1336 * to a file handle.
1337 */
Chuck Lever4c568012007-12-10 14:59:28 -05001338 status = nfs_mount(sap,
1339 args->mount_server.addrlen,
Chuck Lever33832032007-12-10 14:59:13 -05001340 hostname,
Chuck Lever0076d7b2007-07-01 12:13:49 -04001341 args->nfs_server.export_path,
1342 args->mount_server.version,
1343 args->mount_server.protocol,
1344 root_fh);
Chuck Leverefd83402007-09-11 18:00:58 -04001345 if (status == 0)
1346 return 0;
Chuck Lever0076d7b2007-07-01 12:13:49 -04001347
Chuck Lever396cee972008-06-12 12:37:49 -04001348 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
Chuck Lever33832032007-12-10 14:59:13 -05001349 hostname, status);
Chuck Lever0076d7b2007-07-01 12:13:49 -04001350 return status;
1351}
1352
Chuck Leverd1aa0822008-06-23 12:36:45 -04001353static int nfs_parse_simple_hostname(const char *dev_name,
1354 char **hostname, size_t maxnamlen,
1355 char **export_path, size_t maxpathlen)
Chuck Leverdc045892008-06-23 12:36:37 -04001356{
1357 size_t len;
1358 char *colon, *comma;
1359
1360 colon = strchr(dev_name, ':');
1361 if (colon == NULL)
1362 goto out_bad_devname;
1363
1364 len = colon - dev_name;
1365 if (len > maxnamlen)
1366 goto out_hostname;
1367
1368 /* N.B. caller will free nfs_server.hostname in all cases */
1369 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1370 if (!*hostname)
1371 goto out_nomem;
1372
1373 /* kill possible hostname list: not supported */
1374 comma = strchr(*hostname, ',');
1375 if (comma != NULL) {
1376 if (comma == *hostname)
1377 goto out_bad_devname;
1378 *comma = '\0';
1379 }
1380
1381 colon++;
1382 len = strlen(colon);
1383 if (len > maxpathlen)
1384 goto out_path;
1385 *export_path = kstrndup(colon, len, GFP_KERNEL);
1386 if (!*export_path)
1387 goto out_nomem;
1388
1389 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1390 return 0;
1391
1392out_bad_devname:
1393 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1394 return -EINVAL;
1395
1396out_nomem:
1397 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1398 return -ENOMEM;
1399
1400out_hostname:
1401 dfprintk(MOUNT, "NFS: server hostname too long\n");
1402 return -ENAMETOOLONG;
1403
1404out_path:
1405 dfprintk(MOUNT, "NFS: export pathname too long\n");
1406 return -ENAMETOOLONG;
1407}
1408
1409/*
Chuck Leverd1aa0822008-06-23 12:36:45 -04001410 * Hostname has square brackets around it because it contains one or
1411 * more colons. We look for the first closing square bracket, and a
1412 * colon must follow it.
1413 */
1414static int nfs_parse_protected_hostname(const char *dev_name,
1415 char **hostname, size_t maxnamlen,
1416 char **export_path, size_t maxpathlen)
1417{
1418 size_t len;
1419 char *start, *end;
1420
1421 start = (char *)(dev_name + 1);
1422
1423 end = strchr(start, ']');
1424 if (end == NULL)
1425 goto out_bad_devname;
1426 if (*(end + 1) != ':')
1427 goto out_bad_devname;
1428
1429 len = end - start;
1430 if (len > maxnamlen)
1431 goto out_hostname;
1432
1433 /* N.B. caller will free nfs_server.hostname in all cases */
1434 *hostname = kstrndup(start, len, GFP_KERNEL);
1435 if (*hostname == NULL)
1436 goto out_nomem;
1437
1438 end += 2;
1439 len = strlen(end);
1440 if (len > maxpathlen)
1441 goto out_path;
1442 *export_path = kstrndup(end, len, GFP_KERNEL);
1443 if (!*export_path)
1444 goto out_nomem;
1445
1446 return 0;
1447
1448out_bad_devname:
1449 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1450 return -EINVAL;
1451
1452out_nomem:
1453 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1454 return -ENOMEM;
1455
1456out_hostname:
1457 dfprintk(MOUNT, "NFS: server hostname too long\n");
1458 return -ENAMETOOLONG;
1459
1460out_path:
1461 dfprintk(MOUNT, "NFS: export pathname too long\n");
1462 return -ENAMETOOLONG;
1463}
1464
1465/*
1466 * Split "dev_name" into "hostname:export_path".
1467 *
1468 * The leftmost colon demarks the split between the server's hostname
1469 * and the export path. If the hostname starts with a left square
1470 * bracket, then it may contain colons.
1471 *
1472 * Note: caller frees hostname and export path, even on error.
1473 */
1474static int nfs_parse_devname(const char *dev_name,
1475 char **hostname, size_t maxnamlen,
1476 char **export_path, size_t maxpathlen)
1477{
1478 if (*dev_name == '[')
1479 return nfs_parse_protected_hostname(dev_name,
1480 hostname, maxnamlen,
1481 export_path, maxpathlen);
1482
1483 return nfs_parse_simple_hostname(dev_name,
1484 hostname, maxnamlen,
1485 export_path, maxpathlen);
1486}
1487
1488/*
David Howells54ceac42006-08-22 20:06:13 -04001489 * Validate the NFS2/NFS3 mount data
1490 * - fills in the mount root filehandle
Chuck Lever136d5582007-07-01 12:13:54 -04001491 *
1492 * For option strings, user space handles the following behaviors:
1493 *
1494 * + DNS: mapping server host name to IP address ("addr=" option)
1495 *
1496 * + failure mode: how to behave if a mount request can't be handled
1497 * immediately ("fg/bg" option)
1498 *
1499 * + retry: how often to retry a mount request ("retry=" option)
1500 *
1501 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1502 * mountproto=tcp after mountproto=udp, and so on
David Howellsf7b422b2006-06-09 09:34:33 -04001503 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001504static int nfs_validate_mount_data(void *options,
1505 struct nfs_parsed_mount_data *args,
Chuck Lever136d5582007-07-01 12:13:54 -04001506 struct nfs_fh *mntfh,
1507 const char *dev_name)
David Howellsf7b422b2006-06-09 09:34:33 -04001508{
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001509 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
Chuck Lever136d5582007-07-01 12:13:54 -04001510
Chuck Lever5df36e72007-07-01 12:12:56 -04001511 if (data == NULL)
1512 goto out_no_data;
David Howells54ceac42006-08-22 20:06:13 -04001513
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001514 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1515 args->rsize = NFS_MAX_FILE_IO_SIZE;
1516 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04001517 args->acregmin = NFS_DEF_ACREGMIN;
1518 args->acregmax = NFS_DEF_ACREGMAX;
1519 args->acdirmin = NFS_DEF_ACDIRMIN;
1520 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04001521 args->mount_server.port = 0; /* autobind unless user sets port */
Chuck Leverf22d6d72008-03-14 14:10:22 -04001522 args->nfs_server.port = 0; /* autobind unless user sets port */
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001523 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
Chuck Leverdd07c942008-06-24 16:33:46 -04001524 args->auth_flavors[0] = RPC_AUTH_UNIX;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001525
David Howellsf7b422b2006-06-09 09:34:33 -04001526 switch (data->version) {
Chuck Lever5df36e72007-07-01 12:12:56 -04001527 case 1:
1528 data->namlen = 0;
1529 case 2:
1530 data->bsize = 0;
1531 case 3:
1532 if (data->flags & NFS_MOUNT_VER3)
1533 goto out_no_v3;
1534 data->root.size = NFS2_FHSIZE;
1535 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1536 case 4:
1537 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1538 goto out_no_sec;
1539 case 5:
1540 memset(data->context, 0, sizeof(data->context));
1541 case 6:
Trond Myklebustb7e24452008-06-19 15:21:11 -04001542 if (data->flags & NFS_MOUNT_VER3) {
1543 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1544 goto out_invalid_fh;
Chuck Lever5df36e72007-07-01 12:12:56 -04001545 mntfh->size = data->root.size;
Trond Myklebustb7e24452008-06-19 15:21:11 -04001546 } else
Chuck Lever5df36e72007-07-01 12:12:56 -04001547 mntfh->size = NFS2_FHSIZE;
1548
Chuck Lever5df36e72007-07-01 12:12:56 -04001549
1550 memcpy(mntfh->data, data->root.data, mntfh->size);
1551 if (mntfh->size < sizeof(mntfh->data))
1552 memset(mntfh->data + mntfh->size, 0,
1553 sizeof(mntfh->data) - mntfh->size);
Chuck Lever6e88e062007-09-24 15:39:50 -04001554
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001555 /*
1556 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1557 * can deal with.
1558 */
1559 args->flags = data->flags;
1560 args->rsize = data->rsize;
1561 args->wsize = data->wsize;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001562 args->timeo = data->timeo;
1563 args->retrans = data->retrans;
1564 args->acregmin = data->acregmin;
1565 args->acregmax = data->acregmax;
1566 args->acdirmin = data->acdirmin;
1567 args->acdirmax = data->acdirmax;
Chuck Lever4c568012007-12-10 14:59:28 -05001568
1569 memcpy(&args->nfs_server.address, &data->addr,
1570 sizeof(data->addr));
1571 args->nfs_server.addrlen = sizeof(data->addr);
1572 if (!nfs_verify_server_address((struct sockaddr *)
1573 &args->nfs_server.address))
1574 goto out_no_address;
1575
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001576 if (!(data->flags & NFS_MOUNT_TCP))
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -04001577 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001578 /* N.B. caller will free nfs_server.hostname in all cases */
1579 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1580 args->namlen = data->namlen;
1581 args->bsize = data->bsize;
Chuck Leverdd07c942008-06-24 16:33:46 -04001582
1583 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1584 args->auth_flavors[0] = data->pseudoflavor;
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001585 if (!args->nfs_server.hostname)
1586 goto out_nomem;
Eric Parisf9c3a382008-03-05 14:20:18 -05001587
1588 /*
1589 * The legacy version 6 binary mount data from userspace has a
1590 * field used only to transport selinux information into the
1591 * the kernel. To continue to support that functionality we
1592 * have a touch of selinux knowledge here in the NFS code. The
1593 * userspace code converted context=blah to just blah so we are
1594 * converting back to the full string selinux understands.
1595 */
1596 if (data->context[0]){
1597#ifdef CONFIG_SECURITY_SELINUX
1598 int rc;
1599 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1600 if (!opts_str)
1601 return -ENOMEM;
1602 strcpy(opts_str, "context=");
1603 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1604 strcat(opts_str, &data->context[0]);
1605 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1606 kfree(opts_str);
1607 if (rc)
1608 return rc;
1609#else
1610 return -EINVAL;
1611#endif
1612 }
1613
Chuck Lever5df36e72007-07-01 12:12:56 -04001614 break;
Chuck Lever136d5582007-07-01 12:13:54 -04001615 default: {
Chuck Lever136d5582007-07-01 12:13:54 -04001616 int status;
Chuck Lever136d5582007-07-01 12:13:54 -04001617
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001618 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever136d5582007-07-01 12:13:54 -04001619 return -EINVAL;
1620
Chuck Lever6e88e062007-09-24 15:39:50 -04001621 if (!nfs_verify_server_address((struct sockaddr *)
1622 &args->nfs_server.address))
1623 goto out_no_address;
1624
Chuck Levered596a82008-06-26 17:47:05 -04001625 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1626 args->nfs_server.port);
1627
Trond Myklebust259875e2008-07-02 14:43:47 -04001628 nfs_set_mount_transport_protocol(args);
1629
Chuck Leverdc045892008-06-23 12:36:37 -04001630 status = nfs_parse_devname(dev_name,
1631 &args->nfs_server.hostname,
1632 PAGE_SIZE,
1633 &args->nfs_server.export_path,
1634 NFS_MAXPATHLEN);
1635 if (!status)
1636 status = nfs_try_mount(args, mntfh);
Chuck Lever136d5582007-07-01 12:13:54 -04001637
Chuck Leverdc045892008-06-23 12:36:37 -04001638 kfree(args->nfs_server.export_path);
1639 args->nfs_server.export_path = NULL;
Chuck Lever136d5582007-07-01 12:13:54 -04001640
Chuck Lever136d5582007-07-01 12:13:54 -04001641 if (status)
Chuck Leverfdc6e2c2007-08-29 17:58:59 -04001642 return status;
Chuck Lever136d5582007-07-01 12:13:54 -04001643
Chuck Lever136d5582007-07-01 12:13:54 -04001644 break;
1645 }
David Howellsf7b422b2006-06-09 09:34:33 -04001646 }
David Howells54ceac42006-08-22 20:06:13 -04001647
David Howellsf7b422b2006-06-09 09:34:33 -04001648#ifndef CONFIG_NFS_V3
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001649 if (args->flags & NFS_MOUNT_VER3)
Chuck Lever5df36e72007-07-01 12:12:56 -04001650 goto out_v3_not_compiled;
1651#endif /* !CONFIG_NFS_V3 */
David Howells54ceac42006-08-22 20:06:13 -04001652
David Howells54ceac42006-08-22 20:06:13 -04001653 return 0;
Chuck Lever5df36e72007-07-01 12:12:56 -04001654
1655out_no_data:
1656 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1657 return -EINVAL;
1658
1659out_no_v3:
1660 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1661 data->version);
1662 return -EINVAL;
1663
1664out_no_sec:
1665 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1666 return -EINVAL;
1667
Chuck Lever5df36e72007-07-01 12:12:56 -04001668#ifndef CONFIG_NFS_V3
1669out_v3_not_compiled:
1670 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1671 return -EPROTONOSUPPORT;
1672#endif /* !CONFIG_NFS_V3 */
1673
Cyrill Gorcunov63649bd2008-04-17 20:42:09 +04001674out_nomem:
1675 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1676 return -ENOMEM;
1677
Chuck Lever5df36e72007-07-01 12:12:56 -04001678out_no_address:
1679 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1680 return -EINVAL;
1681
1682out_invalid_fh:
1683 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1684 return -EINVAL;
David Howells54ceac42006-08-22 20:06:13 -04001685}
1686
Jeff Layton48b605f2008-06-10 15:38:39 -04001687static int
1688nfs_compare_remount_data(struct nfs_server *nfss,
1689 struct nfs_parsed_mount_data *data)
1690{
1691 if (data->flags != nfss->flags ||
1692 data->rsize != nfss->rsize ||
1693 data->wsize != nfss->wsize ||
1694 data->retrans != nfss->client->cl_timeout->to_retries ||
1695 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1696 data->acregmin != nfss->acregmin / HZ ||
1697 data->acregmax != nfss->acregmax / HZ ||
1698 data->acdirmin != nfss->acdirmin / HZ ||
1699 data->acdirmax != nfss->acdirmax / HZ ||
1700 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1701 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1702 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1703 data->nfs_server.addrlen) != 0)
1704 return -EINVAL;
1705
1706 return 0;
1707}
1708
1709static int
1710nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1711{
1712 int error;
1713 struct nfs_server *nfss = sb->s_fs_info;
1714 struct nfs_parsed_mount_data *data;
1715 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1716 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
Trond Myklebustcd100722008-06-17 16:12:00 -04001717 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
Jeff Layton48b605f2008-06-10 15:38:39 -04001718
1719 /*
1720 * Userspace mount programs that send binary options generally send
1721 * them populated with default values. We have no way to know which
1722 * ones were explicitly specified. Fall back to legacy behavior and
1723 * just return success.
1724 */
Trond Myklebustcd100722008-06-17 16:12:00 -04001725 if ((nfsvers == 4 && options4->version == 1) ||
1726 (nfsvers <= 3 && options->version >= 1 &&
Jeff Layton48b605f2008-06-10 15:38:39 -04001727 options->version <= 6))
1728 return 0;
1729
1730 data = kzalloc(sizeof(*data), GFP_KERNEL);
1731 if (data == NULL)
1732 return -ENOMEM;
1733
1734 /* fill out struct with values from existing mount */
1735 data->flags = nfss->flags;
1736 data->rsize = nfss->rsize;
1737 data->wsize = nfss->wsize;
1738 data->retrans = nfss->client->cl_timeout->to_retries;
1739 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1740 data->acregmin = nfss->acregmin / HZ;
1741 data->acregmax = nfss->acregmax / HZ;
1742 data->acdirmin = nfss->acdirmin / HZ;
1743 data->acdirmax = nfss->acdirmax / HZ;
1744 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1745 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1746 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1747 data->nfs_server.addrlen);
1748
1749 /* overwrite those values with any that were specified */
1750 error = nfs_parse_mount_options((char *)options, data);
1751 if (error < 0)
1752 goto out;
1753
1754 /* compare new mount options with old ones */
1755 error = nfs_compare_remount_data(nfss, data);
1756out:
1757 kfree(data);
1758 return error;
1759}
1760
David Howells54ceac42006-08-22 20:06:13 -04001761/*
1762 * Initialise the common bits of the superblock
1763 */
1764static inline void nfs_initialise_sb(struct super_block *sb)
1765{
1766 struct nfs_server *server = NFS_SB(sb);
1767
1768 sb->s_magic = NFS_SUPER_MAGIC;
1769
1770 /* We probably want something more informative here */
1771 snprintf(sb->s_id, sizeof(sb->s_id),
1772 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1773
1774 if (sb->s_blocksize == 0)
1775 sb->s_blocksize = nfs_block_bits(server->wsize,
1776 &sb->s_blocksize_bits);
1777
1778 if (server->flags & NFS_MOUNT_NOAC)
1779 sb->s_flags |= MS_SYNCHRONOUS;
1780
1781 nfs_super_set_maxbytes(sb, server->maxfilesize);
1782}
1783
1784/*
1785 * Finish setting up an NFS2/3 superblock
1786 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001787static void nfs_fill_super(struct super_block *sb,
1788 struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001789{
1790 struct nfs_server *server = NFS_SB(sb);
1791
1792 sb->s_blocksize_bits = 0;
1793 sb->s_blocksize = 0;
1794 if (data->bsize)
1795 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1796
1797 if (server->flags & NFS_MOUNT_VER3) {
1798 /* The VFS shouldn't apply the umask to mode bits. We will do
1799 * so ourselves when necessary.
1800 */
1801 sb->s_flags |= MS_POSIXACL;
1802 sb->s_time_gran = 1;
1803 }
1804
1805 sb->s_op = &nfs_sops;
1806 nfs_initialise_sb(sb);
1807}
1808
1809/*
1810 * Finish setting up a cloned NFS2/3 superblock
1811 */
1812static void nfs_clone_super(struct super_block *sb,
1813 const struct super_block *old_sb)
1814{
1815 struct nfs_server *server = NFS_SB(sb);
1816
1817 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1818 sb->s_blocksize = old_sb->s_blocksize;
1819 sb->s_maxbytes = old_sb->s_maxbytes;
1820
1821 if (server->flags & NFS_MOUNT_VER3) {
1822 /* The VFS shouldn't apply the umask to mode bits. We will do
1823 * so ourselves when necessary.
1824 */
1825 sb->s_flags |= MS_POSIXACL;
1826 sb->s_time_gran = 1;
1827 }
1828
1829 sb->s_op = old_sb->s_op;
1830 nfs_initialise_sb(sb);
1831}
1832
Trond Myklebust275a5d22007-05-16 16:53:28 -04001833#define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1834
1835static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1836{
1837 const struct nfs_server *a = s->s_fs_info;
1838 const struct rpc_clnt *clnt_a = a->client;
1839 const struct rpc_clnt *clnt_b = b->client;
1840
1841 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1842 goto Ebusy;
1843 if (a->nfs_client != b->nfs_client)
1844 goto Ebusy;
1845 if (a->flags != b->flags)
1846 goto Ebusy;
1847 if (a->wsize != b->wsize)
1848 goto Ebusy;
1849 if (a->rsize != b->rsize)
1850 goto Ebusy;
1851 if (a->acregmin != b->acregmin)
1852 goto Ebusy;
1853 if (a->acregmax != b->acregmax)
1854 goto Ebusy;
1855 if (a->acdirmin != b->acdirmin)
1856 goto Ebusy;
1857 if (a->acdirmax != b->acdirmax)
1858 goto Ebusy;
1859 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1860 goto Ebusy;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001861 return 1;
Trond Myklebust275a5d22007-05-16 16:53:28 -04001862Ebusy:
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001863 return 0;
1864}
1865
1866struct nfs_sb_mountdata {
1867 struct nfs_server *server;
1868 int mntflags;
1869};
1870
1871static int nfs_set_super(struct super_block *s, void *data)
1872{
1873 struct nfs_sb_mountdata *sb_mntdata = data;
1874 struct nfs_server *server = sb_mntdata->server;
1875 int ret;
1876
1877 s->s_flags = sb_mntdata->mntflags;
1878 s->s_fs_info = server;
1879 ret = set_anon_super(s, server);
1880 if (ret == 0)
1881 server->s_dev = s->s_dev;
1882 return ret;
1883}
1884
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001885static int nfs_compare_super_address(struct nfs_server *server1,
1886 struct nfs_server *server2)
1887{
1888 struct sockaddr *sap1, *sap2;
1889
1890 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1891 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1892
1893 if (sap1->sa_family != sap2->sa_family)
1894 return 0;
1895
1896 switch (sap1->sa_family) {
1897 case AF_INET: {
1898 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1899 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1900 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1901 return 0;
1902 if (sin1->sin_port != sin2->sin_port)
1903 return 0;
1904 break;
1905 }
1906 case AF_INET6: {
1907 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1908 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1909 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1910 return 0;
1911 if (sin1->sin6_port != sin2->sin6_port)
1912 return 0;
1913 break;
1914 }
1915 default:
1916 return 0;
1917 }
1918
1919 return 1;
1920}
1921
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001922static int nfs_compare_super(struct super_block *sb, void *data)
1923{
1924 struct nfs_sb_mountdata *sb_mntdata = data;
1925 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1926 int mntflags = sb_mntdata->mntflags;
1927
Chuck Leverfd00a8f2007-12-10 14:57:38 -05001928 if (!nfs_compare_super_address(old, server))
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001929 return 0;
1930 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1931 if (old->flags & NFS_MOUNT_UNSHARED)
1932 return 0;
1933 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1934 return 0;
1935 return nfs_compare_mount_options(sb, server, mntflags);
Trond Myklebust275a5d22007-05-16 16:53:28 -04001936}
1937
Miklos Szeredifa799752008-04-30 00:54:33 -07001938static int nfs_bdi_register(struct nfs_server *server)
1939{
1940 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1941}
1942
David Howells54ceac42006-08-22 20:06:13 -04001943static int nfs_get_sb(struct file_system_type *fs_type,
1944 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1945{
1946 struct nfs_server *server = NULL;
1947 struct super_block *s;
Trond Myklebust33852a12008-06-19 14:20:11 -04001948 struct nfs_parsed_mount_data *data;
1949 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04001950 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04001951 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001952 struct nfs_sb_mountdata sb_mntdata = {
1953 .mntflags = flags,
1954 };
Trond Myklebust33852a12008-06-19 14:20:11 -04001955 int error = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -04001956
Trond Myklebust33852a12008-06-19 14:20:11 -04001957 data = kzalloc(sizeof(*data), GFP_KERNEL);
1958 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1959 if (data == NULL || mntfh == NULL)
1960 goto out_free_fh;
1961
1962 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05001963
David Howells54ceac42006-08-22 20:06:13 -04001964 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04001965 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
David Howells54ceac42006-08-22 20:06:13 -04001966 if (error < 0)
Chuck Lever06559602007-07-01 12:12:35 -04001967 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001968
1969 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001970 server = nfs_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04001971 if (IS_ERR(server)) {
1972 error = PTR_ERR(server);
Chuck Lever06559602007-07-01 12:12:35 -04001973 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001974 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001975 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04001976
Trond Myklebust75180df2007-05-16 16:53:28 -04001977 if (server->flags & NFS_MOUNT_UNSHARED)
1978 compare_super = NULL;
1979
David Howells54ceac42006-08-22 20:06:13 -04001980 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04001981 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04001982 if (IS_ERR(s)) {
1983 error = PTR_ERR(s);
David Howells54ceac42006-08-22 20:06:13 -04001984 goto out_err_nosb;
Trond Myklebust816724e2006-06-24 08:41:41 -04001985 }
1986
David Howells54ceac42006-08-22 20:06:13 -04001987 if (s->s_fs_info != server) {
1988 nfs_free_server(server);
1989 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07001990 } else {
1991 error = nfs_bdi_register(server);
1992 if (error)
1993 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04001994 }
David Howells54ceac42006-08-22 20:06:13 -04001995
1996 if (!s->s_root) {
1997 /* initial superblock/root creation */
Trond Myklebust33852a12008-06-19 14:20:11 -04001998 nfs_fill_super(s, data);
David Howells54ceac42006-08-22 20:06:13 -04001999 }
2000
Trond Myklebust33852a12008-06-19 14:20:11 -04002001 mntroot = nfs_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002002 if (IS_ERR(mntroot)) {
2003 error = PTR_ERR(mntroot);
2004 goto error_splat_super;
2005 }
2006
Trond Myklebust33852a12008-06-19 14:20:11 -04002007 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002008 if (error)
2009 goto error_splat_root;
2010
David Howellsf7b422b2006-06-09 09:34:33 -04002011 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002012 mnt->mnt_sb = s;
2013 mnt->mnt_root = mntroot;
Chuck Lever06559602007-07-01 12:12:35 -04002014 error = 0;
2015
2016out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002017 kfree(data->nfs_server.hostname);
2018 kfree(data->mount_server.hostname);
2019 security_free_mnt_opts(&data->lsm_opts);
2020out_free_fh:
2021 kfree(mntfh);
2022 kfree(data);
Chuck Lever06559602007-07-01 12:12:35 -04002023 return error;
Trond Myklebust816724e2006-06-24 08:41:41 -04002024
David Howells54ceac42006-08-22 20:06:13 -04002025out_err_nosb:
2026 nfs_free_server(server);
Chuck Lever06559602007-07-01 12:12:35 -04002027 goto out;
David Howells54ceac42006-08-22 20:06:13 -04002028
Eric Parisf9c3a382008-03-05 14:20:18 -05002029error_splat_root:
2030 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002031error_splat_super:
2032 up_write(&s->s_umount);
2033 deactivate_super(s);
Chuck Lever06559602007-07-01 12:12:35 -04002034 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002035}
2036
David Howells54ceac42006-08-22 20:06:13 -04002037/*
2038 * Destroy an NFS2/3 superblock
2039 */
David Howellsf7b422b2006-06-09 09:34:33 -04002040static void nfs_kill_super(struct super_block *s)
2041{
2042 struct nfs_server *server = NFS_SB(s);
2043
Miklos Szeredifa799752008-04-30 00:54:33 -07002044 bdi_unregister(&server->backing_dev_info);
David Howellsf7b422b2006-06-09 09:34:33 -04002045 kill_anon_super(s);
David Howells54ceac42006-08-22 20:06:13 -04002046 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002047}
2048
David Howells54ceac42006-08-22 20:06:13 -04002049/*
2050 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2051 */
2052static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2053 const char *dev_name, void *raw_data,
2054 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002055{
2056 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002057 struct super_block *s;
2058 struct nfs_server *server;
2059 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002060 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002061 struct nfs_sb_mountdata sb_mntdata = {
2062 .mntflags = flags,
2063 };
David Howells54ceac42006-08-22 20:06:13 -04002064 int error;
2065
2066 dprintk("--> nfs_xdev_get_sb()\n");
2067
2068 /* create a new volume representation */
2069 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2070 if (IS_ERR(server)) {
2071 error = PTR_ERR(server);
2072 goto out_err_noserver;
2073 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002074 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002075
Trond Myklebust75180df2007-05-16 16:53:28 -04002076 if (server->flags & NFS_MOUNT_UNSHARED)
2077 compare_super = NULL;
2078
David Howells54ceac42006-08-22 20:06:13 -04002079 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002080 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002081 if (IS_ERR(s)) {
2082 error = PTR_ERR(s);
2083 goto out_err_nosb;
2084 }
2085
2086 if (s->s_fs_info != server) {
2087 nfs_free_server(server);
2088 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002089 } else {
2090 error = nfs_bdi_register(server);
2091 if (error)
2092 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002093 }
2094
2095 if (!s->s_root) {
2096 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002097 nfs_clone_super(s, data->sb);
2098 }
2099
2100 mntroot = nfs_get_root(s, data->fh);
2101 if (IS_ERR(mntroot)) {
2102 error = PTR_ERR(mntroot);
2103 goto error_splat_super;
2104 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002105 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
Neil Brown4c1fe2f2007-11-01 16:50:20 +11002106 dput(mntroot);
2107 error = -ESTALE;
2108 goto error_splat_super;
2109 }
David Howells54ceac42006-08-22 20:06:13 -04002110
2111 s->s_flags |= MS_ACTIVE;
2112 mnt->mnt_sb = s;
2113 mnt->mnt_root = mntroot;
2114
Eric Parisf9c3a382008-03-05 14:20:18 -05002115 /* clone any lsm security options from the parent to the new sb */
2116 security_sb_clone_mnt_opts(data->sb, s);
2117
David Howells54ceac42006-08-22 20:06:13 -04002118 dprintk("<-- nfs_xdev_get_sb() = 0\n");
2119 return 0;
2120
2121out_err_nosb:
2122 nfs_free_server(server);
2123out_err_noserver:
2124 dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2125 return error;
2126
2127error_splat_super:
2128 up_write(&s->s_umount);
2129 deactivate_super(s);
2130 dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2131 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002132}
2133
2134#ifdef CONFIG_NFS_V4
David Howells54ceac42006-08-22 20:06:13 -04002135
2136/*
2137 * Finish setting up a cloned NFS4 superblock
2138 */
2139static void nfs4_clone_super(struct super_block *sb,
2140 const struct super_block *old_sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002141{
David Howells54ceac42006-08-22 20:06:13 -04002142 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2143 sb->s_blocksize = old_sb->s_blocksize;
2144 sb->s_maxbytes = old_sb->s_maxbytes;
2145 sb->s_time_gran = 1;
2146 sb->s_op = old_sb->s_op;
2147 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002148}
2149
2150/*
2151 * Set up an NFS4 superblock
2152 */
David Howells54ceac42006-08-22 20:06:13 -04002153static void nfs4_fill_super(struct super_block *sb)
David Howellsf7b422b2006-06-09 09:34:33 -04002154{
David Howellsf7b422b2006-06-09 09:34:33 -04002155 sb->s_time_gran = 1;
David Howellsf7b422b2006-06-09 09:34:33 -04002156 sb->s_op = &nfs4_sops;
David Howells54ceac42006-08-22 20:06:13 -04002157 nfs_initialise_sb(sb);
David Howellsf7b422b2006-06-09 09:34:33 -04002158}
2159
David Howells54ceac42006-08-22 20:06:13 -04002160/*
Chuck Leverf0768eb2007-07-01 12:13:01 -04002161 * Validate NFSv4 mount options
2162 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002163static int nfs4_validate_mount_data(void *options,
2164 struct nfs_parsed_mount_data *args,
2165 const char *dev_name)
Chuck Leverf0768eb2007-07-01 12:13:01 -04002166{
Chuck Lever4c568012007-12-10 14:59:28 -05002167 struct sockaddr_in *ap;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002168 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002169 char *c;
2170
2171 if (data == NULL)
2172 goto out_no_data;
2173
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002174 args->rsize = NFS_MAX_FILE_IO_SIZE;
2175 args->wsize = NFS_MAX_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -04002176 args->acregmin = NFS_DEF_ACREGMIN;
2177 args->acregmax = NFS_DEF_ACREGMAX;
2178 args->acdirmin = NFS_DEF_ACDIRMIN;
2179 args->acdirmax = NFS_DEF_ACDIRMAX;
Chuck Leverf22d6d72008-03-14 14:10:22 -04002180 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
Chuck Lever6738b252008-06-24 16:33:54 -04002181 args->auth_flavors[0] = RPC_AUTH_UNIX;
2182 args->auth_flavor_len = 0;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002183
Chuck Leverf0768eb2007-07-01 12:13:01 -04002184 switch (data->version) {
2185 case 1:
Chuck Lever4c568012007-12-10 14:59:28 -05002186 ap = (struct sockaddr_in *)&args->nfs_server.address;
2187 if (data->host_addrlen > sizeof(args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002188 goto out_no_address;
Chuck Lever4c568012007-12-10 14:59:28 -05002189 if (data->host_addrlen == 0)
2190 goto out_no_address;
2191 args->nfs_server.addrlen = data->host_addrlen;
2192 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002193 return -EFAULT;
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002194 if (!nfs_verify_server_address((struct sockaddr *)
2195 &args->nfs_server.address))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002196 goto out_no_address;
2197
Chuck Lever6738b252008-06-24 16:33:54 -04002198 if (data->auth_flavourlen) {
2199 if (data->auth_flavourlen > 1)
2200 goto out_inval_auth;
Trond Myklebust20c71f52007-09-20 20:23:51 -04002201 if (copy_from_user(&args->auth_flavors[0],
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002202 data->auth_flavours,
Trond Myklebust20c71f52007-09-20 20:23:51 -04002203 sizeof(args->auth_flavors[0])))
Chuck Leverf0768eb2007-07-01 12:13:01 -04002204 return -EFAULT;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002205 }
2206
2207 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2208 if (IS_ERR(c))
2209 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002210 args->nfs_server.hostname = c;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002211
2212 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2213 if (IS_ERR(c))
2214 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002215 args->nfs_server.export_path = c;
2216 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002217
2218 c = strndup_user(data->client_addr.data, 16);
2219 if (IS_ERR(c))
2220 return PTR_ERR(c);
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002221 args->client_address = c;
2222
2223 /*
2224 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2225 * can deal with.
2226 */
2227
2228 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2229 args->rsize = data->rsize;
2230 args->wsize = data->wsize;
2231 args->timeo = data->timeo;
2232 args->retrans = data->retrans;
2233 args->acregmin = data->acregmin;
2234 args->acregmax = data->acregmax;
2235 args->acdirmin = data->acdirmin;
2236 args->acdirmax = data->acdirmax;
2237 args->nfs_server.protocol = data->proto;
Trond Myklebust259875e2008-07-02 14:43:47 -04002238 nfs_validate_transport_protocol(args);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002239
2240 break;
Chuck Lever80071222007-07-01 12:13:59 -04002241 default: {
Chuck Leverdc045892008-06-23 12:36:37 -04002242 int status;
Chuck Lever80071222007-07-01 12:13:59 -04002243
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002244 if (nfs_parse_mount_options((char *)options, args) == 0)
Chuck Lever80071222007-07-01 12:13:59 -04002245 return -EINVAL;
2246
2247 if (!nfs_verify_server_address((struct sockaddr *)
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002248 &args->nfs_server.address))
Chuck Lever80071222007-07-01 12:13:59 -04002249 return -EINVAL;
Chuck Lever80071222007-07-01 12:13:59 -04002250
Chuck Levered596a82008-06-26 17:47:05 -04002251 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2252 args->nfs_server.port);
2253
Trond Myklebust259875e2008-07-02 14:43:47 -04002254 nfs_validate_transport_protocol(args);
2255
Chuck Lever6738b252008-06-24 16:33:54 -04002256 if (args->auth_flavor_len > 1)
Chuck Lever80071222007-07-01 12:13:59 -04002257 goto out_inval_auth;
Chuck Lever80071222007-07-01 12:13:59 -04002258
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04002259 if (args->client_address == NULL)
Jeff Layton0a87cf12007-07-18 11:28:43 -04002260 goto out_no_client_address;
2261
Chuck Leverdc045892008-06-23 12:36:37 -04002262 status = nfs_parse_devname(dev_name,
2263 &args->nfs_server.hostname,
2264 NFS4_MAXNAMLEN,
2265 &args->nfs_server.export_path,
2266 NFS4_MAXPATHLEN);
2267 if (status < 0)
2268 return status;
2269
Chuck Lever80071222007-07-01 12:13:59 -04002270 break;
2271 }
Chuck Leverf0768eb2007-07-01 12:13:01 -04002272 }
2273
2274 return 0;
2275
2276out_no_data:
2277 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2278 return -EINVAL;
2279
2280out_inval_auth:
2281 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2282 data->auth_flavourlen);
2283 return -EINVAL;
2284
2285out_no_address:
2286 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2287 return -EINVAL;
Jeff Layton0a87cf12007-07-18 11:28:43 -04002288
2289out_no_client_address:
2290 dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2291 return -EINVAL;
Chuck Leverf0768eb2007-07-01 12:13:01 -04002292}
2293
2294/*
David Howells54ceac42006-08-22 20:06:13 -04002295 * Get the superblock for an NFS4 mountpoint
2296 */
Trond Myklebust816724e2006-06-24 08:41:41 -04002297static int nfs4_get_sb(struct file_system_type *fs_type,
2298 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002299{
Trond Myklebust33852a12008-06-19 14:20:11 -04002300 struct nfs_parsed_mount_data *data;
David Howells54ceac42006-08-22 20:06:13 -04002301 struct super_block *s;
2302 struct nfs_server *server;
Trond Myklebust33852a12008-06-19 14:20:11 -04002303 struct nfs_fh *mntfh;
David Howells54ceac42006-08-22 20:06:13 -04002304 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002305 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002306 struct nfs_sb_mountdata sb_mntdata = {
2307 .mntflags = flags,
2308 };
Trond Myklebust33852a12008-06-19 14:20:11 -04002309 int error = -ENOMEM;
David Howellsf7b422b2006-06-09 09:34:33 -04002310
Trond Myklebust33852a12008-06-19 14:20:11 -04002311 data = kzalloc(sizeof(*data), GFP_KERNEL);
2312 mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2313 if (data == NULL || mntfh == NULL)
2314 goto out_free_fh;
2315
2316 security_init_mnt_opts(&data->lsm_opts);
Eric Parisf9c3a382008-03-05 14:20:18 -05002317
Chuck Leverf0768eb2007-07-01 12:13:01 -04002318 /* Validate the mount data */
Trond Myklebust33852a12008-06-19 14:20:11 -04002319 error = nfs4_validate_mount_data(raw_data, data, dev_name);
Chuck Leverf0768eb2007-07-01 12:13:01 -04002320 if (error < 0)
2321 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002322
David Howells54ceac42006-08-22 20:06:13 -04002323 /* Get a volume representation */
Trond Myklebust33852a12008-06-19 14:20:11 -04002324 server = nfs4_create_server(data, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002325 if (IS_ERR(server)) {
2326 error = PTR_ERR(server);
Chuck Lever29eb9812007-07-01 12:12:30 -04002327 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002328 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002329 sb_mntdata.server = server;
David Howellsf7b422b2006-06-09 09:34:33 -04002330
Trond Myklebust75180df2007-05-16 16:53:28 -04002331 if (server->flags & NFS4_MOUNT_UNSHARED)
2332 compare_super = NULL;
2333
David Howells54ceac42006-08-22 20:06:13 -04002334 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002335 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
Trond Myklebust816724e2006-06-24 08:41:41 -04002336 if (IS_ERR(s)) {
2337 error = PTR_ERR(s);
David Howellsf7b422b2006-06-09 09:34:33 -04002338 goto out_free;
Trond Myklebust816724e2006-06-24 08:41:41 -04002339 }
2340
Trond Myklebust5dd31772006-08-24 01:03:05 -04002341 if (s->s_fs_info != server) {
2342 nfs_free_server(server);
2343 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002344 } else {
2345 error = nfs_bdi_register(server);
2346 if (error)
2347 goto error_splat_super;
Trond Myklebust5dd31772006-08-24 01:03:05 -04002348 }
2349
David Howells54ceac42006-08-22 20:06:13 -04002350 if (!s->s_root) {
2351 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002352 nfs4_fill_super(s);
Trond Myklebust816724e2006-06-24 08:41:41 -04002353 }
David Howellsf7b422b2006-06-09 09:34:33 -04002354
Trond Myklebust33852a12008-06-19 14:20:11 -04002355 mntroot = nfs4_get_root(s, mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002356 if (IS_ERR(mntroot)) {
2357 error = PTR_ERR(mntroot);
2358 goto error_splat_super;
David Howellsf7b422b2006-06-09 09:34:33 -04002359 }
David Howells54ceac42006-08-22 20:06:13 -04002360
Trond Myklebust33852a12008-06-19 14:20:11 -04002361 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
Eric Paris46c8ac72008-05-02 13:42:42 -07002362 if (error)
2363 goto error_splat_root;
2364
David Howellsf7b422b2006-06-09 09:34:33 -04002365 s->s_flags |= MS_ACTIVE;
David Howells54ceac42006-08-22 20:06:13 -04002366 mnt->mnt_sb = s;
2367 mnt->mnt_root = mntroot;
Chuck Lever29eb9812007-07-01 12:12:30 -04002368 error = 0;
David Howells54ceac42006-08-22 20:06:13 -04002369
Chuck Lever29eb9812007-07-01 12:12:30 -04002370out:
Trond Myklebust33852a12008-06-19 14:20:11 -04002371 kfree(data->client_address);
2372 kfree(data->nfs_server.export_path);
2373 kfree(data->nfs_server.hostname);
2374 security_free_mnt_opts(&data->lsm_opts);
2375out_free_fh:
2376 kfree(mntfh);
2377 kfree(data);
Trond Myklebust816724e2006-06-24 08:41:41 -04002378 return error;
David Howells54ceac42006-08-22 20:06:13 -04002379
Chuck Lever29eb9812007-07-01 12:12:30 -04002380out_free:
2381 nfs_free_server(server);
2382 goto out;
2383
Eric Paris46c8ac72008-05-02 13:42:42 -07002384error_splat_root:
2385 dput(mntroot);
David Howells54ceac42006-08-22 20:06:13 -04002386error_splat_super:
2387 up_write(&s->s_umount);
2388 deactivate_super(s);
Chuck Lever29eb9812007-07-01 12:12:30 -04002389 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -04002390}
2391
2392static void nfs4_kill_super(struct super_block *sb)
2393{
2394 struct nfs_server *server = NFS_SB(sb);
2395
2396 nfs_return_all_delegations(sb);
2397 kill_anon_super(sb);
2398
2399 nfs4_renewd_prepare_shutdown(server);
David Howells54ceac42006-08-22 20:06:13 -04002400 nfs_free_server(server);
David Howellsf7b422b2006-06-09 09:34:33 -04002401}
2402
2403/*
David Howells54ceac42006-08-22 20:06:13 -04002404 * Clone an NFS4 server record on xdev traversal (FSID-change)
David Howellsf7b422b2006-06-09 09:34:33 -04002405 */
David Howells54ceac42006-08-22 20:06:13 -04002406static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2407 const char *dev_name, void *raw_data,
2408 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002409{
2410 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002411 struct super_block *s;
2412 struct nfs_server *server;
2413 struct dentry *mntroot;
Trond Myklebust75180df2007-05-16 16:53:28 -04002414 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002415 struct nfs_sb_mountdata sb_mntdata = {
2416 .mntflags = flags,
2417 };
David Howells54ceac42006-08-22 20:06:13 -04002418 int error;
2419
2420 dprintk("--> nfs4_xdev_get_sb()\n");
2421
2422 /* create a new volume representation */
2423 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2424 if (IS_ERR(server)) {
2425 error = PTR_ERR(server);
2426 goto out_err_noserver;
2427 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002428 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002429
Trond Myklebust75180df2007-05-16 16:53:28 -04002430 if (server->flags & NFS4_MOUNT_UNSHARED)
2431 compare_super = NULL;
2432
David Howells54ceac42006-08-22 20:06:13 -04002433 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002434 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002435 if (IS_ERR(s)) {
2436 error = PTR_ERR(s);
2437 goto out_err_nosb;
2438 }
2439
2440 if (s->s_fs_info != server) {
2441 nfs_free_server(server);
2442 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002443 } else {
2444 error = nfs_bdi_register(server);
2445 if (error)
2446 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002447 }
2448
2449 if (!s->s_root) {
2450 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002451 nfs4_clone_super(s, data->sb);
2452 }
2453
2454 mntroot = nfs4_get_root(s, data->fh);
2455 if (IS_ERR(mntroot)) {
2456 error = PTR_ERR(mntroot);
2457 goto error_splat_super;
2458 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002459 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2460 dput(mntroot);
2461 error = -ESTALE;
2462 goto error_splat_super;
2463 }
David Howells54ceac42006-08-22 20:06:13 -04002464
2465 s->s_flags |= MS_ACTIVE;
2466 mnt->mnt_sb = s;
2467 mnt->mnt_root = mntroot;
2468
Eric Paris46c8ac72008-05-02 13:42:42 -07002469 security_sb_clone_mnt_opts(data->sb, s);
2470
David Howells54ceac42006-08-22 20:06:13 -04002471 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2472 return 0;
2473
2474out_err_nosb:
2475 nfs_free_server(server);
2476out_err_noserver:
2477 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2478 return error;
2479
2480error_splat_super:
2481 up_write(&s->s_umount);
2482 deactivate_super(s);
2483 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2484 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002485}
2486
David Howells54ceac42006-08-22 20:06:13 -04002487/*
2488 * Create an NFS4 server record on referral traversal
2489 */
2490static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2491 const char *dev_name, void *raw_data,
2492 struct vfsmount *mnt)
David Howellsf7b422b2006-06-09 09:34:33 -04002493{
2494 struct nfs_clone_mount *data = raw_data;
David Howells54ceac42006-08-22 20:06:13 -04002495 struct super_block *s;
2496 struct nfs_server *server;
2497 struct dentry *mntroot;
2498 struct nfs_fh mntfh;
Trond Myklebust75180df2007-05-16 16:53:28 -04002499 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002500 struct nfs_sb_mountdata sb_mntdata = {
2501 .mntflags = flags,
2502 };
David Howells54ceac42006-08-22 20:06:13 -04002503 int error;
2504
2505 dprintk("--> nfs4_referral_get_sb()\n");
2506
2507 /* create a new volume representation */
2508 server = nfs4_create_referral_server(data, &mntfh);
2509 if (IS_ERR(server)) {
2510 error = PTR_ERR(server);
2511 goto out_err_noserver;
2512 }
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002513 sb_mntdata.server = server;
David Howells54ceac42006-08-22 20:06:13 -04002514
Trond Myklebust75180df2007-05-16 16:53:28 -04002515 if (server->flags & NFS4_MOUNT_UNSHARED)
2516 compare_super = NULL;
2517
David Howells54ceac42006-08-22 20:06:13 -04002518 /* Get a superblock - note that we may end up sharing one that already exists */
Trond Myklebuste89a5a42007-08-31 10:45:17 -04002519 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
David Howells54ceac42006-08-22 20:06:13 -04002520 if (IS_ERR(s)) {
2521 error = PTR_ERR(s);
2522 goto out_err_nosb;
2523 }
2524
2525 if (s->s_fs_info != server) {
2526 nfs_free_server(server);
2527 server = NULL;
Miklos Szeredifa799752008-04-30 00:54:33 -07002528 } else {
2529 error = nfs_bdi_register(server);
2530 if (error)
2531 goto error_splat_super;
David Howells54ceac42006-08-22 20:06:13 -04002532 }
2533
2534 if (!s->s_root) {
2535 /* initial superblock/root creation */
David Howells54ceac42006-08-22 20:06:13 -04002536 nfs4_fill_super(s);
2537 }
2538
Trond Myklebustf2d0d852007-02-02 14:46:09 -08002539 mntroot = nfs4_get_root(s, &mntfh);
David Howells54ceac42006-08-22 20:06:13 -04002540 if (IS_ERR(mntroot)) {
2541 error = PTR_ERR(mntroot);
2542 goto error_splat_super;
2543 }
Trond Myklebuste9cc6c22008-01-02 13:28:57 -05002544 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2545 dput(mntroot);
2546 error = -ESTALE;
2547 goto error_splat_super;
2548 }
David Howells54ceac42006-08-22 20:06:13 -04002549
2550 s->s_flags |= MS_ACTIVE;
2551 mnt->mnt_sb = s;
2552 mnt->mnt_root = mntroot;
2553
Eric Paris46c8ac72008-05-02 13:42:42 -07002554 security_sb_clone_mnt_opts(data->sb, s);
2555
David Howells54ceac42006-08-22 20:06:13 -04002556 dprintk("<-- nfs4_referral_get_sb() = 0\n");
2557 return 0;
2558
2559out_err_nosb:
2560 nfs_free_server(server);
2561out_err_noserver:
2562 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2563 return error;
2564
2565error_splat_super:
2566 up_write(&s->s_umount);
2567 deactivate_super(s);
2568 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2569 return error;
David Howellsf7b422b2006-06-09 09:34:33 -04002570}
2571
David Howells54ceac42006-08-22 20:06:13 -04002572#endif /* CONFIG_NFS_V4 */