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> | |
9 | #include <linux/parser.h> | |
6471e938 | 10 | |
ebc921ca | 11 | #include "autofs_i.h" |
ebc921ca IK |
12 | |
13 | struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi) | |
14 | { | |
15 | struct autofs_info *ino; | |
16 | ||
17 | ino = kzalloc(sizeof(*ino), GFP_KERNEL); | |
18 | if (ino) { | |
19 | INIT_LIST_HEAD(&ino->active); | |
20 | INIT_LIST_HEAD(&ino->expiring); | |
21 | ino->last_used = jiffies; | |
22 | ino->sbi = sbi; | |
9ccbac76 | 23 | ino->count = 1; |
ebc921ca IK |
24 | } |
25 | return ino; | |
26 | } | |
27 | ||
28 | void autofs_clean_ino(struct autofs_info *ino) | |
29 | { | |
30 | ino->uid = GLOBAL_ROOT_UID; | |
31 | ino->gid = GLOBAL_ROOT_GID; | |
32 | ino->last_used = jiffies; | |
33 | } | |
34 | ||
35 | void autofs_free_ino(struct autofs_info *ino) | |
36 | { | |
ce285c26 | 37 | kfree_rcu(ino, rcu); |
ebc921ca IK |
38 | } |
39 | ||
40 | void autofs_kill_sb(struct super_block *sb) | |
41 | { | |
42 | struct autofs_sb_info *sbi = autofs_sbi(sb); | |
43 | ||
44 | /* | |
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 | |
48 | * deactivate_super. | |
49 | */ | |
50 | if (sbi) { | |
51 | /* Free wait queues, close pipe */ | |
52 | autofs_catatonic_mode(sbi); | |
53 | put_pid(sbi->oz_pgrp); | |
54 | } | |
55 | ||
56 | pr_debug("shutting down\n"); | |
57 | kill_litter_super(sb); | |
58 | if (sbi) | |
59 | kfree_rcu(sbi, rcu); | |
60 | } | |
61 | ||
62 | static int autofs_show_options(struct seq_file *m, struct dentry *root) | |
63 | { | |
64 | struct autofs_sb_info *sbi = autofs_sbi(root->d_sb); | |
65 | struct inode *root_inode = d_inode(root->d_sb->s_root); | |
66 | ||
67 | if (!sbi) | |
68 | return 0; | |
69 | ||
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); | |
81 | ||
82 | if (autofs_type_offset(sbi->type)) | |
874d22d6 | 83 | seq_puts(m, ",offset"); |
ebc921ca | 84 | else if (autofs_type_direct(sbi->type)) |
874d22d6 | 85 | seq_puts(m, ",direct"); |
ebc921ca | 86 | else |
874d22d6 | 87 | seq_puts(m, ",indirect"); |
f5162216 | 88 | if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE) |
874d22d6 | 89 | seq_puts(m, ",strictexpire"); |
60d6d04c | 90 | if (sbi->flags & AUTOFS_SBI_IGNORE) |
874d22d6 | 91 | seq_puts(m, ",ignore"); |
ebc921ca IK |
92 | #ifdef CONFIG_CHECKPOINT_RESTORE |
93 | if (sbi->pipe) | |
94 | seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino); | |
95 | else | |
874d22d6 | 96 | seq_puts(m, ",pipe_ino=-1"); |
ebc921ca IK |
97 | #endif |
98 | return 0; | |
99 | } | |
100 | ||
101 | static void autofs_evict_inode(struct inode *inode) | |
102 | { | |
103 | clear_inode(inode); | |
104 | kfree(inode->i_private); | |
105 | } | |
106 | ||
107 | static const struct super_operations autofs_sops = { | |
108 | .statfs = simple_statfs, | |
109 | .show_options = autofs_show_options, | |
110 | .evict_inode = autofs_evict_inode, | |
111 | }; | |
112 | ||
7efd93ea IK |
113 | enum { |
114 | Opt_err, | |
115 | Opt_direct, | |
116 | Opt_fd, | |
117 | Opt_gid, | |
118 | Opt_ignore, | |
119 | Opt_indirect, | |
120 | Opt_maxproto, | |
121 | Opt_minproto, | |
122 | Opt_offset, | |
123 | Opt_pgrp, | |
124 | Opt_strictexpire, | |
125 | Opt_uid, | |
126 | }; | |
ebc921ca IK |
127 | |
128 | static const match_table_t tokens = { | |
129 | {Opt_fd, "fd=%u"}, | |
130 | {Opt_uid, "uid=%u"}, | |
131 | {Opt_gid, "gid=%u"}, | |
132 | {Opt_pgrp, "pgrp=%u"}, | |
133 | {Opt_minproto, "minproto=%u"}, | |
134 | {Opt_maxproto, "maxproto=%u"}, | |
135 | {Opt_indirect, "indirect"}, | |
136 | {Opt_direct, "direct"}, | |
137 | {Opt_offset, "offset"}, | |
f5162216 | 138 | {Opt_strictexpire, "strictexpire"}, |
60d6d04c | 139 | {Opt_ignore, "ignore"}, |
ebc921ca IK |
140 | {Opt_err, NULL} |
141 | }; | |
142 | ||
546694b8 IK |
143 | static int autofs_parse_fd(struct autofs_sb_info *sbi, int fd) |
144 | { | |
145 | struct file *pipe; | |
146 | int ret; | |
147 | ||
148 | pipe = fget(fd); | |
149 | if (!pipe) { | |
150 | pr_err("could not open pipe file descriptor\n"); | |
151 | return -EBADF; | |
152 | } | |
153 | ||
154 | ret = autofs_check_pipe(pipe); | |
155 | if (ret < 0) { | |
156 | pr_err("Invalid/unusable pipe\n"); | |
157 | fput(pipe); | |
158 | return -EBADF; | |
159 | } | |
160 | ||
161 | if (sbi->pipe) | |
162 | fput(sbi->pipe); | |
163 | ||
164 | sbi->pipefd = fd; | |
165 | sbi->pipe = pipe; | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
9b273166 IK |
170 | static int autofs_parse_param(char *optstr, struct inode *root, |
171 | int *pgrp, bool *pgrp_set, | |
172 | struct autofs_sb_info *sbi) | |
ebc921ca | 173 | { |
ebc921ca IK |
174 | substring_t args[MAX_OPT_ARGS]; |
175 | int option; | |
9bf964c9 IK |
176 | int pipefd = -1; |
177 | kuid_t uid; | |
178 | kgid_t gid; | |
9b273166 | 179 | int token; |
546694b8 | 180 | int ret; |
ebc921ca | 181 | |
9b273166 IK |
182 | token = match_token(optstr, tokens, args); |
183 | switch (token) { | |
184 | case Opt_fd: | |
185 | if (match_int(args, &pipefd)) | |
186 | return 1; | |
187 | ret = autofs_parse_fd(sbi, pipefd); | |
188 | if (ret) | |
189 | return 1; | |
190 | break; | |
191 | case Opt_uid: | |
192 | if (match_int(args, &option)) | |
193 | return 1; | |
194 | uid = make_kuid(current_user_ns(), option); | |
195 | if (!uid_valid(uid)) | |
196 | return 1; | |
197 | root->i_uid = uid; | |
198 | break; | |
199 | case Opt_gid: | |
200 | if (match_int(args, &option)) | |
201 | return 1; | |
202 | gid = make_kgid(current_user_ns(), option); | |
203 | if (!gid_valid(gid)) | |
204 | return 1; | |
205 | root->i_gid = gid; | |
206 | break; | |
207 | case Opt_pgrp: | |
208 | if (match_int(args, &option)) | |
209 | return 1; | |
210 | *pgrp = option; | |
211 | *pgrp_set = true; | |
212 | break; | |
213 | case Opt_minproto: | |
214 | if (match_int(args, &option)) | |
215 | return 1; | |
216 | sbi->min_proto = option; | |
217 | break; | |
218 | case Opt_maxproto: | |
219 | if (match_int(args, &option)) | |
220 | return 1; | |
221 | sbi->max_proto = option; | |
222 | break; | |
223 | case Opt_indirect: | |
224 | set_autofs_type_indirect(&sbi->type); | |
225 | break; | |
226 | case Opt_direct: | |
227 | set_autofs_type_direct(&sbi->type); | |
228 | break; | |
229 | case Opt_offset: | |
230 | set_autofs_type_offset(&sbi->type); | |
231 | break; | |
232 | case Opt_strictexpire: | |
233 | sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; | |
234 | break; | |
235 | case Opt_ignore: | |
236 | sbi->flags |= AUTOFS_SBI_IGNORE; | |
237 | } | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static int parse_options(char *options, | |
243 | struct inode *root, int *pgrp, bool *pgrp_set, | |
244 | struct autofs_sb_info *sbi) | |
245 | { | |
246 | char *p; | |
247 | ||
9bf964c9 IK |
248 | root->i_uid = current_uid(); |
249 | root->i_gid = current_gid(); | |
ebc921ca | 250 | |
ebc921ca IK |
251 | if (!options) |
252 | return 1; | |
253 | ||
254 | while ((p = strsep(&options, ",")) != NULL) { | |
ebc921ca IK |
255 | if (!*p) |
256 | continue; | |
257 | ||
9b273166 | 258 | if (autofs_parse_param(p, root, pgrp, pgrp_set, sbi)) |
ebc921ca | 259 | return 1; |
ebc921ca | 260 | } |
9b273166 | 261 | |
9bf964c9 | 262 | return (sbi->pipefd < 0); |
ebc921ca IK |
263 | } |
264 | ||
a7467430 | 265 | static struct autofs_sb_info *autofs_alloc_sbi(void) |
ebc921ca | 266 | { |
ebc921ca | 267 | struct autofs_sb_info *sbi; |
ebc921ca IK |
268 | |
269 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); | |
270 | if (!sbi) | |
a7467430 | 271 | return NULL; |
ebc921ca | 272 | |
ebc921ca | 273 | sbi->magic = AUTOFS_SBI_MAGIC; |
9d8719a4 | 274 | sbi->flags = AUTOFS_SBI_CATATONIC; |
a7467430 IK |
275 | sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; |
276 | sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; | |
277 | sbi->pipefd = -1; | |
278 | ||
ebc921ca | 279 | set_autofs_type_indirect(&sbi->type); |
ebc921ca IK |
280 | mutex_init(&sbi->wq_mutex); |
281 | mutex_init(&sbi->pipe_mutex); | |
282 | spin_lock_init(&sbi->fs_lock); | |
ebc921ca IK |
283 | spin_lock_init(&sbi->lookup_lock); |
284 | INIT_LIST_HEAD(&sbi->active_list); | |
285 | INIT_LIST_HEAD(&sbi->expiring_list); | |
a7467430 IK |
286 | |
287 | return sbi; | |
288 | } | |
289 | ||
1f50012d IK |
290 | static int autofs_validate_protocol(struct autofs_sb_info *sbi) |
291 | { | |
292 | /* Test versions first */ | |
293 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || | |
294 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
295 | pr_err("kernel does not match daemon version " | |
296 | "daemon (%d, %d) kernel (%d, %d)\n", | |
297 | sbi->min_proto, sbi->max_proto, | |
298 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | /* Establish highest kernel protocol version */ | |
303 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) | |
304 | sbi->version = AUTOFS_MAX_PROTO_VERSION; | |
305 | else | |
306 | sbi->version = sbi->max_proto; | |
307 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
a7467430 IK |
312 | int autofs_fill_super(struct super_block *s, void *data, int silent) |
313 | { | |
314 | struct inode *root_inode; | |
315 | struct dentry *root; | |
316 | struct autofs_sb_info *sbi; | |
317 | struct autofs_info *ino; | |
318 | int pgrp = 0; | |
319 | bool pgrp_set = false; | |
320 | int ret = -EINVAL; | |
321 | ||
322 | sbi = autofs_alloc_sbi(); | |
323 | if (!sbi) | |
324 | return -ENOMEM; | |
325 | ||
326 | pr_debug("starting up, sbi = %p\n", sbi); | |
327 | ||
328 | sbi->sb = s; | |
329 | s->s_fs_info = sbi; | |
ebc921ca IK |
330 | s->s_blocksize = 1024; |
331 | s->s_blocksize_bits = 10; | |
332 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
333 | s->s_op = &autofs_sops; | |
334 | s->s_d_op = &autofs_dentry_operations; | |
335 | s->s_time_gran = 1; | |
336 | ||
337 | /* | |
338 | * Get the root inode and dentry, but defer checking for errors. | |
339 | */ | |
340 | ino = autofs_new_ino(sbi); | |
341 | if (!ino) { | |
342 | ret = -ENOMEM; | |
343 | goto fail_free; | |
344 | } | |
345 | root_inode = autofs_get_inode(s, S_IFDIR | 0755); | |
346 | root = d_make_root(root_inode); | |
f585b283 IK |
347 | if (!root) { |
348 | ret = -ENOMEM; | |
ebc921ca | 349 | goto fail_ino; |
f585b283 | 350 | } |
ebc921ca IK |
351 | |
352 | root->d_fsdata = ino; | |
353 | ||
354 | /* Can this call block? */ | |
9bf964c9 | 355 | if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) { |
ebc921ca IK |
356 | pr_err("called with bogus options\n"); |
357 | goto fail_dput; | |
358 | } | |
359 | ||
1f50012d | 360 | if (autofs_validate_protocol(sbi)) |
ebc921ca | 361 | goto fail_dput; |
ebc921ca IK |
362 | |
363 | if (pgrp_set) { | |
364 | sbi->oz_pgrp = find_get_pid(pgrp); | |
365 | if (!sbi->oz_pgrp) { | |
366 | pr_err("could not find process group %d\n", | |
367 | pgrp); | |
368 | goto fail_dput; | |
369 | } | |
370 | } else { | |
371 | sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); | |
372 | } | |
373 | ||
374 | if (autofs_type_trigger(sbi->type)) | |
375 | __managed_dentry_set_managed(root); | |
376 | ||
377 | root_inode->i_fop = &autofs_root_operations; | |
378 | root_inode->i_op = &autofs_dir_inode_operations; | |
379 | ||
9bf964c9 IK |
380 | pr_debug("pipe fd = %d, pgrp = %u\n", |
381 | sbi->pipefd, pid_nr(sbi->oz_pgrp)); | |
ebc921ca | 382 | |
9d8719a4 | 383 | sbi->flags &= ~AUTOFS_SBI_CATATONIC; |
ebc921ca IK |
384 | |
385 | /* | |
386 | * Success! Install the root dentry now to indicate completion. | |
387 | */ | |
388 | s->s_root = root; | |
389 | return 0; | |
390 | ||
391 | /* | |
392 | * Failure ... clean up. | |
393 | */ | |
ebc921ca IK |
394 | fail_dput: |
395 | dput(root); | |
396 | goto fail_free; | |
397 | fail_ino: | |
398 | autofs_free_ino(ino); | |
399 | fail_free: | |
400 | kfree(sbi); | |
401 | s->s_fs_info = NULL; | |
402 | return ret; | |
403 | } | |
404 | ||
405 | struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) | |
406 | { | |
407 | struct inode *inode = new_inode(sb); | |
408 | ||
409 | if (inode == NULL) | |
410 | return NULL; | |
411 | ||
412 | inode->i_mode = mode; | |
413 | if (sb->s_root) { | |
414 | inode->i_uid = d_inode(sb->s_root)->i_uid; | |
415 | inode->i_gid = d_inode(sb->s_root)->i_gid; | |
416 | } | |
36aa5eae | 417 | inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode); |
ebc921ca IK |
418 | inode->i_ino = get_next_ino(); |
419 | ||
420 | if (S_ISDIR(mode)) { | |
421 | set_nlink(inode, 2); | |
422 | inode->i_op = &autofs_dir_inode_operations; | |
423 | inode->i_fop = &autofs_dir_operations; | |
424 | } else if (S_ISLNK(mode)) { | |
425 | inode->i_op = &autofs_symlink_inode_operations; | |
426 | } else | |
427 | WARN_ON(1); | |
428 | ||
429 | return inode; | |
430 | } |