super.c 75.8 KB
Newer Older
David Howells's avatar
David Howells committed
1 2 3 4 5 6 7
/*
 *  linux/fs/nfs/super.c
 *
 *  Copyright (C) 1992  Rick Sladkey
 *
 *  nfs superblock handling functions
 *
Alan Cox's avatar
Alan Cox committed
8
 *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
David Howells's avatar
David Howells committed
9 10 11 12 13 14 15
 *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
 *
 *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
 *  J.S.Peatfield@damtp.cam.ac.uk
 *
 *  Split from inode.c by David Howells <dhowells@redhat.com>
 *
16 17 18 19 20
 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
 *   particular server are held in the same superblock
 * - NFS superblocks can have several effective roots to the dentry tree
 * - directory type roots are spliced into the tree when a path from one root reaches the root
 *   of another (see nfs_lookup())
David Howells's avatar
David Howells committed
21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include <linux/module.h>
#include <linux/init.h>

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/sunrpc/clnt.h>
34
#include <linux/sunrpc/addr.h>
David Howells's avatar
David Howells committed
35 36
#include <linux/sunrpc/stats.h>
#include <linux/sunrpc/metrics.h>
37
#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\'s avatar
\"Talpey, Thomas\ committed
38
#include <linux/sunrpc/xprtrdma.h>
David Howells's avatar
David Howells committed
39 40 41 42 43 44
#include <linux/nfs_fs.h>
#include <linux/nfs_mount.h>
#include <linux/nfs4_mount.h>
#include <linux/lockd/bind.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
45
#include <linux/namei.h>
David Howells's avatar
David Howells committed
46 47
#include <linux/vfs.h>
#include <linux/inet.h>
48
#include <linux/in6.h>
49
#include <linux/slab.h>
50
#include <net/ipv6.h>
51
#include <linux/netdevice.h>
David Howells's avatar
David Howells committed
52
#include <linux/nfs_xdr.h>
53
#include <linux/magic.h>
54
#include <linux/parser.h>
55
#include <linux/nsproxy.h>
56
#include <linux/rcupdate.h>
David Howells's avatar
David Howells committed
57

58
#include <linux/uaccess.h>
David Howells's avatar
David Howells committed
59 60 61 62 63 64

#include "nfs4_fs.h"
#include "callback.h"
#include "delegation.h"
#include "iostat.h"
#include "internal.h"
65
#include "fscache.h"
66
#include "nfs4session.h"
J. Bruce Fields's avatar
J. Bruce Fields committed
67
#include "pnfs.h"
68
#include "nfs.h"
David Howells's avatar
David Howells committed
69 70

#define NFSDBG_FACILITY		NFSDBG_VFS
71
#define NFS_TEXT_DATA		1
David Howells's avatar
David Howells committed
72

73
#if IS_ENABLED(CONFIG_NFS_V3)
74 75 76 77 78
#define NFS_DEFAULT_VERSION 3
#else
#define NFS_DEFAULT_VERSION 2
#endif

79 80 81 82 83 84 85
enum {
	/* Mount options that take no arguments */
	Opt_soft, Opt_hard,
	Opt_posix, Opt_noposix,
	Opt_cto, Opt_nocto,
	Opt_ac, Opt_noac,
	Opt_lock, Opt_nolock,
\"Talpey, Thomas\'s avatar
\"Talpey, Thomas\ committed
86
	Opt_udp, Opt_tcp, Opt_rdma,
87 88
	Opt_acl, Opt_noacl,
	Opt_rdirplus, Opt_nordirplus,
89
	Opt_sharecache, Opt_nosharecache,
90
	Opt_resvport, Opt_noresvport,
91
	Opt_fscache, Opt_nofscache,
92
	Opt_migration, Opt_nomigration,
93 94 95 96 97 98 99 100 101 102

	/* Mount options that take integer arguments */
	Opt_port,
	Opt_rsize, Opt_wsize, Opt_bsize,
	Opt_timeo, Opt_retrans,
	Opt_acregmin, Opt_acregmax,
	Opt_acdirmin, Opt_acdirmax,
	Opt_actimeo,
	Opt_namelen,
	Opt_mountport,
103
	Opt_mountvers,
104
	Opt_minorversion,
105 106

	/* Mount options that take string arguments */
107
	Opt_nfsvers,
108
	Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
109
	Opt_addr, Opt_mountaddr, Opt_clientaddr,
110
	Opt_lookupcache,
111
	Opt_fscache_uniq,
112
	Opt_local_lock,
113

