xref: /openbmc/linux/fs/nfs/getroot.c (revision f42b3800)
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/seq_file.h>
29 #include <linux/mount.h>
30 #include <linux/nfs_idmap.h>
31 #include <linux/vfs.h>
32 #include <linux/namei.h>
33 #include <linux/mnt_namespace.h>
34 #include <linux/security.h>
35 
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 
39 #include "nfs4_fs.h"
40 #include "delegation.h"
41 #include "internal.h"
42 
43 #define NFSDBG_FACILITY		NFSDBG_CLIENT
44 
45 /*
46  * Set the superblock root dentry.
47  * Note that this function frees the inode in case of error.
48  */
49 static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
50 {
51 	/* The mntroot acts as the dummy root dentry for this superblock */
52 	if (sb->s_root == NULL) {
53 		sb->s_root = d_alloc_root(inode);
54 		if (sb->s_root == NULL) {
55 			iput(inode);
56 			return -ENOMEM;
57 		}
58 		/* Circumvent igrab(): we know the inode is not being freed */
59 		atomic_inc(&inode->i_count);
60 		/*
61 		 * Ensure that this dentry is invisible to d_find_alias().
62 		 * Otherwise, it may be spliced into the tree by
63 		 * d_materialise_unique if a parent directory from the same
64 		 * filesystem gets mounted at a later time.
65 		 * This again causes shrink_dcache_for_umount_subtree() to
66 		 * Oops, since the test for IS_ROOT() will fail.
67 		 */
68 		spin_lock(&dcache_lock);
69 		list_del_init(&sb->s_root->d_alias);
70 		spin_unlock(&dcache_lock);
71 	}
72 	return 0;
73 }
74 
75 /*
76  * get an NFS2/NFS3 root dentry from the root filehandle
77  */
78 struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
79 {
80 	struct nfs_server *server = NFS_SB(sb);
81 	struct nfs_fsinfo fsinfo;
82 	struct nfs_fattr fattr;
83 	struct dentry *mntroot;
84 	struct inode *inode;
85 	int error;
86 
87 	/* get the actual root for this mount */
88 	fsinfo.fattr = &fattr;
89 
90 	error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
91 	if (error < 0) {
92 		dprintk("nfs_get_root: getattr error = %d\n", -error);
93 		return ERR_PTR(error);
94 	}
95 
96 	inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
97 	if (IS_ERR(inode)) {
98 		dprintk("nfs_get_root: get root inode failed\n");
99 		return ERR_CAST(inode);
100 	}
101 
102 	error = nfs_superblock_set_dummy_root(sb, inode);
103 	if (error != 0)
104 		return ERR_PTR(error);
105 
106 	/* root dentries normally start off anonymous and get spliced in later
107 	 * if the dentry tree reaches them; however if the dentry already
108 	 * exists, we'll pick it up at this point and use it as the root
109 	 */
110 	mntroot = d_alloc_anon(inode);
111 	if (!mntroot) {
112 		iput(inode);
113 		dprintk("nfs_get_root: get root dentry failed\n");
114 		return ERR_PTR(-ENOMEM);
115 	}
116 
117 	security_d_instantiate(mntroot, inode);
118 
119 	if (!mntroot->d_op)
120 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
121 
122 	return mntroot;
123 }
124 
125 #ifdef CONFIG_NFS_V4
126 
127 /*
128  * Do a simple pathwalk from the root FH of the server to the nominated target
129  * of the mountpoint
130  * - give error on symlinks
131  * - give error on ".." occurring in the path
132  * - follow traversals
133  */
134 int nfs4_path_walk(struct nfs_server *server,
135 		   struct nfs_fh *mntfh,
136 		   const char *path)
137 {
138 	struct nfs_fsinfo fsinfo;
139 	struct nfs_fattr fattr;
140 	struct nfs_fh lastfh;
141 	struct qstr name;
142 	int ret;
143 
144 	dprintk("--> nfs4_path_walk(,,%s)\n", path);
145 
146 	fsinfo.fattr = &fattr;
147 	nfs_fattr_init(&fattr);
148 
149 	/* Eat leading slashes */
150 	while (*path == '/')
151 		path++;
152 
153 	/* Start by getting the root filehandle from the server */
154 	ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
155 	if (ret < 0) {
156 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
157 		return ret;
158 	}
159 
160 	if (fattr.type != NFDIR) {
161 		printk(KERN_ERR "nfs4_get_root:"
162 		       " getroot encountered non-directory\n");
163 		return -ENOTDIR;
164 	}
165 
166 	/* FIXME: It is quite valid for the server to return a referral here */
167 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
168 		printk(KERN_ERR "nfs4_get_root:"
169 		       " getroot obtained referral\n");
170 		return -EREMOTE;
171 	}
172 
173 next_component:
174 	dprintk("Next: %s\n", path);
175 
176 	/* extract the next bit of the path */
177 	if (!*path)
178 		goto path_walk_complete;
179 
180 	name.name = path;
181 	while (*path && *path != '/')
182 		path++;
183 	name.len = path - (const char *) name.name;
184 
185 	if (name.len > NFS4_MAXNAMLEN)
186 		return -ENAMETOOLONG;
187 
188 eat_dot_dir:
189 	while (*path == '/')
190 		path++;
191 
192 	if (path[0] == '.' && (path[1] == '/' || !path[1])) {
193 		path += 2;
194 		goto eat_dot_dir;
195 	}
196 
197 	/* FIXME: Why shouldn't the user be able to use ".." in the path? */
198 	if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
199 	    ) {
200 		printk(KERN_ERR "nfs4_get_root:"
201 		       " Mount path contains reference to \"..\"\n");
202 		return -EINVAL;
203 	}
204 
205 	/* lookup the next FH in the sequence */
206 	memcpy(&lastfh, mntfh, sizeof(lastfh));
207 
208 	dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
209 
210 	ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
211 						    mntfh, &fattr);
212 	if (ret < 0) {
213 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
214 		return ret;
215 	}
216 
217 	if (fattr.type != NFDIR) {
218 		printk(KERN_ERR "nfs4_get_root:"
219 		       " lookupfh encountered non-directory\n");
220 		return -ENOTDIR;
221 	}
222 
223 	/* FIXME: Referrals are quite valid here too */
224 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
225 		printk(KERN_ERR "nfs4_get_root:"
226 		       " lookupfh obtained referral\n");
227 		return -EREMOTE;
228 	}
229 
230 	goto next_component;
231 
232 path_walk_complete:
233 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
234 	dprintk("<-- nfs4_path_walk() = 0\n");
235 	return 0;
236 }
237 
238 /*
239  * get an NFS4 root dentry from the root filehandle
240  */
241 struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
242 {
243 	struct nfs_server *server = NFS_SB(sb);
244 	struct nfs_fattr fattr;
245 	struct dentry *mntroot;
246 	struct inode *inode;
247 	int error;
248 
249 	dprintk("--> nfs4_get_root()\n");
250 
251 	/* get the info about the server and filesystem */
252 	error = nfs4_server_capabilities(server, mntfh);
253 	if (error < 0) {
254 		dprintk("nfs_get_root: getcaps error = %d\n",
255 			-error);
256 		return ERR_PTR(error);
257 	}
258 
259 	/* get the actual root for this mount */
260 	error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
261 	if (error < 0) {
262 		dprintk("nfs_get_root: getattr error = %d\n", -error);
263 		return ERR_PTR(error);
264 	}
265 
266 	inode = nfs_fhget(sb, mntfh, &fattr);
267 	if (IS_ERR(inode)) {
268 		dprintk("nfs_get_root: get root inode failed\n");
269 		return ERR_CAST(inode);
270 	}
271 
272 	error = nfs_superblock_set_dummy_root(sb, inode);
273 	if (error != 0)
274 		return ERR_PTR(error);
275 
276 	/* root dentries normally start off anonymous and get spliced in later
277 	 * if the dentry tree reaches them; however if the dentry already
278 	 * exists, we'll pick it up at this point and use it as the root
279 	 */
280 	mntroot = d_alloc_anon(inode);
281 	if (!mntroot) {
282 		iput(inode);
283 		dprintk("nfs_get_root: get root dentry failed\n");
284 		return ERR_PTR(-ENOMEM);
285 	}
286 
287 	security_d_instantiate(mntroot, inode);
288 
289 	if (!mntroot->d_op)
290 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
291 
292 	dprintk("<-- nfs4_get_root()\n");
293 	return mntroot;
294 }
295 
296 #endif /* CONFIG_NFS_V4 */
297