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