autofs: fix protocol sub version setting
[linux-2.6-block.git] / fs / autofs / inode.c
CommitLineData
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
12struct 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
27void 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
34void autofs_free_ino(struct autofs_info *ino)
35{
ce285c26 36 kfree_rcu(ino, rcu);
ebc921ca
IK
37}
38
39void 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
61static 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
100static void autofs_evict_inode(struct inode *inode)
101{
102 clear_inode(inode);
103 kfree(inode->i_private);
104}
105
106static 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 112enum {
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
126const 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
141struct 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 */
153static 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
180 if (sbi->pipe)
181 fput(sbi->pipe);
182
e6ec453b 183 sbi->pipefd = result->uint_32;
546694b8
IK
184 sbi->pipe = pipe;
185
186 return 0;
187}
188
e6ec453b 189static int autofs_parse_param(struct fs_context *fc, struct fs_parameter *param)
ebc921ca 190{
e6ec453b
IK
191 struct autofs_fs_context *ctx = fc->fs_private;
192 struct autofs_sb_info *sbi = fc->s_fs_info;
193 struct fs_parse_result result;
9bf964c9
IK
194 kuid_t uid;
195 kgid_t gid;
e6ec453b
IK
196 int opt;
197
198 opt = fs_parse(fc, autofs_param_specs, param, &result);
199 if (opt < 0)
200 return opt;
ebc921ca 201
e6ec453b 202 switch (opt) {
9b273166 203 case Opt_fd:
e6ec453b 204 return autofs_parse_fd(fc, sbi, param, &result);
9b273166 205 case Opt_uid:
e6ec453b 206 uid = make_kuid(current_user_ns(), result.uint_32);
9b273166 207 if (!uid_valid(uid))
e6ec453b
IK
208 return invalfc(fc, "Invalid uid");
209 ctx->uid = uid;
9b273166
IK
210 break;
211 case Opt_gid:
e6ec453b 212 gid = make_kgid(current_user_ns(), result.uint_32);
9b273166 213 if (!gid_valid(gid))
e6ec453b
IK
214 return invalfc(fc, "Invalid gid");
215 ctx->gid = gid;
9b273166
IK
216 break;
217 case Opt_pgrp:
e6ec453b
IK
218 ctx->pgrp = result.uint_32;
219 ctx->pgrp_set = true;
9b273166
IK
220 break;
221 case Opt_minproto:
e6ec453b 222 sbi->min_proto = result.uint_32;
9b273166
IK
223 break;
224 case Opt_maxproto:
e6ec453b 225 sbi->max_proto = result.uint_32;
9b273166
IK
226 break;
227 case Opt_indirect:
228 set_autofs_type_indirect(&sbi->type);
229 break;
230 case Opt_direct:
231 set_autofs_type_direct(&sbi->type);
232 break;
233 case Opt_offset:
234 set_autofs_type_offset(&sbi->type);
235 break;
236 case Opt_strictexpire:
237 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
238 break;
239 case Opt_ignore:
240 sbi->flags |= AUTOFS_SBI_IGNORE;
241 }
242
243 return 0;
244}
245
a7467430 246static struct autofs_sb_info *autofs_alloc_sbi(void)
ebc921ca 247{
ebc921ca 248 struct autofs_sb_info *sbi;
ebc921ca
IK
249
250 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
251 if (!sbi)
a7467430 252 return NULL;
ebc921ca 253
ebc921ca 254 sbi->magic = AUTOFS_SBI_MAGIC;
9d8719a4 255 sbi->flags = AUTOFS_SBI_CATATONIC;
a7467430
IK
256 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
257 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
258 sbi->pipefd = -1;
259
ebc921ca 260 set_autofs_type_indirect(&sbi->type);
ebc921ca
IK
261 mutex_init(&sbi->wq_mutex);
262 mutex_init(&sbi->pipe_mutex);
263 spin_lock_init(&sbi->fs_lock);
ebc921ca
IK
264 spin_lock_init(&sbi->lookup_lock);
265 INIT_LIST_HEAD(&sbi->active_list);
266 INIT_LIST_HEAD(&sbi->expiring_list);
a7467430
IK
267
268 return sbi;
269}
270
e6ec453b 271static int autofs_validate_protocol(struct fs_context *fc)
1f50012d 272{
e6ec453b
IK
273 struct autofs_sb_info *sbi = fc->s_fs_info;
274
1f50012d
IK
275 /* Test versions first */
276 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
277 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
e6ec453b 278 errorf(fc, "kernel does not match daemon version "
1f50012d
IK
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;
dede3671
IK
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 }
1f50012d
IK
301
302 return 0;
303}
304
e6ec453b 305static int autofs_fill_super(struct super_block *s, struct fs_context *fc)
a7467430 306{
e6ec453b
IK
307 struct autofs_fs_context *ctx = fc->fs_private;
308 struct autofs_sb_info *sbi = s->s_fs_info;
a7467430
IK
309 struct inode *root_inode;
310 struct dentry *root;
a7467430 311 struct autofs_info *ino;
e6ec453b 312 int ret = -ENOMEM;
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
IK
328 if (!ino)
329 goto fail;
330
ebc921ca 331 root_inode = autofs_get_inode(s, S_IFDIR | 0755);
e6ec453b
IK
332 root_inode->i_uid = ctx->uid;
333 root_inode->i_gid = ctx->gid;
334
ebc921ca 335 root = d_make_root(root_inode);
e6ec453b 336 if (!root)
ebc921ca 337 goto fail_ino;
ebc921ca
IK
338
339 root->d_fsdata = ino;
340
e6ec453b
IK
341 if (ctx->pgrp_set) {
342 sbi->oz_pgrp = find_get_pid(ctx->pgrp);
ebc921ca 343 if (!sbi->oz_pgrp) {
e6ec453b
IK
344 ret = invalf(fc, "Could not find process group %d",
345 ctx->pgrp);
ebc921ca
IK
346 goto fail_dput;
347 }
348 } else {
349 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
350 }
351
352 if (autofs_type_trigger(sbi->type))
353 __managed_dentry_set_managed(root);
354
355 root_inode->i_fop = &autofs_root_operations;
356 root_inode->i_op = &autofs_dir_inode_operations;
357
9bf964c9
IK
358 pr_debug("pipe fd = %d, pgrp = %u\n",
359 sbi->pipefd, pid_nr(sbi->oz_pgrp));
ebc921ca 360
9d8719a4 361 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
ebc921ca
IK
362
363 /*
364 * Success! Install the root dentry now to indicate completion.
365 */
366 s->s_root = root;
367 return 0;
368
369 /*
370 * Failure ... clean up.
371 */
ebc921ca
IK
372fail_dput:
373 dput(root);
e6ec453b 374 goto fail;
ebc921ca
IK
375fail_ino:
376 autofs_free_ino(ino);
e6ec453b 377fail:
ebc921ca
IK
378 return ret;
379}
380
e6ec453b
IK
381/*
382 * Validate the parameters and then request a superblock.
383 */
384static int autofs_get_tree(struct fs_context *fc)
385{
386 struct autofs_sb_info *sbi = fc->s_fs_info;
387 int ret;
388
389 ret = autofs_validate_protocol(fc);
390 if (ret)
391 return ret;
392
393 if (sbi->pipefd < 0)
394 return invalf(fc, "No control pipe specified");
395
396 return get_tree_nodev(fc, autofs_fill_super);
397}
398
399static void autofs_free_fc(struct fs_context *fc)
400{
401 struct autofs_fs_context *ctx = fc->fs_private;
402 struct autofs_sb_info *sbi = fc->s_fs_info;
403
404 if (sbi) {
405 if (sbi->pipe)
406 fput(sbi->pipe);
407 kfree(sbi);
408 }
409 kfree(ctx);
410}
411
412static const struct fs_context_operations autofs_context_ops = {
413 .free = autofs_free_fc,
414 .parse_param = autofs_parse_param,
415 .get_tree = autofs_get_tree,
416};
417
418/*
419 * Set up the filesystem mount context.
420 */
421int autofs_init_fs_context(struct fs_context *fc)
422{
423 struct autofs_fs_context *ctx;
424 struct autofs_sb_info *sbi;
425
426 ctx = kzalloc(sizeof(struct autofs_fs_context), GFP_KERNEL);
427 if (!ctx)
428 goto nomem;
429
430 ctx->uid = current_uid();
431 ctx->gid = current_gid();
432
433 sbi = autofs_alloc_sbi();
434 if (!sbi)
435 goto nomem_ctx;
436
437 fc->fs_private = ctx;
438 fc->s_fs_info = sbi;
439 fc->ops = &autofs_context_ops;
440 return 0;
441
442nomem_ctx:
443 kfree(ctx);
444nomem:
445 return -ENOMEM;
446}
447
ebc921ca
IK
448struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
449{
450 struct inode *inode = new_inode(sb);
451
452 if (inode == NULL)
453 return NULL;
454
455 inode->i_mode = mode;
456 if (sb->s_root) {
457 inode->i_uid = d_inode(sb->s_root)->i_uid;
458 inode->i_gid = d_inode(sb->s_root)->i_gid;
459 }
36aa5eae 460 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
ebc921ca
IK
461 inode->i_ino = get_next_ino();
462
463 if (S_ISDIR(mode)) {
464 set_nlink(inode, 2);
465 inode->i_op = &autofs_dir_inode_operations;
466 inode->i_fop = &autofs_dir_operations;
467 } else if (S_ISLNK(mode)) {
468 inode->i_op = &autofs_symlink_inode_operations;
469 } else
470 WARN_ON(1);
471
472 return inode;
473}