blob: 6bcc5696f91120f51bfa242426737cec8da15807 [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
David Howellsf7b422b2006-06-09 09:34:33 -040010#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"
Trond Myklebustc228fd32007-01-13 02:28:11 -050019#include "nfs4_fs.h"
David Howellsf7b422b2006-06-09 09:34:33 -040020
21#define NFSDBG_FACILITY NFSDBG_VFS
22
23/*
24 * Check if fs_root is valid
25 */
David Howells509de812006-08-22 20:06:11 -040026static inline char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
David Howellsf7b422b2006-06-09 09:34:33 -040027 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) {
David Howells509de812006-08-22 20:06:11 -040037 const struct nfs4_string *component = &pathname->components[n];
David Howellsf7b422b2006-06-09 09:34:33 -040038 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
David Howells54ceac42006-08-22 20:06:13 -040050/*
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",
Harvey Harrison3110ff82008-05-02 13:42:44 -070089 __func__, path, fs_path);
David Howells54ceac42006-08-22 20:06:13 -040090 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}
David Howellsf7b422b2006-06-09 09:34:33 -0400112
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400113static 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;
J. Bruce Fields460cdbc2008-08-20 16:10:21 -0400119 unsigned int s;
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400120
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
J. Bruce Fields460cdbc2008-08-20 16:10:21 -0400126 for (s = 0; s < location->nservers; s++) {
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400127 struct sockaddr_in addr = {
128 .sin_family = AF_INET,
129 .sin_port = htons(NFS_PORT),
130 };
131
132 if (location->servers[s].len <= 0 ||
J. Bruce Fields460cdbc2008-08-20 16:10:21 -0400133 valid_ipaddr4(location->servers[s].data) < 0)
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400134 continue;
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400135
136 mountdata->hostname = location->servers[s].data;
137 addr.sin_addr.s_addr = in_aton(mountdata->hostname),
138 mountdata->addr = (struct sockaddr *)&addr;
139 mountdata->addrlen = sizeof(addr);
140
141 snprintf(page, PAGE_SIZE, "%s:%s",
142 mountdata->hostname,
143 mountdata->mnt_path);
144
145 mnt = vfs_kern_mount(&nfs4_referral_fs_type, 0, page, mountdata);
146 if (!IS_ERR(mnt))
147 break;
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400148 }
149 return mnt;
150}
151
David Howellsf7b422b2006-06-09 09:34:33 -0400152/**
153 * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
154 * @mnt_parent - mountpoint of parent directory
155 * @dentry - parent directory
Chuck Lever3f43c662007-12-10 14:57:31 -0500156 * @locations - array of NFSv4 server location information
David Howellsf7b422b2006-06-09 09:34:33 -0400157 *
158 */
159static struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
160 const struct dentry *dentry,
David Howells509de812006-08-22 20:06:11 -0400161 const struct nfs4_fs_locations *locations)
David Howellsf7b422b2006-06-09 09:34:33 -0400162{
163 struct vfsmount *mnt = ERR_PTR(-ENOENT);
164 struct nfs_clone_mount mountdata = {
165 .sb = mnt_parent->mnt_sb,
166 .dentry = dentry,
167 .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
168 };
David Howells54ceac42006-08-22 20:06:13 -0400169 char *page = NULL, *page2 = NULL;
Chuck Lever3f43c662007-12-10 14:57:31 -0500170 int loc, error;
David Howellsf7b422b2006-06-09 09:34:33 -0400171
172 if (locations == NULL || locations->nlocations <= 0)
173 goto out;
174
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 dprintk("%s: referral at %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400176 dentry->d_parent->d_name.name, dentry->d_name.name);
177
David Howellsf7b422b2006-06-09 09:34:33 -0400178 page = (char *) __get_free_page(GFP_USER);
David Howells54ceac42006-08-22 20:06:13 -0400179 if (!page)
David Howellsf7b422b2006-06-09 09:34:33 -0400180 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400181
David Howellsf7b422b2006-06-09 09:34:33 -0400182 page2 = (char *) __get_free_page(GFP_USER);
David Howells54ceac42006-08-22 20:06:13 -0400183 if (!page2)
David Howellsf7b422b2006-06-09 09:34:33 -0400184 goto out;
185
David Howells54ceac42006-08-22 20:06:13 -0400186 /* Ensure fs path is a prefix of current dentry path */
187 error = nfs4_validate_fspath(mnt_parent, dentry, locations, page, page2);
188 if (error < 0) {
189 mnt = ERR_PTR(error);
190 goto out;
David Howellsf7b422b2006-06-09 09:34:33 -0400191 }
192
J. Bruce Fields460cdbc2008-08-20 16:10:21 -0400193 for (loc = 0; loc < locations->nlocations; loc++) {
David Howells509de812006-08-22 20:06:11 -0400194 const struct nfs4_fs_location *location = &locations->locations[loc];
David Howellsf7b422b2006-06-09 09:34:33 -0400195
196 if (location == NULL || location->nservers <= 0 ||
J. Bruce Fields460cdbc2008-08-20 16:10:21 -0400197 location->rootpath.ncomponents == 0)
David Howellsf7b422b2006-06-09 09:34:33 -0400198 continue;
David Howellsf7b422b2006-06-09 09:34:33 -0400199
J. Bruce Fields4ada29d2008-08-20 16:10:20 -0400200 mnt = try_location(&mountdata, page, page2, location);
201 if (!IS_ERR(mnt))
202 break;
David Howellsf7b422b2006-06-09 09:34:33 -0400203 }
204
David Howellsf7b422b2006-06-09 09:34:33 -0400205out:
David Howells54ceac42006-08-22 20:06:13 -0400206 free_page((unsigned long) page);
207 free_page((unsigned long) page2);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700208 dprintk("%s: done\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -0400209 return mnt;
210}
211
212/*
213 * nfs_do_refmount - handle crossing a referral on server
214 * @dentry - dentry of referral
215 * @nd - nameidata info
216 *
217 */
218struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
219{
David Howells54ceac42006-08-22 20:06:13 -0400220 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
David Howellsf7b422b2006-06-09 09:34:33 -0400221 struct dentry *parent;
222 struct nfs4_fs_locations *fs_locations = NULL;
223 struct page *page;
224 int err;
225
226 /* BUG_ON(IS_ROOT(dentry)); */
Harvey Harrison3110ff82008-05-02 13:42:44 -0700227 dprintk("%s: enter\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -0400228
229 page = alloc_page(GFP_KERNEL);
230 if (page == NULL)
231 goto out;
232
233 fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
234 if (fs_locations == NULL)
235 goto out_free;
236
237 /* Get locations */
David Howells54ceac42006-08-22 20:06:13 -0400238 mnt = ERR_PTR(-ENOENT);
239
David Howellsf7b422b2006-06-09 09:34:33 -0400240 parent = dget_parent(dentry);
David Howells54ceac42006-08-22 20:06:13 -0400241 dprintk("%s: getting locations for %s/%s\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700242 __func__, parent->d_name.name, dentry->d_name.name);
David Howells54ceac42006-08-22 20:06:13 -0400243
Trond Myklebustc228fd32007-01-13 02:28:11 -0500244 err = nfs4_proc_fs_locations(parent->d_inode, &dentry->d_name, fs_locations, page);
David Howellsf7b422b2006-06-09 09:34:33 -0400245 dput(parent);
David Howells54ceac42006-08-22 20:06:13 -0400246 if (err != 0 ||
247 fs_locations->nlocations <= 0 ||
David Howellsf7b422b2006-06-09 09:34:33 -0400248 fs_locations->fs_path.ncomponents <= 0)
249 goto out_free;
250
251 mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
252out_free:
253 __free_page(page);
254 kfree(fs_locations);
255out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700256 dprintk("%s: done\n", __func__);
David Howellsf7b422b2006-06-09 09:34:33 -0400257 return mnt;
258}