blob: 2f5503902c3757d469e594bd4d72400b35444939 [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>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080020#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
22
23
24static struct dentry *afs_mntpt_lookup(struct inode *dir,
25 struct dentry *dentry,
26 struct nameidata *nd);
27static int afs_mntpt_open(struct inode *inode, struct file *file);
Al Viro008b1502005-08-20 00:17:39 +010028static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
David Howells08e0e7c2007-04-26 15:55:03 -070029static void afs_mntpt_expiry_timed_out(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080031const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .open = afs_mntpt_open,
33};
34
Arjan van de Ven754661f2007-02-12 00:55:38 -080035const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .lookup = afs_mntpt_lookup,
37 .follow_link = afs_mntpt_follow_link,
38 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070039 .getattr = afs_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070043static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Adrian Bunkc1206a22007-10-16 23:26:41 -070045static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * check a symbolic link to see whether it actually encodes a mountpoint
49 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
50 */
David Howells00d3b7a2007-04-26 15:57:07 -070051int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
David Howells00d3b7a2007-04-26 15:57:07 -070053 struct file file = {
54 .private_data = key,
55 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 size_t size;
58 char *buf;
59 int ret;
60
David Howells416351f2007-05-09 02:33:45 -070061 _enter("{%x:%u,%u}",
62 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* read the contents of the symlink into the pagecache */
David Howells00d3b7a2007-04-26 15:57:07 -070065 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (IS_ERR(page)) {
67 ret = PTR_ERR(page);
68 goto out;
69 }
70
71 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (PageError(page))
73 goto out_free;
74
Nick Piggin6fe69002007-05-06 14:49:04 -070075 buf = kmap(page);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* examine the symlink's contents */
78 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070079 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (size > 2 &&
82 (buf[0] == '%' || buf[0] == '#') &&
83 buf[size - 1] == '.'
84 ) {
85 _debug("symlink is a mountpoint");
86 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070087 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_unlock(&vnode->lock);
89 }
90
91 ret = 0;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 kunmap(page);
Nick Piggin6fe69002007-05-06 14:49:04 -070094out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070096out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 _leave(" = %d", ret);
98 return ret;
David Howellsec268152007-04-26 15:49:28 -070099}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * no valid lookup procedure on this sort of dir
103 */
104static struct dentry *afs_mntpt_lookup(struct inode *dir,
105 struct dentry *dentry,
106 struct nameidata *nd)
107{
David Howells08e0e7c2007-04-26 15:55:03 -0700108 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dir,
110 dentry,
111 dentry->d_parent,
112 dentry->d_parent ?
113 dentry->d_parent->d_name.name : (const unsigned char *) "",
114 dentry->d_name.name);
115
116 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * no valid open procedure on this sort of dir
121 */
122static int afs_mntpt_open(struct inode *inode, struct file *file)
123{
David Howells08e0e7c2007-04-26 15:55:03 -0700124 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800126 file->f_path.dentry->d_parent,
127 file->f_path.dentry->d_parent ?
128 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800130 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * create a vfsmount to be automounted
137 */
138static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
139{
140 struct afs_super_info *super;
141 struct vfsmount *mnt;
142 struct page *page = NULL;
143 size_t size;
144 char *buf, *devname = NULL, *options = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int ret;
146
David Howells08e0e7c2007-04-26 15:55:03 -0700147 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 BUG_ON(!mntpt->d_inode);
150
151 ret = -EINVAL;
152 size = mntpt->d_inode->i_size;
153 if (size > PAGE_SIZE - 1)
154 goto error;
155
156 ret = -ENOMEM;
157 devname = (char *) get_zeroed_page(GFP_KERNEL);
158 if (!devname)
159 goto error;
160
161 options = (char *) get_zeroed_page(GFP_KERNEL);
162 if (!options)
163 goto error;
164
165 /* read the contents of the AFS special symlink */
Pekka Enberg090d2b12006-06-23 02:05:08 -0700166 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (IS_ERR(page)) {
168 ret = PTR_ERR(page);
169 goto error;
170 }
171
172 ret = -EIO;
Nick Piggin6fe69002007-05-06 14:49:04 -0700173 if (PageError(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto error;
175
176 buf = kmap(page);
177 memcpy(devname, buf, size);
178 kunmap(page);
179 page_cache_release(page);
180 page = NULL;
181
182 /* work out what options we want */
183 super = AFS_FS_S(mntpt->d_sb);
184 memcpy(options, "cell=", 5);
185 strcpy(options + 5, super->volume->cell->name);
186 if (super->volume->type == AFSVL_RWVOL)
187 strcat(options, ",rwpath");
188
189 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700190 _debug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400191 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700192 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 free_page((unsigned long) devname);
195 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700196 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return mnt;
198
David Howellsec268152007-04-26 15:49:28 -0700199error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (page)
201 page_cache_release(page);
202 if (devname)
203 free_page((unsigned long) devname);
204 if (options)
205 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700206 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * follow a link from a mountpoint directory, thus causing it to be mounted
212 */
Al Viro008b1502005-08-20 00:17:39 +0100213static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int err;
217
David Howells00d3b7a2007-04-26 15:57:07 -0700218 _enter("%p{%s},{%s:%p{%s},}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dentry,
220 dentry->d_name.name,
Jan Blunck4ac91372008-02-14 19:34:32 -0800221 nd->path.mnt->mnt_devname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800223 nd->path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jan Blunck4ac91372008-02-14 19:34:32 -0800225 dput(nd->path.dentry);
226 nd->path.dentry = dget(dentry);
David Howells08e0e7c2007-04-26 15:55:03 -0700227
Jan Blunck4ac91372008-02-14 19:34:32 -0800228 newmnt = afs_mntpt_do_automount(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (IS_ERR(newmnt)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800230 path_put(&nd->path);
Al Viro008b1502005-08-20 00:17:39 +0100231 return (void *)newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
David Howells08e0e7c2007-04-26 15:55:03 -0700234 mntget(newmnt);
235 err = do_add_mount(newmnt, nd, MNT_SHRINKABLE, &afs_vfsmounts);
236 switch (err) {
237 case 0:
Jan Blunck09da59162008-02-14 19:34:37 -0800238 path_put(&nd->path);
Jan Blunck4ac91372008-02-14 19:34:32 -0800239 nd->path.mnt = newmnt;
240 nd->path.dentry = dget(newmnt->mnt_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700241 schedule_delayed_work(&afs_mntpt_expiry_timer,
242 afs_mntpt_expiry_timeout * HZ);
243 break;
244 case -EBUSY:
245 /* someone else made a mount here whilst we were busy */
Jan Blunck4ac91372008-02-14 19:34:32 -0800246 while (d_mountpoint(nd->path.dentry) &&
247 follow_down(&nd->path.mnt, &nd->path.dentry))
David Howells08e0e7c2007-04-26 15:55:03 -0700248 ;
249 err = 0;
250 default:
251 mntput(newmnt);
252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
David Howells08e0e7c2007-04-26 15:55:03 -0700255 _leave(" = %d", err);
Al Viro008b1502005-08-20 00:17:39 +0100256 return ERR_PTR(err);
David Howellsec268152007-04-26 15:49:28 -0700257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * handle mountpoint expiry timer going off
261 */
David Howells08e0e7c2007-04-26 15:55:03 -0700262static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
David Howells08e0e7c2007-04-26 15:55:03 -0700264 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Howells08e0e7c2007-04-26 15:55:03 -0700266 if (!list_empty(&afs_vfsmounts)) {
267 mark_mounts_for_expiry(&afs_vfsmounts);
268 schedule_delayed_work(&afs_mntpt_expiry_timer,
269 afs_mntpt_expiry_timeout * HZ);
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Howells08e0e7c2007-04-26 15:55:03 -0700272 _leave("");
273}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Howells08e0e7c2007-04-26 15:55:03 -0700275/*
276 * kill the AFS mountpoint timer if it's still running
277 */
278void afs_mntpt_kill_timer(void)
279{
280 _enter("");
281
282 ASSERT(list_empty(&afs_vfsmounts));
283 cancel_delayed_work(&afs_mntpt_expiry_timer);
284 flush_scheduled_work();
285}