114 115
	/* Special mount options */
	Opt_userspace, Opt_deprecated, Opt_sloppy,
116 117 118 119

	Opt_err
};

120
static const match_table_t nfs_mount_option_tokens = {
121 122
	{ Opt_userspace, "bg" },
	{ Opt_userspace, "fg" },
123 124
	{ Opt_userspace, "retry=%s" },

125 126
	{ Opt_sloppy, "sloppy" },

127 128
	{ Opt_soft, "soft" },
	{ Opt_hard, "hard" },
129 130
	{ Opt_deprecated, "intr" },
	{ Opt_deprecated, "nointr" },
131 132 133 134 135 136 137 138 139 140
	{ Opt_posix, "posix" },
	{ Opt_noposix, "noposix" },
	{ Opt_cto, "cto" },
	{ Opt_nocto, "nocto" },
	{ Opt_ac, "ac" },
	{ Opt_noac, "noac" },
	{ Opt_lock, "lock" },
	{ Opt_nolock, "nolock" },
	{ Opt_udp, "udp" },
	{ Opt_tcp, "tcp" },
\"Talpey, Thomas\'s avatar
\"Talpey, Thomas\ committed
141
	{ Opt_rdma, "rdma" },
142 143 144 145
	{ Opt_acl, "acl" },
	{ Opt_noacl, "noacl" },
	{ Opt_rdirplus, "rdirplus" },
	{ Opt_nordirplus, "nordirplus" },
146 147
	{ Opt_sharecache, "sharecache" },
	{ Opt_nosharecache, "nosharecache" },
148 149
	{ Opt_resvport, "resvport" },
	{ Opt_noresvport, "noresvport" },
150 151
	{ Opt_fscache, "fsc" },
	{ Opt_nofscache, "nofsc" },
152 153
	{ Opt_migration, "migration" },
	{ Opt_nomigration, "nomigration" },
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
	{ Opt_port, "port=%s" },
	{ Opt_rsize, "rsize=%s" },
	{ Opt_wsize, "wsize=%s" },
	{ Opt_bsize, "bsize=%s" },
	{ Opt_timeo, "timeo=%s" },
	{ Opt_retrans, "retrans=%s" },
	{ Opt_acregmin, "acregmin=%s" },
	{ Opt_acregmax, "acregmax=%s" },
	{ Opt_acdirmin, "acdirmin=%s" },
	{ Opt_acdirmax, "acdirmax=%s" },
	{ Opt_actimeo, "actimeo=%s" },
	{ Opt_namelen, "namlen=%s" },
	{ Opt_mountport, "mountport=%s" },
	{ Opt_mountvers, "mountvers=%s" },
169 170
	{ Opt_minorversion, "minorversion=%s" },

171 172
	{ Opt_nfsvers, "nfsvers=%s" },
	{ Opt_nfsvers, "vers=%s" },
173 174 175 176 177 178

	{ Opt_sec, "sec=%s" },
	{ Opt_proto, "proto=%s" },
	{ Opt_mountproto, "mountproto=%s" },
	{ Opt_addr, "addr=%s" },
	{ Opt_clientaddr, "clientaddr=%s" },
179
	{ Opt_mounthost, "mounthost=%s" },
180
	{ Opt_mountaddr, "mountaddr=%s" },
181

182
	{ Opt_lookupcache, "lookupcache=%s" },
183
	{ Opt_fscache_uniq, "fsc=%s" },
184
	{ Opt_local_lock, "local_lock=%s" },
185

186 187 188
	/* The following needs to be listed after all other options */
	{ Opt_nfsvers, "v%s" },

189 190 191 192
	{ Opt_err, NULL }
};

enum {
193
	Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
194
	Opt_xprt_rdma6,
195 196 197 198

	Opt_xprt_err
};

199
static const match_table_t nfs_xprt_protocol_tokens = {
200
	{ Opt_xprt_udp, "udp" },
201
	{ Opt_xprt_udp6, "udp6" },
202
	{ Opt_xprt_tcp, "tcp" },
203
	{ Opt_xprt_tcp6, "tcp6" },
\"Talpey, Thomas\'s avatar
\"Talpey, Thomas\ committed
204
	{ Opt_xprt_rdma, "rdma" },
205
	{ Opt_xprt_rdma6, "rdma6" },
206 207 208 209 210 211 212 213 214 215 216 217 218

	{ Opt_xprt_err, NULL }
};

enum {
	Opt_sec_none, Opt_sec_sys,
	Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
	Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
	Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,

	Opt_sec_err
};

