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 | ||
9bf964c9 IK |
170 | static int parse_options(char *options, |
171 | struct inode *root, int *pgrp, bool *pgrp_set, | |
172 | struct autofs_sb_info *sbi) | |
ebc921ca IK |
173 | { |
174 | char *p; | |
175 | substring_t args[MAX_OPT_ARGS]; | |
176 | int option; | |
9bf964c9 IK |
177 | int pipefd = -1; |
178 | kuid_t uid; | |
179 | kgid_t gid; | |
546694b8 | 180 | int ret; |
ebc921ca | 181 | |
9bf964c9 IK |
182 | root->i_uid = current_uid(); |
183 | root->i_gid = current_gid(); | |
ebc921ca | 184 | |
ebc921ca IK |
185 | if (!options) |
186 | return 1; | |
187 | ||
188 | while ((p = strsep(&options, ",")) != NULL) { | |
189 | int token; | |
190 | ||
191 | if (!*p) | |
192 | continue; | |
193 | ||
194 | token = match_token(p, tokens, args); | |
195 | switch (token) { | |
196 | case Opt_fd: | |
9bf964c9 | 197 | if (match_int(args, &pipefd)) |
ebc921ca | 198 | return 1; |
546694b8 IK |
199 | ret = autofs_parse_fd(sbi, pipefd); |
200 | if (ret) | |
201 | return 1; | |
ebc921ca IK |
202 | break; |
203 | case Opt_uid: | |
204 | if (match_int(args, &option)) | |
205 | return 1; | |
9bf964c9 IK |
206 | uid = make_kuid(current_user_ns(), option); |
207 | if (!uid_valid(uid)) | |
ebc921ca | 208 | return 1; |
9bf964c9 | 209 | root->i_uid = uid; |
ebc921ca IK |
210 | break; |
211 | case Opt_gid: | |
212 | if (match_int(args, &option)) | |
213 | return 1; | |
9bf964c9 IK |
214 | gid = make_kgid(current_user_ns(), option); |
215 | if (!gid_valid(gid)) | |
ebc921ca | 216 | return 1; |
9bf964c9 | 217 | root->i_gid = gid; |
ebc921ca IK |
218 | break; |
219 | case Opt_pgrp: | |
220 | if (match_int(args, &option)) | |
221 | return 1; | |
222 | *pgrp = option; | |
223 | *pgrp_set = true; | |
224 | break; | |
225 | case Opt_minproto: | |
226 | if (match_int(args, &option)) | |
227 | return 1; | |
9bf964c9 | 228 | sbi->min_proto = option; |
ebc921ca IK |
229 | break; |
230 | case Opt_maxproto: | |
231 | if (match_int(args, &option)) | |
232 | return 1; | |
9bf964c9 | 233 | sbi->max_proto = option; |
ebc921ca IK |
234 | break; |
235 | case Opt_indirect: | |
9bf964c9 | 236 | set_autofs_type_indirect(&sbi->type); |
ebc921ca IK |
237 | break; |
238 | case Opt_direct: | |
9bf964c9 | 239 | set_autofs_type_direct(&sbi->type); |
ebc921ca IK |
240 | break; |
241 | case Opt_offset: | |
9bf964c9 | 242 | set_autofs_type_offset(&sbi->type); |
ebc921ca | 243 | break; |
f5162216 IK |
244 | case Opt_strictexpire: |
245 | sbi->flags |= AUTOFS_SBI_STRICTEXPIRE; | |
246 | break; | |
60d6d04c IK |
247 | case Opt_ignore: |
248 | sbi->flags |= AUTOFS_SBI_IGNORE; | |
249 | break; | |
ebc921ca IK |
250 | default: |
251 | return 1; | |
252 | } | |
253 | } | |
9bf964c9 | 254 | return (sbi->pipefd < 0); |
ebc921ca IK |
255 | } |
256 | ||
a7467430 | 257 | static struct autofs_sb_info *autofs_alloc_sbi(void) |
ebc921ca | 258 | { |
ebc921ca | 259 | struct autofs_sb_info *sbi; |
ebc921ca IK |
260 | |
261 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); | |
262 | if (!sbi) | |
a7467430 | 263 | return NULL; |
ebc921ca | 264 | |
ebc921ca | 265 | sbi->magic = AUTOFS_SBI_MAGIC; |
9d8719a4 | 266 | sbi->flags = AUTOFS_SBI_CATATONIC; |
a7467430 IK |
267 | sbi->min_proto = AUTOFS_MIN_PROTO_VERSION; |
268 | sbi->max_proto = AUTOFS_MAX_PROTO_VERSION; | |
269 | sbi->pipefd = -1; | |
270 | ||
ebc921ca | 271 | set_autofs_type_indirect(&sbi->type); |
ebc921ca IK |
272 | mutex_init(&sbi->wq_mutex); |
273 | mutex_init(&sbi->pipe_mutex); | |
274 | spin_lock_init(&sbi->fs_lock); | |
ebc921ca IK |
275 | spin_lock_init(&sbi->lookup_lock); |
276 | INIT_LIST_HEAD(&sbi->active_list); | |
277 | INIT_LIST_HEAD(&sbi->expiring_list); | |
a7467430 IK |
278 | |
279 | return sbi; | |
280 | } | |
281 | ||
282 | int autofs_fill_super(struct super_block *s, void *data, int silent) | |
283 | { | |
284 | struct inode *root_inode; | |
285 | struct dentry *root; | |
286 | struct autofs_sb_info *sbi; | |
287 | struct autofs_info *ino; | |
288 | int pgrp = 0; | |
289 | bool pgrp_set = false; | |
290 | int ret = -EINVAL; | |
291 | ||
292 | sbi = autofs_alloc_sbi(); | |
293 | if (!sbi) | |
294 | return -ENOMEM; | |
295 | ||
296 | pr_debug("starting up, sbi = %p\n", sbi); | |
297 | ||
298 | sbi->sb = s; | |
299 | s->s_fs_info = sbi; | |
ebc921ca IK |
300 | s->s_blocksize = 1024; |
301 | s->s_blocksize_bits = 10; | |
302 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
303 | s->s_op = &autofs_sops; | |
304 | s->s_d_op = &autofs_dentry_operations; | |
305 | s->s_time_gran = 1; | |
306 | ||
307 | /* | |
308 | * Get the root inode and dentry, but defer checking for errors. | |
309 | */ | |
310 | ino = autofs_new_ino(sbi); | |
311 | if (!ino) { | |
312 | ret = -ENOMEM; | |
313 | goto fail_free; | |
314 | } | |
315 | root_inode = autofs_get_inode(s, S_IFDIR | 0755); | |
316 | root = d_make_root(root_inode); | |
f585b283 IK |
317 | if (!root) { |
318 | ret = -ENOMEM; | |
ebc921ca | 319 | goto fail_ino; |
f585b283 | 320 | } |
ebc921ca IK |
321 | |
322 | root->d_fsdata = ino; | |
323 | ||
324 | /* Can this call block? */ | |
9bf964c9 | 325 | if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) { |
ebc921ca IK |
326 | pr_err("called with bogus options\n"); |
327 | goto fail_dput; | |
328 | } | |
329 | ||
330 | /* Test versions first */ | |
331 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || | |
332 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
333 | pr_err("kernel does not match daemon version " | |
334 | "daemon (%d, %d) kernel (%d, %d)\n", | |
335 | sbi->min_proto, sbi->max_proto, | |
336 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); | |
337 | goto fail_dput; | |
338 | } | |
339 | ||
340 | /* Establish highest kernel protocol version */ | |
341 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) | |
342 | sbi->version = AUTOFS_MAX_PROTO_VERSION; | |
343 | else | |
344 | sbi->version = sbi->max_proto; | |
345 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; | |
346 | ||
347 | if (pgrp_set) { | |
348 | sbi->oz_pgrp = find_get_pid(pgrp); | |
349 | if (!sbi->oz_pgrp) { | |
350 | pr_err("could not find process group %d\n", | |
351 | pgrp); | |
352 | goto fail_dput; | |
353 | } | |
354 | } else { | |
355 | sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); | |
356 | } | |
357 | ||
358 | if (autofs_type_trigger(sbi->type)) | |
359 | __managed_dentry_set_managed(root); | |
360 | ||
361 | root_inode->i_fop = &autofs_root_operations; | |
362 | root_inode->i_op = &autofs_dir_inode_operations; | |
363 | ||
9bf964c9 IK |
364 | pr_debug("pipe fd = %d, pgrp = %u\n", |
365 | sbi->pipefd, pid_nr(sbi->oz_pgrp)); | |
ebc921ca | 366 | |
9d8719a4 | 367 | sbi->flags &= ~AUTOFS_SBI_CATATONIC; |
ebc921ca IK |
368 | |
369 | /* | |
370 | * Success! Install the root dentry now to indicate completion. | |
371 | */ | |
372 | s->s_root = root; | |
373 | return 0; | |
374 | ||
375 | /* | |
376 | * Failure ... clean up. | |
377 | */ | |
ebc921ca IK |
378 | fail_dput: |
379 | dput(root); | |
380 | goto fail_free; | |
381 | fail_ino: | |
382 | autofs_free_ino(ino); | |
383 | fail_free: | |
384 | kfree(sbi); | |
385 | s->s_fs_info = NULL; | |
386 | return ret; | |
387 | } | |
388 | ||
389 | struct inode *autofs_get_inode(struct super_block *sb, umode_t mode) | |
390 | { | |
391 | struct inode *inode = new_inode(sb); | |
392 | ||
393 | if (inode == NULL) | |
394 | return NULL; | |
395 | ||
396 | inode->i_mode = mode; | |
397 | if (sb->s_root) { | |
398 | inode->i_uid = d_inode(sb->s_root)->i_uid; | |
399 | inode->i_gid = d_inode(sb->s_root)->i_gid; | |
400 | } | |
36aa5eae | 401 | inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode); |
ebc921ca IK |
402 | inode->i_ino = get_next_ino(); |
403 | ||
404 | if (S_ISDIR(mode)) { | |
405 | set_nlink(inode, 2); | |
406 | inode->i_op = &autofs_dir_inode_operations; | |
407 | inode->i_fop = &autofs_dir_operations; | |
408 | } else if (S_ISLNK(mode)) { | |
409 | inode->i_op = &autofs_symlink_inode_operations; | |
410 | } else | |
411 | WARN_ON(1); | |
412 | ||
413 | return inode; | |
414 | } |