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
 *
 * 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/fs.h>
#include <linux/pagemap.h>
#include <linux/mount.h>
#include <linux/namei.h>
19
#include <linux/gfp.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26
#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);
27
static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
28
static void afs_mntpt_expiry_timed_out(struct work_struct *work);
L
Linus Torvalds 已提交
29

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

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

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

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

/*
 * 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 已提交
50
int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
L
Linus Torvalds 已提交
51
{
D
David Howells 已提交
52 53 54
	struct file file = {
		.private_data = key,
	};
L
Linus Torvalds 已提交
55 56 57 58 59
	struct page *page;
	size_t size;
	char *buf;
	int ret;

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

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

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

74 75
	buf = kmap(page);

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

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

	ret = 0;

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

/*
 * no valid lookup procedure on this sort of dir
 */
static struct dentry *afs_mntpt_lookup(struct inode *dir,
				       struct dentry *dentry,
				       struct nameidata *nd)
{
107
	_enter("%p,%p{%p{%s},%s}",
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115
	       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 已提交
116
}
L
Linus Torvalds 已提交
117 118 119 120 121 122

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

	return -EREMOTE;
D
David Howells 已提交
132
}
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140

/*
 * create a vfsmount to be automounted
 */
static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
{
	struct afs_super_info *super;
	struct vfsmount *mnt;
141
	struct page *page;
L
Linus Torvalds 已提交
142
	size_t size;
143
	char *buf, *devname, *options;
L
Linus Torvalds 已提交
144 145
	int ret;

146
	_enter("{%s}", mntpt->d_name.name);
L
Linus Torvalds 已提交
147 148 149 150 151 152

	BUG_ON(!mntpt->d_inode);

	ret = -EINVAL;
	size = mntpt->d_inode->i_size;
	if (size > PAGE_SIZE - 1)
153
		goto error_no_devname;
L
Linus Torvalds 已提交
154 155 156 157

	ret = -ENOMEM;
	devname = (char *) get_zeroed_page(GFP_KERNEL);
	if (!devname)
158
		goto error_no_devname;
L
Linus Torvalds 已提交
159 160 161

	options = (char *) get_zeroed_page(GFP_KERNEL);
	if (!options)
162
		goto error_no_options;
L
Linus Torvalds 已提交
163 164

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

	ret = -EIO;
172
	if (PageError(page))
L
Linus Torvalds 已提交
173 174
		goto error;

D
David Howells 已提交
175
	buf = kmap_atomic(page, KM_USER0);
L
Linus Torvalds 已提交
176
	memcpy(devname, buf, size);
D
David Howells 已提交
177
	kunmap_atomic(buf, KM_USER0);
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188
	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 */
189
	_debug("--- attempting mount %s -o %s ---", devname, options);
190
	mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
191
	_debug("--- mount result %p ---", mnt);
L
Linus Torvalds 已提交
192 193 194

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

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

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

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

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

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

233
	mntget(newmnt);
234
	err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
235 236
	switch (err) {
	case 0:
237
		path_put(&nd->path);
238 239
		nd->path.mnt = newmnt;
		nd->path.dentry = dget(newmnt->mnt_root);
240 241 242 243 244
		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 */
245
		while (d_mountpoint(nd->path.dentry) &&
A
Al Viro 已提交
246
		       follow_down(&nd->path))
247 248 249 250 251
			;
		err = 0;
	default:
		mntput(newmnt);
		break;
L
Linus Torvalds 已提交
252 253
	}

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

/*
 * handle mountpoint expiry timer going off
 */
261
static void afs_mntpt_expiry_timed_out(struct work_struct *work)
L
Linus Torvalds 已提交
262
{
263 264 265 266 267 268 269 270 271 272
	_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 已提交
273

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

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