| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/fs/nfs/nfs4namespace.c | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com> | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 5 |  * - Modified by David Howells <dhowells@redhat.com> | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 6 |  * | 
 | 7 |  * NFSv4 namespace | 
 | 8 |  */ | 
 | 9 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 10 | #include <linux/dcache.h> | 
 | 11 | #include <linux/mount.h> | 
 | 12 | #include <linux/namei.h> | 
 | 13 | #include <linux/nfs_fs.h> | 
 | 14 | #include <linux/string.h> | 
 | 15 | #include <linux/sunrpc/clnt.h> | 
 | 16 | #include <linux/vfs.h> | 
 | 17 | #include <linux/inet.h> | 
 | 18 | #include "internal.h" | 
| Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 19 | #include "nfs4_fs.h" | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 20 |  | 
 | 21 | #define NFSDBG_FACILITY		NFSDBG_VFS | 
 | 22 |  | 
 | 23 | /* | 
 | 24 |  * Check if fs_root is valid | 
 | 25 |  */ | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 26 | static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname, | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 27 | 					 char *buffer, ssize_t buflen) | 
 | 28 | { | 
 | 29 | 	char *end = buffer + buflen; | 
 | 30 | 	int n; | 
 | 31 |  | 
 | 32 | 	*--end = '\0'; | 
 | 33 | 	buflen--; | 
 | 34 |  | 
 | 35 | 	n = pathname->ncomponents; | 
 | 36 | 	while (--n >= 0) { | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 37 | 		const struct nfs4_string *component = &pathname->components[n]; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 38 | 		buflen -= component->len + 1; | 
 | 39 | 		if (buflen < 0) | 
 | 40 | 			goto Elong; | 
 | 41 | 		end -= component->len; | 
 | 42 | 		memcpy(end, component->data, component->len); | 
 | 43 | 		*--end = '/'; | 
 | 44 | 	} | 
 | 45 | 	return end; | 
 | 46 | Elong: | 
 | 47 | 	return ERR_PTR(-ENAMETOOLONG); | 
 | 48 | } | 
 | 49 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 50 | /* | 
 | 51 |  * Determine the mount path as a string | 
 | 52 |  */ | 
 | 53 | static char *nfs4_path(const struct vfsmount *mnt_parent, | 
 | 54 | 		       const struct dentry *dentry, | 
 | 55 | 		       char *buffer, ssize_t buflen) | 
 | 56 | { | 
 | 57 | 	const char *srvpath; | 
 | 58 |  | 
 | 59 | 	srvpath = strchr(mnt_parent->mnt_devname, ':'); | 
 | 60 | 	if (srvpath) | 
 | 61 | 		srvpath++; | 
 | 62 | 	else | 
 | 63 | 		srvpath = mnt_parent->mnt_devname; | 
 | 64 |  | 
 | 65 | 	return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen); | 
 | 66 | } | 
 | 67 |  | 
 | 68 | /* | 
 | 69 |  * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we | 
 | 70 |  * believe to be the server path to this dentry | 
 | 71 |  */ | 
 | 72 | static int nfs4_validate_fspath(const struct vfsmount *mnt_parent, | 
 | 73 | 				const struct dentry *dentry, | 
 | 74 | 				const struct nfs4_fs_locations *locations, | 
 | 75 | 				char *page, char *page2) | 
 | 76 | { | 
 | 77 | 	const char *path, *fs_path; | 
 | 78 |  | 
 | 79 | 	path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE); | 
 | 80 | 	if (IS_ERR(path)) | 
 | 81 | 		return PTR_ERR(path); | 
 | 82 |  | 
 | 83 | 	fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE); | 
 | 84 | 	if (IS_ERR(fs_path)) | 
 | 85 | 		return PTR_ERR(fs_path); | 
 | 86 |  | 
 | 87 | 	if (strncmp(path, fs_path, strlen(fs_path)) != 0) { | 
 | 88 | 		dprintk("%s: path %s does not begin with fsroot %s\n", | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 89 | 			__func__, path, fs_path); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 90 | 		return -ENOENT; | 
 | 91 | 	} | 
 | 92 |  | 
 | 93 | 	return 0; | 
 | 94 | } | 
 | 95 |  | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 96 | static struct vfsmount *try_location(struct nfs_clone_mount *mountdata, | 
 | 97 | 				     char *page, char *page2, | 
 | 98 | 				     const struct nfs4_fs_location *location) | 
 | 99 | { | 
 | 100 | 	struct vfsmount *mnt = ERR_PTR(-ENOENT); | 
 | 101 | 	char *mnt_path; | 
| J. Bruce Fields | ea31a44 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 102 | 	int page2len; | 
| J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 103 | 	unsigned int s; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 104 |  | 
 | 105 | 	mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE); | 
 | 106 | 	if (IS_ERR(mnt_path)) | 
 | 107 | 		return mnt; | 
 | 108 | 	mountdata->mnt_path = mnt_path; | 
