getroot.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* getroot.c: get the root dentry for an NFS mount
 *
 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/init.h>

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/stats.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_mount.h>
#include <linux/nfs4_mount.h>
#include <linux/lockd/bind.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
#include <linux/nfs_idmap.h>
#include <linux/vfs.h>
#include <linux/namei.h>
33
#include <linux/security.h>
34 35 36 37 38 39 40 41 42 43

#include <asm/system.h>
#include <asm/uaccess.h>

#include "nfs4_fs.h"
#include "delegation.h"
#include "internal.h"

#define NFSDBG_FACILITY		NFSDBG_CLIENT

T
Trond Myklebust 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * Set the superblock root dentry.
 * Note that this function frees the inode in case of error.
 */
static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
{
	/* The mntroot acts as the dummy root dentry for this superblock */
	if (sb->s_root == NULL) {
		sb->s_root = d_alloc_root(inode);
		if (sb->s_root == NULL) {
			iput(inode);
			return -ENOMEM;
		}
A
Al Viro 已提交
57
		ihold(inode);
T
Trond Myklebust 已提交
58 59 60 61 62 63 64 65
		/*
		 * Ensure that this dentry is invisible to d_find_alias().
		 * Otherwise, it may be spliced into the tree by
		 * d_materialise_unique if a parent directory from the same
		 * filesystem gets mounted at a later time.
		 * This again causes shrink_dcache_for_umount_subtree() to
		 * Oops, since the test for IS_ROOT() will fail.
		 */
66
		spin_lock(&sb->s_root->d_inode->i_lock);
N
Nick Piggin 已提交
67
		spin_lock(&sb->s_root->d_lock);
T
Trond Myklebust 已提交
68
		list_del_init(&sb->s_root->d_alias);
N
Nick Piggin 已提交
69
		spin_unlock(&sb->s_root->d_lock);
70
		spin_unlock(&sb->s_root->d_inode->i_lock);
T
Trond Myklebust 已提交
71 72 73 74
	}
	return 0;
}

75 76 77
/*
 * get an NFS2/NFS3 root dentry from the root filehandle
 */
78 79
struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
			    const char *devname)
80 81 82
{
	struct nfs_server *server = NFS_SB(sb);
	struct nfs_fsinfo fsinfo;
83
	struct dentry *ret;
84
	struct inode *inode;
85
	void *name = kstrdup(devname, GFP_KERNEL);
86 87
	int error;

88 89 90
	if (!name)
		return ERR_PTR(-ENOMEM);

91
	/* get the actual root for this mount */
92
	fsinfo.fattr = nfs_alloc_fattr();
93 94
	if (fsinfo.fattr == NULL) {
		kfree(name);
95
		return ERR_PTR(-ENOMEM);
96
	}
97 98 99 100

	error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
	if (error < 0) {
		dprintk("nfs_get_root: getattr error = %d\n", -error);
101 102
		ret = ERR_PTR(error);
		goto out;
103 104 105 106 107
	}

	inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
	if (IS_ERR(inode)) {
		dprintk("nfs_get_root: get root inode failed\n");
108 109
		ret = ERR_CAST(inode);
		goto out;
110 111
	}

T
Trond Myklebust 已提交
112
	error = nfs_superblock_set_dummy_root(sb, inode);
113 114 115 116
	if (error != 0) {
		ret = ERR_PTR(error);
		goto out;
	}
T
Trond Myklebust 已提交
117

118 119 120 121
	/* root dentries normally start off anonymous and get spliced in later
	 * if the dentry tree reaches them; however if the dentry already
	 * exists, we'll pick it up at this point and use it as the root
	 */
122 123
	ret = d_obtain_alias(inode);
	if (IS_ERR(ret)) {
124
		dprintk("nfs_get_root: get root dentry failed\n");
125
		goto out;
126 127
	}

128
	security_d_instantiate(ret, inode);
129 130 131 132 133 134
	spin_lock(&ret->d_lock);
	if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
		ret->d_fsdata = name;
		name = NULL;
	}
	spin_unlock(&ret->d_lock);
135
out:
136 137
	if (name)
		kfree(name);
