154ceac45SDavid Howells /* getroot.c: get the root dentry for an NFS mount 254ceac45SDavid Howells * 354ceac45SDavid Howells * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 454ceac45SDavid Howells * Written by David Howells (dhowells@redhat.com) 554ceac45SDavid Howells * 654ceac45SDavid Howells * This program is free software; you can redistribute it and/or 754ceac45SDavid Howells * modify it under the terms of the GNU General Public License 854ceac45SDavid Howells * as published by the Free Software Foundation; either version 954ceac45SDavid Howells * 2 of the License, or (at your option) any later version. 1054ceac45SDavid Howells */ 1154ceac45SDavid Howells 1254ceac45SDavid Howells #include <linux/module.h> 1354ceac45SDavid Howells #include <linux/init.h> 1454ceac45SDavid Howells 1554ceac45SDavid Howells #include <linux/time.h> 1654ceac45SDavid Howells #include <linux/kernel.h> 1754ceac45SDavid Howells #include <linux/mm.h> 1854ceac45SDavid Howells #include <linux/string.h> 1954ceac45SDavid Howells #include <linux/stat.h> 2054ceac45SDavid Howells #include <linux/errno.h> 2154ceac45SDavid Howells #include <linux/unistd.h> 2254ceac45SDavid Howells #include <linux/sunrpc/clnt.h> 2354ceac45SDavid Howells #include <linux/sunrpc/stats.h> 2454ceac45SDavid Howells #include <linux/nfs_fs.h> 2554ceac45SDavid Howells #include <linux/nfs_mount.h> 2654ceac45SDavid Howells #include <linux/lockd/bind.h> 2754ceac45SDavid Howells #include <linux/seq_file.h> 2854ceac45SDavid Howells #include <linux/mount.h> 2954ceac45SDavid Howells #include <linux/vfs.h> 3054ceac45SDavid Howells #include <linux/namei.h> 31738a3519SDavid Howells #include <linux/security.h> 3254ceac45SDavid Howells 3354ceac45SDavid Howells #include <asm/uaccess.h> 3454ceac45SDavid Howells 354e437e95SStanislav Kinsbursky #include "internal.h" 364e437e95SStanislav Kinsbursky 3754ceac45SDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 3854ceac45SDavid Howells 3954ceac45SDavid Howells /* 40b09b9417STrond Myklebust * Set the superblock root dentry. 41b09b9417STrond Myklebust * Note that this function frees the inode in case of error. 42b09b9417STrond Myklebust */ 43b09b9417STrond Myklebust static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode) 44b09b9417STrond Myklebust { 45b09b9417STrond Myklebust /* The mntroot acts as the dummy root dentry for this superblock */ 46b09b9417STrond Myklebust if (sb->s_root == NULL) { 4748fde701SAl Viro sb->s_root = d_make_root(inode); 4848fde701SAl Viro if (sb->s_root == NULL) 49b09b9417STrond Myklebust return -ENOMEM; 507de9c6eeSAl Viro ihold(inode); 51a10db50aSTrond Myklebust /* 52a10db50aSTrond Myklebust * Ensure that this dentry is invisible to d_find_alias(). 53a10db50aSTrond Myklebust * Otherwise, it may be spliced into the tree by 54a10db50aSTrond Myklebust * d_materialise_unique if a parent directory from the same 55a10db50aSTrond Myklebust * filesystem gets mounted at a later time. 56a10db50aSTrond Myklebust * This again causes shrink_dcache_for_umount_subtree() to 57a10db50aSTrond Myklebust * Oops, since the test for IS_ROOT() will fail. 58a10db50aSTrond Myklebust */ 59873feea0SNick Piggin spin_lock(&sb->s_root->d_inode->i_lock); 60b23fb0a6SNick Piggin spin_lock(&sb->s_root->d_lock); 61b3d9b7a3SAl Viro hlist_del_init(&sb->s_root->d_alias); 62b23fb0a6SNick Piggin spin_unlock(&sb->s_root->d_lock); 63873feea0SNick Piggin spin_unlock(&sb->s_root->d_inode->i_lock); 64b09b9417STrond Myklebust } 65b09b9417STrond Myklebust return 0; 66b09b9417STrond Myklebust } 67b09b9417STrond Myklebust 68b09b9417STrond Myklebust /* 6954ceac45SDavid Howells * get an NFS2/NFS3 root dentry from the root filehandle 7054ceac45SDavid Howells */ 710d5839adSAl Viro struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh, 720d5839adSAl Viro const char *devname) 7354ceac45SDavid Howells { 7454ceac45SDavid Howells struct nfs_server *server = NFS_SB(sb); 7554ceac45SDavid Howells struct nfs_fsinfo fsinfo; 768bac9db9STrond Myklebust struct dentry *ret; 7754ceac45SDavid Howells struct inode *inode; 78b1942c5fSAl Viro void *name = kstrdup(devname, GFP_KERNEL); 7954ceac45SDavid Howells int error; 8054ceac45SDavid Howells 81b1942c5fSAl Viro if (!name) 82b1942c5fSAl Viro return ERR_PTR(-ENOMEM); 83b1942c5fSAl Viro 8454ceac45SDavid Howells /* get the actual root for this mount */ 858bac9db9STrond Myklebust fsinfo.fattr = nfs_alloc_fattr(); 86b1942c5fSAl Viro if (fsinfo.fattr == NULL) { 87b1942c5fSAl Viro kfree(name); 888bac9db9STrond Myklebust return ERR_PTR(-ENOMEM); 89b1942c5fSAl Viro } 9054ceac45SDavid Howells 9154ceac45SDavid Howells error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); 9254ceac45SDavid Howells if (error < 0) { 9354ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 948bac9db9STrond Myklebust ret = ERR_PTR(error); 958bac9db9STrond Myklebust goto out; 9654ceac45SDavid Howells } 9754ceac45SDavid Howells 9854ceac45SDavid Howells inode = nfs_fhget(sb, mntfh, fsinfo.fattr); 9954ceac45SDavid Howells if (IS_ERR(inode)) { 10054ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 1018bac9db9STrond Myklebust ret = ERR_CAST(inode); 1028bac9db9STrond Myklebust goto out; 10354ceac45SDavid Howells } 10454ceac45SDavid Howells 105b09b9417STrond Myklebust error = nfs_superblock_set_dummy_root(sb, inode); 1068bac9db9STrond Myklebust if (error != 0) { 1078bac9db9STrond Myklebust ret = ERR_PTR(error); 1088bac9db9STrond Myklebust goto out; 1098bac9db9STrond Myklebust } 110b09b9417STrond Myklebust 11154ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 11254ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 11354ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 11454ceac45SDavid Howells */ 1158bac9db9STrond Myklebust ret = d_obtain_alias(inode); 1168bac9db9STrond Myklebust if (IS_ERR(ret)) { 11754ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 1188bac9db9STrond Myklebust goto out; 11954ceac45SDavid Howells } 12054ceac45SDavid Howells 1218bac9db9STrond Myklebust security_d_instantiate(ret, inode); 122b1942c5fSAl Viro spin_lock(&ret->d_lock); 123b1942c5fSAl Viro if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) { 124b1942c5fSAl Viro ret->d_fsdata = name; 125b1942c5fSAl Viro name = NULL; 126b1942c5fSAl Viro } 127b1942c5fSAl Viro spin_unlock(&ret->d_lock); 1288bac9db9STrond Myklebust out: 129b1942c5fSAl Viro if (name) 130b1942c5fSAl Viro kfree(name); 1318bac9db9STrond Myklebust nfs_free_fattr(fsinfo.fattr); 1328bac9db9STrond Myklebust return ret; 13354ceac45SDavid Howells } 134