blob: 2e51c6994148f30f4ec8d858b1e318b1d58980c0 [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,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010038 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
wangleibec5eb62010-08-11 09:38:04 +010041const struct inode_operations afs_autocell_inode_operations = {
wangleibec5eb62010-08-11 09:38:04 +010042 .getattr = afs_getattr,
43};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070046static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Adrian Bunkc1206a22007-10-16 23:26:41 -070048static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * no valid lookup procedure on this sort of dir
52 */
53static struct dentry *afs_mntpt_lookup(struct inode *dir,
54 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040055 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Al Viroa4555892014-10-21 20:11:25 -040057 _enter("%p,%p{%pd2}", dir, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -070059}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * no valid open procedure on this sort of dir
63 */
64static int afs_mntpt_open(struct inode *inode, struct file *file)
65{
Al Viroa4555892014-10-21 20:11:25 -040066 _enter("%p,%p{%pD2}", inode, file, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -070068}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * create a vfsmount to be automounted
72 */
73static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
74{
David Howells4d673da2018-02-06 06:26:30 +000075 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct vfsmount *mnt;
wangleibec5eb62010-08-11 09:38:04 +010077 struct afs_vnode *vnode;
David Howells083fd8b2010-04-21 12:01:23 +010078 struct page *page;
wangleibec5eb62010-08-11 09:38:04 +010079 char *devname, *options;
80 bool rwpath = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int ret;
82
Al Viroa4555892014-10-21 20:11:25 -040083 _enter("{%pd}", mntpt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
David Howells2b0143b2015-03-17 22:25:59 +000085 BUG_ON(!d_inode(mntpt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ret = -ENOMEM;
88 devname = (char *) get_zeroed_page(GFP_KERNEL);
89 if (!devname)
David Howells083fd8b2010-04-21 12:01:23 +010090 goto error_no_devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 options = (char *) get_zeroed_page(GFP_KERNEL);
93 if (!options)
David Howells083fd8b2010-04-21 12:01:23 +010094 goto error_no_options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
David Howells2b0143b2015-03-17 22:25:59 +000096 vnode = AFS_FS_I(d_inode(mntpt));
wangleibec5eb62010-08-11 09:38:04 +010097 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
98 /* if the directory is a pseudo directory, use the d_name */
99 static const char afs_root_cell[] = ":root.cell.";
100 unsigned size = mntpt->d_name.len;
101
102 ret = -ENOENT;
103 if (size < 2 || size > AFS_MAXCELLNAME)
104 goto error_no_page;
105
106 if (mntpt->d_name.name[0] == '.') {
David Howells4d673da2018-02-06 06:26:30 +0000107 devname[0] = '%';
108 memcpy(devname + 1, mntpt->d_name.name + 1, size - 1);
wangleibec5eb62010-08-11 09:38:04 +0100109 memcpy(devname + size, afs_root_cell,
110 sizeof(afs_root_cell));
111 rwpath = true;
112 } else {
David Howells4d673da2018-02-06 06:26:30 +0000113 devname[0] = '#';
wangleibec5eb62010-08-11 09:38:04 +0100114 memcpy(devname + 1, mntpt->d_name.name, size);
115 memcpy(devname + size + 1, afs_root_cell,
116 sizeof(afs_root_cell));
117 }
118 } else {
119 /* read the contents of the AFS special symlink */
David Howells2b0143b2015-03-17 22:25:59 +0000120 loff_t size = i_size_read(d_inode(mntpt));
wangleibec5eb62010-08-11 09:38:04 +0100121 char *buf;
122
123 ret = -EINVAL;
124 if (size > PAGE_SIZE - 1)
125 goto error_no_page;
126
David Howells2b0143b2015-03-17 22:25:59 +0000127 page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
wangleibec5eb62010-08-11 09:38:04 +0100128 if (IS_ERR(page)) {
129 ret = PTR_ERR(page);
130 goto error_no_page;
131 }
132
David Howellsf51375c2018-10-20 00:57:57 +0100133 if (PageError(page)) {
134 ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
wangleibec5eb62010-08-11 09:38:04 +0100135 goto error;
David Howellsf51375c2018-10-20 00:57:57 +0100136 }
wangleibec5eb62010-08-11 09:38:04 +0100137
Cong Wangda4aa362011-11-25 23:14:26 +0800138 buf = kmap_atomic(page);
wangleibec5eb62010-08-11 09:38:04 +0100139 memcpy(devname, buf, size);
Cong Wangda4aa362011-11-25 23:14:26 +0800140 kunmap_atomic(buf);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 put_page(page);
wangleibec5eb62010-08-11 09:38:04 +0100142 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* work out what options we want */
David Howells4d673da2018-02-06 06:26:30 +0000146 as = AFS_FS_S(mntpt->d_sb);
147 if (as->cell) {
148 memcpy(options, "cell=", 5);
149 strcpy(options + 5, as->cell->name);
150 if ((as->volume && as->volume->type == AFSVL_RWVOL) || rwpath)
151 strcat(options, ",rwpath");
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* try and do the mount */
David Howells08e0e7c2007-04-26 15:55:03 -0700155 _debug("--- attempting mount %s -o %s ---", devname, options);
Eric W. Biederman93faccbb2017-02-01 06:06:16 +1300156 mnt = vfs_submount(mntpt, &afs_fs_type, devname, options);
David Howells08e0e7c2007-04-26 15:55:03 -0700157 _debug("--- mount result %p ---", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 free_page((unsigned long) devname);
160 free_page((unsigned long) options);
David Howells08e0e7c2007-04-26 15:55:03 -0700161 _leave(" = %p", mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return mnt;
163
David Howellsec268152007-04-26 15:49:28 -0700164error:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300165 put_page(page);
David Howells083fd8b2010-04-21 12:01:23 +0100166error_no_page:
167 free_page((unsigned long) options);
168error_no_options:
169 free_page((unsigned long) devname);
170error_no_devname:
David Howells08e0e7c2007-04-26 15:55:03 -0700171 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
David Howellsd18610b2011-01-14 19:04:05 +0000176 * handle an automount point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
David Howellsd18610b2011-01-14 19:04:05 +0000178struct vfsmount *afs_d_automount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Al Viroa4555892014-10-21 20:11:25 -0400182 _enter("{%pd}", path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David Howellsd18610b2011-01-14 19:04:05 +0000184 newmnt = afs_mntpt_do_automount(path->dentry);
185 if (IS_ERR(newmnt))
186 return newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Howellsea5b7782011-01-14 19:10:03 +0000188 mntget(newmnt); /* prevent immediate expiration */
189 mnt_set_expiry(newmnt, &afs_vfsmounts);
190 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
191 afs_mntpt_expiry_timeout * HZ);
Al Viro5ffc2832011-11-25 02:22:06 -0500192 _leave(" = %p", newmnt);
David Howellsea5b7782011-01-14 19:10:03 +0000193 return newmnt;
David Howellsec268152007-04-26 15:49:28 -0700194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * handle mountpoint expiry timer going off
198 */
David Howells08e0e7c2007-04-26 15:55:03 -0700199static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David Howells08e0e7c2007-04-26 15:55:03 -0700201 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
David Howells08e0e7c2007-04-26 15:55:03 -0700203 if (!list_empty(&afs_vfsmounts)) {
204 mark_mounts_for_expiry(&afs_vfsmounts);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000205 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
206 afs_mntpt_expiry_timeout * HZ);
David Howells08e0e7c2007-04-26 15:55:03 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Howells08e0e7c2007-04-26 15:55:03 -0700209 _leave("");
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Howells08e0e7c2007-04-26 15:55:03 -0700212/*
213 * kill the AFS mountpoint timer if it's still running
214 */
215void afs_mntpt_kill_timer(void)
216{
217 _enter("");
218
219 ASSERT(list_empty(&afs_vfsmounts));
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000220 cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
David Howells08e0e7c2007-04-26 15:55:03 -0700221}