12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 254ceac45SDavid Howells /* getroot.c: get the root dentry for an NFS mount 354ceac45SDavid Howells * 454ceac45SDavid Howells * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 554ceac45SDavid Howells * Written by David Howells (dhowells@redhat.com) 654ceac45SDavid Howells */ 754ceac45SDavid Howells 854ceac45SDavid Howells #include <linux/module.h> 954ceac45SDavid Howells #include <linux/init.h> 1054ceac45SDavid Howells 1154ceac45SDavid Howells #include <linux/time.h> 1254ceac45SDavid Howells #include <linux/kernel.h> 1354ceac45SDavid Howells #include <linux/mm.h> 1454ceac45SDavid Howells #include <linux/string.h> 1554ceac45SDavid Howells #include <linux/stat.h> 1654ceac45SDavid Howells #include <linux/errno.h> 1754ceac45SDavid Howells #include <linux/unistd.h> 1854ceac45SDavid Howells #include <linux/sunrpc/clnt.h> 1954ceac45SDavid Howells #include <linux/sunrpc/stats.h> 2054ceac45SDavid Howells #include <linux/nfs_fs.h> 2154ceac45SDavid Howells #include <linux/nfs_mount.h> 2254ceac45SDavid Howells #include <linux/lockd/bind.h> 2354ceac45SDavid Howells #include <linux/seq_file.h> 2454ceac45SDavid Howells #include <linux/mount.h> 2554ceac45SDavid Howells #include <linux/vfs.h> 2654ceac45SDavid Howells #include <linux/namei.h> 27738a3519SDavid Howells #include <linux/security.h> 2854ceac45SDavid Howells 297c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 3054ceac45SDavid Howells 314e437e95SStanislav Kinsbursky #include "internal.h" 324e437e95SStanislav Kinsbursky 3354ceac45SDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 3454ceac45SDavid Howells 3554ceac45SDavid Howells /* 36b09b9417STrond Myklebust * Set the superblock root dentry. 37b09b9417STrond Myklebust * Note that this function frees the inode in case of error. 38b09b9417STrond Myklebust */ 39b09b9417STrond Myklebust static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode) 40b09b9417STrond Myklebust { 41b09b9417STrond Myklebust /* The mntroot acts as the dummy root dentry for this superblock */ 42b09b9417STrond Myklebust if (sb->s_root == NULL) { 4348fde701SAl Viro sb->s_root = d_make_root(inode); 4448fde701SAl Viro if (sb->s_root == NULL) 45b09b9417STrond Myklebust return -ENOMEM; 467de9c6eeSAl Viro ihold(inode); 47a10db50aSTrond Myklebust /* 48a10db50aSTrond Myklebust * Ensure that this dentry is invisible to d_find_alias(). 49a10db50aSTrond Myklebust * Otherwise, it may be spliced into the tree by 5041d28bcaSAl Viro * d_splice_alias if a parent directory from the same 51a10db50aSTrond Myklebust * filesystem gets mounted at a later time. 52a10db50aSTrond Myklebust * This again causes shrink_dcache_for_umount_subtree() to 53a10db50aSTrond Myklebust * Oops, since the test for IS_ROOT() will fail. 54a10db50aSTrond Myklebust */ 552b0143b5SDavid Howells spin_lock(&d_inode(sb->s_root)->i_lock); 56b23fb0a6SNick Piggin spin_lock(&sb->s_root->d_lock); 57946e51f2SAl Viro hlist_del_init(&sb->s_root->d_u.d_alias); 58b23fb0a6SNick Piggin spin_unlock(&sb->s_root->d_lock); 592b0143b5SDavid Howells spin_unlock(&d_inode(sb->s_root)->i_lock); 60b09b9417STrond Myklebust } 61b09b9417STrond Myklebust return 0; 62b09b9417STrond Myklebust } 63b09b9417STrond Myklebust 64b09b9417STrond Myklebust /* 6554ceac45SDavid Howells * get an NFS2/NFS3 root dentry from the root filehandle 6654ceac45SDavid Howells */ 6762a55d08SScott Mayhew int nfs_get_root(struct super_block *s, struct fs_context *fc) 6854ceac45SDavid Howells { 6962a55d08SScott Mayhew struct nfs_fs_context *ctx = nfs_fc2context(fc); 7062a55d08SScott Mayhew struct nfs_server *server = NFS_SB(s); 7154ceac45SDavid Howells struct nfs_fsinfo fsinfo; 7262a55d08SScott Mayhew struct dentry *root; 7354ceac45SDavid Howells struct inode *inode; 7462a55d08SScott Mayhew char *name; 7562a55d08SScott Mayhew int error = -ENOMEM; 7654ceac45SDavid Howells 7762a55d08SScott Mayhew name = kstrdup(fc->source, GFP_KERNEL); 78b1942c5fSAl Viro if (!name) 7962a55d08SScott Mayhew goto out; 80b1942c5fSAl Viro 8154ceac45SDavid Howells /* get the actual root for this mount */ 828bac9db9STrond Myklebust fsinfo.fattr = nfs_alloc_fattr(); 8362a55d08SScott Mayhew if (fsinfo.fattr == NULL) 8462a55d08SScott Mayhew goto out_name; 8554ceac45SDavid Howells 8662a55d08SScott Mayhew error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo); 8754ceac45SDavid Howells if (error < 0) { 8854ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 89ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't getattr on root"); 9062a55d08SScott Mayhew goto out_fattr; 9154ceac45SDavid Howells } 9254ceac45SDavid Howells 9362a55d08SScott Mayhew inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL); 9454ceac45SDavid Howells if (IS_ERR(inode)) { 9554ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 9662a55d08SScott Mayhew error = PTR_ERR(inode); 97ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root inode"); 9862a55d08SScott Mayhew goto out_fattr; 9954ceac45SDavid Howells } 10054ceac45SDavid Howells 10162a55d08SScott Mayhew error = nfs_superblock_set_dummy_root(s, inode); 10262a55d08SScott Mayhew if (error != 0) 10362a55d08SScott Mayhew goto out_fattr; 104b09b9417STrond Myklebust 10554ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 10654ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 10754ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 10854ceac45SDavid Howells */ 10962a55d08SScott Mayhew root = d_obtain_root(inode); 11062a55d08SScott Mayhew if (IS_ERR(root)) { 11154ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 11262a55d08SScott Mayhew error = PTR_ERR(root); 113ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root dentry"); 11462a55d08SScott Mayhew goto out_fattr; 11554ceac45SDavid Howells } 11654ceac45SDavid Howells 11762a55d08SScott Mayhew security_d_instantiate(root, inode); 11862a55d08SScott Mayhew spin_lock(&root->d_lock); 11962a55d08SScott Mayhew if (IS_ROOT(root) && !root->d_fsdata && 12062a55d08SScott Mayhew !(root->d_flags & DCACHE_NFSFS_RENAMED)) { 12162a55d08SScott Mayhew root->d_fsdata = name; 122b1942c5fSAl Viro name = NULL; 123b1942c5fSAl Viro } 12462a55d08SScott Mayhew spin_unlock(&root->d_lock); 12562a55d08SScott Mayhew fc->root = root; 12662a55d08SScott Mayhew error = 0; 12762a55d08SScott Mayhew 12862a55d08SScott Mayhew out_fattr: 1298bac9db9STrond Myklebust nfs_free_fattr(fsinfo.fattr); 13062a55d08SScott Mayhew out_name: 13162a55d08SScott Mayhew kfree(name); 13262a55d08SScott Mayhew out: 13362a55d08SScott Mayhew return error; 13454ceac45SDavid Howells } 135