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