1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
7 #include <linux/seq_file.h>
8 #include <linux/pagemap.h>
12 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
14 struct autofs_info *ino;
16 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
18 INIT_LIST_HEAD(&ino->active);
19 INIT_LIST_HEAD(&ino->expiring);
20 ino->last_used = jiffies;
27 void autofs_clean_ino(struct autofs_info *ino)
29 ino->uid = GLOBAL_ROOT_UID;
30 ino->gid = GLOBAL_ROOT_GID;
31 ino->last_used = jiffies;
34 void autofs_free_ino(struct autofs_info *ino)
39 void autofs_kill_sb(struct super_block *sb)
41 struct autofs_sb_info *sbi = autofs_sbi(sb);
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
50 /* Free wait queues, close pipe */
51 autofs_catatonic_mode(sbi);
52 put_pid(sbi->oz_pgrp);
55 pr_debug("shutting down\n");
56 kill_litter_super(sb);
61 static int autofs_show_options(struct seq_file *m, struct dentry *root)
63 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
64 struct inode *root_inode = d_inode(root->d_sb->s_root);
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);
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");
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
93 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
95 seq_puts(m, ",pipe_ino=-1");
100 static void autofs_evict_inode(struct inode *inode)
103 kfree(inode->i_private);
106 static const struct super_operations autofs_sops = {
107 .statfs = simple_statfs,
108 .show_options = autofs_show_options,
109 .evict_inode = autofs_evict_inode,
126 const struct fs_parameter_spec autofs_param_specs[] = {
127 fsparam_flag ("direct", Opt_direct),
128 fsparam_fd ("fd", Opt_fd),
129 fsparam_u32 ("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_u32 ("uid", Opt_uid),
141 struct autofs_fs_context {
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.
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)
160 if (param->type == fs_value_is_file) {
161 /* came through the new api */
165 pipe = fget(result->uint_32);
168 errorf(fc, "could not open pipe file descriptor");
172 ret = autofs_check_pipe(pipe);
174 errorf(fc, "Invalid/unusable pipe");
175 if (param->type != fs_value_is_file)
180 autofs_set_packet_pipe_flags(pipe);
185 sbi->pipefd = result->uint_32;
191 static int autofs_parse_param(struct fs_context *fc, struct fs_parameter *param)
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;
200 opt = fs_parse(fc, autofs_param_specs, param, &result);
206 return autofs_parse_fd(fc, sbi, param, &result);
208 uid = make_kuid(current_user_ns(), result.uint_32);
210 return invalfc(fc, "Invalid uid");
214 gid = make_kgid(current_user_ns(), result.uint_32);
216 return invalfc(fc, "Invalid gid");
220 ctx->pgrp = result.uint_32;
221 ctx->pgrp_set = true;
224 sbi->min_proto = result.uint_32;
227 sbi->max_proto = result.uint_32;
230 set_autofs_type_indirect(&sbi->type);
233 set_autofs_type_direct(&sbi->type);
236 set_autofs_type_offset(&sbi->type);
238 case Opt_strictexpire:
239 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
242 sbi->flags |= AUTOFS_SBI_IGNORE;
248 static struct autofs_sb_info *autofs_alloc_sbi(void)
250 struct autofs_sb_info *sbi;
252 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
256 sbi->magic = AUTOFS_SBI_MAGIC;
257 sbi->flags = AUTOFS_SBI_CATATONIC;
258 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
259 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
262 set_autofs_type_indirect(&sbi->type);
263 mutex_init(&sbi->wq_mutex);
264 mutex_init(&sbi->pipe_mutex);
265 spin_lock_init(&sbi->fs_lock);
266 spin_lock_init(&sbi->lookup_lock);
267 INIT_LIST_HEAD(&sbi->active_list);
268 INIT_LIST_HEAD(&sbi->expiring_list);
273 static int autofs_validate_protocol(struct fs_context *fc)
275 struct autofs_sb_info *sbi = fc->s_fs_info;
277 /* Test versions first */
278 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
279 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
280 errorf(fc, "kernel does not match daemon version "
281 "daemon (%d, %d) kernel (%d, %d)\n",
282 sbi->min_proto, sbi->max_proto,
283 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
287 /* Establish highest kernel protocol version */
288 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
289 sbi->version = AUTOFS_MAX_PROTO_VERSION;
291 sbi->version = sbi->max_proto;
293 switch (sbi->version) {
295 sbi->sub_version = 7;
298 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
301 sbi->sub_version = 0;
307 static int autofs_fill_super(struct super_block *s, struct fs_context *fc)
309 struct autofs_fs_context *ctx = fc->fs_private;
310 struct autofs_sb_info *sbi = s->s_fs_info;
311 struct inode *root_inode;
312 struct autofs_info *ino;
314 pr_debug("starting up, sbi = %p\n", sbi);
317 s->s_blocksize = 1024;
318 s->s_blocksize_bits = 10;
319 s->s_magic = AUTOFS_SUPER_MAGIC;
320 s->s_op = &autofs_sops;
321 s->s_d_op = &autofs_dentry_operations;
325 * Get the root inode and dentry, but defer checking for errors.
327 ino = autofs_new_ino(sbi);
331 root_inode = autofs_get_inode(s, S_IFDIR | 0755);
335 root_inode->i_uid = ctx->uid;
336 root_inode->i_gid = ctx->gid;
337 root_inode->i_fop = &autofs_root_operations;
338 root_inode->i_op = &autofs_dir_inode_operations;
340 s->s_root = d_make_root(root_inode);
341 if (unlikely(!s->s_root)) {
342 autofs_free_ino(ino);
345 s->s_root->d_fsdata = ino;
348 sbi->oz_pgrp = find_get_pid(ctx->pgrp);
350 return invalf(fc, "Could not find process group %d",
353 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
355 if (autofs_type_trigger(sbi->type))
356 /* s->s_root won't be contended so there's little to
357 * be gained by not taking the d_lock when setting
358 * d_flags, even when a lot mounts are being done.
360 managed_dentry_set_managed(s->s_root);
362 pr_debug("pipe fd = %d, pgrp = %u\n",
363 sbi->pipefd, pid_nr(sbi->oz_pgrp));
365 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
370 * Validate the parameters and then request a superblock.
372 static int autofs_get_tree(struct fs_context *fc)
374 struct autofs_sb_info *sbi = fc->s_fs_info;
377 ret = autofs_validate_protocol(fc);
382 return invalf(fc, "No control pipe specified");
384 return get_tree_nodev(fc, autofs_fill_super);
387 static void autofs_free_fc(struct fs_context *fc)
389 struct autofs_fs_context *ctx = fc->fs_private;
390 struct autofs_sb_info *sbi = fc->s_fs_info;
400 static const struct fs_context_operations autofs_context_ops = {
401 .free = autofs_free_fc,
402 .parse_param = autofs_parse_param,
403 .get_tree = autofs_get_tree,
407 * Set up the filesystem mount context.
409 int autofs_init_fs_context(struct fs_context *fc)
411 struct autofs_fs_context *ctx;
412 struct autofs_sb_info *sbi;
414 ctx = kzalloc(sizeof(struct autofs_fs_context), GFP_KERNEL);
418 ctx->uid = current_uid();
419 ctx->gid = current_gid();
421 sbi = autofs_alloc_sbi();
425 fc->fs_private = ctx;
427 fc->ops = &autofs_context_ops;
436 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
438 struct inode *inode = new_inode(sb);
443 inode->i_mode = mode;
445 inode->i_uid = d_inode(sb->s_root)->i_uid;
446 inode->i_gid = d_inode(sb->s_root)->i_gid;
448 simple_inode_init_ts(inode);
449 inode->i_ino = get_next_ino();
453 inode->i_op = &autofs_dir_inode_operations;
454 inode->i_fop = &autofs_dir_operations;
455 } else if (S_ISLNK(mode)) {
456 inode->i_op = &autofs_symlink_inode_operations;