blob: f532d6d3bd28c176b69fd42ad431bdea579adfad [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* mountpoint management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/mount.h>
14#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
David Howells13fcc682018-11-01 23:07:27 +000016#include <linux/fs_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
19
20static struct dentry *afs_mntpt_lookup(struct inode *dir,
21 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040022 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static int afs_mntpt_open(struct inode *inode, struct file *file);
David Howells08e0e7c2007-04-26 15:55:03 -070024static void afs_mntpt_expiry_timed_out(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080026const struct file_operations afs_mntpt_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .open = afs_mntpt_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020028 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
Arjan van de Ven754661f2007-02-12 00:55:38 -080031const struct inode_operations afs_mntpt_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .lookup = afs_mntpt_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .readlink = page_readlink,
David Howells416351f2007-05-09 02:33:45 -070034 .getattr = afs_getattr,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010035 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
wangleibec5eb62010-08-11 09:38:04 +010038const struct inode_operations afs_autocell_inode_operations = {
wangleibec5eb62010-08-11 09:38:04 +010039 .getattr = afs_getattr,
40};
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static LIST_HEAD(afs_vfsmounts);
David Howells08e0e7c2007-04-26 15:55:03 -070043static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Adrian Bunkc1206a22007-10-16 23:26:41 -070045static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David Howellsc99c2172018-11-01 23:07:27 +000047static const char afs_root_volume[] = "root.cell";
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * no valid lookup procedure on this sort of dir
51 */
52static struct dentry *afs_mntpt_lookup(struct inode *dir,
53 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040054 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Al Viroa4555892014-10-21 20:11:25 -040056 _enter("%p,%p{%pd2}", dir, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ERR_PTR(-EREMOTE);
David Howellsec268152007-04-26 15:49:28 -070058}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * no valid open procedure on this sort of dir
62 */
63static int afs_mntpt_open(struct inode *inode, struct file *file)
64{
Al Viroa4555892014-10-21 20:11:25 -040065 _enter("%p,%p{%pD2}", inode, file, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -EREMOTE;
David Howellsec268152007-04-26 15:49:28 -070067}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
David Howellsc99c2172018-11-01 23:07:27 +000070 * Set the parameters for the proposed superblock.
71 */
72static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
73{
74 struct afs_fs_context *ctx = fc->fs_private;
75 struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
76 struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
77 struct afs_cell *cell;
78 const char *p;
79 int ret;
80
81 if (fc->net_ns != src_as->net_ns) {
82 put_net(fc->net_ns);
83 fc->net_ns = get_net(src_as->net_ns);
84 }
85
86 if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
87 ctx->type = AFSVL_RWVOL;
88 ctx->force = true;
89 }
90 if (ctx->cell) {
91 afs_put_cell(ctx->net, ctx->cell);
92 ctx->cell = NULL;
93 }
94 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
95 /* if the directory is a pseudo directory, use the d_name */
96 unsigned size = mntpt->d_name.len;
97
98 if (size < 2)
99 return -ENOENT;
100
101 p = mntpt->d_name.name;
102 if (mntpt->d_name.name[0] == '.') {
103 size--;
104 p++;
105 ctx->type = AFSVL_RWVOL;
106 ctx->force = true;
107 }
108 if (size > AFS_MAXCELLNAME)
109 return -ENAMETOOLONG;
110
111 cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
112 if (IS_ERR(cell)) {
113 pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
114 return PTR_ERR(cell);
115 }
116 ctx->cell = cell;
117
118 ctx->volname = afs_root_volume;
119 ctx->volnamesz = sizeof(afs_root_volume) - 1;
120 } else {
121 /* read the contents of the AFS special symlink */
122 struct page *page;
123 loff_t size = i_size_read(d_inode(mntpt));
124 char *buf;
125
126 if (src_as->cell)
127 ctx->cell = afs_get_cell(src_as->cell);
128
129 if (size > PAGE_SIZE - 1)
130 return -EINVAL;
131
132 page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
133 if (IS_ERR(page))
134 return PTR_ERR(page);
135
136 if (PageError(page)) {
137 ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
138 put_page(page);
139 return ret;
140 }
141
142 buf = kmap(page);
143 ret = vfs_parse_fs_string(fc, "source", buf, size);
144 kunmap(page);
145 put_page(page);
146 if (ret < 0)
147 return ret;
148 }
149
150 return 0;
151}
152
153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * create a vfsmount to be automounted
155 */
156static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
157{
David Howellsc99c2172018-11-01 23:07:27 +0000158 struct fs_context *fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int ret;
161
David Howells2b0143b2015-03-17 22:25:59 +0000162 BUG_ON(!d_inode(mntpt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David Howellsc99c2172018-11-01 23:07:27 +0000164 fc = fs_context_for_submount(&afs_fs_type, mntpt);
165 if (IS_ERR(fc))
166 return ERR_CAST(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howellsc99c2172018-11-01 23:07:27 +0000168 ret = afs_mntpt_set_params(fc, mntpt);
169 if (!ret)
170 mnt = fc_mount(fc);
171 else
172 mnt = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Howellsc99c2172018-11-01 23:07:27 +0000174 put_fs_context(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return mnt;
David Howellsec268152007-04-26 15:49:28 -0700176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
David Howellsd18610b2011-01-14 19:04:05 +0000179 * handle an automount point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
David Howellsd18610b2011-01-14 19:04:05 +0000181struct vfsmount *afs_d_automount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct vfsmount *newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Al Viroa4555892014-10-21 20:11:25 -0400185 _enter("{%pd}", path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howellsd18610b2011-01-14 19:04:05 +0000187 newmnt = afs_mntpt_do_automount(path->dentry);
188 if (IS_ERR(newmnt))
189 return newmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Howellsea5b7782011-01-14 19:10:03 +0000191 mntget(newmnt); /* prevent immediate expiration */
192 mnt_set_expiry(newmnt, &afs_vfsmounts);
193 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
194 afs_mntpt_expiry_timeout * HZ);
Al Viro5ffc2832011-11-25 02:22:06 -0500195 _leave(" = %p", newmnt);
David Howellsea5b7782011-01-14 19:10:03 +0000196 return newmnt;
David Howellsec268152007-04-26 15:49:28 -0700197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * handle mountpoint expiry timer going off
201 */
David Howells08e0e7c2007-04-26 15:55:03 -0700202static void afs_mntpt_expiry_timed_out(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Howells08e0e7c2007-04-26 15:55:03 -0700204 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Howells08e0e7c2007-04-26 15:55:03 -0700206 if (!list_empty(&afs_vfsmounts)) {
207 mark_mounts_for_expiry(&afs_vfsmounts);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000208 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
209 afs_mntpt_expiry_timeout * HZ);
David Howells08e0e7c2007-04-26 15:55:03 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Howells08e0e7c2007-04-26 15:55:03 -0700212 _leave("");
213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
David Howells08e0e7c2007-04-26 15:55:03 -0700215/*
216 * kill the AFS mountpoint timer if it's still running
217 */
218void afs_mntpt_kill_timer(void)
219{
220 _enter("");
221
222 ASSERT(list_empty(&afs_vfsmounts));
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000223 cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
David Howells08e0e7c2007-04-26 15:55:03 -0700224}