blob: 9682c33d5daf44d66351ca29ebb5c02cb39ca1ae [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,
Al Viro00cd8dd2012-06-10 17:13:09 -040025 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int afs_mntpt_open(struct inode *inode, struct file *file);
David Howells08e0e7c2007-04-26 15:55:03 -070027static void afs_mntpt_expiry_timed_out(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080029const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .open = afs_mntpt_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020031 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070037 .getattr = afs_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
wangleibec5eb62010-08-11 09:38:04 +010040const struct inode_operations afs_autocell_inode_operations = {
wangleibec5eb62010-08-11 09:38:04 +010041 .getattr = afs_getattr,
42};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070045static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Adrian Bunkc1206a22007-10-16 23:26:41 -070047static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * check a symbolic link to see whether it actually encodes a mountpoint
51 * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately
52 */
David Howells00d3b7a2007-04-26 15:57:07 -070053int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 size_t size;
57 char *buf;
58 int ret;
59
David Howells416351f2007-05-09 02:33:45 -070060 _enter("{%x:%u,%u}",
61 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* read the contents of the symlink into the pagecache */
Al Virof6d335c2010-05-21 15:27:09 +010064 page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0,
65 afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (IS_ERR(page)) {
67 ret = PTR_ERR(page);
68 goto out;
69 }
70
71 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (PageError(page))
73 goto out_free;
74
Nick Piggin6fe69002007-05-06 14:49:04 -070075 buf = kmap(page);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* examine the symlink's contents */
78 size = vnode->status.size;
David Howells08e0e7c2007-04-26 15:55:03 -070079 _debug("symlink to %*.*s", (int) size, (int) size, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (size > 2 &&
82 (buf[0] == '%' || buf[0] == '#') &&
83 buf[size - 1] == '.'
84 ) {
85 _debug("symlink is a mountpoint");
86 spin_lock(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -070087 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
David Howellsd18610b2011-01-14 19:04:05 +000088 vnode->vfs_inode.i_flags |= S_AUTOMOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_unlock(&vnode->lock);
90 }
91
92 ret = 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 kunmap(page);
Nick Piggin6fe69002007-05-06 14:49:04 -070095out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -070097out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 _leave(" = %d", ret);
99 return ret;
David Howellsec268152007-04-26 15:49:28 -0700100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * no valid lookup procedure on this sort of dir
104 */
105static struct dentry *afs_mntpt_lookup(struct inode *dir,
106 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400107 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
David Howells08e0e7c2007-04-26 15:55:03 -0700109 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dir,
111 dentry,
112 dentry->d_parent,
113 dentry->d_parent ?
114 dentry->d_parent->d_name.name : (const unsigned char *) "",
115 dentry->d_name.name);
116
117 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -0700118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * no valid open procedure on this sort of dir
122 */
123static int afs_mntpt_open(struct inode *inode, struct file *file)
124{
David Howells08e0e7c2007-04-26 15:55:03 -0700125 _enter("%p,%p{%p{%s},%s}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 inode, file,
Josef Sipek1d56a962006-12-08 02:36:50 -0800127 file->f_path.dentry->d_parent,
128 file->f_path.dentry->d_parent ?
129 file->f_path.dentry->d_parent->d_name.name :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 (const unsigned char *) "",
Josef Sipek1d56a962006-12-08 02:36:50 -0800131 file->f_path.dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * create a vfsmount to be automounted
138 */
139static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
140{
141 struct afs_super_info *super;
142 struct vfsmount *mnt;
wangleibec5eb62010-08-11 09:38:04 +0100143 struct afs_vnode *vnode;
David Howells083fd8b2010-04-21 12:01:23 +0100144 struct page *page;
wangleibec5eb62010-08-11 09:38:04 +0100145 char *devname, *options;
146 bool rwpath = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int ret;
148
David Howells08e0e7c2007-04-26 15:55:03 -0700149 _enter("{%s}", mntpt->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 BUG_ON(!mntpt->d_inode);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ret = -ENOMEM;
154 devname = (char *) get_zeroed_page(GFP_KERNEL);
155 if (!devname)
David Howells083fd8b2010-04-21 12:01:23 +0100156 goto error_no_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 options = (char *) get_zeroed_page(GFP_KERNEL);
159 if (!options)
David Howells083fd8b2010-04-21 12:01:23 +0100160 goto error_no_options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
wangleibec5eb62010-08-11 09:38:04 +0100162 vnode = AFS_FS_I(mntpt->d_inode);
163 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
164 /* if the directory is a pseudo directory, use the d_name */
165 static const char afs_root_cell[] = ":root.cell.";
166 unsigned size = mntpt->d_name.len;
167
168 ret = -ENOENT;
169 if (size < 2 || size > AFS_MAXCELLNAME)
170 goto error_no_page;
171
172 if (mntpt->d_name.name[0] == '.') {
173 devname[0] = '#';
174 memcpy(devname + 1, mntpt->d_name.name, size - 1);
175 memcpy(devname + size, afs_root_cell,
176 sizeof(afs_root_cell));
177 rwpath = true;
178 } else {
179 devname[0] = '%';
180 memcpy(devname + 1, mntpt->d_name.name, size);
181 memcpy(devname + size + 1, afs_root_cell,
182 sizeof(afs_root_cell));
183 }
184 } else {
185 /* read the contents of the AFS special symlink */
186 loff_t size = i_size_read(mntpt->d_inode);
187 char *buf;
188
189 ret = -EINVAL;
190 if (size > PAGE_SIZE - 1)
191 goto error_no_page;
192
193 page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL);
194 if (IS_ERR(page)) {
195 ret = PTR_ERR(page);
196 goto error_no_page;
197 }
198
199 ret = -EIO;
200 if (PageError(page))
201 goto error;
202
Cong Wangda4aa362011-11-25 23:14:26 +0800203 buf = kmap_atomic(page);
wangleibec5eb62010-08-11 09:38:04 +0100204 memcpy(devname, buf, size);
Cong Wangda4aa362011-11-25 23:14:26 +0800205 kunmap_atomic(buf);
wangleibec5eb62010-08-11 09:38:04 +0100206 page_cache_release(page);
207 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* work out what options we want */
211 super = AFS_FS_S(mntpt->d_sb);
212 memcpy(options, "cell=", 5);
213 strcpy(options + 5, super->volume->cell->name);
wangleibec5eb62010-08-11 09:38:04 +0100214 if (super->volume->type == AFSVL_RWVOL || rwpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 strcat(options, ",rwpath");
216
217 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700218 _debug("--- attempting mount %s -o %s ---", devname, options);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400219 mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700220 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 free_page((unsigned long) devname);
223 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700224 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return mnt;
226
David Howellsec268152007-04-26 15:49:28 -0700227error:
David Howells083fd8b2010-04-21 12:01:23 +0100228 page_cache_release(page);
229error_no_page:
230 free_page((unsigned long) options);
231error_no_options:
232 free_page((unsigned long) devname);
233error_no_devname:
David Howells08e0e7c2007-04-26 15:55:03 -0700234 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
David Howellsd18610b2011-01-14 19:04:05 +0000239 * handle an automount point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
David Howellsd18610b2011-01-14 19:04:05 +0000241struct vfsmount *afs_d_automount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Al Viro5ffc2832011-11-25 02:22:06 -0500245 _enter("{%s}", path->dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Howellsd18610b2011-01-14 19:04:05 +0000247 newmnt = afs_mntpt_do_automount(path->dentry);
248 if (IS_ERR(newmnt))
249 return newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David Howellsea5b7782011-01-14 19:10:03 +0000251 mntget(newmnt); /* prevent immediate expiration */
252 mnt_set_expiry(newmnt, &afs_vfsmounts);
253 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
254 afs_mntpt_expiry_timeout * HZ);
Al Viro5ffc2832011-11-25 02:22:06 -0500255 _leave(" = %p", newmnt);
David Howellsea5b7782011-01-14 19:10:03 +0000256 return newmnt;
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 */
David Howells08e0e7c2007-04-26 15:55:03 -0700262static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
David Howells08e0e7c2007-04-26 15:55:03 -0700264 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Howells08e0e7c2007-04-26 15:55:03 -0700266 if (!list_empty(&afs_vfsmounts)) {
267 mark_mounts_for_expiry(&afs_vfsmounts);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000268 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
269 afs_mntpt_expiry_timeout * HZ);
David Howells08e0e7c2007-04-26 15:55:03 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Howells08e0e7c2007-04-26 15:55:03 -0700272 _leave("");
273}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Howells08e0e7c2007-04-26 15:55:03 -0700275/*
276 * kill the AFS mountpoint timer if it's still running
277 */
278void afs_mntpt_kill_timer(void)
279{
280 _enter("");
281
282 ASSERT(list_empty(&afs_vfsmounts));
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000283 cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
David Howells08e0e7c2007-04-26 15:55:03 -0700284}