getroot.c 5.9 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 57 58
/*
 * 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;
		}
		/* Circumvent igrab(): we know the inode is not being freed */
		atomic_inc(&inode->i_count);
T
Trond Myklebust 已提交
59 60 61 62 63 64 65 66 67 68 69
		/*
		 * 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.
		 */
		spin_lock(&dcache_lock);
		list_del_init(&sb->s_root->d_alias);
		spin_unlock(&dcache_lock);
T
Trond Myklebust 已提交
70 71 72 73
	}
	return 0;
}

74 75 76 77 78 79 80
/*
 * 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;
81
	struct dentry *ret;
82 83 84 85
	struct inode *inode;
	int error;

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

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

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

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

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

120
	security_d_instantiate(ret, inode);
121

122 123 124 125 126
	if (ret->d_op == NULL)
		ret->d_op = server->nfs_client->rpc_ops->dentry_ops;
out:
	nfs_free_fattr(fsinfo.fattr);
	return ret;
127 128 129 130
}

#ifdef CONFIG_NFS_V4

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

136
	dprintk("--> nfs4_get_rootfh()\n");
137

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

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

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

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

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

/*
 * 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);
177 178
	struct nfs_fattr *fattr = NULL;
	struct dentry *ret;
179 180 181 182 183 184 185 186 187 188 189 190 191
	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);
	}

192 193 194 195
	fattr = nfs_alloc_fattr();
	if (fattr == NULL)
		return ERR_PTR(-ENOMEM);;

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

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

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

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

227
	security_d_instantiate(ret, inode);
228

229 230
	if (ret->d_op == NULL)
		ret->d_op = server->nfs_client->rpc_ops->dentry_ops;
231

232 233
out:
	nfs_free_fattr(fattr);
234
	dprintk("<-- nfs4_get_root()\n");
235
	return ret;
236 237 238
}

#endif /* CONFIG_NFS_V4 */