namespace.c 9.3 KB
Newer Older
1 2 3 4
/*
 * linux/fs/nfs/namespace.c
 *
 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5
 * - Modified by David Howells <dhowells@redhat.com>
6 7 8 9 10
 *
 * NFS namespace
 */

#include <linux/dcache.h>
11
#include <linux/gfp.h>
12 13 14 15 16 17
#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/nfs_fs.h>
#include <linux/string.h>
#include <linux/sunrpc/clnt.h>
#include <linux/vfs.h>
18
#include <linux/sunrpc/gss_api.h>
D
David Howells 已提交
19
#include "internal.h"
20 21 22

#define NFSDBG_FACILITY		NFSDBG_VFS

23
static void nfs_expire_automounts(struct work_struct *work);
D
David Howells 已提交
24

A
Adrian Bunk 已提交
25
static LIST_HEAD(nfs_automount_list);
26
static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
T
Trond Myklebust 已提交
27 28
int nfs_mountpoint_expiry_timeout = 500 * HZ;

29
static struct vfsmount *nfs_do_submount(struct dentry *dentry,
A
Adrian Bunk 已提交
30
					struct nfs_fh *fh,
31 32
					struct nfs_fattr *fattr,
					rpc_authflavor_t authflavor);
A
Adrian Bunk 已提交
33

D
David Howells 已提交
34 35
/*
 * nfs_path - reconstruct the path given an arbitrary dentry
36
 * @base - used to return pointer to the end of devname part of path
D
David Howells 已提交
37 38 39 40
 * @dentry - pointer to dentry
 * @buffer - result buffer
 * @buflen - length of buffer
 *
41 42
 * Helper function for constructing the server pathname
 * by arbitrary hashed dentry.
D
David Howells 已提交
43 44
 *
 * This is mainly for use in figuring out the path on the
45 46
 * server side when automounting on top of an existing partition
 * and in generating /proc/mounts and friends.
D
David Howells 已提交
47
 */
48
char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
D
David Howells 已提交
49
{
50
	char *end;
D
David Howells 已提交
51
	int namelen;
52
	unsigned seq;
53
	const char *base;
D
David Howells 已提交
54

55 56
rename_retry:
	end = buffer+buflen;
D
David Howells 已提交
57 58
	*--end = '\0';
	buflen--;
59 60 61

	seq = read_seqbegin(&rename_lock);
	rcu_read_lock();
62 63 64 65
	while (1) {
		spin_lock(&dentry->d_lock);
		if (IS_ROOT(dentry))
			break;
D
David Howells 已提交
66 67 68
		namelen = dentry->d_name.len;
		buflen -= namelen + 1;
		if (buflen < 0)
69
			goto Elong_unlock;
D
David Howells 已提交
70 71 72
		end -= namelen;
		memcpy(end, dentry->d_name.name, namelen);
		*--end = '/';
73
		spin_unlock(&dentry->d_lock);
D
David Howells 已提交
74 75
		dentry = dentry->d_parent;
	}
76 77 78
	if (read_seqretry(&rename_lock, seq)) {
		spin_unlock(&dentry->d_lock);
		rcu_read_unlock();
79
		goto rename_retry;
80
	}
81
	if (*end != '/') {
82 83 84
		if (--buflen < 0) {
			spin_unlock(&dentry->d_lock);
			rcu_read_unlock();
85
			goto Elong;
86
		}
87 88
		*--end = '/';
	}
89 90 91 92 93 94 95 96
	*p = end;
	base = dentry->d_fsdata;
	if (!base) {
		spin_unlock(&dentry->d_lock);
		rcu_read_unlock();
		WARN_ON(1);
		return end;
	}
D
David Howells 已提交
97 98 99 100 101
	namelen = strlen(base);
	/* Strip off excess slashes in base string */
	while (namelen > 0 && base[namelen - 1] == '/')
		namelen--;
	buflen -= namelen;
102
	if (buflen < 0) {
D
Dan Carpenter 已提交
103
		spin_unlock(&dentry->d_lock);
104
		rcu_read_unlock();
D
David Howells 已提交
105
		goto Elong;
106
	}
D
David Howells 已提交
107 108
	end -= namelen;
	memcpy(end, base, namelen);
109 110
	spin_unlock(&dentry->d_lock);
	rcu_read_unlock();
D
David Howells 已提交
111
	return end;
112
Elong_unlock:
D
Dan Carpenter 已提交
113
	spin_unlock(&dentry->d_lock);
114 115 116
	rcu_read_unlock();
	if (read_seqretry(&rename_lock, seq))
		goto rename_retry;
D
David Howells 已提交
117 118 119 120
Elong:
	return ERR_PTR(-ENAMETOOLONG);
}