219
static const match_table_t nfs_secflavor_tokens = {
220 221 222 223 224 225 226 227 228 229 230 231
	{ Opt_sec_none, "none" },
	{ Opt_sec_none, "null" },
	{ Opt_sec_sys, "sys" },

	{ Opt_sec_krb5, "krb5" },
	{ Opt_sec_krb5i, "krb5i" },
	{ Opt_sec_krb5p, "krb5p" },

	{ Opt_sec_lkey, "lkey" },
	{ Opt_sec_lkeyi, "lkeyi" },
	{ Opt_sec_lkeyp, "lkeyp" },

232 233 234 235
	{ Opt_sec_spkm, "spkm3" },
	{ Opt_sec_spkmi, "spkm3i" },
	{ Opt_sec_spkmp, "spkm3p" },

236 237 238
	{ Opt_sec_err, NULL }
};

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
enum {
	Opt_lookupcache_all, Opt_lookupcache_positive,
	Opt_lookupcache_none,

	Opt_lookupcache_err
};

static match_table_t nfs_lookupcache_tokens = {
	{ Opt_lookupcache_all, "all" },
	{ Opt_lookupcache_positive, "pos" },
	{ Opt_lookupcache_positive, "positive" },
	{ Opt_lookupcache_none, "none" },

	{ Opt_lookupcache_err, NULL }
};

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
enum {
	Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
	Opt_local_lock_none,

	Opt_local_lock_err
};

static match_table_t nfs_local_lock_tokens = {
	{ Opt_local_lock_all, "all" },
	{ Opt_local_lock_flock, "flock" },
	{ Opt_local_lock_posix, "posix" },
	{ Opt_local_lock_none, "none" },

	{ Opt_local_lock_err, NULL }
};

271 272
enum {
	Opt_vers_2, Opt_vers_3, Opt_vers_4, Opt_vers_4_0,
273
	Opt_vers_4_1, Opt_vers_4_2,
274 275 276 277 278 279 280 281 282 283

	Opt_vers_err
};

static match_table_t nfs_vers_tokens = {
	{ Opt_vers_2, "2" },
	{ Opt_vers_3, "3" },
	{ Opt_vers_4, "4" },
	{ Opt_vers_4_0, "4.0" },
	{ Opt_vers_4_1, "4.1" },
284
	{ Opt_vers_4_2, "4.2" },
285 286 287

	{ Opt_vers_err, NULL }
};
288

289 290
static struct dentry *nfs_xdev_mount(struct file_system_type *fs_type,
		int flags, const char *dev_name, void *raw_data);
David Howells's avatar
David Howells committed
291

292
struct file_system_type nfs_fs_type = {
David Howells's avatar
David Howells committed
293 294
	.owner		= THIS_MODULE,
	.name		= "nfs",
295
	.mount		= nfs_fs_mount,
David Howells's avatar
David Howells committed
296
	.kill_sb	= nfs_kill_super,
297
	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
David Howells's avatar
David Howells committed
298
};
299
MODULE_ALIAS_FS("nfs");
300
EXPORT_SYMBOL_GPL(nfs_fs_type);
David Howells's avatar
David Howells committed
301

302
struct file_system_type nfs_xdev_fs_type = {
David Howells's avatar
David Howells committed
303 304
	.owner		= THIS_MODULE,
	.name		= "nfs",
305
	.mount		= nfs_xdev_mount,
David Howells's avatar
David Howells committed
306
	.kill_sb	= nfs_kill_super,
307
	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
David Howells's avatar
David Howells committed
308 309
};

310
const struct super_operations nfs_sops = {
David Howells's avatar
David Howells committed
311 312 313
	.alloc_inode	= nfs_alloc_inode,
	.destroy_inode	= nfs_destroy_inode,
	.write_inode	= nfs_write_inode,
314
	.drop_inode	= nfs_drop_inode,
David Howells's avatar
David Howells committed
315
	.statfs		= nfs_statfs,
316
	.evict_inode	= nfs_evict_inode,
David Howells's avatar
David Howells committed
317 318
	.umount_begin	= nfs_umount_begin,
	.show_options	= nfs_show_options,
319 320
	.show_devname	= nfs_show_devname,
	.show_path	= nfs_show_path,
David Howells's avatar
David Howells committed
321
	.show_stats	= nfs_show_stats,
322
	.remount_fs	= nfs_remount,
David Howells's avatar
David Howells committed
323
};
324
EXPORT_SYMBOL_GPL(nfs_sops);
David Howells's avatar
David Howells committed
325

