xref: /openbmc/linux/fs/nfs/nfs4namespace.c (revision ef95d31e)
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>
14f7b422b1SDavid Howells #include <linux/string.h>
15f7b422b1SDavid Howells #include <linux/sunrpc/clnt.h>
16f7b422b1SDavid Howells #include <linux/vfs.h>
17f7b422b1SDavid Howells #include <linux/inet.h>
18f7b422b1SDavid Howells #include "internal.h"
19c228fd3aSTrond Myklebust #include "nfs4_fs.h"
20f7b422b1SDavid Howells 
21f7b422b1SDavid Howells #define NFSDBG_FACILITY		NFSDBG_VFS
22f7b422b1SDavid Howells 
23f7b422b1SDavid Howells /*
24ef95d31eSTrond Myklebust  * Convert the NFSv4 pathname components into a standard posix path.
25ef95d31eSTrond Myklebust  *
26ef95d31eSTrond Myklebust  * Note that the resulting string will be placed at the end of the buffer
27f7b422b1SDavid Howells  */
28509de811SDavid Howells static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
29f7b422b1SDavid Howells 					 char *buffer, ssize_t buflen)
30f7b422b1SDavid Howells {
31f7b422b1SDavid Howells 	char *end = buffer + buflen;
32f7b422b1SDavid Howells 	int n;
33f7b422b1SDavid Howells 
34f7b422b1SDavid Howells 	*--end = '\0';
35f7b422b1SDavid Howells 	buflen--;
36f7b422b1SDavid Howells 
37f7b422b1SDavid Howells 	n = pathname->ncomponents;
38f7b422b1SDavid Howells 	while (--n >= 0) {
39509de811SDavid Howells 		const struct nfs4_string *component = &pathname->components[n];
40f7b422b1SDavid Howells 		buflen -= component->len + 1;
41f7b422b1SDavid Howells 		if (buflen < 0)
42f7b422b1SDavid Howells 			goto Elong;
43f7b422b1SDavid Howells 		end -= component->len;
44f7b422b1SDavid Howells 		memcpy(end, component->data, component->len);
45f7b422b1SDavid Howells 		*--end = '/';
46f7b422b1SDavid Howells 	}
47f7b422b1SDavid Howells 	return end;
48f7b422b1SDavid Howells Elong:
49f7b422b1SDavid Howells 	return ERR_PTR(-ENAMETOOLONG);
50f7b422b1SDavid Howells }
51f7b422b1SDavid Howells 
5254ceac45SDavid Howells /*
5354ceac45SDavid Howells  * Determine the mount path as a string
5454ceac45SDavid Howells  */
5554ceac45SDavid Howells static char *nfs4_path(const struct vfsmount *mnt_parent,
5654ceac45SDavid Howells 		       const struct dentry *dentry,
5754ceac45SDavid Howells 		       char *buffer, ssize_t buflen)
5854ceac45SDavid Howells {
5954ceac45SDavid Howells 	const char *srvpath;
6054ceac45SDavid Howells 
6154ceac45SDavid Howells 	srvpath = strchr(mnt_parent->mnt_devname, ':');
6254ceac45SDavid Howells 	if (srvpath)
6354ceac45SDavid Howells 		srvpath++;
6454ceac45SDavid Howells 	else
6554ceac45SDavid Howells 		srvpath = mnt_parent->mnt_devname;
6654ceac45SDavid Howells 
6754ceac45SDavid Howells 	return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen);
6854ceac45SDavid Howells }
6954ceac45SDavid Howells 
7054ceac45SDavid Howells /*
7154ceac45SDavid Howells  * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
7254ceac45SDavid Howells  * believe to be the server path to this dentry
7354ceac45SDavid Howells  */
7454ceac45SDavid Howells static int nfs4_validate_fspath(const struct vfsmount *mnt_parent,
7554ceac45SDavid Howells 				const struct dentry *dentry,
7654ceac45SDavid Howells 				const struct nfs4_fs_locations *locations,
7754ceac45SDavid Howells 				char *page, char *page2)
7854ceac45SDavid Howells {
7954ceac45SDavid Howells 	const char *path, *fs_path;
8054ceac45SDavid Howells 
8154ceac45SDavid Howells 	path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE);
8254ceac45SDavid Howells 	if (IS_ERR(path))
8354ceac45SDavid Howells 		return PTR_ERR(path);
8454ceac45SDavid Howells 
8554ceac45SDavid Howells 	fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
8654ceac45SDavid Howells 	if (IS_ERR(fs_path))
8754ceac45SDavid Howells 		return PTR_ERR(fs_path);
8854ceac45SDavid Howells 
8954ceac45SDavid Howells 	if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
9054ceac45SDavid Howells 		dprintk("%s: path %s does not begin with fsroot %s\n",
913110ff80SHarvey Harrison 			__func__, path, fs_path);
9254ceac45SDavid Howells 		return -ENOENT;
9354ceac45SDavid Howells 	}
9454ceac45SDavid Howells 
9554ceac45SDavid Howells 	return 0;
9654ceac45SDavid Howells }
9754ceac45SDavid Howells 
984ada29d5SJ. Bruce Fields static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
994ada29d5SJ. Bruce Fields 				     char *page, char *page2,
1004ada29d5SJ. Bruce Fields 				     const struct nfs4_fs_location *location)
1014ada29d5SJ. Bruce Fields {
1024ada29d5SJ. Bruce Fields 	struct vfsmount *mnt = ERR_PTR(-ENOENT);
1034ada29d5SJ. Bruce Fields 	char *mnt_path;
104ef95d31eSTrond Myklebust 	unsigned int maxbuflen;
105460cdbc8SJ. Bruce Fields 	unsigned int s;
1064ada29d5SJ. Bruce Fields 
1074ada29d5SJ. Bruce Fields 	mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
1084ada29d5SJ. Bruce Fields 	if (IS_ERR(mnt_path))
1094ada29d5SJ. Bruce Fields 		return mnt;
1104ada29d5SJ. Bruce Fields 	mountdata->mnt_path = mnt_path;
111ef95d31eSTrond Myklebust 	maxbuflen = mnt_path - 1 - page2;
1124ada29d5SJ. Bruce Fields 
113460cdbc8SJ. Bruce Fields 	for (s = 0; s < location->nservers; s++) {
114ea31a443SJ. Bruce Fields 		const struct nfs4_string *buf = &location->servers[s];
115ea31a443SJ. Bruce Fields 		struct sockaddr_storage addr;
1164ada29d5SJ. Bruce Fields 
117ef95d31eSTrond Myklebust 		if (buf->len <= 0 || buf->len >= maxbuflen)
1184ada29d5SJ. Bruce Fields 			continue;
1194ada29d5SJ. Bruce Fields 
1204ada29d5SJ. Bruce Fields 		mountdata->addr = (struct sockaddr *)&addr;
121ea31a443SJ. Bruce Fields 
122ea31a443SJ. Bruce Fields 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
123ea31a443SJ. Bruce Fields 			continue;
124ea31a443SJ. Bruce Fields 		nfs_parse_ip_address(buf->data, buf->len,
125ea31a443SJ. Bruce Fields 				mountdata->addr, &mountdata->addrlen);
126ea31a443SJ. Bruce Fields 		if (mountdata->addr->sa_family == AF_UNSPEC)
127ea31a443SJ. Bruce Fields 			continue;
128ea31a443SJ. Bruce Fields 		nfs_set_port(mountdata->addr, NFS_PORT);
129ea31a443SJ. Bruce Fields 
130ef95d31eSTrond Myklebust 		memcpy(page2, buf->data, buf->len);
131ef95d31eSTrond Myklebust 		page2[buf->len] = '\0';
132ea31a443SJ. Bruce Fields 		mountdata->hostname = page2;
1334ada29d5SJ. Bruce Fields 
1344ada29d5SJ. Bruce Fields 		snprintf(page, PAGE_SIZE, "%s:%s",
1354ada29d5SJ. Bruce Fields 				mountdata->hostname,
1364ada29d5SJ. Bruce Fields 				mountdata->mnt_path);
1374ada29d5SJ. Bruce Fields 
1384ada29d5SJ. Bruce Fields 		mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
1394ada29d5SJ. Bruce Fields 		if (!IS_ERR(mnt))
1404ada29d5SJ. Bruce Fields 			break;
1414ada29d5SJ. Bruce Fields 	}
1424ada29d5SJ. Bruce Fields 	return mnt;
1434ada29d5SJ. Bruce Fields }
1444ada29d5SJ. Bruce Fields 
145f7b422b1SDavid Howells /**
146f7b422b1SDavid Howells  * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
147f7b422b1SDavid Howells  * @mnt_parent - mountpoint of parent directory
148f7b422b1SDavid Howells  * @dentry - parent directory
1493f43c666SChuck Lever  * @locations - array of NFSv4 server location information
150f7b422b1SDavid Howells  *
151f7b422b1SDavid Howells  */
152f7b422b1SDavid Howells static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
153f7b422b1SDavid Howells 					    const struct dentry *dentry,
154509de811SDavid Howells 					    const struct nfs4_fs_locations *locations)
155f7b422b1SDavid Howells {
156f7b422b1SDavid Howells 	struct vfsmount *mnt = ERR_PTR(-ENOENT);
157f7b422b1SDavid Howells 	struct nfs_clone_mount mountdata = {
158f7b422b1SDavid Howells 		.sb = mnt_parent->mnt_sb,
159f7b422b1SDavid Howells 		.dentry = dentry,
160f7b422b1SDavid Howells 		.authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
161f7b422b1SDavid Howells 	};
16254ceac45SDavid Howells 	char *page = NULL, *page2 = NULL;
1633f43c666SChuck Lever 	int loc, error;
164f7b422b1SDavid Howells 
165f7b422b1SDavid Howells 	if (locations == NULL || locations->nlocations <= 0)
166f7b422b1SDavid Howells 		goto out;
167f7b422b1SDavid Howells 
1683110ff80SHarvey Harrison 	dprintk("%s: referral at %s/%s\n", __func__,
169f7b422b1SDavid Howells 		dentry->d_parent->d_name.name, dentry->d_name.name);
170f7b422b1SDavid Howells 
171f7b422b1SDavid Howells 	page = (char *) __get_free_page(GFP_USER);
17254ceac45SDavid Howells 	if (!page)
173f7b422b1SDavid Howells 		goto out;
17454ceac45SDavid Howells 
175f7b422b1SDavid Howells 	page2 = (char *) __get_free_page(GFP_USER);
17654ceac45SDavid Howells 	if (!page2)
177f7b422b1SDavid Howells 		goto out;
178f7b422b1SDavid Howells 
17954ceac45SDavid Howells 	/* Ensure fs path is a prefix of current dentry path */
18054ceac45SDavid Howells 	error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
18154ceac45SDavid Howells 	if (error < 0) {
18254ceac45SDavid Howells 		mnt = ERR_PTR(error);
18354ceac45SDavid Howells 		goto out;
184f7b422b1SDavid Howells 	}
185f7b422b1SDavid Howells 
186460cdbc8SJ. Bruce Fields 	for (loc = 0; loc < locations->nlocations; loc++) {
187509de811SDavid Howells 		const struct nfs4_fs_location *location = &locations->locations[loc];
188f7b422b1SDavid Howells 
189f7b422b1SDavid Howells 		if (location == NULL || location->nservers <= 0 ||
190460cdbc8SJ. Bruce Fields 		    location->rootpath.ncomponents == 0)
191f7b422b1SDavid Howells 			continue;
192f7b422b1SDavid Howells 
1934ada29d5SJ. Bruce Fields 		mnt = try_location(&mountdata, page, page2, location);
1944ada29d5SJ. Bruce Fields 		if (!IS_ERR(mnt))
195f7b422b1SDavid Howells 			break;
196f7b422b1SDavid Howells 	}
197f7b422b1SDavid Howells 
19854ceac45SDavid Howells out:
199f7b422b1SDavid Howells 	free_page((unsigned long) page);
200f7b422b1SDavid Howells 	free_page((unsigned long) page2);
2013110ff80SHarvey Harrison 	dprintk("%s: done\n", __func__);
202f7b422b1SDavid Howells 	return mnt;
203f7b422b1SDavid Howells }
204f7b422b1SDavid Howells 
205f7b422b1SDavid Howells /*
206f7b422b1SDavid Howells  * nfs_do_refmount - handle crossing a referral on server
207f7b422b1SDavid Howells  * @dentry - dentry of referral
208f7b422b1SDavid Howells  * @nd - nameidata info
209f7b422b1SDavid Howells  *
210f7b422b1SDavid Howells  */
211f7b422b1SDavid Howells struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
212f7b422b1SDavid Howells {
21354ceac45SDavid Howells 	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
214f7b422b1SDavid Howells 	struct dentry *parent;
215f7b422b1SDavid Howells 	struct nfs4_fs_locations *fs_locations = NULL;
216f7b422b1SDavid Howells 	struct page *page;
217f7b422b1SDavid Howells 	int err;
218f7b422b1SDavid Howells 
219f7b422b1SDavid Howells 	/* BUG_ON(IS_ROOT(dentry)); */
2203110ff80SHarvey Harrison 	dprintk("%s: enter\n", __func__);
221f7b422b1SDavid Howells 
222f7b422b1SDavid Howells 	page = alloc_page(GFP_KERNEL);
223f7b422b1SDavid Howells 	if (page == NULL)
224f7b422b1SDavid Howells 		goto out;
225f7b422b1SDavid Howells 
226f7b422b1SDavid Howells 	fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
227f7b422b1SDavid Howells 	if (fs_locations == NULL)
228f7b422b1SDavid Howells 		goto out_free;
229f7b422b1SDavid Howells 
230f7b422b1SDavid Howells 	/* Get locations */
23154ceac45SDavid Howells 	mnt = ERR_PTR(-ENOENT);
23254ceac45SDavid Howells 
233f7b422b1SDavid Howells 	parent = dget_parent(dentry);
23454ceac45SDavid Howells 	dprintk("%s: getting locations for %s/%s\n",
2353110ff80SHarvey Harrison 		__func__, parent->d_name.name, dentry->d_name.name);
23654ceac45SDavid Howells 
237c228fd3aSTrond Myklebust 	err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
238f7b422b1SDavid Howells 	dput(parent);
23954ceac45SDavid Howells 	if (err != 0 ||
24054ceac45SDavid Howells 	    fs_locations->nlocations <= 0 ||
241f7b422b1SDavid Howells 	    fs_locations->fs_path.ncomponents <= 0)
242f7b422b1SDavid Howells 		goto out_free;
243f7b422b1SDavid Howells 
244f7b422b1SDavid Howells 	mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
245f7b422b1SDavid Howells out_free:
246f7b422b1SDavid Howells 	__free_page(page);
247f7b422b1SDavid Howells 	kfree(fs_locations);
248f7b422b1SDavid Howells out:
2493110ff80SHarvey Harrison 	dprintk("%s: done\n", __func__);
250f7b422b1SDavid Howells 	return mnt;
251f7b422b1SDavid Howells }
252