Merge tag 'configfs-for-4.7' of git://git.infradead.org/users/hch/configfs
[linux-2.6-block.git] / fs / nfs / getroot.c
CommitLineData
54ceac45
DH
1/* 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
54ceac45
DH
12#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>
54ceac45 26#include <linux/lockd/bind.h>
54ceac45
DH
27#include <linux/seq_file.h>
28#include <linux/mount.h>
54ceac45
DH
29#include <linux/vfs.h>
30#include <linux/namei.h>
738a3519 31#include <linux/security.h>
54ceac45 32
54ceac45
DH
33#include <asm/uaccess.h>
34
4e437e95
SK
35#include "internal.h"
36
54ceac45 37#define NFSDBG_FACILITY NFSDBG_CLIENT
54ceac45 38
b09b9417
TM
39/*
40 * Set the superblock root dentry.
41 * Note that this function frees the inode in case of error.
42 */
43static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
44{
45 /* The mntroot acts as the dummy root dentry for this superblock */
46 if (sb->s_root == NULL) {
48fde701
AV
47 sb->s_root = d_make_root(inode);
48 if (sb->s_root == NULL)
b09b9417 49 return -ENOMEM;
7de9c6ee 50 ihold(inode);
a10db50a
TM
51 /*
52 * Ensure that this dentry is invisible to d_find_alias().
53 * Otherwise, it may be spliced into the tree by
41d28bca 54 * d_splice_alias if a parent directory from the same
a10db50a
TM
55 * filesystem gets mounted at a later time.
56 * This again causes shrink_dcache_for_umount_subtree() to
57 * Oops, since the test for IS_ROOT() will fail.
58 */
2b0143b5 59 spin_lock(&d_inode(sb->s_root)->i_lock);
b23fb0a6 60 spin_lock(&sb->s_root->d_lock);
946e51f2 61 hlist_del_init(&sb->s_root->d_u.d_alias);
b23fb0a6 62 spin_unlock(&sb->s_root->d_lock);
2b0143b5 63 spin_unlock(&d_inode(sb->s_root)->i_lock);
b09b9417
TM
64 }
65 return 0;
66}
67
54ceac45
DH
68/*
69 * get an NFS2/NFS3 root dentry from the root filehandle
70 */
0d5839ad
AV
71struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
72 const char *devname)
54ceac45
DH
73{
74 struct nfs_server *server = NFS_SB(sb);
75 struct nfs_fsinfo fsinfo;
8bac9db9 76 struct dentry *ret;
54ceac45 77 struct inode *inode;
b1942c5f 78 void *name = kstrdup(devname, GFP_KERNEL);
54ceac45
DH
79 int error;
80
b1942c5f
AV
81 if (!name)
82 return ERR_PTR(-ENOMEM);
83
54ceac45 84 /* get the actual root for this mount */
8bac9db9 85 fsinfo.fattr = nfs_alloc_fattr();
b1942c5f
AV
86 if (fsinfo.fattr == NULL) {
87 kfree(name);
8bac9db9 88 return ERR_PTR(-ENOMEM);
b1942c5f 89 }
54ceac45
DH
90
91 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
92 if (error < 0) {
93 dprintk("nfs_get_root: getattr error = %d\n", -error);
8bac9db9
TM
94 ret = ERR_PTR(error);
95 goto out;
54ceac45
DH
96 }
97
1775fd3e 98 inode = nfs_fhget(sb, mntfh, fsinfo.fattr, NULL);
54ceac45
DH
99 if (IS_ERR(inode)) {
100 dprintk("nfs_get_root: get root inode failed\n");
8bac9db9
TM
101 ret = ERR_CAST(inode);
102 goto out;
54ceac45
DH
103 }
104
b09b9417 105 error = nfs_superblock_set_dummy_root(sb, inode);
8bac9db9
TM
106 if (error != 0) {
107 ret = ERR_PTR(error);
108 goto out;
109 }
b09b9417 110
54ceac45
DH
111 /* root dentries normally start off anonymous and get spliced in later
112 * if the dentry tree reaches them; however if the dentry already
113 * exists, we'll pick it up at this point and use it as the root
114 */
1a0a397e 115 ret = d_obtain_root(inode);
8bac9db9 116 if (IS_ERR(ret)) {
54ceac45 117 dprintk("nfs_get_root: get root dentry failed\n");
8bac9db9 118 goto out;
54ceac45
DH
119 }
120
8bac9db9 121 security_d_instantiate(ret, inode);
b1942c5f 122 spin_lock(&ret->d_lock);
4dfc7fdb
KM
123 if (IS_ROOT(ret) && !ret->d_fsdata &&
124 !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
b1942c5f
AV
125 ret->d_fsdata = name;
126 name = NULL;
127 }
128 spin_unlock(&ret->d_lock);
8bac9db9 129out:
96aa1549 130 kfree(name);
8bac9db9
TM
131 nfs_free_fattr(fsinfo.fattr);
132 return ret;
54ceac45 133}