326
#if IS_ENABLED(CONFIG_NFS_V4)
327
static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *);
328 329
static int nfs4_validate_mount_data(void *options,
	struct nfs_parsed_mount_data *args, const char *dev_name);
330 331 332 333 334 335

struct file_system_type nfs4_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfs4",
	.mount		= nfs_fs_mount,
	.kill_sb	= nfs_kill_super,
336
	.fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
337
};
338
MODULE_ALIAS_FS("nfs4");
339
MODULE_ALIAS("nfs4");
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
EXPORT_SYMBOL_GPL(nfs4_fs_type);

static int __init register_nfs4_fs(void)
{
	return register_filesystem(&nfs4_fs_type);
}

static void unregister_nfs4_fs(void)
{
	unregister_filesystem(&nfs4_fs_type);
}
#else
static int __init register_nfs4_fs(void)
{
	return 0;
}

static void unregister_nfs4_fs(void)
{
}
David Howells's avatar
David Howells committed
360 361
#endif

362
static struct shrinker acl_shrinker = {
363 364
	.count_objects	= nfs_access_cache_count,
	.scan_objects	= nfs_access_cache_scan,
365 366
	.seeks		= DEFAULT_SEEKS,
};
367

David Howells's avatar
David Howells committed
368 369 370 371 372 373 374 375 376 377 378
/*
 * Register the NFS filesystems
 */
int __init register_nfs_fs(void)
{
	int ret;

        ret = register_filesystem(&nfs_fs_type);
	if (ret < 0)
		goto error_0;

379
	ret = register_nfs4_fs();
David Howells's avatar
David Howells committed
380 381
	if (ret < 0)
		goto error_1;
382 383 384 385

	ret = nfs_register_sysctl();
	if (ret < 0)
		goto error_2;
386 387 388
	ret = register_shrinker(&acl_shrinker);
	if (ret < 0)
		goto error_3;
David Howells's avatar
David Howells committed
389
	return 0;
390 391
error_3:
	nfs_unregister_sysctl();
392 393
error_2:
	unregister_nfs4_fs();
David Howells's avatar
David Howells committed
394 395 396 397 398 399 400 401 402 403 404
error_1:
	unregister_filesystem(&nfs_fs_type);
error_0:
	return ret;
}

/*
 * Unregister the NFS filesystems
 */
void __exit unregister_nfs_fs(void)
{
405
	unregister_shrinker(&acl_shrinker);
406
	nfs_unregister_sysctl();
407
	unregister_nfs4_fs();
David Howells's avatar
David Howells committed
408 409 410
	unregister_filesystem(&nfs_fs_type);
}

411
bool nfs_sb_active(struct super_block *sb)
412
{
413
	struct nfs_server *server = NFS_SB(sb);
414

415 416 417 418 419
	if (!atomic_inc_not_zero(&sb->s_active))
		return false;
	if (atomic_inc_return(&server->active) != 1)
		atomic_dec(&sb->s_active);
	return true;
420
}
421
EXPORT_SYMBOL_GPL(nfs_sb_active);
422

423
void nfs_sb_deactive(struct super_block *sb)
424 425
{
	struct nfs_server *server = NFS_SB(sb);
426 427 428

	if (atomic_dec_and_test(&server->active))
		deactivate_super(sb);
429
}
430
EXPORT_SYMBOL_GPL(nfs_sb_deactive);
431

David Howells's avatar
David Howells committed
432 433 434
/*
 * Deliver file system statistics to userspace
 */
