blob: 24e47f3bbd17334fb640c634034605a4d6ad041c [file] [log] [blame]
David Howellsf7b422b2006-06-09 09:34:33 -04001/*
2 * linux/fs/nfs/nfs4namespace.c
3 *
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
David Howells54ceac42006-08-22 20:06:13 -04005 * - Modified by David Howells <dhowells@redhat.com>
David Howellsf7b422b2006-06-09 09:34:33 -04006 *
7 * NFSv4 namespace
8 */
9
10#include <linux/config.h>
11
12#include <linux/dcache.h>
13#include <linux/mount.h>
14#include <linux/namei.h>
15#include <linux/nfs_fs.h>
16#include <linux/string.h>
17#include <linux/sunrpc/clnt.h>
18#include <linux/vfs.h>
19#include <linux/inet.h>
20#include "internal.h"
21
22#define NFSDBG_FACILITY NFSDBG_VFS
23
24/*
25 * Check if fs_root is valid
26 */
David Howells509de812006-08-22 20:06:11 -040027static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
David Howellsf7b422b2006-06-09 09:34:33 -040028 char *buffer, ssize_t buflen)
29{
30 char *end = buffer + buflen;
31 int n;
32
33 *--end = '\0';
34 buflen--;
35
36 n = pathname->ncomponents;
37 while (--n >= 0) {
David Howells509de812006-08-22 20:06:11 -040038 const struct nfs4_string *component = &pathname->components[n];
David Howellsf7b422b2006-06-09 09:34:33 -040039 buflen -= component->len + 1;
40 if (buflen < 0)
41 goto Elong;
42 end -= component->len;
43 memcpy(end, component->data, component->len);
44 *--end = '/';
45 }
46 return end;
47Elong:
48 return ERR_PTR(-ENAMETOOLONG);
49}
50
David Howells54ceac42006-08-22 20:06:13 -040051/*
52 * Determine the mount path as a string
53 */
54static char *nfs4_path(const struct vfsmount *mnt_parent,
55 const struct dentry *dentry,
56 char *buffer, ssize_t buflen)
57{
58 const char *srvpath;
59
60 srvpath = strchr(mnt_parent->mnt_devname, ':');
61 if (srvpath)
62 srvpath++;
63 else
64 srvpath = mnt_parent->mnt_devname;
65
66 return nfs_path(srvpath, mnt_parent->mnt_root, dentry, buffer, buflen);
67}
68
69/*
70 * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
71 * believe to be the server path to this dentry
72 */
73static int nfs4_validate_fspath(const struct vfsmount *mnt_parent,
74 const struct dentry *dentry,
75 const struct nfs4_fs_locations *locations,
76 char *page, char *page2)
77{
78 const char *path, *fs_path;
79
80 path = nfs4_path(mnt_parent, dentry, page, PAGE_SIZE);
81 if (IS_ERR(path))
82 return PTR_ERR(path);
83
84 fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
85 if (IS_ERR(fs_path))
86 return PTR_ERR(fs_path);
87
88 if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
89 dprintk("%s: path %s does not begin with fsroot %s\n",
90 __FUNCTION__, path, fs_path);
91 return -ENOENT;
92 }
93
94 return 0;
95}
96
97/*
98 * Check if the string represents a "valid" IPv4 address
99 */
100static inline int valid_ipaddr4(const char *buf)
101{
102 int rc, count, in[4];
103
104 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
105 if (rc != 4)
106 return -EINVAL;
107 for (count = 0; count < 4; count++) {
108 if (in[count] > 255)
109 return -EINVAL;
110 }
111 return 0;
112}
David Howellsf7b422b2006-06-09 09:34:33 -0400113
114/**
115 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
116 * @mnt_parent - mountpoint of parent directory
117 * @dentry - parent directory
118 * @fspath - fs path returned in fs_locations
119 * @mntpath - mount path to new server
120 * @hostname - hostname of new server
121 * @addr - host addr of new server
122 *
123 */
124static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
125 const struct dentry *dentry,
David Howells509de812006-08-22 20:06:11 -0400126 const struct nfs4_fs_locations *locations)
David Howellsf7b422b2006-06-09 09:34:33 -0400127{
128 struct vfsmount *mnt = ERR_PTR(-ENOENT);
129 struct nfs_clone_mount mountdata = {
130 .sb = mnt_parent->mnt_sb,
131 .dentry = dentry,
132 .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
133 };
David Howells54ceac42006-08-22 20:06:13 -0400134 char *page = NULL, *page2 = NULL;
David Howellsf7b422b2006-06-09 09:34:33 -0400135 char *devname;
David Howells54ceac42006-08-22 20:06:13 -0400136 int loc, s, error;
David Howellsf7b422b2006-06-09 09:34:33 -0400137
138 if (locations == NULL || locations->nlocations <= 0)
139 goto out;
140
141 dprintk("%s: referral at %s/%s\n", __FUNCTION__,
142 dentry->d_parent->d_name.name, dentry->d_name.name);
143
David Howellsf7b422b2006-06-09 09:34:33 -0400144 page = (char *) __get_free_page(GFP_USER);
David Howells54ceac42006-08-22 20:06:13 -0400145 if (!page)
David Howellsf7b422b2006-06-09 09:34:33 -0400146 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400147
David Howellsf7b422b2006-06-09 09:34:33 -0400148 page2 = (char *) __get_free_page(GFP_USER);
David Howells54ceac42006-08-22 20:06:13 -0400149 if (!page2)
David Howellsf7b422b2006-06-09 09:34:33 -0400150 goto out;
151
David Howells54ceac42006-08-22 20:06:13 -0400152 /* Ensure fs path is a prefix of current dentry path */
153 error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
154 if (error < 0) {
155 mnt = ERR_PTR(error);
156 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -0400157 }
158
159 devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
160 if (IS_ERR(devname)) {
161 mnt = (struct vfsmount *)devname;
David Howells54ceac42006-08-22 20:06:13 -0400162 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -0400163 }
164
165 loc = 0;
166 while (loc < locations->nlocations && IS_ERR(mnt)) {
David Howells509de812006-08-22 20:06:11 -0400167 const struct nfs4_fs_location *location = &locations->locations[loc];
David Howellsf7b422b2006-06-09 09:34:33 -0400168 char *mnt_path;
169
170 if (location == NULL || location->nservers <= 0 ||
171 location->rootpath.ncomponents == 0) {
172 loc++;
173 continue;
174 }
175
176 mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
177 if (IS_ERR(mnt_path)) {
178 loc++;
179 continue;
180 }
181 mountdata.mnt_path = mnt_path;
182
183 s = 0;
184 while (s < location->nservers) {
185 struct sockaddr_in addr = {};
186
187 if (location->servers[s].len <= 0 ||
188 valid_ipaddr4(location->servers[s].data) < 0) {
189 s++;
190 continue;
191 }
192
193 mountdata.hostname = location->servers[s].data;
194 addr.sin_addr.s_addr = in_aton(mountdata.hostname);
195 addr.sin_family = AF_INET;
196 addr.sin_port = htons(NFS_PORT);
197 mountdata.addr = &addr;
198
David Howells54ceac42006-08-22 20:06:13 -0400199 mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400200 if (!IS_ERR(mnt)) {
201 break;
202 }
203 s++;
204 }
205 loc++;
206 }
207
David Howellsf7b422b2006-06-09 09:34:33 -0400208out:
David Howells54ceac42006-08-22 20:06:13 -0400209 free_page((unsigned long) page);
210 free_page((unsigned long) page2);
David Howellsf7b422b2006-06-09 09:34:33 -0400211 dprintk("%s: done\n", __FUNCTION__);
212 return mnt;
213}
214
215/*
216 * nfs_do_refmount - handle crossing a referral on server
217 * @dentry - dentry of referral
218 * @nd - nameidata info
219 *
220 */
221struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
222{
David Howells54ceac42006-08-22 20:06:13 -0400223 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
David Howellsf7b422b2006-06-09 09:34:33 -0400224 struct dentry *parent;
225 struct nfs4_fs_locations *fs_locations = NULL;
226 struct page *page;
227 int err;
228
229 /* BUG_ON(IS_ROOT(dentry)); */
230 dprintk("%s: enter\n", __FUNCTION__);
231
232 page = alloc_page(GFP_KERNEL);
233 if (page == NULL)
234 goto out;
235
236 fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
237 if (fs_locations == NULL)
238 goto out_free;
239
240 /* Get locations */
David Howells54ceac42006-08-22 20:06:13 -0400241 mnt = ERR_PTR(-ENOENT);
242
David Howellsf7b422b2006-06-09 09:34:33 -0400243 parent = dget_parent(dentry);
David Howells54ceac42006-08-22 20:06:13 -0400244 dprintk("%s: getting locations for %s/%s\n",
245 __FUNCTION__, parent->d_name.name, dentry->d_name.name);
246
David Howellsf7b422b2006-06-09 09:34:33 -0400247 err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
248 dput(parent);
David Howells54ceac42006-08-22 20:06:13 -0400249 if (err != 0 ||
250 fs_locations->nlocations <= 0 ||
David Howellsf7b422b2006-06-09 09:34:33 -0400251 fs_locations->fs_path.ncomponents <= 0)
252 goto out_free;
253
254 mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
255out_free:
256 __free_page(page);
257 kfree(fs_locations);
258out:
259 dprintk("%s: done\n", __FUNCTION__);
260 return mnt;
261}