xref: /openbmc/linux/fs/nfs/nfs3client.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21179acc6SBryan Schumaker #include <linux/nfs_fs.h>
31179acc6SBryan Schumaker #include <linux/nfs_mount.h>
436d3e3dcSPeng Tao #include <linux/sunrpc/addr.h>
51179acc6SBryan Schumaker #include "internal.h"
63fc3edf1STrond Myklebust #include "nfs3_fs.h"
7f4057ffdSBenjamin Coddington #include "netns.h"
8f4057ffdSBenjamin Coddington #include "sysfs.h"
91179acc6SBryan Schumaker 
101179acc6SBryan Schumaker #ifdef CONFIG_NFS_V3_ACL
111179acc6SBryan Schumaker static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
121179acc6SBryan Schumaker static const struct rpc_version *nfsacl_version[] = {
131179acc6SBryan Schumaker 	[3]			= &nfsacl_version3,
141179acc6SBryan Schumaker };
151179acc6SBryan Schumaker 
161179acc6SBryan Schumaker const struct rpc_program nfsacl_program = {
171179acc6SBryan Schumaker 	.name			= "nfsacl",
181179acc6SBryan Schumaker 	.number			= NFS_ACL_PROGRAM,
191179acc6SBryan Schumaker 	.nrvers			= ARRAY_SIZE(nfsacl_version),
201179acc6SBryan Schumaker 	.version		= nfsacl_version,
211179acc6SBryan Schumaker 	.stats			= &nfsacl_rpcstat,
221179acc6SBryan Schumaker };
231179acc6SBryan Schumaker 
241179acc6SBryan Schumaker /*
251179acc6SBryan Schumaker  * Initialise an NFSv3 ACL client connection
261179acc6SBryan Schumaker  */
nfs_init_server_aclclient(struct nfs_server * server)271179acc6SBryan Schumaker static void nfs_init_server_aclclient(struct nfs_server *server)
281179acc6SBryan Schumaker {
291179acc6SBryan Schumaker 	if (server->flags & NFS_MOUNT_NOACL)
301179acc6SBryan Schumaker 		goto out_noacl;
311179acc6SBryan Schumaker 
321179acc6SBryan Schumaker 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
331179acc6SBryan Schumaker 	if (IS_ERR(server->client_acl))
341179acc6SBryan Schumaker 		goto out_noacl;
351179acc6SBryan Schumaker 
36f4057ffdSBenjamin Coddington 	nfs_sysfs_link_rpc_client(server, server->client_acl, NULL);
37f4057ffdSBenjamin Coddington 
381179acc6SBryan Schumaker 	/* No errors! Assume that Sun nfsacls are supported */
391179acc6SBryan Schumaker 	server->caps |= NFS_CAP_ACLS;
401179acc6SBryan Schumaker 	return;
411179acc6SBryan Schumaker 
421179acc6SBryan Schumaker out_noacl:
431179acc6SBryan Schumaker 	server->caps &= ~NFS_CAP_ACLS;
441179acc6SBryan Schumaker }
451179acc6SBryan Schumaker #else
nfs_init_server_aclclient(struct nfs_server * server)461179acc6SBryan Schumaker static inline void nfs_init_server_aclclient(struct nfs_server *server)
471179acc6SBryan Schumaker {
481179acc6SBryan Schumaker 	server->flags &= ~NFS_MOUNT_NOACL;
491179acc6SBryan Schumaker 	server->caps &= ~NFS_CAP_ACLS;
501179acc6SBryan Schumaker }
511179acc6SBryan Schumaker #endif
521179acc6SBryan Schumaker 
nfs3_create_server(struct fs_context * fc)5362a55d08SScott Mayhew struct nfs_server *nfs3_create_server(struct fs_context *fc)
541179acc6SBryan Schumaker {
5562a55d08SScott Mayhew 	struct nfs_server *server = nfs_create_server(fc);
5662a55d08SScott Mayhew 
571179acc6SBryan Schumaker 	/* Create a client RPC handle for the NFS v3 ACL management interface */
581179acc6SBryan Schumaker 	if (!IS_ERR(server))
591179acc6SBryan Schumaker 		nfs_init_server_aclclient(server);
601179acc6SBryan Schumaker 	return server;
611179acc6SBryan Schumaker }
621179acc6SBryan Schumaker 
nfs3_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)631179acc6SBryan Schumaker struct nfs_server *nfs3_clone_server(struct nfs_server *source,
641179acc6SBryan Schumaker 				     struct nfs_fh *fh,
651179acc6SBryan Schumaker 				     struct nfs_fattr *fattr,
661179acc6SBryan Schumaker 				     rpc_authflavor_t flavor)
671179acc6SBryan Schumaker {
681179acc6SBryan Schumaker 	struct nfs_server *server = nfs_clone_server(source, fh, fattr, flavor);
691179acc6SBryan Schumaker 	if (!IS_ERR(server) && !IS_ERR(source->client_acl))
701179acc6SBryan Schumaker 		nfs_init_server_aclclient(server);
711179acc6SBryan Schumaker 	return server;
721179acc6SBryan Schumaker }
731a04c6e1SPeng Tao 
741a04c6e1SPeng Tao /*
751a04c6e1SPeng Tao  * Set up a pNFS Data Server client over NFSv3.
761a04c6e1SPeng Tao  *
771a04c6e1SPeng Tao  * Return any existing nfs_client that matches server address,port,version
781a04c6e1SPeng Tao  * and minorversion.
791a04c6e1SPeng Tao  *
801a04c6e1SPeng Tao  * For a new nfs_client, use a soft mount (default), a low retrans and a
811a04c6e1SPeng Tao  * low timeout interval so that if a connection is lost, we retry through
821a04c6e1SPeng Tao  * the MDS.
831a04c6e1SPeng Tao  */
nfs3_set_ds_client(struct nfs_server * mds_srv,const struct sockaddr_storage * ds_addr,int ds_addrlen,int ds_proto,unsigned int ds_timeo,unsigned int ds_retrans)84b224f7cbSTigran Mkrtchyan struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv,
85cf0d7e7fSKees Cook 		const struct sockaddr_storage *ds_addr, int ds_addrlen,
867d38de3fSAnna Schumaker 		int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
871a04c6e1SPeng Tao {
885c6e5b60STrond Myklebust 	struct rpc_timeout ds_timeout;
89*537935f7STrond Myklebust 	unsigned long connect_timeout = ds_timeo * (ds_retrans + 1) * HZ / 10;
90b224f7cbSTigran Mkrtchyan 	struct nfs_client *mds_clp = mds_srv->nfs_client;
911a04c6e1SPeng Tao 	struct nfs_client_initdata cl_init = {
921a04c6e1SPeng Tao 		.addr = ds_addr,
931a04c6e1SPeng Tao 		.addrlen = ds_addrlen,
945c6e5b60STrond Myklebust 		.nodename = mds_clp->cl_rpcclient->cl_nodename,
955c6e5b60STrond Myklebust 		.ip_addr = mds_clp->cl_ipaddr,
961a04c6e1SPeng Tao 		.nfs_mod = &nfs_v3,
971a04c6e1SPeng Tao 		.proto = ds_proto,
981a04c6e1SPeng Tao 		.net = mds_clp->cl_net,
995c6e5b60STrond Myklebust 		.timeparms = &ds_timeout,
1001a58e8a0STrond Myklebust 		.cred = mds_srv->cred,
1016c0a8c5fSChuck Lever 		.xprtsec = mds_clp->cl_xprtsec,
102*537935f7STrond Myklebust 		.connect_timeout = connect_timeout,
103*537935f7STrond Myklebust 		.reconnect_timeout = connect_timeout,
1041a04c6e1SPeng Tao 	};
1051a04c6e1SPeng Tao 	struct nfs_client *clp;
10636d3e3dcSPeng Tao 	char buf[INET6_ADDRSTRLEN + 1];
10736d3e3dcSPeng Tao 
10836d3e3dcSPeng Tao 	/* fake a hostname because lockd wants it */
109cf0d7e7fSKees Cook 	if (rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf)) <= 0)
11036d3e3dcSPeng Tao 		return ERR_PTR(-EINVAL);
11136d3e3dcSPeng Tao 	cl_init.hostname = buf;
1121a04c6e1SPeng Tao 
113c8407f2eSChuck Lever 	switch (ds_proto) {
114c8407f2eSChuck Lever 	case XPRT_TRANSPORT_TCP:
115c8407f2eSChuck Lever 	case XPRT_TRANSPORT_TCP_TLS:
116c8407f2eSChuck Lever 		if (mds_clp->cl_nconnect > 1)
117bb71e4a5STrond Myklebust 			cl_init.nconnect = mds_clp->cl_nconnect;
118c8407f2eSChuck Lever 	}
119bb71e4a5STrond Myklebust 
120b224f7cbSTigran Mkrtchyan 	if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
121d0372b67STrond Myklebust 		__set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
122b224f7cbSTigran Mkrtchyan 
12352f98f1aSTrond Myklebust 	__set_bit(NFS_CS_DS, &cl_init.init_flags);
124c6eb5843STrond Myklebust 
1251a04c6e1SPeng Tao 	/* Use the MDS nfs_client cl_ipaddr. */
1261a04c6e1SPeng Tao 	nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
1277d38de3fSAnna Schumaker 	clp = nfs_get_client(&cl_init);
1281a04c6e1SPeng Tao 
1291a04c6e1SPeng Tao 	return clp;
1301a04c6e1SPeng Tao }
1311a04c6e1SPeng Tao EXPORT_SYMBOL_GPL(nfs3_set_ds_client);
132