435
int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
David Howells's avatar
David Howells committed
436
{
437
	struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells's avatar
David Howells committed
438 439
	unsigned char blockbits;
	unsigned long blockres;
440
	struct nfs_fh *fh = NFS_FH(d_inode(dentry));
441 442 443 444 445 446
	struct nfs_fsstat res;
	int error = -ENOMEM;

	res.fattr = nfs_alloc_fattr();
	if (res.fattr == NULL)
		goto out_err;
David Howells's avatar
David Howells committed
447

448
	error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
Menyhart Zoltan's avatar
Menyhart Zoltan committed
449 450 451 452 453
	if (unlikely(error == -ESTALE)) {
		struct dentry *pd_dentry;

		pd_dentry = dget_parent(dentry);
		if (pd_dentry != NULL) {
454
			nfs_zap_caches(d_inode(pd_dentry));
Menyhart Zoltan's avatar
Menyhart Zoltan committed
455 456 457
			dput(pd_dentry);
		}
	}
458
	nfs_free_fattr(res.fattr);
David Howells's avatar
David Howells committed
459 460
	if (error < 0)
		goto out_err;
461

462
	buf->f_type = NFS_SUPER_MAGIC;
David Howells's avatar
David Howells committed
463 464 465 466 467 468

	/*
	 * Current versions of glibc do not correctly handle the
	 * case where f_frsize != f_bsize.  Eventually we want to
	 * report the value of wtmult in this field.
	 */
469
	buf->f_frsize = dentry->d_sb->s_blocksize;
David Howells's avatar
David Howells committed
470 471 472 473 474 475 476 477

	/*
	 * On most *nix systems, f_blocks, f_bfree, and f_bavail
	 * are reported in units of f_frsize.  Linux hasn't had
	 * an f_frsize field in its statfs struct until recently,
	 * thus historically Linux's sys_statfs reports these
	 * fields in units of f_bsize.
	 */
478 479
	buf->f_bsize = dentry->d_sb->s_blocksize;
	blockbits = dentry->d_sb->s_blocksize_bits;
David Howells's avatar
David Howells committed
480 481 482 483 484 485 486 487 488
	blockres = (1 << blockbits) - 1;
	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
	buf->f_bavail = (res.abytes + blockres) >> blockbits;

	buf->f_files = res.tfiles;
	buf->f_ffree = res.afiles;

	buf->f_namelen = server->namelen;
489

David Howells's avatar
David Howells committed
490 491 492
	return 0;

 out_err:
493
	dprintk("%s: statfs error = %d\n", __func__, -error);
494
	return error;
David Howells's avatar
David Howells committed
495
}
496
EXPORT_SYMBOL_GPL(nfs_statfs);
David Howells's avatar
David Howells committed
497

498 499 500
/*
 * Map the security flavour number to a name
 */
501 502
static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
{
503
	static const struct {
504 505
		rpc_authflavor_t flavour;
		const char *str;
506 507
	} sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
		/* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
508 509 510 511 512 513 514 515 516 517 518
		{ RPC_AUTH_NULL, "null" },
		{ RPC_AUTH_UNIX, "sys" },
		{ RPC_AUTH_GSS_KRB5, "krb5" },
		{ RPC_AUTH_GSS_KRB5I, "krb5i" },
		{ RPC_AUTH_GSS_KRB5P, "krb5p" },
		{ RPC_AUTH_GSS_LKEY, "lkey" },
		{ RPC_AUTH_GSS_LKEYI, "lkeyi" },
		{ RPC_AUTH_GSS_LKEYP, "lkeyp" },
		{ RPC_AUTH_GSS_SPKM, "spkm" },
		{ RPC_AUTH_GSS_SPKMI, "spkmi" },
		{ RPC_AUTH_GSS_SPKMP, "spkmp" },
519
		{ UINT_MAX, "unknown" }
520 521 522
	};
	int i;

523
	for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
524 525 526 527 528 529
		if (sec_flavours[i].flavour == flavour)
			break;
	}
	return sec_flavours[i].str;
}

530 531 532 533
static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
				  int showdefaults)
{
	struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
NeilBrown's avatar
NeilBrown committed
534
	char *proto = NULL;
535 536 537 538 539

	switch (sap->sa_family) {
	case AF_INET:
		switch (nfss->mountd_protocol) {
		case IPPROTO_UDP:
NeilBrown's avatar
NeilBrown committed
540
			proto = RPCBIND_NETID_UDP;
541 542
			break;
		case IPPROTO_TCP:
NeilBrown's avatar
NeilBrown committed
543
			proto = RPCBIND_NETID_TCP;
544 545 546 547 548 549
			break;
		}
		break;
	case AF_INET6:
		switch (nfss->mountd_protocol) {
		case IPPROTO_UDP:
NeilBrown's avatar
NeilBrown committed
550
			proto = RPCBIND_NETID_UDP6;
551 552
			break;
		case IPPROTO_TCP:
NeilBrown's avatar
NeilBrown committed
553
			proto = RPCBIND_NETID_TCP6;
554 555 556 557
			break;
		}
		break;
	}
NeilBrown's avatar
NeilBrown committed
558 559
	if (proto || showdefaults)
		seq_printf(m, ",mountproto=%s", proto ?: "auto");
560 561
}

562 563 564 565 566
static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
				    int showdefaults)
{
	struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;

567 568 569
	if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
		return;

570 571 572
	switch (sap->sa_family) {
	case AF_INET: {
		struct sockaddr_in *sin = (struct sockaddr_in *)sap;
Harvey Harrison's avatar
Harvey Harrison committed
573
		seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
574 575 576 577
		break;
	}
	case AF_INET6: {
		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
578
		seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
579 580 581 582 583 584 585 586 587
		break;
	}
	default:
		if (showdefaults)
			seq_printf(m, ",mountaddr=unspecified");
	}

	if (nfss->mountd_version || showdefaults)
		seq_printf(m, ",mountvers=%u", nfss->mountd_version);
588 589 590
	if ((nfss->mountd_port &&
		nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
		showdefaults)
591 592
		seq_printf(m, ",mountport=%u", nfss->mountd_port);

593
	nfs_show_mountd_netid(m, nfss, showdefaults);
594 595
}

596
#if IS_ENABLED(CONFIG_NFS_V4)
597 598 599 600 601 602 603 604 605 606 607 608 609 610
static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
				    int showdefaults)
{
	struct nfs_client *clp = nfss->nfs_client;

	seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
}
#else
static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
				    int showdefaults)
{
}
#endif

611 612 613 614 615 616 617 618 619
static void nfs_show_nfs_version(struct seq_file *m,
		unsigned int version,
		unsigned int minorversion)
{
	seq_printf(m, ",vers=%u", version);
	if (version == 4)
		seq_printf(m, ".%u", minorversion);
}

David Howells's avatar
David Howells committed
620 621 622
/*
 * Describe the mount options in force on this server representation
 */
623 624
static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
				   int showdefaults)