121
#ifdef CONFIG_NFS_V4
122
static rpc_authflavor_t nfs_find_best_sec(struct nfs4_secinfo_flavors *flavors)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
	struct gss_api_mech *mech;
	struct xdr_netobj oid;
	int i;
	rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;

	for (i = 0; i < flavors->num_flavors; i++) {
		struct nfs4_secinfo_flavor *flavor;
		flavor = &flavors->flavors[i];

		if (flavor->flavor == RPC_AUTH_NULL || flavor->flavor == RPC_AUTH_UNIX) {
			pseudoflavor = flavor->flavor;
			break;
		} else if (flavor->flavor == RPC_AUTH_GSS) {
			oid.len  = flavor->gss.sec_oid4.len;
			oid.data = flavor->gss.sec_oid4.data;
			mech = gss_mech_get_by_OID(&oid);
			if (!mech)
				continue;
			pseudoflavor = gss_svc_to_pseudoflavor(mech, flavor->gss.service);
			gss_mech_put(mech);
			break;
		}
	}

	return pseudoflavor;
}

151 152 153
static int nfs_negotiate_security(const struct dentry *parent,
				  const struct dentry *dentry,
				  rpc_authflavor_t *flavor)
154 155 156 157
{
	struct page *page;
	struct nfs4_secinfo_flavors *flavors;
	int (*secinfo)(struct inode *, const struct qstr *, struct nfs4_secinfo_flavors *);
158
	int ret = -EPERM;
159 160 161 162 163

	secinfo = NFS_PROTO(parent->d_inode)->secinfo;
	if (secinfo != NULL) {
		page = alloc_page(GFP_KERNEL);
		if (!page) {
164
			ret = -ENOMEM;
165 166 167
			goto out;
		}
		flavors = page_address(page);
168
		ret = secinfo(parent->d_inode, &dentry->d_name, flavors);
169
		*flavor = nfs_find_best_sec(flavors);
170 171 172 173
		put_page(page);
	}

out:
174
	return ret;
175 176
}

177 178 179 180
static int nfs_lookup_with_sec(struct nfs_server *server, struct dentry *parent,
			       struct dentry *dentry, struct path *path,
			       struct nfs_fh *fh, struct nfs_fattr *fattr,
			       rpc_authflavor_t *flavor)
181 182 183 184 185
{
	struct rpc_clnt *clone;
	struct rpc_auth *auth;
	int err;

186 187
	err = nfs_negotiate_security(parent, path->dentry, flavor);
	if (err < 0)
188 189
		goto out;
	clone  = rpc_clone_client(server->client);
190
	auth   = rpcauth_create(*flavor, clone);
191
	if (!auth) {
192
		err = -EIO;
193
		goto out_shutdown;
194 195 196 197
	}
	err = server->nfs_client->rpc_ops->lookup(clone, parent->d_inode,
						  &path->dentry->d_name,
						  fh, fattr);
198 199
out_shutdown:
	rpc_shutdown_client(clone);
200
out:
201
	return err;
202 203
}
#else /* CONFIG_NFS_V4 */
204 205 206 207 208
static inline int nfs_lookup_with_sec(struct nfs_server *server,
				      struct dentry *parent, struct dentry *dentry,
				      struct path *path, struct nfs_fh *fh,
				      struct nfs_fattr *fattr,
				      rpc_authflavor_t *flavor)
209 210 211 212 213
{
	return -EPERM;
}
#endif /* CONFIG_NFS_V4 */

214
/*
215 216
 * nfs_d_automount - Handle crossing a mountpoint on the server
 * @path - The mountpoint
217 218 219 220 221 222 223 224 225
 *
 * When we encounter a mountpoint on the server, we want to set up
 * a mountpoint on the client too, to prevent inode numbers from
 * colliding, and to allow "df" to work properly.
 * On NFSv4, we also want to allow for the fact that different
 * filesystems may be migrated to different servers in a failover
 * situation, and that different filesystems may want to use
 * different security flavours.
 */
