linux/fs/nfs/getroot.c
<<
>>
Prefs
   1/* 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
  12#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>
  26#include <linux/lockd/bind.h>
  27#include <linux/seq_file.h>
  28#include <linux/mount.h>
  29#include <linux/vfs.h>
  30#include <linux/namei.h>
  31#include <linux/security.h>
  32
  33#include <linux/uaccess.h>
  34
  35#include "internal.h"
  36
  37#define NFSDBG_FACILITY         NFSDBG_CLIENT
  38
  39/*
  40 * 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) {
  47                sb->s_root = d_make_root(inode);
  48                if (sb->s_root == NULL)
  49                        return -ENOMEM;
  50                ihold(inode);
  51                /*
  52                 * Ensure that this dentry is invisible to d_find_alias().
  53                 * Otherwise, it may be spliced into the tree by
  54                 * d_splice_alias if a parent directory from the same
  55                 * 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                 */
  59                spin_lock(&d_inode(sb->s_root)->i_lock);
  60                spin_lock(&sb->s_root->d_lock);
  61                hlist_del_init(&sb->s_root->d_u.d_alias);
  62                spin_unlock(&sb->s_root->d_lock);
  63                spin_unlock(&d_inode(sb->s_root)->i_lock);
  64        }
  65        return 0;
  66}
  67
  68/*
  69 * get an NFS2/NFS3 root dentry from the root filehandle
  70 */
  71int nfs_get_root(struct super_block *s, struct fs_context *fc)
  72{
  73        struct nfs_fs_context *ctx = nfs_fc2context(fc);
  74        struct nfs_server *server = NFS_SB(s);
  75        struct nfs_fsinfo fsinfo;
  76        struct dentry *root;
  77        struct inode *inode;
  78        char *name;
  79        int error = -ENOMEM;
  80        unsigned long kflags = 0, kflags_out = 0;
  81
  82        name = kstrdup(fc->source, GFP_KERNEL);
  83        if (!name)
  84                goto out;
  85
  86        /* get the actual root for this mount */
  87        fsinfo.fattr = nfs_alloc_fattr();
  88        if (fsinfo.fattr == NULL)
  89                goto out_name;
  90
  91        fsinfo.fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
  92        if (IS_ERR(fsinfo.fattr->label))
  93                goto out_fattr;
  94        error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
  95        if (error < 0) {
  96                dprintk("nfs_get_root: getattr error = %d\n", -error);
  97                nfs_errorf(fc, "NFS: Couldn't getattr on root");
  98                goto out_label;
  99        }
 100
 101        inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL);
 102        if (IS_ERR(inode)) {
 103                dprintk("nfs_get_root: get root inode failed\n");
 104                error = PTR_ERR(inode);
 105                nfs_errorf(fc, "NFS: Couldn't get root inode");
 106                goto out_label;
 107        }
 108
 109        error = nfs_superblock_set_dummy_root(s, inode);
 110        if (error != 0)
 111                goto out_label;
 112
 113        /* root dentries normally start off anonymous and get spliced in later
 114         * if the dentry tree reaches them; however if the dentry already
 115         * exists, we'll pick it up at this point and use it as the root
 116         */
 117        root = d_obtain_root(inode);
 118        if (IS_ERR(root)) {
 119                dprintk("nfs_get_root: get root dentry failed\n");
 120                error = PTR_ERR(root);
 121                nfs_errorf(fc, "NFS: Couldn't get root dentry");
 122                goto out_label;
 123        }
 124
 125        security_d_instantiate(root, inode);
 126        spin_lock(&root->d_lock);
 127        if (IS_ROOT(root) && !root->d_fsdata &&
 128            !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
 129                root->d_fsdata = name;
 130                name = NULL;
 131        }
 132        spin_unlock(&root->d_lock);
 133        fc->root = root;
 134        if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
 135                kflags |= SECURITY_LSM_NATIVE_LABELS;
 136        if (ctx->clone_data.sb) {
 137                if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
 138                        error = -ESTALE;
 139                        goto error_splat_root;
 140                }
 141                /* clone lsm security options from the parent to the new sb */
 142                error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
 143                                                   s, kflags, &kflags_out);
 144        } else {
 145                error = security_sb_set_mnt_opts(s, fc->security,
 146                                                        kflags, &kflags_out);
 147        }
 148        if (error)
 149                goto error_splat_root;
 150        if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
 151                !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
 152                NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
 153
 154        nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label);
 155        error = 0;
 156
 157out_label:
 158        nfs4_label_free(fsinfo.fattr->label);
 159out_fattr:
 160        nfs_free_fattr(fsinfo.fattr);
 161out_name:
 162        kfree(name);
 163out:
 164        return error;
 165error_splat_root:
 166        dput(fc->root);
 167        fc->root = NULL;
 168        goto out_label;
 169}
 170