exportfs: remove old methods
[wrt350n-kernel.git] / fs / nfs / getroot.c
blob522e5ad4d8ad9b3946e201926c7f799ddf0f2e36
1 /* getroot.c: get the root dentry for an NFS mount
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/init.h>
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>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
39 #include "nfs4_fs.h"
40 #include "delegation.h"
41 #include "internal.h"
43 #define NFSDBG_FACILITY NFSDBG_CLIENT
46 * get an NFS2/NFS3 root dentry from the root filehandle
48 struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
50 struct nfs_server *server = NFS_SB(sb);
51 struct nfs_fsinfo fsinfo;
52 struct nfs_fattr fattr;
53 struct dentry *mntroot;
54 struct inode *inode;
55 int error;
57 /* create a dummy root dentry with dummy inode for this superblock */
58 if (!sb->s_root) {
59 struct nfs_fh dummyfh;
60 struct dentry *root;
61 struct inode *iroot;
63 memset(&dummyfh, 0, sizeof(dummyfh));
64 memset(&fattr, 0, sizeof(fattr));
65 nfs_fattr_init(&fattr);
66 fattr.valid = NFS_ATTR_FATTR;
67 fattr.type = NFDIR;
68 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
69 fattr.nlink = 2;
71 iroot = nfs_fhget(sb, &dummyfh, &fattr);
72 if (IS_ERR(iroot))
73 return ERR_PTR(PTR_ERR(iroot));
75 root = d_alloc_root(iroot);
76 if (!root) {
77 iput(iroot);
78 return ERR_PTR(-ENOMEM);
81 sb->s_root = root;
84 /* get the actual root for this mount */
85 fsinfo.fattr = &fattr;
87 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
88 if (error < 0) {
89 dprintk("nfs_get_root: getattr error = %d\n", -error);
90 return ERR_PTR(error);
93 inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
94 if (IS_ERR(inode)) {
95 dprintk("nfs_get_root: get root inode failed\n");
96 return ERR_PTR(PTR_ERR(inode));
99 /* root dentries normally start off anonymous and get spliced in later
100 * if the dentry tree reaches them; however if the dentry already
101 * exists, we'll pick it up at this point and use it as the root
103 mntroot = d_alloc_anon(inode);
104 if (!mntroot) {
105 iput(inode);
106 dprintk("nfs_get_root: get root dentry failed\n");
107 return ERR_PTR(-ENOMEM);
110 security_d_instantiate(mntroot, inode);
112 if (!mntroot->d_op)
113 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
115 return mntroot;
118 #ifdef CONFIG_NFS_V4
121 * Do a simple pathwalk from the root FH of the server to the nominated target
122 * of the mountpoint
123 * - give error on symlinks
124 * - give error on ".." occurring in the path
125 * - follow traversals
127 int nfs4_path_walk(struct nfs_server *server,
128 struct nfs_fh *mntfh,
129 const char *path)
131 struct nfs_fsinfo fsinfo;
132 struct nfs_fattr fattr;
133 struct nfs_fh lastfh;
134 struct qstr name;
135 int ret;
137 dprintk("--> nfs4_path_walk(,,%s)\n", path);
139 fsinfo.fattr = &fattr;
140 nfs_fattr_init(&fattr);
142 /* Eat leading slashes */
143 while (*path == '/')
144 path++;
146 /* Start by getting the root filehandle from the server */
147 ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
148 if (ret < 0) {
149 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
150 return ret;
153 if (fattr.type != NFDIR) {
154 printk(KERN_ERR "nfs4_get_root:"
155 " getroot encountered non-directory\n");
156 return -ENOTDIR;
159 /* FIXME: It is quite valid for the server to return a referral here */
160 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
161 printk(KERN_ERR "nfs4_get_root:"
162 " getroot obtained referral\n");
163 return -EREMOTE;
166 next_component:
167 dprintk("Next: %s\n", path);
169 /* extract the next bit of the path */
170 if (!*path)
171 goto path_walk_complete;
173 name.name = path;
174 while (*path && *path != '/')
175 path++;
176 name.len = path - (const char *) name.name;
178 if (name.len > NFS4_MAXNAMLEN)
179 return -ENAMETOOLONG;
181 eat_dot_dir:
182 while (*path == '/')
183 path++;
185 if (path[0] == '.' && (path[1] == '/' || !path[1])) {
186 path += 2;
187 goto eat_dot_dir;
190 /* FIXME: Why shouldn't the user be able to use ".." in the path? */
191 if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
193 printk(KERN_ERR "nfs4_get_root:"
194 " Mount path contains reference to \"..\"\n");
195 return -EINVAL;
198 /* lookup the next FH in the sequence */
199 memcpy(&lastfh, mntfh, sizeof(lastfh));
201 dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
203 ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
204 mntfh, &fattr);
205 if (ret < 0) {
206 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
207 return ret;
210 if (fattr.type != NFDIR) {
211 printk(KERN_ERR "nfs4_get_root:"
212 " lookupfh encountered non-directory\n");
213 return -ENOTDIR;
216 /* FIXME: Referrals are quite valid here too */
217 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
218 printk(KERN_ERR "nfs4_get_root:"
219 " lookupfh obtained referral\n");
220 return -EREMOTE;
223 goto next_component;
225 path_walk_complete:
226 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
227 dprintk("<-- nfs4_path_walk() = 0\n");
228 return 0;
232 * get an NFS4 root dentry from the root filehandle
234 struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
236 struct nfs_server *server = NFS_SB(sb);
237 struct nfs_fattr fattr;
238 struct dentry *mntroot;
239 struct inode *inode;
240 int error;
242 dprintk("--> nfs4_get_root()\n");
244 /* create a dummy root dentry with dummy inode for this superblock */
245 if (!sb->s_root) {
246 struct nfs_fh dummyfh;
247 struct dentry *root;
248 struct inode *iroot;
250 memset(&dummyfh, 0, sizeof(dummyfh));
251 memset(&fattr, 0, sizeof(fattr));
252 nfs_fattr_init(&fattr);
253 fattr.valid = NFS_ATTR_FATTR;
254 fattr.type = NFDIR;
255 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
256 fattr.nlink = 2;
258 iroot = nfs_fhget(sb, &dummyfh, &fattr);
259 if (IS_ERR(iroot))
260 return ERR_PTR(PTR_ERR(iroot));
262 root = d_alloc_root(iroot);
263 if (!root) {
264 iput(iroot);
265 return ERR_PTR(-ENOMEM);
268 sb->s_root = root;
271 /* get the info about the server and filesystem */
272 error = nfs4_server_capabilities(server, mntfh);
273 if (error < 0) {
274 dprintk("nfs_get_root: getcaps error = %d\n",
275 -error);
276 return ERR_PTR(error);
279 /* get the actual root for this mount */
280 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
281 if (error < 0) {
282 dprintk("nfs_get_root: getattr error = %d\n", -error);
283 return ERR_PTR(error);
286 inode = nfs_fhget(sb, mntfh, &fattr);
287 if (IS_ERR(inode)) {
288 dprintk("nfs_get_root: get root inode failed\n");
289 return ERR_PTR(PTR_ERR(inode));
292 /* root dentries normally start off anonymous and get spliced in later
293 * if the dentry tree reaches them; however if the dentry already
294 * exists, we'll pick it up at this point and use it as the root
296 mntroot = d_alloc_anon(inode);
297 if (!mntroot) {
298 iput(inode);
299 dprintk("nfs_get_root: get root dentry failed\n");
300 return ERR_PTR(-ENOMEM);
303 security_d_instantiate(mntroot, inode);
305 if (!mntroot->d_op)
306 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
308 dprintk("<-- nfs4_get_root()\n");
309 return mntroot;
312 #endif /* CONFIG_NFS_V4 */