mntpt.c 6.6 KB
Newer Older
D
David Howells 已提交
1
/* mountpoint management
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/mount.h>
#include <linux/namei.h>
20
#include <linux/mnt_namespace.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27
#include "internal.h"


static struct dentry *afs_mntpt_lookup(struct inode *dir,
				       struct dentry *dentry,
				       struct nameidata *nd);
static int afs_mntpt_open(struct inode *inode, struct file *file);
28
static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
29
static void afs_mntpt_expiry_timed_out(struct work_struct *work);
L
Linus Torvalds 已提交
30

31
const struct file_operations afs_mntpt_file_operations = {
L
Linus Torvalds 已提交
32 33 34
	.open		= afs_mntpt_open,
};

35
const struct inode_operations afs_mntpt_inode_operations = {
L
Linus Torvalds 已提交
36 37 38
	.lookup		= afs_mntpt_lookup,
	.follow_link	= afs_mntpt_follow_link,
	.readlink	= page_readlink,
D
David Howells 已提交
39
	.getattr	= afs_getattr,
L
Linus Torvalds 已提交
40 41 42
};

static LIST_HEAD(afs_vfsmounts);
43
static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
L
Linus Torvalds 已提交
44

A
Adrian Bunk 已提交
45
static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
L
Linus Torvalds 已提交
46 47 48 49 50

/*
 * check a symbolic link to see whether it actually encodes a mountpoint
 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
 */
