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