blob: 08c11a0b66bdc9e20aafa88bf1db118c59af29ec [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,
39 .getattr = afs_inode_getattr,
40};
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
David Howells08e0e7c2007-04-26 15:55:03 -070045unsigned 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 */
51int afs_mntpt_check_symlink(struct afs_vnode *vnode)
52{
53 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 size_t size;
55 char *buf;
56 int ret;
57
58 _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
59
60 /* read the contents of the symlink into the pagecache */
Pekka Enberg090d2b12006-06-23 02:05:08 -070061 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (IS_ERR(page)) {
63 ret = PTR_ERR(page);
64 goto out;
65 }
66
67 ret = -EIO;
68 wait_on_page_locked(page);
69 buf = kmap(page);
70 if (!PageUptodate(page))
71 goto out_free;
72 if (PageError(page))
73 goto out_free;
74
75 /* examine the symlink's contents */
76 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070077 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (size > 2 &&
80 (buf[0] == '%' || buf[0] == '#') &&
81 buf[size - 1] == '.'
82 ) {
83 _debug("symlink is a mountpoint");
84 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070085 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_unlock(&vnode->lock);
87 }
88
89 ret = 0;
90
David Howellsec268152007-04-26 15:49:28 -070091out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 kunmap(page);
93 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070094out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 _leave(" = %d", ret);
96 return ret;
David Howellsec268152007-04-26 15:49:28 -070097}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * no valid lookup procedure on this sort of dir
101 */
102static struct dentry *afs_mntpt_lookup(struct inode *dir,
103 struct dentry *dentry,
104 struct nameidata *nd)
105{
David Howells08e0e7c2007-04-26 15:55:03 -0700106 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dir,
108 dentry,
109 dentry->d_parent,
110 dentry->d_parent ?
111 dentry->d_parent->d_name.name : (const unsigned char *) "",
112 dentry->d_name.name);
113
114 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
118 * no valid open procedure on this sort of dir
119 */
120static int afs_mntpt_open(struct inode *inode, struct file *file)
121{
David Howells08e0e7c2007-04-26 15:55:03 -0700122 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800124 file->f_path.dentry->d_parent,
125 file->f_path.dentry->d_parent ?
126 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800128 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * create a vfsmount to be automounted
135 */
136static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
137{
138 struct afs_super_info *super;
139 struct vfsmount *mnt;
140 struct page *page = NULL;
141 size_t size;
142 char *buf, *devname = NULL, *options = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int ret;
144
David Howells08e0e7c2007-04-26 15:55:03 -0700145 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 BUG_ON(!mntpt->d_inode);
148
149 ret = -EINVAL;
150 size = mntpt->d_inode->i_size;
151 if (size > PAGE_SIZE - 1)
152 goto error;
153
154 ret = -ENOMEM;
155 devname = (char *) get_zeroed_page(GFP_KERNEL);
156 if (!devname)
157 goto error;
158
159 options = (char *) get_zeroed_page(GFP_KERNEL);
160 if (!options)
161 goto error;
162
163 /* read the contents of the AFS special symlink */
Pekka Enberg090d2b12006-06-23 02:05:08 -0700164 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (IS_ERR(page)) {
166 ret = PTR_ERR(page);
167 goto error;
168 }
169
170 ret = -EIO;
171 wait_on_page_locked(page);
172 if (!PageUptodate(page) || PageError(page))
173 goto error;
174
175 buf = kmap(page);
176 memcpy(devname, buf, size);
177 kunmap(page);
178 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 Howells08e0e7c2007-04-26 15:55:03 -0700217 _enter("%p{%s},{%s:%p{%s}}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 dentry,
219 dentry->d_name.name,
220 nd->mnt->mnt_devname,
221 dentry,
222 nd->dentry->d_name.name);
223
David Howells08e0e7c2007-04-26 15:55:03 -0700224 dput(nd->dentry);
225 nd->dentry = dget(dentry);
226
227 newmnt = afs_mntpt_do_automount(nd->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (IS_ERR(newmnt)) {
229 path_release(nd);
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);
234 err = do_add_mount(newmnt, nd, MNT_SHRINKABLE, &afs_vfsmounts);
235 switch (err) {
236 case 0:
237 path_release(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 nd->mnt = newmnt;
David Howells08e0e7c2007-04-26 15:55:03 -0700239 nd->dentry = dget(newmnt->mnt_root);
240 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 */
245 while (d_mountpoint(nd->dentry) &&
246 follow_down(&nd->mnt, &nd->dentry))
247 ;
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}
285
286/*
287 * begin unmount by attempting to remove all automounted mountpoints we added
288 */
289void afs_umount_begin(struct vfsmount *vfsmnt, int flags)
290{
291 shrink_submounts(vfsmnt, &afs_vfsmounts);
David Howellsec268152007-04-26 15:49:28 -0700292}