Revert "NFS: Ensure we return zero if applications attempt to write zero bytes"
[deliverable/linux.git] / fs / nfs / getroot.c
CommitLineData
54ceac45
DH
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
54ceac45
DH
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>
54ceac45
DH
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>
6b3286ed 33#include <linux/mnt_namespace.h>
738a3519 34#include <linux/security.h>
54ceac45
DH
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
54ceac45 44
b09b9417
TM
45/*
46 * 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);
60 }
61 return 0;
62}
63
54ceac45
DH
64/*
65 * get an NFS2/NFS3 root dentry from the root filehandle
66 */
67struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
68{
69 struct nfs_server *server = NFS_SB(sb);
70 struct nfs_fsinfo fsinfo;
71 struct nfs_fattr fattr;
72 struct dentry *mntroot;
73 struct inode *inode;
74 int error;
75
54ceac45
DH
76 /* get the actual root for this mount */
77 fsinfo.fattr = &fattr;
78
79 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
80 if (error < 0) {
81 dprintk("nfs_get_root: getattr error = %d\n", -error);
82 return ERR_PTR(error);
83 }
84
85 inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
86 if (IS_ERR(inode)) {
87 dprintk("nfs_get_root: get root inode failed\n");
88 return ERR_PTR(PTR_ERR(inode));
89 }
90
b09b9417
TM
91 error = nfs_superblock_set_dummy_root(sb, inode);
92 if (error != 0)
93 return ERR_PTR(error);
94
54ceac45
DH
95 /* root dentries normally start off anonymous and get spliced in later
96 * if the dentry tree reaches them; however if the dentry already
97 * exists, we'll pick it up at this point and use it as the root
98 */
99 mntroot = d_alloc_anon(inode);
100 if (!mntroot) {
101 iput(inode);
102 dprintk("nfs_get_root: get root dentry failed\n");
103 return ERR_PTR(-ENOMEM);
104 }
105
738a3519
DH
106 security_d_instantiate(mntroot, inode);
107
54ceac45
DH
108 if (!mntroot->d_op)
109 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
110
111 return mntroot;
112}
113
114#ifdef CONFIG_NFS_V4
115
116/*
117 * Do a simple pathwalk from the root FH of the server to the nominated target
118 * of the mountpoint
119 * - give error on symlinks
120 * - give error on ".." occurring in the path
121 * - follow traversals
122 */
123int nfs4_path_walk(struct nfs_server *server,
124 struct nfs_fh *mntfh,
125 const char *path)
126{
127 struct nfs_fsinfo fsinfo;
128 struct nfs_fattr fattr;
129 struct nfs_fh lastfh;
130 struct qstr name;
131 int ret;
54ceac45
DH
132
133 dprintk("--> nfs4_path_walk(,,%s)\n", path);
134
135 fsinfo.fattr = &fattr;
136 nfs_fattr_init(&fattr);
137
faebf4e2
TM
138 /* Eat leading slashes */
139 while (*path == '/')
140 path++;
54ceac45
DH
141
142 /* Start by getting the root filehandle from the server */
143 ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
144 if (ret < 0) {
145 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
146 return ret;
147 }
148
149 if (fattr.type != NFDIR) {
150 printk(KERN_ERR "nfs4_get_root:"
151 " getroot encountered non-directory\n");
152 return -ENOTDIR;
153 }
154
faebf4e2 155 /* FIXME: It is quite valid for the server to return a referral here */
54ceac45
DH
156 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
157 printk(KERN_ERR "nfs4_get_root:"
158 " getroot obtained referral\n");
159 return -EREMOTE;
160 }
161
162next_component:
163 dprintk("Next: %s\n", path);
164
165 /* extract the next bit of the path */
166 if (!*path)
167 goto path_walk_complete;
168
169 name.name = path;
170 while (*path && *path != '/')
171 path++;
172 name.len = path - (const char *) name.name;
173
54af3bb5
TM
174 if (name.len > NFS4_MAXNAMLEN)
175 return -ENAMETOOLONG;
176
54ceac45
DH
177eat_dot_dir:
178 while (*path == '/')
179 path++;
180
181 if (path[0] == '.' && (path[1] == '/' || !path[1])) {
182 path += 2;
183 goto eat_dot_dir;
184 }
185
faebf4e2 186 /* FIXME: Why shouldn't the user be able to use ".." in the path? */
54ceac45
DH
187 if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
188 ) {
189 printk(KERN_ERR "nfs4_get_root:"
190 " Mount path contains reference to \"..\"\n");
191 return -EINVAL;
192 }
193
194 /* lookup the next FH in the sequence */
195 memcpy(&lastfh, mntfh, sizeof(lastfh));
196
197 dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
198
199 ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
200 mntfh, &fattr);
201 if (ret < 0) {
202 dprintk("nfs4_get_root: getroot error = %d\n", -ret);
203 return ret;
204 }
205
206 if (fattr.type != NFDIR) {
207 printk(KERN_ERR "nfs4_get_root:"
208 " lookupfh encountered non-directory\n");
209 return -ENOTDIR;
210 }
211
faebf4e2 212 /* FIXME: Referrals are quite valid here too */
54ceac45
DH
213 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
214 printk(KERN_ERR "nfs4_get_root:"
215 " lookupfh obtained referral\n");
216 return -EREMOTE;
217 }
218
219 goto next_component;
220
221path_walk_complete:
222 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
223 dprintk("<-- nfs4_path_walk() = 0\n");
224 return 0;
225}
226
227/*
228 * get an NFS4 root dentry from the root filehandle
229 */
230struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
231{
232 struct nfs_server *server = NFS_SB(sb);
233 struct nfs_fattr fattr;
234 struct dentry *mntroot;
235 struct inode *inode;
236 int error;
237
238 dprintk("--> nfs4_get_root()\n");
239
54ceac45
DH
240 /* get the info about the server and filesystem */
241 error = nfs4_server_capabilities(server, mntfh);
242 if (error < 0) {
243 dprintk("nfs_get_root: getcaps error = %d\n",
244 -error);
245 return ERR_PTR(error);
246 }
247
248 /* get the actual root for this mount */
249 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
250 if (error < 0) {
251 dprintk("nfs_get_root: getattr error = %d\n", -error);
252 return ERR_PTR(error);
253 }
254
255 inode = nfs_fhget(sb, mntfh, &fattr);
256 if (IS_ERR(inode)) {
257 dprintk("nfs_get_root: get root inode failed\n");
258 return ERR_PTR(PTR_ERR(inode));
259 }
260
b09b9417
TM
261 error = nfs_superblock_set_dummy_root(sb, inode);
262 if (error != 0)
263 return ERR_PTR(error);
264
54ceac45
DH
265 /* root dentries normally start off anonymous and get spliced in later
266 * if the dentry tree reaches them; however if the dentry already
267 * exists, we'll pick it up at this point and use it as the root
268 */
269 mntroot = d_alloc_anon(inode);
270 if (!mntroot) {
271 iput(inode);
272 dprintk("nfs_get_root: get root dentry failed\n");
273 return ERR_PTR(-ENOMEM);
274 }
275
738a3519
DH
276 security_d_instantiate(mntroot, inode);
277
54ceac45
DH
278 if (!mntroot->d_op)
279 mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
280
281 dprintk("<-- nfs4_get_root()\n");
282 return mntroot;
283}
284
285#endif /* CONFIG_NFS_V4 */
This page took 0.15761 seconds and 5 git commands to generate.