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 rpc_authflavor_t
nfs4_negotiate_security(struct inode
*inode
, struct qstr
*name
)
138 struct nfs4_secinfo_flavors
*flavors
;
139 rpc_authflavor_t flavor
;
142 page
= alloc_page(GFP_KERNEL
);
145 flavors
= page_address(page
);
147 err
= nfs4_proc_secinfo(inode
, name
, flavors
);
153 flavor
= nfs_find_best_sec(flavors
);
161 * Please call rpc_shutdown_client() when you are done with this client.
163 struct rpc_clnt
*nfs4_create_sec_client(struct rpc_clnt
*clnt
, struct inode
*inode
,
166 struct rpc_clnt
*clone
;
167 struct rpc_auth
*auth
;
168 rpc_authflavor_t flavor
;
170 flavor
= nfs4_negotiate_security(inode
, name
);
172 return ERR_PTR(flavor
);
174 clone
= rpc_clone_client(clnt
);
178 auth
= rpcauth_create(flavor
, clone
);
180 rpc_shutdown_client(clone
);
181 clone
= ERR_PTR(-EIO
);
187 static struct vfsmount
*try_location(struct nfs_clone_mount
*mountdata
,
188 char *page
, char *page2
,
189 const struct nfs4_fs_location
*location
)
191 const size_t addr_bufsize
= sizeof(struct sockaddr_storage
);
192 struct vfsmount
*mnt
= ERR_PTR(-ENOENT
);
194 unsigned int maxbuflen
;
197 mnt_path
= nfs4_pathname_string(&location
->rootpath
, page2
, PAGE_SIZE
);
198 if (IS_ERR(mnt_path
))
199 return ERR_CAST(mnt_path
);
200 mountdata
->mnt_path
= mnt_path
;
201 maxbuflen
= mnt_path
- 1 - page2
;
203 mountdata
->addr
= kmalloc(addr_bufsize
, GFP_KERNEL
);
204 if (mountdata
->addr
== NULL
)
205 return ERR_PTR(-ENOMEM
);
207 for (s
= 0; s
< location
->nservers
; s
++) {
208 const struct nfs4_string
*buf
= &location
->servers
[s
];
210 if (buf
->len
<= 0 || buf
->len
>= maxbuflen
)
213 if (memchr(buf
->data
, IPV6_SCOPE_DELIMITER
, buf
->len
))
216 mountdata
->addrlen
= nfs_parse_server_name(buf
->data
, buf
->len
,
217 mountdata
->addr
, addr_bufsize
,
218 NFS_SB(mountdata
->sb
));
219 if (mountdata
->addrlen
== 0)
222 rpc_set_port(mountdata
->addr
, NFS_PORT
);
224 memcpy(page2
, buf
->data
, buf
->len
);
225 page2
[buf
->len
] = '\0';
226 mountdata
->hostname
= page2
;
228 snprintf(page
, PAGE_SIZE
, "%s:%s",
230 mountdata
->mnt_path
);
232 mnt
= vfs_kern_mount(&nfs4_referral_fs_type
, 0, page
, mountdata
);
236 kfree(mountdata
->addr
);
241 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
242 * @dentry - parent directory
243 * @locations - array of NFSv4 server location information
246 static struct vfsmount
*nfs_follow_referral(struct dentry
*dentry
,
247 const struct nfs4_fs_locations
*locations
)
249 struct vfsmount
*mnt
= ERR_PTR(-ENOENT
);
250 struct nfs_clone_mount mountdata
= {
253 .authflavor
= NFS_SB(dentry
->d_sb
)->client
->cl_auth
->au_flavor
,
255 char *page
= NULL
, *page2
= NULL
;
258 if (locations
== NULL
|| locations
->nlocations
<= 0)
261 dprintk("%s: referral at %s/%s\n", __func__
,
262 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
264 page
= (char *) __get_free_page(GFP_USER
);
268 page2
= (char *) __get_free_page(GFP_USER
);
272 /* Ensure fs path is a prefix of current dentry path */
273 error
= nfs4_validate_fspath(dentry
, locations
, page
, page2
);
275 mnt
= ERR_PTR(error
);
279 for (loc
= 0; loc
< locations
->nlocations
; loc
++) {
280 const struct nfs4_fs_location
*location
= &locations
->locations
[loc
];
282 if (location
== NULL
|| location
->nservers
<= 0 ||
283 location
->rootpath
.ncomponents
== 0)
286 mnt
= try_location(&mountdata
, page
, page2
, location
);
292 free_page((unsigned long) page
);
293 free_page((unsigned long) page2
);
294 dprintk("%s: done\n", __func__
);
299 * nfs_do_refmount - handle crossing a referral on server
300 * @dentry - dentry of referral
303 struct vfsmount
*nfs_do_refmount(struct rpc_clnt
*client
, struct dentry
*dentry
)
305 struct vfsmount
*mnt
= ERR_PTR(-ENOMEM
);
306 struct dentry
*parent
;
307 struct nfs4_fs_locations
*fs_locations
= NULL
;
311 /* BUG_ON(IS_ROOT(dentry)); */
312 dprintk("%s: enter\n", __func__
);
314 page
= alloc_page(GFP_KERNEL
);
318 fs_locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
319 if (fs_locations
== NULL
)
323 mnt
= ERR_PTR(-ENOENT
);
325 parent
= dget_parent(dentry
);
326 dprintk("%s: getting locations for %s/%s\n",
327 __func__
, parent
->d_name
.name
, dentry
->d_name
.name
);
329 err
= nfs4_proc_fs_locations(client
, parent
->d_inode
, &dentry
->d_name
, fs_locations
, page
);
332 fs_locations
->nlocations
<= 0 ||
333 fs_locations
->fs_path
.ncomponents
<= 0)
336 mnt
= nfs_follow_referral(dentry
, fs_locations
);
341 dprintk("%s: done\n", __func__
);