1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/nfs_fs.h>
3 #include <linux/nfs_mount.h>
4 #include <linux/sunrpc/addr.h>
5 #include "internal.h"
6 #include "nfs3_fs.h"
7 
8 #ifdef CONFIG_NFS_V3_ACL
9 static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
10 static const struct rpc_version *nfsacl_version[] = {
11 	[3]			= &nfsacl_version3,
12 };
13 
14 const struct rpc_program nfsacl_program = {
15 	.name			= "nfsacl",
16 	.number			= NFS_ACL_PROGRAM,
17 	.nrvers			= ARRAY_SIZE(nfsacl_version),
18 	.version		= nfsacl_version,
19 	.stats			= &nfsacl_rpcstat,
20 };
21 
22 /*
23  * Initialise an NFSv3 ACL client connection
24  */
nfs_init_server_aclclient(struct nfs_server * server)25 static void nfs_init_server_aclclient(struct nfs_server *server)
26 {
27 	if (server->flags & NFS_MOUNT_NOACL)
28 		goto out_noacl;
29 
30 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
31 	if (IS_ERR(server->client_acl))
32 		goto out_noacl;
33 
34 	/* No errors! Assume that Sun nfsacls are supported */
35 	server->caps |= NFS_CAP_ACLS;
36 	return;
37 
38 out_noacl:
39 	server->caps &= ~NFS_CAP_ACLS;
40 }
41 #else
nfs_init_server_aclclient(struct nfs_server * server)42 static inline void nfs_init_server_aclclient(struct nfs_server *server)
43 {
44 	server->flags &= ~NFS_MOUNT_NOACL;
45 	server->caps &= ~NFS_CAP_ACLS;
46 }
47 #endif
48 
nfs3_create_server(struct fs_context * fc)49 struct nfs_server *nfs3_create_server(struct fs_context *fc)
50 {
51 	struct nfs_server *server = nfs_create_server(fc);
52 
53 	/* Create a client RPC handle for the NFS v3 ACL management interface */
54 	if (!IS_ERR(server))
55 		nfs_init_server_aclclient(server);
56 	return server;
57 }
58 
nfs3_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)59 struct nfs_server *nfs3_clone_server(struct nfs_server *source,
60 				     struct nfs_fh *fh,
61 				     struct nfs_fattr *fattr,
62 				     rpc_authflavor_t flavor)
63 {
64 	struct nfs_server *server = nfs_clone_server(source, fh, fattr, flavor);
65 	if (!IS_ERR(server) && !IS_ERR(source->client_acl))
66 		nfs_init_server_aclclient(server);
67 	return server;
68 }
69 
70 /*
71  * Set up a pNFS Data Server client over NFSv3.
72  *
73  * Return any existing nfs_client that matches server address,port,version
74  * and minorversion.
75  *
76  * For a new nfs_client, use a soft mount (default), a low retrans and a
77  * low timeout interval so that if a connection is lost, we retry through
78  * the MDS.
79  */
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)80 struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv,
81 		const struct sockaddr_storage *ds_addr, int ds_addrlen,
82 		int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
83 {
84 	struct rpc_timeout ds_timeout;
85 	struct nfs_client *mds_clp = mds_srv->nfs_client;
86 	struct nfs_client_initdata cl_init = {
87 		.addr = ds_addr,
88 		.addrlen = ds_addrlen,
89 		.nodename = mds_clp->cl_rpcclient->cl_nodename,
90 		.ip_addr = mds_clp->cl_ipaddr,
91 		.nfs_mod = &nfs_v3,
92 		.proto = ds_proto,
93 		.net = mds_clp->cl_net,
94 		.timeparms = &ds_timeout,
95 		.cred = mds_srv->cred,
96 	};
97 	struct nfs_client *clp;
98 	char buf[INET6_ADDRSTRLEN + 1];
99 
100 	/* fake a hostname because lockd wants it */
101 	if (rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf)) <= 0)
102 		return ERR_PTR(-EINVAL);
103 	cl_init.hostname = buf;
104 
105 	if (mds_clp->cl_nconnect > 1 && ds_proto == XPRT_TRANSPORT_TCP)
106 		cl_init.nconnect = mds_clp->cl_nconnect;
107 
108 	if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
109 		__set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
110 
111 	__set_bit(NFS_CS_DS, &cl_init.init_flags);
112 
113 	/* Use the MDS nfs_client cl_ipaddr. */
114 	nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
115 	clp = nfs_get_client(&cl_init);
116 
117 	return clp;
118 }
119 EXPORT_SYMBOL_GPL(nfs3_set_ds_client);
120