1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* mountpoint management
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/fs.h>
12*4882a593Smuzhiyun #include <linux/pagemap.h>
13*4882a593Smuzhiyun #include <linux/mount.h>
14*4882a593Smuzhiyun #include <linux/namei.h>
15*4882a593Smuzhiyun #include <linux/gfp.h>
16*4882a593Smuzhiyun #include <linux/fs_context.h>
17*4882a593Smuzhiyun #include "internal.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static struct dentry *afs_mntpt_lookup(struct inode *dir,
21*4882a593Smuzhiyun struct dentry *dentry,
22*4882a593Smuzhiyun unsigned int flags);
23*4882a593Smuzhiyun static int afs_mntpt_open(struct inode *inode, struct file *file);
24*4882a593Smuzhiyun static void afs_mntpt_expiry_timed_out(struct work_struct *work);
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun const struct file_operations afs_mntpt_file_operations = {
27*4882a593Smuzhiyun .open = afs_mntpt_open,
28*4882a593Smuzhiyun .llseek = noop_llseek,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun const struct inode_operations afs_mntpt_inode_operations = {
32*4882a593Smuzhiyun .lookup = afs_mntpt_lookup,
33*4882a593Smuzhiyun .readlink = page_readlink,
34*4882a593Smuzhiyun .getattr = afs_getattr,
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun const struct inode_operations afs_autocell_inode_operations = {
38*4882a593Smuzhiyun .getattr = afs_getattr,
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static LIST_HEAD(afs_vfsmounts);
42*4882a593Smuzhiyun static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun static const char afs_root_volume[] = "root.cell";
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * no valid lookup procedure on this sort of dir
50*4882a593Smuzhiyun */
afs_mntpt_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)51*4882a593Smuzhiyun static struct dentry *afs_mntpt_lookup(struct inode *dir,
52*4882a593Smuzhiyun struct dentry *dentry,
53*4882a593Smuzhiyun unsigned int flags)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun _enter("%p,%p{%pd2}", dir, dentry, dentry);
56*4882a593Smuzhiyun return ERR_PTR(-EREMOTE);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * no valid open procedure on this sort of dir
61*4882a593Smuzhiyun */
afs_mntpt_open(struct inode * inode,struct file * file)62*4882a593Smuzhiyun static int afs_mntpt_open(struct inode *inode, struct file *file)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun _enter("%p,%p{%pD2}", inode, file, file);
65*4882a593Smuzhiyun return -EREMOTE;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * Set the parameters for the proposed superblock.
70*4882a593Smuzhiyun */
afs_mntpt_set_params(struct fs_context * fc,struct dentry * mntpt)71*4882a593Smuzhiyun static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun struct afs_fs_context *ctx = fc->fs_private;
74*4882a593Smuzhiyun struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
75*4882a593Smuzhiyun struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
76*4882a593Smuzhiyun struct afs_cell *cell;
77*4882a593Smuzhiyun const char *p;
78*4882a593Smuzhiyun int ret;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if (fc->net_ns != src_as->net_ns) {
81*4882a593Smuzhiyun put_net(fc->net_ns);
82*4882a593Smuzhiyun fc->net_ns = get_net(src_as->net_ns);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
86*4882a593Smuzhiyun ctx->type = AFSVL_RWVOL;
87*4882a593Smuzhiyun ctx->force = true;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun if (ctx->cell) {
90*4882a593Smuzhiyun afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt);
91*4882a593Smuzhiyun ctx->cell = NULL;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
94*4882a593Smuzhiyun /* if the directory is a pseudo directory, use the d_name */
95*4882a593Smuzhiyun unsigned size = mntpt->d_name.len;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (size < 2)
98*4882a593Smuzhiyun return -ENOENT;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun p = mntpt->d_name.name;
101*4882a593Smuzhiyun if (mntpt->d_name.name[0] == '.') {
102*4882a593Smuzhiyun size--;
103*4882a593Smuzhiyun p++;
104*4882a593Smuzhiyun ctx->type = AFSVL_RWVOL;
105*4882a593Smuzhiyun ctx->force = true;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun if (size > AFS_MAXCELLNAME)
108*4882a593Smuzhiyun return -ENAMETOOLONG;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
111*4882a593Smuzhiyun if (IS_ERR(cell)) {
112*4882a593Smuzhiyun pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
113*4882a593Smuzhiyun return PTR_ERR(cell);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun ctx->cell = cell;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun ctx->volname = afs_root_volume;
118*4882a593Smuzhiyun ctx->volnamesz = sizeof(afs_root_volume) - 1;
119*4882a593Smuzhiyun } else {
120*4882a593Smuzhiyun /* read the contents of the AFS special symlink */
121*4882a593Smuzhiyun struct page *page;
122*4882a593Smuzhiyun loff_t size = i_size_read(d_inode(mntpt));
123*4882a593Smuzhiyun char *buf;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (src_as->cell)
126*4882a593Smuzhiyun ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (size < 2 || size > PAGE_SIZE - 1)
129*4882a593Smuzhiyun return -EINVAL;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
132*4882a593Smuzhiyun if (IS_ERR(page))
133*4882a593Smuzhiyun return PTR_ERR(page);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (PageError(page)) {
136*4882a593Smuzhiyun ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
137*4882a593Smuzhiyun put_page(page);
138*4882a593Smuzhiyun return ret;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun buf = kmap(page);
142*4882a593Smuzhiyun ret = -EINVAL;
143*4882a593Smuzhiyun if (buf[size - 1] == '.')
144*4882a593Smuzhiyun ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
145*4882a593Smuzhiyun kunmap(page);
146*4882a593Smuzhiyun put_page(page);
147*4882a593Smuzhiyun if (ret < 0)
148*4882a593Smuzhiyun return ret;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * create a vfsmount to be automounted
156*4882a593Smuzhiyun */
afs_mntpt_do_automount(struct dentry * mntpt)157*4882a593Smuzhiyun static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct fs_context *fc;
160*4882a593Smuzhiyun struct vfsmount *mnt;
161*4882a593Smuzhiyun int ret;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun BUG_ON(!d_inode(mntpt));
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun fc = fs_context_for_submount(&afs_fs_type, mntpt);
166*4882a593Smuzhiyun if (IS_ERR(fc))
167*4882a593Smuzhiyun return ERR_CAST(fc);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun ret = afs_mntpt_set_params(fc, mntpt);
170*4882a593Smuzhiyun if (!ret)
171*4882a593Smuzhiyun mnt = fc_mount(fc);
172*4882a593Smuzhiyun else
173*4882a593Smuzhiyun mnt = ERR_PTR(ret);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun put_fs_context(fc);
176*4882a593Smuzhiyun return mnt;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun * handle an automount point
181*4882a593Smuzhiyun */
afs_d_automount(struct path * path)182*4882a593Smuzhiyun struct vfsmount *afs_d_automount(struct path *path)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct vfsmount *newmnt;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun _enter("{%pd}", path->dentry);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun newmnt = afs_mntpt_do_automount(path->dentry);
189*4882a593Smuzhiyun if (IS_ERR(newmnt))
190*4882a593Smuzhiyun return newmnt;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun mntget(newmnt); /* prevent immediate expiration */
193*4882a593Smuzhiyun mnt_set_expiry(newmnt, &afs_vfsmounts);
194*4882a593Smuzhiyun queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
195*4882a593Smuzhiyun afs_mntpt_expiry_timeout * HZ);
196*4882a593Smuzhiyun _leave(" = %p", newmnt);
197*4882a593Smuzhiyun return newmnt;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * handle mountpoint expiry timer going off
202*4882a593Smuzhiyun */
afs_mntpt_expiry_timed_out(struct work_struct * work)203*4882a593Smuzhiyun static void afs_mntpt_expiry_timed_out(struct work_struct *work)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun _enter("");
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (!list_empty(&afs_vfsmounts)) {
208*4882a593Smuzhiyun mark_mounts_for_expiry(&afs_vfsmounts);
209*4882a593Smuzhiyun queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
210*4882a593Smuzhiyun afs_mntpt_expiry_timeout * HZ);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun _leave("");
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * kill the AFS mountpoint timer if it's still running
218*4882a593Smuzhiyun */
afs_mntpt_kill_timer(void)219*4882a593Smuzhiyun void afs_mntpt_kill_timer(void)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun _enter("");
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun ASSERT(list_empty(&afs_vfsmounts));
224*4882a593Smuzhiyun cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
225*4882a593Smuzhiyun }
226