xref: /linux/fs/autofs/inode.c (revision 39fb993038e158d4cbba47f6dbda6fe77a0e8cb9)
1 /*
2  * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3  * Copyright 2005-2006 Ian Kent <raven@themaw.net>
4  *
5  * This file is part of the Linux kernel and is made available under
6  * the terms of the GNU General Public License, version 2, or at your
7  * option, any later version, incorporated herein by reference.
8  */
9 
10 #include <linux/seq_file.h>
11 #include <linux/pagemap.h>
12 #include <linux/parser.h>
13 #include <linux/magic.h>
14 
15 #include "autofs_i.h"
16 
17 struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
18 {
19 	struct autofs_info *ino;
20 
21 	ino = kzalloc(sizeof(*ino), GFP_KERNEL);
22 	if (ino) {
23 		INIT_LIST_HEAD(&ino->active);
24 		INIT_LIST_HEAD(&ino->expiring);
25 		ino->last_used = jiffies;
26 		ino->sbi = sbi;
27 	}
28 	return ino;
29 }
30 
31 void autofs_clean_ino(struct autofs_info *ino)
32 {
33 	ino->uid = GLOBAL_ROOT_UID;
34 	ino->gid = GLOBAL_ROOT_GID;
35 	ino->last_used = jiffies;
36 }
37 
38 void autofs_free_ino(struct autofs_info *ino)
39 {
40 	kfree(ino);
41 }
42 
43 void autofs_kill_sb(struct super_block *sb)
44 {
45 	struct autofs_sb_info *sbi = autofs_sbi(sb);
46 
47 	/*
48 	 * In the event of a failure in get_sb_nodev the superblock
49 	 * info is not present so nothing else has been setup, so
50 	 * just call kill_anon_super when we are called from
51 	 * deactivate_super.
52 	 */
53 	if (sbi) {
54 		/* Free wait queues, close pipe */
55 		autofs_catatonic_mode(sbi);
56 		put_pid(sbi->oz_pgrp);
57 	}
58 
59 	pr_debug("shutting down\n");
60 	kill_litter_super(sb);
61 	if (sbi)
62 		kfree_rcu(sbi, rcu);
63 }
64 
65 static int autofs_show_options(struct seq_file *m, struct dentry *root)
66 {
67 	struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
68 	struct inode *root_inode = d_inode(root->d_sb->s_root);
69 
70 	if (!sbi)
71 		return 0;
72 
73 	seq_printf(m, ",fd=%d", sbi->pipefd);
74 	if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
75 		seq_printf(m, ",uid=%u",
76 			from_kuid_munged(&init_user_ns, root_inode->i_uid));
77 	if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
78 		seq_printf(m, ",gid=%u",
79 			from_kgid_munged(&init_user_ns, root_inode->i_gid));
80 	seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
81 	seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
82 	seq_printf(m, ",minproto=%d", sbi->min_proto);
83 	seq_printf(m, ",maxproto=%d", sbi->max_proto);
84 
85 	if (autofs_type_offset(sbi->type))
86 		seq_printf(m, ",offset");
87 	else if (autofs_type_direct(sbi->type))
88 		seq_printf(m, ",direct");
89 	else
90 		seq_printf(m, ",indirect");
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_printf(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};
114 
115 static const match_table_t tokens = {
116 	{Opt_fd, "fd=%u"},
117 	{Opt_uid, "uid=%u"},
118 	{Opt_gid, "gid=%u"},
119 	{Opt_pgrp, "pgrp=%u"},
120 	{Opt_minproto, "minproto=%u"},
121 	{Opt_maxproto, "maxproto=%u"},
122 	{Opt_indirect, "indirect"},
123 	{Opt_direct, "direct"},
124 	{Opt_offset, "offset"},
125 	{Opt_err, NULL}
126 };
127 
128 static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
129 			 int *pgrp, bool *pgrp_set, unsigned int *type,
130 			 int *minproto, int *maxproto)
131 {
132 	char *p;
133 	substring_t args[MAX_OPT_ARGS];
134 	int option;
135 
136 	*uid = current_uid();
137 	*gid = current_gid();
138 
139 	*minproto = AUTOFS_MIN_PROTO_VERSION;
140 	*maxproto = AUTOFS_MAX_PROTO_VERSION;
141 
142 	*pipefd = -1;
143 
144 	if (!options)
145 		return 1;
146 
147 	while ((p = strsep(&options, ",")) != NULL) {
148 		int token;
149 
150 		if (!*p)
151 			continue;
152 
153 		token = match_token(p, tokens, args);
154 		switch (token) {
155 		case Opt_fd:
156 			if (match_int(args, pipefd))
157 				return 1;
158 			break;
159 		case Opt_uid:
160 			if (match_int(args, &option))
161 				return 1;
162 			*uid = make_kuid(current_user_ns(), option);
163 			if (!uid_valid(*uid))
164 				return 1;
165 			break;
166 		case Opt_gid:
167 			if (match_int(args, &option))
168 				return 1;
169 			*gid = make_kgid(current_user_ns(), option);
170 			if (!gid_valid(*gid))
171 				return 1;
172 			break;
173 		case Opt_pgrp:
174 			if (match_int(args, &option))
175 				return 1;
176 			*pgrp = option;
177 			*pgrp_set = true;
178 			break;
179 		case Opt_minproto:
180 			if (match_int(args, &option))
181 				return 1;
182 			*minproto = option;
183 			break;
184 		case Opt_maxproto:
185 			if (match_int(args, &option))
186 				return 1;
187 			*maxproto = option;
188 			break;
189 		case Opt_indirect:
190 			set_autofs_type_indirect(type);
191 			break;
192 		case Opt_direct:
193 			set_autofs_type_direct(type);
194 			break;
195 		case Opt_offset:
196 			set_autofs_type_offset(type);
197 			break;
198 		default:
199 			return 1;
200 		}
201 	}
202 	return (*pipefd < 0);
203 }
204 
205 int autofs_fill_super(struct super_block *s, void *data, int silent)
206 {
207 	struct inode *root_inode;
208 	struct dentry *root;
209 	struct file *pipe;
210 	int pipefd;
211 	struct autofs_sb_info *sbi;
212 	struct autofs_info *ino;
213 	int pgrp = 0;
214 	bool pgrp_set = false;
215 	int ret = -EINVAL;
216 
217 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
218 	if (!sbi)
219 		return -ENOMEM;
220 	pr_debug("starting up, sbi = %p\n", sbi);
221 
222 	s->s_fs_info = sbi;
223 	sbi->magic = AUTOFS_SBI_MAGIC;
224 	sbi->pipefd = -1;
225 	sbi->pipe = NULL;
226 	sbi->catatonic = 1;
227 	sbi->exp_timeout = 0;
228 	sbi->oz_pgrp = NULL;
229 	sbi->sb = s;
230 	sbi->version = 0;
231 	sbi->sub_version = 0;
232 	set_autofs_type_indirect(&sbi->type);
233 	sbi->min_proto = 0;
234 	sbi->max_proto = 0;
235 	mutex_init(&sbi->wq_mutex);
236 	mutex_init(&sbi->pipe_mutex);
237 	spin_lock_init(&sbi->fs_lock);
238 	sbi->queues = NULL;
239 	spin_lock_init(&sbi->lookup_lock);
240 	INIT_LIST_HEAD(&sbi->active_list);
241 	INIT_LIST_HEAD(&sbi->expiring_list);
242 	s->s_blocksize = 1024;
243 	s->s_blocksize_bits = 10;
244 	s->s_magic = AUTOFS_SUPER_MAGIC;
245 	s->s_op = &autofs_sops;
246 	s->s_d_op = &autofs_dentry_operations;
247 	s->s_time_gran = 1;
248 
249 	/*
250 	 * Get the root inode and dentry, but defer checking for errors.
251 	 */
252 	ino = autofs_new_ino(sbi);
253 	if (!ino) {
254 		ret = -ENOMEM;
255 		goto fail_free;
256 	}
257 	root_inode = autofs_get_inode(s, S_IFDIR | 0755);
258 	root = d_make_root(root_inode);
259 	if (!root)
260 		goto fail_ino;
261 	pipe = NULL;
262 
263 	root->d_fsdata = ino;
264 
265 	/* Can this call block? */
266 	if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
267 			  &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
268 			  &sbi->max_proto)) {
269 		pr_err("called with bogus options\n");
270 		goto fail_dput;
271 	}
272 
273 	/* Test versions first */
274 	if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
275 	    sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
276 		pr_err("kernel does not match daemon version "
277 		       "daemon (%d, %d) kernel (%d, %d)\n",
278 		       sbi->min_proto, sbi->max_proto,
279 		       AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
280 		goto fail_dput;
281 	}
282 
283 	/* Establish highest kernel protocol version */
284 	if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
285 		sbi->version = AUTOFS_MAX_PROTO_VERSION;
286 	else
287 		sbi->version = sbi->max_proto;
288 	sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
289 
290 	if (pgrp_set) {
291 		sbi->oz_pgrp = find_get_pid(pgrp);
292 		if (!sbi->oz_pgrp) {
293 			pr_err("could not find process group %d\n",
294 				pgrp);
295 			goto fail_dput;
296 		}
297 	} else {
298 		sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
299 	}
300 
301 	if (autofs_type_trigger(sbi->type))
302 		__managed_dentry_set_managed(root);
303 
304 	root_inode->i_fop = &autofs_root_operations;
305 	root_inode->i_op = &autofs_dir_inode_operations;
306 
307 	pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp));
308 	pipe = fget(pipefd);
309 
310 	if (!pipe) {
311 		pr_err("could not open pipe file descriptor\n");
312 		goto fail_put_pid;
313 	}
314 	ret = autofs_prepare_pipe(pipe);
315 	if (ret < 0)
316 		goto fail_fput;
317 	sbi->pipe = pipe;
318 	sbi->pipefd = pipefd;
319 	sbi->catatonic = 0;
320 
321 	/*
322 	 * Success! Install the root dentry now to indicate completion.
323 	 */
324 	s->s_root = root;
325 	return 0;
326 
327 	/*
328 	 * Failure ... clean up.
329 	 */
330 fail_fput:
331 	pr_err("pipe file descriptor does not contain proper ops\n");
332 	fput(pipe);
333 fail_put_pid:
334 	put_pid(sbi->oz_pgrp);
335 fail_dput:
336 	dput(root);
337 	goto fail_free;
338 fail_ino:
339 	autofs_free_ino(ino);
340 fail_free:
341 	kfree(sbi);
342 	s->s_fs_info = NULL;
343 	return ret;
344 }
345 
346 struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
347 {
348 	struct inode *inode = new_inode(sb);
349 
350 	if (inode == NULL)
351 		return NULL;
352 
353 	inode->i_mode = mode;
354 	if (sb->s_root) {
355 		inode->i_uid = d_inode(sb->s_root)->i_uid;
356 		inode->i_gid = d_inode(sb->s_root)->i_gid;
357 	}
358 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
359 	inode->i_ino = get_next_ino();
360 
361 	if (S_ISDIR(mode)) {
362 		set_nlink(inode, 2);
363 		inode->i_op = &autofs_dir_inode_operations;
364 		inode->i_fop = &autofs_dir_operations;
365 	} else if (S_ISLNK(mode)) {
366 		inode->i_op = &autofs_symlink_inode_operations;
367 	} else
368 		WARN_ON(1);
369 
370 	return inode;
371 }
372