nfs: break up nfs_follow_referral
[deliverable/linux.git] / fs / nfs / nfs4namespace.c
CommitLineData
f7b422b1
DH
1/*
2 * linux/fs/nfs/nfs4namespace.c
3 *
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
54ceac45 5 * - Modified by David Howells <dhowells@redhat.com>
f7b422b1
DH
6 *
7 * NFSv4 namespace
8 */
9
f7b422b1
DH
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"
c228fd3a 19#include "nfs4_fs.h"
f7b422b1
DH
20
21#define NFSDBG_FACILITY NFSDBG_VFS
22
23/*
24 * Check if fs_root is valid
25 */
509de811 26static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
f7b422b1
DH
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) {
509de811 37 const struct nfs4_string *component = &pathname->components[n];
f7b422b1
DH
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;
46Elong:
47 return ERR_PTR(-ENAMETOOLONG);
48}
49
54ceac45
DH
50/*
51 * Determine the mount path as a string
52 */
53static 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 */
72static 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",
3110ff80 89 __func__, path, fs_path);
54ceac45
DH
90 return -ENOENT;
91 }
92
93 return 0;
94}
95
96/*
97 * Check if the string represents a "valid" IPv4 address
98 */
99static inline int valid_ipaddr4(const char *buf)
100{
101 int rc, count, in[4];
102
103 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
104 if (rc != 4)
105 return -EINVAL;
106 for (count = 0; count < 4; count++) {
107 if (in[count] > 255)
108 return -EINVAL;
109 }
110 return 0;
111}
f7b422b1 112
4ada29d5
BF
113static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
114 char *page, char *page2,
115 const struct nfs4_fs_location *location)
116{
117 struct vfsmount *mnt = ERR_PTR(-ENOENT);
118 char *mnt_path;
119 unsigned int s = 0;
120
121 mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
122 if (IS_ERR(mnt_path))
123 return mnt;
124 mountdata->mnt_path = mnt_path;
125
126 while (s < location->nservers) {
127 struct sockaddr_in addr = {
128 .sin_family = AF_INET,
129 .sin_port = htons(NFS_PORT),
130 };
131
132 if (location->servers[s].len <= 0 ||
133 valid_ipaddr4(location->servers[s].data) < 0) {
134 s++;
135 continue;
136 }
137
138 mountdata->hostname = location->servers[s].data;
139 addr.sin_addr.s_addr = in_aton(mountdata->hostname),
140 mountdata->addr = (struct sockaddr *)&addr;
141 mountdata->addrlen = sizeof(addr);
142
143 snprintf(page, PAGE_SIZE, "%s:%s",
144 mountdata->hostname,
145 mountdata->mnt_path);
146
147 mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
148 if (!IS_ERR(mnt))
149 break;
150 s++;
151 }
152 return mnt;
153}
154
f7b422b1
DH
155/**
156 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
157 * @mnt_parent - mountpoint of parent directory
158 * @dentry - parent directory
3f43c666 159 * @locations - array of NFSv4 server location information
f7b422b1
DH
160 *
161 */
162static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
163 const struct dentry *dentry,
509de811 164 const struct nfs4_fs_locations *locations)
f7b422b1
DH
165{
166 struct vfsmount *mnt = ERR_PTR(-ENOENT);
167 struct nfs_clone_mount mountdata = {
168 .sb = mnt_parent->mnt_sb,
169 .dentry = dentry,
170 .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
171 };
54ceac45 172 char *page = NULL, *page2 = NULL;
3f43c666 173 int loc, error;
f7b422b1
DH
174
175 if (locations == NULL || locations->nlocations <= 0)
176 goto out;
177
3110ff80 178 dprintk("%s: referral at %s/%s\n", __func__,
f7b422b1
DH
179 dentry->d_parent->d_name.name, dentry->d_name.name);
180
f7b422b1 181 page = (char *) __get_free_page(GFP_USER);
54ceac45 182 if (!page)
f7b422b1 183 goto out;
54ceac45 184
f7b422b1 185 page2 = (char *) __get_free_page(GFP_USER);
54ceac45 186 if (!page2)
f7b422b1
DH
187 goto out;
188
54ceac45
DH
189 /* Ensure fs path is a prefix of current dentry path */
190 error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
191 if (error < 0) {
192 mnt = ERR_PTR(error);
193 goto out;
f7b422b1
DH
194 }
195
f7b422b1 196 loc = 0;
4ada29d5 197 while (loc < locations->nlocations) {
509de811 198 const struct nfs4_fs_location *location = &locations->locations[loc];
f7b422b1
DH
199
200 if (location == NULL || location->nservers <= 0 ||
201 location->rootpath.ncomponents == 0) {
202 loc++;
203 continue;
204 }
205
4ada29d5
BF
206 mnt = try_location(&mountdata, page, page2, location);
207 if (!IS_ERR(mnt))
208 break;
f7b422b1
DH
209 loc++;
210 }
211
f7b422b1 212out:
54ceac45
DH
213 free_page((unsigned long) page);
214 free_page((unsigned long) page2);
3110ff80 215 dprintk("%s: done\n", __func__);
f7b422b1
DH
216 return mnt;
217}
218
219/*
220 * nfs_do_refmount - handle crossing a referral on server
221 * @dentry - dentry of referral
222 * @nd - nameidata info
223 *
224 */
225struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
226{
54ceac45 227 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
f7b422b1
DH
228 struct dentry *parent;
229 struct nfs4_fs_locations *fs_locations = NULL;
230 struct page *page;
231 int err;
232
233 /* BUG_ON(IS_ROOT(dentry)); */
3110ff80 234 dprintk("%s: enter\n", __func__);
f7b422b1
DH
235
236 page = alloc_page(GFP_KERNEL);
237 if (page == NULL)
238 goto out;
239
240 fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
241 if (fs_locations == NULL)
242 goto out_free;
243
244 /* Get locations */
54ceac45
DH
245 mnt = ERR_PTR(-ENOENT);
246
f7b422b1 247 parent = dget_parent(dentry);
54ceac45 248 dprintk("%s: getting locations for %s/%s\n",
3110ff80 249 __func__, parent->d_name.name, dentry->d_name.name);
54ceac45 250
c228fd3a 251 err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
f7b422b1 252 dput(parent);
54ceac45
DH
253 if (err != 0 ||
254 fs_locations->nlocations <= 0 ||
f7b422b1
DH
255 fs_locations->fs_path.ncomponents <= 0)
256 goto out_free;
257
258 mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
259out_free:
260 __free_page(page);
261 kfree(fs_locations);
262out:
3110ff80 263 dprintk("%s: done\n", __func__);
f7b422b1
DH
264 return mnt;
265}
This page took 0.42637 seconds and 5 git commands to generate.