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
;
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
,
112 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
113 Opt_indirect
, Opt_direct
, Opt_offset
, Opt_strictexpire
,
116 static const match_table_t tokens
= {
120 {Opt_pgrp
, "pgrp=%u"},
121 {Opt_minproto
, "minproto=%u"},
122 {Opt_maxproto
, "maxproto=%u"},
123 {Opt_indirect
, "indirect"},
124 {Opt_direct
, "direct"},
125 {Opt_offset
, "offset"},
126 {Opt_strictexpire
, "strictexpire"},
127 {Opt_ignore
, "ignore"},
131 static int parse_options(char *options
,
132 struct inode
*root
, int *pgrp
, bool *pgrp_set
,
133 struct autofs_sb_info
*sbi
)
136 substring_t args
[MAX_OPT_ARGS
];
142 root
->i_uid
= current_uid();
143 root
->i_gid
= current_gid();
145 sbi
->min_proto
= AUTOFS_MIN_PROTO_VERSION
;
146 sbi
->max_proto
= AUTOFS_MAX_PROTO_VERSION
;
153 while ((p
= strsep(&options
, ",")) != NULL
) {
159 token
= match_token(p
, tokens
, args
);
162 if (match_int(args
, &pipefd
))
164 sbi
->pipefd
= pipefd
;
167 if (match_int(args
, &option
))
169 uid
= make_kuid(current_user_ns(), option
);
175 if (match_int(args
, &option
))
177 gid
= make_kgid(current_user_ns(), option
);
183 if (match_int(args
, &option
))
189 if (match_int(args
, &option
))
191 sbi
->min_proto
= option
;
194 if (match_int(args
, &option
))
196 sbi
->max_proto
= option
;
199 set_autofs_type_indirect(&sbi
->type
);
202 set_autofs_type_direct(&sbi
->type
);
205 set_autofs_type_offset(&sbi
->type
);
207 case Opt_strictexpire
:
208 sbi
->flags
|= AUTOFS_SBI_STRICTEXPIRE
;
211 sbi
->flags
|= AUTOFS_SBI_IGNORE
;
217 return (sbi
->pipefd
< 0);
220 int autofs_fill_super(struct super_block
*s
, void *data
, int silent
)
222 struct inode
*root_inode
;
225 struct autofs_sb_info
*sbi
;
226 struct autofs_info
*ino
;
228 bool pgrp_set
= false;
231 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
234 pr_debug("starting up, sbi = %p\n", sbi
);
237 sbi
->magic
= AUTOFS_SBI_MAGIC
;
240 sbi
->exp_timeout
= 0;
244 sbi
->sub_version
= 0;
245 sbi
->flags
= AUTOFS_SBI_CATATONIC
;
246 set_autofs_type_indirect(&sbi
->type
);
249 mutex_init(&sbi
->wq_mutex
);
250 mutex_init(&sbi
->pipe_mutex
);
251 spin_lock_init(&sbi
->fs_lock
);
253 spin_lock_init(&sbi
->lookup_lock
);
254 INIT_LIST_HEAD(&sbi
->active_list
);
255 INIT_LIST_HEAD(&sbi
->expiring_list
);
256 s
->s_blocksize
= 1024;
257 s
->s_blocksize_bits
= 10;
258 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
259 s
->s_op
= &autofs_sops
;
260 s
->s_d_op
= &autofs_dentry_operations
;
264 * Get the root inode and dentry, but defer checking for errors.
266 ino
= autofs_new_ino(sbi
);
271 root_inode
= autofs_get_inode(s
, S_IFDIR
| 0755);
272 root
= d_make_root(root_inode
);
279 root
->d_fsdata
= ino
;
281 /* Can this call block? */
282 if (parse_options(data
, root_inode
, &pgrp
, &pgrp_set
, sbi
)) {
283 pr_err("called with bogus options\n");
287 /* Test versions first */
288 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
289 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
290 pr_err("kernel does not match daemon version "
291 "daemon (%d, %d) kernel (%d, %d)\n",
292 sbi
->min_proto
, sbi
->max_proto
,
293 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
297 /* Establish highest kernel protocol version */
298 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
299 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
301 sbi
->version
= sbi
->max_proto
;
302 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
305 sbi
->oz_pgrp
= find_get_pid(pgrp
);
307 pr_err("could not find process group %d\n",
312 sbi
->oz_pgrp
= get_task_pid(current
, PIDTYPE_PGID
);
315 if (autofs_type_trigger(sbi
->type
))
316 __managed_dentry_set_managed(root
);
318 root_inode
->i_fop
= &autofs_root_operations
;
319 root_inode
->i_op
= &autofs_dir_inode_operations
;
321 pr_debug("pipe fd = %d, pgrp = %u\n",
322 sbi
->pipefd
, pid_nr(sbi
->oz_pgrp
));
323 pipe
= fget(sbi
->pipefd
);
326 pr_err("could not open pipe file descriptor\n");
329 ret
= autofs_prepare_pipe(pipe
);
333 sbi
->flags
&= ~AUTOFS_SBI_CATATONIC
;
336 * Success! Install the root dentry now to indicate completion.
342 * Failure ... clean up.
345 pr_err("pipe file descriptor does not contain proper ops\n");
348 put_pid(sbi
->oz_pgrp
);
353 autofs_free_ino(ino
);
360 struct inode
*autofs_get_inode(struct super_block
*sb
, umode_t mode
)
362 struct inode
*inode
= new_inode(sb
);
367 inode
->i_mode
= mode
;
369 inode
->i_uid
= d_inode(sb
->s_root
)->i_uid
;
370 inode
->i_gid
= d_inode(sb
->s_root
)->i_gid
;
372 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
373 inode
->i_ino
= get_next_ino();
377 inode
->i_op
= &autofs_dir_inode_operations
;
378 inode
->i_fop
= &autofs_dir_operations
;
379 } else if (S_ISLNK(mode
)) {
380 inode
->i_op
= &autofs_symlink_inode_operations
;