xref: /openbmc/linux/fs/nfs/getroot.c (revision b09b9417)
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);
60b09b9417STrond Myklebust 	}
61b09b9417STrond Myklebust 	return 0;
62b09b9417STrond Myklebust }
63b09b9417STrond Myklebust 
64b09b9417STrond Myklebust /*
6554ceac45SDavid Howells  * get an NFS2/NFS3 root dentry from the root filehandle
6654ceac45SDavid Howells  */
6754ceac45SDavid Howells struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
6854ceac45SDavid Howells {
6954ceac45SDavid Howells 	struct nfs_server *server = NFS_SB(sb);
7054ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
7154ceac45SDavid Howells 	struct nfs_fattr fattr;
7254ceac45SDavid Howells 	struct dentry *mntroot;
7354ceac45SDavid Howells 	struct inode *inode;
7454ceac45SDavid Howells 	int error;
7554ceac45SDavid Howells 
7654ceac45SDavid Howells 	/* get the actual root for this mount */
7754ceac45SDavid Howells 	fsinfo.fattr = &fattr;
7854ceac45SDavid Howells 
7954ceac45SDavid Howells 	error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
8054ceac45SDavid Howells 	if (error < 0) {
8154ceac45SDavid Howells 		dprintk("nfs_get_root: getattr error = %d\n", -error);
8254ceac45SDavid Howells 		return ERR_PTR(error);
8354ceac45SDavid Howells 	}
8454ceac45SDavid Howells 
8554ceac45SDavid Howells 	inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
8654ceac45SDavid Howells 	if (IS_ERR(inode)) {
8754ceac45SDavid Howells 		dprintk("nfs_get_root: get root inode failed\n");
8854ceac45SDavid Howells 		return ERR_PTR(PTR_ERR(inode));
8954ceac45SDavid Howells 	}
9054ceac45SDavid Howells 
91b09b9417STrond Myklebust 	error = nfs_superblock_set_dummy_root(sb, inode);
92b09b9417STrond Myklebust 	if (error != 0)
93b09b9417STrond Myklebust 		return ERR_PTR(error);
94b09b9417STrond Myklebust 
9554ceac45SDavid Howells 	/* root dentries normally start off anonymous and get spliced in later
9654ceac45SDavid Howells 	 * if the dentry tree reaches them; however if the dentry already
9754ceac45SDavid Howells 	 * exists, we'll pick it up at this point and use it as the root
9854ceac45SDavid Howells 	 */
9954ceac45SDavid Howells 	mntroot = d_alloc_anon(inode);
10054ceac45SDavid Howells 	if (!mntroot) {
10154ceac45SDavid Howells 		iput(inode);
10254ceac45SDavid Howells 		dprintk("nfs_get_root: get root dentry failed\n");
10354ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
10454ceac45SDavid Howells 	}
10554ceac45SDavid Howells 
106738a3519SDavid Howells 	security_d_instantiate(mntroot, inode);
107738a3519SDavid Howells 
10854ceac45SDavid Howells 	if (!mntroot->d_op)
10954ceac45SDavid Howells 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
11054ceac45SDavid Howells 
11154ceac45SDavid Howells 	return mntroot;
11254ceac45SDavid Howells }
11354ceac45SDavid Howells 
11454ceac45SDavid Howells #ifdef CONFIG_NFS_V4
11554ceac45SDavid Howells 
11654ceac45SDavid Howells /*
11754ceac45SDavid Howells  * Do a simple pathwalk from the root FH of the server to the nominated target
11854ceac45SDavid Howells  * of the mountpoint
11954ceac45SDavid Howells  * - give error on symlinks
12054ceac45SDavid Howells  * - give error on ".." occurring in the path
12154ceac45SDavid Howells  * - follow traversals
12254ceac45SDavid Howells  */
12354ceac45SDavid Howells int nfs4_path_walk(struct nfs_server *server,
12454ceac45SDavid Howells 		   struct nfs_fh *mntfh,
12554ceac45SDavid Howells 		   const char *path)
12654ceac45SDavid Howells {
12754ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
12854ceac45SDavid Howells 	struct nfs_fattr fattr;
12954ceac45SDavid Howells 	struct nfs_fh lastfh;
13054ceac45SDavid Howells 	struct qstr name;
13154ceac45SDavid Howells 	int ret;
13254ceac45SDavid Howells 
13354ceac45SDavid Howells 	dprintk("--> nfs4_path_walk(,,%s)\n", path);
13454ceac45SDavid Howells 
13554ceac45SDavid Howells 	fsinfo.fattr = &fattr;
13654ceac45SDavid Howells 	nfs_fattr_init(&fattr);
13754ceac45SDavid Howells 
138faebf4e2STrond Myklebust 	/* Eat leading slashes */
139faebf4e2STrond Myklebust 	while (*path == '/')
140faebf4e2STrond Myklebust 		path++;
14154ceac45SDavid Howells 
14254ceac45SDavid Howells 	/* Start by getting the root filehandle from the server */
14354ceac45SDavid Howells 	ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
14454ceac45SDavid Howells 	if (ret < 0) {
14554ceac45SDavid Howells 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
14654ceac45SDavid Howells 		return ret;
14754ceac45SDavid Howells 	}
14854ceac45SDavid Howells 
14954ceac45SDavid Howells 	if (fattr.type != NFDIR) {
15054ceac45SDavid Howells 		printk(KERN_ERR "nfs4_get_root:"
15154ceac45SDavid Howells 		       " getroot encountered non-directory\n");
15254ceac45SDavid Howells 		return -ENOTDIR;
15354ceac45SDavid Howells 	}
15454ceac45SDavid Howells 
155faebf4e2STrond Myklebust 	/* FIXME: It is quite valid for the server to return a referral here */
15654ceac45SDavid Howells 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
15754ceac45SDavid Howells 		printk(KERN_ERR "nfs4_get_root:"
15854ceac45SDavid Howells 		       " getroot obtained referral\n");
15954ceac45SDavid Howells 		return -EREMOTE;
16054ceac45SDavid Howells 	}
16154ceac45SDavid Howells 
16254ceac45SDavid Howells next_component:
16354ceac45SDavid Howells 	dprintk("Next: %s\n", path);
16454ceac45SDavid Howells 
16554ceac45SDavid Howells 	/* extract the next bit of the path */
16654ceac45SDavid Howells 	if (!*path)
16754ceac45SDavid Howells 		goto path_walk_complete;
16854ceac45SDavid Howells 
16954ceac45SDavid Howells 	name.name = path;
17054ceac45SDavid Howells 	while (*path && *path != '/')
17154ceac45SDavid Howells 		path++;
17254ceac45SDavid Howells 	name.len = path - (const char *) name.name;
17354ceac45SDavid Howells 
17454af3bb5STrond Myklebust 	if (name.len > NFS4_MAXNAMLEN)
17554af3bb5STrond Myklebust 		return -ENAMETOOLONG;
17654af3bb5STrond Myklebust 
17754ceac45SDavid Howells eat_dot_dir:
17854ceac45SDavid Howells 	while (*path == '/')
17954ceac45SDavid Howells 		path++;
18054ceac45SDavid Howells 
18154ceac45SDavid Howells 	if (path[0] == '.' && (path[1] == '/' || !path[1])) {
18254ceac45SDavid Howells 		path += 2;
18354ceac45SDavid Howells 		goto eat_dot_dir;
18454ceac45SDavid Howells 	}
18554ceac45SDavid Howells 
186faebf4e2STrond Myklebust 	/* FIXME: Why shouldn't the user be able to use ".." in the path? */
18754ceac45SDavid Howells 	if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
18854ceac45SDavid Howells 	    ) {
18954ceac45SDavid Howells 		printk(KERN_ERR "nfs4_get_root:"
19054ceac45SDavid Howells 		       " Mount path contains reference to \"..\"\n");
19154ceac45SDavid Howells 		return -EINVAL;
19254ceac45SDavid Howells 	}
19354ceac45SDavid Howells 
19454ceac45SDavid Howells 	/* lookup the next FH in the sequence */
19554ceac45SDavid Howells 	memcpy(&lastfh, mntfh, sizeof(lastfh));
19654ceac45SDavid Howells 
19754ceac45SDavid Howells 	dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
19854ceac45SDavid Howells 
19954ceac45SDavid Howells 	ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
20054ceac45SDavid Howells 						    mntfh, &fattr);
20154ceac45SDavid Howells 	if (ret < 0) {
20254ceac45SDavid Howells 		dprintk("nfs4_get_root: getroot error = %d\n", -ret);
20354ceac45SDavid Howells 		return ret;
20454ceac45SDavid Howells 	}
20554ceac45SDavid Howells 
20654ceac45SDavid Howells 	if (fattr.type != NFDIR) {
20754ceac45SDavid Howells 		printk(KERN_ERR "nfs4_get_root:"
20854ceac45SDavid Howells 		       " lookupfh encountered non-directory\n");
20954ceac45SDavid Howells 		return -ENOTDIR;
21054ceac45SDavid Howells 	}
21154ceac45SDavid Howells 
212faebf4e2STrond Myklebust 	/* FIXME: Referrals are quite valid here too */
21354ceac45SDavid Howells 	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
21454ceac45SDavid Howells 		printk(KERN_ERR "nfs4_get_root:"
21554ceac45SDavid Howells 		       " lookupfh obtained referral\n");
21654ceac45SDavid Howells 		return -EREMOTE;
21754ceac45SDavid Howells 	}
21854ceac45SDavid Howells 
21954ceac45SDavid Howells 	goto next_component;
22054ceac45SDavid Howells 
22154ceac45SDavid Howells path_walk_complete:
22254ceac45SDavid Howells 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
22354ceac45SDavid Howells 	dprintk("<-- nfs4_path_walk() = 0\n");
22454ceac45SDavid Howells 	return 0;
22554ceac45SDavid Howells }
22654ceac45SDavid Howells 
22754ceac45SDavid Howells /*
22854ceac45SDavid Howells  * get an NFS4 root dentry from the root filehandle
22954ceac45SDavid Howells  */
23054ceac45SDavid Howells struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
23154ceac45SDavid Howells {
23254ceac45SDavid Howells 	struct nfs_server *server = NFS_SB(sb);
23354ceac45SDavid Howells 	struct nfs_fattr fattr;
23454ceac45SDavid Howells 	struct dentry *mntroot;
23554ceac45SDavid Howells 	struct inode *inode;
23654ceac45SDavid Howells 	int error;
23754ceac45SDavid Howells 
23854ceac45SDavid Howells 	dprintk("--> nfs4_get_root()\n");
23954ceac45SDavid Howells 
24054ceac45SDavid Howells 	/* get the info about the server and filesystem */
24154ceac45SDavid Howells 	error = nfs4_server_capabilities(server, mntfh);
24254ceac45SDavid Howells 	if (error < 0) {
24354ceac45SDavid Howells 		dprintk("nfs_get_root: getcaps error = %d\n",
24454ceac45SDavid Howells 			-error);
24554ceac45SDavid Howells 		return ERR_PTR(error);
24654ceac45SDavid Howells 	}
24754ceac45SDavid Howells 
24854ceac45SDavid Howells 	/* get the actual root for this mount */
24954ceac45SDavid Howells 	error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
25054ceac45SDavid Howells 	if (error < 0) {
25154ceac45SDavid Howells 		dprintk("nfs_get_root: getattr error = %d\n", -error);
25254ceac45SDavid Howells 		return ERR_PTR(error);
25354ceac45SDavid Howells 	}
25454ceac45SDavid Howells 
25554ceac45SDavid Howells 	inode = nfs_fhget(sb, mntfh, &fattr);
25654ceac45SDavid Howells 	if (IS_ERR(inode)) {
25754ceac45SDavid Howells 		dprintk("nfs_get_root: get root inode failed\n");
25854ceac45SDavid Howells 		return ERR_PTR(PTR_ERR(inode));
25954ceac45SDavid Howells 	}
26054ceac45SDavid Howells 
261b09b9417STrond Myklebust 	error = nfs_superblock_set_dummy_root(sb, inode);
262b09b9417STrond Myklebust 	if (error != 0)
263b09b9417STrond Myklebust 		return ERR_PTR(error);
264b09b9417STrond Myklebust 
26554ceac45SDavid Howells 	/* root dentries normally start off anonymous and get spliced in later
26654ceac45SDavid Howells 	 * if the dentry tree reaches them; however if the dentry already
26754ceac45SDavid Howells 	 * exists, we'll pick it up at this point and use it as the root
26854ceac45SDavid Howells 	 */
26954ceac45SDavid Howells 	mntroot = d_alloc_anon(inode);
27054ceac45SDavid Howells 	if (!mntroot) {
27154ceac45SDavid Howells 		iput(inode);
27254ceac45SDavid Howells 		dprintk("nfs_get_root: get root dentry failed\n");
27354ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
27454ceac45SDavid Howells 	}
27554ceac45SDavid Howells 
276738a3519SDavid Howells 	security_d_instantiate(mntroot, inode);
277738a3519SDavid Howells 
27854ceac45SDavid Howells 	if (!mntroot->d_op)
27954ceac45SDavid Howells 		mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
28054ceac45SDavid Howells 
28154ceac45SDavid Howells 	dprintk("<-- nfs4_get_root()\n");
28254ceac45SDavid Howells 	return mntroot;
28354ceac45SDavid Howells }
28454ceac45SDavid Howells 
28554ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */
286