Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 2 | /* mountpoint management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/mount.h> |
| 14 | #include <linux/namei.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
David Howells | 13fcc68 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 16 | #include <linux/fs_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
| 19 | |
| 20 | static struct dentry *afs_mntpt_lookup(struct inode *dir, |
| 21 | struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 22 | unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | static int afs_mntpt_open(struct inode *inode, struct file *file); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 24 | static void afs_mntpt_expiry_timed_out(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 26 | const struct file_operations afs_mntpt_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | .open = afs_mntpt_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 28 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 31 | const struct inode_operations afs_mntpt_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | .lookup = afs_mntpt_lookup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | .readlink = page_readlink, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 34 | .getattr = afs_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 37 | const struct inode_operations afs_autocell_inode_operations = { |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 38 | .getattr = afs_getattr, |
| 39 | }; |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static LIST_HEAD(afs_vfsmounts); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 42 | static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 44 | static unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 46 | static const char afs_root_volume[] = "root.cell"; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 53 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 55 | _enter("%p,%p{%pd2}", dir, dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return ERR_PTR(-EREMOTE); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 57 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 64 | _enter("%p,%p{%pD2}", inode, file, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return -EREMOTE; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 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(mntpt->d_sb); |
| 75 | struct afs_vnode *vnode = AFS_FS_I(d_inode(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(fc->net_ns); |
| 82 | fc->net_ns = get_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) { |
David Howells | dca54a7 | 2020-10-13 20:51:59 +0100 | [diff] [blame] | 90 | afs_unuse_cell(ctx->net, ctx->cell, afs_cell_trace_unuse_mntpt); |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 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(ctx->net, p, size, NULL, false); |
| 111 | if (IS_ERR(cell)) { |
| 112 | pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt); |
| 113 | return PTR_ERR(cell); |
| 114 | } |
| 115 | ctx->cell = cell; |
| 116 | |
| 117 | ctx->volname = afs_root_volume; |
| 118 | ctx->volnamesz = sizeof(afs_root_volume) - 1; |
| 119 | } else { |
| 120 | /* read the contents of the AFS special symlink */ |
| 121 | struct page *page; |
| 122 | loff_t size = i_size_read(d_inode(mntpt)); |
| 123 | char *buf; |
| 124 | |
| 125 | if (src_as->cell) |
David Howells | dca54a7 | 2020-10-13 20:51:59 +0100 | [diff] [blame] | 126 | ctx->cell = afs_use_cell(src_as->cell, afs_cell_trace_use_mntpt); |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 127 | |
David Howells | 158d583 | 2019-12-09 15:04:45 +0000 | [diff] [blame] | 128 | if (size < 2 || size > PAGE_SIZE - 1) |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | |
| 131 | page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL); |
| 132 | if (IS_ERR(page)) |
| 133 | return PTR_ERR(page); |
| 134 | |
| 135 | if (PageError(page)) { |
| 136 | ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt); |
| 137 | put_page(page); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | buf = kmap(page); |
David Howells | 158d583 | 2019-12-09 15:04:45 +0000 | [diff] [blame] | 142 | ret = -EINVAL; |
| 143 | if (buf[size - 1] == '.') |
| 144 | ret = vfs_parse_fs_string(fc, "source", buf, size - 1); |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 145 | kunmap(page); |
| 146 | put_page(page); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | * create a vfsmount to be automounted |
| 156 | */ |
| 157 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) |
| 158 | { |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 159 | struct fs_context *fc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | int ret; |
| 162 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 163 | BUG_ON(!d_inode(mntpt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 165 | fc = fs_context_for_submount(&afs_fs_type, mntpt); |
| 166 | if (IS_ERR(fc)) |
| 167 | return ERR_CAST(fc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 169 | ret = afs_mntpt_set_params(fc, mntpt); |
| 170 | if (!ret) |
| 171 | mnt = fc_mount(fc); |
| 172 | else |
| 173 | mnt = ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 175 | put_fs_context(fc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return mnt; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 180 | * handle an automount point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | */ |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 182 | struct vfsmount *afs_d_automount(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct vfsmount *newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 186 | _enter("{%pd}", path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 188 | newmnt = afs_mntpt_do_automount(path->dentry); |
| 189 | if (IS_ERR(newmnt)) |
| 190 | return newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 192 | mntget(newmnt); /* prevent immediate expiration */ |
| 193 | mnt_set_expiry(newmnt, &afs_vfsmounts); |
| 194 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 195 | afs_mntpt_expiry_timeout * HZ); |
Al Viro | 5ffc283 | 2011-11-25 02:22:06 -0500 | [diff] [blame] | 196 | _leave(" = %p", newmnt); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 197 | return newmnt; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* |
| 201 | * handle mountpoint expiry timer going off |
| 202 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 203 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 205 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 207 | if (!list_empty(&afs_vfsmounts)) { |
| 208 | mark_mounts_for_expiry(&afs_vfsmounts); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 209 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 210 | afs_mntpt_expiry_timeout * HZ); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 213 | _leave(""); |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 216 | /* |
| 217 | * kill the AFS mountpoint timer if it's still running |
| 218 | */ |
| 219 | void afs_mntpt_kill_timer(void) |
| 220 | { |
| 221 | _enter(""); |
| 222 | |
| 223 | ASSERT(list_empty(&afs_vfsmounts)); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 224 | cancel_delayed_work_sync(&afs_mntpt_expiry_timer); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 225 | } |