Commit | Line | Data |
---|---|---|
d6910058 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
ebc921ca IK |
2 | /* |
3 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | |
4 | * Copyright 2005-2006 Ian Kent <raven@themaw.net> | |
ebc921ca IK |
5 | */ |
6 | ||
ebc921ca IK |
7 | #include <linux/seq_file.h> |
8 | #include <linux/pagemap.h> | |
6471e938 | 9 | |
ebc921ca | 10 | #include "autofs_i.h" |
ebc921ca IK |
11 | |
12 | struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi) | |
13 | { | |
14 | struct autofs_info *ino; | |
15 | ||
16 | ino = kzalloc(sizeof(*ino), GFP_KERNEL); | |
17 | if (ino) { | |
18 | INIT_LIST_HEAD(&ino->active); | |
19 | INIT_LIST_HEAD(&ino->expiring); | |
20 | ino->last_used = jiffies; | |
21 | ino->sbi = sbi; | |
9ccbac76 | 22 | ino->count = 1; |
ebc921ca IK |
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 | { | |
ce285c26 | 36 | kfree_rcu(ino, rcu); |
ebc921ca IK |
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(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(root->d_sb); | |
64 | struct inode *root_inode = d_inode(root->d_sb->s_root); | |
65 | ||
66 | if (!sbi) | |
67 | return 0; | |
68 | ||
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); | |
80 | ||
81 | if (autofs_type_offset(sbi->type)) | |
874d22d6 | 82 | seq_puts(m, ",offset"); |
ebc921ca | 83 | else if (autofs_type_direct(sbi->type)) |
874d22d6 | 84 | seq_puts(m, ",direct"); |
ebc921ca | 85 | else |
874d22d6 | 86 | seq_puts(m, ",indirect"); |
f5162216 | 87 | if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE) |
874d22d6 | 88 | seq_puts(m, ",strictexpire"); |
60d6d04c | 89 | if (sbi->flags & AUTOFS_SBI_IGNORE) |
874d22d6 | 90 | seq_puts(m, ",ignore"); |
ebc921ca IK |
91 | #ifdef CONFIG_CHECKPOINT_RESTORE |
92 | if (sbi->pipe) | |
93 | seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino); | |
94 | else | |
874d22d6 | 95 | seq_puts(m, ",pipe_ino=-1"); |
ebc921ca IK |
96 | #endif |
97 | return 0; | |
98 | } | |
99 | ||
100 | static void autofs_evict_inode(struct inode *inode) | |
101 | { | |
102 | clear_inode(inode); | |
103 | kfree(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 | ||
7efd93ea | 112 | enum { |
7efd93ea IK |
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 | }; | |
ebc921ca | 125 | |
e6ec453b IK |
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 | {} | |
ebc921ca IK |
139 | }; |
140 | ||
e6ec453b IK |
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) | |
546694b8 IK |
156 | { |
157 | struct file *pipe; | |
158 | int ret; | |
159 | ||
e6ec453b IK |
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(result->uint_32); | |
166 | } | |
546694b8 | 167 | if (!pipe) { |
e6ec453b | 168 | errorf(fc, "could not open pipe file descriptor"); |
546694b8 IK |
169 | return -EBADF; |
170 | } | |
171 | ||
172 | ret = autofs_check_pipe(pipe); | |
173 | if (ret < 0) { | |
e6ec453b IK |
174 | errorf(fc, "Invalid/unusable pipe"); |
175 | if (param->type != fs_value_is_file) | |
176 | fput(pipe); | |
546694b8 IK |
177 | return -EBADF; |
178 | } | |
179 | ||
d3c50061 IK |
180 | autofs_set_packet_pipe_flags(pipe); |
181 | ||
546694b8 IK |
182 | if (sbi->pipe) |
183 | fput(sbi->pipe); | |
184 | ||
e6ec453b | 185 | sbi->pipefd = result->uint_32; |
546694b8 IK |
186 | sbi->pipe = pipe; |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
e6ec453b | 191 | static int autofs_parse_param(struct fs_context *fc, struct fs_parameter *param) |
ebc921ca | 192 | { |
e6ec453b IK |
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; | |
9bf964c9 IK |
196 | kuid_t uid; |
197 | kgid_t gid; | |
e6ec453b IK |
198 | int opt; |
199 | ||
200 | opt = fs_parse(fc, autofs_param_specs, param, &result); | |
201 | if (opt < 0) | |
202 | return opt; | |
ebc921ca | 203 | |
e6ec453b | 204 | switch (opt) { |
9b273166 | 205 | case Opt_fd: |
e6ec453b | 206 | return autofs_parse_fd(fc, sbi, param, &result); |
9b273166 | 207 | case Opt_uid: |
e6ec453b | 208 | uid = make_kuid(current_user_ns(), result.uint_32); |
9b273166 | 209 | if (!uid_valid(uid)) |
e6ec453b IK |
210 | return invalfc(fc, "Invalid uid"); |
211 | ctx->uid = uid; | |
9b273166 IK |
212 | break; |
213 | case Opt_gid: | |
e6ec453b | 214 | gid = make_kgid(current_user_ns(), result.uint_32); |
9b273166 | 215 | if (!gid_valid(gid)) |
e6ec453b IK |
216 | return invalfc(fc, "Invalid gid"); |
217 | ctx->gid = gid; | |
9b273166 IK |
218 | break; |
219 | case Opt_pgrp: | |
e6ec453b IK |
220 | ctx->pgrp = result.uint_32; |
221 | ctx->pgrp_set = true; | |
9b273166 IK |
222 | break; |
223 | case Opt_minproto: | |
e6ec453b | 224 | sbi->min_proto = result.uint_32; |
9b273166 IK |
225 | break; |
226 | case Opt_maxproto: | |
e6ec453b | 227 | sbi->max_proto = result.uint_32; |
9b273166 IK |
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 | ||
a7467430 | 248 | static struct autofs_sb_info *autofs_alloc_sbi(void) |
ebc921ca | 249 | { |
ebc921ca | 250 | struct autofs_sb_info *sbi; |
ebc921ca IK |
251 | |
252 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); | |
253 | if (!sbi) | |
a7467430 | 254 | return NULL; |
ebc921ca | 255 | |
ebc921ca | 256 | sbi->magic = AUTOFS_SBI_MAGIC; |
9d8719a4 | 257 | sbi->flags = AUTOFS_SBI_CATATONIC; |
a7467430 IK |
258 | sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; |
259 | sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; | |
260 | sbi->pipefd = -1; | |
261 | ||
ebc921ca | 262 | set_autofs_type_indirect(&sbi->type); |
ebc921ca IK |
263 | mutex_init(&sbi->wq_mutex); |
264 | mutex_init(&sbi->pipe_mutex); | |
265 | spin_lock_init(&sbi->fs_lock); | |
ebc921ca IK |
266 | spin_lock_init(&sbi->lookup_lock); |
267 | INIT_LIST_HEAD(&sbi->active_list); | |
268 | INIT_LIST_HEAD(&sbi->expiring_list); | |
a7467430 IK |
269 | |
270 | return sbi; | |
271 | } | |
272 | ||
e6ec453b | 273 | static int autofs_validate_protocol(struct fs_context *fc) |
1f50012d | 274 | { |
e6ec453b IK |
275 | struct autofs_sb_info *sbi = fc->s_fs_info; |
276 | ||
1f50012d IK |
277 | /* Test versions first */ |
278 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || | |
279 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
e6ec453b | 280 | errorf(fc, "kernel does not match daemon version " |
1f50012d IK |
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; | |
dede3671 IK |
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 | } | |
1f50012d IK |
303 | |
304 | return 0; | |
305 | } | |
306 | ||
e6ec453b | 307 | static int autofs_fill_super(struct super_block *s, struct fs_context *fc) |
a7467430 | 308 | { |
e6ec453b IK |
309 | struct autofs_fs_context *ctx = fc->fs_private; |
310 | struct autofs_sb_info *sbi = s->s_fs_info; | |
a7467430 | 311 | struct inode *root_inode; |
a7467430 | 312 | struct autofs_info *ino; |
a7467430 IK |
313 | |
314 | pr_debug("starting up, sbi = %p\n", sbi); | |
315 | ||
316 | sbi->sb = s; | |
ebc921ca IK |
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; | |
322 | s->s_time_gran = 1; | |
323 | ||
324 | /* | |
325 | * Get the root inode and dentry, but defer checking for errors. | |
326 | */ | |
327 | ino = autofs_new_ino(sbi); | |
e6ec453b | 328 | if (!ino) |
66917f85 | 329 | return -ENOMEM; |
e6ec453b | 330 | |
ebc921ca | 331 | root_inode = autofs_get_inode(s, S_IFDIR | 0755); |
66917f85 IK |
332 | if (!root_inode) |
333 | return -ENOMEM; | |
334 | ||
e6ec453b IK |
335 | root_inode->i_uid = ctx->uid; |
336 | root_inode->i_gid = ctx->gid; | |
66917f85 IK |
337 | root_inode->i_fop = &autofs_root_operations; |
338 | root_inode->i_op = &autofs_dir_inode_operations; | |
e6ec453b | 339 | |
66917f85 IK |
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; | |
ebc921ca | 346 | |
e6ec453b IK |
347 | if (ctx->pgrp_set) { |
348 | sbi->oz_pgrp = find_get_pid(ctx->pgrp); | |
66917f85 IK |
349 | if (!sbi->oz_pgrp) |
350 | return invalf(fc, "Could not find process group %d", | |
351 | ctx->pgrp); | |
352 | } else | |
ebc921ca | 353 | sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); |
ebc921ca IK |
354 | |
355 | if (autofs_type_trigger(sbi->type)) | |
66917f85 IK |
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); | |
ebc921ca | 361 | |
9bf964c9 IK |
362 | pr_debug("pipe fd = %d, pgrp = %u\n", |
363 | sbi->pipefd, pid_nr(sbi->oz_pgrp)); | |
ebc921ca | 364 | |
9d8719a4 | 365 | sbi->flags &= ~AUTOFS_SBI_CATATONIC; |
ebc921ca | 366 | return 0; |
ebc921ca IK |
367 | } |
368 | ||
e6ec453b IK |
369 | /* |
370 | * Validate the parameters and then request a superblock. | |
371 | */ | |
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 | ||
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 | */ | |
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(sizeof(struct autofs_fs_context), GFP_KERNEL); | |
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 | ||
ebc921ca IK |
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 | } | |
2c11fd98 | 448 | simple_inode_init_ts(inode); |
ebc921ca IK |
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 | } |