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