1/* -*- linux-c -*- --------------------------------------------------------- *
2 *
3 * linux/fs/autofs/inode.c
4 *
5 *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 *
7 * This file is part of the Linux kernel and is made available under
8 * the terms of the GNU General Public License, version 2, or at your
9 * option, any later version, incorporated herein by reference.
10 *
11 * ------------------------------------------------------------------------- */
12
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/file.h>
17#include <linux/parser.h>
18#include <linux/bitops.h>
19#include <linux/magic.h>
20#include "autofs_i.h"
21#include <linux/module.h>
22
23void autofs_kill_sb(struct super_block *sb)
24{
25	struct autofs_sb_info *sbi = autofs_sbi(sb);
26	unsigned int n;
27
28	/*
29	 * In the event of a failure in get_sb_nodev the superblock
30	 * info is not present so nothing else has been setup, so
31	 * just call kill_anon_super when we are called from
32	 * deactivate_super.
33	 */
34	if (!sbi)
35		goto out_kill_sb;
36
37	if (!sbi->catatonic)
38		autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
39
40	put_pid(sbi->oz_pgrp);
41
42	autofs_hash_nuke(sbi);
43	for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) {
44		if (test_bit(n, sbi->symlink_bitmap))
45			kfree(sbi->symlink[n].data);
46	}
47
48	kfree(sb->s_fs_info);
49
50out_kill_sb:
51	DPRINTK(("autofs: shutting down\n"));
52	kill_anon_super(sb);
53}
54
55static void autofs_read_inode(struct inode *inode);
56
57static const struct super_operations autofs_sops = {
58	.read_inode	= autofs_read_inode,
59	.statfs		= simple_statfs,
60};
61
62enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
63
64static match_table_t autofs_tokens = {
65	{Opt_fd, "fd=%u"},
66	{Opt_uid, "uid=%u"},
67	{Opt_gid, "gid=%u"},
68	{Opt_pgrp, "pgrp=%u"},
69	{Opt_minproto, "minproto=%u"},
70	{Opt_maxproto, "maxproto=%u"},
71	{Opt_err, NULL}
72};
73
74static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
75		pid_t *pgrp, int *minproto, int *maxproto)
76{
77	char *p;
78	substring_t args[MAX_OPT_ARGS];
79	int option;
80
81	*uid = current->uid;
82	*gid = current->gid;
83	*pgrp = process_group(current);
84
85	*minproto = *maxproto = AUTOFS_PROTO_VERSION;
86
87	*pipefd = -1;
88
89	if (!options)
90		return 1;
91
92	while ((p = strsep(&options, ",")) != NULL) {
93		int token;
94		if (!*p)
95			continue;
96
97		token = match_token(p, autofs_tokens, args);
98		switch (token) {
99		case Opt_fd:
100			if (match_int(&args[0], &option))
101				return 1;
102			*pipefd = option;
103			break;
104		case Opt_uid:
105			if (match_int(&args[0], &option))
106				return 1;
107			*uid = option;
108			break;
109		case Opt_gid:
110			if (match_int(&args[0], &option))
111				return 1;
112			*gid = option;
113			break;
114		case Opt_pgrp:
115			if (match_int(&args[0], &option))
116				return 1;
117			*pgrp = option;
118			break;
119		case Opt_minproto:
120			if (match_int(&args[0], &option))
121				return 1;
122			*minproto = option;
123			break;
124		case Opt_maxproto:
125			if (match_int(&args[0], &option))
126				return 1;
127			*maxproto = option;
128			break;
129		default:
130			return 1;
131		}
132	}
133	return (*pipefd < 0);
134}
135
136int autofs_fill_super(struct super_block *s, void *data, int silent)
137{
138	struct inode * root_inode;
139	struct dentry * root;
140	struct file * pipe;
141	int pipefd;
142	struct autofs_sb_info *sbi;
143	int minproto, maxproto;
144	pid_t pgid;
145
146	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
147	if (!sbi)
148		goto fail_unlock;
149	DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
150
151	s->s_fs_info = sbi;
152	sbi->magic = AUTOFS_SBI_MAGIC;
153	sbi->pipe = NULL;
154	sbi->catatonic = 1;
155	sbi->exp_timeout = 0;
156	autofs_initialize_hash(&sbi->dirhash);
157	sbi->queues = NULL;
158	memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
159	sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
160	s->s_blocksize = 1024;
161	s->s_blocksize_bits = 10;
162	s->s_magic = AUTOFS_SUPER_MAGIC;
163	s->s_op = &autofs_sops;
164	s->s_time_gran = 1;
165	sbi->sb = s;
166
167	root_inode = iget(s, AUTOFS_ROOT_INO);
168	root = d_alloc_root(root_inode);
169	pipe = NULL;
170
171	if (!root)
172		goto fail_iput;
173
174	/* Can this call block?  - WTF cares? s is locked. */
175	if (parse_options(data, &pipefd, &root_inode->i_uid,
176				&root_inode->i_gid, &pgid, &minproto,
177				&maxproto)) {
178		printk("autofs: called with bogus options\n");
179		goto fail_dput;
180	}
181
182	/* Couldn't this be tested earlier? */
183	if (minproto > AUTOFS_PROTO_VERSION ||
184	     maxproto < AUTOFS_PROTO_VERSION) {
185		printk("autofs: kernel does not match daemon version\n");
186		goto fail_dput;
187	}
188
189	DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid));
190	sbi->oz_pgrp = find_get_pid(pgid);
191
192	if (!sbi->oz_pgrp) {
193		printk("autofs: could not find process group %d\n", pgid);
194		goto fail_dput;
195	}
196
197	pipe = fget(pipefd);
198
199	if (!pipe) {
200		printk("autofs: could not open pipe file descriptor\n");
201		goto fail_put_pid;
202	}
203
204	if (!pipe->f_op || !pipe->f_op->write)
205		goto fail_fput;
206	sbi->pipe = pipe;
207	sbi->catatonic = 0;
208
209	/*
210	 * Success! Install the root dentry now to indicate completion.
211	 */
212	s->s_root = root;
213	return 0;
214
215fail_fput:
216	printk("autofs: pipe file descriptor does not contain proper ops\n");
217	fput(pipe);
218fail_put_pid:
219	put_pid(sbi->oz_pgrp);
220fail_dput:
221	dput(root);
222	goto fail_free;
223fail_iput:
224	printk("autofs: get root dentry failed\n");
225	iput(root_inode);
226fail_free:
227	kfree(sbi);
228	s->s_fs_info = NULL;
229fail_unlock:
230	return -EINVAL;
231}
232
233static void autofs_read_inode(struct inode *inode)
234{
235	ino_t ino = inode->i_ino;
236	unsigned int n;
237	struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
238
239	/* Initialize to the default case (stub directory) */
240
241	inode->i_op = &simple_dir_inode_operations;
242	inode->i_fop = &simple_dir_operations;
243	inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
244	inode->i_nlink = 2;
245	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
246	inode->i_blocks = 0;
247
248	if (ino == AUTOFS_ROOT_INO) {
249		inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
250		inode->i_op = &autofs_root_inode_operations;
251		inode->i_fop = &autofs_root_operations;
252		inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
253		return;
254	}
255
256	inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
257	inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
258
259	if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
260		/* Symlink inode - should be in symlink list */
261		struct autofs_symlink *sl;
262
263		n = ino - AUTOFS_FIRST_SYMLINK;
264		if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
265			printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
266			return;
267		}
268
269		inode->i_op = &autofs_symlink_inode_operations;
270		sl = &sbi->symlink[n];
271		inode->i_private = sl;
272		inode->i_mode = S_IFLNK | S_IRWXUGO;
273		inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
274		inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
275		inode->i_size = sl->len;
276		inode->i_nlink = 1;
277	}
278}
279