Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[sfrench/cifs-2.6.git] / fs / nfs / getroot.c
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 #define NFS_PARANOIA 1
45
46 /*
47  * get an NFS2/NFS3 root dentry from the root filehandle
48  */
49 struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
50 {
51         struct nfs_server *server = NFS_SB(sb);
52         struct nfs_fsinfo fsinfo;
53         struct nfs_fattr fattr;
54         struct dentry *mntroot;
55         struct inode *inode;
56         int error;
57
58         /* create a dummy root dentry with dummy inode for this superblock */
59         if (!sb->s_root) {
60                 struct nfs_fh dummyfh;
61                 struct dentry *root;
62                 struct inode *iroot;
63
64                 memset(&dummyfh, 0, sizeof(dummyfh));
65                 memset(&fattr, 0, sizeof(fattr));
66                 nfs_fattr_init(&fattr);
67                 fattr.valid = NFS_ATTR_FATTR;
68                 fattr.type = NFDIR;
69                 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
70                 fattr.nlink = 2;
71
72                 iroot = nfs_fhget(sb, &dummyfh, &fattr);
73                 if (IS_ERR(iroot))
74                         return ERR_PTR(PTR_ERR(iroot));
75
76                 root = d_alloc_root(iroot);
77                 if (!root) {
78                         iput(iroot);
79                         return ERR_PTR(-ENOMEM);
80                 }
81
82                 sb->s_root = root;
83         }
84
85         /* get the actual root for this mount */
86         fsinfo.fattr = &fattr;
87
88         error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
89         if (error < 0) {
90                 dprintk("nfs_get_root: getattr error = %d\n", -error);
91                 return ERR_PTR(error);
92         }
93
94         inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
95         if (IS_ERR(inode)) {
96                 dprintk("nfs_get_root: get root inode failed\n");
97                 return ERR_PTR(PTR_ERR(inode));
98         }
99
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
103          */
104         mntroot = d_alloc_anon(inode);
105         if (!mntroot) {
106                 iput(inode);
107                 dprintk("nfs_get_root: get root dentry failed\n");
108                 return ERR_PTR(-ENOMEM);
109         }
110
111         security_d_instantiate(mntroot, inode);
112
113         if (!mntroot->d_op)
114                 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
115
116         return mntroot;
117 }
118
119 #ifdef CONFIG_NFS_V4
120
121 /*
122  * Do a simple pathwalk from the root FH of the server to the nominated target
123  * of the mountpoint
124  * - give error on symlinks
125  * - give error on ".." occurring in the path
126  * - follow traversals
127  */
128 int nfs4_path_walk(struct nfs_server *server,
129                    struct nfs_fh *mntfh,
130                    const char *path)
131 {
132         struct nfs_fsinfo fsinfo;
133         struct nfs_fattr fattr;
134         struct nfs_fh lastfh;
135         struct qstr name;
136         int ret;
137
138         dprintk("--> nfs4_path_walk(,,%s)\n", path);
139
140         fsinfo.fattr = &fattr;
141         nfs_fattr_init(&fattr);
142
143         /* Eat leading slashes */
144         while (*path == '/')
145                 path++;
146
147         /* Start by getting the root filehandle from the server */
148         ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
149         if (ret < 0) {
150                 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
151                 return ret;
152         }
153
154         if (fattr.type != NFDIR) {
155                 printk(KERN_ERR "nfs4_get_root:"
156                        " getroot encountered non-directory\n");
157                 return -ENOTDIR;
158         }
159
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");
164                 return -EREMOTE;
165         }
166
167 next_component:
168         dprintk("Next: %s\n", path);
169
170         /* extract the next bit of the path */
171         if (!*path)
172                 goto path_walk_complete;
173
174         name.name = path;
175         while (*path && *path != '/')
176                 path++;
177         name.len = path - (const char *) name.name;
178
179 eat_dot_dir:
180         while (*path == '/')
181                 path++;
182
183         if (path[0] == '.' && (path[1] == '/' || !path[1])) {
184                 path += 2;
185                 goto eat_dot_dir;
186         }
187
188         /* FIXME: Why shouldn't the user be able to use ".." in the path? */
189         if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
190             ) {
191                 printk(KERN_ERR "nfs4_get_root:"
192                        " Mount path contains reference to \"..\"\n");
193                 return -EINVAL;
194         }
195
196         /* lookup the next FH in the sequence */
197         memcpy(&lastfh, mntfh, sizeof(lastfh));
198
199         dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
200
201         ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
202                                                     mntfh, &fattr);
203         if (ret < 0) {
204                 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
205                 return ret;
206         }
207
208         if (fattr.type != NFDIR) {
209                 printk(KERN_ERR "nfs4_get_root:"
210                        " lookupfh encountered non-directory\n");
211                 return -ENOTDIR;
212         }
213
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");
218                 return -EREMOTE;
219         }
220
221         goto next_component;
222
223 path_walk_complete:
224         memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
225         dprintk("<-- nfs4_path_walk() = 0\n");
226         return 0;
227 }
228
229 /*
230  * get an NFS4 root dentry from the root filehandle
231  */
232 struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
233 {
234         struct nfs_server *server = NFS_SB(sb);
235         struct nfs_fattr fattr;
236         struct dentry *mntroot;
237         struct inode *inode;
238         int error;
239
240         dprintk("--> nfs4_get_root()\n");
241
242         /* create a dummy root dentry with dummy inode for this superblock */
243         if (!sb->s_root) {
244                 struct nfs_fh dummyfh;
245                 struct dentry *root;
246                 struct inode *iroot;
247
248                 memset(&dummyfh, 0, sizeof(dummyfh));
249                 memset(&fattr, 0, sizeof(fattr));
250                 nfs_fattr_init(&fattr);
251                 fattr.valid = NFS_ATTR_FATTR;
252                 fattr.type = NFDIR;
253                 fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
254                 fattr.nlink = 2;
255
256                 iroot = nfs_fhget(sb, &dummyfh, &fattr);
257                 if (IS_ERR(iroot))
258                         return ERR_PTR(PTR_ERR(iroot));
259
260                 root = d_alloc_root(iroot);
261                 if (!root) {
262                         iput(iroot);
263                         return ERR_PTR(-ENOMEM);
264                 }
265
266                 sb->s_root = root;
267         }
268
269         /* get the info about the server and filesystem */
270         error = nfs4_server_capabilities(server, mntfh);
271         if (error < 0) {
272                 dprintk("nfs_get_root: getcaps error = %d\n",
273                         -error);
274                 return ERR_PTR(error);
275         }
276
277         /* get the actual root for this mount */
278         error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
279         if (error < 0) {
280                 dprintk("nfs_get_root: getattr error = %d\n", -error);
281                 return ERR_PTR(error);
282         }
283
284         inode = nfs_fhget(sb, mntfh, &fattr);
285         if (IS_ERR(inode)) {
286                 dprintk("nfs_get_root: get root inode failed\n");
287                 return ERR_PTR(PTR_ERR(inode));
288         }
289
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
293          */
294         mntroot = d_alloc_anon(inode);
295         if (!mntroot) {
296                 iput(inode);
297                 dprintk("nfs_get_root: get root dentry failed\n");
298                 return ERR_PTR(-ENOMEM);
299         }
300
301         security_d_instantiate(mntroot, inode);
302
303         if (!mntroot->d_op)
304                 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
305
306         dprintk("<-- nfs4_get_root()\n");
307         return mntroot;
308 }
309
310 #endif /* CONFIG_NFS_V4 */