Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
| 3 | * Copyright 2005-2006 Ian Kent <raven@themaw.net> |
| 4 | * |
| 5 | * This file is part of the Linux kernel and is made available under |
| 6 | * the terms of the GNU General Public License, version 2, or at your |
| 7 | * option, any later version, incorporated herein by reference. |
| 8 | */ |
| 9 | |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/parser.h> |
Ian Kent | 6471e93 | 2018-06-07 17:11:48 -0700 | [diff] [blame] | 13 | |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 14 | #include "autofs_i.h" |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 15 | |
| 16 | struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi) |
| 17 | { |
| 18 | struct autofs_info *ino; |
| 19 | |
| 20 | ino = kzalloc(sizeof(*ino), GFP_KERNEL); |
| 21 | if (ino) { |
| 22 | INIT_LIST_HEAD(&ino->active); |
| 23 | INIT_LIST_HEAD(&ino->expiring); |
| 24 | ino->last_used = jiffies; |
| 25 | ino->sbi = sbi; |
| 26 | } |
| 27 | return ino; |
| 28 | } |
| 29 | |
| 30 | void autofs_clean_ino(struct autofs_info *ino) |
| 31 | { |
| 32 | ino->uid = GLOBAL_ROOT_UID; |
| 33 | ino->gid = GLOBAL_ROOT_GID; |
| 34 | ino->last_used = jiffies; |
| 35 | } |
| 36 | |
| 37 | void autofs_free_ino(struct autofs_info *ino) |
| 38 | { |
| 39 | kfree(ino); |
| 40 | } |
| 41 | |
| 42 | void autofs_kill_sb(struct super_block *sb) |
| 43 | { |
| 44 | struct autofs_sb_info *sbi = autofs_sbi(sb); |
| 45 | |
| 46 | /* |
| 47 | * In the event of a failure in get_sb_nodev the superblock |
| 48 | * info is not present so nothing else has been setup, so |
| 49 | * just call kill_anon_super when we are called from |
| 50 | * deactivate_super. |
| 51 | */ |
| 52 | if (sbi) { |
| 53 | /* Free wait queues, close pipe */ |
| 54 | autofs_catatonic_mode(sbi); |
| 55 | put_pid(sbi->oz_pgrp); |
| 56 | } |
| 57 | |
| 58 | pr_debug("shutting down\n"); |
| 59 | kill_litter_super(sb); |
| 60 | if (sbi) |
| 61 | kfree_rcu(sbi, rcu); |
| 62 | } |
| 63 | |
| 64 | static int autofs_show_options(struct seq_file *m, struct dentry *root) |
| 65 | { |
| 66 | struct autofs_sb_info *sbi = autofs_sbi(root->d_sb); |
| 67 | struct inode *root_inode = d_inode(root->d_sb->s_root); |
| 68 | |
| 69 | if (!sbi) |
| 70 | return 0; |
| 71 | |
| 72 | seq_printf(m, ",fd=%d", sbi->pipefd); |
| 73 | if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID)) |
| 74 | seq_printf(m, ",uid=%u", |
| 75 | from_kuid_munged(&init_user_ns, root_inode->i_uid)); |
| 76 | if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID)) |
| 77 | seq_printf(m, ",gid=%u", |
| 78 | from_kgid_munged(&init_user_ns, root_inode->i_gid)); |
| 79 | seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp)); |
| 80 | seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ); |
| 81 | seq_printf(m, ",minproto=%d", sbi->min_proto); |
| 82 | seq_printf(m, ",maxproto=%d", sbi->max_proto); |
| 83 | |
| 84 | if (autofs_type_offset(sbi->type)) |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 85 | seq_puts(m, ",offset"); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 86 | else if (autofs_type_direct(sbi->type)) |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 87 | seq_puts(m, ",direct"); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 88 | else |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 89 | seq_puts(m, ",indirect"); |
Ian Kent | f516221 | 2019-01-03 15:27:43 -0800 | [diff] [blame] | 90 | if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE) |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 91 | seq_puts(m, ",strictexpire"); |
Ian Kent | 60d6d04 | 2019-03-07 16:29:09 -0800 | [diff] [blame] | 92 | if (sbi->flags & AUTOFS_SBI_IGNORE) |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 93 | seq_puts(m, ",ignore"); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 94 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 95 | if (sbi->pipe) |
| 96 | seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino); |
| 97 | else |
Ian Kent | 874d22d | 2019-03-07 16:29:12 -0800 | [diff] [blame^] | 98 | seq_puts(m, ",pipe_ino=-1"); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 99 | #endif |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static void autofs_evict_inode(struct inode *inode) |
| 104 | { |
| 105 | clear_inode(inode); |
| 106 | kfree(inode->i_private); |
| 107 | } |
| 108 | |
| 109 | static const struct super_operations autofs_sops = { |
| 110 | .statfs = simple_statfs, |
| 111 | .show_options = autofs_show_options, |
| 112 | .evict_inode = autofs_evict_inode, |
| 113 | }; |
| 114 | |
| 115 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, |
Ian Kent | 60d6d04 | 2019-03-07 16:29:09 -0800 | [diff] [blame] | 116 | Opt_indirect, Opt_direct, Opt_offset, Opt_strictexpire, |
| 117 | Opt_ignore}; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 118 | |
| 119 | static const match_table_t tokens = { |
| 120 | {Opt_fd, "fd=%u"}, |
| 121 | {Opt_uid, "uid=%u"}, |
| 122 | {Opt_gid, "gid=%u"}, |
| 123 | {Opt_pgrp, "pgrp=%u"}, |
| 124 | {Opt_minproto, "minproto=%u"}, |
| 125 | {Opt_maxproto, "maxproto=%u"}, |
| 126 | {Opt_indirect, "indirect"}, |
| 127 | {Opt_direct, "direct"}, |
| 128 | {Opt_offset, "offset"}, |
Ian Kent | f516221 | 2019-01-03 15:27:43 -0800 | [diff] [blame] | 129 | {Opt_strictexpire, "strictexpire"}, |
Ian Kent | 60d6d04 | 2019-03-07 16:29:09 -0800 | [diff] [blame] | 130 | {Opt_ignore, "ignore"}, |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 131 | {Opt_err, NULL} |
| 132 | }; |
| 133 | |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 134 | static int parse_options(char *options, |
| 135 | struct inode *root, int *pgrp, bool *pgrp_set, |
| 136 | struct autofs_sb_info *sbi) |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 137 | { |
| 138 | char *p; |
| 139 | substring_t args[MAX_OPT_ARGS]; |
| 140 | int option; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 141 | int pipefd = -1; |
| 142 | kuid_t uid; |
| 143 | kgid_t gid; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 144 | |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 145 | root->i_uid = current_uid(); |
| 146 | root->i_gid = current_gid(); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 147 | |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 148 | sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; |
| 149 | sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 150 | |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 151 | sbi->pipefd = -1; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 152 | |
| 153 | if (!options) |
| 154 | return 1; |
| 155 | |
| 156 | while ((p = strsep(&options, ",")) != NULL) { |
| 157 | int token; |
| 158 | |
| 159 | if (!*p) |
| 160 | continue; |
| 161 | |
| 162 | token = match_token(p, tokens, args); |
| 163 | switch (token) { |
| 164 | case Opt_fd: |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 165 | if (match_int(args, &pipefd)) |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 166 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 167 | sbi->pipefd = pipefd; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 168 | break; |
| 169 | case Opt_uid: |
| 170 | if (match_int(args, &option)) |
| 171 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 172 | uid = make_kuid(current_user_ns(), option); |
| 173 | if (!uid_valid(uid)) |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 174 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 175 | root->i_uid = uid; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 176 | break; |
| 177 | case Opt_gid: |
| 178 | if (match_int(args, &option)) |
| 179 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 180 | gid = make_kgid(current_user_ns(), option); |
| 181 | if (!gid_valid(gid)) |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 182 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 183 | root->i_gid = gid; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 184 | break; |
| 185 | case Opt_pgrp: |
| 186 | if (match_int(args, &option)) |
| 187 | return 1; |
| 188 | *pgrp = option; |
| 189 | *pgrp_set = true; |
| 190 | break; |
| 191 | case Opt_minproto: |
| 192 | if (match_int(args, &option)) |
| 193 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 194 | sbi->min_proto = option; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 195 | break; |
| 196 | case Opt_maxproto: |
| 197 | if (match_int(args, &option)) |
| 198 | return 1; |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 199 | sbi->max_proto = option; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 200 | break; |
| 201 | case Opt_indirect: |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 202 | set_autofs_type_indirect(&sbi->type); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 203 | break; |
| 204 | case Opt_direct: |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 205 | set_autofs_type_direct(&sbi->type); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 206 | break; |
| 207 | case Opt_offset: |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 208 | set_autofs_type_offset(&sbi->type); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 209 | break; |
Ian Kent | f516221 | 2019-01-03 15:27:43 -0800 | [diff] [blame] | 210 | case Opt_strictexpire: |
| 211 | sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; |
| 212 | break; |
Ian Kent | 60d6d04 | 2019-03-07 16:29:09 -0800 | [diff] [blame] | 213 | case Opt_ignore: |
| 214 | sbi->flags |= AUTOFS_SBI_IGNORE; |
| 215 | break; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 216 | default: |
| 217 | return 1; |
| 218 | } |
| 219 | } |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 220 | return (sbi->pipefd < 0); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | int autofs_fill_super(struct super_block *s, void *data, int silent) |
| 224 | { |
| 225 | struct inode *root_inode; |
| 226 | struct dentry *root; |
| 227 | struct file *pipe; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 228 | struct autofs_sb_info *sbi; |
| 229 | struct autofs_info *ino; |
| 230 | int pgrp = 0; |
| 231 | bool pgrp_set = false; |
| 232 | int ret = -EINVAL; |
| 233 | |
| 234 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
| 235 | if (!sbi) |
| 236 | return -ENOMEM; |
| 237 | pr_debug("starting up, sbi = %p\n", sbi); |
| 238 | |
| 239 | s->s_fs_info = sbi; |
| 240 | sbi->magic = AUTOFS_SBI_MAGIC; |
| 241 | sbi->pipefd = -1; |
| 242 | sbi->pipe = NULL; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 243 | sbi->exp_timeout = 0; |
| 244 | sbi->oz_pgrp = NULL; |
| 245 | sbi->sb = s; |
| 246 | sbi->version = 0; |
| 247 | sbi->sub_version = 0; |
Ian Kent | 9d8719a | 2019-01-03 15:27:39 -0800 | [diff] [blame] | 248 | sbi->flags = AUTOFS_SBI_CATATONIC; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 249 | set_autofs_type_indirect(&sbi->type); |
| 250 | sbi->min_proto = 0; |
| 251 | sbi->max_proto = 0; |
| 252 | mutex_init(&sbi->wq_mutex); |
| 253 | mutex_init(&sbi->pipe_mutex); |
| 254 | spin_lock_init(&sbi->fs_lock); |
| 255 | sbi->queues = NULL; |
| 256 | spin_lock_init(&sbi->lookup_lock); |
| 257 | INIT_LIST_HEAD(&sbi->active_list); |
| 258 | INIT_LIST_HEAD(&sbi->expiring_list); |
| 259 | s->s_blocksize = 1024; |
| 260 | s->s_blocksize_bits = 10; |
| 261 | s->s_magic = AUTOFS_SUPER_MAGIC; |
| 262 | s->s_op = &autofs_sops; |
| 263 | s->s_d_op = &autofs_dentry_operations; |
| 264 | s->s_time_gran = 1; |
| 265 | |
| 266 | /* |
| 267 | * Get the root inode and dentry, but defer checking for errors. |
| 268 | */ |
| 269 | ino = autofs_new_ino(sbi); |
| 270 | if (!ino) { |
| 271 | ret = -ENOMEM; |
| 272 | goto fail_free; |
| 273 | } |
| 274 | root_inode = autofs_get_inode(s, S_IFDIR | 0755); |
| 275 | root = d_make_root(root_inode); |
Ian Kent | f585b28 | 2019-02-01 14:21:29 -0800 | [diff] [blame] | 276 | if (!root) { |
| 277 | ret = -ENOMEM; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 278 | goto fail_ino; |
Ian Kent | f585b28 | 2019-02-01 14:21:29 -0800 | [diff] [blame] | 279 | } |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 280 | pipe = NULL; |
| 281 | |
| 282 | root->d_fsdata = ino; |
| 283 | |
| 284 | /* Can this call block? */ |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 285 | if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) { |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 286 | pr_err("called with bogus options\n"); |
| 287 | goto fail_dput; |
| 288 | } |
| 289 | |
| 290 | /* Test versions first */ |
| 291 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || |
| 292 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { |
| 293 | pr_err("kernel does not match daemon version " |
| 294 | "daemon (%d, %d) kernel (%d, %d)\n", |
| 295 | sbi->min_proto, sbi->max_proto, |
| 296 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); |
| 297 | goto fail_dput; |
| 298 | } |
| 299 | |
| 300 | /* Establish highest kernel protocol version */ |
| 301 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) |
| 302 | sbi->version = AUTOFS_MAX_PROTO_VERSION; |
| 303 | else |
| 304 | sbi->version = sbi->max_proto; |
| 305 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; |
| 306 | |
| 307 | if (pgrp_set) { |
| 308 | sbi->oz_pgrp = find_get_pid(pgrp); |
| 309 | if (!sbi->oz_pgrp) { |
| 310 | pr_err("could not find process group %d\n", |
| 311 | pgrp); |
| 312 | goto fail_dput; |
| 313 | } |
| 314 | } else { |
| 315 | sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); |
| 316 | } |
| 317 | |
| 318 | if (autofs_type_trigger(sbi->type)) |
| 319 | __managed_dentry_set_managed(root); |
| 320 | |
| 321 | root_inode->i_fop = &autofs_root_operations; |
| 322 | root_inode->i_op = &autofs_dir_inode_operations; |
| 323 | |
Ian Kent | 9bf964c | 2019-01-03 15:27:36 -0800 | [diff] [blame] | 324 | pr_debug("pipe fd = %d, pgrp = %u\n", |
| 325 | sbi->pipefd, pid_nr(sbi->oz_pgrp)); |
| 326 | pipe = fget(sbi->pipefd); |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 327 | |
| 328 | if (!pipe) { |
| 329 | pr_err("could not open pipe file descriptor\n"); |
| 330 | goto fail_put_pid; |
| 331 | } |
| 332 | ret = autofs_prepare_pipe(pipe); |
| 333 | if (ret < 0) |
| 334 | goto fail_fput; |
| 335 | sbi->pipe = pipe; |
Ian Kent | 9d8719a | 2019-01-03 15:27:39 -0800 | [diff] [blame] | 336 | sbi->flags &= ~AUTOFS_SBI_CATATONIC; |
Ian Kent | ebc921c | 2018-06-07 17:11:13 -0700 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Success! Install the root dentry now to indicate completion. |
| 340 | */ |
| 341 | s->s_root = root; |
| 342 | return 0; |
| 343 | |
| 344 | /* |
| 345 | * Failure ... clean up. |
| 346 | */ |
| 347 | fail_fput: |
| 348 | pr_err("pipe file descriptor does not contain proper ops\n"); |
| 349 | fput(pipe); |
| 350 | fail_put_pid: |
| 351 | put_pid(sbi->oz_pgrp); |
| 352 | fail_dput: |
| 353 | dput(root); |
| 354 | goto fail_free; |
| 355 | fail_ino: |
| 356 | autofs_free_ino(ino); |
| 357 | fail_free: |
| 358 | kfree(sbi); |
| 359 | s->s_fs_info = NULL; |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) |
| 364 | { |
| 365 | struct inode *inode = new_inode(sb); |
| 366 | |
| 367 | if (inode == NULL) |
| 368 | return NULL; |
| 369 | |
| 370 | inode->i_mode = mode; |
| 371 | if (sb->s_root) { |
| 372 | inode->i_uid = d_inode(sb->s_root)->i_uid; |
| 373 | inode->i_gid = d_inode(sb->s_root)->i_gid; |
| 374 | } |
| 375 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
| 376 | inode->i_ino = get_next_ino(); |
| 377 | |
| 378 | if (S_ISDIR(mode)) { |
| 379 | set_nlink(inode, 2); |
| 380 | inode->i_op = &autofs_dir_inode_operations; |
| 381 | inode->i_fop = &autofs_dir_operations; |
| 382 | } else if (S_ISLNK(mode)) { |
| 383 | inode->i_op = &autofs_symlink_inode_operations; |
| 384 | } else |
| 385 | WARN_ON(1); |
| 386 | |
| 387 | return inode; |
| 388 | } |