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/nfs4_mount.h> 2754ceac45SDavid Howells #include <linux/lockd/bind.h> 2854ceac45SDavid Howells #include <linux/seq_file.h> 2954ceac45SDavid Howells #include <linux/mount.h> 3054ceac45SDavid Howells #include <linux/nfs_idmap.h> 3154ceac45SDavid Howells #include <linux/vfs.h> 3254ceac45SDavid Howells #include <linux/namei.h> 336b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 34738a3519SDavid Howells #include <linux/security.h> 3554ceac45SDavid Howells 3654ceac45SDavid Howells #include <asm/system.h> 3754ceac45SDavid Howells #include <asm/uaccess.h> 3854ceac45SDavid Howells 3954ceac45SDavid Howells #include "nfs4_fs.h" 4054ceac45SDavid Howells #include "delegation.h" 4154ceac45SDavid Howells #include "internal.h" 4254ceac45SDavid Howells 4354ceac45SDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 4454ceac45SDavid Howells 4554ceac45SDavid Howells /* 46b09b9417STrond Myklebust * Set the superblock root dentry. 47b09b9417STrond Myklebust * Note that this function frees the inode in case of error. 48b09b9417STrond Myklebust */ 49b09b9417STrond Myklebust static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode) 50b09b9417STrond Myklebust { 51b09b9417STrond Myklebust /* The mntroot acts as the dummy root dentry for this superblock */ 52b09b9417STrond Myklebust if (sb->s_root == NULL) { 53b09b9417STrond Myklebust sb->s_root = d_alloc_root(inode); 54b09b9417STrond Myklebust if (sb->s_root == NULL) { 55b09b9417STrond Myklebust iput(inode); 56b09b9417STrond Myklebust return -ENOMEM; 57b09b9417STrond Myklebust } 58b09b9417STrond Myklebust /* Circumvent igrab(): we know the inode is not being freed */ 59b09b9417STrond Myklebust atomic_inc(&inode->i_count); 60a10db50aSTrond Myklebust /* 61a10db50aSTrond Myklebust * Ensure that this dentry is invisible to d_find_alias(). 62a10db50aSTrond Myklebust * Otherwise, it may be spliced into the tree by 63a10db50aSTrond Myklebust * d_materialise_unique if a parent directory from the same 64a10db50aSTrond Myklebust * filesystem gets mounted at a later time. 65a10db50aSTrond Myklebust * This again causes shrink_dcache_for_umount_subtree() to 66a10db50aSTrond Myklebust * Oops, since the test for IS_ROOT() will fail. 67a10db50aSTrond Myklebust */ 68a10db50aSTrond Myklebust spin_lock(&dcache_lock); 69a10db50aSTrond Myklebust list_del_init(&sb->s_root->d_alias); 70a10db50aSTrond Myklebust spin_unlock(&dcache_lock); 71b09b9417STrond Myklebust } 72b09b9417STrond Myklebust return 0; 73b09b9417STrond Myklebust } 74b09b9417STrond Myklebust 75b09b9417STrond Myklebust /* 7654ceac45SDavid Howells * get an NFS2/NFS3 root dentry from the root filehandle 7754ceac45SDavid Howells */ 7854ceac45SDavid Howells struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh) 7954ceac45SDavid Howells { 8054ceac45SDavid Howells struct nfs_server *server = NFS_SB(sb); 8154ceac45SDavid Howells struct nfs_fsinfo fsinfo; 8254ceac45SDavid Howells struct nfs_fattr fattr; 8354ceac45SDavid Howells struct dentry *mntroot; 8454ceac45SDavid Howells struct inode *inode; 8554ceac45SDavid Howells int error; 8654ceac45SDavid Howells 8754ceac45SDavid Howells /* get the actual root for this mount */ 8854ceac45SDavid Howells fsinfo.fattr = &fattr; 8954ceac45SDavid Howells 9054ceac45SDavid Howells error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); 9154ceac45SDavid Howells if (error < 0) { 9254ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 9354ceac45SDavid Howells return ERR_PTR(error); 9454ceac45SDavid Howells } 9554ceac45SDavid Howells 9654ceac45SDavid Howells inode = nfs_fhget(sb, mntfh, fsinfo.fattr); 9754ceac45SDavid Howells if (IS_ERR(inode)) { 9854ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 9954ceac45SDavid Howells return ERR_PTR(PTR_ERR(inode)); 10054ceac45SDavid Howells } 10154ceac45SDavid Howells 102b09b9417STrond Myklebust error = nfs_superblock_set_dummy_root(sb, inode); 103b09b9417STrond Myklebust if (error != 0) 104b09b9417STrond Myklebust return ERR_PTR(error); 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 */ 11054ceac45SDavid Howells mntroot = d_alloc_anon(inode); 11154ceac45SDavid Howells if (!mntroot) { 11254ceac45SDavid Howells iput(inode); 11354ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 11454ceac45SDavid Howells return ERR_PTR(-ENOMEM); 11554ceac45SDavid Howells } 11654ceac45SDavid Howells 117738a3519SDavid Howells security_d_instantiate(mntroot, inode); 118738a3519SDavid Howells 11954ceac45SDavid Howells if (!mntroot->d_op) 12054ceac45SDavid Howells mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops; 12154ceac45SDavid Howells 12254ceac45SDavid Howells return mntroot; 12354ceac45SDavid Howells } 12454ceac45SDavid Howells 12554ceac45SDavid Howells #ifdef CONFIG_NFS_V4 12654ceac45SDavid Howells 12754ceac45SDavid Howells /* 12854ceac45SDavid Howells * Do a simple pathwalk from the root FH of the server to the nominated target 12954ceac45SDavid Howells * of the mountpoint 13054ceac45SDavid Howells * - give error on symlinks 13154ceac45SDavid Howells * - give error on ".." occurring in the path 13254ceac45SDavid Howells * - follow traversals 13354ceac45SDavid Howells */ 13454ceac45SDavid Howells int nfs4_path_walk(struct nfs_server *server, 13554ceac45SDavid Howells struct nfs_fh *mntfh, 13654ceac45SDavid Howells const char *path) 13754ceac45SDavid Howells { 13854ceac45SDavid Howells struct nfs_fsinfo fsinfo; 13954ceac45SDavid Howells struct nfs_fattr fattr; 14054ceac45SDavid Howells struct nfs_fh lastfh; 14154ceac45SDavid Howells struct qstr name; 14254ceac45SDavid Howells int ret; 14354ceac45SDavid Howells 14454ceac45SDavid Howells dprintk("--> nfs4_path_walk(,,%s)\n", path); 14554ceac45SDavid Howells 14654ceac45SDavid Howells fsinfo.fattr = &fattr; 14754ceac45SDavid Howells nfs_fattr_init(&fattr); 14854ceac45SDavid Howells 149faebf4e2STrond Myklebust /* Eat leading slashes */ 150faebf4e2STrond Myklebust while (*path == '/') 151faebf4e2STrond Myklebust path++; 15254ceac45SDavid Howells 15354ceac45SDavid Howells /* Start by getting the root filehandle from the server */ 15454ceac45SDavid Howells ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); 15554ceac45SDavid Howells if (ret < 0) { 15654ceac45SDavid Howells dprintk("nfs4_get_root: getroot error = %d\n", -ret); 15754ceac45SDavid Howells return ret; 15854ceac45SDavid Howells } 15954ceac45SDavid Howells 16054ceac45SDavid Howells if (fattr.type != NFDIR) { 16154ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 16254ceac45SDavid Howells " getroot encountered non-directory\n"); 16354ceac45SDavid Howells return -ENOTDIR; 16454ceac45SDavid Howells } 16554ceac45SDavid Howells 166faebf4e2STrond Myklebust /* FIXME: It is quite valid for the server to return a referral here */ 16754ceac45SDavid Howells if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) { 16854ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 16954ceac45SDavid Howells " getroot obtained referral\n"); 17054ceac45SDavid Howells return -EREMOTE; 17154ceac45SDavid Howells } 17254ceac45SDavid Howells 17354ceac45SDavid Howells next_component: 17454ceac45SDavid Howells dprintk("Next: %s\n", path); 17554ceac45SDavid Howells 17654ceac45SDavid Howells /* extract the next bit of the path */ 17754ceac45SDavid Howells if (!*path) 17854ceac45SDavid Howells goto path_walk_complete; 17954ceac45SDavid Howells 18054ceac45SDavid Howells name.name = path; 18154ceac45SDavid Howells while (*path && *path != '/') 18254ceac45SDavid Howells path++; 18354ceac45SDavid Howells name.len = path - (const char *) name.name; 18454ceac45SDavid Howells 18554af3bb5STrond Myklebust if (name.len > NFS4_MAXNAMLEN) 18654af3bb5STrond Myklebust return -ENAMETOOLONG; 18754af3bb5STrond Myklebust 18854ceac45SDavid Howells eat_dot_dir: 18954ceac45SDavid Howells while (*path == '/') 19054ceac45SDavid Howells path++; 19154ceac45SDavid Howells 19254ceac45SDavid Howells if (path[0] == '.' && (path[1] == '/' || !path[1])) { 19354ceac45SDavid Howells path += 2; 19454ceac45SDavid Howells goto eat_dot_dir; 19554ceac45SDavid Howells } 19654ceac45SDavid Howells 197faebf4e2STrond Myklebust /* FIXME: Why shouldn't the user be able to use ".." in the path? */ 19854ceac45SDavid Howells if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2]) 19954ceac45SDavid Howells ) { 20054ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 20154ceac45SDavid Howells " Mount path contains reference to \"..\"\n"); 20254ceac45SDavid Howells return -EINVAL; 20354ceac45SDavid Howells } 20454ceac45SDavid Howells 20554ceac45SDavid Howells /* lookup the next FH in the sequence */ 20654ceac45SDavid Howells memcpy(&lastfh, mntfh, sizeof(lastfh)); 20754ceac45SDavid Howells 20854ceac45SDavid Howells dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path); 20954ceac45SDavid Howells 21054ceac45SDavid Howells ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name, 21154ceac45SDavid Howells mntfh, &fattr); 21254ceac45SDavid Howells if (ret < 0) { 21354ceac45SDavid Howells dprintk("nfs4_get_root: getroot error = %d\n", -ret); 21454ceac45SDavid Howells return ret; 21554ceac45SDavid Howells } 21654ceac45SDavid Howells 21754ceac45SDavid Howells if (fattr.type != NFDIR) { 21854ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 21954ceac45SDavid Howells " lookupfh encountered non-directory\n"); 22054ceac45SDavid Howells return -ENOTDIR; 22154ceac45SDavid Howells } 22254ceac45SDavid Howells 223faebf4e2STrond Myklebust /* FIXME: Referrals are quite valid here too */ 22454ceac45SDavid Howells if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) { 22554ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 22654ceac45SDavid Howells " lookupfh obtained referral\n"); 22754ceac45SDavid Howells return -EREMOTE; 22854ceac45SDavid Howells } 22954ceac45SDavid Howells 23054ceac45SDavid Howells goto next_component; 23154ceac45SDavid Howells 23254ceac45SDavid Howells path_walk_complete: 23354ceac45SDavid Howells memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid)); 23454ceac45SDavid Howells dprintk("<-- nfs4_path_walk() = 0\n"); 23554ceac45SDavid Howells return 0; 23654ceac45SDavid Howells } 23754ceac45SDavid Howells 23854ceac45SDavid Howells /* 23954ceac45SDavid Howells * get an NFS4 root dentry from the root filehandle 24054ceac45SDavid Howells */ 24154ceac45SDavid Howells struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh) 24254ceac45SDavid Howells { 24354ceac45SDavid Howells struct nfs_server *server = NFS_SB(sb); 24454ceac45SDavid Howells struct nfs_fattr fattr; 24554ceac45SDavid Howells struct dentry *mntroot; 24654ceac45SDavid Howells struct inode *inode; 24754ceac45SDavid Howells int error; 24854ceac45SDavid Howells 24954ceac45SDavid Howells dprintk("--> nfs4_get_root()\n"); 25054ceac45SDavid Howells 25154ceac45SDavid Howells /* get the info about the server and filesystem */ 25254ceac45SDavid Howells error = nfs4_server_capabilities(server, mntfh); 25354ceac45SDavid Howells if (error < 0) { 25454ceac45SDavid Howells dprintk("nfs_get_root: getcaps error = %d\n", 25554ceac45SDavid Howells -error); 25654ceac45SDavid Howells return ERR_PTR(error); 25754ceac45SDavid Howells } 25854ceac45SDavid Howells 25954ceac45SDavid Howells /* get the actual root for this mount */ 26054ceac45SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr); 26154ceac45SDavid Howells if (error < 0) { 26254ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 26354ceac45SDavid Howells return ERR_PTR(error); 26454ceac45SDavid Howells } 26554ceac45SDavid Howells 26654ceac45SDavid Howells inode = nfs_fhget(sb, mntfh, &fattr); 26754ceac45SDavid Howells if (IS_ERR(inode)) { 26854ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 26954ceac45SDavid Howells return ERR_PTR(PTR_ERR(inode)); 27054ceac45SDavid Howells } 27154ceac45SDavid Howells 272b09b9417STrond Myklebust error = nfs_superblock_set_dummy_root(sb, inode); 273b09b9417STrond Myklebust if (error != 0) 274b09b9417STrond Myklebust return ERR_PTR(error); 275b09b9417STrond Myklebust 27654ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 27754ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 27854ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 27954ceac45SDavid Howells */ 28054ceac45SDavid Howells mntroot = d_alloc_anon(inode); 28154ceac45SDavid Howells if (!mntroot) { 28254ceac45SDavid Howells iput(inode); 28354ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 28454ceac45SDavid Howells return ERR_PTR(-ENOMEM); 28554ceac45SDavid Howells } 28654ceac45SDavid Howells 287738a3519SDavid Howells security_d_instantiate(mntroot, inode); 288738a3519SDavid Howells 28954ceac45SDavid Howells if (!mntroot->d_op) 29054ceac45SDavid Howells mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops; 29154ceac45SDavid Howells 29254ceac45SDavid Howells dprintk("<-- nfs4_get_root()\n"); 29354ceac45SDavid Howells return mntroot; 29454ceac45SDavid Howells } 29554ceac45SDavid Howells 29654ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */ 297