namespace.c 6.4 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 11 12 13 14 15 16
 *
 * NFS namespace
 */

#include <linux/dcache.h>
#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 已提交
17
#include "internal.h"
18 19 20

#define NFSDBG_FACILITY		NFSDBG_VFS

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

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

A
Adrian Bunk 已提交
27 28 29 30 31
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 已提交
32 33 34
/*
 * nfs_path - reconstruct the path given an arbitrary dentry
 * @base - arbitrary string to prepend to the path
35
 * @droot - pointer to root dentry for mountpoint
D
David Howells 已提交
36 37 38 39 40 41 42 43 44 45
 * @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.
 */
46 47 48
char *nfs_path(const char *base,
	       const struct dentry *droot,
	       const struct dentry *dentry,
D
David Howells 已提交
49 50 51 52 53 54 55 56
	       char *buffer, ssize_t buflen)
{
	char *end = buffer+buflen;
	int namelen;

	*--end = '\0';
	buflen--;
	spin_lock(&dcache_lock);
57
	while (!IS_ROOT(dentry) && dentry != droot) {
D
David Howells 已提交
58 59 60
		namelen = dentry->d_name.len;
		buflen -= namelen + 1;
		if (buflen < 0)
61
			goto Elong_unlock;
D
David Howells 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		end -= namelen;
		memcpy(end, dentry->d_name.name, namelen);
		*--end = '/';
		dentry = dentry->d_parent;
	}
	spin_unlock(&dcache_lock);
	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;
78 79
Elong_unlock:
	spin_unlock(&dcache_lock);
D
David Howells 已提交
80 81 82 83
Elong:
	return ERR_PTR(-ENAMETOOLONG);
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 * 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;
	struct nfs_fh fh;
	struct nfs_fattr fattr;
	int err;

106 107
	dprintk("--> nfs_follow_mountpoint()\n");

108 109 110 111
	BUG_ON(IS_ROOT(dentry));
	dprintk("%s: enter\n", __FUNCTION__);
	dput(nd->dentry);
	nd->dentry = dget(dentry);
112

113 114
	/* Look it up again */
	parent = dget_parent(nd->dentry);
115 116 117
	err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
						  &nd->dentry->d_name,
						  &fh, &fattr);
118 119 120 121
	dput(parent);
	if (err != 0)
		goto out_err;

M
Manoj Naik 已提交
122 123 124 125
	if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
		mnt = nfs_do_refmount(nd->mnt, nd->dentry);
	else
		mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr);
126 127 128 129 130
	err = PTR_ERR(mnt);
	if (IS_ERR(mnt))
		goto out_err;

	mntget(mnt);
T
Trond Myklebust 已提交
131
	err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list);
132 133 134 135 136 137 138 139 140 141
	if (err < 0) {
		mntput(mnt);
		if (err == -EBUSY)
			goto out_follow;
		goto out_err;
	}
	mntput(nd->mnt);
	dput(nd->dentry);
	nd->mnt = mnt;
	nd->dentry = dget(mnt->mnt_root);
T
Trond Myklebust 已提交
142
	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
143 144
out:
	dprintk("%s: done, returned %d\n", __FUNCTION__, err);
145 146

	dprintk("<-- nfs_follow_mountpoint() = %d\n", err);
147 148 149 150 151 152 153 154 155 156 157
	return ERR_PTR(err);
out_err:
	path_release(nd);
	goto out;
out_follow:
	while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
		;
	err = 0;
	goto out;
}

158
const struct inode_operations nfs_mountpoint_inode_operations = {
159 160 161
	.follow_link	= nfs_follow_mountpoint,
	.getattr	= nfs_getattr,
};
T
Trond Myklebust 已提交
162

163
const struct inode_operations nfs_referral_inode_operations = {
M
Manoj Naik 已提交
164 165 166
	.follow_link	= nfs_follow_mountpoint,
};

167
static void nfs_expire_automounts(struct work_struct *work)
T
Trond Myklebust 已提交
168
{
169
	struct list_head *list = &nfs_automount_list;
T
Trond Myklebust 已提交
170 171 172 173 174 175 176 177

	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)
{
178
	if (list_empty(&nfs_automount_list))
179
		cancel_delayed_work(&nfs_automount_task);
T
Trond Myklebust 已提交
180
}
D
David Howells 已提交
181 182 183 184

/*
 * Clone a mountpoint of the appropriate type
 */
D
David Howells 已提交
185 186
static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
					   const char *devname,
D
David Howells 已提交
187 188 189 190
					   struct nfs_clone_mount *mountdata)
{
#ifdef CONFIG_NFS_V4
	struct vfsmount *mnt = NULL;
191
	switch (server->nfs_client->cl_nfsversion) {
D
David Howells 已提交
192 193
		case 2:
		case 3:
194
			mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
195 196
			break;
		case 4:
197
			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
198 199 200
	}
	return mnt;
#else
201
	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
D
David Howells 已提交
202 203 204 205 206 207 208 209 210 211 212
#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 已提交
213 214 215 216
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 已提交
217 218 219 220 221 222 223 224 225 226 227
{
	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;

228 229
	dprintk("--> nfs_do_submount()\n");

D
David Howells 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
			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:
	dprintk("%s: done\n", __FUNCTION__);
244 245

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