]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/autofs/inode.c
Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-focal-kernel.git] / fs / autofs / inode.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
5 */
6
7 #include <linux/seq_file.h>
8 #include <linux/pagemap.h>
9 #include <linux/parser.h>
10
11 #include "autofs_i.h"
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;
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 {
36 kfree_rcu(ino, rcu);
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))
82 seq_puts(m, ",offset");
83 else if (autofs_type_direct(sbi->type))
84 seq_puts(m, ",direct");
85 else
86 seq_puts(m, ",indirect");
87 if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE)
88 seq_puts(m, ",strictexpire");
89 if (sbi->flags & AUTOFS_SBI_IGNORE)
90 seq_puts(m, ",ignore");
91 #ifdef CONFIG_CHECKPOINT_RESTORE
92 if (sbi->pipe)
93 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
94 else
95 seq_puts(m, ",pipe_ino=-1");
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
112 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
113 Opt_indirect, Opt_direct, Opt_offset, Opt_strictexpire,
114 Opt_ignore};
115
116 static const match_table_t tokens = {
117 {Opt_fd, "fd=%u"},
118 {Opt_uid, "uid=%u"},
119 {Opt_gid, "gid=%u"},
120 {Opt_pgrp, "pgrp=%u"},
121 {Opt_minproto, "minproto=%u"},
122 {Opt_maxproto, "maxproto=%u"},
123 {Opt_indirect, "indirect"},
124 {Opt_direct, "direct"},
125 {Opt_offset, "offset"},
126 {Opt_strictexpire, "strictexpire"},
127 {Opt_ignore, "ignore"},
128 {Opt_err, NULL}
129 };
130
131 static int parse_options(char *options,
132 struct inode *root, int *pgrp, bool *pgrp_set,
133 struct autofs_sb_info *sbi)
134 {
135 char *p;
136 substring_t args[MAX_OPT_ARGS];
137 int option;
138 int pipefd = -1;
139 kuid_t uid;
140 kgid_t gid;
141
142 root->i_uid = current_uid();
143 root->i_gid = current_gid();
144
145 sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
146 sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
147
148 sbi->pipefd = -1;
149
150 if (!options)
151 return 1;
152
153 while ((p = strsep(&options, ",")) != NULL) {
154 int token;
155
156 if (!*p)
157 continue;
158
159 token = match_token(p, tokens, args);
160 switch (token) {
161 case Opt_fd:
162 if (match_int(args, &pipefd))
163 return 1;
164 sbi->pipefd = pipefd;
165 break;
166 case Opt_uid:
167 if (match_int(args, &option))
168 return 1;
169 uid = make_kuid(current_user_ns(), option);
170 if (!uid_valid(uid))
171 return 1;
172 root->i_uid = uid;
173 break;
174 case Opt_gid:
175 if (match_int(args, &option))
176 return 1;
177 gid = make_kgid(current_user_ns(), option);
178 if (!gid_valid(gid))
179 return 1;
180 root->i_gid = gid;
181 break;
182 case Opt_pgrp:
183 if (match_int(args, &option))
184 return 1;
185 *pgrp = option;
186 *pgrp_set = true;
187 break;
188 case Opt_minproto:
189 if (match_int(args, &option))
190 return 1;
191 sbi->min_proto = option;
192 break;
193 case Opt_maxproto:
194 if (match_int(args, &option))
195 return 1;
196 sbi->max_proto = option;
197 break;
198 case Opt_indirect:
199 set_autofs_type_indirect(&sbi->type);
200 break;
201 case Opt_direct:
202 set_autofs_type_direct(&sbi->type);
203 break;
204 case Opt_offset:
205 set_autofs_type_offset(&sbi->type);
206 break;
207 case Opt_strictexpire:
208 sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
209 break;
210 case Opt_ignore:
211 sbi->flags |= AUTOFS_SBI_IGNORE;
212 break;
213 default:
214 return 1;
215 }
216 }
217 return (sbi->pipefd < 0);
218 }
219
220 int autofs_fill_super(struct super_block *s, void *data, int silent)
221 {
222 struct inode *root_inode;
223 struct dentry *root;
224 struct file *pipe;
225 struct autofs_sb_info *sbi;
226 struct autofs_info *ino;
227 int pgrp = 0;
228 bool pgrp_set = false;
229 int ret = -EINVAL;
230
231 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
232 if (!sbi)
233 return -ENOMEM;
234 pr_debug("starting up, sbi = %p\n", sbi);
235
236 s->s_fs_info = sbi;
237 sbi->magic = AUTOFS_SBI_MAGIC;
238 sbi->pipefd = -1;
239 sbi->pipe = NULL;
240 sbi->exp_timeout = 0;
241 sbi->oz_pgrp = NULL;
242 sbi->sb = s;
243 sbi->version = 0;
244 sbi->sub_version = 0;
245 sbi->flags = AUTOFS_SBI_CATATONIC;
246 set_autofs_type_indirect(&sbi->type);
247 sbi->min_proto = 0;
248 sbi->max_proto = 0;
249 mutex_init(&sbi->wq_mutex);
250 mutex_init(&sbi->pipe_mutex);
251 spin_lock_init(&sbi->fs_lock);
252 sbi->queues = NULL;
253 spin_lock_init(&sbi->lookup_lock);
254 INIT_LIST_HEAD(&sbi->active_list);
255 INIT_LIST_HEAD(&sbi->expiring_list);
256 s->s_blocksize = 1024;
257 s->s_blocksize_bits = 10;
258 s->s_magic = AUTOFS_SUPER_MAGIC;
259 s->s_op = &autofs_sops;
260 s->s_d_op = &autofs_dentry_operations;
261 s->s_time_gran = 1;
262
263 /*
264 * Get the root inode and dentry, but defer checking for errors.
265 */
266 ino = autofs_new_ino(sbi);
267 if (!ino) {
268 ret = -ENOMEM;
269 goto fail_free;
270 }
271 root_inode = autofs_get_inode(s, S_IFDIR | 0755);
272 root = d_make_root(root_inode);
273 if (!root) {
274 ret = -ENOMEM;
275 goto fail_ino;
276 }
277 pipe = NULL;
278
279 root->d_fsdata = ino;
280
281 /* Can this call block? */
282 if (parse_options(data, root_inode, &pgrp, &pgrp_set, sbi)) {
283 pr_err("called with bogus options\n");
284 goto fail_dput;
285 }
286
287 /* Test versions first */
288 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
289 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
290 pr_err("kernel does not match daemon version "
291 "daemon (%d, %d) kernel (%d, %d)\n",
292 sbi->min_proto, sbi->max_proto,
293 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
294 goto fail_dput;
295 }
296
297 /* Establish highest kernel protocol version */
298 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
299 sbi->version = AUTOFS_MAX_PROTO_VERSION;
300 else
301 sbi->version = sbi->max_proto;
302 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
303
304 if (pgrp_set) {
305 sbi->oz_pgrp = find_get_pid(pgrp);
306 if (!sbi->oz_pgrp) {
307 pr_err("could not find process group %d\n",
308 pgrp);
309 goto fail_dput;
310 }
311 } else {
312 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
313 }
314
315 if (autofs_type_trigger(sbi->type))
316 __managed_dentry_set_managed(root);
317
318 root_inode->i_fop = &autofs_root_operations;
319 root_inode->i_op = &autofs_dir_inode_operations;
320
321 pr_debug("pipe fd = %d, pgrp = %u\n",
322 sbi->pipefd, pid_nr(sbi->oz_pgrp));
323 pipe = fget(sbi->pipefd);
324
325 if (!pipe) {
326 pr_err("could not open pipe file descriptor\n");
327 goto fail_put_pid;
328 }
329 ret = autofs_prepare_pipe(pipe);
330 if (ret < 0)
331 goto fail_fput;
332 sbi->pipe = pipe;
333 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
334
335 /*
336 * Success! Install the root dentry now to indicate completion.
337 */
338 s->s_root = root;
339 return 0;
340
341 /*
342 * Failure ... clean up.
343 */
344 fail_fput:
345 pr_err("pipe file descriptor does not contain proper ops\n");
346 fput(pipe);
347 fail_put_pid:
348 put_pid(sbi->oz_pgrp);
349 fail_dput:
350 dput(root);
351 goto fail_free;
352 fail_ino:
353 autofs_free_ino(ino);
354 fail_free:
355 kfree(sbi);
356 s->s_fs_info = NULL;
357 return ret;
358 }
359
360 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
361 {
362 struct inode *inode = new_inode(sb);
363
364 if (inode == NULL)
365 return NULL;
366
367 inode->i_mode = mode;
368 if (sb->s_root) {
369 inode->i_uid = d_inode(sb->s_root)->i_uid;
370 inode->i_gid = d_inode(sb->s_root)->i_gid;
371 }
372 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
373 inode->i_ino = get_next_ino();
374
375 if (S_ISDIR(mode)) {
376 set_nlink(inode, 2);
377 inode->i_op = &autofs_dir_inode_operations;
378 inode->i_fop = &autofs_dir_operations;
379 } else if (S_ISLNK(mode)) {
380 inode->i_op = &autofs_symlink_inode_operations;
381 } else
382 WARN_ON(1);
383
384 return inode;
385 }