blob: 878c4c5982d943622487d04db3b82d3814afe27d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells54ceac42006-08-22 20:06:13 -04002/* 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)
David Howells54ceac42006-08-22 20:06:13 -04006 */
7
David Howells54ceac42006-08-22 20:06:13 -04008#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>
David Howells54ceac42006-08-22 20:06:13 -040022#include <linux/lockd/bind.h>
David Howells54ceac42006-08-22 20:06:13 -040023#include <linux/seq_file.h>
24#include <linux/mount.h>
David Howells54ceac42006-08-22 20:06:13 -040025#include <linux/vfs.h>
26#include <linux/namei.h>
David Howells738a3512006-07-30 14:58:27 -040027#include <linux/security.h>
David Howells54ceac42006-08-22 20:06:13 -040028
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
David Howells54ceac42006-08-22 20:06:13 -040030
Stanislav Kinsbursky4e437e92012-10-02 14:19:14 +040031#include "internal.h"
32
David Howells54ceac42006-08-22 20:06:13 -040033#define NFSDBG_FACILITY NFSDBG_CLIENT
David Howells54ceac42006-08-22 20:06:13 -040034
35/*
Trond Myklebustb09b9412007-10-25 13:56:10 -040036 * Set the superblock root dentry.
37 * Note that this function frees the inode in case of error.
38 */
39static 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) {
Al Viro48fde702012-01-08 22:15:13 -050043 sb->s_root = d_make_root(inode);
44 if (sb->s_root == NULL)
Trond Myklebustb09b9412007-10-25 13:56:10 -040045 return -ENOMEM;
Al Viro7de9c6ee2010-10-23 11:11:40 -040046 ihold(inode);
Trond Myklebusta10db502007-12-12 11:12:15 -050047 /*
48 * Ensure that this dentry is invisible to d_find_alias().
49 * Otherwise, it may be spliced into the tree by
Al Viro41d28bc2014-10-12 22:24:21 -040050 * d_splice_alias if a parent directory from the same
Trond Myklebusta10db502007-12-12 11:12:15 -050051 * 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 */
David Howells2b0143b2015-03-17 22:25:59 +000055 spin_lock(&d_inode(sb->s_root)->i_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110056 spin_lock(&sb->s_root->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -040057 hlist_del_init(&sb->s_root->d_u.d_alias);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110058 spin_unlock(&sb->s_root->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +000059 spin_unlock(&d_inode(sb->s_root)->i_lock);
Trond Myklebustb09b9412007-10-25 13:56:10 -040060 }
61 return 0;
62}
63
64/*
David Howells54ceac42006-08-22 20:06:13 -040065 * get an NFS2/NFS3 root dentry from the root filehandle
66 */
Al Viro0d5839a2011-03-16 05:27:27 -040067struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
68 const char *devname)
David Howells54ceac42006-08-22 20:06:13 -040069{
70 struct nfs_server *server = NFS_SB(sb);
71 struct nfs_fsinfo fsinfo;
Trond Myklebust8bac9db2010-04-16 16:22:48 -040072 struct dentry *ret;
David Howells54ceac42006-08-22 20:06:13 -040073 struct inode *inode;
Al Virob1942c52011-03-16 05:44:14 -040074 void *name = kstrdup(devname, GFP_KERNEL);
David Howells54ceac42006-08-22 20:06:13 -040075 int error;
76
Al Virob1942c52011-03-16 05:44:14 -040077 if (!name)
78 return ERR_PTR(-ENOMEM);
79
David Howells54ceac42006-08-22 20:06:13 -040080 /* get the actual root for this mount */
Trond Myklebust8bac9db2010-04-16 16:22:48 -040081 fsinfo.fattr = nfs_alloc_fattr();
Al Virob1942c52011-03-16 05:44:14 -040082 if (fsinfo.fattr == NULL) {
83 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040084 return ERR_PTR(-ENOMEM);
Al Virob1942c52011-03-16 05:44:14 -040085 }
David Howells54ceac42006-08-22 20:06:13 -040086
87 error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
88 if (error < 0) {
89 dprintk("nfs_get_root: getattr error = %d\n", -error);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040090 ret = ERR_PTR(error);
91 goto out;
David Howells54ceac42006-08-22 20:06:13 -040092 }
93
David Quigley1775fd32013-05-22 12:50:42 -040094 inode = nfs_fhget(sb, mntfh, fsinfo.fattr, NULL);
David Howells54ceac42006-08-22 20:06:13 -040095 if (IS_ERR(inode)) {
96 dprintk("nfs_get_root: get root inode failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -040097 ret = ERR_CAST(inode);
98 goto out;
David Howells54ceac42006-08-22 20:06:13 -040099 }
100
Trond Myklebustb09b9412007-10-25 13:56:10 -0400101 error = nfs_superblock_set_dummy_root(sb, inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400102 if (error != 0) {
103 ret = ERR_PTR(error);
104 goto out;
105 }
Trond Myklebustb09b9412007-10-25 13:56:10 -0400106
David Howells54ceac42006-08-22 20:06:13 -0400107 /* root dentries normally start off anonymous and get spliced in later
108 * if the dentry tree reaches them; however if the dentry already
109 * exists, we'll pick it up at this point and use it as the root
110 */
J. Bruce Fields1a0a3972014-02-14 17:35:37 -0500111 ret = d_obtain_root(inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400112 if (IS_ERR(ret)) {
David Howells54ceac42006-08-22 20:06:13 -0400113 dprintk("nfs_get_root: get root dentry failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400114 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400115 }
116
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400117 security_d_instantiate(ret, inode);
Al Virob1942c52011-03-16 05:44:14 -0400118 spin_lock(&ret->d_lock);
Kinglong Mee4dfc7fd2014-04-15 17:22:59 +0800119 if (IS_ROOT(ret) && !ret->d_fsdata &&
120 !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
Al Virob1942c52011-03-16 05:44:14 -0400121 ret->d_fsdata = name;
122 name = NULL;
123 }
124 spin_unlock(&ret->d_lock);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400125out:
Tim Gardner96aa1542013-02-12 13:03:42 -0700126 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400127 nfs_free_fattr(fsinfo.fattr);
128 return ret;
David Howells54ceac42006-08-22 20:06:13 -0400129}