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
 *
 * NFS namespace
 */

B
Bryan Schumaker 已提交
10
#include <linux/module.h>
11
#include <linux/dcache.h>
12
#include <linux/gfp.h>
13 14 15 16 17 18
#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>
19
#include <linux/sunrpc/gss_api.h>
D
David Howells 已提交
20
#include "internal.h"
21 22 23

#define NFSDBG_FACILITY		NFSDBG_VFS

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

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

D
David Howells 已提交
30 31
/*
 * nfs_path - reconstruct the path given an arbitrary dentry
32
 * @base - used to return pointer to the end of devname part of path
D
David Howells 已提交
33 34 35
 * @dentry - pointer to dentry
 * @buffer - result buffer
 * @buflen - length of buffer
36
 * @flags - options (see below)
D
David Howells 已提交
37
 *
38 39
 * Helper function for constructing the server pathname
 * by arbitrary hashed dentry.
D
David Howells 已提交
40 41
 *
 * This is mainly for use in figuring out the path on the
42 43
 * server side when automounting on top of an existing partition
 * and in generating /proc/mounts and friends.
44 45 46 47 48
 *
 * Supported flags:
 * NFS_PATH_CANONICAL: ensure there is exactly one slash after
 *		       the original device (export) name
 *		       (if unset, the original name is returned verbatim)
D
David Howells 已提交
49
 */
50 51
char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
	       unsigned flags)
D
David Howells 已提交
52
{
53
	char *end;
D
David Howells 已提交
54
	int namelen;
55
	unsigned seq;
56
	const char *base;
D
David Howells 已提交
57

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

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

127
/*
128 129
 * nfs_d_automount - Handle crossing a mountpoint on the server
 * @path - The mountpoint
130 131 132 133 134 135 136 137 138
 *
 * 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.
 */
139
struct vfsmount *nfs_d_automount(struct path *path)
140 141
{
	struct vfsmount *mnt;
B
Bryan Schumaker 已提交
142
	struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
143 144
	struct nfs_fh *fh = NULL;
	struct nfs_fattr *fattr = NULL;
145

146
	dprintk("--> nfs_d_automount()\n");
147

148 149 150
	mnt = ERR_PTR(-ESTALE);
	if (IS_ROOT(path->dentry))
		goto out_nofree;
151

152
	mnt = ERR_PTR(-ENOMEM);
153 154 155
	fh = nfs_alloc_fhandle();
	fattr = nfs_alloc_fattr();
	if (fh == NULL || fattr == NULL)
156
		goto out;
157

158
	dprintk("%s: enter\n", __func__);
159

B
Bryan Schumaker 已提交
160
	mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
161
	if (IS_ERR(mnt))
162
		goto out;
163

164 165 166 167
	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);
168

169
out:
170 171
	nfs_free_fattr(fattr);
	nfs_free_fhandle(fh);
172
out_nofree:
173 174 175 176
	if (IS_ERR(mnt))
		dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
	else
		dprintk("<-- %s() = %p\n", __func__, mnt);
177
	return mnt;
178 179
}

180
const struct inode_operations nfs_mountpoint_inode_operations = {
181 182
	.getattr	= nfs_getattr,
};
T
Trond Myklebust 已提交
183

184
const struct inode_operations nfs_referral_inode_operations = {
M
Manoj Naik 已提交
185 186
};

187
static void nfs_expire_automounts(struct work_struct *work)
T
Trond Myklebust 已提交
188
{
189
	struct list_head *list = &nfs_automount_list;
T
Trond Myklebust 已提交
190 191 192 193 194 195 196 197

	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)
{
198
	if (list_empty(&nfs_automount_list))
199
		cancel_delayed_work(&nfs_automount_task);
T
Trond Myklebust 已提交
200
}
D
David Howells 已提交
201 202 203 204

/*
 * Clone a mountpoint of the appropriate type
 */
D
David Howells 已提交
205 206
static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
					   const char *devname,
D
David Howells 已提交
207 208
					   struct nfs_clone_mount *mountdata)
{
209
	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
210 211 212 213 214 215 216
}

/**
 * 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
217
 * @authflavor - security flavor to use when performing the mount
D
David Howells 已提交
218 219
 *
 */
B
Bryan Schumaker 已提交
220 221
struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
				 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
D
David Howells 已提交
222 223
{
	struct nfs_clone_mount mountdata = {
224
		.sb = dentry->d_sb,
D
David Howells 已提交
225 226 227
		.dentry = dentry,
		.fh = fh,
		.fattr = fattr,
228
		.authflavor = authflavor,
D
David Howells 已提交
229 230 231 232 233
	};
	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
	char *page = (char *) __get_free_page(GFP_USER);
	char *devname;

234 235
	dprintk("--> nfs_do_submount()\n");

236
	dprintk("%s: submounting on %s/%s\n", __func__,
D
David Howells 已提交
237 238 239 240
			dentry->d_parent->d_name.name,
			dentry->d_name.name);
	if (page == NULL)
		goto out;
241
	devname = nfs_devname(dentry, page, PAGE_SIZE);
D
David Howells 已提交
242 243 244
	mnt = (struct vfsmount *)devname;
	if (IS_ERR(devname))
		goto free_page;
245
	mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
D
David Howells 已提交
246 247 248
free_page:
	free_page((unsigned long)page);
out:
249
	dprintk("%s: done\n", __func__);
250 251

	dprintk("<-- nfs_do_submount() = %p\n", mnt);
D
David Howells 已提交
252 253
	return mnt;
}
B
Bryan Schumaker 已提交
254
EXPORT_SYMBOL_GPL(nfs_do_submount);
B
Bryan Schumaker 已提交
255 256 257 258 259 260 261 262

struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
			      struct nfs_fh *fh, struct nfs_fattr *fattr)
{
	int err;
	struct dentry *parent = dget_parent(dentry);

	/* Look it up again to get its attributes */
263
	err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
B
Bryan Schumaker 已提交
264 265 266 267 268 269
	dput(parent);
	if (err != 0)
		return ERR_PTR(err);

	return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
}
B
Bryan Schumaker 已提交
270
EXPORT_SYMBOL_GPL(nfs_submount);