1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
7 #include <linux/seq_file.h>
8 #include <linux/pagemap.h>
9 #include <linux/parser.h>
13 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
15 struct autofs_info *ino;
17 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
19 INIT_LIST_HEAD(&ino->active);
20 INIT_LIST_HEAD(&ino->expiring);
21 ino->last_used = jiffies;
28 void autofs_clean_ino(struct autofs_info *ino)
30 ino->uid = GLOBAL_ROOT_UID;
31 ino->gid = GLOBAL_ROOT_GID;
32 ino->last_used = jiffies;
35 void autofs_free_ino(struct autofs_info *ino)
40 void autofs_kill_sb(struct super_block *sb)
42 struct autofs_sb_info *sbi = autofs_sbi(sb);
45 * In the event of a failure in get_sb_nodev the superblock
46 * info is not present so nothing else has been setup, so
47 * just call kill_anon_super when we are called from
51 /* Free wait queues, close pipe */
52 autofs_catatonic_mode(sbi);
53 put_pid(sbi->oz_pgrp);
56 pr_debug("shutting down\n");
57 kill_litter_super(sb);
62 static int autofs_show_options(struct seq_file *m, struct dentry *root)
64 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
65 struct inode *root_inode = d_inode(root->d_sb->s_root);
70 seq_printf(m, ",fd=%d", sbi->pipefd);
71 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
72 seq_printf(m, ",uid=%u",
73 from_kuid_munged(&init_user_ns, root_inode->i_uid));
74 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
75 seq_printf(m, ",gid=%u",
76 from_kgid_munged(&init_user_ns, root_inode->i_gid));
77 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
78 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
79 seq_printf(m, ",minproto=%d", sbi->min_proto);
80 seq_printf(m, ",maxproto=%d", sbi->max_proto);
82 if (autofs_type_offset(sbi->type))
83 seq_puts(m, ",offset");
84 else if (autofs_type_direct(sbi->type))
85 seq_puts(m, ",direct");
87 seq_puts(m, ",indirect");
88 if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE)
89 seq_puts(m, ",strictexpire");
90 if (sbi->flags & AUTOFS_SBI_IGNORE)
91 seq_puts(m, ",ignore");
92 #ifdef CONFIG_CHECKPOINT_RESTORE
94 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
96 seq_puts(m, ",pipe_ino=-1");
101 static void autofs_evict_inode(struct inode *inode)
104 kfree(inode->i_private);
107 static const struct super_operations autofs_sops = {
108 .statfs = simple_statfs,
109 .show_options = autofs_show_options,
110 .evict_inode = autofs_evict_inode,
113 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
114 Opt_indirect, Opt_direct, Opt_offset, Opt_strictexpire,
117 static const match_table_t tokens = {
121 {Opt_pgrp, "pgrp=%u"},
122 {Opt_minproto, "minproto=%u"},
123 {Opt_maxproto, "maxproto=%u"},
124 {Opt_indirect, "indirect"},
125 {Opt_direct, "direct"},
126 {Opt_offset, "offset"},
127 {Opt_strictexpire, "strictexpire"},
128 {Opt_ignore, "ignore"},
132 static int parse_options(char *options,
133 struct inode *root, int *pgrp, bool *pgrp_set,
134 struct autofs_sb_info *sbi)
137 substring_t args[MAX_OPT_ARGS];
143 root->i_uid = current_uid();
144 root->i_gid = current_gid();
146 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
147 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
154 while ((p = strsep(&options, ",")) != NULL) {
160 token = match_token(p, tokens, args);
163 if (match_int(args, &pipefd))
165 sbi->pipefd = pipefd;
168 if (match_int(args, &option))
170 uid = make_kuid(current_user_ns(), option);
176 if (match_int(args, &option))
178 gid = make_kgid(current_user_ns(), option);
184 if (match_int(args, &option))
190 if (match_int(args, &option))
192 sbi->min_proto = option;
195 if (match_int(args, &option))
197 sbi->max_proto = option;
200 set_autofs_type_indirect(&sbi->type);
203 set_autofs_type_direct(&sbi->type);
206 set_autofs_type_offset(&sbi->type);
208 case Opt_strictexpire:
209 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
212 sbi->flags |= AUTOFS_SBI_IGNORE;
218 return (sbi->pipefd < 0);
221 int autofs_fill_super(struct super_block *s, void *data, int silent)
223 struct inode *root_inode;
226 struct autofs_sb_info *sbi;
227 struct autofs_info *ino;
229 bool pgrp_set = false;
232 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
235 pr_debug("starting up, sbi = %p\n", sbi);
238 sbi->magic = AUTOFS_SBI_MAGIC;
241 sbi->exp_timeout = 0;
245 sbi->sub_version = 0;
246 sbi->flags = AUTOFS_SBI_CATATONIC;
247 set_autofs_type_indirect(&sbi->type);
250 mutex_init(&sbi->wq_mutex);
251 mutex_init(&sbi->pipe_mutex);
252 spin_lock_init(&sbi->fs_lock);
254 spin_lock_init(&sbi->lookup_lock);
255 INIT_LIST_HEAD(&sbi->active_list);
256 INIT_LIST_HEAD(&sbi->expiring_list);
257 s->s_blocksize = 1024;
258 s->s_blocksize_bits = 10;
259 s->s_magic = AUTOFS_SUPER_MAGIC;
260 s->s_op = &autofs_sops;
261 s->s_d_op = &autofs_dentry_operations;
265 * Get the root inode and dentry, but defer checking for errors.
267 ino = autofs_new_ino(sbi);
272 root_inode = autofs_get_inode(s, S_IFDIR | 0755);
273 root = d_make_root(root_inode);
280 root->d_fsdata = ino;
282 /* Can this call block? */
283 if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) {
284 pr_err("called with bogus options\n");
288 /* Test versions first */
289 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
290 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
291 pr_err("kernel does not match daemon version "
292 "daemon (%d, %d) kernel (%d, %d)\n",
293 sbi->min_proto, sbi->max_proto,
294 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
298 /* Establish highest kernel protocol version */
299 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
300 sbi->version = AUTOFS_MAX_PROTO_VERSION;
302 sbi->version = sbi->max_proto;
303 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
306 sbi->oz_pgrp = find_get_pid(pgrp);
308 pr_err("could not find process group %d\n",
313 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
316 if (autofs_type_trigger(sbi->type))
317 __managed_dentry_set_managed(root);
319 root_inode->i_fop = &autofs_root_operations;
320 root_inode->i_op = &autofs_dir_inode_operations;
322 pr_debug("pipe fd = %d, pgrp = %u\n",
323 sbi->pipefd, pid_nr(sbi->oz_pgrp));
324 pipe = fget(sbi->pipefd);
327 pr_err("could not open pipe file descriptor\n");
330 ret = autofs_prepare_pipe(pipe);
334 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
337 * Success! Install the root dentry now to indicate completion.
343 * Failure ... clean up.
346 pr_err("pipe file descriptor does not contain proper ops\n");
349 put_pid(sbi->oz_pgrp);
354 autofs_free_ino(ino);
361 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
363 struct inode *inode = new_inode(sb);
368 inode->i_mode = mode;
370 inode->i_uid = d_inode(sb->s_root)->i_uid;
371 inode->i_gid = d_inode(sb->s_root)->i_gid;
373 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
374 inode->i_ino = get_next_ino();
378 inode->i_op = &autofs_dir_inode_operations;
379 inode->i_fop = &autofs_dir_operations;
380 } else if (S_ISLNK(mode)) {
381 inode->i_op = &autofs_symlink_inode_operations;