1
2
3
4
5
6
7
8
9
10
11
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>
26#include <linux/lockd/bind.h>
27#include <linux/seq_file.h>
28#include <linux/mount.h>
29#include <linux/vfs.h>
30#include <linux/namei.h>
31#include <linux/security.h>
32
33#include <linux/uaccess.h>
34
35#include "internal.h"
36
37#define NFSDBG_FACILITY NFSDBG_CLIENT
38
39
40
41
42
43static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
44{
45
46 if (sb->s_root == NULL) {
47 sb->s_root = d_make_root(inode);
48 if (sb->s_root == NULL)
49 return -ENOMEM;
50 ihold(inode);
51
52
53
54
55
56
57
58
59 spin_lock(&d_inode(sb->s_root)->i_lock);
60 spin_lock(&sb->s_root->d_lock);
61 hlist_del_init(&sb->s_root->d_u.d_alias);
62 spin_unlock(&sb->s_root->d_lock);
63 spin_unlock(&d_inode(sb->s_root)->i_lock);
64 }
65 return 0;
66}
67
68
69
70
71int nfs_get_root(struct super_block *s, struct fs_context *fc)
72{
73 struct nfs_fs_context *ctx = nfs_fc2context(fc);
74 struct nfs_server *server = NFS_SB(s);
75 struct nfs_fsinfo fsinfo;
76 struct dentry *root;
77 struct inode *inode;
78 char *name;
79 int error = -ENOMEM;
80 unsigned long kflags = 0, kflags_out = 0;
81
82 name = kstrdup(fc->source, GFP_KERNEL);
83 if (!name)
84 goto out;
85
86
87 fsinfo.fattr = nfs_alloc_fattr();
88 if (fsinfo.fattr == NULL)
89 goto out_name;
90
91 fsinfo.fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
92 if (IS_ERR(fsinfo.fattr->label))
93 goto out_fattr;
94 error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
95 if (error < 0) {
96 dprintk("nfs_get_root: getattr error = %d\n", -error);
97 nfs_errorf(fc, "NFS: Couldn't getattr on root");
98 goto out_label;
99 }
100
101 inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL);
102 if (IS_ERR(inode)) {
103 dprintk("nfs_get_root: get root inode failed\n");
104 error = PTR_ERR(inode);
105 nfs_errorf(fc, "NFS: Couldn't get root inode");
106 goto out_label;
107 }
108
109 error = nfs_superblock_set_dummy_root(s, inode);
110 if (error != 0)
111 goto out_label;
112
113
114
115
116
117 root = d_obtain_root(inode);
118 if (IS_ERR(root)) {
119 dprintk("nfs_get_root: get root dentry failed\n");
120 error = PTR_ERR(root);
121 nfs_errorf(fc, "NFS: Couldn't get root dentry");
122 goto out_label;
123 }
124
125 security_d_instantiate(root, inode);
126 spin_lock(&root->d_lock);
127 if (IS_ROOT(root) && !root->d_fsdata &&
128 !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
129 root->d_fsdata = name;
130 name = NULL;
131 }
132 spin_unlock(&root->d_lock);
133 fc->root = root;
134 if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
135 kflags |= SECURITY_LSM_NATIVE_LABELS;
136 if (ctx->clone_data.sb) {
137 if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
138 error = -ESTALE;
139 goto error_splat_root;
140 }
141
142 error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
143 s, kflags, &kflags_out);
144 } else {
145 error = security_sb_set_mnt_opts(s, fc->security,
146 kflags, &kflags_out);
147 }
148 if (error)
149 goto error_splat_root;
150 if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
151 !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
152 NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
153
154 nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label);
155 error = 0;
156
157out_label:
158 nfs4_label_free(fsinfo.fattr->label);
159out_fattr:
160 nfs_free_fattr(fsinfo.fattr);
161out_name:
162 kfree(name);
163out:
164 return error;
165error_splat_root:
166 dput(fc->root);
167 fc->root = NULL;
168 goto out_label;
169}
170