| 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" | 
 | 19 |  | 
 | 20 | #define NFSDBG_FACILITY		NFSDBG_VFS | 
 | 21 |  | 
 | 22 | /* | 
 | 23 |  * Check if fs_root is valid | 
 | 24 |  */ | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 25 | static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname, | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 26 | 					 char *buffer, ssize_t buflen) | 
 | 27 | { | 
 | 28 | 	char *end = buffer + buflen; | 
 | 29 | 	int n; | 
 | 30 |  | 
 | 31 | 	*--end = '\0'; | 
 | 32 | 	buflen--; | 
 | 33 |  | 
 | 34 | 	n = pathname->ncomponents; | 
 | 35 | 	while (--n >= 0) { | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 36 | 		const struct nfs4_string *component = &pathname->components[n]; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 37 | 		buflen -= component->len + 1; | 
 | 38 | 		if (buflen < 0) | 
 | 39 | 			goto Elong; | 
 | 40 | 		end -= component->len; | 
 | 41 | 		memcpy(end, component->data, component->len); | 
 | 42 | 		*--end = '/'; | 
 | 43 | 	} | 
 | 44 | 	return end; | 
 | 45 | Elong: | 
 | 46 | 	return ERR_PTR(-ENAMETOOLONG); | 
 | 47 | } | 
 | 48 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 49 | /* | 
 | 50 |  * Determine the mount path as a string | 
 | 51 |  */ | 
 | 52 | static char *nfs4_path(const struct vfsmount *mnt_parent, | 
 | 53 | 		       const struct dentry *dentry, | 
 | 54 | 		       char *buffer, ssize_t buflen) | 
 | 55 | { | 
 | 56 | 	const char *srvpath; | 
 | 57 |  | 
 | 58 | 	srvpath = strchr(mnt_parent->mnt_devname, ':'); | 
 | 59 | 	if (srvpath) | 
 | 60 | 		srvpath++; | 
 | 61 | 	else | 
 | 62 | 		srvpath = mnt_parent->mnt_devname; | 
 | 63 |  | 
 | 64 | 	return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen); | 
 | 65 | } | 
 | 66 |  | 
 | 67 | /* | 
 | 68 |  * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we | 
 | 69 |  * believe to be the server path to this dentry | 
 | 70 |  */ | 
 | 71 | static int nfs4_validate_fspath(const struct vfsmount *mnt_parent, | 
 | 72 | 				const struct dentry *dentry, | 
 | 73 | 				const struct nfs4_fs_locations *locations, | 
 | 74 | 				char *page, char *page2) | 
 | 75 | { | 
 | 76 | 	const char *path, *fs_path; | 
 | 77 |  | 
 | 78 | 	path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE); | 
 | 79 | 	if (IS_ERR(path)) | 
 | 80 | 		return PTR_ERR(path); | 
 | 81 |  | 
 | 82 | 	fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE); | 
 | 83 | 	if (IS_ERR(fs_path)) | 
 | 84 | 		return PTR_ERR(fs_path); | 
 | 85 |  | 
 | 86 | 	if (strncmp(path, fs_path, strlen(fs_path)) != 0) { | 
 | 87 | 		dprintk("%s: path %s does not begin with fsroot %s\n", | 
 | 88 | 			__FUNCTION__, path, fs_path); | 
 | 89 | 		return -ENOENT; | 
 | 90 | 	} | 
 | 91 |  | 
 | 92 | 	return 0; | 
 | 93 | } | 
 | 94 |  | 
 | 95 | /* | 
 | 96 |  * Check if the string represents a "valid" IPv4 address | 
 | 97 |  */ | 
 | 98 | static inline int valid_ipaddr4(const char *buf) | 
 | 99 | { | 
 | 100 | 	int rc, count, in[4]; | 
 | 101 |  | 
 | 102 | 	rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); | 
 | 103 | 	if (rc != 4) | 
 | 104 | 		return -EINVAL; | 
 | 105 | 	for (count = 0; count < 4; count++) { | 
 | 106 | 		if (in[count] > 255) | 
 | 107 | 			return -EINVAL; | 
 | 108 | 	} | 
 | 109 | 	return 0; | 
 | 110 | } | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 111 |  | 
 | 112 | /** | 
 | 113 |  * nfs_follow_referral - set up mountpoint when hitting a referral on moved error | 
 | 114 |  * @mnt_parent - mountpoint of parent directory | 
 | 115 |  * @dentry - parent directory | 
 | 116 |  * @fspath - fs path returned in fs_locations | 
 | 117 |  * @mntpath - mount path to new server | 
 | 118 |  * @hostname - hostname of new server | 
 | 119 |  * @addr - host addr of new server | 
 | 120 |  * | 
 | 121 |  */ | 
 | 122 | static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent, | 
 | 123 | 					    const struct dentry *dentry, | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 124 | 					    const struct nfs4_fs_locations *locations) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 125 | { | 
 | 126 | 	struct vfsmount *mnt = ERR_PTR(-ENOENT); | 
 | 127 | 	struct nfs_clone_mount mountdata = { | 
 | 128 | 		.sb = mnt_parent->mnt_sb, | 
 | 129 | 		.dentry = dentry, | 
 | 130 | 		.authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor, | 
 | 131 | 	}; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 132 | 	char *page = NULL, *page2 = NULL; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 133 | 	char *devname; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 134 | 	int loc, s, error; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 135 |  | 
 | 136 | 	if (locations == NULL || locations->nlocations <= 0) | 
 | 137 | 		goto out; | 
 | 138 |  | 
 | 139 | 	dprintk("%s: referral at %s/%s\n", __FUNCTION__, | 
 | 140 | 		dentry->d_parent->d_name.name, dentry->d_name.name); | 
 | 141 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 142 | 	page = (char *) __get_free_page(GFP_USER); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 143 | 	if (!page) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 144 | 		goto out; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 145 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 146 | 	page2 = (char *) __get_free_page(GFP_USER); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 147 | 	if (!page2) | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 148 | 		goto out; | 
 | 149 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 150 | 	/* Ensure fs path is a prefix of current dentry path */ | 
 | 151 | 	error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2); | 
 | 152 | 	if (error < 0) { | 
 | 153 | 		mnt = ERR_PTR(error); | 
 | 154 | 		goto out; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 155 | 	} | 
 | 156 |  | 
 | 157 | 	devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE); | 
 | 158 | 	if (IS_ERR(devname)) { | 
 | 159 | 		mnt = (struct vfsmount *)devname; | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 160 | 		goto out; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 161 | 	} | 
 | 162 |  | 
 | 163 | 	loc = 0; | 
 | 164 | 	while (loc < locations->nlocations && IS_ERR(mnt)) { | 
| David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 165 | 		const struct nfs4_fs_location *location = &locations->locations[loc]; | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 166 | 		char *mnt_path; | 
 | 167 |  | 
 | 168 | 		if (location == NULL || location->nservers <= 0 || | 
 | 169 | 		    location->rootpath.ncomponents == 0) { | 
 | 170 | 			loc++; | 
 | 171 | 			continue; | 
 | 172 | 		} | 
 | 173 |  | 
 | 174 | 		mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE); | 
 | 175 | 		if (IS_ERR(mnt_path)) { | 
 | 176 | 			loc++; | 
 | 177 | 			continue; | 
 | 178 | 		} | 
 | 179 | 		mountdata.mnt_path = mnt_path; | 
 | 180 |  | 
 | 181 | 		s = 0; | 
 | 182 | 		while (s < location->nservers) { | 
 | 183 | 			struct sockaddr_in addr = {}; | 
 | 184 |  | 
 | 185 | 			if (location->servers[s].len <= 0 || | 
 | 186 | 			    valid_ipaddr4(location->servers[s].data) < 0) { | 
 | 187 | 				s++; | 
 | 188 | 				continue; | 
 | 189 | 			} | 
 | 190 |  | 
 | 191 | 			mountdata.hostname = location->servers[s].data; | 
 | 192 | 			addr.sin_addr.s_addr = in_aton(mountdata.hostname); | 
 | 193 | 			addr.sin_family = AF_INET; | 
 | 194 | 			addr.sin_port = htons(NFS_PORT); | 
 | 195 | 			mountdata.addr = &addr; | 
 | 196 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 197 | 			mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, devname, &mountdata); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 198 | 			if (!IS_ERR(mnt)) { | 
 | 199 | 				break; | 
 | 200 | 			} | 
 | 201 | 			s++; | 
 | 202 | 		} | 
 | 203 | 		loc++; | 
 | 204 | 	} | 
 | 205 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 206 | out: | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 207 | 	free_page((unsigned long) page); | 
 | 208 | 	free_page((unsigned long) page2); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 209 | 	dprintk("%s: done\n", __FUNCTION__); | 
 | 210 | 	return mnt; | 
 | 211 | } | 
 | 212 |  | 
 | 213 | /* | 
 | 214 |  * nfs_do_refmount - handle crossing a referral on server | 
 | 215 |  * @dentry - dentry of referral | 
 | 216 |  * @nd - nameidata info | 
 | 217 |  * | 
 | 218 |  */ | 
 | 219 | struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry) | 
 | 220 | { | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 221 | 	struct vfsmount *mnt = ERR_PTR(-ENOMEM); | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 222 | 	struct dentry *parent; | 
 | 223 | 	struct nfs4_fs_locations *fs_locations = NULL; | 
 | 224 | 	struct page *page; | 
 | 225 | 	int err; | 
 | 226 |  | 
 | 227 | 	/* BUG_ON(IS_ROOT(dentry)); */ | 
 | 228 | 	dprintk("%s: enter\n", __FUNCTION__); | 
 | 229 |  | 
 | 230 | 	page = alloc_page(GFP_KERNEL); | 
 | 231 | 	if (page == NULL) | 
 | 232 | 		goto out; | 
 | 233 |  | 
 | 234 | 	fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | 
 | 235 | 	if (fs_locations == NULL) | 
 | 236 | 		goto out_free; | 
 | 237 |  | 
 | 238 | 	/* Get locations */ | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 239 | 	mnt = ERR_PTR(-ENOENT); | 
 | 240 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 241 | 	parent = dget_parent(dentry); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 242 | 	dprintk("%s: getting locations for %s/%s\n", | 
 | 243 | 		__FUNCTION__, parent->d_name.name, dentry->d_name.name); | 
 | 244 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 245 | 	err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page); | 
 | 246 | 	dput(parent); | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 247 | 	if (err != 0 || | 
 | 248 | 	    fs_locations->nlocations <= 0 || | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 249 | 	    fs_locations->fs_path.ncomponents <= 0) | 
 | 250 | 		goto out_free; | 
 | 251 |  | 
 | 252 | 	mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations); | 
 | 253 | out_free: | 
 | 254 | 	__free_page(page); | 
 | 255 | 	kfree(fs_locations); | 
 | 256 | out: | 
 | 257 | 	dprintk("%s: done\n", __FUNCTION__); | 
 | 258 | 	return mnt; | 
 | 259 | } |