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, |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 35 | .listxattr = afs_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 38 | const struct inode_operations afs_autocell_inode_operations = { |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 39 | .getattr = afs_getattr, |
| 40 | }; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static LIST_HEAD(afs_vfsmounts); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 43 | 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] | 44 | |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 45 | static unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 47 | static const char afs_root_volume[] = "root.cell"; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * no valid lookup procedure on this sort of dir |
| 51 | */ |
| 52 | static struct dentry *afs_mntpt_lookup(struct inode *dir, |
| 53 | struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 54 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 56 | _enter("%p,%p{%pd2}", dir, dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return ERR_PTR(-EREMOTE); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* |
| 61 | * no valid open procedure on this sort of dir |
| 62 | */ |
| 63 | static int afs_mntpt_open(struct inode *inode, struct file *file) |
| 64 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 65 | _enter("%p,%p{%pD2}", inode, file, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return -EREMOTE; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 70 | * Set the parameters for the proposed superblock. |
| 71 | */ |
| 72 | static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt) |
| 73 | { |
| 74 | struct afs_fs_context *ctx = fc->fs_private; |
| 75 | struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb); |
| 76 | struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt)); |
| 77 | struct afs_cell *cell; |
| 78 | const char *p; |
| 79 | int ret; |
| 80 | |
| 81 | if (fc->net_ns != src_as->net_ns) { |
| 82 | put_net(fc->net_ns); |
| 83 | fc->net_ns = get_net(src_as->net_ns); |
| 84 | } |
| 85 | |
| 86 | if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) { |
| 87 | ctx->type = AFSVL_RWVOL; |
| 88 | ctx->force = true; |
| 89 | } |
| 90 | if (ctx->cell) { |
| 91 | afs_put_cell(ctx->net, ctx->cell); |
| 92 | ctx->cell = NULL; |
| 93 | } |
| 94 | if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) { |
| 95 | /* if the directory is a pseudo directory, use the d_name */ |
| 96 | unsigned size = mntpt->d_name.len; |
| 97 | |
| 98 | if (size < 2) |
| 99 | return -ENOENT; |
| 100 | |
| 101 | p = mntpt->d_name.name; |
| 102 | if (mntpt->d_name.name[0] == '.') { |
| 103 | size--; |
| 104 | p++; |
| 105 | ctx->type = AFSVL_RWVOL; |
| 106 | ctx->force = true; |
| 107 | } |
| 108 | if (size > AFS_MAXCELLNAME) |
| 109 | return -ENAMETOOLONG; |
| 110 | |
| 111 | cell = afs_lookup_cell(ctx->net, p, size, NULL, false); |
| 112 | if (IS_ERR(cell)) { |
| 113 | pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt); |
| 114 | return PTR_ERR(cell); |
| 115 | } |
| 116 | ctx->cell = cell; |
| 117 | |
| 118 | ctx->volname = afs_root_volume; |
| 119 | ctx->volnamesz = sizeof(afs_root_volume) - 1; |
| 120 | } else { |
| 121 | /* read the contents of the AFS special symlink */ |
| 122 | struct page *page; |
| 123 | loff_t size = i_size_read(d_inode(mntpt)); |
| 124 | char *buf; |
| 125 | |
| 126 | if (src_as->cell) |
| 127 | ctx->cell = afs_get_cell(src_as->cell); |
| 128 | |
David Howells | 158d583 | 2019-12-09 15:04:45 +0000 | [diff] [blame] | 129 | if (size < 2 || size > PAGE_SIZE - 1) |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | |
| 132 | page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL); |
| 133 | if (IS_ERR(page)) |
| 134 | return PTR_ERR(page); |
| 135 | |
| 136 | if (PageError(page)) { |
| 137 | ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt); |
| 138 | put_page(page); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | buf = kmap(page); |
David Howells | 158d583 | 2019-12-09 15:04:45 +0000 | [diff] [blame] | 143 | ret = -EINVAL; |
| 144 | if (buf[size - 1] == '.') |
| 145 | ret = vfs_parse_fs_string(fc, "source", buf, size - 1); |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 146 | kunmap(page); |
| 147 | put_page(page); |
| 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * create a vfsmount to be automounted |
| 157 | */ |
| 158 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) |
| 159 | { |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 160 | struct fs_context *fc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | int ret; |
| 163 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 164 | BUG_ON(!d_inode(mntpt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 166 | fc = fs_context_for_submount(&afs_fs_type, mntpt); |
| 167 | if (IS_ERR(fc)) |
| 168 | return ERR_CAST(fc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 170 | ret = afs_mntpt_set_params(fc, mntpt); |
| 171 | if (!ret) |
| 172 | mnt = fc_mount(fc); |
| 173 | else |
| 174 | mnt = ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
David Howells | c99c217 | 2018-11-01 23:07:27 +0000 | [diff] [blame] | 176 | put_fs_context(fc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return mnt; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 181 | * handle an automount point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | */ |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 183 | struct vfsmount *afs_d_automount(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | struct vfsmount *newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 187 | _enter("{%pd}", path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 189 | newmnt = afs_mntpt_do_automount(path->dentry); |
| 190 | if (IS_ERR(newmnt)) |
| 191 | return newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 193 | mntget(newmnt); /* prevent immediate expiration */ |
| 194 | mnt_set_expiry(newmnt, &afs_vfsmounts); |
| 195 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 196 | afs_mntpt_expiry_timeout * HZ); |
Al Viro | 5ffc283 | 2011-11-25 02:22:06 -0500 | [diff] [blame] | 197 | _leave(" = %p", newmnt); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 198 | return newmnt; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* |
| 202 | * handle mountpoint expiry timer going off |
| 203 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 204 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 206 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 208 | if (!list_empty(&afs_vfsmounts)) { |
| 209 | mark_mounts_for_expiry(&afs_vfsmounts); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 210 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 211 | afs_mntpt_expiry_timeout * HZ); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 214 | _leave(""); |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 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)); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 225 | cancel_delayed_work_sync(&afs_mntpt_expiry_timer); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 226 | } |