Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* getroot.c: get the root dentry for an NFS mount
0003  *
0004  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 
0011 #include <linux/time.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mm.h>
0014 #include <linux/string.h>
0015 #include <linux/stat.h>
0016 #include <linux/errno.h>
0017 #include <linux/unistd.h>
0018 #include <linux/sunrpc/clnt.h>
0019 #include <linux/sunrpc/stats.h>
0020 #include <linux/nfs_fs.h>
0021 #include <linux/nfs_mount.h>
0022 #include <linux/lockd/bind.h>
0023 #include <linux/seq_file.h>
0024 #include <linux/mount.h>
0025 #include <linux/vfs.h>
0026 #include <linux/namei.h>
0027 #include <linux/security.h>
0028 
0029 #include <linux/uaccess.h>
0030 
0031 #include "internal.h"
0032 
0033 #define NFSDBG_FACILITY     NFSDBG_CLIENT
0034 
0035 /*
0036  * Set the superblock root dentry.
0037  * Note that this function frees the inode in case of error.
0038  */
0039 static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
0040 {
0041     /* The mntroot acts as the dummy root dentry for this superblock */
0042     if (sb->s_root == NULL) {
0043         sb->s_root = d_make_root(inode);
0044         if (sb->s_root == NULL)
0045             return -ENOMEM;
0046         ihold(inode);
0047         /*
0048          * Ensure that this dentry is invisible to d_find_alias().
0049          * Otherwise, it may be spliced into the tree by
0050          * d_splice_alias if a parent directory from the same
0051          * filesystem gets mounted at a later time.
0052          * This again causes shrink_dcache_for_umount_subtree() to
0053          * Oops, since the test for IS_ROOT() will fail.
0054          */
0055         spin_lock(&d_inode(sb->s_root)->i_lock);
0056         spin_lock(&sb->s_root->d_lock);
0057         hlist_del_init(&sb->s_root->d_u.d_alias);
0058         spin_unlock(&sb->s_root->d_lock);
0059         spin_unlock(&d_inode(sb->s_root)->i_lock);
0060     }
0061     return 0;
0062 }
0063 
0064 /*
0065  * get an NFS2/NFS3 root dentry from the root filehandle
0066  */
0067 int nfs_get_root(struct super_block *s, struct fs_context *fc)
0068 {
0069     struct nfs_fs_context *ctx = nfs_fc2context(fc);
0070     struct nfs_server *server = NFS_SB(s), *clone_server;
0071     struct nfs_fsinfo fsinfo;
0072     struct dentry *root;
0073     struct inode *inode;
0074     char *name;
0075     int error = -ENOMEM;
0076     unsigned long kflags = 0, kflags_out = 0;
0077 
0078     name = kstrdup(fc->source, GFP_KERNEL);
0079     if (!name)
0080         goto out;
0081 
0082     /* get the actual root for this mount */
0083     fsinfo.fattr = nfs_alloc_fattr_with_label(server);
0084     if (fsinfo.fattr == NULL)
0085         goto out_name;
0086 
0087     error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
0088     if (error < 0) {
0089         dprintk("nfs_get_root: getattr error = %d\n", -error);
0090         nfs_errorf(fc, "NFS: Couldn't getattr on root");
0091         goto out_fattr;
0092     }
0093 
0094     inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr);
0095     if (IS_ERR(inode)) {
0096         dprintk("nfs_get_root: get root inode failed\n");
0097         error = PTR_ERR(inode);
0098         nfs_errorf(fc, "NFS: Couldn't get root inode");
0099         goto out_fattr;
0100     }
0101 
0102     error = nfs_superblock_set_dummy_root(s, inode);
0103     if (error != 0)
0104         goto out_fattr;
0105 
0106     /* root dentries normally start off anonymous and get spliced in later
0107      * if the dentry tree reaches them; however if the dentry already
0108      * exists, we'll pick it up at this point and use it as the root
0109      */
0110     root = d_obtain_root(inode);
0111     if (IS_ERR(root)) {
0112         dprintk("nfs_get_root: get root dentry failed\n");
0113         error = PTR_ERR(root);
0114         nfs_errorf(fc, "NFS: Couldn't get root dentry");
0115         goto out_fattr;
0116     }
0117 
0118     security_d_instantiate(root, inode);
0119     spin_lock(&root->d_lock);
0120     if (IS_ROOT(root) && !root->d_fsdata &&
0121         !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
0122         root->d_fsdata = name;
0123         name = NULL;
0124     }
0125     spin_unlock(&root->d_lock);
0126     fc->root = root;
0127     if (server->caps & NFS_CAP_SECURITY_LABEL)
0128         kflags |= SECURITY_LSM_NATIVE_LABELS;
0129     if (ctx->clone_data.sb) {
0130         if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
0131             error = -ESTALE;
0132             goto error_splat_root;
0133         }
0134         /* clone lsm security options from the parent to the new sb */
0135         error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
0136                            s, kflags, &kflags_out);
0137         if (error)
0138             goto error_splat_root;
0139         clone_server = NFS_SB(ctx->clone_data.sb);
0140         server->has_sec_mnt_opts = clone_server->has_sec_mnt_opts;
0141     } else {
0142         error = security_sb_set_mnt_opts(s, fc->security,
0143                             kflags, &kflags_out);
0144     }
0145     if (error)
0146         goto error_splat_root;
0147     if (server->caps & NFS_CAP_SECURITY_LABEL &&
0148         !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
0149         server->caps &= ~NFS_CAP_SECURITY_LABEL;
0150 
0151     nfs_setsecurity(inode, fsinfo.fattr);
0152     error = 0;
0153 
0154 out_fattr:
0155     nfs_free_fattr(fsinfo.fattr);
0156 out_name:
0157     kfree(name);
0158 out:
0159     return error;
0160 error_splat_root:
0161     dput(fc->root);
0162     fc->root = NULL;
0163     goto out_fattr;
0164 }