include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / fs / afs / mntpt.c
CommitLineData
ec26815a 1/* mountpoint management
1da177e4
LT
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>
1da177e4
LT
15#include <linux/fs.h>
16#include <linux/pagemap.h>
17#include <linux/mount.h>
18#include <linux/namei.h>
5a0e3ad6 19#include <linux/gfp.h>
1da177e4
LT
20#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);
008b150a 27static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
08e0e7c8 28static void afs_mntpt_expiry_timed_out(struct work_struct *work);
1da177e4 29
4b6f5d20 30const struct file_operations afs_mntpt_file_operations = {
1da177e4
LT
31 .open = afs_mntpt_open,
32};
33
754661f1 34const struct inode_operations afs_mntpt_inode_operations = {
1da177e4
LT
35 .lookup = afs_mntpt_lookup,
36 .follow_link = afs_mntpt_follow_link,
37 .readlink = page_readlink,
416351f2 38 .getattr = afs_getattr,
1da177e4
LT
39};
40
41static LIST_HEAD(afs_vfsmounts);
08e0e7c8 42static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
1da177e4 43
c1206a2c 44static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
1da177e4 45
1da177e4
LT
46/*
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 */
00d3b7a4 50int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
1da177e4 51{
00d3b7a4
DH
52 struct file file = {
53 .private_data = key,
54 };
1da177e4 55 struct page *page;
1da177e4
LT
56 size_t size;
57 char *buf;
58 int ret;
59
416351f2
DH
60 _enter("{%x:%u,%u}",
61 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
1da177e4
LT
62
63 /* read the contents of the symlink into the pagecache */
00d3b7a4 64 page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file);
1da177e4
LT
65 if (IS_ERR(page)) {
66 ret = PTR_ERR(page);
67 goto out;
68 }
69
70 ret = -EIO;
1da177e4
LT
71 if (PageError(page))
72 goto out_free;
73
6fe6900e
NP
74 buf = kmap(page);
75
1da177e4
LT
76 /* examine the symlink's contents */
77 size = vnode->status.size;
08e0e7c8 78 _debug("symlink to %*.*s", (int) size, (int) size, buf);
1da177e4
LT
79
80 if (size > 2 &&
81 (buf[0] == '%' || buf[0] == '#') &&
82 buf[size - 1] == '.'
83 ) {
84 _debug("symlink is a mountpoint");
85 spin_lock(&vnode->lock);
08e0e7c8 86 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
1da177e4
LT
87 spin_unlock(&vnode->lock);
88 }
89
90 ret = 0;
91
1da177e4 92 kunmap(page);
6fe6900e 93out_free:
1da177e4 94 page_cache_release(page);
ec26815a 95out:
1da177e4
LT
96 _leave(" = %d", ret);
97 return ret;
ec26815a 98}
1da177e4 99
1da177e4
LT
100/*
101 * no valid lookup procedure on this sort of dir
102 */
103static struct dentry *afs_mntpt_lookup(struct inode *dir,
104 struct dentry *dentry,
105 struct nameidata *nd)
106{
08e0e7c8 107 _enter("%p,%p{%p{%s},%s}",
1da177e4
LT
108 dir,
109 dentry,
110 dentry->d_parent,
111 dentry->d_parent ?
112 dentry->d_parent->d_name.name : (const unsigned char *) "",
113 dentry->d_name.name);
114
115 return ERR_PTR(-EREMOTE);
ec26815a 116}
1da177e4 117
1da177e4
LT
118/*
119 * no valid open procedure on this sort of dir
120 */
121static int afs_mntpt_open(struct inode *inode, struct file *file)
122{
08e0e7c8 123 _enter("%p,%p{%p{%s},%s}",
1da177e4 124 inode, file,
1d56a969
JS
125 file->f_path.dentry->d_parent,
126 file->f_path.dentry->d_parent ?
127 file->f_path.dentry->d_parent->d_name.name :
1da177e4 128 (const unsigned char *) "",
1d56a969 129 file->f_path.dentry->d_name.name);
1da177e4
LT
130
131 return -EREMOTE;
ec26815a 132}
1da177e4 133
1da177e4
LT
134/*
135 * create a vfsmount to be automounted
136 */
137static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
138{
139 struct afs_super_info *super;
140 struct vfsmount *mnt;
141 struct page *page = NULL;
142 size_t size;
143 char *buf, *devname = NULL, *options = NULL;
1da177e4
LT
144 int ret;
145
08e0e7c8 146 _enter("{%s}", mntpt->d_name.name);
1da177e4
LT
147
148 BUG_ON(!mntpt->d_inode);
149
150 ret = -EINVAL;
151 size = mntpt->d_inode->i_size;
152 if (size > PAGE_SIZE - 1)
153 goto error;
154
155 ret = -ENOMEM;
156 devname = (char *) get_zeroed_page(GFP_KERNEL);
157 if (!devname)
158 goto error;
159
160 options = (char *) get_zeroed_page(GFP_KERNEL);
161 if (!options)
162 goto error;
163
164 /* read the contents of the AFS special symlink */
090d2b18 165 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
1da177e4
LT
166 if (IS_ERR(page)) {
167 ret = PTR_ERR(page);
168 goto error;
169 }
170
171 ret = -EIO;
6fe6900e 172 if (PageError(page))
1da177e4
LT
173 goto error;
174
9b3f26c9 175 buf = kmap_atomic(page, KM_USER0);
1da177e4 176 memcpy(devname, buf, size);
9b3f26c9 177 kunmap_atomic(buf, KM_USER0);
1da177e4
LT
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 */
08e0e7c8 189 _debug("--- attempting mount %s -o %s ---", devname, options);
1f5ce9e9 190 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
08e0e7c8 191 _debug("--- mount result %p ---", mnt);
1da177e4
LT
192
193 free_page((unsigned long) devname);
194 free_page((unsigned long) options);
08e0e7c8 195 _leave(" = %p", mnt);
1da177e4
LT
196 return mnt;
197
ec26815a 198error:
1da177e4
LT
199 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);
08e0e7c8 205 _leave(" = %d", ret);
1da177e4 206 return ERR_PTR(ret);
ec26815a 207}
1da177e4 208
1da177e4
LT
209/*
210 * follow a link from a mountpoint directory, thus causing it to be mounted
211 */
008b150a 212static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
213{
214 struct vfsmount *newmnt;
1da177e4
LT
215 int err;
216
00d3b7a4 217 _enter("%p{%s},{%s:%p{%s},}",
1da177e4
LT
218 dentry,
219 dentry->d_name.name,
4ac91378 220 nd->path.mnt->mnt_devname,
1da177e4 221 dentry,
4ac91378 222 nd->path.dentry->d_name.name);
1da177e4 223
4ac91378
JB
224 dput(nd->path.dentry);
225 nd->path.dentry = dget(dentry);
08e0e7c8 226
4ac91378 227 newmnt = afs_mntpt_do_automount(nd->path.dentry);
1da177e4 228 if (IS_ERR(newmnt)) {
1d957f9b 229 path_put(&nd->path);
008b150a 230 return (void *)newmnt;
1da177e4
LT
231 }
232
08e0e7c8 233 mntget(newmnt);
8d66bf54 234 err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
08e0e7c8
DH
235 switch (err) {
236 case 0:
09da5916 237 path_put(&nd->path);
4ac91378
JB
238 nd->path.mnt = newmnt;
239 nd->path.dentry = dget(newmnt->mnt_root);
08e0e7c8
DH
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 */
4ac91378 245 while (d_mountpoint(nd->path.dentry) &&
9393bd07 246 follow_down(&nd->path))
08e0e7c8
DH
247 ;
248 err = 0;
249 default:
250 mntput(newmnt);
251 break;
1da177e4
LT
252 }
253
08e0e7c8 254 _leave(" = %d", err);
008b150a 255 return ERR_PTR(err);
ec26815a 256}
1da177e4 257
1da177e4
LT
258/*
259 * handle mountpoint expiry timer going off
260 */
08e0e7c8 261static void afs_mntpt_expiry_timed_out(struct work_struct *work)
1da177e4 262{
08e0e7c8
DH
263 _enter("");
264
265 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 }
270
271 _leave("");
272}
1da177e4 273
08e0e7c8
DH
274/*
275 * kill the AFS mountpoint timer if it's still running
276 */
277void afs_mntpt_kill_timer(void)
278{
279 _enter("");
1da177e4 280
08e0e7c8
DH
281 ASSERT(list_empty(&afs_vfsmounts));
282 cancel_delayed_work(&afs_mntpt_expiry_timer);
283 flush_scheduled_work();
284}