blob: ca3fa81b068abf6af81e8cd5d613d5f46b0e89af [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 "super.h"
22#include "cell.h"
23#include "volume.h"
24#include "vnode.h"
25#include "internal.h"
26
27
28static struct dentry *afs_mntpt_lookup(struct inode *dir,
29 struct dentry *dentry,
30 struct nameidata *nd);
31static int afs_mntpt_open(struct inode *inode, struct file *file);
Al Viro008b1502005-08-20 00:17:39 +010032static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080034const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .open = afs_mntpt_open,
36};
37
Arjan van de Ven754661f2007-02-12 00:55:38 -080038const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 .lookup = afs_mntpt_lookup,
40 .follow_link = afs_mntpt_follow_link,
41 .readlink = page_readlink,
42 .getattr = afs_inode_getattr,
43};
44
45static LIST_HEAD(afs_vfsmounts);
46
47static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
48
49struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
50 .timed_out = afs_mntpt_expiry_timed_out,
51};
52
53struct afs_timer afs_mntpt_expiry_timer;
54
55unsigned long afs_mntpt_expiry_timeout = 20;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * check a symbolic link to see whether it actually encodes a mountpoint
59 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
60 */
61int afs_mntpt_check_symlink(struct afs_vnode *vnode)
62{
63 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 size_t size;
65 char *buf;
66 int ret;
67
68 _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
69
70 /* read the contents of the symlink into the pagecache */
Pekka Enberg090d2b12006-06-23 02:05:08 -070071 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (IS_ERR(page)) {
73 ret = PTR_ERR(page);
74 goto out;
75 }
76
77 ret = -EIO;
78 wait_on_page_locked(page);
79 buf = kmap(page);
80 if (!PageUptodate(page))
81 goto out_free;
82 if (PageError(page))
83 goto out_free;
84
85 /* examine the symlink's contents */
86 size = vnode->status.size;
87 _debug("symlink to %*.*s", size, (int) size, buf);
88
89 if (size > 2 &&
90 (buf[0] == '%' || buf[0] == '#') &&
91 buf[size - 1] == '.'
92 ) {
93 _debug("symlink is a mountpoint");
94 spin_lock(&vnode->lock);
95 vnode->flags |= AFS_VNODE_MOUNTPOINT;
96 spin_unlock(&vnode->lock);
97 }
98
99 ret = 0;
100
David Howellsec268152007-04-26 15:49:28 -0700101out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 kunmap(page);
103 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700104out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 _leave(" = %d", ret);
106 return ret;
David Howellsec268152007-04-26 15:49:28 -0700107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * no valid lookup procedure on this sort of dir
111 */
112static struct dentry *afs_mntpt_lookup(struct inode *dir,
113 struct dentry *dentry,
114 struct nameidata *nd)
115{
116 kenter("%p,%p{%p{%s},%s}",
117 dir,
118 dentry,
119 dentry->d_parent,
120 dentry->d_parent ?
121 dentry->d_parent->d_name.name : (const unsigned char *) "",
122 dentry->d_name.name);
123
124 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * no valid open procedure on this sort of dir
129 */
130static int afs_mntpt_open(struct inode *inode, struct file *file)
131{
132 kenter("%p,%p{%p{%s},%s}",
133 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800134 file->f_path.dentry->d_parent,
135 file->f_path.dentry->d_parent ?
136 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800138 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * create a vfsmount to be automounted
145 */
146static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
147{
148 struct afs_super_info *super;
149 struct vfsmount *mnt;
150 struct page *page = NULL;
151 size_t size;
152 char *buf, *devname = NULL, *options = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int ret;
154
155 kenter("{%s}", mntpt->d_name.name);
156
157 BUG_ON(!mntpt->d_inode);
158
159 ret = -EINVAL;
160 size = mntpt->d_inode->i_size;
161 if (size > PAGE_SIZE - 1)
162 goto error;
163
164 ret = -ENOMEM;
165 devname = (char *) get_zeroed_page(GFP_KERNEL);
166 if (!devname)
167 goto error;
168
169 options = (char *) get_zeroed_page(GFP_KERNEL);
170 if (!options)
171 goto error;
172
173 /* read the contents of the AFS special symlink */
Pekka Enberg090d2b12006-06-23 02:05:08 -0700174 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (IS_ERR(page)) {
176 ret = PTR_ERR(page);
177 goto error;
178 }
179
180 ret = -EIO;
181 wait_on_page_locked(page);
182 if (!PageUptodate(page) || PageError(page))
183 goto error;
184
185 buf = kmap(page);
186 memcpy(devname, buf, size);
187 kunmap(page);
188 page_cache_release(page);
189 page = NULL;
190
191 /* work out what options we want */
192 super = AFS_FS_S(mntpt->d_sb);
193 memcpy(options, "cell=", 5);
194 strcpy(options + 5, super->volume->cell->name);
195 if (super->volume->type == AFSVL_RWVOL)
196 strcat(options, ",rwpath");
197
198 /* try and do the mount */
199 kdebug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400200 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 kdebug("--- mount result %p ---", mnt);
202
203 free_page((unsigned long) devname);
204 free_page((unsigned long) options);
205 kleave(" = %p", mnt);
206 return mnt;
207
David Howellsec268152007-04-26 15:49:28 -0700208error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (page)
210 page_cache_release(page);
211 if (devname)
212 free_page((unsigned long) devname);
213 if (options)
214 free_page((unsigned long) options);
215 kleave(" = %d", ret);
216 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * follow a link from a mountpoint directory, thus causing it to be mounted
221 */
Al Viro008b1502005-08-20 00:17:39 +0100222static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct vfsmount *newmnt;
225 struct dentry *old_dentry;
226 int err;
227
228 kenter("%p{%s},{%s:%p{%s}}",
229 dentry,
230 dentry->d_name.name,
231 nd->mnt->mnt_devname,
232 dentry,
233 nd->dentry->d_name.name);
234
235 newmnt = afs_mntpt_do_automount(dentry);
236 if (IS_ERR(newmnt)) {
237 path_release(nd);
Al Viro008b1502005-08-20 00:17:39 +0100238 return (void *)newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 old_dentry = nd->dentry;
242 nd->dentry = dentry;
243 err = do_add_mount(newmnt, nd, 0, &afs_vfsmounts);
244 nd->dentry = old_dentry;
245
246 path_release(nd);
247
248 if (!err) {
249 mntget(newmnt);
250 nd->mnt = newmnt;
251 dget(newmnt->mnt_root);
252 nd->dentry = newmnt->mnt_root;
253 }
254
255 kleave(" = %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 */
262static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
263{
264 kenter("");
265
266 mark_mounts_for_expiry(&afs_vfsmounts);
267
268 afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
269 afs_mntpt_expiry_timeout * HZ);
270
271 kleave("");
David Howellsec268152007-04-26 15:49:28 -0700272}