2 * linux/fs/nfs/nfs4namespace.c
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5 * - Modified by David Howells <dhowells@redhat.com>
10 #include <linux/dcache.h>
11 #include <linux/mount.h>
12 #include <linux/namei.h>
13 #include <linux/nfs_fs.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/vfs.h>
18 #include <linux/inet.h>
21 #include "dns_resolve.h"
23 #define NFSDBG_FACILITY NFSDBG_VFS
26 * Convert the NFSv4 pathname components into a standard posix path.
28 * Note that the resulting string will be placed at the end of the buffer
30 static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
31 char *buffer, ssize_t buflen)
33 char *end = buffer + buflen;
39 n = pathname->ncomponents;
41 const struct nfs4_string *component = &pathname->components[n];
42 buflen -= component->len + 1;
45 end -= component->len;
46 memcpy(end, component->data, component->len);
51 return ERR_PTR(-ENAMETOOLONG);
55 * return the path component of "<server>:<path>"
56 * nfspath - the "<server>:<path>" string
57 * end - one past the last char that could contain "<server>:"
58 * returns NULL on failure
60 static char *nfs_path_component(const char *nfspath, const char *end)
64 if (*nfspath == '[') {
65 /* parse [] escaped IPv6 addrs */
66 p = strchr(nfspath, ']');
67 if (p != NULL && ++p < end && *p == ':')
70 /* otherwise split on first colon */
71 p = strchr(nfspath, ':');
72 if (p != NULL && p < end)
79 * Determine the mount path as a string
81 static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
84 char *path = nfs_path(&limit, dentry, buffer, buflen);
86 char *path_component = nfs_path_component(path, limit);
88 return path_component;
94 * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
95 * believe to be the server path to this dentry
97 static int nfs4_validate_fspath(struct dentry *dentry,
98 const struct nfs4_fs_locations *locations,
99 char *page, char *page2)
101 const char *path, *fs_path;
103 path = nfs4_path(dentry, page, PAGE_SIZE);
105 return PTR_ERR(path);
107 fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
109 return PTR_ERR(fs_path);
111 if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
112 dprintk("%s: path %s does not begin with fsroot %s\n",
113 __func__, path, fs_path);
120 static size_t nfs_parse_server_name(char *string, size_t len,
121 struct sockaddr *sa, size_t salen, struct nfs_server *server)
123 struct net *net = rpc_net_ns(server->client);
126 ret = rpc_pton(net, string, len, sa, salen);
128 ret = nfs_dns_resolve_name(net, string, len, sa, salen);
135 static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
136 char *page, char *page2,
137 const struct nfs4_fs_location *location)
139 const size_t addr_bufsize = sizeof(struct sockaddr_storage);
140 struct vfsmount *mnt = ERR_PTR(-ENOENT);
142 unsigned int maxbuflen;
145 mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
146 if (IS_ERR(mnt_path))
147 return ERR_CAST(mnt_path);
148 mountdata->mnt_path = mnt_path;
149 maxbuflen = mnt_path - 1 - page2;
151 mountdata->addr = kmalloc(addr_bufsize, GFP_KERNEL);
152 if (mountdata->addr == NULL)
153 return ERR_PTR(-ENOMEM);
155 for (s = 0; s < location->nservers; s++) {
156 const struct nfs4_string *buf = &location->servers[s];
158 if (buf->len <= 0 || buf->len >= maxbuflen)
161 if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
164 mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
165 mountdata->addr, addr_bufsize,
166 NFS_SB(mountdata->sb));
167 if (mountdata->addrlen == 0)
170 rpc_set_port(mountdata->addr, NFS_PORT);
172 memcpy(page2, buf->data, buf->len);
173 page2[buf->len] = '\0';
174 mountdata->hostname = page2;
176 snprintf(page, PAGE_SIZE, "%s:%s",
178 mountdata->mnt_path);
180 mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
184 kfree(mountdata->addr);
189 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
190 * @dentry - parent directory
191 * @locations - array of NFSv4 server location information
194 static struct vfsmount *nfs_follow_referral(struct dentry *dentry,
195 const struct nfs4_fs_locations *locations)
197 struct vfsmount *mnt = ERR_PTR(-ENOENT);
198 struct nfs_clone_mount mountdata = {
201 .authflavor = NFS_SB(dentry->d_sb)->client->cl_auth->au_flavor,
203 char *page = NULL, *page2 = NULL;
206 if (locations == NULL || locations->nlocations <= 0)
209 dprintk("%s: referral at %s/%s\n", __func__,
210 dentry->d_parent->d_name.name, dentry->d_name.name);
212 page = (char *) __get_free_page(GFP_USER);
216 page2 = (char *) __get_free_page(GFP_USER);
220 /* Ensure fs path is a prefix of current dentry path */
221 error = nfs4_validate_fspath(dentry, locations, page, page2);
223 mnt = ERR_PTR(error);
227 for (loc = 0; loc < locations->nlocations; loc++) {
228 const struct nfs4_fs_location *location = &locations->locations[loc];
230 if (location == NULL || location->nservers <= 0 ||
231 location->rootpath.ncomponents == 0)
234 mnt = try_location(&mountdata, page, page2, location);
240 free_page((unsigned long) page);
241 free_page((unsigned long) page2);
242 dprintk("%s: done\n", __func__);
247 * nfs_do_refmount - handle crossing a referral on server
248 * @dentry - dentry of referral
251 struct vfsmount *nfs_do_refmount(struct dentry *dentry)
253 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
254 struct dentry *parent;
255 struct nfs4_fs_locations *fs_locations = NULL;
259 /* BUG_ON(IS_ROOT(dentry)); */
260 dprintk("%s: enter\n", __func__);
262 page = alloc_page(GFP_KERNEL);
266 fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
267 if (fs_locations == NULL)
271 mnt = ERR_PTR(-ENOENT);
273 parent = dget_parent(dentry);
274 dprintk("%s: getting locations for %s/%s\n",
275 __func__, parent->d_name.name, dentry->d_name.name);
277 err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
280 fs_locations->nlocations <= 0 ||
281 fs_locations->fs_path.ncomponents <= 0)
284 mnt = nfs_follow_referral(dentry, fs_locations);
289 dprintk("%s: done\n", __func__);