xref: /OK3568_Linux_fs/kernel/fs/nfs/nfs4namespace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * linux/fs/nfs/nfs4namespace.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
6*4882a593Smuzhiyun  * - Modified by David Howells <dhowells@redhat.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * NFSv4 namespace
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/dcache.h>
13*4882a593Smuzhiyun #include <linux/mount.h>
14*4882a593Smuzhiyun #include <linux/namei.h>
15*4882a593Smuzhiyun #include <linux/nfs_fs.h>
16*4882a593Smuzhiyun #include <linux/nfs_mount.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/string.h>
19*4882a593Smuzhiyun #include <linux/sunrpc/clnt.h>
20*4882a593Smuzhiyun #include <linux/sunrpc/addr.h>
21*4882a593Smuzhiyun #include <linux/vfs.h>
22*4882a593Smuzhiyun #include <linux/inet.h>
23*4882a593Smuzhiyun #include "internal.h"
24*4882a593Smuzhiyun #include "nfs4_fs.h"
25*4882a593Smuzhiyun #include "nfs.h"
26*4882a593Smuzhiyun #include "dns_resolve.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define NFSDBG_FACILITY		NFSDBG_VFS
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * Work out the length that an NFSv4 path would render to as a standard posix
32*4882a593Smuzhiyun  * path, with a leading slash but no terminating slash.
33*4882a593Smuzhiyun  */
nfs4_pathname_len(const struct nfs4_pathname * pathname)34*4882a593Smuzhiyun static ssize_t nfs4_pathname_len(const struct nfs4_pathname *pathname)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	ssize_t len = 0;
37*4882a593Smuzhiyun 	int i;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	for (i = 0; i < pathname->ncomponents; i++) {
40*4882a593Smuzhiyun 		const struct nfs4_string *component = &pathname->components[i];
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 		if (component->len > NAME_MAX)
43*4882a593Smuzhiyun 			goto too_long;
44*4882a593Smuzhiyun 		len += 1 + component->len; /* Adding "/foo" */
45*4882a593Smuzhiyun 		if (len > PATH_MAX)
46*4882a593Smuzhiyun 			goto too_long;
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 	return len;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun too_long:
51*4882a593Smuzhiyun 	return -ENAMETOOLONG;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * Convert the NFSv4 pathname components into a standard posix path.
56*4882a593Smuzhiyun  */
nfs4_pathname_string(const struct nfs4_pathname * pathname,unsigned short * _len)57*4882a593Smuzhiyun static char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
58*4882a593Smuzhiyun 				  unsigned short *_len)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	ssize_t len;
61*4882a593Smuzhiyun 	char *buf, *p;
62*4882a593Smuzhiyun 	int i;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	len = nfs4_pathname_len(pathname);
65*4882a593Smuzhiyun 	if (len < 0)
66*4882a593Smuzhiyun 		return ERR_PTR(len);
67*4882a593Smuzhiyun 	*_len = len;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	p = buf = kmalloc(len + 1, GFP_KERNEL);
70*4882a593Smuzhiyun 	if (!buf)
71*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	for (i = 0; i < pathname->ncomponents; i++) {
74*4882a593Smuzhiyun 		const struct nfs4_string *component = &pathname->components[i];
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 		*p++ = '/';
77*4882a593Smuzhiyun 		memcpy(p, component->data, component->len);
78*4882a593Smuzhiyun 		p += component->len;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	*p = 0;
82*4882a593Smuzhiyun 	return buf;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun  * return the path component of "<server>:<path>"
87*4882a593Smuzhiyun  *  nfspath - the "<server>:<path>" string
88*4882a593Smuzhiyun  *  end - one past the last char that could contain "<server>:"
89*4882a593Smuzhiyun  * returns NULL on failure
90*4882a593Smuzhiyun  */
nfs_path_component(const char * nfspath,const char * end)91*4882a593Smuzhiyun static char *nfs_path_component(const char *nfspath, const char *end)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	char *p;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (*nfspath == '[') {
96*4882a593Smuzhiyun 		/* parse [] escaped IPv6 addrs */
97*4882a593Smuzhiyun 		p = strchr(nfspath, ']');
98*4882a593Smuzhiyun 		if (p != NULL && ++p < end && *p == ':')
99*4882a593Smuzhiyun 			return p + 1;
100*4882a593Smuzhiyun 	} else {
101*4882a593Smuzhiyun 		/* otherwise split on first colon */
102*4882a593Smuzhiyun 		p = strchr(nfspath, ':');
103*4882a593Smuzhiyun 		if (p != NULL && p < end)
104*4882a593Smuzhiyun 			return p + 1;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	return NULL;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun  * Determine the mount path as a string
111*4882a593Smuzhiyun  */
nfs4_path(struct dentry * dentry,char * buffer,ssize_t buflen)112*4882a593Smuzhiyun static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	char *limit;
115*4882a593Smuzhiyun 	char *path = nfs_path(&limit, dentry, buffer, buflen,
116*4882a593Smuzhiyun 			      NFS_PATH_CANONICAL);
117*4882a593Smuzhiyun 	if (!IS_ERR(path)) {
118*4882a593Smuzhiyun 		char *path_component = nfs_path_component(path, limit);
119*4882a593Smuzhiyun 		if (path_component)
120*4882a593Smuzhiyun 			return path_component;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 	return path;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
127*4882a593Smuzhiyun  * believe to be the server path to this dentry
128*4882a593Smuzhiyun  */
nfs4_validate_fspath(struct dentry * dentry,const struct nfs4_fs_locations * locations,struct nfs_fs_context * ctx)129*4882a593Smuzhiyun static int nfs4_validate_fspath(struct dentry *dentry,
130*4882a593Smuzhiyun 				const struct nfs4_fs_locations *locations,
131*4882a593Smuzhiyun 				struct nfs_fs_context *ctx)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	const char *path;
134*4882a593Smuzhiyun 	char *fs_path;
135*4882a593Smuzhiyun 	unsigned short len;
136*4882a593Smuzhiyun 	char *buf;
137*4882a593Smuzhiyun 	int n;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	buf = kmalloc(4096, GFP_KERNEL);
140*4882a593Smuzhiyun 	if (!buf)
141*4882a593Smuzhiyun 		return -ENOMEM;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	path = nfs4_path(dentry, buf, 4096);
144*4882a593Smuzhiyun 	if (IS_ERR(path)) {
145*4882a593Smuzhiyun 		kfree(buf);
146*4882a593Smuzhiyun 		return PTR_ERR(path);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	fs_path = nfs4_pathname_string(&locations->fs_path, &len);
150*4882a593Smuzhiyun 	if (IS_ERR(fs_path)) {
151*4882a593Smuzhiyun 		kfree(buf);
152*4882a593Smuzhiyun 		return PTR_ERR(fs_path);
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	n = strncmp(path, fs_path, len);
156*4882a593Smuzhiyun 	kfree(buf);
157*4882a593Smuzhiyun 	kfree(fs_path);
158*4882a593Smuzhiyun 	if (n != 0) {
159*4882a593Smuzhiyun 		dprintk("%s: path %s does not begin with fsroot %s\n",
160*4882a593Smuzhiyun 			__func__, path, ctx->nfs_server.export_path);
161*4882a593Smuzhiyun 		return -ENOENT;
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
nfs_parse_server_name(char * string,size_t len,struct sockaddr * sa,size_t salen,struct net * net,int port)167*4882a593Smuzhiyun size_t nfs_parse_server_name(char *string, size_t len, struct sockaddr *sa,
168*4882a593Smuzhiyun 			     size_t salen, struct net *net, int port)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	ssize_t ret;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	ret = rpc_pton(net, string, len, sa, salen);
173*4882a593Smuzhiyun 	if (ret == 0) {
174*4882a593Smuzhiyun 		ret = rpc_uaddr2sockaddr(net, string, len, sa, salen);
175*4882a593Smuzhiyun 		if (ret == 0) {
176*4882a593Smuzhiyun 			ret = nfs_dns_resolve_name(net, string, len, sa, salen);
177*4882a593Smuzhiyun 			if (ret < 0)
178*4882a593Smuzhiyun 				ret = 0;
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 	} else if (port) {
181*4882a593Smuzhiyun 		rpc_set_port(sa, port);
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 	return ret;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun  * nfs_find_best_sec - Find a security mechanism supported locally
188*4882a593Smuzhiyun  * @clnt: pointer to rpc_clnt
189*4882a593Smuzhiyun  * @server: NFS server struct
190*4882a593Smuzhiyun  * @flavors: List of security tuples returned by SECINFO procedure
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * Return an rpc client that uses the first security mechanism in
193*4882a593Smuzhiyun  * "flavors" that is locally supported.  The "flavors" array
194*4882a593Smuzhiyun  * is searched in the order returned from the server, per RFC 3530
195*4882a593Smuzhiyun  * recommendation and each flavor is checked for membership in the
196*4882a593Smuzhiyun  * sec= mount option list if it exists.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * Return -EPERM if no matching flavor is found in the array.
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  * Please call rpc_shutdown_client() when you are done with this rpc client.
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  */
nfs_find_best_sec(struct rpc_clnt * clnt,struct nfs_server * server,struct nfs4_secinfo_flavors * flavors)203*4882a593Smuzhiyun static struct rpc_clnt *nfs_find_best_sec(struct rpc_clnt *clnt,
204*4882a593Smuzhiyun 					  struct nfs_server *server,
205*4882a593Smuzhiyun 					  struct nfs4_secinfo_flavors *flavors)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	rpc_authflavor_t pflavor;
208*4882a593Smuzhiyun 	struct nfs4_secinfo4 *secinfo;
209*4882a593Smuzhiyun 	unsigned int i;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	for (i = 0; i < flavors->num_flavors; i++) {
212*4882a593Smuzhiyun 		secinfo = &flavors->flavors[i];
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 		switch (secinfo->flavor) {
215*4882a593Smuzhiyun 		case RPC_AUTH_NULL:
216*4882a593Smuzhiyun 		case RPC_AUTH_UNIX:
217*4882a593Smuzhiyun 		case RPC_AUTH_GSS:
218*4882a593Smuzhiyun 			pflavor = rpcauth_get_pseudoflavor(secinfo->flavor,
219*4882a593Smuzhiyun 							&secinfo->flavor_info);
220*4882a593Smuzhiyun 			/* does the pseudoflavor match a sec= mount opt? */
221*4882a593Smuzhiyun 			if (pflavor != RPC_AUTH_MAXFLAVOR &&
222*4882a593Smuzhiyun 			    nfs_auth_info_match(&server->auth_info, pflavor)) {
223*4882a593Smuzhiyun 				struct rpc_clnt *new;
224*4882a593Smuzhiyun 				struct rpc_cred *cred;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 				/* Cloning creates an rpc_auth for the flavor */
227*4882a593Smuzhiyun 				new = rpc_clone_client_set_auth(clnt, pflavor);
228*4882a593Smuzhiyun 				if (IS_ERR(new))
229*4882a593Smuzhiyun 					continue;
230*4882a593Smuzhiyun 				/**
231*4882a593Smuzhiyun 				* Check that the user actually can use the
232*4882a593Smuzhiyun 				* flavor. This is mostly for RPC_AUTH_GSS
233*4882a593Smuzhiyun 				* where cr_init obtains a gss context
234*4882a593Smuzhiyun 				*/
235*4882a593Smuzhiyun 				cred = rpcauth_lookupcred(new->cl_auth, 0);
236*4882a593Smuzhiyun 				if (IS_ERR(cred)) {
237*4882a593Smuzhiyun 					rpc_shutdown_client(new);
238*4882a593Smuzhiyun 					continue;
239*4882a593Smuzhiyun 				}
240*4882a593Smuzhiyun 				put_rpccred(cred);
241*4882a593Smuzhiyun 				return new;
242*4882a593Smuzhiyun 			}
243*4882a593Smuzhiyun 		}
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 	return ERR_PTR(-EPERM);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun  * nfs4_negotiate_security - in response to an NFS4ERR_WRONGSEC on lookup,
250*4882a593Smuzhiyun  * return an rpc_clnt that uses the best available security flavor with
251*4882a593Smuzhiyun  * respect to the secinfo flavor list and the sec= mount options.
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * @clnt: RPC client to clone
254*4882a593Smuzhiyun  * @inode: directory inode
255*4882a593Smuzhiyun  * @name: lookup name
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * Please call rpc_shutdown_client() when you are done with this rpc client.
258*4882a593Smuzhiyun  */
259*4882a593Smuzhiyun struct rpc_clnt *
nfs4_negotiate_security(struct rpc_clnt * clnt,struct inode * inode,const struct qstr * name)260*4882a593Smuzhiyun nfs4_negotiate_security(struct rpc_clnt *clnt, struct inode *inode,
261*4882a593Smuzhiyun 					const struct qstr *name)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	struct page *page;
264*4882a593Smuzhiyun 	struct nfs4_secinfo_flavors *flavors;
265*4882a593Smuzhiyun 	struct rpc_clnt *new;
266*4882a593Smuzhiyun 	int err;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	page = alloc_page(GFP_KERNEL);
269*4882a593Smuzhiyun 	if (!page)
270*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	flavors = page_address(page);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	err = nfs4_proc_secinfo(inode, name, flavors);
275*4882a593Smuzhiyun 	if (err < 0) {
276*4882a593Smuzhiyun 		new = ERR_PTR(err);
277*4882a593Smuzhiyun 		goto out;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	new = nfs_find_best_sec(clnt, NFS_SERVER(inode), flavors);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun out:
283*4882a593Smuzhiyun 	put_page(page);
284*4882a593Smuzhiyun 	return new;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
try_location(struct fs_context * fc,const struct nfs4_fs_location * location)287*4882a593Smuzhiyun static int try_location(struct fs_context *fc,
288*4882a593Smuzhiyun 			const struct nfs4_fs_location *location)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
291*4882a593Smuzhiyun 	unsigned int len, s;
292*4882a593Smuzhiyun 	char *export_path, *source, *p;
293*4882a593Smuzhiyun 	int ret = -ENOENT;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Allocate a buffer big enough to hold any of the hostnames plus a
296*4882a593Smuzhiyun 	 * terminating char and also a buffer big enough to hold the hostname
297*4882a593Smuzhiyun 	 * plus a colon plus the path.
298*4882a593Smuzhiyun 	 */
299*4882a593Smuzhiyun 	len = 0;
300*4882a593Smuzhiyun 	for (s = 0; s < location->nservers; s++) {
301*4882a593Smuzhiyun 		const struct nfs4_string *buf = &location->servers[s];
302*4882a593Smuzhiyun 		if (buf->len > len)
303*4882a593Smuzhiyun 			len = buf->len;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	kfree(ctx->nfs_server.hostname);
307*4882a593Smuzhiyun 	ctx->nfs_server.hostname = kmalloc(len + 1, GFP_KERNEL);
308*4882a593Smuzhiyun 	if (!ctx->nfs_server.hostname)
309*4882a593Smuzhiyun 		return -ENOMEM;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	export_path = nfs4_pathname_string(&location->rootpath,
312*4882a593Smuzhiyun 					   &ctx->nfs_server.export_path_len);
313*4882a593Smuzhiyun 	if (IS_ERR(export_path))
314*4882a593Smuzhiyun 		return PTR_ERR(export_path);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	kfree(ctx->nfs_server.export_path);
317*4882a593Smuzhiyun 	ctx->nfs_server.export_path = export_path;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	source = kmalloc(len + 1 + ctx->nfs_server.export_path_len + 1,
320*4882a593Smuzhiyun 			 GFP_KERNEL);
321*4882a593Smuzhiyun 	if (!source)
322*4882a593Smuzhiyun 		return -ENOMEM;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	kfree(fc->source);
325*4882a593Smuzhiyun 	fc->source = source;
326*4882a593Smuzhiyun 	for (s = 0; s < location->nservers; s++) {
327*4882a593Smuzhiyun 		const struct nfs4_string *buf = &location->servers[s];
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
330*4882a593Smuzhiyun 			continue;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 		ctx->nfs_server.addrlen =
333*4882a593Smuzhiyun 			nfs_parse_server_name(buf->data, buf->len,
334*4882a593Smuzhiyun 					      &ctx->nfs_server.address,
335*4882a593Smuzhiyun 					      sizeof(ctx->nfs_server._address),
336*4882a593Smuzhiyun 					      fc->net_ns, 0);
337*4882a593Smuzhiyun 		if (ctx->nfs_server.addrlen == 0)
338*4882a593Smuzhiyun 			continue;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 		rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		memcpy(ctx->nfs_server.hostname, buf->data, buf->len);
343*4882a593Smuzhiyun 		ctx->nfs_server.hostname[buf->len] = '\0';
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 		p = source;
346*4882a593Smuzhiyun 		memcpy(p, buf->data, buf->len);
347*4882a593Smuzhiyun 		p += buf->len;
348*4882a593Smuzhiyun 		*p++ = ':';
349*4882a593Smuzhiyun 		memcpy(p, ctx->nfs_server.export_path, ctx->nfs_server.export_path_len);
350*4882a593Smuzhiyun 		p += ctx->nfs_server.export_path_len;
351*4882a593Smuzhiyun 		*p = 0;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		ret = nfs4_get_referral_tree(fc);
354*4882a593Smuzhiyun 		if (ret == 0)
355*4882a593Smuzhiyun 			return 0;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	return ret;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /**
362*4882a593Smuzhiyun  * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
363*4882a593Smuzhiyun  * @fc: pointer to struct nfs_fs_context
364*4882a593Smuzhiyun  * @locations: array of NFSv4 server location information
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  */
nfs_follow_referral(struct fs_context * fc,const struct nfs4_fs_locations * locations)367*4882a593Smuzhiyun static int nfs_follow_referral(struct fs_context *fc,
368*4882a593Smuzhiyun 			       const struct nfs4_fs_locations *locations)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
371*4882a593Smuzhiyun 	int loc, error;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	if (locations == NULL || locations->nlocations <= 0)
374*4882a593Smuzhiyun 		return -ENOENT;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	dprintk("%s: referral at %pd2\n", __func__, ctx->clone_data.dentry);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* Ensure fs path is a prefix of current dentry path */
379*4882a593Smuzhiyun 	error = nfs4_validate_fspath(ctx->clone_data.dentry, locations, ctx);
380*4882a593Smuzhiyun 	if (error < 0)
381*4882a593Smuzhiyun 		return error;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	error = -ENOENT;
384*4882a593Smuzhiyun 	for (loc = 0; loc < locations->nlocations; loc++) {
385*4882a593Smuzhiyun 		const struct nfs4_fs_location *location = &locations->locations[loc];
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		if (location == NULL || location->nservers <= 0 ||
388*4882a593Smuzhiyun 		    location->rootpath.ncomponents == 0)
389*4882a593Smuzhiyun 			continue;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		error = try_location(fc, location);
392*4882a593Smuzhiyun 		if (error == 0)
393*4882a593Smuzhiyun 			return 0;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	return error;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun  * nfs_do_refmount - handle crossing a referral on server
401*4882a593Smuzhiyun  * @dentry - dentry of referral
402*4882a593Smuzhiyun  *
403*4882a593Smuzhiyun  */
nfs_do_refmount(struct fs_context * fc,struct rpc_clnt * client)404*4882a593Smuzhiyun static int nfs_do_refmount(struct fs_context *fc, struct rpc_clnt *client)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
407*4882a593Smuzhiyun 	struct dentry *dentry, *parent;
408*4882a593Smuzhiyun 	struct nfs4_fs_locations *fs_locations = NULL;
409*4882a593Smuzhiyun 	struct page *page;
410*4882a593Smuzhiyun 	int err = -ENOMEM;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* BUG_ON(IS_ROOT(dentry)); */
413*4882a593Smuzhiyun 	page = alloc_page(GFP_KERNEL);
414*4882a593Smuzhiyun 	if (!page)
415*4882a593Smuzhiyun 		return -ENOMEM;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
418*4882a593Smuzhiyun 	if (!fs_locations)
419*4882a593Smuzhiyun 		goto out_free;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/* Get locations */
422*4882a593Smuzhiyun 	dentry = ctx->clone_data.dentry;
423*4882a593Smuzhiyun 	parent = dget_parent(dentry);
424*4882a593Smuzhiyun 	dprintk("%s: getting locations for %pd2\n",
425*4882a593Smuzhiyun 		__func__, dentry);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	err = nfs4_proc_fs_locations(client, d_inode(parent), &dentry->d_name, fs_locations, page);
428*4882a593Smuzhiyun 	dput(parent);
429*4882a593Smuzhiyun 	if (err != 0)
430*4882a593Smuzhiyun 		goto out_free_2;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	err = -ENOENT;
433*4882a593Smuzhiyun 	if (fs_locations->nlocations <= 0 ||
434*4882a593Smuzhiyun 	    fs_locations->fs_path.ncomponents <= 0)
435*4882a593Smuzhiyun 		goto out_free_2;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	err = nfs_follow_referral(fc, fs_locations);
438*4882a593Smuzhiyun out_free_2:
439*4882a593Smuzhiyun 	kfree(fs_locations);
440*4882a593Smuzhiyun out_free:
441*4882a593Smuzhiyun 	__free_page(page);
442*4882a593Smuzhiyun 	return err;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun 
nfs4_submount(struct fs_context * fc,struct nfs_server * server)445*4882a593Smuzhiyun int nfs4_submount(struct fs_context *fc, struct nfs_server *server)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
448*4882a593Smuzhiyun 	struct dentry *dentry = ctx->clone_data.dentry;
449*4882a593Smuzhiyun 	struct dentry *parent = dget_parent(dentry);
450*4882a593Smuzhiyun 	struct inode *dir = d_inode(parent);
451*4882a593Smuzhiyun 	struct rpc_clnt *client;
452*4882a593Smuzhiyun 	int ret;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* Look it up again to get its attributes and sec flavor */
455*4882a593Smuzhiyun 	client = nfs4_proc_lookup_mountpoint(dir, dentry, ctx->mntfh,
456*4882a593Smuzhiyun 					     ctx->clone_data.fattr);
457*4882a593Smuzhiyun 	dput(parent);
458*4882a593Smuzhiyun 	if (IS_ERR(client))
459*4882a593Smuzhiyun 		return PTR_ERR(client);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	ctx->selected_flavor = client->cl_auth->au_flavor;
462*4882a593Smuzhiyun 	if (ctx->clone_data.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
463*4882a593Smuzhiyun 		ret = nfs_do_refmount(fc, client);
464*4882a593Smuzhiyun 	} else {
465*4882a593Smuzhiyun 		ret = nfs_do_submount(fc);
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	rpc_shutdown_client(client);
469*4882a593Smuzhiyun 	return ret;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun  * Try one location from the fs_locations array.
474*4882a593Smuzhiyun  *
475*4882a593Smuzhiyun  * Returns zero on success, or a negative errno value.
476*4882a593Smuzhiyun  */
nfs4_try_replacing_one_location(struct nfs_server * server,char * page,char * page2,const struct nfs4_fs_location * location)477*4882a593Smuzhiyun static int nfs4_try_replacing_one_location(struct nfs_server *server,
478*4882a593Smuzhiyun 		char *page, char *page2,
479*4882a593Smuzhiyun 		const struct nfs4_fs_location *location)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	const size_t addr_bufsize = sizeof(struct sockaddr_storage);
482*4882a593Smuzhiyun 	struct net *net = rpc_net_ns(server->client);
483*4882a593Smuzhiyun 	struct sockaddr *sap;
484*4882a593Smuzhiyun 	unsigned int s;
485*4882a593Smuzhiyun 	size_t salen;
486*4882a593Smuzhiyun 	int error;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	sap = kmalloc(addr_bufsize, GFP_KERNEL);
489*4882a593Smuzhiyun 	if (sap == NULL)
490*4882a593Smuzhiyun 		return -ENOMEM;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	error = -ENOENT;
493*4882a593Smuzhiyun 	for (s = 0; s < location->nservers; s++) {
494*4882a593Smuzhiyun 		const struct nfs4_string *buf = &location->servers[s];
495*4882a593Smuzhiyun 		char *hostname;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 		if (buf->len <= 0 || buf->len > PAGE_SIZE)
498*4882a593Smuzhiyun 			continue;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len) != NULL)
501*4882a593Smuzhiyun 			continue;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		salen = nfs_parse_server_name(buf->data, buf->len,
504*4882a593Smuzhiyun 						sap, addr_bufsize, net, 0);
505*4882a593Smuzhiyun 		if (salen == 0)
506*4882a593Smuzhiyun 			continue;
507*4882a593Smuzhiyun 		rpc_set_port(sap, NFS_PORT);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		error = -ENOMEM;
510*4882a593Smuzhiyun 		hostname = kmemdup_nul(buf->data, buf->len, GFP_KERNEL);
511*4882a593Smuzhiyun 		if (hostname == NULL)
512*4882a593Smuzhiyun 			break;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		error = nfs4_update_server(server, hostname, sap, salen, net);
515*4882a593Smuzhiyun 		kfree(hostname);
516*4882a593Smuzhiyun 		if (error == 0)
517*4882a593Smuzhiyun 			break;
518*4882a593Smuzhiyun 	}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	kfree(sap);
521*4882a593Smuzhiyun 	return error;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /**
525*4882a593Smuzhiyun  * nfs4_replace_transport - set up transport to destination server
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * @server: export being migrated
528*4882a593Smuzhiyun  * @locations: fs_locations array
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * Returns zero on success, or a negative errno value.
531*4882a593Smuzhiyun  *
532*4882a593Smuzhiyun  * The client tries all the entries in the "locations" array, in the
533*4882a593Smuzhiyun  * order returned by the server, until one works or the end of the
534*4882a593Smuzhiyun  * array is reached.
535*4882a593Smuzhiyun  */
nfs4_replace_transport(struct nfs_server * server,const struct nfs4_fs_locations * locations)536*4882a593Smuzhiyun int nfs4_replace_transport(struct nfs_server *server,
537*4882a593Smuzhiyun 			   const struct nfs4_fs_locations *locations)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	char *page = NULL, *page2 = NULL;
540*4882a593Smuzhiyun 	int loc, error;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	error = -ENOENT;
543*4882a593Smuzhiyun 	if (locations == NULL || locations->nlocations <= 0)
544*4882a593Smuzhiyun 		goto out;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	error = -ENOMEM;
547*4882a593Smuzhiyun 	page = (char *) __get_free_page(GFP_USER);
548*4882a593Smuzhiyun 	if (!page)
549*4882a593Smuzhiyun 		goto out;
550*4882a593Smuzhiyun 	page2 = (char *) __get_free_page(GFP_USER);
551*4882a593Smuzhiyun 	if (!page2)
552*4882a593Smuzhiyun 		goto out;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	for (loc = 0; loc < locations->nlocations; loc++) {
555*4882a593Smuzhiyun 		const struct nfs4_fs_location *location =
556*4882a593Smuzhiyun 						&locations->locations[loc];
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 		if (location == NULL || location->nservers <= 0 ||
559*4882a593Smuzhiyun 		    location->rootpath.ncomponents == 0)
560*4882a593Smuzhiyun 			continue;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 		error = nfs4_try_replacing_one_location(server, page,
563*4882a593Smuzhiyun 							page2, location);
564*4882a593Smuzhiyun 		if (error == 0)
565*4882a593Smuzhiyun 			break;
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun out:
569*4882a593Smuzhiyun 	free_page((unsigned long)page);
570*4882a593Smuzhiyun 	free_page((unsigned long)page2);
571*4882a593Smuzhiyun 	return error;
572*4882a593Smuzhiyun }
573