ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / afs / mntpt.c
1 /* mntpt.c: mountpoint management
2  *
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>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/namespace.h>
22 #include "super.h"
23 #include "cell.h"
24 #include "volume.h"
25 #include "vnode.h"
26 #include "internal.h"
27
28
29 static struct dentry *afs_mntpt_lookup(struct inode *dir,
30                                        struct dentry *dentry,
31                                        struct nameidata *nd);
32 static int afs_mntpt_open(struct inode *inode, struct file *file);
33
34 #ifdef AFS_AUTOMOUNT_SUPPORT
35 static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
36 #endif
37
38 struct file_operations afs_mntpt_file_operations = {
39         .open           = afs_mntpt_open,
40 };
41
42 struct inode_operations afs_mntpt_inode_operations = {
43         .lookup         = afs_mntpt_lookup,
44 #ifdef AFS_AUTOMOUNT_SUPPORT
45         .follow_link    = afs_mntpt_follow_link,
46 #endif
47         .readlink       = page_readlink,
48         .getattr        = afs_inode_getattr,
49 };
50
51 #ifdef AFS_AUTOMOUNT_SUPPORT
52 static LIST_HEAD(afs_vfsmounts);
53
54 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer);
55
56 struct afs_timer_ops afs_mntpt_expiry_timer_ops = {
57         .timed_out      = afs_mntpt_expiry_timed_out,
58 };
59
60 struct afs_timer afs_mntpt_expiry_timer;
61
62 unsigned long afs_mntpt_expiry_timeout = 20;
63 #endif
64
65 /*****************************************************************************/
66 /*
67  * check a symbolic link to see whether it actually encodes a mountpoint
68  * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
69  */
70 int afs_mntpt_check_symlink(struct afs_vnode *vnode)
71 {
72         struct page *page;
73         filler_t *filler;
74         size_t size;
75         char *buf;
76         int ret;
77
78         _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique);
79
80         /* read the contents of the symlink into the pagecache */
81         filler = (filler_t *) AFS_VNODE_TO_I(vnode)->i_mapping->a_ops->readpage;
82
83         page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
84                                filler, NULL);
85         if (IS_ERR(page)) {
86                 ret = PTR_ERR(page);
87                 goto out;
88         }
89
90         ret = -EIO;
91         wait_on_page_locked(page);
92         buf = kmap(page);
93         if (!PageUptodate(page))
94                 goto out_free;
95         if (PageError(page))
96                 goto out_free;
97
98         /* examine the symlink's contents */
99         size = vnode->status.size;
100         _debug("symlink to %*.*s", size, (int) size, buf);
101
102         if (size > 2 &&
103             (buf[0] == '%' || buf[0] == '#') &&
104             buf[size - 1] == '.'
105             ) {
106                 _debug("symlink is a mountpoint");
107                 spin_lock(&vnode->lock);
108                 vnode->flags |= AFS_VNODE_MOUNTPOINT;
109                 spin_unlock(&vnode->lock);
110         }
111
112         ret = 0;
113
114  out_free:
115         kunmap(page);
116         page_cache_release(page);
117  out:
118         _leave(" = %d", ret);
119         return ret;
120
121 } /* end afs_mntpt_check_symlink() */
122
123 /*****************************************************************************/
124 /*
125  * no valid lookup procedure on this sort of dir
126  */
127 static struct dentry *afs_mntpt_lookup(struct inode *dir,
128                                        struct dentry *dentry,
129                                        struct nameidata *nd)
130 {
131         kenter("%p,%p{%p{%s},%s}",
132                dir,
133                dentry,
134                dentry->d_parent,
135                dentry->d_parent ?
136                dentry->d_parent->d_name.name : (const unsigned char *) "",
137                dentry->d_name.name);
138
139         return ERR_PTR(-EREMOTE);
140 } /* end afs_mntpt_lookup() */
141
142 /*****************************************************************************/
143 /*
144  * no valid open procedure on this sort of dir
145  */
146 static int afs_mntpt_open(struct inode *inode, struct file *file)
147 {
148         kenter("%p,%p{%p{%s},%s}",
149                inode, file,
150                file->f_dentry->d_parent,
151                file->f_dentry->d_parent ?
152                file->f_dentry->d_parent->d_name.name :
153                (const unsigned char *) "",
154                file->f_dentry->d_name.name);
155
156         return -EREMOTE;
157 } /* end afs_mntpt_open() */
158
159 #ifdef AFS_AUTOMOUNT_SUPPORT
160 /*****************************************************************************/
161 /*
162  * create a vfsmount to be automounted
163  */
164 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
165 {
166         struct afs_super_info *super;
167         struct vfsmount *mnt;
168         struct page *page = NULL;
169         size_t size;
170         char *buf, *devname = NULL, *options = NULL;
171         int ret;
172
173         kenter("{%s}", mntpt->d_name.name);
174
175         BUG_ON(!mntpt->d_inode);
176
177         ret = -EINVAL;
178         size = mntpt->d_inode->i_size;
179         if (size > PAGE_SIZE - 1)
180                 goto error;
181
182         ret = -ENOMEM;
183         devname = (char *) get_zeroed_page(GFP_KERNEL);
184         if (!devname)
185                 goto error;
186
187         options = (char *) get_zeroed_page(GFP_KERNEL);
188         if (!options)
189                 goto error;
190
191         /* read the contents of the AFS special symlink */
192         filler_t *filler = mntpt->d_inode->i_mapping->a_ops->readpage;
193
194         page = read_cache_page(mntpt->d_inode->i_mapping, 0, filler, NULL);
195         if (IS_ERR(page)) {
196                 ret = PTR_ERR(page);
197                 goto error;
198         }
199
200         ret = -EIO;
201         wait_on_page_locked(page);
202         if (!PageUptodate(page) || PageError(page))
203                 goto error;
204
205         buf = kmap(page);
206         memcpy(devname, buf, size);
207         kunmap(page);
208         page_cache_release(page);
209         page = NULL;
210
211         /* work out what options we want */
212         super = AFS_FS_S(mntpt->d_sb);
213         memcpy(options, "cell=", 5);
214         strcpy(options + 5, super->volume->cell->name);
215         if (super->volume->type == AFSVL_RWVOL)
216                 strcat(options, ",rwpath");
217
218         /* try and do the mount */
219         kdebug("--- attempting mount %s -o %s ---", devname, options);
220         mnt = do_kern_mount("afs", 0, devname, options);
221         kdebug("--- mount result %p ---", mnt);
222
223         free_page((unsigned long) devname);
224         free_page((unsigned long) options);
225         kleave(" = %p", mnt);
226         return mnt;
227
228  error:
229         if (page)
230                 page_cache_release(page);
231         if (devname)
232                 free_page((unsigned long) devname);
233         if (options)
234                 free_page((unsigned long) options);
235         kleave(" = %d", ret);
236         return ERR_PTR(ret);
237 } /* end afs_mntpt_do_automount() */
238
239 /*****************************************************************************/
240 /*
241  * follow a link from a mountpoint directory, thus causing it to be mounted
242  */
243 static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
244 {
245         struct nameidata newnd;
246         struct vfsmount *newmnt;
247         int err;
248
249         kenter("%p{%s},{%s:%p{%s}}",
250                dentry,
251                dentry->d_name.name,
252                nd->mnt->mnt_devname,
253                dentry,
254                nd->dentry->d_name.name);
255
256         newmnt = afs_mntpt_do_automount(dentry);
257         if (IS_ERR(newmnt))
258                 return PTR_ERR(newmnt);
259
260         struct_cpy(&newnd, nd);
261         newnd.dentry = dentry;
262         err = do_add_mount(newmnt, &newnd, 0, &afs_vfsmounts);
263
264         if (!err) {
265                 path_release(nd);
266                 mntget(newmnt);
267                 nd->mnt = newmnt;
268                 dget(newmnt->mnt_root);
269                 nd->dentry = newmnt->mnt_root;
270         }
271
272         kleave(" = %d", err);
273         return err;
274 } /* end afs_mntpt_follow_link() */
275
276 /*****************************************************************************/
277 /*
278  * handle mountpoint expiry timer going off
279  */
280 static void afs_mntpt_expiry_timed_out(struct afs_timer *timer)
281 {
282         kenter("");
283
284         mark_mounts_for_expiry(&afs_vfsmounts);
285
286         afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
287                                 afs_mntpt_expiry_timeout * HZ);
288
289         kleave("");
290 } /* end afs_mntpt_expiry_timed_out() */
291 #endif