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"); 99e231c2eeSDavid Howells return ERR_CAST(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 */ 11044003728SChristoph Hellwig mntroot = d_obtain_alias(inode); 11144003728SChristoph Hellwig if (IS_ERR(mntroot)) { 11254ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 11344003728SChristoph Hellwig return mntroot; 11454ceac45SDavid Howells } 11554ceac45SDavid Howells 116738a3519SDavid Howells security_d_instantiate(mntroot, inode); 117738a3519SDavid Howells 11854ceac45SDavid Howells if (!mntroot->d_op) 11954ceac45SDavid Howells mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops; 12054ceac45SDavid Howells 12154ceac45SDavid Howells return mntroot; 12254ceac45SDavid Howells } 12354ceac45SDavid Howells 12454ceac45SDavid Howells #ifdef CONFIG_NFS_V4 12554ceac45SDavid Howells 12654ceac45SDavid Howells /* 12754ceac45SDavid Howells * Do a simple pathwalk from the root FH of the server to the nominated target 12854ceac45SDavid Howells * of the mountpoint 12954ceac45SDavid Howells * - give error on symlinks 13054ceac45SDavid Howells * - give error on ".." occurring in the path 13154ceac45SDavid Howells * - follow traversals 13254ceac45SDavid Howells */ 13354ceac45SDavid Howells int nfs4_path_walk(struct nfs_server *server, 13454ceac45SDavid Howells struct nfs_fh *mntfh, 13554ceac45SDavid Howells const char *path) 13654ceac45SDavid Howells { 13754ceac45SDavid Howells struct nfs_fsinfo fsinfo; 13854ceac45SDavid Howells struct nfs_fattr fattr; 13954ceac45SDavid Howells struct nfs_fh lastfh; 14054ceac45SDavid Howells struct qstr name; 14154ceac45SDavid Howells int ret; 14254ceac45SDavid Howells 14354ceac45SDavid Howells dprintk("--> nfs4_path_walk(,,%s)\n", path); 14454ceac45SDavid Howells 14554ceac45SDavid Howells fsinfo.fattr = &fattr; 14654ceac45SDavid Howells nfs_fattr_init(&fattr); 14754ceac45SDavid Howells 148faebf4e2STrond Myklebust /* Eat leading slashes */ 149faebf4e2STrond Myklebust while (*path == '/') 150faebf4e2STrond Myklebust path++; 15154ceac45SDavid Howells 15254ceac45SDavid Howells /* Start by getting the root filehandle from the server */ 15354ceac45SDavid Howells ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); 15454ceac45SDavid Howells if (ret < 0) { 15554ceac45SDavid Howells dprintk("nfs4_get_root: getroot error = %d\n", -ret); 15654ceac45SDavid Howells return ret; 15754ceac45SDavid Howells } 15854ceac45SDavid Howells 15954ceac45SDavid Howells if (fattr.type != NFDIR) { 16054ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 16154ceac45SDavid Howells " getroot encountered non-directory\n"); 16254ceac45SDavid Howells return -ENOTDIR; 16354ceac45SDavid Howells } 16454ceac45SDavid Howells 165faebf4e2STrond Myklebust /* FIXME: It is quite valid for the server to return a referral here */ 16654ceac45SDavid Howells if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) { 16754ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 16854ceac45SDavid Howells " getroot obtained referral\n"); 16954ceac45SDavid Howells return -EREMOTE; 17054ceac45SDavid Howells } 17154ceac45SDavid Howells 17254ceac45SDavid Howells next_component: 17354ceac45SDavid Howells dprintk("Next: %s\n", path); 17454ceac45SDavid Howells 17554ceac45SDavid Howells /* extract the next bit of the path */ 17654ceac45SDavid Howells if (!*path) 17754ceac45SDavid Howells goto path_walk_complete; 17854ceac45SDavid Howells 17954ceac45SDavid Howells name.name = path; 18054ceac45SDavid Howells while (*path && *path != '/') 18154ceac45SDavid Howells path++; 18254ceac45SDavid Howells name.len = path - (const char *) name.name; 18354ceac45SDavid Howells 18454af3bb5STrond Myklebust if (name.len > NFS4_MAXNAMLEN) 18554af3bb5STrond Myklebust return -ENAMETOOLONG; 18654af3bb5STrond Myklebust 18754ceac45SDavid Howells eat_dot_dir: 18854ceac45SDavid Howells while (*path == '/') 18954ceac45SDavid Howells path++; 19054ceac45SDavid Howells 19154ceac45SDavid Howells if (path[0] == '.' && (path[1] == '/' || !path[1])) { 19254ceac45SDavid Howells path += 2; 19354ceac45SDavid Howells goto eat_dot_dir; 19454ceac45SDavid Howells } 19554ceac45SDavid Howells 196faebf4e2STrond Myklebust /* FIXME: Why shouldn't the user be able to use ".." in the path? */ 19754ceac45SDavid Howells if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2]) 19854ceac45SDavid Howells ) { 19954ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 20054ceac45SDavid Howells " Mount path contains reference to \"..\"\n"); 20154ceac45SDavid Howells return -EINVAL; 20254ceac45SDavid Howells } 20354ceac45SDavid Howells 20454ceac45SDavid Howells /* lookup the next FH in the sequence */ 20554ceac45SDavid Howells memcpy(&lastfh, mntfh, sizeof(lastfh)); 20654ceac45SDavid Howells 20754ceac45SDavid Howells dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path); 20854ceac45SDavid Howells 20954ceac45SDavid Howells ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name, 21054ceac45SDavid Howells mntfh, &fattr); 21154ceac45SDavid Howells if (ret < 0) { 21254ceac45SDavid Howells dprintk("nfs4_get_root: getroot error = %d\n", -ret); 21354ceac45SDavid Howells return ret; 21454ceac45SDavid Howells } 21554ceac45SDavid Howells 21654ceac45SDavid Howells if (fattr.type != NFDIR) { 21754ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 21854ceac45SDavid Howells " lookupfh encountered non-directory\n"); 21954ceac45SDavid Howells return -ENOTDIR; 22054ceac45SDavid Howells } 22154ceac45SDavid Howells 222faebf4e2STrond Myklebust /* FIXME: Referrals are quite valid here too */ 22354ceac45SDavid Howells if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) { 22454ceac45SDavid Howells printk(KERN_ERR "nfs4_get_root:" 22554ceac45SDavid Howells " lookupfh obtained referral\n"); 22654ceac45SDavid Howells return -EREMOTE; 22754ceac45SDavid Howells } 22854ceac45SDavid Howells 22954ceac45SDavid Howells goto next_component; 23054ceac45SDavid Howells 23154ceac45SDavid Howells path_walk_complete: 23254ceac45SDavid Howells memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid)); 23354ceac45SDavid Howells dprintk("<-- nfs4_path_walk() = 0\n"); 23454ceac45SDavid Howells return 0; 23554ceac45SDavid Howells } 23654ceac45SDavid Howells 23754ceac45SDavid Howells /* 23854ceac45SDavid Howells * get an NFS4 root dentry from the root filehandle 23954ceac45SDavid Howells */ 24054ceac45SDavid Howells struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh) 24154ceac45SDavid Howells { 24254ceac45SDavid Howells struct nfs_server *server = NFS_SB(sb); 24354ceac45SDavid Howells struct nfs_fattr fattr; 24454ceac45SDavid Howells struct dentry *mntroot; 24554ceac45SDavid Howells struct inode *inode; 24654ceac45SDavid Howells int error; 24754ceac45SDavid Howells 24854ceac45SDavid Howells dprintk("--> nfs4_get_root()\n"); 24954ceac45SDavid Howells 25054ceac45SDavid Howells /* get the info about the server and filesystem */ 25154ceac45SDavid Howells error = nfs4_server_capabilities(server, mntfh); 25254ceac45SDavid Howells if (error < 0) { 25354ceac45SDavid Howells dprintk("nfs_get_root: getcaps error = %d\n", 25454ceac45SDavid Howells -error); 25554ceac45SDavid Howells return ERR_PTR(error); 25654ceac45SDavid Howells } 25754ceac45SDavid Howells 25854ceac45SDavid Howells /* get the actual root for this mount */ 25954ceac45SDavid Howells error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr); 26054ceac45SDavid Howells if (error < 0) { 26154ceac45SDavid Howells dprintk("nfs_get_root: getattr error = %d\n", -error); 26254ceac45SDavid Howells return ERR_PTR(error); 26354ceac45SDavid Howells } 26454ceac45SDavid Howells 26554ceac45SDavid Howells inode = nfs_fhget(sb, mntfh, &fattr); 26654ceac45SDavid Howells if (IS_ERR(inode)) { 26754ceac45SDavid Howells dprintk("nfs_get_root: get root inode failed\n"); 268e231c2eeSDavid Howells return ERR_CAST(inode); 26954ceac45SDavid Howells } 27054ceac45SDavid Howells 271b09b9417STrond Myklebust error = nfs_superblock_set_dummy_root(sb, inode); 272b09b9417STrond Myklebust if (error != 0) 273b09b9417STrond Myklebust return ERR_PTR(error); 274b09b9417STrond Myklebust 27554ceac45SDavid Howells /* root dentries normally start off anonymous and get spliced in later 27654ceac45SDavid Howells * if the dentry tree reaches them; however if the dentry already 27754ceac45SDavid Howells * exists, we'll pick it up at this point and use it as the root 27854ceac45SDavid Howells */ 27944003728SChristoph Hellwig mntroot = d_obtain_alias(inode); 28044003728SChristoph Hellwig if (IS_ERR(mntroot)) { 28154ceac45SDavid Howells dprintk("nfs_get_root: get root dentry failed\n"); 28244003728SChristoph Hellwig return mntroot; 28354ceac45SDavid Howells } 28454ceac45SDavid Howells 285738a3519SDavid Howells security_d_instantiate(mntroot, inode); 286738a3519SDavid Howells 28754ceac45SDavid Howells if (!mntroot->d_op) 28854ceac45SDavid Howells mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops; 28954ceac45SDavid Howells 29054ceac45SDavid Howells dprintk("<-- nfs4_get_root()\n"); 29154ceac45SDavid Howells return mntroot; 29254ceac45SDavid Howells } 29354ceac45SDavid Howells 29454ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */ 295