David Howells's avatar
David Howells committed
625
{
626
	static const struct proc_nfs_info {
David Howells's avatar
David Howells committed
627
		int flag;
628 629
		const char *str;
		const char *nostr;
David Howells's avatar
David Howells committed
630 631
	} nfs_info[] = {
		{ NFS_MOUNT_SOFT, ",soft", ",hard" },
632
		{ NFS_MOUNT_POSIX, ",posix", "" },
David Howells's avatar
David Howells committed
633 634 635 636
		{ NFS_MOUNT_NOCTO, ",nocto", "" },
		{ NFS_MOUNT_NOAC, ",noac", "" },
		{ NFS_MOUNT_NONLM, ",nolock", "" },
		{ NFS_MOUNT_NOACL, ",noacl", "" },
637
		{ NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
638 639
		{ NFS_MOUNT_UNSHARED, ",nosharecache", "" },
		{ NFS_MOUNT_NORESVPORT, ",noresvport", "" },
David Howells's avatar
David Howells committed
640 641
		{ 0, NULL, NULL }
	};
642
	const struct proc_nfs_info *nfs_infop;
643
	struct nfs_client *clp = nfss->nfs_client;
644
	u32 version = clp->rpc_ops->version;
645
	int local_flock, local_fcntl;
David Howells's avatar
David Howells committed
646

647
	nfs_show_nfs_version(m, version, clp->cl_minorversion);
648 649
	seq_printf(m, ",rsize=%u", nfss->rsize);
	seq_printf(m, ",wsize=%u", nfss->wsize);
650 651 652
	if (nfss->bsize != 0)
		seq_printf(m, ",bsize=%u", nfss->bsize);
	seq_printf(m, ",namlen=%u", nfss->namelen);
653
	if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
654
		seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
655
	if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
656
		seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
657
	if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
658
		seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
659
	if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
660
		seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
David Howells's avatar
David Howells committed
661 662 663 664 665 666
	for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
		if (nfss->flags & nfs_infop->flag)
			seq_puts(m, nfs_infop->str);
		else
			seq_puts(m, nfs_infop->nostr);
	}
667
	rcu_read_lock();
668
	seq_printf(m, ",proto=%s",
669
		   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
670
	rcu_read_unlock();
671 672 673 674 675 676 677
	if (version == 4) {
		if (nfss->port != NFS_PORT)
			seq_printf(m, ",port=%u", nfss->port);
	} else
		if (nfss->port)
			seq_printf(m, ",port=%u", nfss->port);

678 679
	seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
	seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
680
	seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
681 682 683

	if (version != 4)
		nfs_show_mountd_options(m, nfss, showdefaults);
684 685
	else
		nfs_show_nfsv4_options(m, nfss, showdefaults);
686

687 688
	if (nfss->options & NFS_OPTION_FSCACHE)
		seq_printf(m, ",fsc");
689

690 691 692
	if (nfss->options & NFS_OPTION_MIGRATION)
		seq_printf(m, ",migration");

693 694 695 696 697 698
	if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
		if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
			seq_printf(m, ",lookupcache=none");
		else
			seq_printf(m, ",lookupcache=pos");
	}
699 700 701 702 703 704 705 706 707 708 709 710

	local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
	local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;

	if (!local_flock && !local_fcntl)
		seq_printf(m, ",local_lock=none");
	else if (local_flock && local_fcntl)
		seq_printf(m, ",local_lock=all");
	else if (local_flock)
		seq_printf(m, ",local_lock=flock");
	else
		seq_printf(m, ",local_lock=posix");
David Howells's avatar
David Howells committed
711 712 713 714 715
}

