blob: a9e23039ea34604649156b66cd10dc664ece394a [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/fs.h>
16#include <linux/pagemap.h>
17#include <linux/mount.h>
18#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.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{
52 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 size_t size;
54 char *buf;
55 int ret;
56
David Howells416351f2007-05-09 02:33:45 -070057 _enter("{%x:%u,%u}",
58 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /* read the contents of the symlink into the pagecache */
Al Virof6d335c2010-05-21 15:27:09 +010061 page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
62 afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (IS_ERR(page)) {
64 ret = PTR_ERR(page);
65 goto out;
66 }
67
68 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (PageError(page))
70 goto out_free;
71
Nick Piggin6fe69002007-05-06 14:49:04 -070072 buf = kmap(page);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* examine the symlink's contents */
75 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070076 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (size > 2 &&
79 (buf[0] == '%' || buf[0] == '#') &&
80 buf[size - 1] == '.'
81 ) {
82 _debug("symlink is a mountpoint");
83 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070084 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 spin_unlock(&vnode->lock);
86 }
87
88 ret = 0;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 kunmap(page);
Nick Piggin6fe69002007-05-06 14:49:04 -070091out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 _leave(" = %d", ret);
95 return ret;
David Howellsec268152007-04-26 15:49:28 -070096}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * no valid lookup procedure on this sort of dir
100 */
101static struct dentry *afs_mntpt_lookup(struct inode *dir,
102 struct dentry *dentry,
103 struct nameidata *nd)
104{
David Howells08e0e7c2007-04-26 15:55:03 -0700105 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 dir,
107 dentry,
108 dentry->d_parent,
109 dentry->d_parent ?
110 dentry->d_parent->d_name.name : (const unsigned char *) "",
111 dentry->d_name.name);
112
113 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * no valid open procedure on this sort of dir
118 */
119static int afs_mntpt_open(struct inode *inode, struct file *file)
120{
David Howells08e0e7c2007-04-26 15:55:03 -0700121 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800123 file->f_path.dentry->d_parent,
124 file->f_path.dentry->d_parent ?
125 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800127 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * create a vfsmount to be automounted
134 */
135static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
136{
137 struct afs_super_info *super;
138 struct vfsmount *mnt;
David Howells083fd8b2010-04-21 12:01:23 +0100139 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 size_t size;
David Howells083fd8b2010-04-21 12:01:23 +0100141 char *buf, *devname, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int ret;
143
David Howells08e0e7c2007-04-26 15:55:03 -0700144 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 BUG_ON(!mntpt->d_inode);
147
148 ret = -EINVAL;
149 size = mntpt->d_inode->i_size;
150 if (size > PAGE_SIZE - 1)
David Howells083fd8b2010-04-21 12:01:23 +0100151 goto error_no_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 ret = -ENOMEM;
154 devname = (char *) get_zeroed_page(GFP_KERNEL);
155 if (!devname)
David Howells083fd8b2010-04-21 12:01:23 +0100156 goto error_no_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 options = (char *) get_zeroed_page(GFP_KERNEL);
159 if (!options)
David Howells083fd8b2010-04-21 12:01:23 +0100160 goto error_no_options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* read the contents of the AFS special symlink */
Pekka Enberg090d2b12006-06-23 02:05:08 -0700163 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (IS_ERR(page)) {
165 ret = PTR_ERR(page);
David Howells083fd8b2010-04-21 12:01:23 +0100166 goto error_no_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 ret = -EIO;
Nick Piggin6fe69002007-05-06 14:49:04 -0700170 if (PageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto error;
172
David Howells9b3f26c2009-04-03 16:42:41 +0100173 buf = kmap_atomic(page, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 memcpy(devname, buf, size);
David Howells9b3f26c2009-04-03 16:42:41 +0100175 kunmap_atomic(buf, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 page_cache_release(page);
177 page = NULL;
178
179 /* work out what options we want */
180 super = AFS_FS_S(mntpt->d_sb);
181 memcpy(options, "cell=", 5);
182 strcpy(options + 5, super->volume->cell->name);
183 if (super->volume->type == AFSVL_RWVOL)
184 strcat(options, ",rwpath");
185
186 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700187 _debug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400188 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700189 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 free_page((unsigned long) devname);
192 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700193 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return mnt;
195
David Howellsec268152007-04-26 15:49:28 -0700196error:
David Howells083fd8b2010-04-21 12:01:23 +0100197 page_cache_release(page);
198error_no_page:
199 free_page((unsigned long) options);
200error_no_options:
201 free_page((unsigned long) devname);
202error_no_devname:
David Howells08e0e7c2007-04-26 15:55:03 -0700203 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * follow a link from a mountpoint directory, thus causing it to be mounted
209 */
Al Viro008b1502005-08-20 00:17:39 +0100210static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int err;
214
David Howells00d3b7a2007-04-26 15:57:07 -0700215 _enter("%p{%s},{%s:%p{%s},}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dentry,
217 dentry->d_name.name,
Jan Blunck4ac91372008-02-14 19:34:32 -0800218 nd->path.mnt->mnt_devname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800220 nd->path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jan Blunck4ac91372008-02-14 19:34:32 -0800222 dput(nd->path.dentry);
223 nd->path.dentry = dget(dentry);
David Howells08e0e7c2007-04-26 15:55:03 -0700224
Jan Blunck4ac91372008-02-14 19:34:32 -0800225 newmnt = afs_mntpt_do_automount(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (IS_ERR(newmnt)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800227 path_put(&nd->path);
Al Viro008b1502005-08-20 00:17:39 +0100228 return (void *)newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
David Howells08e0e7c2007-04-26 15:55:03 -0700231 mntget(newmnt);
Al Viro8d66bf52008-08-01 09:05:54 -0400232 err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -0700233 switch (err) {
234 case 0:
Jan Blunck09da59162008-02-14 19:34:37 -0800235 path_put(&nd->path);
Jan Blunck4ac91372008-02-14 19:34:32 -0800236 nd->path.mnt = newmnt;
237 nd->path.dentry = dget(newmnt->mnt_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700238 schedule_delayed_work(&afs_mntpt_expiry_timer,
239 afs_mntpt_expiry_timeout * HZ);
240 break;
241 case -EBUSY:
242 /* someone else made a mount here whilst we were busy */
Jan Blunck4ac91372008-02-14 19:34:32 -0800243 while (d_mountpoint(nd->path.dentry) &&
Al Viro9393bd02009-04-18 13:58:15 -0400244 follow_down(&nd->path))
David Howells08e0e7c2007-04-26 15:55:03 -0700245 ;
246 err = 0;
247 default:
248 mntput(newmnt);
249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
David Howells08e0e7c2007-04-26 15:55:03 -0700252 _leave(" = %d", err);
Al Viro008b1502005-08-20 00:17:39 +0100253 return ERR_PTR(err);
David Howellsec268152007-04-26 15:49:28 -0700254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * handle mountpoint expiry timer going off
258 */
David Howells08e0e7c2007-04-26 15:55:03 -0700259static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
David Howells08e0e7c2007-04-26 15:55:03 -0700261 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Howells08e0e7c2007-04-26 15:55:03 -0700263 if (!list_empty(&afs_vfsmounts)) {
264 mark_mounts_for_expiry(&afs_vfsmounts);
265 schedule_delayed_work(&afs_mntpt_expiry_timer,
266 afs_mntpt_expiry_timeout * HZ);
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Howells08e0e7c2007-04-26 15:55:03 -0700269 _leave("");
270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Howells08e0e7c2007-04-26 15:55:03 -0700272/*
273 * kill the AFS mountpoint timer if it's still running
274 */
275void afs_mntpt_kill_timer(void)
276{
277 _enter("");
278
279 ASSERT(list_empty(&afs_vfsmounts));
280 cancel_delayed_work(&afs_mntpt_expiry_timer);
281 flush_scheduled_work();
282}