namespace.c 6.9 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>
D
David Howells 已提交
18
#include "internal.h"
19 20 21

#define NFSDBG_FACILITY		NFSDBG_VFS

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

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

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

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

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

	seq = read_seqbegin(&rename_lock);
	rcu_read_lock();
63
	while (!IS_ROOT(dentry) && dentry != droot) {
D
David Howells 已提交
64 65 66
		namelen = dentry->d_name.len;
		buflen -= namelen + 1;
		if (buflen < 0)
67
			goto Elong_unlock;
D
David Howells 已提交
68 69 70 71 72
		end -= namelen;
		memcpy(end, dentry->d_name.name, namelen);
		*--end = '/';
		dentry = dentry->d_parent;
	}
73 74 75
	rcu_read_unlock();
	if (read_seqretry(&rename_lock, seq))
		goto rename_retry;
76 77 78 79 80
	if (*end != '/') {
		if (--buflen < 0)
			goto Elong;
		*--end = '/';
	}
D
David Howells 已提交
81 82 83 84 85 86 87 88 89 90
	namelen = strlen(base);
	/* Strip off excess slashes in base string */
	while (namelen > 0 && base[namelen - 1] == '/')
		namelen--;
	buflen -= namelen;
	if (buflen < 0)
		goto Elong;
	end -= namelen;
	memcpy(end, base, namelen);
	return end;
91
Elong_unlock:
92 93 94
	rcu_read_unlock();
	if (read_seqretry(&rename_lock, seq))
		goto rename_retry;
D
David Howells 已提交
95 96 97 98
Elong:
	return ERR_PTR(-ENAMETOOLONG);
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/*
 * nfs_follow_mountpoint - handle crossing a mountpoint on the server
 * @dentry - dentry of mountpoint
 * @nd - nameidata info
 *
 * 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.
 */
static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
{
	struct vfsmount *mnt;
	struct nfs_server *server = NFS_SERVER(dentry->d_inode);
	struct dentry *parent;
117 118
	struct nfs_fh *fh = NULL;
	struct nfs_fattr *fattr = NULL;
119 120
	int err;

121 122
	dprintk("--> nfs_follow_mountpoint()\n");

123 124 125 126
	err = -ESTALE;
	if (IS_ROOT(dentry))
		goto out_err;

127 128 129 130 131 132
	err = -ENOMEM;
	fh = nfs_alloc_fhandle();
	fattr = nfs_alloc_fattr();
	if (fh == NULL || fattr == NULL)
		goto out_err;

133
	dprintk("%s: enter\n", __func__);
134 135
	dput(nd->path.dentry);
	nd->path.dentry = dget(dentry);
136

137
	/* Look it up again */
138
	parent = dget_parent(nd->path.dentry);
139
	err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
140
						  &nd->path.dentry->d_name,
141
						  fh, fattr);
142 143 144 145
	dput(parent);
	if (err != 0)
		goto out_err;

146
	if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
147
		mnt = nfs_do_refmount(nd->path.mnt, nd->path.dentry);
M
Manoj Naik 已提交
148
	else
149 150
		mnt = nfs_do_submount(nd->path.mnt, nd->path.dentry, fh,
				      fattr);
151 152 153 154 155
	err = PTR_ERR(mnt);
	if (IS_ERR(mnt))
		goto out_err;

	mntget(mnt);
156
	err = do_add_mount(mnt, &nd->path, nd->path.mnt->mnt_flags|MNT_SHRINKABLE,
157
			   &nfs_automount_list);
158 159 160 161 162 163
	if (err < 0) {
		mntput(mnt);
		if (err == -EBUSY)
			goto out_follow;
		goto out_err;
	}
J
Jan Blunck 已提交
164
	path_put(&nd->path);
165 166
	nd->path.mnt = mnt;
	nd->path.dentry = dget(mnt->mnt_root);
T
Trond Myklebust 已提交
167
	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
168
out:
169 170
	nfs_free_fattr(fattr);
	nfs_free_fhandle(fh);
171
	dprintk("%s: done, returned %d\n", __func__, err);
172 173

	dprintk("<-- nfs_follow_mountpoint() = %d\n", err);
174 175
	return ERR_PTR(err);
out_err:
J
Jan Blunck 已提交
176
	path_put(&nd->path);
177 178
	goto out;
out_follow:
179
	err = follow_down(&nd->path, false);
180 181 182
	goto out;
}

183
const struct inode_operations nfs_mountpoint_inode_operations = {
184 185 186
	.follow_link	= nfs_follow_mountpoint,
	.getattr	= nfs_getattr,
};
T
Trond Myklebust 已提交
187

188
const struct inode_operations nfs_referral_inode_operations = {
M
Manoj Naik 已提交
189 190 191
	.follow_link	= nfs_follow_mountpoint,
};

192
static void nfs_expire_automounts(struct work_struct *work)
T
Trond Myklebust 已提交
193
{
194
	struct list_head *list = &nfs_automount_list;
T
Trond Myklebust 已提交
195 196 197 198 199 200 201 202

	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)
{
203
	if (list_empty(&nfs_automount_list))
204
		cancel_delayed_work(&nfs_automount_task);
T
Trond Myklebust 已提交
205
}
D
David Howells 已提交
206 207 208 209

/*
 * Clone a mountpoint of the appropriate type
 */
D
David Howells 已提交
210 211
static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
					   const char *devname,
D
David Howells 已提交
212 213 214
					   struct nfs_clone_mount *mountdata)
{
#ifdef CONFIG_NFS_V4
215
	struct vfsmount *mnt = ERR_PTR(-EINVAL);
216
	switch (server->nfs_client->rpc_ops->version) {
D
David Howells 已提交
217 218
		case 2:
		case 3:
219
			mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
220 221
			break;
		case 4:
222
			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
223 224 225
	}
	return mnt;
#else
226
	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
227 228 229 230 231 232 233 234 235 236 237
#endif
}

/**
 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
 * @mnt_parent - mountpoint of parent directory
 * @dentry - parent directory
 * @fh - filehandle for new root dentry
 * @fattr - attributes for new root inode
 *
 */
A
Adrian Bunk 已提交
238 239 240 241
static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
					const struct dentry *dentry,
					struct nfs_fh *fh,
					struct nfs_fattr *fattr)
D
David Howells 已提交
242 243 244 245 246 247 248 249 250 251 252
{
	struct nfs_clone_mount mountdata = {
		.sb = mnt_parent->mnt_sb,
		.dentry = dentry,
		.fh = fh,
		.fattr = fattr,
	};
	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
	char *page = (char *) __get_free_page(GFP_USER);
	char *devname;

253 254
	dprintk("--> nfs_do_submount()\n");

255
	dprintk("%s: submounting on %s/%s\n", __func__,
D
David Howells 已提交
256 257 258 259 260 261 262 263 264 265 266 267
			dentry->d_parent->d_name.name,
			dentry->d_name.name);
	if (page == NULL)
		goto out;
	devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
	mnt = (struct vfsmount *)devname;
	if (IS_ERR(devname))
		goto free_page;
	mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
free_page:
	free_page((unsigned long)page);
out:
268
	dprintk("%s: done\n", __func__);
269 270

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