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 */
nfs_init_server_aclclient(struct nfs_server * server)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
nfs_init_server_aclclient(struct nfs_server * server)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
nfs3_create_server(struct fs_context * fc)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
nfs3_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)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 */
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)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 unsigned long connect_timeout = ds_timeo * (ds_retrans + 1) * HZ / 10;
90 struct nfs_client *mds_clp = mds_srv->nfs_client;
91 struct nfs_client_initdata cl_init = {
92 .addr = ds_addr,
93 .addrlen = ds_addrlen,
94 .nodename = mds_clp->cl_rpcclient->cl_nodename,
95 .ip_addr = mds_clp->cl_ipaddr,
96 .nfs_mod = &nfs_v3,
97 .proto = ds_proto,
98 .net = mds_clp->cl_net,
99 .timeparms = &ds_timeout,
100 .cred = mds_srv->cred,
101 .xprtsec = mds_clp->cl_xprtsec,
102 .connect_timeout = connect_timeout,
103 .reconnect_timeout = connect_timeout,
104 };
105 struct nfs_client *clp;
106 char buf[INET6_ADDRSTRLEN + 1];
107
108 /* fake a hostname because lockd wants it */
109 if (rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf)) <= 0)
110 return ERR_PTR(-EINVAL);
111 cl_init.hostname = buf;
112
113 switch (ds_proto) {
114 case XPRT_TRANSPORT_RDMA:
115 case XPRT_TRANSPORT_TCP:
116 case XPRT_TRANSPORT_TCP_TLS:
117 if (mds_clp->cl_nconnect > 1)
118 cl_init.nconnect = mds_clp->cl_nconnect;
119 }
120
121 if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
122 __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
123
124 __set_bit(NFS_CS_DS, &cl_init.init_flags);
125
126 /* Use the MDS nfs_client cl_ipaddr. */
127 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
128 clp = nfs_get_client(&cl_init);
129
130 return clp;
131 }
132 EXPORT_SYMBOL_GPL(nfs3_set_ds_client);
133