1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* getroot.c: get the root dentry for an NFS mount
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/time.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/mm.h>
14*4882a593Smuzhiyun #include <linux/string.h>
15*4882a593Smuzhiyun #include <linux/stat.h>
16*4882a593Smuzhiyun #include <linux/errno.h>
17*4882a593Smuzhiyun #include <linux/unistd.h>
18*4882a593Smuzhiyun #include <linux/sunrpc/clnt.h>
19*4882a593Smuzhiyun #include <linux/sunrpc/stats.h>
20*4882a593Smuzhiyun #include <linux/nfs_fs.h>
21*4882a593Smuzhiyun #include <linux/nfs_mount.h>
22*4882a593Smuzhiyun #include <linux/lockd/bind.h>
23*4882a593Smuzhiyun #include <linux/seq_file.h>
24*4882a593Smuzhiyun #include <linux/mount.h>
25*4882a593Smuzhiyun #include <linux/vfs.h>
26*4882a593Smuzhiyun #include <linux/namei.h>
27*4882a593Smuzhiyun #include <linux/security.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/uaccess.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include "internal.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define NFSDBG_FACILITY NFSDBG_CLIENT
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * Set the superblock root dentry.
37*4882a593Smuzhiyun * Note that this function frees the inode in case of error.
38*4882a593Smuzhiyun */
nfs_superblock_set_dummy_root(struct super_block * sb,struct inode * inode)39*4882a593Smuzhiyun static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun /* The mntroot acts as the dummy root dentry for this superblock */
42*4882a593Smuzhiyun if (sb->s_root == NULL) {
43*4882a593Smuzhiyun sb->s_root = d_make_root(inode);
44*4882a593Smuzhiyun if (sb->s_root == NULL)
45*4882a593Smuzhiyun return -ENOMEM;
46*4882a593Smuzhiyun ihold(inode);
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Ensure that this dentry is invisible to d_find_alias().
49*4882a593Smuzhiyun * Otherwise, it may be spliced into the tree by
50*4882a593Smuzhiyun * d_splice_alias if a parent directory from the same
51*4882a593Smuzhiyun * filesystem gets mounted at a later time.
52*4882a593Smuzhiyun * This again causes shrink_dcache_for_umount_subtree() to
53*4882a593Smuzhiyun * Oops, since the test for IS_ROOT() will fail.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun spin_lock(&d_inode(sb->s_root)->i_lock);
56*4882a593Smuzhiyun spin_lock(&sb->s_root->d_lock);
57*4882a593Smuzhiyun hlist_del_init(&sb->s_root->d_u.d_alias);
58*4882a593Smuzhiyun spin_unlock(&sb->s_root->d_lock);
59*4882a593Smuzhiyun spin_unlock(&d_inode(sb->s_root)->i_lock);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * get an NFS2/NFS3 root dentry from the root filehandle
66*4882a593Smuzhiyun */
nfs_get_root(struct super_block * s,struct fs_context * fc)67*4882a593Smuzhiyun int nfs_get_root(struct super_block *s, struct fs_context *fc)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun struct nfs_fs_context *ctx = nfs_fc2context(fc);
70*4882a593Smuzhiyun struct nfs_server *server = NFS_SB(s);
71*4882a593Smuzhiyun struct nfs_fsinfo fsinfo;
72*4882a593Smuzhiyun struct dentry *root;
73*4882a593Smuzhiyun struct inode *inode;
74*4882a593Smuzhiyun char *name;
75*4882a593Smuzhiyun int error = -ENOMEM;
76*4882a593Smuzhiyun unsigned long kflags = 0, kflags_out = 0;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun name = kstrdup(fc->source, GFP_KERNEL);
79*4882a593Smuzhiyun if (!name)
80*4882a593Smuzhiyun goto out;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* get the actual root for this mount */
83*4882a593Smuzhiyun fsinfo.fattr = nfs_alloc_fattr();
84*4882a593Smuzhiyun if (fsinfo.fattr == NULL)
85*4882a593Smuzhiyun goto out_name;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun fsinfo.fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
88*4882a593Smuzhiyun if (IS_ERR(fsinfo.fattr->label))
89*4882a593Smuzhiyun goto out_fattr;
90*4882a593Smuzhiyun error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
91*4882a593Smuzhiyun if (error < 0) {
92*4882a593Smuzhiyun dprintk("nfs_get_root: getattr error = %d\n", -error);
93*4882a593Smuzhiyun nfs_errorf(fc, "NFS: Couldn't getattr on root");
94*4882a593Smuzhiyun goto out_label;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL);
98*4882a593Smuzhiyun if (IS_ERR(inode)) {
99*4882a593Smuzhiyun dprintk("nfs_get_root: get root inode failed\n");
100*4882a593Smuzhiyun error = PTR_ERR(inode);
101*4882a593Smuzhiyun nfs_errorf(fc, "NFS: Couldn't get root inode");
102*4882a593Smuzhiyun goto out_label;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun error = nfs_superblock_set_dummy_root(s, inode);
106*4882a593Smuzhiyun if (error != 0)
107*4882a593Smuzhiyun goto out_label;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* root dentries normally start off anonymous and get spliced in later
110*4882a593Smuzhiyun * if the dentry tree reaches them; however if the dentry already
111*4882a593Smuzhiyun * exists, we'll pick it up at this point and use it as the root
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun root = d_obtain_root(inode);
114*4882a593Smuzhiyun if (IS_ERR(root)) {
115*4882a593Smuzhiyun dprintk("nfs_get_root: get root dentry failed\n");
116*4882a593Smuzhiyun error = PTR_ERR(root);
117*4882a593Smuzhiyun nfs_errorf(fc, "NFS: Couldn't get root dentry");
118*4882a593Smuzhiyun goto out_label;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun security_d_instantiate(root, inode);
122*4882a593Smuzhiyun spin_lock(&root->d_lock);
123*4882a593Smuzhiyun if (IS_ROOT(root) && !root->d_fsdata &&
124*4882a593Smuzhiyun !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
125*4882a593Smuzhiyun root->d_fsdata = name;
126*4882a593Smuzhiyun name = NULL;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun spin_unlock(&root->d_lock);
129*4882a593Smuzhiyun fc->root = root;
130*4882a593Smuzhiyun if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
131*4882a593Smuzhiyun kflags |= SECURITY_LSM_NATIVE_LABELS;
132*4882a593Smuzhiyun if (ctx->clone_data.sb) {
133*4882a593Smuzhiyun if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
134*4882a593Smuzhiyun error = -ESTALE;
135*4882a593Smuzhiyun goto error_splat_root;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun /* clone lsm security options from the parent to the new sb */
138*4882a593Smuzhiyun error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
139*4882a593Smuzhiyun s, kflags, &kflags_out);
140*4882a593Smuzhiyun } else {
141*4882a593Smuzhiyun error = security_sb_set_mnt_opts(s, fc->security,
142*4882a593Smuzhiyun kflags, &kflags_out);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun if (error)
145*4882a593Smuzhiyun goto error_splat_root;
146*4882a593Smuzhiyun if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
147*4882a593Smuzhiyun !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
148*4882a593Smuzhiyun NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label);
151*4882a593Smuzhiyun error = 0;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun out_label:
154*4882a593Smuzhiyun nfs4_label_free(fsinfo.fattr->label);
155*4882a593Smuzhiyun out_fattr:
156*4882a593Smuzhiyun nfs_free_fattr(fsinfo.fattr);
157*4882a593Smuzhiyun out_name:
158*4882a593Smuzhiyun kfree(name);
159*4882a593Smuzhiyun out:
160*4882a593Smuzhiyun return error;
161*4882a593Smuzhiyun error_splat_root:
162*4882a593Smuzhiyun dput(fc->root);
163*4882a593Smuzhiyun fc->root = NULL;
164*4882a593Smuzhiyun goto out_label;
165*4882a593Smuzhiyun }
166