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