/*
 * Describe the mount options on this VFS mountpoint
 */
716
int nfs_show_options(struct seq_file *m, struct dentry *root)
David Howells's avatar
David Howells committed
717
{
718
	struct nfs_server *nfss = NFS_SB(root->d_sb);
David Howells's avatar
David Howells committed
719 720 721

	nfs_show_mount_options(m, nfss, 0);

722
	rcu_read_lock();
723 724 725
	seq_printf(m, ",addr=%s",
			rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
							RPC_DISPLAY_ADDR));
726
	rcu_read_unlock();
David Howells's avatar
David Howells committed
727 728 729

	return 0;
}
730
EXPORT_SYMBOL_GPL(nfs_show_options);
731

732
#if IS_ENABLED(CONFIG_NFS_V4)
J. Bruce Fields's avatar
J. Bruce Fields committed
733
#ifdef CONFIG_NFS_V4_1
734
static void show_sessions(struct seq_file *m, struct nfs_server *server)
J. Bruce Fields's avatar
J. Bruce Fields committed
735 736 737 738 739
{
	if (nfs4_has_session(server->nfs_client))
		seq_printf(m, ",sessions");
}
#else
740 741
static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
#endif
J. Bruce Fields's avatar
J. Bruce Fields committed
742 743 744
#endif

#ifdef CONFIG_NFS_V4_1
745
static void show_pnfs(struct seq_file *m, struct nfs_server *server)
J. Bruce Fields's avatar
J. Bruce Fields committed
746 747 748 749 750 751 752
{
	seq_printf(m, ",pnfs=");
	if (server->pnfs_curr_ld)
		seq_printf(m, "%s", server->pnfs_curr_ld->name);
	else
		seq_printf(m, "not configured");
}
753 754 755

static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
{
756 757
	if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
		struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
758 759 760 761 762 763
		seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
			   "date='%llu,%u'",
			   impl_id->name, impl_id->domain,
			   impl_id->date.seconds, impl_id->date.nseconds);
	}
}
764
#else
765
#if IS_ENABLED(CONFIG_NFS_V4)
766 767 768
static void show_pnfs(struct seq_file *m, struct nfs_server *server)
{
}
769
#endif
770 771 772
static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
{
}
773
#endif
David Howells's avatar
David Howells committed
774

775
int nfs_show_devname(struct seq_file *m, struct dentry *root)
776 777 778 779 780 781
{
	char *page = (char *) __get_free_page(GFP_KERNEL);
	char *devname, *dummy;
	int err = 0;
	if (!page)
		return -ENOMEM;
782
	devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
783 784 785 786 787 788 789
	if (IS_ERR(devname))
		err = PTR_ERR(devname);
	else
		seq_escape(m, devname, " \t\n\\");
	free_page((unsigned long)page);
	return err;
}
790
EXPORT_SYMBOL_GPL(nfs_show_devname);
791

792
int nfs_show_path(struct seq_file *m, struct dentry *dentry)
793 794 795 796
{
	seq_puts(m, "/");
	return 0;
}
797
EXPORT_SYMBOL_GPL(nfs_show_path);
798

David Howells's avatar
David Howells committed
799 800 801
/*
 * Present statistical information for this VFS mountpoint
 */
802
int nfs_show_stats(struct seq_file *m, struct dentry *root)
David Howells's avatar
David Howells committed
803 804
{
	int i, cpu;
805
	struct nfs_server *nfss = NFS_SB(root->d_sb);
David Howells's avatar
David Howells committed
806 807 808 809 810 811 812 813 814
	struct rpc_auth *auth = nfss->client->cl_auth;
	struct nfs_iostats totals = { };

	seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);

	/*
	 * Display all mount option settings
	 */
	seq_printf(m, "\n\topts:\t");
815
	seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
816 817 818
	seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
	seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
	seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
David Howells's avatar
David Howells committed
819 820 821 822
	nfs_show_mount_options(m, nfss, 1);

	seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);

823
	show_implementation_id(m, nfss);
824

