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>
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>
40 #include "delegation.h"
43 #define NFSDBG_FACILITY NFSDBG_CLIENT
44 #define NFS_PARANOIA 1
47 * get an NFS2/NFS3 root dentry from the root filehandle
49 struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
51 struct nfs_server *server = NFS_SB(sb);
52 struct nfs_fsinfo fsinfo;
53 struct nfs_fattr fattr;
54 struct dentry *mntroot;
58 /* create a dummy root dentry with dummy inode for this superblock */
60 struct nfs_fh dummyfh;
64 memset(&dummyfh, 0, sizeof(dummyfh));
65 memset(&fattr, 0, sizeof(fattr));
66 nfs_fattr_init(&fattr);
67 fattr.valid = NFS_ATTR_FATTR;
69 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
72 iroot = nfs_fhget(sb, &dummyfh, &fattr);
74 return ERR_PTR(PTR_ERR(iroot));
76 root = d_alloc_root(iroot);
79 return ERR_PTR(-ENOMEM);
85 /* get the actual root for this mount */
86 fsinfo.fattr = &fattr;
88 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
90 dprintk("nfs_get_root: getattr error = %d\n", -error);
91 return ERR_PTR(error);
94 inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
96 dprintk("nfs_get_root: get root inode failed\n");
97 return ERR_PTR(PTR_ERR(inode));
100 /* root dentries normally start off anonymous and get spliced in later
101 * if the dentry tree reaches them; however if the dentry already
102 * exists, we'll pick it up at this point and use it as the root
104 mntroot = d_alloc_anon(inode);
107 dprintk("nfs_get_root: get root dentry failed\n");
108 return ERR_PTR(-ENOMEM);
111 security_d_instantiate(mntroot, inode);
114 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
122 * Do a simple pathwalk from the root FH of the server to the nominated target
124 * - give error on symlinks
125 * - give error on ".." occurring in the path
126 * - follow traversals
128 int nfs4_path_walk(struct nfs_server *server,
129 struct nfs_fh *mntfh,
132 struct nfs_fsinfo fsinfo;
133 struct nfs_fattr fattr;
134 struct nfs_fh lastfh;
138 dprintk("--> nfs4_path_walk(,,%s)\n", path);
140 fsinfo.fattr = &fattr;
141 nfs_fattr_init(&fattr);
143 /* Eat leading slashes */
147 /* Start by getting the root filehandle from the server */
148 ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
150 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
154 if (fattr.type != NFDIR) {
155 printk(KERN_ERR "nfs4_get_root:"
156 " getroot encountered non-directory\n");
160 /* FIXME: It is quite valid for the server to return a referral here */
161 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
162 printk(KERN_ERR "nfs4_get_root:"
163 " getroot obtained referral\n");
168 dprintk("Next: %s\n", path);
170 /* extract the next bit of the path */
172 goto path_walk_complete;
175 while (*path && *path != '/')
177 name.len = path - (const char *) name.name;
183 if (path[0] == '.' && (path[1] == '/' || !path[1])) {
188 /* FIXME: Why shouldn't the user be able to use ".." in the path? */
189 if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
191 printk(KERN_ERR "nfs4_get_root:"
192 " Mount path contains reference to \"..\"\n");
196 /* lookup the next FH in the sequence */
197 memcpy(&lastfh, mntfh, sizeof(lastfh));
199 dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
201 ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
204 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
208 if (fattr.type != NFDIR) {
209 printk(KERN_ERR "nfs4_get_root:"
210 " lookupfh encountered non-directory\n");
214 /* FIXME: Referrals are quite valid here too */
215 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
216 printk(KERN_ERR "nfs4_get_root:"
217 " lookupfh obtained referral\n");
224 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
225 dprintk("<-- nfs4_path_walk() = 0\n");
230 * get an NFS4 root dentry from the root filehandle
232 struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
234 struct nfs_server *server = NFS_SB(sb);
235 struct nfs_fattr fattr;
236 struct dentry *mntroot;
240 dprintk("--> nfs4_get_root()\n");
242 /* create a dummy root dentry with dummy inode for this superblock */
244 struct nfs_fh dummyfh;
248 memset(&dummyfh, 0, sizeof(dummyfh));
249 memset(&fattr, 0, sizeof(fattr));
250 nfs_fattr_init(&fattr);
251 fattr.valid = NFS_ATTR_FATTR;
253 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
256 iroot = nfs_fhget(sb, &dummyfh, &fattr);
258 return ERR_PTR(PTR_ERR(iroot));
260 root = d_alloc_root(iroot);
263 return ERR_PTR(-ENOMEM);
269 /* get the info about the server and filesystem */
270 error = nfs4_server_capabilities(server, mntfh);
272 dprintk("nfs_get_root: getcaps error = %d\n",
274 return ERR_PTR(error);
277 /* get the actual root for this mount */
278 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
280 dprintk("nfs_get_root: getattr error = %d\n", -error);
281 return ERR_PTR(error);
284 inode = nfs_fhget(sb, mntfh, &fattr);
286 dprintk("nfs_get_root: get root inode failed\n");
287 return ERR_PTR(PTR_ERR(inode));
290 /* root dentries normally start off anonymous and get spliced in later
291 * if the dentry tree reaches them; however if the dentry already
292 * exists, we'll pick it up at this point and use it as the root
294 mntroot = d_alloc_anon(inode);
297 dprintk("nfs_get_root: get root dentry failed\n");
298 return ERR_PTR(-ENOMEM);
301 security_d_instantiate(mntroot, inode);
304 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
306 dprintk("<-- nfs4_get_root()\n");
310 #endif /* CONFIG_NFS_V4 */