David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 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 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 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/nfs4_mount.h> |
| 27 | #include <linux/lockd/bind.h> |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/mount.h> |
| 30 | #include <linux/nfs_idmap.h> |
| 31 | #include <linux/vfs.h> |
| 32 | #include <linux/namei.h> |
David Howells | 738a351 | 2006-07-30 14:58:27 -0400 | [diff] [blame] | 33 | #include <linux/security.h> |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 34 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 35 | #include <asm/uaccess.h> |
| 36 | |
| 37 | #include "nfs4_fs.h" |
| 38 | #include "delegation.h" |
| 39 | #include "internal.h" |
| 40 | |
| 41 | #define NFSDBG_FACILITY NFSDBG_CLIENT |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 42 | |
| 43 | /* |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 44 | * Set the superblock root dentry. |
| 45 | * Note that this function frees the inode in case of error. |
| 46 | */ |
| 47 | static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode) |
| 48 | { |
| 49 | /* The mntroot acts as the dummy root dentry for this superblock */ |
| 50 | if (sb->s_root == NULL) { |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 51 | sb->s_root = d_make_root(inode); |
| 52 | if (sb->s_root == NULL) |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 53 | return -ENOMEM; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 54 | ihold(inode); |
Trond Myklebust | a10db50 | 2007-12-12 11:12:15 -0500 | [diff] [blame] | 55 | /* |
| 56 | * Ensure that this dentry is invisible to d_find_alias(). |
| 57 | * Otherwise, it may be spliced into the tree by |
| 58 | * d_materialise_unique if a parent directory from the same |
| 59 | * filesystem gets mounted at a later time. |
| 60 | * This again causes shrink_dcache_for_umount_subtree() to |
| 61 | * Oops, since the test for IS_ROOT() will fail. |
| 62 | */ |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 63 | spin_lock(&sb->s_root->d_inode->i_lock); |
Nick Piggin | b23fb0a | 2011-01-07 17:49:35 +1100 | [diff] [blame] | 64 | spin_lock(&sb->s_root->d_lock); |
Trond Myklebust | a10db50 | 2007-12-12 11:12:15 -0500 | [diff] [blame] | 65 | list_del_init(&sb->s_root->d_alias); |
Nick Piggin | b23fb0a | 2011-01-07 17:49:35 +1100 | [diff] [blame] | 66 | spin_unlock(&sb->s_root->d_lock); |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 67 | spin_unlock(&sb->s_root->d_inode->i_lock); |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 73 | * get an NFS2/NFS3 root dentry from the root filehandle |
| 74 | */ |
Al Viro | 0d5839a | 2011-03-16 05:27:27 -0400 | [diff] [blame] | 75 | struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh, |
| 76 | const char *devname) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 77 | { |
| 78 | struct nfs_server *server = NFS_SB(sb); |
| 79 | struct nfs_fsinfo fsinfo; |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 80 | struct dentry *ret; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 81 | struct inode *inode; |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 82 | void *name = kstrdup(devname, GFP_KERNEL); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 83 | int error; |
| 84 | |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 85 | if (!name) |
| 86 | return ERR_PTR(-ENOMEM); |
| 87 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 88 | /* get the actual root for this mount */ |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 89 | fsinfo.fattr = nfs_alloc_fattr(); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 90 | if (fsinfo.fattr == NULL) { |
| 91 | kfree(name); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 92 | return ERR_PTR(-ENOMEM); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 93 | } |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 94 | |
| 95 | error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); |
| 96 | if (error < 0) { |
| 97 | dprintk("nfs_get_root: getattr error = %d\n", -error); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 98 | ret = ERR_PTR(error); |
| 99 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | inode = nfs_fhget(sb, mntfh, fsinfo.fattr); |
| 103 | if (IS_ERR(inode)) { |
| 104 | dprintk("nfs_get_root: get root inode failed\n"); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 105 | ret = ERR_CAST(inode); |
| 106 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 109 | error = nfs_superblock_set_dummy_root(sb, inode); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 110 | if (error != 0) { |
| 111 | ret = ERR_PTR(error); |
| 112 | goto out; |
| 113 | } |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 114 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 115 | /* root dentries normally start off anonymous and get spliced in later |
| 116 | * if the dentry tree reaches them; however if the dentry already |
| 117 | * exists, we'll pick it up at this point and use it as the root |
| 118 | */ |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 119 | ret = d_obtain_alias(inode); |
| 120 | if (IS_ERR(ret)) { |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 121 | dprintk("nfs_get_root: get root dentry failed\n"); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 122 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 125 | security_d_instantiate(ret, inode); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 126 | spin_lock(&ret->d_lock); |
| 127 | if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) { |
| 128 | ret->d_fsdata = name; |
| 129 | name = NULL; |
| 130 | } |
| 131 | spin_unlock(&ret->d_lock); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 132 | out: |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 133 | if (name) |
| 134 | kfree(name); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 135 | nfs_free_fattr(fsinfo.fattr); |
| 136 | return ret; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | #ifdef CONFIG_NFS_V4 |
| 140 | |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 141 | int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 142 | { |
| 143 | struct nfs_fsinfo fsinfo; |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 144 | int ret = -ENOMEM; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 145 | |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 146 | dprintk("--> nfs4_get_rootfh()\n"); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 147 | |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 148 | fsinfo.fattr = nfs_alloc_fattr(); |
| 149 | if (fsinfo.fattr == NULL) |
| 150 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 151 | |
| 152 | /* Start by getting the root filehandle from the server */ |
| 153 | ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo); |
| 154 | if (ret < 0) { |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 155 | dprintk("nfs4_get_rootfh: getroot error = %d\n", -ret); |
| 156 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Andy Adamson | f799bdb | 2010-06-16 09:51:02 -0400 | [diff] [blame] | 159 | if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE) |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 160 | || !S_ISDIR(fsinfo.fattr->mode)) { |
| 161 | printk(KERN_ERR "nfs4_get_rootfh:" |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 162 | " getroot encountered non-directory\n"); |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 163 | ret = -ENOTDIR; |
| 164 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 167 | if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) { |
| 168 | printk(KERN_ERR "nfs4_get_rootfh:" |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 169 | " getroot obtained referral\n"); |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 170 | ret = -EREMOTE; |
| 171 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Trond Myklebust | 815409d | 2010-04-16 16:22:46 -0400 | [diff] [blame] | 174 | memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid)); |
| 175 | out: |
| 176 | nfs_free_fattr(fsinfo.fattr); |
| 177 | dprintk("<-- nfs4_get_rootfh() = %d\n", ret); |
| 178 | return ret; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * get an NFS4 root dentry from the root filehandle |
| 183 | */ |
Al Viro | 0d5839a | 2011-03-16 05:27:27 -0400 | [diff] [blame] | 184 | struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh, |
| 185 | const char *devname) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 186 | { |
| 187 | struct nfs_server *server = NFS_SB(sb); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 188 | struct nfs_fattr *fattr = NULL; |
| 189 | struct dentry *ret; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 190 | struct inode *inode; |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 191 | void *name = kstrdup(devname, GFP_KERNEL); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 192 | int error; |
| 193 | |
| 194 | dprintk("--> nfs4_get_root()\n"); |
| 195 | |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 196 | if (!name) |
| 197 | return ERR_PTR(-ENOMEM); |
| 198 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 199 | /* get the info about the server and filesystem */ |
| 200 | error = nfs4_server_capabilities(server, mntfh); |
| 201 | if (error < 0) { |
| 202 | dprintk("nfs_get_root: getcaps error = %d\n", |
| 203 | -error); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 204 | kfree(name); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 205 | return ERR_PTR(error); |
| 206 | } |
| 207 | |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 208 | fattr = nfs_alloc_fattr(); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 209 | if (fattr == NULL) { |
| 210 | kfree(name); |
| 211 | return ERR_PTR(-ENOMEM); |
| 212 | } |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 213 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 214 | /* get the actual root for this mount */ |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 215 | error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 216 | if (error < 0) { |
| 217 | dprintk("nfs_get_root: getattr error = %d\n", -error); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 218 | ret = ERR_PTR(error); |
| 219 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Gusev Vitaliy | 4667058 | 2011-03-23 00:40:25 +0300 | [diff] [blame] | 222 | if (fattr->valid & NFS_ATTR_FATTR_FSID && |
| 223 | !nfs_fsid_equal(&server->fsid, &fattr->fsid)) |
| 224 | memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); |
| 225 | |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 226 | inode = nfs_fhget(sb, mntfh, fattr); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 227 | if (IS_ERR(inode)) { |
| 228 | dprintk("nfs_get_root: get root inode failed\n"); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 229 | ret = ERR_CAST(inode); |
| 230 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 233 | error = nfs_superblock_set_dummy_root(sb, inode); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 234 | if (error != 0) { |
| 235 | ret = ERR_PTR(error); |
| 236 | goto out; |
| 237 | } |
Trond Myklebust | b09b941 | 2007-10-25 13:56:10 -0400 | [diff] [blame] | 238 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 239 | /* root dentries normally start off anonymous and get spliced in later |
| 240 | * if the dentry tree reaches them; however if the dentry already |
| 241 | * exists, we'll pick it up at this point and use it as the root |
| 242 | */ |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 243 | ret = d_obtain_alias(inode); |
| 244 | if (IS_ERR(ret)) { |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 245 | dprintk("nfs_get_root: get root dentry failed\n"); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 246 | goto out; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 249 | security_d_instantiate(ret, inode); |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 250 | spin_lock(&ret->d_lock); |
| 251 | if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) { |
| 252 | ret->d_fsdata = name; |
| 253 | name = NULL; |
| 254 | } |
| 255 | spin_unlock(&ret->d_lock); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 256 | out: |
Al Viro | b1942c5 | 2011-03-16 05:44:14 -0400 | [diff] [blame] | 257 | if (name) |
| 258 | kfree(name); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 259 | nfs_free_fattr(fattr); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 260 | dprintk("<-- nfs4_get_root()\n"); |
Trond Myklebust | 8bac9db | 2010-04-16 16:22:48 -0400 | [diff] [blame] | 261 | return ret; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | #endif /* CONFIG_NFS_V4 */ |