138 139
	nfs_free_fattr(fsinfo.fattr);
	return ret;
140 141 142 143
}

#ifdef CONFIG_NFS_V4

144
int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
145 146
{
	struct nfs_fsinfo fsinfo;
147
	int ret = -ENOMEM;
148

149
	dprintk("--> nfs4_get_rootfh()\n");
150

151 152 153
	fsinfo.fattr = nfs_alloc_fattr();
	if (fsinfo.fattr == NULL)
		goto out;
154 155 156 157

	/* Start by getting the root filehandle from the server */
	ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
	if (ret < 0) {
158 159
		dprintk("nfs4_get_rootfh: getroot error = %d\n", -ret);
		goto out;
160 161
	}

162
	if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE)
163 164
			|| !S_ISDIR(fsinfo.fattr->mode)) {
		printk(KERN_ERR "nfs4_get_rootfh:"
165
		       " getroot encountered non-directory\n");
166 167
		ret = -ENOTDIR;
		goto out;
168 169
	}

170 171
	if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
		printk(KERN_ERR "nfs4_get_rootfh:"
172
		       " getroot obtained referral\n");
173 174
		ret = -EREMOTE;
		goto out;
175 176
	}

177 178 179 180 181
	memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid));
out:
	nfs_free_fattr(fsinfo.fattr);
	dprintk("<-- nfs4_get_rootfh() = %d\n", ret);
	return ret;
182 183 184 185 186
}

/*
 * get an NFS4 root dentry from the root filehandle
 */
187 188
struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh,
			     const char *devname)
189 190
{
	struct nfs_server *server = NFS_SB(sb);
191 192
	struct nfs_fattr *fattr = NULL;
	struct dentry *ret;
193
	struct inode *inode;
194
	void *name = kstrdup(devname, GFP_KERNEL);
195 196 197 198
	int error;

	dprintk("--> nfs4_get_root()\n");

199 200 201
	if (!name)
		return ERR_PTR(-ENOMEM);

202 203 204 205 206
	/* get the info about the server and filesystem */
	error = nfs4_server_capabilities(server, mntfh);
	if (error < 0) {
		dprintk("nfs_get_root: getcaps error = %d\n",
			-error);
207
		kfree(name);
208 209 210
		return ERR_PTR(error);
	}

211
	fattr = nfs_alloc_fattr();
212 213 214 215
	if (fattr == NULL) {
		kfree(name);
		return ERR_PTR(-ENOMEM);
	}
216

217
	/* get the actual root for this mount */
218
	error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
219 220
	if (error < 0) {
		dprintk("nfs_get_root: getattr error = %d\n", -error);
221 222
		ret = ERR_PTR(error);
		goto out;
223 224
	}

225 226 227 228
	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));

229
	inode = nfs_fhget(sb, mntfh, fattr);
230 231
	if (IS_ERR(inode)) {
		dprintk("nfs_get_root: get root inode failed\n");
232 233
		ret = ERR_CAST(inode);
		goto out;
234 235
	}

T
Trond Myklebust 已提交
236
	error = nfs_superblock_set_dummy_root(sb, inode);
237 238 239 240
	if (error != 0) {
		ret = ERR_PTR(error);
		goto out;
	}
T
Trond Myklebust 已提交
241

242 243 244 245
	/* root dentries normally start off anonymous and get spliced in later
	 * if the dentry tree reaches them; however if the dentry already
	 * exists, we'll pick it up at this point and use it as the root
	 */
246 247
	ret = d_obtain_alias(inode);
	if (IS_ERR(ret)) {
248
		dprintk("nfs_get_root: get root dentry failed\n");
249
		goto out;
250 251
	}

252
	security_d_instantiate(ret, inode);
253 254 255 256 257 258
	spin_lock(&ret->d_lock);
	if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
		ret->d_fsdata = name;
		name = NULL;
	}
	spin_unlock(&ret->d_lock);
259
out:
260 261
	if (name)
		kfree(name);
262
	nfs_free_fattr(fattr);
263
	dprintk("<-- nfs4_get_root()\n");
264
	return ret;
265 266 267
}

#endif /* CONFIG_NFS_V4 */