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