D
David Howells 已提交
51
int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
L
Linus Torvalds 已提交
52
{
D
David Howells 已提交
53 54 55
	struct file file = {
		.private_data = key,
	};
L
Linus Torvalds 已提交
56 57 58 59 60
	struct page *page;
	size_t size;
	char *buf;
	int ret;

D
David Howells 已提交
61 62
	_enter("{%x:%u,%u}",
	       vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
L
Linus Torvalds 已提交
63 64

	/* read the contents of the symlink into the pagecache */
D
David Howells 已提交
65
	page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74
	if (IS_ERR(page)) {
		ret = PTR_ERR(page);
		goto out;
	}

	ret = -EIO;
	if (PageError(page))
		goto out_free;

75 76
	buf = kmap(page);

L
Linus Torvalds 已提交
77 78
	/* examine the symlink's contents */
	size = vnode->status.size;
79
	_debug("symlink to %*.*s", (int) size, (int) size, buf);
L
Linus Torvalds 已提交
80 81 82 83 84 85 86

	if (size > 2 &&
	    (buf[0] == '%' || buf[0] == '#') &&
	    buf[size - 1] == '.'
	    ) {
		_debug("symlink is a mountpoint");
		spin_lock(&vnode->lock);
87
		set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
L
Linus Torvalds 已提交
88 89 90 91 92 93
		spin_unlock(&vnode->lock);
	}

	ret = 0;

	kunmap(page);
94
out_free:
L
Linus Torvalds 已提交
95
	page_cache_release(page);
D
David Howells 已提交
96
out:
L
Linus Torvalds 已提交
97 98
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
99
}
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107

/*
 * no valid lookup procedure on this sort of dir
 */
static struct dentry *afs_mntpt_lookup(struct inode *dir,
				       struct dentry *dentry,
				       struct nameidata *nd)
{
108
	_enter("%p,%p{%p{%s},%s}",
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116
	       dir,
	       dentry,
	       dentry->d_parent,
	       dentry->d_parent ?
	       dentry->d_parent->d_name.name : (const unsigned char *) "",
	       dentry->d_name.name);

	return ERR_PTR(-EREMOTE);
D
David Howells 已提交
117
}
L
Linus Torvalds 已提交
118 119 120 121 122 123

/*
 * no valid open procedure on this sort of dir
 */
static int afs_mntpt_open(struct inode *inode, struct file *file)
{
124
	_enter("%p,%p{%p{%s},%s}",
L
Linus Torvalds 已提交
125
	       inode, file,
J
Josef Sipek 已提交
126 127 128
	       file->f_path.dentry->d_parent,
	       file->f_path.dentry->d_parent ?
	       file->f_path.dentry->d_parent->d_name.name :
L
Linus Torvalds 已提交
129
	       (const unsigned char *) "",
J
Josef Sipek 已提交
130
	       file->f_path.dentry->d_name.name);
L
Linus Torvalds 已提交
131 132

	return -EREMOTE;
D
David Howells 已提交
133
}
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146

/*
 * create a vfsmount to be automounted
 */
static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
{
	struct afs_super_info *super;
	struct vfsmount *mnt;
	struct page *page = NULL;
	size_t size;
	char *buf, *devname = NULL, *options = NULL;
	int ret;

147
	_enter("{%s}", mntpt->d_name.name);
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	BUG_ON(!mntpt->d_inode);

	ret = -EINVAL;
	size = mntpt->d_inode->i_size;
	if (size > PAGE_SIZE - 1)
		goto error;

	ret = -ENOMEM;
	devname = (char *) get_zeroed_page(GFP_KERNEL);
	if (!devname)
		goto error;

	options = (char *) get_zeroed_page(GFP_KERNEL);
	if (!options)
		goto error;

	/* read the contents of the AFS special symlink */
166
	page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
L
Linus Torvalds 已提交
167 168 169 170 171 172
	if (IS_ERR(page)) {
		ret = PTR_ERR(page);
		goto error;
	}

	ret = -EIO;
173
	if (PageError(page))
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		goto error;

	buf = kmap(page);
	memcpy(devname, buf, size);
	kunmap(page);
	page_cache_release(page);
	page = NULL;

	/* work out what options we want */
	super = AFS_FS_S(mntpt->d_sb);
	memcpy(options, "cell=", 5);
	strcpy(options + 5, super->volume->cell->name);
	if (super->volume->type == AFSVL_RWVOL)
		strcat(options, ",rwpath");

	/* try and do the mount */
190
	_debug("--- attempting mount %s -o %s ---", devname, options);
191
	mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
192
	_debug("--- mount result %p ---", mnt);
L
Linus Torvalds 已提交
193 194 195

	free_page((unsigned long) devname);
	free_page((unsigned long) options);
196
	_leave(" = %p", mnt);
L
Linus Torvalds 已提交
197 198
	return mnt;

D
David Howells 已提交
199
error:
L
Linus Torvalds 已提交
200 201 202 203 204 205
	if (page)
		page_cache_release(page);
	if (devname)
		free_page((unsigned long) devname);
	if (options)
		free_page((unsigned long) options);
206
	_leave(" = %d", ret);
L
Linus Torvalds 已提交
207
	return ERR_PTR(ret);
D
David Howells 已提交
208
}
L
Linus Torvalds 已提交
209 210 211 212

/*
 * follow a link from a mountpoint directory, thus causing it to be mounted
 */
213
static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
214 215 216 217
{
	struct vfsmount *newmnt;
	int err;

D
David Howells 已提交
218
	_enter("%p{%s},{%s:%p{%s},}",
L
Linus Torvalds 已提交
219 220
	       dentry,
	       dentry->d_name.name,
221
	       nd->path.mnt->mnt_devname,
L
Linus Torvalds 已提交
222
	       dentry,
223
	       nd->path.dentry->d_name.name);
L
Linus Torvalds 已提交
224

225 226
	dput(nd->path.dentry);
	nd->path.dentry = dget(dentry);
227

228
	newmnt = afs_mntpt_do_automount(nd->path.dentry);
L
Linus Torvalds 已提交
229
	if (IS_ERR(newmnt)) {
J
Jan Blunck 已提交
230
		path_put(&nd->path);
231
		return (void *)newmnt;
L
Linus Torvalds 已提交
232 233
	}

234
	mntget(newmnt);
235
	err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
236 237
	switch (err) {
	case 0:
238
		path_put(&nd->path);
239 240
		nd->path.mnt = newmnt;
		nd->path.dentry = dget(newmnt->mnt_root);
241 242 243 244 245
		schedule_delayed_work(&afs_mntpt_expiry_timer,
				      afs_mntpt_expiry_timeout * HZ);
		break;
	case -EBUSY:
		/* someone else made a mount here whilst we were busy */
246 247
		while (d_mountpoint(nd->path.dentry) &&
		       follow_down(&nd->path.mnt, &nd->path.dentry))
248 249 250 251 252
			;
		err = 0;
	default:
		mntput(newmnt);
		break;
L
Linus Torvalds 已提交
253 254
	}

255
	_leave(" = %d", err);
256
	return ERR_PTR(err);
D
David Howells 已提交
257
}
L
Linus Torvalds 已提交
258 259 260 261

/*
 * handle mountpoint expiry timer going off
 */
262
static void afs_mntpt_expiry_timed_out(struct work_struct *work)
L
Linus Torvalds 已提交
263
{
264 265 266 267 268 269 270 271 272 273
	_enter("");

	if (!list_empty(&afs_vfsmounts)) {
		mark_mounts_for_expiry(&afs_vfsmounts);
		schedule_delayed_work(&afs_mntpt_expiry_timer,
				      afs_mntpt_expiry_timeout * HZ);
	}

	_leave("");
}
L
Linus Torvalds 已提交
274

275 276 277 278 279 280
/*
 * kill the AFS mountpoint timer if it's still running
 */
void afs_mntpt_kill_timer(void)
{
	_enter("");
L
Linus Torvalds 已提交
281

282 283 284 285
	ASSERT(list_empty(&afs_vfsmounts));
	cancel_delayed_work(&afs_mntpt_expiry_timer);
	flush_scheduled_work();
}