David Howells's avatar
David Howells committed
825 826
	seq_printf(m, "\n\tcaps:\t");
	seq_printf(m, "caps=0x%x", nfss->caps);
827 828 829 830
	seq_printf(m, ",wtmult=%u", nfss->wtmult);
	seq_printf(m, ",dtsize=%u", nfss->dtsize);
	seq_printf(m, ",bsize=%u", nfss->bsize);
	seq_printf(m, ",namlen=%u", nfss->namelen);
David Howells's avatar
David Howells committed
831

832
#if IS_ENABLED(CONFIG_NFS_V4)
833
	if (nfss->nfs_client->rpc_ops->version == 4) {
David Howells's avatar
David Howells committed
834 835 836
		seq_printf(m, "\n\tnfsv4:\t");
		seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
		seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
837
		seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
David Howells's avatar
David Howells committed
838
		seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
J. Bruce Fields's avatar
J. Bruce Fields committed
839 840
		show_sessions(m, nfss);
		show_pnfs(m, nfss);
David Howells's avatar
David Howells committed
841 842 843 844 845 846
	}
#endif

	/*
	 * Display security flavor in effect for this mount
	 */
847
	seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
David Howells's avatar
David Howells committed
848
	if (auth->au_flavor)
849
		seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
David Howells's avatar
David Howells committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863

	/*
	 * Display superblock I/O counters
	 */
	for_each_possible_cpu(cpu) {
		struct nfs_iostats *stats;

		preempt_disable();
		stats = per_cpu_ptr(nfss->io_stats, cpu);

		for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
			totals.events[i] += stats->events[i];
		for (i = 0; i < __NFSIOS_BYTESMAX; i++)
			totals.bytes[i] += stats->bytes[i];
864 865 866 867
#ifdef CONFIG_NFS_FSCACHE
		for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
			totals.fscache[i] += stats->fscache[i];
#endif
David Howells's avatar
David Howells committed
868 869 870 871 872 873 874 875 876 877

		preempt_enable();
	}

	seq_printf(m, "\n\tevents:\t");
	for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
		seq_printf(m, "%lu ", totals.events[i]);
	seq_printf(m, "\n\tbytes:\t");
	for (i = 0; i < __NFSIOS_BYTESMAX; i++)
		seq_printf(m, "%Lu ", totals.bytes[i]);
878 879 880 881
#ifdef CONFIG_NFS_FSCACHE
	if (nfss->options & NFS_OPTION_FSCACHE) {
		seq_printf(m, "\n\tfsc:\t");
		for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
882
			seq_printf(m, "%Lu ", totals.fscache[i]);
883 884
	}
#endif
David Howells's avatar
David Howells committed
885 886 887 888 889 890
	seq_printf(m, "\n");

	rpc_print_iostats(m, nfss->client);

	return 0;
}
891
EXPORT_SYMBOL_GPL(nfs_show_stats);
David Howells's avatar
David Howells committed
892 893 894

/*
 * Begin unmount by attempting to remove all automounted mountpoints we added
895
 * in response to xdev traversals and referrals
David Howells's avatar
David Howells committed
896
 */
897
void nfs_umount_begin(struct super_block *sb)
David Howells's avatar
David Howells committed
898
{
899
	struct nfs_server *server;
900 901
	struct rpc_clnt *rpc;

902
	server = NFS_SB(sb);
903 904 905 906 907 908 909
	/* -EIO all pending I/O */
	rpc = server->client_acl;
	if (!IS_ERR(rpc))
		rpc_killall_tasks(rpc);
	rpc = server->client;
	if (!IS_ERR(rpc))
		rpc_killall_tasks(rpc);
David Howells's avatar
David Howells committed
910
}
911
EXPORT_SYMBOL_GPL(nfs_umount_begin);
David Howells's avatar
David Howells committed
912

913
static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(void)
914 915 916 917 918
{
	struct nfs_parsed_mount_data *data;

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (data) {
919 920
		data->timeo		= NFS_UNSPEC_TIMEO;
		data->retrans		= NFS_UNSPEC_RETRANS;
921 922 923 924
		data->acregmin		= NFS_DEF_ACREGMIN;
		data->acregmax		= NFS_DEF_ACREGMAX;
		data->acdirmin		= NFS_DEF_ACDIRMIN;
		data->acdirmax		= NFS_DEF_ACDIRMAX;
925
		data->mount_server.port	= NFS_UNSPEC_PORT;
926
		data->nfs_server.port	= NFS_UNSPEC_PORT;
Trond Myklebust's avatar