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(size: sizeof(*ino), GFP_KERNEL); |
17 | if (ino) { |
18 | INIT_LIST_HEAD(list: &ino->active); |
19 | INIT_LIST_HEAD(list: &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(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(sb: root->d_sb); |
64 | struct inode *root_inode = d_inode(dentry: root->d_sb->s_root); |
65 | |
66 | if (!sbi) |
67 | return 0; |
68 | |
69 | seq_printf(m, fmt: ",fd=%d" , sbi->pipefd); |
70 | if (!uid_eq(left: 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(objp: 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_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), |
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(fd: 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 | kuid_t uid; |
197 | kgid_t gid; |
198 | int opt; |
199 | |
200 | opt = fs_parse(fc, desc: autofs_param_specs, param, result: &result); |
201 | if (opt < 0) |
202 | return opt; |
203 | |
204 | switch (opt) { |
205 | case Opt_fd: |
206 | return autofs_parse_fd(fc, sbi, param, result: &result); |
207 | case Opt_uid: |
208 | uid = make_kuid(current_user_ns(), uid: result.uint_32); |
209 | if (!uid_valid(uid)) |
210 | return invalfc(fc, "Invalid uid" ); |
211 | ctx->uid = uid; |
212 | break; |
213 | case Opt_gid: |
214 | gid = make_kgid(current_user_ns(), gid: result.uint_32); |
215 | if (!gid_valid(gid)) |
216 | return invalfc(fc, "Invalid gid" ); |
217 | ctx->gid = gid; |
218 | break; |
219 | case Opt_pgrp: |
220 | ctx->pgrp = result.uint_32; |
221 | ctx->pgrp_set = true; |
222 | break; |
223 | case Opt_minproto: |
224 | sbi->min_proto = result.uint_32; |
225 | break; |
226 | case Opt_maxproto: |
227 | sbi->max_proto = result.uint_32; |
228 | break; |
229 | case Opt_indirect: |
230 | set_autofs_type_indirect(&sbi->type); |
231 | break; |
232 | case Opt_direct: |
233 | set_autofs_type_direct(&sbi->type); |
234 | break; |
235 | case Opt_offset: |
236 | set_autofs_type_offset(&sbi->type); |
237 | break; |
238 | case Opt_strictexpire: |
239 | sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; |
240 | break; |
241 | case Opt_ignore: |
242 | sbi->flags |= AUTOFS_SBI_IGNORE; |
243 | } |
244 | |
245 | return 0; |
246 | } |
247 | |
248 | static struct autofs_sb_info *autofs_alloc_sbi(void) |
249 | { |
250 | struct autofs_sb_info *sbi; |
251 | |
252 | sbi = kzalloc(size: sizeof(*sbi), GFP_KERNEL); |
253 | if (!sbi) |
254 | return NULL; |
255 | |
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; |
260 | sbi->pipefd = -1; |
261 | |
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(list: &sbi->active_list); |
268 | INIT_LIST_HEAD(list: &sbi->expiring_list); |
269 | |
270 | return sbi; |
271 | } |
272 | |
273 | static int autofs_validate_protocol(struct fs_context *fc) |
274 | { |
275 | struct autofs_sb_info *sbi = fc->s_fs_info; |
276 | |
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); |
284 | return -EINVAL; |
285 | } |
286 | |
287 | /* Establish highest kernel protocol version */ |
288 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) |
289 | sbi->version = AUTOFS_MAX_PROTO_VERSION; |
290 | else |
291 | sbi->version = sbi->max_proto; |
292 | |
293 | switch (sbi->version) { |
294 | case 4: |
295 | sbi->sub_version = 7; |
296 | break; |
297 | case 5: |
298 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; |
299 | break; |
300 | default: |
301 | sbi->sub_version = 0; |
302 | } |
303 | |
304 | return 0; |
305 | } |
306 | |
307 | static int autofs_fill_super(struct super_block *s, struct fs_context *fc) |
308 | { |
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 dentry *root; |
313 | struct autofs_info *ino; |
314 | int ret = -ENOMEM; |
315 | |
316 | pr_debug("starting up, sbi = %p\n" , sbi); |
317 | |
318 | sbi->sb = s; |
319 | s->s_blocksize = 1024; |
320 | s->s_blocksize_bits = 10; |
321 | s->s_magic = AUTOFS_SUPER_MAGIC; |
322 | s->s_op = &autofs_sops; |
323 | s->s_d_op = &autofs_dentry_operations; |
324 | s->s_time_gran = 1; |
325 | |
326 | /* |
327 | * Get the root inode and dentry, but defer checking for errors. |
328 | */ |
329 | ino = autofs_new_ino(sbi); |
330 | if (!ino) |
331 | goto fail; |
332 | |
333 | root_inode = autofs_get_inode(s, S_IFDIR | 0755); |
334 | root_inode->i_uid = ctx->uid; |
335 | root_inode->i_gid = ctx->gid; |
336 | |
337 | root = d_make_root(root_inode); |
338 | if (!root) |
339 | goto fail_ino; |
340 | |
341 | root->d_fsdata = ino; |
342 | |
343 | if (ctx->pgrp_set) { |
344 | sbi->oz_pgrp = find_get_pid(nr: ctx->pgrp); |
345 | if (!sbi->oz_pgrp) { |
346 | ret = invalf(fc, "Could not find process group %d" , |
347 | ctx->pgrp); |
348 | goto fail_dput; |
349 | } |
350 | } else { |
351 | sbi->oz_pgrp = get_task_pid(current, type: PIDTYPE_PGID); |
352 | } |
353 | |
354 | if (autofs_type_trigger(type: sbi->type)) |
355 | __managed_dentry_set_managed(dentry: root); |
356 | |
357 | root_inode->i_fop = &autofs_root_operations; |
358 | root_inode->i_op = &autofs_dir_inode_operations; |
359 | |
360 | pr_debug("pipe fd = %d, pgrp = %u\n" , |
361 | sbi->pipefd, pid_nr(sbi->oz_pgrp)); |
362 | |
363 | sbi->flags &= ~AUTOFS_SBI_CATATONIC; |
364 | |
365 | /* |
366 | * Success! Install the root dentry now to indicate completion. |
367 | */ |
368 | s->s_root = root; |
369 | return 0; |
370 | |
371 | /* |
372 | * Failure ... clean up. |
373 | */ |
374 | fail_dput: |
375 | dput(root); |
376 | goto fail; |
377 | fail_ino: |
378 | autofs_free_ino(ino); |
379 | fail: |
380 | return ret; |
381 | } |
382 | |
383 | /* |
384 | * Validate the parameters and then request a superblock. |
385 | */ |
386 | static int autofs_get_tree(struct fs_context *fc) |
387 | { |
388 | struct autofs_sb_info *sbi = fc->s_fs_info; |
389 | int ret; |
390 | |
391 | ret = autofs_validate_protocol(fc); |
392 | if (ret) |
393 | return ret; |
394 | |
395 | if (sbi->pipefd < 0) |
396 | return invalf(fc, "No control pipe specified" ); |
397 | |
398 | return get_tree_nodev(fc, fill_super: autofs_fill_super); |
399 | } |
400 | |
401 | static void autofs_free_fc(struct fs_context *fc) |
402 | { |
403 | struct autofs_fs_context *ctx = fc->fs_private; |
404 | struct autofs_sb_info *sbi = fc->s_fs_info; |
405 | |
406 | if (sbi) { |
407 | if (sbi->pipe) |
408 | fput(sbi->pipe); |
409 | kfree(objp: sbi); |
410 | } |
411 | kfree(objp: ctx); |
412 | } |
413 | |
414 | static const struct fs_context_operations autofs_context_ops = { |
415 | .free = autofs_free_fc, |
416 | .parse_param = autofs_parse_param, |
417 | .get_tree = autofs_get_tree, |
418 | }; |
419 | |
420 | /* |
421 | * Set up the filesystem mount context. |
422 | */ |
423 | int autofs_init_fs_context(struct fs_context *fc) |
424 | { |
425 | struct autofs_fs_context *ctx; |
426 | struct autofs_sb_info *sbi; |
427 | |
428 | ctx = kzalloc(size: sizeof(struct autofs_fs_context), GFP_KERNEL); |
429 | if (!ctx) |
430 | goto nomem; |
431 | |
432 | ctx->uid = current_uid(); |
433 | ctx->gid = current_gid(); |
434 | |
435 | sbi = autofs_alloc_sbi(); |
436 | if (!sbi) |
437 | goto nomem_ctx; |
438 | |
439 | fc->fs_private = ctx; |
440 | fc->s_fs_info = sbi; |
441 | fc->ops = &autofs_context_ops; |
442 | return 0; |
443 | |
444 | nomem_ctx: |
445 | kfree(objp: ctx); |
446 | nomem: |
447 | return -ENOMEM; |
448 | } |
449 | |
450 | struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) |
451 | { |
452 | struct inode *inode = new_inode(sb); |
453 | |
454 | if (inode == NULL) |
455 | return NULL; |
456 | |
457 | inode->i_mode = mode; |
458 | if (sb->s_root) { |
459 | inode->i_uid = d_inode(dentry: sb->s_root)->i_uid; |
460 | inode->i_gid = d_inode(dentry: sb->s_root)->i_gid; |
461 | } |
462 | simple_inode_init_ts(inode); |
463 | inode->i_ino = get_next_ino(); |
464 | |
465 | if (S_ISDIR(mode)) { |
466 | set_nlink(inode, nlink: 2); |
467 | inode->i_op = &autofs_dir_inode_operations; |
468 | inode->i_fop = &autofs_dir_operations; |
469 | } else if (S_ISLNK(mode)) { |
470 | inode->i_op = &autofs_symlink_inode_operations; |
471 | } else |
472 | WARN_ON(1); |
473 | |
474 | return inode; |
475 | } |
476 | |