blob: b7c9b2df1f299dd15d30b81d533b602d8ae09572 [file] [log] [blame]
David Howells54ceac42006-08-22 20:06:13 -04001/* 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
David Howells54ceac42006-08-22 20:06:13 -040012#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>
David Howells54ceac42006-08-22 20:06:13 -040028#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>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080033#include <linux/mnt_namespace.h>
David Howells738a3512006-07-30 14:58:27 -040034#include <linux/security.h>
David Howells54ceac42006-08-22 20:06:13 -040035
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
David Howells54ceac42006-08-22 20:06:13 -040044
45/*
Trond Myklebustb09b9412007-10-25 13:56:10 -040046 * Set the superblock root dentry.
47 * Note that this function frees the inode in case of error.
48 */
49static 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);
Trond Myklebusta10db502007-12-12 11:12:15 -050060 /*
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);
Trond Myklebustb09b9412007-10-25 13:56:10 -040071 }
72 return 0;
73}
74
75/*
David Howells54ceac42006-08-22 20:06:13 -040076 * get an NFS2/NFS3 root dentry from the root filehandle
77 */
78struct 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
David Howells54ceac42006-08-22 20:06:13 -040087 /* 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");
David Howellse231c2e2008-02-07 00:15:26 -080099 return ERR_CAST(inode);
David Howells54ceac42006-08-22 20:06:13 -0400100 }
101
Trond Myklebustb09b9412007-10-25 13:56:10 -0400102 error = nfs_superblock_set_dummy_root(sb, inode);
103 if (error != 0)
104 return ERR_PTR(error);
105
David Howells54ceac42006-08-22 20:06:13 -0400106 /* 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 */
Christoph Hellwig44003722008-08-11 15:49:04 +0200110 mntroot = d_obtain_alias(inode);
111 if (IS_ERR(mntroot)) {
David Howells54ceac42006-08-22 20:06:13 -0400112 dprintk("nfs_get_root: get root dentry failed\n");
Christoph Hellwig44003722008-08-11 15:49:04 +0200113 return mntroot;
David Howells54ceac42006-08-22 20:06:13 -0400114 }
115
David Howells738a3512006-07-30 14:58:27 -0400116 security_d_instantiate(mntroot, inode);
117
David Howells54ceac42006-08-22 20:06:13 -0400118 if (!mntroot->d_op)
119 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
120
121 return mntroot;
122}
123
124#ifdef CONFIG_NFS_V4
125
126/*
127 * Do a simple pathwalk from the root FH of the server to the nominated target
128 * of the mountpoint
129 * - give error on symlinks
130 * - give error on ".." occurring in the path
131 * - follow traversals
132 */
133int nfs4_path_walk(struct nfs_server *server,
134 struct nfs_fh *mntfh,
135 const char *path)
136{
137 struct nfs_fsinfo fsinfo;
138 struct nfs_fattr fattr;
139 struct nfs_fh lastfh;
140 struct qstr name;
141 int ret;
David Howells54ceac42006-08-22 20:06:13 -0400142
143 dprintk("--> nfs4_path_walk(,,%s)\n", path);
144
145 fsinfo.fattr = &fattr;
146 nfs_fattr_init(&fattr);
147
Trond Myklebustfaebf4e2007-01-13 02:28:11 -0500148 /* Eat leading slashes */
149 while (*path == '/')
150 path++;
David Howells54ceac42006-08-22 20:06:13 -0400151
152 /* Start by getting the root filehandle from the server */
153 ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
154 if (ret < 0) {
155 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
156 return ret;
157 }
158
159 if (fattr.type != NFDIR) {
160 printk(KERN_ERR "nfs4_get_root:"
161 " getroot encountered non-directory\n");
162 return -ENOTDIR;
163 }
164
Trond Myklebustfaebf4e2007-01-13 02:28:11 -0500165 /* FIXME: It is quite valid for the server to return a referral here */
David Howells54ceac42006-08-22 20:06:13 -0400166 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
167 printk(KERN_ERR "nfs4_get_root:"
168 " getroot obtained referral\n");
169 return -EREMOTE;
170 }
171
172next_component:
173 dprintk("Next: %s\n", path);
174
175 /* extract the next bit of the path */
176 if (!*path)
177 goto path_walk_complete;
178
179 name.name = path;
180 while (*path && *path != '/')
181 path++;
182 name.len = path - (const char *) name.name;
183
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400184 if (name.len > NFS4_MAXNAMLEN)
185 return -ENAMETOOLONG;
186
David Howells54ceac42006-08-22 20:06:13 -0400187eat_dot_dir:
188 while (*path == '/')
189 path++;
190
191 if (path[0] == '.' && (path[1] == '/' || !path[1])) {
192 path += 2;
193 goto eat_dot_dir;
194 }
195
Trond Myklebustfaebf4e2007-01-13 02:28:11 -0500196 /* FIXME: Why shouldn't the user be able to use ".." in the path? */
David Howells54ceac42006-08-22 20:06:13 -0400197 if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
198 ) {
199 printk(KERN_ERR "nfs4_get_root:"
200 " Mount path contains reference to \"..\"\n");
201 return -EINVAL;
202 }
203
204 /* lookup the next FH in the sequence */
205 memcpy(&lastfh, mntfh, sizeof(lastfh));
206
207 dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
208
209 ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
210 mntfh, &fattr);
211 if (ret < 0) {
212 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
213 return ret;
214 }
215
216 if (fattr.type != NFDIR) {
217 printk(KERN_ERR "nfs4_get_root:"
218 " lookupfh encountered non-directory\n");
219 return -ENOTDIR;
220 }
221
Trond Myklebustfaebf4e2007-01-13 02:28:11 -0500222 /* FIXME: Referrals are quite valid here too */
David Howells54ceac42006-08-22 20:06:13 -0400223 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
224 printk(KERN_ERR "nfs4_get_root:"
225 " lookupfh obtained referral\n");
226 return -EREMOTE;
227 }
228
229 goto next_component;
230
231path_walk_complete:
232 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
233 dprintk("<-- nfs4_path_walk() = 0\n");
234 return 0;
235}
236
237/*
238 * get an NFS4 root dentry from the root filehandle
239 */
240struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
241{
242 struct nfs_server *server = NFS_SB(sb);
243 struct nfs_fattr fattr;
244 struct dentry *mntroot;
245 struct inode *inode;
246 int error;
247
248 dprintk("--> nfs4_get_root()\n");
249
David Howells54ceac42006-08-22 20:06:13 -0400250 /* get the info about the server and filesystem */
251 error = nfs4_server_capabilities(server, mntfh);
252 if (error < 0) {
253 dprintk("nfs_get_root: getcaps error = %d\n",
254 -error);
255 return ERR_PTR(error);
256 }
257
258 /* get the actual root for this mount */
259 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
260 if (error < 0) {
261 dprintk("nfs_get_root: getattr error = %d\n", -error);
262 return ERR_PTR(error);
263 }
264
265 inode = nfs_fhget(sb, mntfh, &fattr);
266 if (IS_ERR(inode)) {
267 dprintk("nfs_get_root: get root inode failed\n");
David Howellse231c2e2008-02-07 00:15:26 -0800268 return ERR_CAST(inode);
David Howells54ceac42006-08-22 20:06:13 -0400269 }
270
Trond Myklebustb09b9412007-10-25 13:56:10 -0400271 error = nfs_superblock_set_dummy_root(sb, inode);
272 if (error != 0)
273 return ERR_PTR(error);
274
David Howells54ceac42006-08-22 20:06:13 -0400275 /* root dentries normally start off anonymous and get spliced in later
276 * if the dentry tree reaches them; however if the dentry already
277 * exists, we'll pick it up at this point and use it as the root
278 */
Christoph Hellwig44003722008-08-11 15:49:04 +0200279 mntroot = d_obtain_alias(inode);
280 if (IS_ERR(mntroot)) {
David Howells54ceac42006-08-22 20:06:13 -0400281 dprintk("nfs_get_root: get root dentry failed\n");
Christoph Hellwig44003722008-08-11 15:49:04 +0200282 return mntroot;
David Howells54ceac42006-08-22 20:06:13 -0400283 }
284
David Howells738a3512006-07-30 14:58:27 -0400285 security_d_instantiate(mntroot, inode);
286
David Howells54ceac42006-08-22 20:06:13 -0400287 if (!mntroot->d_op)
288 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
289
290 dprintk("<-- nfs4_get_root()\n");
291 return mntroot;
292}
293
294#endif /* CONFIG_NFS_V4 */