| J. Bruce Fields | ea31a44 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 109 | 	page2 += strlen(mnt_path) + 1; | 
 | 110 | 	page2len = PAGE_SIZE - strlen(mnt_path) - 1; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 111 |  | 
| J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 112 | 	for (s = 0; s < location->nservers; s++) { | 
| J. Bruce Fields | ea31a44 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 113 | 		const struct nfs4_string *buf = &location->servers[s]; | 
 | 114 | 		struct sockaddr_storage addr; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 115 |  | 
| J. Bruce Fields | ea31a44 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 116 | 		if (buf->len <= 0 || buf->len >= PAGE_SIZE) | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 117 | 			continue; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 118 |  | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 119 | 		mountdata->addr = (struct sockaddr *)&addr; | 
| J. Bruce Fields | ea31a44 | 2008-08-20 16:10:23 -0400 | [diff] [blame] | 120 |  | 
 | 121 | 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len)) | 
 | 122 | 			continue; | 
 | 123 | 		nfs_parse_ip_address(buf->data, buf->len, | 
 | 124 | 				mountdata->addr, &mountdata->addrlen); | 
 | 125 | 		if (mountdata->addr->sa_family == AF_UNSPEC) | 
 | 126 | 			continue; | 
 | 127 | 		nfs_set_port(mountdata->addr, NFS_PORT); | 
 | 128 |  | 
 | 129 | 		strncpy(page2, buf->data, page2len); | 
 | 130 | 		page2[page2len] = '\0'; | 
 | 131 | 		mountdata->hostname = page2; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 132 |  | 
 | 133 | 		snprintf(page, PAGE_SIZE, "%s:%s", | 
 | 134 | 				mountdata->hostname, | 
 | 135 | 				mountdata->mnt_path); | 
 | 136 |  | 
 | 137 | 		mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata); | 
 | 138 | 		if (!IS_ERR(mnt)) | 
 | 139 | 			break; | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 140 | 	} | 
 | 141 | 	return mnt; | 
 | 142 | } | 
 | 143 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 144 | /** | 
 | 145 |  * nfs_follow_referral - set up mountpoint when hitting a referral on moved error | 
 | 146 |  * @mnt_parent - mountpoint of parent directory | 
 | 147 |  * @dentry - parent directory | 
| Chuck Lever | 3f43c66 | 2007-12-10 14:57:31 -0500 | [diff] [blame] | 148 |  * @locations - array of NFSv4 server location information | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 149 |  * | 
 | 150 |  */ | 
 | 151 | static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent, | 
 | 152 | 					    const struct dentry *dentry, | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 153 | 					    const struct nfs4_fs_locations *locations) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 154 | { | 
 | 155 | 	struct vfsmount *mnt = ERR_PTR(-ENOENT); | 
 | 156 | 	struct nfs_clone_mount mountdata = { | 
 | 157 | 		.sb = mnt_parent->mnt_sb, | 
 | 158 | 		.dentry = dentry, | 
 | 159 | 		.authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor, | 
 | 160 | 	}; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 161 | 	char *page = NULL, *page2 = NULL; | 
| Chuck Lever | 3f43c66 | 2007-12-10 14:57:31 -0500 | [diff] [blame] | 162 | 	int loc, error; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 163 |  | 
 | 164 | 	if (locations == NULL || locations->nlocations <= 0) | 
 | 165 | 		goto out; | 
 | 166 |  | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 167 | 	dprintk("%s: referral at %s/%s\n", __func__, | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 168 | 		dentry->d_parent->d_name.name, dentry->d_name.name); | 
 | 169 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 170 | 	page = (char *) __get_free_page(GFP_USER); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 171 | 	if (!page) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 172 | 		goto out; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 173 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 174 | 	page2 = (char *) __get_free_page(GFP_USER); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 175 | 	if (!page2) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 176 | 		goto out; | 
 | 177 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 178 | 	/* Ensure fs path is a prefix of current dentry path */ | 
 | 179 | 	error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2); | 
 | 180 | 	if (error < 0) { | 
 | 181 | 		mnt = ERR_PTR(error); | 
 | 182 | 		goto out; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 183 | 	} | 
 | 184 |  | 
