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