blob: eecd8b6991862f907de1a00fd1df54dc084059a1 [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>
David Howells13fcc682018-11-01 23:07:27 +000020#include <linux/fs_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
22
23
24static struct dentry *afs_mntpt_lookup(struct inode *dir,
25 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040026 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int afs_mntpt_open(struct inode *inode, struct file *file);
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,
Arnd Bergmann6038f372010-08-15 18:52:59 +020032 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Arjan van de Ven754661f2007-02-12 00:55:38 -080035const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .lookup = afs_mntpt_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070038 .getattr = afs_getattr,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010039 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
wangleibec5eb62010-08-11 09:38:04 +010042const struct inode_operations afs_autocell_inode_operations = {
wangleibec5eb62010-08-11 09:38:04 +010043 .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
David Howellsc99c2172018-11-01 23:07:27 +000051static const char afs_root_volume[] = "root.cell";
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * no valid lookup procedure on this sort of dir
55 */
56static struct dentry *afs_mntpt_lookup(struct inode *dir,
57 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040058 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Al Viroa4555892014-10-21 20:11:25 -040060 _enter("%p,%p{%pd2}", dir, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -070062}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * no valid open procedure on this sort of dir
66 */
67static int afs_mntpt_open(struct inode *inode, struct file *file)
68{
Al Viroa4555892014-10-21 20:11:25 -040069 _enter("%p,%p{%pD2}", inode, file, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -070071}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
David Howellsc99c2172018-11-01 23:07:27 +000074 * Set the parameters for the proposed superblock.
75 */
76static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
77{
78 struct afs_fs_context *ctx = fc->fs_private;
79 struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
80 struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
81 struct afs_cell *cell;
82 const char *p;
83 int ret;
84
85 if (fc->net_ns != src_as->net_ns) {
86 put_net(fc->net_ns);
87 fc->net_ns = get_net(src_as->net_ns);
88 }
89
90 if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
91 ctx->type = AFSVL_RWVOL;
92 ctx->force = true;
93 }
94 if (ctx->cell) {
95 afs_put_cell(ctx->net, ctx->cell);
96 ctx->cell = NULL;
97 }
98 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
99 /* if the directory is a pseudo directory, use the d_name */
100 unsigned size = mntpt->d_name.len;
101
102 if (size < 2)
103 return -ENOENT;
104
105 p = mntpt->d_name.name;
106 if (mntpt->d_name.name[0] == '.') {
107 size--;
108 p++;
109 ctx->type = AFSVL_RWVOL;
110 ctx->force = true;
111 }
112 if (size > AFS_MAXCELLNAME)
113 return -ENAMETOOLONG;
114
115 cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
116 if (IS_ERR(cell)) {
117 pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
118 return PTR_ERR(cell);
119 }
120 ctx->cell = cell;
121
122 ctx->volname = afs_root_volume;
123 ctx->volnamesz = sizeof(afs_root_volume) - 1;
124 } else {
125 /* read the contents of the AFS special symlink */
126 struct page *page;
127 loff_t size = i_size_read(d_inode(mntpt));
128 char *buf;
129
130 if (src_as->cell)
131 ctx->cell = afs_get_cell(src_as->cell);
132
133 if (size > PAGE_SIZE - 1)
134 return -EINVAL;
135
136 page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
137 if (IS_ERR(page))
138 return PTR_ERR(page);
139
140 if (PageError(page)) {
141 ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
142 put_page(page);
143 return ret;
144 }
145
146 buf = kmap(page);
147 ret = vfs_parse_fs_string(fc, "source", buf, size);
148 kunmap(page);
149 put_page(page);
150 if (ret < 0)
151 return ret;
152 }
153
154 return 0;
155}
156
157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * create a vfsmount to be automounted
159 */
160static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
161{
David Howellsc99c2172018-11-01 23:07:27 +0000162 struct fs_context *fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int ret;
165
David Howells2b0143b2015-03-17 22:25:59 +0000166 BUG_ON(!d_inode(mntpt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howellsc99c2172018-11-01 23:07:27 +0000168 fc = fs_context_for_submount(&afs_fs_type, mntpt);
169 if (IS_ERR(fc))
170 return ERR_CAST(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Howellsc99c2172018-11-01 23:07:27 +0000172 ret = afs_mntpt_set_params(fc, mntpt);
173 if (!ret)
174 mnt = fc_mount(fc);
175 else
176 mnt = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
David Howellsc99c2172018-11-01 23:07:27 +0000178 put_fs_context(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return mnt;
David Howellsec268152007-04-26 15:49:28 -0700180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
David Howellsd18610b2011-01-14 19:04:05 +0000183 * handle an automount point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
David Howellsd18610b2011-01-14 19:04:05 +0000185struct vfsmount *afs_d_automount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Al Viroa4555892014-10-21 20:11:25 -0400189 _enter("{%pd}", path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Howellsd18610b2011-01-14 19:04:05 +0000191 newmnt = afs_mntpt_do_automount(path->dentry);
192 if (IS_ERR(newmnt))
193 return newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
David Howellsea5b7782011-01-14 19:10:03 +0000195 mntget(newmnt); /* prevent immediate expiration */
196 mnt_set_expiry(newmnt, &afs_vfsmounts);
197 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
198 afs_mntpt_expiry_timeout * HZ);
Al Viro5ffc2832011-11-25 02:22:06 -0500199 _leave(" = %p", newmnt);
David Howellsea5b7782011-01-14 19:10:03 +0000200 return newmnt;
David Howellsec268152007-04-26 15:49:28 -0700201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
204 * handle mountpoint expiry timer going off
205 */
David Howells08e0e7c2007-04-26 15:55:03 -0700206static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Howells08e0e7c2007-04-26 15:55:03 -0700208 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David Howells08e0e7c2007-04-26 15:55:03 -0700210 if (!list_empty(&afs_vfsmounts)) {
211 mark_mounts_for_expiry(&afs_vfsmounts);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000212 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
213 afs_mntpt_expiry_timeout * HZ);
David Howells08e0e7c2007-04-26 15:55:03 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David Howells08e0e7c2007-04-26 15:55:03 -0700216 _leave("");
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David Howells08e0e7c2007-04-26 15:55:03 -0700219/*
220 * kill the AFS mountpoint timer if it's still running
221 */
222void afs_mntpt_kill_timer(void)
223{
224 _enter("");
225
226 ASSERT(list_empty(&afs_vfsmounts));
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000227 cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
David Howells08e0e7c2007-04-26 15:55:03 -0700228}