blob: 4ca6f5c8038e02dfddb3a8a33dec71a8cb20295f [file] [log] [blame]
David Howells54ceac42006-08-22 20:06:13 -04001/* 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
David Howells54ceac42006-08-22 20:06:13 -040012#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>
David Howells54ceac42006-08-22 20:06:13 -040028#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>
David Howells738a3512006-07-30 14:58:27 -040033#include <linux/security.h>
David Howells54ceac42006-08-22 20:06:13 -040034
David Howells54ceac42006-08-22 20:06:13 -040035#include <asm/uaccess.h>
36
37#include "nfs4_fs.h"
38#include "delegation.h"
39#include "internal.h"
40
41#define NFSDBG_FACILITY NFSDBG_CLIENT
David Howells54ceac42006-08-22 20:06:13 -040042
43/*
Trond Myklebustb09b9412007-10-25 13:56:10 -040044 * Set the superblock root dentry.
45 * Note that this function frees the inode in case of error.
46 */
47static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
48{
49 /* The mntroot acts as the dummy root dentry for this superblock */
50 if (sb->s_root == NULL) {
Al Viro48fde702012-01-08 22:15:13 -050051 sb->s_root = d_make_root(inode);
52 if (sb->s_root == NULL)
Trond Myklebustb09b9412007-10-25 13:56:10 -040053 return -ENOMEM;
Al Viro7de9c6ee2010-10-23 11:11:40 -040054 ihold(inode);
Trond Myklebusta10db502007-12-12 11:12:15 -050055 /*
56 * Ensure that this dentry is invisible to d_find_alias().
57 * Otherwise, it may be spliced into the tree by
58 * d_materialise_unique if a parent directory from the same
59 * filesystem gets mounted at a later time.
60 * This again causes shrink_dcache_for_umount_subtree() to
61 * Oops, since the test for IS_ROOT() will fail.
62 */
Nick Piggin873feea2011-01-07 17:50:06 +110063 spin_lock(&sb->s_root->d_inode->i_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110064 spin_lock(&sb->s_root->d_lock);
Trond Myklebusta10db502007-12-12 11:12:15 -050065 list_del_init(&sb->s_root->d_alias);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110066 spin_unlock(&sb->s_root->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +110067 spin_unlock(&sb->s_root->d_inode->i_lock);
Trond Myklebustb09b9412007-10-25 13:56:10 -040068 }
69 return 0;
70}
71
72/*
David Howells54ceac42006-08-22 20:06:13 -040073 * get an NFS2/NFS3 root dentry from the root filehandle
74 */
Al Viro0d5839a2011-03-16 05:27:27 -040075struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
76 const char *devname)
David Howells54ceac42006-08-22 20:06:13 -040077{
78 struct nfs_server *server = NFS_SB(sb);
79 struct nfs_fsinfo fsinfo;
Trond Myklebust8bac9db2010-04-16 16:22:48 -040080 struct dentry *ret;
David Howells54ceac42006-08-22 20:06:13 -040081 struct inode *inode;
Al Virob1942c52011-03-16 05:44:14 -040082 void *name = kstrdup(devname, GFP_KERNEL);
David Howells54ceac42006-08-22 20:06:13 -040083 int error;
84
Al Virob1942c52011-03-16 05:44:14 -040085 if (!name)
86 return ERR_PTR(-ENOMEM);
87
David Howells54ceac42006-08-22 20:06:13 -040088 /* get the actual root for this mount */
Trond Myklebust8bac9db2010-04-16 16:22:48 -040089 fsinfo.fattr = nfs_alloc_fattr();
Al Virob1942c52011-03-16 05:44:14 -040090 if (fsinfo.fattr == NULL) {
91 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040092 return ERR_PTR(-ENOMEM);
Al Virob1942c52011-03-16 05:44:14 -040093 }
David Howells54ceac42006-08-22 20:06:13 -040094
95 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
96 if (error < 0) {
97 dprintk("nfs_get_root: getattr error = %d\n", -error);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040098 ret = ERR_PTR(error);
99 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400100 }
101
102 inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
103 if (IS_ERR(inode)) {
104 dprintk("nfs_get_root: get root inode failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400105 ret = ERR_CAST(inode);
106 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400107 }
108
Trond Myklebustb09b9412007-10-25 13:56:10 -0400109 error = nfs_superblock_set_dummy_root(sb, inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400110 if (error != 0) {
111 ret = ERR_PTR(error);
112 goto out;
113 }
Trond Myklebustb09b9412007-10-25 13:56:10 -0400114
David Howells54ceac42006-08-22 20:06:13 -0400115 /* root dentries normally start off anonymous and get spliced in later
116 * if the dentry tree reaches them; however if the dentry already
117 * exists, we'll pick it up at this point and use it as the root
118 */
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400119 ret = d_obtain_alias(inode);
120 if (IS_ERR(ret)) {
David Howells54ceac42006-08-22 20:06:13 -0400121 dprintk("nfs_get_root: get root dentry failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400122 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400123 }
124
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400125 security_d_instantiate(ret, inode);
Al Virob1942c52011-03-16 05:44:14 -0400126 spin_lock(&ret->d_lock);
127 if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
128 ret->d_fsdata = name;
129 name = NULL;
130 }
131 spin_unlock(&ret->d_lock);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400132out:
Al Virob1942c52011-03-16 05:44:14 -0400133 if (name)
134 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400135 nfs_free_fattr(fsinfo.fattr);
136 return ret;
David Howells54ceac42006-08-22 20:06:13 -0400137}
138
139#ifdef CONFIG_NFS_V4
140
Trond Myklebust815409d2010-04-16 16:22:46 -0400141int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -0400142{
143 struct nfs_fsinfo fsinfo;
Trond Myklebust815409d2010-04-16 16:22:46 -0400144 int ret = -ENOMEM;
David Howells54ceac42006-08-22 20:06:13 -0400145
Trond Myklebust815409d2010-04-16 16:22:46 -0400146 dprintk("--> nfs4_get_rootfh()\n");
David Howells54ceac42006-08-22 20:06:13 -0400147
Trond Myklebust815409d2010-04-16 16:22:46 -0400148 fsinfo.fattr = nfs_alloc_fattr();
149 if (fsinfo.fattr == NULL)
150 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400151
152 /* Start by getting the root filehandle from the server */
153 ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
154 if (ret < 0) {
Trond Myklebust815409d2010-04-16 16:22:46 -0400155 dprintk("nfs4_get_rootfh: getroot error = %d\n", -ret);
156 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400157 }
158
Andy Adamsonf799bdb2010-06-16 09:51:02 -0400159 if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE)
Trond Myklebust815409d2010-04-16 16:22:46 -0400160 || !S_ISDIR(fsinfo.fattr->mode)) {
161 printk(KERN_ERR "nfs4_get_rootfh:"
David Howells54ceac42006-08-22 20:06:13 -0400162 " getroot encountered non-directory\n");
Trond Myklebust815409d2010-04-16 16:22:46 -0400163 ret = -ENOTDIR;
164 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400165 }
166
Trond Myklebust815409d2010-04-16 16:22:46 -0400167 if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
168 printk(KERN_ERR "nfs4_get_rootfh:"
David Howells54ceac42006-08-22 20:06:13 -0400169 " getroot obtained referral\n");
Trond Myklebust815409d2010-04-16 16:22:46 -0400170 ret = -EREMOTE;
171 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400172 }
173
Trond Myklebust815409d2010-04-16 16:22:46 -0400174 memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid));
175out:
176 nfs_free_fattr(fsinfo.fattr);
177 dprintk("<-- nfs4_get_rootfh() = %d\n", ret);
178 return ret;
David Howells54ceac42006-08-22 20:06:13 -0400179}
180
181/*
182 * get an NFS4 root dentry from the root filehandle
183 */
Al Viro0d5839a2011-03-16 05:27:27 -0400184struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh,
185 const char *devname)
David Howells54ceac42006-08-22 20:06:13 -0400186{
187 struct nfs_server *server = NFS_SB(sb);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400188 struct nfs_fattr *fattr = NULL;
189 struct dentry *ret;
David Howells54ceac42006-08-22 20:06:13 -0400190 struct inode *inode;
Al Virob1942c52011-03-16 05:44:14 -0400191 void *name = kstrdup(devname, GFP_KERNEL);
David Howells54ceac42006-08-22 20:06:13 -0400192 int error;
193
194 dprintk("--> nfs4_get_root()\n");
195
Al Virob1942c52011-03-16 05:44:14 -0400196 if (!name)
197 return ERR_PTR(-ENOMEM);
198
David Howells54ceac42006-08-22 20:06:13 -0400199 /* get the info about the server and filesystem */
200 error = nfs4_server_capabilities(server, mntfh);
201 if (error < 0) {
202 dprintk("nfs_get_root: getcaps error = %d\n",
203 -error);
Al Virob1942c52011-03-16 05:44:14 -0400204 kfree(name);
David Howells54ceac42006-08-22 20:06:13 -0400205 return ERR_PTR(error);
206 }
207
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400208 fattr = nfs_alloc_fattr();
Al Virob1942c52011-03-16 05:44:14 -0400209 if (fattr == NULL) {
210 kfree(name);
211 return ERR_PTR(-ENOMEM);
212 }
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400213
David Howells54ceac42006-08-22 20:06:13 -0400214 /* get the actual root for this mount */
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400215 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -0400216 if (error < 0) {
217 dprintk("nfs_get_root: getattr error = %d\n", -error);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400218 ret = ERR_PTR(error);
219 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400220 }
221
Gusev Vitaliy46670582011-03-23 00:40:25 +0300222 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
223 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
224 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
225
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400226 inode = nfs_fhget(sb, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -0400227 if (IS_ERR(inode)) {
228 dprintk("nfs_get_root: get root inode failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400229 ret = ERR_CAST(inode);
230 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400231 }
232
Trond Myklebustb09b9412007-10-25 13:56:10 -0400233 error = nfs_superblock_set_dummy_root(sb, inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400234 if (error != 0) {
235 ret = ERR_PTR(error);
236 goto out;
237 }
Trond Myklebustb09b9412007-10-25 13:56:10 -0400238
David Howells54ceac42006-08-22 20:06:13 -0400239 /* root dentries normally start off anonymous and get spliced in later
240 * if the dentry tree reaches them; however if the dentry already
241 * exists, we'll pick it up at this point and use it as the root
242 */
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400243 ret = d_obtain_alias(inode);
244 if (IS_ERR(ret)) {
David Howells54ceac42006-08-22 20:06:13 -0400245 dprintk("nfs_get_root: get root dentry failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400246 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400247 }
248
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400249 security_d_instantiate(ret, inode);
Al Virob1942c52011-03-16 05:44:14 -0400250 spin_lock(&ret->d_lock);
251 if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
252 ret->d_fsdata = name;
253 name = NULL;
254 }
255 spin_unlock(&ret->d_lock);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400256out:
Al Virob1942c52011-03-16 05:44:14 -0400257 if (name)
258 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400259 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -0400260 dprintk("<-- nfs4_get_root()\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400261 return ret;
David Howells54ceac42006-08-22 20:06:13 -0400262}
263
264#endif /* CONFIG_NFS_V4 */