| J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 185 | 	for (loc = 0; loc < locations->nlocations; loc++) { | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 186 | 		const struct nfs4_fs_location *location = &locations->locations[loc]; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 187 |  | 
 | 188 | 		if (location == NULL || location->nservers <= 0 || | 
| J. Bruce Fields | 460cdbc | 2008-08-20 16:10:21 -0400 | [diff] [blame] | 189 | 		    location->rootpath.ncomponents == 0) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 190 | 			continue; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 191 |  | 
| J. Bruce Fields | 4ada29d | 2008-08-20 16:10:20 -0400 | [diff] [blame] | 192 | 		mnt = try_location(&mountdata, page, page2, location); | 
 | 193 | 		if (!IS_ERR(mnt)) | 
 | 194 | 			break; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 195 | 	} | 
 | 196 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 197 | out: | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 198 | 	free_page((unsigned long) page); | 
 | 199 | 	free_page((unsigned long) page2); | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 200 | 	dprintk("%s: done\n", __func__); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 201 | 	return mnt; | 
 | 202 | } | 
 | 203 |  | 
 | 204 | /* | 
 | 205 |  * nfs_do_refmount - handle crossing a referral on server | 
 | 206 |  * @dentry - dentry of referral | 
 | 207 |  * @nd - nameidata info | 
 | 208 |  * | 
 | 209 |  */ | 
 | 210 | struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry) | 
 | 211 | { | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 212 | 	struct vfsmount *mnt = ERR_PTR(-ENOMEM); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 213 | 	struct dentry *parent; | 
 | 214 | 	struct nfs4_fs_locations *fs_locations = NULL; | 
 | 215 | 	struct page *page; | 
 | 216 | 	int err; | 
 | 217 |  | 
 | 218 | 	/* BUG_ON(IS_ROOT(dentry)); */ | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 219 | 	dprintk("%s: enter\n", __func__); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 220 |  | 
 | 221 | 	page = alloc_page(GFP_KERNEL); | 
 | 222 | 	if (page == NULL) | 
 | 223 | 		goto out; | 
 | 224 |  | 
 | 225 | 	fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | 
 | 226 | 	if (fs_locations == NULL) | 
 | 227 | 		goto out_free; | 
 | 228 |  | 
 | 229 | 	/* Get locations */ | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 230 | 	mnt = ERR_PTR(-ENOENT); | 
 | 231 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 232 | 	parent = dget_parent(dentry); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 233 | 	dprintk("%s: getting locations for %s/%s\n", | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 234 | 		__func__, parent->d_name.name, dentry->d_name.name); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 235 |  | 
| Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 236 | 	err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 237 | 	dput(parent); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 238 | 	if (err != 0 || | 
 | 239 | 	    fs_locations->nlocations <= 0 || | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 240 | 	    fs_locations->fs_path.ncomponents <= 0) | 
 | 241 | 		goto out_free; | 
 | 242 |  | 
 | 243 | 	mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations); | 
 | 244 | out_free: | 
 | 245 | 	__free_page(page); | 
 | 246 | 	kfree(fs_locations); | 
 | 247 | out: | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 248 | 	dprintk("%s: done\n", __func__); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 249 | 	return mnt; | 
 | 250 | } |