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); 70eae00c5dSScott 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 */ 83d755ad8dSAnna Schumaker fsinfo.fattr = nfs_alloc_fattr_with_label(server); 8462a55d08SScott Mayhew if (fsinfo.fattr == NULL) 8562a55d08SScott Mayhew goto out_name; 8654ceac45SDavid Howells 8762a55d08SScott Mayhew error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo); 8854ceac45SDavid Howells if (error < 0) { 8954ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 90ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't getattr on root"); 91d755ad8dSAnna Schumaker goto out_fattr; 9254ceac45SDavid Howells } 9354ceac45SDavid Howells 94*cf7ab00aSAnna Schumaker inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr); 9554ceac45SDavid Howells if (IS_ERR(inode)) { 9654ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 9762a55d08SScott Mayhew error = PTR_ERR(inode); 98ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root inode"); 99d755ad8dSAnna Schumaker goto out_fattr; 10054ceac45SDavid Howells } 10154ceac45SDavid Howells 10262a55d08SScott Mayhew error = nfs_superblock_set_dummy_root(s, inode); 10362a55d08SScott Mayhew if (error != 0) 104d755ad8dSAnna Schumaker goto out_fattr; 105b09b9417STrond Myklebust 10654ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 10754ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 10854ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 10954ceac45SDavid Howells */ 11062a55d08SScott Mayhew root = d_obtain_root(inode); 11162a55d08SScott Mayhew if (IS_ERR(root)) { 11254ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 11362a55d08SScott Mayhew error = PTR_ERR(root); 114ce8866f0SScott Mayhew nfs_errorf(fc, "NFS: Couldn't get root dentry"); 115d755ad8dSAnna Schumaker goto out_fattr; 11654ceac45SDavid Howells } 11754ceac45SDavid Howells 11862a55d08SScott Mayhew security_d_instantiate(root, inode); 11962a55d08SScott Mayhew spin_lock(&root->d_lock); 12062a55d08SScott Mayhew if (IS_ROOT(root) && !root->d_fsdata && 12162a55d08SScott Mayhew !(root->d_flags & DCACHE_NFSFS_RENAMED)) { 12262a55d08SScott Mayhew root->d_fsdata = name; 123b1942c5fSAl Viro name = NULL; 124b1942c5fSAl Viro } 12562a55d08SScott Mayhew spin_unlock(&root->d_lock); 12662a55d08SScott Mayhew fc->root = root; 127eae00c5dSScott Mayhew if (server->caps & NFS_CAP_SECURITY_LABEL) 128779df6a5SScott Mayhew kflags |= SECURITY_LSM_NATIVE_LABELS; 129779df6a5SScott Mayhew if (ctx->clone_data.sb) { 130779df6a5SScott Mayhew if (d_inode(fc->root)->i_fop != &nfs_dir_operations) { 131779df6a5SScott Mayhew error = -ESTALE; 132779df6a5SScott Mayhew goto error_splat_root; 133779df6a5SScott Mayhew } 134779df6a5SScott Mayhew /* clone lsm security options from the parent to the new sb */ 135779df6a5SScott Mayhew error = security_sb_clone_mnt_opts(ctx->clone_data.sb, 136779df6a5SScott Mayhew s, kflags, &kflags_out); 137eae00c5dSScott Mayhew if (error) 138eae00c5dSScott Mayhew goto error_splat_root; 139eae00c5dSScott Mayhew clone_server = NFS_SB(ctx->clone_data.sb); 140eae00c5dSScott Mayhew server->has_sec_mnt_opts = clone_server->has_sec_mnt_opts; 141779df6a5SScott Mayhew } else { 142779df6a5SScott Mayhew error = security_sb_set_mnt_opts(s, fc->security, 143779df6a5SScott Mayhew kflags, &kflags_out); 144779df6a5SScott Mayhew } 145779df6a5SScott Mayhew if (error) 146779df6a5SScott Mayhew goto error_splat_root; 147eae00c5dSScott Mayhew if (server->caps & NFS_CAP_SECURITY_LABEL && 148779df6a5SScott Mayhew !(kflags_out & SECURITY_LSM_NATIVE_LABELS)) 149eae00c5dSScott Mayhew server->caps &= ~NFS_CAP_SECURITY_LABEL; 150779df6a5SScott Mayhew 151779df6a5SScott Mayhew nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label); 15262a55d08SScott Mayhew error = 0; 15362a55d08SScott Mayhew 15462a55d08SScott Mayhew out_fattr: 1558bac9db9STrond Myklebust nfs_free_fattr(fsinfo.fattr); 15662a55d08SScott Mayhew out_name: 15762a55d08SScott Mayhew kfree(name); 15862a55d08SScott Mayhew out: 15962a55d08SScott Mayhew return error; 160779df6a5SScott Mayhew error_splat_root: 161779df6a5SScott Mayhew dput(fc->root); 162779df6a5SScott Mayhew fc->root = NULL; 163d755ad8dSAnna Schumaker goto out_fattr; 16454ceac45SDavid Howells } 165