xref: /linux/fs/nfs/getroot.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /* getroot.c: get the root dentry for an NFS mount
2  *
3  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 
15 #include <linux/time.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/sunrpc/stats.h>
24 #include <linux/nfs_fs.h>
25 #include <linux/nfs_mount.h>
26 #include <linux/nfs4_mount.h>
27 #include <linux/lockd/bind.h>
28 #include <linux/smp_lock.h>
29 #include <linux/seq_file.h>
30 #include <linux/mount.h>
31 #include <linux/nfs_idmap.h>
32 #include <linux/vfs.h>
33 #include <linux/namei.h>
34 #include <linux/mnt_namespace.h>
35 #include <linux/security.h>
36 
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 
40 #include "nfs4_fs.h"
41 #include "delegation.h"
42 #include "internal.h"
43 
44 #define NFSDBG_FACILITY		NFSDBG_CLIENT
45 #define NFS_PARANOIA 1
46 
47 /*
48  * get an NFS2/NFS3 root dentry from the root filehandle
49  */
50 struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
51 {
52 	struct nfs_server *server = NFS_SB(sb);
53 	struct nfs_fsinfo fsinfo;
54 	struct nfs_fattr fattr;
55 	struct dentry *mntroot;
56 	struct inode *inode;
57 	int error;
58 
59 	/* create a dummy root dentry with dummy inode for this superblock */
60 	if (!sb->s_root) {
61 		struct nfs_fh dummyfh;
62 		struct dentry *root;
63 		struct inode *iroot;
64 
65 		memset(&dummyfh, 0, sizeof(dummyfh));
66 		memset(&fattr, 0, sizeof(fattr));
67 		nfs_fattr_init(&fattr);
68 		fattr.valid = NFS_ATTR_FATTR;
69 		fattr.type = NFDIR;
70 		fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
71 		fattr.nlink = 2;
72 
73 		iroot = nfs_fhget(sb, &dummyfh, &fattr);
74 		if (IS_ERR(iroot))
75 			return ERR_PTR(PTR_ERR(iroot));
76 
77 		root = d_alloc_root(iroot);
78 		if (!root) {
79 			iput(iroot);
80 			return ERR_PTR(-ENOMEM);
81 		}
82 
83 		sb->s_root = root;
84 	}
85 
86 	/* get the actual root for this mount */
87 	fsinfo.fattr = &fattr;
88 
89 	error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
90 	if (error < 0) {
91 		dprintk("nfs_get_root: getattr error = %d\n", -error);
92 		return ERR_PTR(error);
93 	}
94 
95 	inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
96 	if (IS_ERR(inode)) {
97 		dprintk("nfs_get_root: get root inode failed\n");
98 		return ERR_PTR(PTR_ERR(inode));
99 	}
100 
101 	/* root dentries normally start off anonymous and get spliced in later
102 	 * if the dentry tree reaches them; however if the dentry already
103 	 * exists, we'll pick it up at this point and use it as the root
104 	 */
105 	mntroot = d_alloc_anon(inode);
106 	if (!mntroot) {
107 		iput(inode);
108 		dprintk("nfs_get_root: get root dentry failed\n");
109 		return ERR_PTR(-ENOMEM);
110 	}
111 
112 	security_d_instantiate(mntroot, inode);
113 
114 	if (!mntroot->d_op)
115 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
116 
117 	return mntroot;
118 }
119 
120 #ifdef CONFIG_NFS_V4
121 
122 /*
123  * Do a simple pathwalk from the root FH of the server to the nominated target
124  * of the mountpoint
125  * - give error on symlinks
126  * - give error on ".." occurring in the path
127  * - follow traversals
128  */
129 int nfs4_path_walk(struct nfs_server *server,
130 		   struct nfs_fh *mntfh,
131 		   const char *path)
132 {
133 	struct nfs_fsinfo fsinfo;
134 	struct nfs_fattr fattr;
135 	struct nfs_fh lastfh;
136 	struct qstr name;
137 	int ret;
138 
139 	dprintk("--> nfs4_path_walk(,,%s)\n", path);
140 
141 	fsinfo.fattr = &fattr;
142 	nfs_fattr_init(&fattr);
143 
144 	/* Eat leading slashes */
145 	while (*path == '/')
146 		path++;
147 
148 	/* Start by getting the root filehandle from the server */
149 	ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
150 	if (ret < 0) {
151 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
152 		return ret;
153 	}
154 
155 	if (fattr.type != NFDIR) {
156 		printk(KERN_ERR "nfs4_get_root:"
157 		       " getroot encountered non-directory\n");
158 		return -ENOTDIR;
159 	}
160 
161 	/* FIXME: It is quite valid for the server to return a referral here */
162 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
163 		printk(KERN_ERR "nfs4_get_root:"
164 		       " getroot obtained referral\n");
165 		return -EREMOTE;
166 	}
167 
168 next_component:
169 	dprintk("Next: %s\n", path);
170 
171 	/* extract the next bit of the path */
172 	if (!*path)
173 		goto path_walk_complete;
174 
175 	name.name = path;
176 	while (*path && *path != '/')
177 		path++;
178 	name.len = path - (const char *) name.name;
179 
180 eat_dot_dir:
181 	while (*path == '/')
182 		path++;
183 
184 	if (path[0] == '.' && (path[1] == '/' || !path[1])) {
185 		path += 2;
186 		goto eat_dot_dir;
187 	}
188 
189 	/* FIXME: Why shouldn't the user be able to use ".." in the path? */
190 	if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
191 	    ) {
192 		printk(KERN_ERR "nfs4_get_root:"
193 		       " Mount path contains reference to \"..\"\n");
194 		return -EINVAL;
195 	}
196 
197 	/* lookup the next FH in the sequence */
198 	memcpy(&lastfh, mntfh, sizeof(lastfh));
199 
200 	dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
201 
202 	ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
203 						    mntfh, &fattr);
204 	if (ret < 0) {
205 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
206 		return ret;
207 	}
208 
209 	if (fattr.type != NFDIR) {
210 		printk(KERN_ERR "nfs4_get_root:"
211 		       " lookupfh encountered non-directory\n");
212 		return -ENOTDIR;
213 	}
214 
215 	/* FIXME: Referrals are quite valid here too */
216 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
217 		printk(KERN_ERR "nfs4_get_root:"
218 		       " lookupfh obtained referral\n");
219 		return -EREMOTE;
220 	}
221 
222 	goto next_component;
223 
224 path_walk_complete:
225 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
226 	dprintk("<-- nfs4_path_walk() = 0\n");
227 	return 0;
228 }
229 
230 /*
231  * get an NFS4 root dentry from the root filehandle
232  */
233 struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
234 {
235 	struct nfs_server *server = NFS_SB(sb);
236 	struct nfs_fattr fattr;
237 	struct dentry *mntroot;
238 	struct inode *inode;
239 	int error;
240 
241 	dprintk("--> nfs4_get_root()\n");
242 
243 	/* create a dummy root dentry with dummy inode for this superblock */
244 	if (!sb->s_root) {
245 		struct nfs_fh dummyfh;
246 		struct dentry *root;
247 		struct inode *iroot;
248 
249 		memset(&dummyfh, 0, sizeof(dummyfh));
250 		memset(&fattr, 0, sizeof(fattr));
251 		nfs_fattr_init(&fattr);
252 		fattr.valid = NFS_ATTR_FATTR;
253 		fattr.type = NFDIR;
254 		fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
255 		fattr.nlink = 2;
256 
257 		iroot = nfs_fhget(sb, &dummyfh, &fattr);
258 		if (IS_ERR(iroot))
259 			return ERR_PTR(PTR_ERR(iroot));
260 
261 		root = d_alloc_root(iroot);
262 		if (!root) {
263 			iput(iroot);
264 			return ERR_PTR(-ENOMEM);
265 		}
266 
267 		sb->s_root = root;
268 	}
269 
270 	/* get the info about the server and filesystem */
271 	error = nfs4_server_capabilities(server, mntfh);
272 	if (error < 0) {
273 		dprintk("nfs_get_root: getcaps error = %d\n",
274 			-error);
275 		return ERR_PTR(error);
276 	}
277 
278 	/* get the actual root for this mount */
279 	error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
280 	if (error < 0) {
281 		dprintk("nfs_get_root: getattr error = %d\n", -error);
282 		return ERR_PTR(error);
283 	}
284 
285 	inode = nfs_fhget(sb, mntfh, &fattr);
286 	if (IS_ERR(inode)) {
287 		dprintk("nfs_get_root: get root inode failed\n");
288 		return ERR_PTR(PTR_ERR(inode));
289 	}
290 
291 	/* root dentries normally start off anonymous and get spliced in later
292 	 * if the dentry tree reaches them; however if the dentry already
293 	 * exists, we'll pick it up at this point and use it as the root
294 	 */
295 	mntroot = d_alloc_anon(inode);
296 	if (!mntroot) {
297 		iput(inode);
298 		dprintk("nfs_get_root: get root dentry failed\n");
299 		return ERR_PTR(-ENOMEM);
300 	}
301 
302 	security_d_instantiate(mntroot, inode);
303 
304 	if (!mntroot->d_op)
305 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
306 
307 	dprintk("<-- nfs4_get_root()\n");
308 	return mntroot;
309 }
310 
311 #endif /* CONFIG_NFS_V4 */
312