226
struct vfsmount *nfs_d_automount(struct path *path)
227 228
{
	struct vfsmount *mnt;
229
	struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
230
	struct dentry *parent;
231 232
	struct nfs_fh *fh = NULL;
	struct nfs_fattr *fattr = NULL;
233
	int err;
234
	rpc_authflavor_t flavor = RPC_AUTH_UNIX;
235

236
	dprintk("--> nfs_d_automount()\n");
237

238 239 240
	mnt = ERR_PTR(-ESTALE);
	if (IS_ROOT(path->dentry))
		goto out_nofree;
241

242
	mnt = ERR_PTR(-ENOMEM);
243 244 245
	fh = nfs_alloc_fhandle();
	fattr = nfs_alloc_fattr();
	if (fh == NULL || fattr == NULL)
246
		goto out;
247

248
	dprintk("%s: enter\n", __func__);
249

250 251
	/* Look it up again to get its attributes */
	parent = dget_parent(path->dentry);
252
	err = server->nfs_client->rpc_ops->lookup(server->client, parent->d_inode,
253
						  &path->dentry->d_name,
254
						  fh, fattr);
255 256
	if (err == -EPERM && NFS_PROTO(parent->d_inode)->secinfo != NULL)
		err = nfs_lookup_with_sec(server, parent, path->dentry, path, fh, fattr, &flavor);
257
	dput(parent);
258 259 260 261
	if (err != 0) {
		mnt = ERR_PTR(err);
		goto out;
	}
262

263
	if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
264
		mnt = nfs_do_refmount(path->dentry);
M
Manoj Naik 已提交
265
	else
266
		mnt = nfs_do_submount(path->dentry, fh, fattr, flavor);
267
	if (IS_ERR(mnt))
268
		goto out;
269

270 271 272 273
	dprintk("%s: done, success\n", __func__);
	mntget(mnt); /* prevent immediate expiration */
	mnt_set_expiry(mnt, &nfs_automount_list);
	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
274

275
out:
276 277
	nfs_free_fattr(fattr);
	nfs_free_fhandle(fh);
278 279 280
out_nofree:
	dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
	return mnt;
281 282
}

283
const struct inode_operations nfs_mountpoint_inode_operations = {
284 285
	.getattr	= nfs_getattr,
};
T
Trond Myklebust 已提交
286

287
const struct inode_operations nfs_referral_inode_operations = {
M
Manoj Naik 已提交
288 289
};

290
static void nfs_expire_automounts(struct work_struct *work)
T
Trond Myklebust 已提交
291
{
292
	struct list_head *list = &nfs_automount_list;
T
Trond Myklebust 已提交
293 294 295 296 297 298 299 300

	mark_mounts_for_expiry(list);
	if (!list_empty(list))
		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
}

void nfs_release_automount_timer(void)
{
301
	if (list_empty(&nfs_automount_list))
302
		cancel_delayed_work(&nfs_automount_task);
T
Trond Myklebust 已提交
303
}
D
David Howells 已提交
304 305 306 307

/*
 * Clone a mountpoint of the appropriate type
 */
D
David Howells 已提交
308 309
static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
					   const char *devname,
D
David Howells 已提交
310 311 312
					   struct nfs_clone_mount *mountdata)
{
#ifdef CONFIG_NFS_V4
313
	struct vfsmount *mnt = ERR_PTR(-EINVAL);
314
	switch (server->nfs_client->rpc_ops->version) {
D
David Howells 已提交
315 316
		case 2:
		case 3:
317
			mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
318 319
			break;
		case 4:
320
			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
321 322 323
	}
	return mnt;
#else
324
	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
325 326 327 328 329 330 331 332
#endif
}

/**
 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
 * @dentry - parent directory
 * @fh - filehandle for new root dentry
 * @fattr - attributes for new root inode
333
 * @authflavor - security flavor to use when performing the mount
D
David Howells 已提交
334 335
 *
 */
336
static struct vfsmount *nfs_do_submount(struct dentry *dentry,
A
Adrian Bunk 已提交
337
					struct nfs_fh *fh,
338 339
					struct nfs_fattr *fattr,
					rpc_authflavor_t authflavor)
D
David Howells 已提交
340 341
{
	struct nfs_clone_mount mountdata = {
342
		.sb = dentry->d_sb,
D
David Howells 已提交
343 344 345
		.dentry = dentry,
		.fh = fh,
		.fattr = fattr,
346
		.authflavor = authflavor,
D
David Howells 已提交
347 348 349 350 351
	};
	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
	char *page = (char *) __get_free_page(GFP_USER);
	char *devname;

352 353
	dprintk("--> nfs_do_submount()\n");

354
	dprintk("%s: submounting on %s/%s\n", __func__,
D
David Howells 已提交
355 356 357 358
			dentry->d_parent->d_name.name,
			dentry->d_name.name);
	if (page == NULL)
		goto out;
359
	devname = nfs_devname(dentry, page, PAGE_SIZE);
D
David Howells 已提交
360 361 362
	mnt = (struct vfsmount *)devname;
	if (IS_ERR(devname))
		goto free_page;
363
	mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
D
David Howells 已提交
364 365 366
free_page:
	free_page((unsigned long)page);
out:
367
	dprintk("%s: done\n", __func__);
368 369

	dprintk("<-- nfs_do_submount() = %p\n", mnt);
D
David Howells 已提交
370 371
	return mnt;
}