blob: 6d552686c498fae427e2b9408bd03e96e1153699 [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/fs.h>
16#include <linux/pagemap.h>
17#include <linux/mount.h>
18#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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);
Al Viro008b1502005-08-20 00:17:39 +010027static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
David Howells08e0e7c2007-04-26 15:55:03 -070028static void afs_mntpt_expiry_timed_out(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080030const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .open = afs_mntpt_open,
32};
33
Arjan van de Ven754661f2007-02-12 00:55:38 -080034const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .lookup = afs_mntpt_lookup,
36 .follow_link = afs_mntpt_follow_link,
37 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070038 .getattr = afs_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
wangleibec5eb62010-08-11 09:38:04 +010041const struct inode_operations afs_autocell_inode_operations = {
42 .follow_link = afs_mntpt_follow_link,
43 .getattr = afs_getattr,
44};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070047static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Adrian Bunkc1206a22007-10-16 23:26:41 -070049static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * check a symbolic link to see whether it actually encodes a mountpoint
53 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
54 */
David Howells00d3b7a2007-04-26 15:57:07 -070055int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 size_t size;
59 char *buf;
60 int ret;
61
David Howells416351f2007-05-09 02:33:45 -070062 _enter("{%x:%u,%u}",
63 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /* read the contents of the symlink into the pagecache */
Al Virof6d335c2010-05-21 15:27:09 +010066 page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
67 afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (IS_ERR(page)) {
69 ret = PTR_ERR(page);
70 goto out;
71 }
72
73 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (PageError(page))
75 goto out_free;
76
Nick Piggin6fe69002007-05-06 14:49:04 -070077 buf = kmap(page);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* examine the symlink's contents */
80 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070081 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (size > 2 &&
84 (buf[0] == '%' || buf[0] == '#') &&
85 buf[size - 1] == '.'
86 ) {
87 _debug("symlink is a mountpoint");
88 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070089 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock(&vnode->lock);
91 }
92
93 ret = 0;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kunmap(page);
Nick Piggin6fe69002007-05-06 14:49:04 -070096out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070098out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 _leave(" = %d", ret);
100 return ret;
David Howellsec268152007-04-26 15:49:28 -0700101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * no valid lookup procedure on this sort of dir
105 */
106static struct dentry *afs_mntpt_lookup(struct inode *dir,
107 struct dentry *dentry,
108 struct nameidata *nd)
109{
David Howells08e0e7c2007-04-26 15:55:03 -0700110 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dir,
112 dentry,
113 dentry->d_parent,
114 dentry->d_parent ?
115 dentry->d_parent->d_name.name : (const unsigned char *) "",
116 dentry->d_name.name);
117
118 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * no valid open procedure on this sort of dir
123 */
124static int afs_mntpt_open(struct inode *inode, struct file *file)
125{
David Howells08e0e7c2007-04-26 15:55:03 -0700126 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800128 file->f_path.dentry->d_parent,
129 file->f_path.dentry->d_parent ?
130 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800132 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * create a vfsmount to be automounted
139 */
140static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
141{
142 struct afs_super_info *super;
143 struct vfsmount *mnt;
wangleibec5eb62010-08-11 09:38:04 +0100144 struct afs_vnode *vnode;
David Howells083fd8b2010-04-21 12:01:23 +0100145 struct page *page;
wangleibec5eb62010-08-11 09:38:04 +0100146 char *devname, *options;
147 bool rwpath = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int ret;
149
David Howells08e0e7c2007-04-26 15:55:03 -0700150 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 BUG_ON(!mntpt->d_inode);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ret = -ENOMEM;
155 devname = (char *) get_zeroed_page(GFP_KERNEL);
156 if (!devname)
David Howells083fd8b2010-04-21 12:01:23 +0100157 goto error_no_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 options = (char *) get_zeroed_page(GFP_KERNEL);
160 if (!options)
David Howells083fd8b2010-04-21 12:01:23 +0100161 goto error_no_options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
wangleibec5eb62010-08-11 09:38:04 +0100163 vnode = AFS_FS_I(mntpt->d_inode);
164 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
165 /* if the directory is a pseudo directory, use the d_name */
166 static const char afs_root_cell[] = ":root.cell.";
167 unsigned size = mntpt->d_name.len;
168
169 ret = -ENOENT;
170 if (size < 2 || size > AFS_MAXCELLNAME)
171 goto error_no_page;
172
173 if (mntpt->d_name.name[0] == '.') {
174 devname[0] = '#';
175 memcpy(devname + 1, mntpt->d_name.name, size - 1);
176 memcpy(devname + size, afs_root_cell,
177 sizeof(afs_root_cell));
178 rwpath = true;
179 } else {
180 devname[0] = '%';
181 memcpy(devname + 1, mntpt->d_name.name, size);
182 memcpy(devname + size + 1, afs_root_cell,
183 sizeof(afs_root_cell));
184 }
185 } else {
186 /* read the contents of the AFS special symlink */
187 loff_t size = i_size_read(mntpt->d_inode);
188 char *buf;
189
190 ret = -EINVAL;
191 if (size > PAGE_SIZE - 1)
192 goto error_no_page;
193
194 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
195 if (IS_ERR(page)) {
196 ret = PTR_ERR(page);
197 goto error_no_page;
198 }
199
200 ret = -EIO;
201 if (PageError(page))
202 goto error;
203
204 buf = kmap_atomic(page, KM_USER0);
205 memcpy(devname, buf, size);
206 kunmap_atomic(buf, KM_USER0);
207 page_cache_release(page);
208 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* 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);
wangleibec5eb62010-08-11 09:38:04 +0100215 if (super->volume->type == AFSVL_RWVOL || rwpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 strcat(options, ",rwpath");
217
218 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700219 _debug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400220 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700221 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 free_page((unsigned long) devname);
224 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700225 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return mnt;
227
David Howellsec268152007-04-26 15:49:28 -0700228error:
David Howells083fd8b2010-04-21 12:01:23 +0100229 page_cache_release(page);
230error_no_page:
231 free_page((unsigned long) options);
232error_no_options:
233 free_page((unsigned long) devname);
234error_no_devname:
David Howells08e0e7c2007-04-26 15:55:03 -0700235 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * follow a link from a mountpoint directory, thus causing it to be mounted
241 */
Al Viro008b1502005-08-20 00:17:39 +0100242static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int err;
246
David Howells00d3b7a2007-04-26 15:57:07 -0700247 _enter("%p{%s},{%s:%p{%s},}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dentry,
249 dentry->d_name.name,
Jan Blunck4ac91372008-02-14 19:34:32 -0800250 nd->path.mnt->mnt_devname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800252 nd->path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jan Blunck4ac91372008-02-14 19:34:32 -0800254 dput(nd->path.dentry);
255 nd->path.dentry = dget(dentry);
David Howells08e0e7c2007-04-26 15:55:03 -0700256
Jan Blunck4ac91372008-02-14 19:34:32 -0800257 newmnt = afs_mntpt_do_automount(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (IS_ERR(newmnt)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800259 path_put(&nd->path);
Al Viro008b1502005-08-20 00:17:39 +0100260 return (void *)newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
David Howells08e0e7c2007-04-26 15:55:03 -0700263 mntget(newmnt);
Al Viro8d66bf52008-08-01 09:05:54 -0400264 err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -0700265 switch (err) {
266 case 0:
Jan Blunck09da59162008-02-14 19:34:37 -0800267 path_put(&nd->path);
Jan Blunck4ac91372008-02-14 19:34:32 -0800268 nd->path.mnt = newmnt;
269 nd->path.dentry = dget(newmnt->mnt_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700270 schedule_delayed_work(&afs_mntpt_expiry_timer,
271 afs_mntpt_expiry_timeout * HZ);
272 break;
273 case -EBUSY:
274 /* someone else made a mount here whilst we were busy */
Jan Blunck4ac91372008-02-14 19:34:32 -0800275 while (d_mountpoint(nd->path.dentry) &&
Al Viro9393bd02009-04-18 13:58:15 -0400276 follow_down(&nd->path))
David Howells08e0e7c2007-04-26 15:55:03 -0700277 ;
278 err = 0;
279 default:
280 mntput(newmnt);
281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
David Howells08e0e7c2007-04-26 15:55:03 -0700284 _leave(" = %d", err);
Al Viro008b1502005-08-20 00:17:39 +0100285 return ERR_PTR(err);
David Howellsec268152007-04-26 15:49:28 -0700286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * handle mountpoint expiry timer going off
290 */
David Howells08e0e7c2007-04-26 15:55:03 -0700291static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
David Howells08e0e7c2007-04-26 15:55:03 -0700293 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
David Howells08e0e7c2007-04-26 15:55:03 -0700295 if (!list_empty(&afs_vfsmounts)) {
296 mark_mounts_for_expiry(&afs_vfsmounts);
297 schedule_delayed_work(&afs_mntpt_expiry_timer,
298 afs_mntpt_expiry_timeout * HZ);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
David Howells08e0e7c2007-04-26 15:55:03 -0700301 _leave("");
302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David Howells08e0e7c2007-04-26 15:55:03 -0700304/*
305 * kill the AFS mountpoint timer if it's still running
306 */
307void afs_mntpt_kill_timer(void)
308{
309 _enter("");
310
311 ASSERT(list_empty(&afs_vfsmounts));
312 cancel_delayed_work(&afs_mntpt_expiry_timer);
313 flush_scheduled_work();
314}