xref: /openbmc/linux/fs/nfs/nfs4namespace.c (revision 33faaa38)
1f7b422b1SDavid Howells /*
2f7b422b1SDavid Howells  * linux/fs/nfs/nfs4namespace.c
3f7b422b1SDavid Howells  *
4f7b422b1SDavid Howells  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
554ceac45SDavid Howells  * - Modified by David Howells <dhowells@redhat.com>
6f7b422b1SDavid Howells  *
7f7b422b1SDavid Howells  * NFSv4 namespace
8f7b422b1SDavid Howells  */
9f7b422b1SDavid Howells 
10f7b422b1SDavid Howells #include <linux/dcache.h>
11f7b422b1SDavid Howells #include <linux/mount.h>
12f7b422b1SDavid Howells #include <linux/namei.h>
13f7b422b1SDavid Howells #include <linux/nfs_fs.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15f7b422b1SDavid Howells #include <linux/string.h>
16f7b422b1SDavid Howells #include <linux/sunrpc/clnt.h>
17f7b422b1SDavid Howells #include <linux/vfs.h>
18f7b422b1SDavid Howells #include <linux/inet.h>
19f7b422b1SDavid Howells #include "internal.h"
20c228fd3aSTrond Myklebust #include "nfs4_fs.h"
217d7ea882STrond Myklebust #include "dns_resolve.h"
22f7b422b1SDavid Howells 
23f7b422b1SDavid Howells #define NFSDBG_FACILITY		NFSDBG_VFS
24f7b422b1SDavid Howells 
25f7b422b1SDavid Howells /*
26ef95d31eSTrond Myklebust  * Convert the NFSv4 pathname components into a standard posix path.
27ef95d31eSTrond Myklebust  *
28ef95d31eSTrond Myklebust  * Note that the resulting string will be placed at the end of the buffer
29f7b422b1SDavid Howells  */
30509de811SDavid Howells static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
31f7b422b1SDavid Howells 					 char *buffer, ssize_t buflen)
32f7b422b1SDavid Howells {
33f7b422b1SDavid Howells 	char *end = buffer + buflen;
34f7b422b1SDavid Howells 	int n;
35f7b422b1SDavid Howells 
36f7b422b1SDavid Howells 	*--end = '\0';
37f7b422b1SDavid Howells 	buflen--;
38f7b422b1SDavid Howells 
39f7b422b1SDavid Howells 	n = pathname->ncomponents;
40f7b422b1SDavid Howells 	while (--n >= 0) {
41509de811SDavid Howells 		const struct nfs4_string *component = &pathname->components[n];
42f7b422b1SDavid Howells 		buflen -= component->len + 1;
43f7b422b1SDavid Howells 		if (buflen < 0)
44f7b422b1SDavid Howells 			goto Elong;
45f7b422b1SDavid Howells 		end -= component->len;
46f7b422b1SDavid Howells 		memcpy(end, component->data, component->len);
47f7b422b1SDavid Howells 		*--end = '/';
48f7b422b1SDavid Howells 	}
49f7b422b1SDavid Howells 	return end;
50f7b422b1SDavid Howells Elong:
51f7b422b1SDavid Howells 	return ERR_PTR(-ENAMETOOLONG);
52f7b422b1SDavid Howells }
53f7b422b1SDavid Howells 
5454ceac45SDavid Howells /*
5554ceac45SDavid Howells  * Determine the mount path as a string
5654ceac45SDavid Howells  */
57b514f872SAl Viro static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
5854ceac45SDavid Howells {
59b514f872SAl Viro 	char *limit;
60b514f872SAl Viro 	char *path = nfs_path(&limit, dentry, buffer, buflen);
61b514f872SAl Viro 	if (!IS_ERR(path)) {
62b514f872SAl Viro 		char *colon = strchr(path, ':');
63b514f872SAl Viro 		if (colon && colon < limit)
64b514f872SAl Viro 			path = colon + 1;
65b514f872SAl Viro 	}
66b514f872SAl Viro 	return path;
6754ceac45SDavid Howells }
6854ceac45SDavid Howells 
6954ceac45SDavid Howells /*
7054ceac45SDavid Howells  * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
7154ceac45SDavid Howells  * believe to be the server path to this dentry
7254ceac45SDavid Howells  */
73b514f872SAl Viro static int nfs4_validate_fspath(struct dentry *dentry,
7454ceac45SDavid Howells 				const struct nfs4_fs_locations *locations,
7554ceac45SDavid Howells 				char *page, char *page2)
7654ceac45SDavid Howells {
7754ceac45SDavid Howells 	const char *path, *fs_path;
7854ceac45SDavid Howells 
79b514f872SAl Viro 	path = nfs4_path(dentry, page, PAGE_SIZE);
8054ceac45SDavid Howells 	if (IS_ERR(path))
8154ceac45SDavid Howells 		return PTR_ERR(path);
8254ceac45SDavid Howells 
8354ceac45SDavid Howells 	fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
8454ceac45SDavid Howells 	if (IS_ERR(fs_path))
8554ceac45SDavid Howells 		return PTR_ERR(fs_path);
8654ceac45SDavid Howells 
8754ceac45SDavid Howells 	if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
8854ceac45SDavid Howells 		dprintk("%s: path %s does not begin with fsroot %s\n",
893110ff80SHarvey Harrison 			__func__, path, fs_path);
9054ceac45SDavid Howells 		return -ENOENT;
9154ceac45SDavid Howells 	}
9254ceac45SDavid Howells 
9354ceac45SDavid Howells 	return 0;
9454ceac45SDavid Howells }
9554ceac45SDavid Howells 
967d7ea882STrond Myklebust static size_t nfs_parse_server_name(char *string, size_t len,
971b340d01SStanislav Kinsbursky 		struct sockaddr *sa, size_t salen, struct nfs_server *server)
987d7ea882STrond Myklebust {
997d7ea882STrond Myklebust 	ssize_t ret;
10033faaa38SStanislav Kinsbursky 	struct net *net = server->client->cl_xprt->xprt_net;
1017d7ea882STrond Myklebust 
10233faaa38SStanislav Kinsbursky 	ret = rpc_pton(net, string, len, sa, salen);
1037d7ea882STrond Myklebust 	if (ret == 0) {
10433faaa38SStanislav Kinsbursky 		ret = nfs_dns_resolve_name(net, string, len, sa, salen);
1057d7ea882STrond Myklebust 		if (ret < 0)
1067d7ea882STrond Myklebust 			ret = 0;
1077d7ea882STrond Myklebust 	}
1087d7ea882STrond Myklebust 	return ret;
1097d7ea882STrond Myklebust }
1107d7ea882STrond Myklebust 
1114ada29d5SJ. Bruce Fields static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
1124ada29d5SJ. Bruce Fields 				     char *page, char *page2,
1134ada29d5SJ. Bruce Fields 				     const struct nfs4_fs_location *location)
1144ada29d5SJ. Bruce Fields {
115364d015eSTrond Myklebust 	const size_t addr_bufsize = sizeof(struct sockaddr_storage);
1164ada29d5SJ. Bruce Fields 	struct vfsmount *mnt = ERR_PTR(-ENOENT);
1174ada29d5SJ. Bruce Fields 	char *mnt_path;
118ef95d31eSTrond Myklebust 	unsigned int maxbuflen;
119460cdbc8SJ. Bruce Fields 	unsigned int s;
1204ada29d5SJ. Bruce Fields 
1214ada29d5SJ. Bruce Fields 	mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
1224ada29d5SJ. Bruce Fields 	if (IS_ERR(mnt_path))
123517be09dSTrond Myklebust 		return ERR_CAST(mnt_path);
1244ada29d5SJ. Bruce Fields 	mountdata->mnt_path = mnt_path;
125ef95d31eSTrond Myklebust 	maxbuflen = mnt_path - 1 - page2;
1264ada29d5SJ. Bruce Fields 
127364d015eSTrond Myklebust 	mountdata->addr = kmalloc(addr_bufsize, GFP_KERNEL);
128364d015eSTrond Myklebust 	if (mountdata->addr == NULL)
129364d015eSTrond Myklebust 		return ERR_PTR(-ENOMEM);
130364d015eSTrond Myklebust 
131460cdbc8SJ. Bruce Fields 	for (s = 0; s < location->nservers; s++) {
132ea31a443SJ. Bruce Fields 		const struct nfs4_string *buf = &location->servers[s];
1334ada29d5SJ. Bruce Fields 
134ef95d31eSTrond Myklebust 		if (buf->len <= 0 || buf->len >= maxbuflen)
1354ada29d5SJ. Bruce Fields 			continue;
1364ada29d5SJ. Bruce Fields 
137ea31a443SJ. Bruce Fields 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
138ea31a443SJ. Bruce Fields 			continue;
139517be09dSTrond Myklebust 
140517be09dSTrond Myklebust 		mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
1411b340d01SStanislav Kinsbursky 				mountdata->addr, addr_bufsize,
1421b340d01SStanislav Kinsbursky 				NFS_SB(mountdata->sb));
14353a0b9c4SChuck Lever 		if (mountdata->addrlen == 0)
144ea31a443SJ. Bruce Fields 			continue;
145517be09dSTrond Myklebust 
146ec6ee612SChuck Lever 		rpc_set_port(mountdata->addr, NFS_PORT);
147ea31a443SJ. Bruce Fields 
148ef95d31eSTrond Myklebust 		memcpy(page2, buf->data, buf->len);
149ef95d31eSTrond Myklebust 		page2[buf->len] = '\0';
150ea31a443SJ. Bruce Fields 		mountdata->hostname = page2;
1514ada29d5SJ. Bruce Fields 
1524ada29d5SJ. Bruce Fields 		snprintf(page, PAGE_SIZE, "%s:%s",
1534ada29d5SJ. Bruce Fields 				mountdata->hostname,
1544ada29d5SJ. Bruce Fields 				mountdata->mnt_path);
1554ada29d5SJ. Bruce Fields 
1564ada29d5SJ. Bruce Fields 		mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
1574ada29d5SJ. Bruce Fields 		if (!IS_ERR(mnt))
1584ada29d5SJ. Bruce Fields 			break;
1594ada29d5SJ. Bruce Fields 	}
160364d015eSTrond Myklebust 	kfree(mountdata->addr);
1614ada29d5SJ. Bruce Fields 	return mnt;
1624ada29d5SJ. Bruce Fields }
1634ada29d5SJ. Bruce Fields 
164f7b422b1SDavid Howells /**
165f7b422b1SDavid Howells  * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
166f7b422b1SDavid Howells  * @dentry - parent directory
1673f43c666SChuck Lever  * @locations - array of NFSv4 server location information
168f7b422b1SDavid Howells  *
169f7b422b1SDavid Howells  */
170f8ad9c4bSAl Viro static struct vfsmount *nfs_follow_referral(struct dentry *dentry,
171509de811SDavid Howells 					    const struct nfs4_fs_locations *locations)
172f7b422b1SDavid Howells {
173f7b422b1SDavid Howells 	struct vfsmount *mnt = ERR_PTR(-ENOENT);
174f7b422b1SDavid Howells 	struct nfs_clone_mount mountdata = {
175f8ad9c4bSAl Viro 		.sb = dentry->d_sb,
176f7b422b1SDavid Howells 		.dentry = dentry,
177f8ad9c4bSAl Viro 		.authflavor = NFS_SB(dentry->d_sb)->client->cl_auth->au_flavor,
178f7b422b1SDavid Howells 	};
17954ceac45SDavid Howells 	char *page = NULL, *page2 = NULL;
1803f43c666SChuck Lever 	int loc, error;
181f7b422b1SDavid Howells 
182f7b422b1SDavid Howells 	if (locations == NULL || locations->nlocations <= 0)
183f7b422b1SDavid Howells 		goto out;
184f7b422b1SDavid Howells 
1853110ff80SHarvey Harrison 	dprintk("%s: referral at %s/%s\n", __func__,
186f7b422b1SDavid Howells 		dentry->d_parent->d_name.name, dentry->d_name.name);
187f7b422b1SDavid Howells 
188f7b422b1SDavid Howells 	page = (char *) __get_free_page(GFP_USER);
18954ceac45SDavid Howells 	if (!page)
190f7b422b1SDavid Howells 		goto out;
19154ceac45SDavid Howells 
192f7b422b1SDavid Howells 	page2 = (char *) __get_free_page(GFP_USER);
19354ceac45SDavid Howells 	if (!page2)
194f7b422b1SDavid Howells 		goto out;
195f7b422b1SDavid Howells 
19654ceac45SDavid Howells 	/* Ensure fs path is a prefix of current dentry path */
197b514f872SAl Viro 	error = nfs4_validate_fspath(dentry, locations, page, page2);
19854ceac45SDavid Howells 	if (error < 0) {
19954ceac45SDavid Howells 		mnt = ERR_PTR(error);
20054ceac45SDavid Howells 		goto out;
201f7b422b1SDavid Howells 	}
202f7b422b1SDavid Howells 
203460cdbc8SJ. Bruce Fields 	for (loc = 0; loc < locations->nlocations; loc++) {
204509de811SDavid Howells 		const struct nfs4_fs_location *location = &locations->locations[loc];
205f7b422b1SDavid Howells 
206f7b422b1SDavid Howells 		if (location == NULL || location->nservers <= 0 ||
207460cdbc8SJ. Bruce Fields 		    location->rootpath.ncomponents == 0)
208f7b422b1SDavid Howells 			continue;
209f7b422b1SDavid Howells 
2104ada29d5SJ. Bruce Fields 		mnt = try_location(&mountdata, page, page2, location);
2114ada29d5SJ. Bruce Fields 		if (!IS_ERR(mnt))
212f7b422b1SDavid Howells 			break;
213f7b422b1SDavid Howells 	}
214f7b422b1SDavid Howells 
21554ceac45SDavid Howells out:
216f7b422b1SDavid Howells 	free_page((unsigned long) page);
217f7b422b1SDavid Howells 	free_page((unsigned long) page2);
2183110ff80SHarvey Harrison 	dprintk("%s: done\n", __func__);
219f7b422b1SDavid Howells 	return mnt;
220f7b422b1SDavid Howells }
221f7b422b1SDavid Howells 
222f7b422b1SDavid Howells /*
223f7b422b1SDavid Howells  * nfs_do_refmount - handle crossing a referral on server
224f7b422b1SDavid Howells  * @dentry - dentry of referral
225f7b422b1SDavid Howells  *
226f7b422b1SDavid Howells  */
227f8ad9c4bSAl Viro struct vfsmount *nfs_do_refmount(struct dentry *dentry)
228f7b422b1SDavid Howells {
22954ceac45SDavid Howells 	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
230f7b422b1SDavid Howells 	struct dentry *parent;
231f7b422b1SDavid Howells 	struct nfs4_fs_locations *fs_locations = NULL;
232f7b422b1SDavid Howells 	struct page *page;
233f7b422b1SDavid Howells 	int err;
234f7b422b1SDavid Howells 
235f7b422b1SDavid Howells 	/* BUG_ON(IS_ROOT(dentry)); */
2363110ff80SHarvey Harrison 	dprintk("%s: enter\n", __func__);
237f7b422b1SDavid Howells 
238f7b422b1SDavid Howells 	page = alloc_page(GFP_KERNEL);
239f7b422b1SDavid Howells 	if (page == NULL)
240f7b422b1SDavid Howells 		goto out;
241f7b422b1SDavid Howells 
242f7b422b1SDavid Howells 	fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
243f7b422b1SDavid Howells 	if (fs_locations == NULL)
244f7b422b1SDavid Howells 		goto out_free;
245f7b422b1SDavid Howells 
246f7b422b1SDavid Howells 	/* Get locations */
24754ceac45SDavid Howells 	mnt = ERR_PTR(-ENOENT);
24854ceac45SDavid Howells 
249f7b422b1SDavid Howells 	parent = dget_parent(dentry);
25054ceac45SDavid Howells 	dprintk("%s: getting locations for %s/%s\n",
2513110ff80SHarvey Harrison 		__func__, parent->d_name.name, dentry->d_name.name);
25254ceac45SDavid Howells 
253c228fd3aSTrond Myklebust 	err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
254f7b422b1SDavid Howells 	dput(parent);
25554ceac45SDavid Howells 	if (err != 0 ||
25654ceac45SDavid Howells 	    fs_locations->nlocations <= 0 ||
257f7b422b1SDavid Howells 	    fs_locations->fs_path.ncomponents <= 0)
258f7b422b1SDavid Howells 		goto out_free;
259f7b422b1SDavid Howells 
260f8ad9c4bSAl Viro 	mnt = nfs_follow_referral(dentry, fs_locations);
261f7b422b1SDavid Howells out_free:
262f7b422b1SDavid Howells 	__free_page(page);
263f7b422b1SDavid Howells 	kfree(fs_locations);
264f7b422b1SDavid Howells out:
2653110ff80SHarvey Harrison 	dprintk("%s: done\n", __func__);
266f7b422b1SDavid Howells 	return mnt;
267f7b422b1SDavid Howells }
268