1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved 4 * Copyright 2005-2006 Ian Kent <raven@themaw.net> 5 */ 6 7 #include <linux/seq_file.h> 8 #include <linux/pagemap.h> 9 10 #include "autofs_i.h" 11 12 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi) 13 { 14 struct autofs_info *ino; 15 16 ino = kzalloc(sizeof(*ino), GFP_KERNEL); 17 if (ino) { 18 INIT_LIST_HEAD(&ino->active); 19 INIT_LIST_HEAD(&ino->expiring); 20 ino->last_used = jiffies; 21 ino->sbi = sbi; 22 ino->exp_timeout = -1; 23 ino->count = 1; 24 } 25 return ino; 26 } 27 28 void autofs_clean_ino(struct autofs_info *ino) 29 { 30 ino->uid = GLOBAL_ROOT_UID; 31 ino->gid = GLOBAL_ROOT_GID; 32 ino->exp_timeout = -1; 33 ino->last_used = jiffies; 34 } 35 36 void autofs_free_ino(struct autofs_info *ino) 37 { 38 kfree_rcu(ino, rcu); 39 } 40 41 void autofs_kill_sb(struct super_block *sb) 42 { 43 struct autofs_sb_info *sbi = autofs_sbi(sb); 44 45 /* 46 * In the event of a failure in get_sb_nodev the superblock 47 * info is not present so nothing else has been setup, so 48 * just call kill_anon_super when we are called from 49 * deactivate_super. 50 */ 51 if (sbi) { 52 /* Free wait queues, close pipe */ 53 autofs_catatonic_mode(sbi); 54 put_pid(sbi->oz_pgrp); 55 } 56 57 pr_debug("shutting down\n"); 58 kill_litter_super(sb); 59 if (sbi) 60 kfree_rcu(sbi, rcu); 61 } 62 63 static int autofs_show_options(struct seq_file *m, struct dentry *root) 64 { 65 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb); 66 struct inode *root_inode = d_inode(root->d_sb->s_root); 67 68 if (!sbi) 69 return 0; 70 71 seq_printf(m, ",fd=%d", sbi->pipefd); 72 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID)) 73 seq_printf(m, ",uid=%u", 74 from_kuid_munged(&init_user_ns, root_inode->i_uid)); 75 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID)) 76 seq_printf(m, ",gid=%u", 77 from_kgid_munged(&init_user_ns, root_inode->i_gid)); 78 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp)); 79 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ); 80 seq_printf(m, ",minproto=%d", sbi->min_proto); 81 seq_printf(m, ",maxproto=%d", sbi->max_proto); 82 83 if (autofs_type_offset(sbi->type)) 84 seq_puts(m, ",offset"); 85 else if (autofs_type_direct(sbi->type)) 86 seq_puts(m, ",direct"); 87 else 88 seq_puts(m, ",indirect"); 89 if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE) 90 seq_puts(m, ",strictexpire"); 91 if (sbi->flags & AUTOFS_SBI_IGNORE) 92 seq_puts(m, ",ignore"); 93 #ifdef CONFIG_CHECKPOINT_RESTORE 94 if (sbi->pipe) 95 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino); 96 else 97 seq_puts(m, ",pipe_ino=-1"); 98 #endif 99 return 0; 100 } 101 102 static void autofs_evict_inode(struct inode *inode) 103 { 104 clear_inode(inode); 105 kfree(inode->i_private); 106 } 107 108 static const struct super_operations autofs_sops = { 109 .statfs = simple_statfs, 110 .show_options = autofs_show_options, 111 .evict_inode = autofs_evict_inode, 112 }; 113 114 enum { 115 Opt_direct, 116 Opt_fd, 117 Opt_gid, 118 Opt_ignore, 119 Opt_indirect, 120 Opt_maxproto, 121 Opt_minproto, 122 Opt_offset, 123 Opt_pgrp, 124 Opt_strictexpire, 125 Opt_uid, 126 }; 127 128 const struct fs_parameter_spec autofs_param_specs[] = { 129 fsparam_flag ("direct", Opt_direct), 130 fsparam_fd ("fd", Opt_fd), 131 fsparam_gid ("gid", Opt_gid), 132 fsparam_flag ("ignore", Opt_ignore), 133 fsparam_flag ("indirect", Opt_indirect), 134 fsparam_u32 ("maxproto", Opt_maxproto), 135 fsparam_u32 ("minproto", Opt_minproto), 136 fsparam_flag ("offset", Opt_offset), 137 fsparam_u32 ("pgrp", Opt_pgrp), 138 fsparam_flag ("strictexpire", Opt_strictexpire), 139 fsparam_uid ("uid", Opt_uid), 140 {} 141 }; 142 143 struct autofs_fs_context { 144 kuid_t uid; 145 kgid_t gid; 146 int pgrp; 147 bool pgrp_set; 148 }; 149 150 /* 151 * Open the fd. We do it here rather than in get_tree so that it's done in the 152 * context of the system call that passed the data and not the one that 153 * triggered the superblock creation, lest the fd gets reassigned. 154 */ 155 static int autofs_parse_fd(struct fs_context *fc, struct autofs_sb_info *sbi, 156 struct fs_parameter *param, 157 struct fs_parse_result *result) 158 { 159 struct file *pipe; 160 int ret; 161 162 if (param->type == fs_value_is_file) { 163 /* came through the new api */ 164 pipe = param->file; 165 param->file = NULL; 166 } else { 167 pipe = fget(result->uint_32); 168 } 169 if (!pipe) { 170 errorf(fc, "could not open pipe file descriptor"); 171 return -EBADF; 172 } 173 174 ret = autofs_check_pipe(pipe); 175 if (ret < 0) { 176 errorf(fc, "Invalid/unusable pipe"); 177 fput(pipe); 178 return -EBADF; 179 } 180 181 autofs_set_packet_pipe_flags(pipe); 182 183 if (sbi->pipe) 184 fput(sbi->pipe); 185 186 sbi->pipefd = result->uint_32; 187 sbi->pipe = pipe; 188 189 return 0; 190 } 191 192 static int autofs_parse_param(struct fs_context *fc, struct fs_parameter *param) 193 { 194 struct autofs_fs_context *ctx = fc->fs_private; 195 struct autofs_sb_info *sbi = fc->s_fs_info; 196 struct fs_parse_result result; 197 int opt; 198 199 opt = fs_parse(fc, autofs_param_specs, param, &result); 200 if (opt < 0) 201 return opt; 202 203 switch (opt) { 204 case Opt_fd: 205 return autofs_parse_fd(fc, sbi, param, &result); 206 case Opt_uid: 207 ctx->uid = result.uid; 208 break; 209 case Opt_gid: 210 ctx->gid = result.gid; 211 break; 212 case Opt_pgrp: 213 ctx->pgrp = result.uint_32; 214 ctx->pgrp_set = true; 215 break; 216 case Opt_minproto: 217 sbi->min_proto = result.uint_32; 218 break; 219 case Opt_maxproto: 220 sbi->max_proto = result.uint_32; 221 break; 222 case Opt_indirect: 223 set_autofs_type_indirect(&sbi->type); 224 break; 225 case Opt_direct: 226 set_autofs_type_direct(&sbi->type); 227 break; 228 case Opt_offset: 229 set_autofs_type_offset(&sbi->type); 230 break; 231 case Opt_strictexpire: 232 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; 233 break; 234 case Opt_ignore: 235 sbi->flags |= AUTOFS_SBI_IGNORE; 236 } 237 238 return 0; 239 } 240 241 static struct autofs_sb_info *autofs_alloc_sbi(void) 242 { 243 struct autofs_sb_info *sbi; 244 245 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 246 if (!sbi) 247 return NULL; 248 249 sbi->magic = AUTOFS_SBI_MAGIC; 250 sbi->flags = AUTOFS_SBI_CATATONIC; 251 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; 252 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; 253 sbi->pipefd = -1; 254 sbi->mnt_ns_id = to_ns_common(current->nsproxy->mnt_ns)->ns_id; 255 256 set_autofs_type_indirect(&sbi->type); 257 mutex_init(&sbi->wq_mutex); 258 mutex_init(&sbi->pipe_mutex); 259 spin_lock_init(&sbi->fs_lock); 260 spin_lock_init(&sbi->lookup_lock); 261 INIT_LIST_HEAD(&sbi->active_list); 262 INIT_LIST_HEAD(&sbi->expiring_list); 263 264 return sbi; 265 } 266 267 static int autofs_validate_protocol(struct fs_context *fc) 268 { 269 struct autofs_sb_info *sbi = fc->s_fs_info; 270 271 /* Test versions first */ 272 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || 273 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { 274 errorf(fc, "kernel does not match daemon version " 275 "daemon (%d, %d) kernel (%d, %d)\n", 276 sbi->min_proto, sbi->max_proto, 277 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); 278 return -EINVAL; 279 } 280 281 /* Establish highest kernel protocol version */ 282 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) 283 sbi->version = AUTOFS_MAX_PROTO_VERSION; 284 else 285 sbi->version = sbi->max_proto; 286 287 switch (sbi->version) { 288 case 4: 289 sbi->sub_version = 7; 290 break; 291 case 5: 292 sbi->sub_version = AUTOFS_PROTO_SUBVERSION; 293 break; 294 default: 295 sbi->sub_version = 0; 296 } 297 298 return 0; 299 } 300 301 static int autofs_fill_super(struct super_block *s, struct fs_context *fc) 302 { 303 struct autofs_fs_context *ctx = fc->fs_private; 304 struct autofs_sb_info *sbi = s->s_fs_info; 305 struct inode *root_inode; 306 struct autofs_info *ino; 307 308 pr_debug("starting up, sbi = %p\n", sbi); 309 310 sbi->sb = s; 311 s->s_blocksize = 1024; 312 s->s_blocksize_bits = 10; 313 s->s_magic = AUTOFS_SUPER_MAGIC; 314 s->s_op = &autofs_sops; 315 set_default_d_op(s, &autofs_dentry_operations); 316 s->s_time_gran = 1; 317 318 /* 319 * Get the root inode and dentry, but defer checking for errors. 320 */ 321 ino = autofs_new_ino(sbi); 322 if (!ino) 323 return -ENOMEM; 324 325 root_inode = autofs_get_inode(s, S_IFDIR | 0755); 326 if (!root_inode) 327 return -ENOMEM; 328 329 root_inode->i_uid = ctx->uid; 330 root_inode->i_gid = ctx->gid; 331 root_inode->i_fop = &autofs_root_operations; 332 root_inode->i_op = &autofs_dir_inode_operations; 333 334 s->s_root = d_make_root(root_inode); 335 if (unlikely(!s->s_root)) { 336 autofs_free_ino(ino); 337 return -ENOMEM; 338 } 339 s->s_root->d_fsdata = ino; 340 341 if (ctx->pgrp_set) { 342 sbi->oz_pgrp = find_get_pid(ctx->pgrp); 343 if (!sbi->oz_pgrp) 344 return invalf(fc, "Could not find process group %d", 345 ctx->pgrp); 346 } else 347 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); 348 349 if (autofs_type_trigger(sbi->type)) 350 /* s->s_root won't be contended so there's little to 351 * be gained by not taking the d_lock when setting 352 * d_flags, even when a lot mounts are being done. 353 */ 354 managed_dentry_set_managed(s->s_root); 355 356 pr_debug("pipe fd = %d, pgrp = %u\n", 357 sbi->pipefd, pid_nr(sbi->oz_pgrp)); 358 359 sbi->flags &= ~AUTOFS_SBI_CATATONIC; 360 return 0; 361 } 362 363 /* 364 * Validate the parameters and then request a superblock. 365 */ 366 static int autofs_get_tree(struct fs_context *fc) 367 { 368 struct autofs_sb_info *sbi = fc->s_fs_info; 369 int ret; 370 371 ret = autofs_validate_protocol(fc); 372 if (ret) 373 return ret; 374 375 if (sbi->pipefd < 0) 376 return invalf(fc, "No control pipe specified"); 377 378 return get_tree_nodev(fc, autofs_fill_super); 379 } 380 381 static void autofs_free_fc(struct fs_context *fc) 382 { 383 struct autofs_fs_context *ctx = fc->fs_private; 384 struct autofs_sb_info *sbi = fc->s_fs_info; 385 386 if (sbi) { 387 if (sbi->pipe) 388 fput(sbi->pipe); 389 kfree(sbi); 390 } 391 kfree(ctx); 392 } 393 394 static const struct fs_context_operations autofs_context_ops = { 395 .free = autofs_free_fc, 396 .parse_param = autofs_parse_param, 397 .get_tree = autofs_get_tree, 398 }; 399 400 /* 401 * Set up the filesystem mount context. 402 */ 403 int autofs_init_fs_context(struct fs_context *fc) 404 { 405 struct autofs_fs_context *ctx; 406 struct autofs_sb_info *sbi; 407 408 ctx = kzalloc(sizeof(struct autofs_fs_context), GFP_KERNEL); 409 if (!ctx) 410 goto nomem; 411 412 ctx->uid = current_uid(); 413 ctx->gid = current_gid(); 414 415 sbi = autofs_alloc_sbi(); 416 if (!sbi) 417 goto nomem_ctx; 418 419 fc->fs_private = ctx; 420 fc->s_fs_info = sbi; 421 fc->ops = &autofs_context_ops; 422 return 0; 423 424 nomem_ctx: 425 kfree(ctx); 426 nomem: 427 return -ENOMEM; 428 } 429 430 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) 431 { 432 struct inode *inode = new_inode(sb); 433 434 if (inode == NULL) 435 return NULL; 436 437 inode->i_mode = mode; 438 if (sb->s_root) { 439 inode->i_uid = d_inode(sb->s_root)->i_uid; 440 inode->i_gid = d_inode(sb->s_root)->i_gid; 441 } 442 simple_inode_init_ts(inode); 443 inode->i_ino = get_next_ino(); 444 445 if (S_ISDIR(mode)) { 446 set_nlink(inode, 2); 447 inode->i_op = &autofs_dir_inode_operations; 448 inode->i_fop = &autofs_dir_operations; 449 } else if (S_ISLNK(mode)) { 450 inode->i_op = &autofs_symlink_inode_operations; 451 } else 452 WARN_ON(1); 453 454 return inode; 455 } 456