blob: 391dafaf918273648784a00a81a8dc4a53e33cc9 [file] [log] [blame]
David Howells54ceac42006-08-22 20:06:13 -04001/* 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
David Howells54ceac42006-08-22 20:06:13 -040012#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>
David Howells54ceac42006-08-22 20:06:13 -040026#include <linux/lockd/bind.h>
David Howells54ceac42006-08-22 20:06:13 -040027#include <linux/seq_file.h>
28#include <linux/mount.h>
David Howells54ceac42006-08-22 20:06:13 -040029#include <linux/vfs.h>
30#include <linux/namei.h>
David Howells738a3512006-07-30 14:58:27 -040031#include <linux/security.h>
David Howells54ceac42006-08-22 20:06:13 -040032
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
David Howells54ceac42006-08-22 20:06:13 -040034
Stanislav Kinsbursky4e437e92012-10-02 14:19:14 +040035#include "internal.h"
36
David Howells54ceac42006-08-22 20:06:13 -040037#define NFSDBG_FACILITY NFSDBG_CLIENT
David Howells54ceac42006-08-22 20:06:13 -040038
39/*
Trond Myklebustb09b9412007-10-25 13:56:10 -040040 * 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) {
Al Viro48fde702012-01-08 22:15:13 -050047 sb->s_root = d_make_root(inode);
48 if (sb->s_root == NULL)
Trond Myklebustb09b9412007-10-25 13:56:10 -040049 return -ENOMEM;
Al Viro7de9c6ee2010-10-23 11:11:40 -040050 ihold(inode);
Trond Myklebusta10db502007-12-12 11:12:15 -050051 /*
52 * Ensure that this dentry is invisible to d_find_alias().
53 * Otherwise, it may be spliced into the tree by
Al Viro41d28bc2014-10-12 22:24:21 -040054 * d_splice_alias if a parent directory from the same
Trond Myklebusta10db502007-12-12 11:12:15 -050055 * 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 */
David Howells2b0143b2015-03-17 22:25:59 +000059 spin_lock(&d_inode(sb->s_root)->i_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110060 spin_lock(&sb->s_root->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -040061 hlist_del_init(&sb->s_root->d_u.d_alias);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110062 spin_unlock(&sb->s_root->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +000063 spin_unlock(&d_inode(sb->s_root)->i_lock);
Trond Myklebustb09b9412007-10-25 13:56:10 -040064 }
65 return 0;
66}
67
68/*
David Howells54ceac42006-08-22 20:06:13 -040069 * get an NFS2/NFS3 root dentry from the root filehandle
70 */
Al Viro0d5839a2011-03-16 05:27:27 -040071struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
72 const char *devname)
David Howells54ceac42006-08-22 20:06:13 -040073{
74 struct nfs_server *server = NFS_SB(sb);
75 struct nfs_fsinfo fsinfo;
Trond Myklebust8bac9db2010-04-16 16:22:48 -040076 struct dentry *ret;
David Howells54ceac42006-08-22 20:06:13 -040077 struct inode *inode;
Al Virob1942c52011-03-16 05:44:14 -040078 void *name = kstrdup(devname, GFP_KERNEL);
David Howells54ceac42006-08-22 20:06:13 -040079 int error;
80
Al Virob1942c52011-03-16 05:44:14 -040081 if (!name)
82 return ERR_PTR(-ENOMEM);
83
David Howells54ceac42006-08-22 20:06:13 -040084 /* get the actual root for this mount */
Trond Myklebust8bac9db2010-04-16 16:22:48 -040085 fsinfo.fattr = nfs_alloc_fattr();
Al Virob1942c52011-03-16 05:44:14 -040086 if (fsinfo.fattr == NULL) {
87 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040088 return ERR_PTR(-ENOMEM);
Al Virob1942c52011-03-16 05:44:14 -040089 }
David Howells54ceac42006-08-22 20:06:13 -040090
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);
Trond Myklebust8bac9db2010-04-16 16:22:48 -040094 ret = ERR_PTR(error);
95 goto out;
David Howells54ceac42006-08-22 20:06:13 -040096 }
97
David Quigley1775fd32013-05-22 12:50:42 -040098 inode = nfs_fhget(sb, mntfh, fsinfo.fattr, NULL);
David Howells54ceac42006-08-22 20:06:13 -040099 if (IS_ERR(inode)) {
100 dprintk("nfs_get_root: get root inode failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400101 ret = ERR_CAST(inode);
102 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400103 }
104
Trond Myklebustb09b9412007-10-25 13:56:10 -0400105 error = nfs_superblock_set_dummy_root(sb, inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400106 if (error != 0) {
107 ret = ERR_PTR(error);
108 goto out;
109 }
Trond Myklebustb09b9412007-10-25 13:56:10 -0400110
David Howells54ceac42006-08-22 20:06:13 -0400111 /* 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 */
J. Bruce Fields1a0a3972014-02-14 17:35:37 -0500115 ret = d_obtain_root(inode);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400116 if (IS_ERR(ret)) {
David Howells54ceac42006-08-22 20:06:13 -0400117 dprintk("nfs_get_root: get root dentry failed\n");
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400118 goto out;
David Howells54ceac42006-08-22 20:06:13 -0400119 }
120
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400121 security_d_instantiate(ret, inode);
Al Virob1942c52011-03-16 05:44:14 -0400122 spin_lock(&ret->d_lock);
Kinglong Mee4dfc7fd2014-04-15 17:22:59 +0800123 if (IS_ROOT(ret) && !ret->d_fsdata &&
124 !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
Al Virob1942c52011-03-16 05:44:14 -0400125 ret->d_fsdata = name;
126 name = NULL;
127 }
128 spin_unlock(&ret->d_lock);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400129out:
Tim Gardner96aa1542013-02-12 13:03:42 -0700130 kfree(name);
Trond Myklebust8bac9db2010-04-16 16:22:48 -0400131 nfs_free_fattr(fsinfo.fattr);
132 return ret;
David Howells54ceac42006-08-22 20:06:13 -0400133}