getroot.c 6.5 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

#include <asm/uaccess.h>

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

#define NFSDBG_FACILITY		NFSDBG_CLIENT

T
Trond Myklebust 已提交
43 44 45 46 47 48 49 50
/*
 * 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) {
51 52
		sb->s_root = d_make_root(inode);
		if (sb->s_root == NULL)
T
Trond Myklebust 已提交
53
			return -ENOMEM;
A
Al Viro 已提交
54
		ihold(inode);
T
Trond Myklebust 已提交
55 56 57 58 59 60 61 62
		/*
		 * 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.
		 */
63
		spin_lock(&sb->s_root->d_inode->i_lock);
N
Nick Piggin 已提交
64
		spin_lock(&sb->s_root->d_lock);
T
Trond Myklebust 已提交
65
		list_del_init(&sb->s_root->d_alias);
N
Nick Piggin 已提交
66
		spin_unlock(&sb->s_root->d_lock);
67
		spin_unlock(&sb->s_root->d_inode->i_lock);
T
Trond Myklebust 已提交
68 69 70 71
	}
	return 0;
}

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

85 86 87
	if (!name)
		return ERR_PTR(-ENOMEM);

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

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

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

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

115 116 117 118
	/* 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
	 */
119 120
	ret = d_obtain_alias(inode);
	if (IS_ERR(ret)) {
121
		dprintk("nfs_get_root: get root dentry failed\n");
122
		goto out;
123 124
	}

125
	security_d_instantiate(ret, inode);
126 127 128 129 130 131
	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);
132
out:
133 134
	if (name)
		kfree(name);
135 136
	nfs_free_fattr(fsinfo.fattr);
	return ret;
137 138 139 140
}

#ifdef CONFIG_NFS_V4

141
int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
142 143
{
	struct nfs_fsinfo fsinfo;
144
	int ret = -ENOMEM;
145

146
	dprintk("--> nfs4_get_rootfh()\n");
147

148 149 150
	fsinfo.fattr = nfs_alloc_fattr();
	if (fsinfo.fattr == NULL)
		goto out;
151 152 153 154

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

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

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

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

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

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

196 197 198
	if (!name)
		return ERR_PTR(-ENOMEM);

199 200 201 202 203
	/* 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);
204
		kfree(name);
205 206 207
		return ERR_PTR(error);
	}

208
	fattr = nfs_alloc_fattr();
209 210 211 212
	if (fattr == NULL) {
		kfree(name);
		return ERR_PTR(-ENOMEM);
	}
213

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

222 223 224 225
	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));

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

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

239 240 241 242
	/* 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
	 */
243 244
	ret = d_obtain_alias(inode);
	if (IS_ERR(ret)) {
245
		dprintk("nfs_get_root: get root dentry failed\n");
246
		goto out;
247 248
	}

249
	security_d_instantiate(ret, inode);
250 251 252 253 254 255
	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);
256
out:
257 258
	if (name)
		kfree(name);
259
	nfs_free_fattr(fattr);
260
	dprintk("<-- nfs4_get_root()\n");
261
	return ret;
262 263 264
}

#endif /* CONFIG_NFS_V4 */