1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* mountpoint management
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
12 #include <linux/pagemap.h>
13 #include <linux/mount.h>
14 #include <linux/namei.h>
15 #include <linux/gfp.h>
16 #include <linux/fs_context.h>
20 static struct dentry *afs_mntpt_lookup(struct inode *dir,
21 struct dentry *dentry,
23 static int afs_mntpt_open(struct inode *inode, struct file *file);
24 static void afs_mntpt_expiry_timed_out(struct work_struct *work);
26 const struct file_operations afs_mntpt_file_operations = {
27 .open = afs_mntpt_open,
28 .llseek = noop_llseek,
31 const struct inode_operations afs_mntpt_inode_operations = {
32 .lookup = afs_mntpt_lookup,
33 .readlink = page_readlink,
34 .getattr = afs_getattr,
37 const struct inode_operations afs_autocell_inode_operations = {
38 .getattr = afs_getattr,
41 static LIST_HEAD(afs_vfsmounts);
42 static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
44 static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
46 static const char afs_root_volume[] = "root.cell";
49 * no valid lookup procedure on this sort of dir
51 static struct dentry *afs_mntpt_lookup(struct inode *dir,
52 struct dentry *dentry,
55 _enter("%p,%p{%pd2}", dir, dentry, dentry);
56 return ERR_PTR(-EREMOTE);
60 * no valid open procedure on this sort of dir
62 static int afs_mntpt_open(struct inode *inode, struct file *file)
64 _enter("%p,%p{%pD2}", inode, file, file);
69 * Set the parameters for the proposed superblock.
71 static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
73 struct afs_fs_context *ctx = fc->fs_private;
74 struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
75 struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
76 struct afs_cell *cell;
80 if (fc->net_ns != src_as->net_ns) {
82 fc->net_ns = get_net(src_as->net_ns);
85 if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
86 ctx->type = AFSVL_RWVOL;
90 afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt);
93 if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
94 /* if the directory is a pseudo directory, use the d_name */
95 unsigned size = mntpt->d_name.len;
100 p = mntpt->d_name.name;
101 if (mntpt->d_name.name[0] == '.') {
104 ctx->type = AFSVL_RWVOL;
107 if (size > AFS_MAXCELLNAME)
108 return -ENAMETOOLONG;
110 cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
112 pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
113 return PTR_ERR(cell);
117 ctx->volname = afs_root_volume;
118 ctx->volnamesz = sizeof(afs_root_volume) - 1;
120 /* read the contents of the AFS special symlink */
122 loff_t size = i_size_read(d_inode(mntpt));
126 ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt);
128 if (size < 2 || size > PAGE_SIZE - 1)
131 page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
133 return PTR_ERR(page);
137 if (buf[size - 1] == '.')
138 ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
149 * create a vfsmount to be automounted
151 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
153 struct fs_context *fc;
154 struct vfsmount *mnt;
157 BUG_ON(!d_inode(mntpt));
159 fc = fs_context_for_submount(&afs_fs_type, mntpt);
163 ret = afs_mntpt_set_params(fc, mntpt);
174 * handle an automount point
176 struct vfsmount *afs_d_automount(struct path *path)
178 struct vfsmount *newmnt;
180 _enter("{%pd}", path->dentry);
182 newmnt = afs_mntpt_do_automount(path->dentry);
186 mntget(newmnt); /* prevent immediate expiration */
187 mnt_set_expiry(newmnt, &afs_vfsmounts);
188 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
189 afs_mntpt_expiry_timeout * HZ);
190 _leave(" = %p", newmnt);
195 * handle mountpoint expiry timer going off
197 static void afs_mntpt_expiry_timed_out(struct work_struct *work)
201 if (!list_empty(&afs_vfsmounts)) {
202 mark_mounts_for_expiry(&afs_vfsmounts);
203 queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
204 afs_mntpt_expiry_timeout * HZ);
211 * kill the AFS mountpoint timer if it's still running
213 void afs_mntpt_kill_timer(void)
217 ASSERT(list_empty(&afs_vfsmounts));
218 cancel_delayed_work_sync(&afs_mntpt_expiry_timer);