getroot.c 5.8 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 78 79 80 81
/*
 * get an NFS2/NFS3 root dentry from the root filehandle
 */
struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
{
	struct nfs_server *server = NFS_SB(sb);
	struct nfs_fsinfo fsinfo;
82
	struct dentry *ret;
83 84 85 86
	struct inode *inode;
	int error;

	/* get the actual root for this mount */
87 88 89
	fsinfo.fattr = nfs_alloc_fattr();
	if (fsinfo.fattr == NULL)
		return ERR_PTR(-ENOMEM);
90 91 92 93

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

	inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
	if (IS_ERR(inode)) {
		dprintk("nfs_get_root: get root inode failed\n");
101 102
		ret = ERR_CAST(inode);
		goto out;
103 104
	}

T
Trond Myklebust 已提交
105
	error = nfs_superblock_set_dummy_root(sb, inode);
106 107 108 109
	if (error != 0) {
		ret = ERR_PTR(error);
		goto out;
	}
T
Trond Myklebust 已提交
110

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

121 122 123 124
	security_d_instantiate(ret, inode);
out:
	nfs_free_fattr(fsinfo.fattr);
	return ret;
125 126 127 128
}

#ifdef CONFIG_NFS_V4

129
int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
130 131
{
	struct nfs_fsinfo fsinfo;
132
	int ret = -ENOMEM;
133

134
	dprintk("--> nfs4_get_rootfh()\n");
135

136 137 138
	fsinfo.fattr = nfs_alloc_fattr();
	if (fsinfo.fattr == NULL)
		goto out;
139 140 141 142

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

147
	if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE)
148 149
			|| !S_ISDIR(fsinfo.fattr->mode)) {
		printk(KERN_ERR "nfs4_get_rootfh:"
150
		       " getroot encountered non-directory\n");
151 152
		ret = -ENOTDIR;
		goto out;
153 154
	}

155 156
	if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
		printk(KERN_ERR "nfs4_get_rootfh:"
157
		       " getroot obtained referral\n");
158 159
		ret = -EREMOTE;
		goto out;
160 161
	}

162 163 164 165 166
	memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid));
out:
	nfs_free_fattr(fsinfo.fattr);
	dprintk("<-- nfs4_get_rootfh() = %d\n", ret);
	return ret;
167 168 169 170 171 172 173 174
}

/*
 * get an NFS4 root dentry from the root filehandle
 */
struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
{
	struct nfs_server *server = NFS_SB(sb);
175 176
	struct nfs_fattr *fattr = NULL;
	struct dentry *ret;
177 178 179 180 181 182 183 184 185 186 187 188 189
	struct inode *inode;
	int error;

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

	/* 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);
		return ERR_PTR(error);
	}

190 191 192 193
	fattr = nfs_alloc_fattr();
	if (fattr == NULL)
		return ERR_PTR(-ENOMEM);;

194
	/* get the actual root for this mount */
195
	error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
196 197
	if (error < 0) {
		dprintk("nfs_get_root: getattr error = %d\n", -error);
198 199
		ret = ERR_PTR(error);
		goto out;
200 201
	}

202
	inode = nfs_fhget(sb, mntfh, fattr);
203 204
	if (IS_ERR(inode)) {
		dprintk("nfs_get_root: get root inode failed\n");
205 206
		ret = ERR_CAST(inode);
		goto out;
207 208
	}

T
Trond Myklebust 已提交
209
	error = nfs_superblock_set_dummy_root(sb, inode);
210 211 212 213
	if (error != 0) {
		ret = ERR_PTR(error);
		goto out;
	}
T
Trond Myklebust 已提交
214

215 216 217 218
	/* 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
	 */
219 220
	ret = d_obtain_alias(inode);
	if (IS_ERR(ret)) {
221
		dprintk("nfs_get_root: get root dentry failed\n");
222
		goto out;
223 224
	}

225
	security_d_instantiate(ret, inode);
226

227 228
out:
	nfs_free_fattr(fattr);
229
	dprintk("<-- nfs4_get_root()\n");
230
	return ret;
231 232 233
}

#endif /* CONFIG_NFS_V4 */