mntpt.c 7.4 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
	.open		= afs_mntpt_open,
32
	.llseek		= noop_llseek,
L
Linus Torvalds 已提交
33 34
};

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 43 44 45 46
const struct inode_operations afs_autocell_inode_operations = {
	.follow_link	= afs_mntpt_follow_link,
	.getattr	= afs_getattr,
};

L
Linus Torvalds 已提交
47
static LIST_HEAD(afs_vfsmounts);
48
static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
L
Linus Torvalds 已提交
49

A
Adrian Bunk 已提交
50
static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
L
Linus Torvalds 已提交
51 52 53 54 55

/*
 * 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 已提交
56
int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
L
Linus Torvalds 已提交
57 58 59 60 61 62
{
	struct page *page;
	size_t size;
	char *buf;
	int ret;

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

	/* read the contents of the symlink into the pagecache */
67 68
	page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
			       afs_page_filler, key);
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77
	if (IS_ERR(page)) {
		ret = PTR_ERR(page);
		goto out;
	}

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

78 79
	buf = kmap(page);

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

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

	ret = 0;

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

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

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

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

/*
 * create a vfsmount to be automounted
 */
static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
{
	struct afs_super_info *super;
	struct vfsmount *mnt;
145
	struct afs_vnode *vnode;
146
	struct page *page;
147 148
	char *devname, *options;
	bool rwpath = false;
L
Linus Torvalds 已提交
149 150
	int ret;

151
	_enter("{%s}", mntpt->d_name.name);
L
Linus Torvalds 已提交
152 153 154 155 156 157

	BUG_ON(!mntpt->d_inode);

	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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	vnode = AFS_FS_I(mntpt->d_inode);
	if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
		/* if the directory is a pseudo directory, use the d_name */
		static const char afs_root_cell[] = ":root.cell.";
		unsigned size = mntpt->d_name.len;

		ret = -ENOENT;
		if (size < 2 || size > AFS_MAXCELLNAME)
			goto error_no_page;

		if (mntpt->d_name.name[0] == '.') {
			devname[0] = '#';
			memcpy(devname + 1, mntpt->d_name.name, size - 1);
			memcpy(devname + size, afs_root_cell,
			       sizeof(afs_root_cell));
			rwpath = true;
		} else {
			devname[0] = '%';
			memcpy(devname + 1, mntpt->d_name.name, size);
			memcpy(devname + size + 1, afs_root_cell,
			       sizeof(afs_root_cell));
		}
	} else {
		/* read the contents of the AFS special symlink */
		loff_t size = i_size_read(mntpt->d_inode);
		char *buf;

		ret = -EINVAL;
		if (size > PAGE_SIZE - 1)
			goto error_no_page;

		page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
		if (IS_ERR(page)) {
			ret = PTR_ERR(page);
			goto error_no_page;
		}

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

		buf = kmap_atomic(page, KM_USER0);
		memcpy(devname, buf, size);
		kunmap_atomic(buf, KM_USER0);
		page_cache_release(page);
		page = NULL;
L
Linus Torvalds 已提交
210 211 212 213 214 215
	}

	/* work out what options we want */
	super = AFS_FS_S(mntpt->d_sb);
	memcpy(options, "cell=", 5);
	strcpy(options + 5, super->volume->cell->name);
216
	if (super->volume->type == AFSVL_RWVOL || rwpath)
L
Linus Torvalds 已提交
217 218 219
		strcat(options, ",rwpath");

	/* try and do the mount */
220
	_debug("--- attempting mount %s -o %s ---", devname, options);
221
	mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
222
	_debug("--- mount result %p ---", mnt);
L
Linus Torvalds 已提交
223 224 225

	free_page((unsigned long) devname);
	free_page((unsigned long) options);
226
	_leave(" = %p", mnt);
L
Linus Torvalds 已提交
227 228
	return mnt;

D
David Howells 已提交
229
error:
230 231 232 233 234 235
	page_cache_release(page);
error_no_page:
	free_page((unsigned long) options);
error_no_options:
	free_page((unsigned long) devname);
error_no_devname:
236
	_leave(" = %d", ret);
L
Linus Torvalds 已提交
237
	return ERR_PTR(ret);
D
David Howells 已提交
238
}
L
Linus Torvalds 已提交
239 240 241 242

/*
 * follow a link from a mountpoint directory, thus causing it to be mounted
 */
243
static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
244 245 246 247
{
	struct vfsmount *newmnt;
	int err;

D
David Howells 已提交
248
	_enter("%p{%s},{%s:%p{%s},}",
L
Linus Torvalds 已提交
249 250
	       dentry,
	       dentry->d_name.name,
251
	       nd->path.mnt->mnt_devname,
L
Linus Torvalds 已提交
252
	       dentry,
253
	       nd->path.dentry->d_name.name);
L
Linus Torvalds 已提交
254

255 256
	dput(nd->path.dentry);
	nd->path.dentry = dget(dentry);
257

258
	newmnt = afs_mntpt_do_automount(nd->path.dentry);
L
Linus Torvalds 已提交
259
	if (IS_ERR(newmnt)) {
J
Jan Blunck 已提交
260
		path_put(&nd->path);
261
		return (void *)newmnt;
L
Linus Torvalds 已提交
262 263
	}

264
	mntget(newmnt);
265
	err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
266 267
	switch (err) {
	case 0:
268
		path_put(&nd->path);
269 270
		nd->path.mnt = newmnt;
		nd->path.dentry = dget(newmnt->mnt_root);
271 272
		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
				   afs_mntpt_expiry_timeout * HZ);
273 274 275
		break;
	case -EBUSY:
		/* someone else made a mount here whilst we were busy */
276
		err = follow_down(&nd->path, false);
277 278 279
	default:
		mntput(newmnt);
		break;
L
Linus Torvalds 已提交
280 281
	}

282
	_leave(" = %d", err);
283
	return ERR_PTR(err);
D
David Howells 已提交
284
}
L
Linus Torvalds 已提交
285 286 287 288

/*
 * handle mountpoint expiry timer going off
 */
289
static void afs_mntpt_expiry_timed_out(struct work_struct *work)
L
Linus Torvalds 已提交
290
{
291 292 293 294
	_enter("");

	if (!list_empty(&afs_vfsmounts)) {
		mark_mounts_for_expiry(&afs_vfsmounts);
295 296
		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
				   afs_mntpt_expiry_timeout * HZ);
297 298 299 300
	}

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

302 303 304 305 306 307
/*
 * kill the AFS mountpoint timer if it's still running
 */
void afs_mntpt_kill_timer(void)
{
	_enter("");
L
Linus Torvalds 已提交
308

309
	ASSERT(list_empty(&afs_vfsmounts));
310
	cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
311
}