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); 70*eae00c5dSScott Mayhew struct nfs_server *server = NFS_SB(s), *clone_server; 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; 76779df6a5SScott Mayhew unsigned long kflags = 0, kflags_out = 0; 7754ceac45SDavid Howells 7862a55d08SScott Mayhew name = kstrdup(fc->source, GFP_KERNEL); 79b1942c5fSAl Viro if (!name) 8062a55d08SScott Mayhew goto out; 81b1942c5fSAl Viro 8254ceac45SDavid Howells /* get the actual root for this mount */ 838bac9db9STrond Myklebust fsinfo.fattr = nfs_alloc_fattr(); 8462a55d08SScott Mayhew if (fsinfo.fattr == NULL) 8562a55d08SScott Mayhew goto out_name; 8654ceac45SDavid Howells 87779df6a5SScott Mayhew fsinfo.fattr->label = nfs4_label_alloc(server, GFP_KERNEL); 88779df6a5SScott Mayhew if (IS_ERR(fsinfo.fattr->label)) 89779df6a5SScott Mayhew goto out_fattr; 9062a55d08SScott Mayhew error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo); 9154ceac45SDavid Howells if (error < 0) { 9254ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 93ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't getattr on root"); 94779df6a5SScott Mayhew goto out_label; 9554ceac45SDavid Howells } 9654ceac45SDavid Howells 9762a55d08SScott Mayhew inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL); 9854ceac45SDavid Howells if (IS_ERR(inode)) { 9954ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 10062a55d08SScott Mayhew error = PTR_ERR(inode); 101ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root inode"); 102779df6a5SScott Mayhew goto out_label; 10354ceac45SDavid Howells } 10454ceac45SDavid Howells 10562a55d08SScott Mayhew error = nfs_superblock_set_dummy_root(s, inode); 10662a55d08SScott Mayhew if (error != 0) 107779df6a5SScott Mayhew goto out_label; 108b09b9417STrond Myklebust 10954ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 11054ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 11154ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 11254ceac45SDavid Howells */ 11362a55d08SScott Mayhew root = d_obtain_root(inode); 11462a55d08SScott Mayhew if (IS_ERR(root)) { 11554ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 11662a55d08SScott Mayhew error = PTR_ERR(root); 117ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root dentry"); 118779df6a5SScott Mayhew goto out_label; 11954ceac45SDavid Howells } 12054ceac45SDavid Howells 12162a55d08SScott Mayhew security_d_instantiate(root, inode); 12262a55d08SScott Mayhew spin_lock(&root->d_lock); 12362a55d08SScott Mayhew if (IS_ROOT(root) && !root->d_fsdata && 12462a55d08SScott Mayhew !(root->d_flags & DCACHE_NFSFS_RENAMED)) { 12562a55d08SScott Mayhew root->d_fsdata = name; 126b1942c5fSAl Viro name = NULL; 127b1942c5fSAl Viro } 12862a55d08SScott Mayhew spin_unlock(&root->d_lock); 12962a55d08SScott Mayhew fc->root = root; 130*eae00c5dSScott Mayhew if (server->caps & NFS_CAP_SECURITY_LABEL) 131779df6a5SScott Mayhew kflags |= SECURITY_LSM_NATIVE_LABELS; 132779df6a5SScott Mayhew if (ctx->clone_data.sb) { 133779df6a5SScott Mayhew if (d_inode(fc->root)->i_fop != &nfs_dir_operations) { 134779df6a5SScott Mayhew error = -ESTALE; 135779df6a5SScott Mayhew goto error_splat_root; 136779df6a5SScott Mayhew } 137779df6a5SScott Mayhew /* clone lsm security options from the parent to the new sb */ 138779df6a5SScott Mayhew error = security_sb_clone_mnt_opts(ctx->clone_data.sb, 139779df6a5SScott Mayhew s, kflags, &kflags_out); 140*eae00c5dSScott Mayhew if (error) 141*eae00c5dSScott Mayhew goto error_splat_root; 142*eae00c5dSScott Mayhew clone_server = NFS_SB(ctx->clone_data.sb); 143*eae00c5dSScott Mayhew server->has_sec_mnt_opts = clone_server->has_sec_mnt_opts; 144779df6a5SScott Mayhew } else { 145779df6a5SScott Mayhew error = security_sb_set_mnt_opts(s, fc->security, 146779df6a5SScott Mayhew kflags, &kflags_out); 147779df6a5SScott Mayhew } 148779df6a5SScott Mayhew if (error) 149779df6a5SScott Mayhew goto error_splat_root; 150*eae00c5dSScott Mayhew if (server->caps & NFS_CAP_SECURITY_LABEL && 151779df6a5SScott Mayhew !(kflags_out & SECURITY_LSM_NATIVE_LABELS)) 152*eae00c5dSScott Mayhew server->caps &= ~NFS_CAP_SECURITY_LABEL; 153779df6a5SScott Mayhew 154779df6a5SScott Mayhew nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label); 15562a55d08SScott Mayhew error = 0; 15662a55d08SScott Mayhew 157779df6a5SScott Mayhew out_label: 158779df6a5SScott Mayhew nfs4_label_free(fsinfo.fattr->label); 15962a55d08SScott Mayhew out_fattr: 1608bac9db9STrond Myklebust nfs_free_fattr(fsinfo.fattr); 16162a55d08SScott Mayhew out_name: 16262a55d08SScott Mayhew kfree(name); 16362a55d08SScott Mayhew out: 16462a55d08SScott Mayhew return error; 165779df6a5SScott Mayhew error_splat_root: 166779df6a5SScott Mayhew dput(fc->root); 167779df6a5SScott Mayhew fc->root = NULL; 168779df6a5SScott Mayhew goto out_label; 16954ceac45SDavid Howells } 170