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