blob: 5ffb570cd3a8e1d962b62d2b43324012741ff888 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* mountpoint management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
18#include <linux/mount.h>
19#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
22
23static struct dentry *afs_mntpt_lookup(struct inode *dir,
24 struct dentry *dentry,
25 struct nameidata *nd);
26static int afs_mntpt_open(struct inode *inode, struct file *file);
Al Viro008b1502005-08-20 00:17:39 +010027static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
David Howells08e0e7c2007-04-26 15:55:03 -070028static void afs_mntpt_expiry_timed_out(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080030const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .open = afs_mntpt_open,
32};
33
Arjan van de Ven754661f2007-02-12 00:55:38 -080034const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .lookup = afs_mntpt_lookup,
36 .follow_link = afs_mntpt_follow_link,
37 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070038 .getattr = afs_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070042static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Adrian Bunkc1206a22007-10-16 23:26:41 -070044static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * check a symbolic link to see whether it actually encodes a mountpoint
48 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
49 */
David Howells00d3b7a2007-04-26 15:57:07 -070050int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
David Howells00d3b7a2007-04-26 15:57:07 -070052 struct file file = {
53 .private_data = key,
54 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 size_t size;
57 char *buf;
58 int ret;
59
David Howells416351f2007-05-09 02:33:45 -070060 _enter("{%x:%u,%u}",
61 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* read the contents of the symlink into the pagecache */
David Howells00d3b7a2007-04-26 15:57:07 -070064 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (IS_ERR(page)) {
66 ret = PTR_ERR(page);
67 goto out;
68 }
69
70 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (PageError(page))
72 goto out_free;
73
Nick Piggin6fe69002007-05-06 14:49:04 -070074 buf = kmap(page);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* examine the symlink's contents */
77 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070078 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (size > 2 &&
81 (buf[0] == '%' || buf[0] == '#') &&
82 buf[size - 1] == '.'
83 ) {
84 _debug("symlink is a mountpoint");
85 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070086 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 spin_unlock(&vnode->lock);
88 }
89
90 ret = 0;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 kunmap(page);
Nick Piggin6fe69002007-05-06 14:49:04 -070093out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070095out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 _leave(" = %d", ret);
97 return ret;
David Howellsec268152007-04-26 15:49:28 -070098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * no valid lookup procedure on this sort of dir
102 */
103static struct dentry *afs_mntpt_lookup(struct inode *dir,
104 struct dentry *dentry,
105 struct nameidata *nd)
106{
David Howells08e0e7c2007-04-26 15:55:03 -0700107 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dir,
109 dentry,
110 dentry->d_parent,
111 dentry->d_parent ?
112 dentry->d_parent->d_name.name : (const unsigned char *) "",
113 dentry->d_name.name);
114
115 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * no valid open procedure on this sort of dir
120 */
121static int afs_mntpt_open(struct inode *inode, struct file *file)
122{
David Howells08e0e7c2007-04-26 15:55:03 -0700123 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800125 file->f_path.dentry->d_parent,
126 file->f_path.dentry->d_parent ?
127 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800129 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * create a vfsmount to be automounted
136 */
137static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
138{
139 struct afs_super_info *super;
140 struct vfsmount *mnt;
141 struct page *page = NULL;
142 size_t size;
143 char *buf, *devname = NULL, *options = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret;
145
David Howells08e0e7c2007-04-26 15:55:03 -0700146 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 BUG_ON(!mntpt->d_inode);
149
150 ret = -EINVAL;
151 size = mntpt->d_inode->i_size;
152 if (size > PAGE_SIZE - 1)
153 goto error;
154
155 ret = -ENOMEM;
156 devname = (char *) get_zeroed_page(GFP_KERNEL);
157 if (!devname)
158 goto error;
159
160 options = (char *) get_zeroed_page(GFP_KERNEL);
161 if (!options)
162 goto error;
163
164 /* read the contents of the AFS special symlink */
Pekka Enberg090d2b12006-06-23 02:05:08 -0700165 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (IS_ERR(page)) {
167 ret = PTR_ERR(page);
168 goto error;
169 }
170
171 ret = -EIO;
Nick Piggin6fe69002007-05-06 14:49:04 -0700172 if (PageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto error;
174
David Howells9b3f26c2009-04-03 16:42:41 +0100175 buf = kmap_atomic(page, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 memcpy(devname, buf, size);
David Howells9b3f26c2009-04-03 16:42:41 +0100177 kunmap_atomic(buf, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 page_cache_release(page);
179 page = NULL;
180
181 /* work out what options we want */
182 super = AFS_FS_S(mntpt->d_sb);
183 memcpy(options, "cell=", 5);
184 strcpy(options + 5, super->volume->cell->name);
185 if (super->volume->type == AFSVL_RWVOL)
186 strcat(options, ",rwpath");
187
188 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700189 _debug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400190 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700191 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 free_page((unsigned long) devname);
194 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700195 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return mnt;
197
David Howellsec268152007-04-26 15:49:28 -0700198error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (page)
200 page_cache_release(page);
201 if (devname)
202 free_page((unsigned long) devname);
203 if (options)
204 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700205 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * follow a link from a mountpoint directory, thus causing it to be mounted
211 */
Al Viro008b1502005-08-20 00:17:39 +0100212static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int err;
216
David Howells00d3b7a2007-04-26 15:57:07 -0700217 _enter("%p{%s},{%s:%p{%s},}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 dentry,
219 dentry->d_name.name,
Jan Blunck4ac91372008-02-14 19:34:32 -0800220 nd->path.mnt->mnt_devname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800222 nd->path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jan Blunck4ac91372008-02-14 19:34:32 -0800224 dput(nd->path.dentry);
225 nd->path.dentry = dget(dentry);
David Howells08e0e7c2007-04-26 15:55:03 -0700226
Jan Blunck4ac91372008-02-14 19:34:32 -0800227 newmnt = afs_mntpt_do_automount(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (IS_ERR(newmnt)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800229 path_put(&nd->path);
Al Viro008b1502005-08-20 00:17:39 +0100230 return (void *)newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
David Howells08e0e7c2007-04-26 15:55:03 -0700233 mntget(newmnt);
Al Viro8d66bf52008-08-01 09:05:54 -0400234 err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -0700235 switch (err) {
236 case 0:
Jan Blunck09da59162008-02-14 19:34:37 -0800237 path_put(&nd->path);
Jan Blunck4ac91372008-02-14 19:34:32 -0800238 nd->path.mnt = newmnt;
239 nd->path.dentry = dget(newmnt->mnt_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700240 schedule_delayed_work(&afs_mntpt_expiry_timer,
241 afs_mntpt_expiry_timeout * HZ);
242 break;
243 case -EBUSY:
244 /* someone else made a mount here whilst we were busy */
Jan Blunck4ac91372008-02-14 19:34:32 -0800245 while (d_mountpoint(nd->path.dentry) &&
Al Viro9393bd02009-04-18 13:58:15 -0400246 follow_down(&nd->path))
David Howells08e0e7c2007-04-26 15:55:03 -0700247 ;
248 err = 0;
249 default:
250 mntput(newmnt);
251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
David Howells08e0e7c2007-04-26 15:55:03 -0700254 _leave(" = %d", err);
Al Viro008b1502005-08-20 00:17:39 +0100255 return ERR_PTR(err);
David Howellsec268152007-04-26 15:49:28 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
259 * handle mountpoint expiry timer going off
260 */
David Howells08e0e7c2007-04-26 15:55:03 -0700261static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
David Howells08e0e7c2007-04-26 15:55:03 -0700263 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
David Howells08e0e7c2007-04-26 15:55:03 -0700265 if (!list_empty(&afs_vfsmounts)) {
266 mark_mounts_for_expiry(&afs_vfsmounts);
267 schedule_delayed_work(&afs_mntpt_expiry_timer,
268 afs_mntpt_expiry_timeout * HZ);
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David Howells08e0e7c2007-04-26 15:55:03 -0700271 _leave("");
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Howells08e0e7c2007-04-26 15:55:03 -0700274/*
275 * kill the AFS mountpoint timer if it's still running
276 */
277void afs_mntpt_kill_timer(void)
278{
279 _enter("");
280
281 ASSERT(list_empty(&afs_vfsmounts));
282 cancel_delayed_work(&afs_mntpt_expiry_timer);
283 flush_scheduled_work();
284}