Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6-block.git] / fs / autofs / inode.c
CommitLineData
1da177e4
LT
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>
e18fa700 19#include <linux/magic.h>
1da177e4
LT
20#include "autofs_i.h"
21#include <linux/module.h>
22
0e7d7382 23void autofs_kill_sb(struct super_block *sb)
1da177e4
LT
24{
25 struct autofs_sb_info *sbi = autofs_sbi(sb);
26 unsigned int n;
27
ba8df43c
IK
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
c949d4eb
JK
31 * just call kill_anon_super when we are called from
32 * deactivate_super.
ba8df43c
IK
33 */
34 if (!sbi)
c949d4eb 35 goto out_kill_sb;
ba8df43c 36
d78e53c8 37 if (!sbi->catatonic)
1da177e4
LT
38 autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
39
fa0334f1
SB
40 put_pid(sbi->oz_pgrp);
41
f76baf93 42 autofs_hash_nuke(sbi);
d78e53c8
SB
43 for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) {
44 if (test_bit(n, sbi->symlink_bitmap))
1da177e4
LT
45 kfree(sbi->symlink[n].data);
46 }
47
48 kfree(sb->s_fs_info);
49
c949d4eb 50out_kill_sb:
1da177e4 51 DPRINTK(("autofs: shutting down\n"));
0e7d7382 52 kill_anon_super(sb);
1da177e4
LT
53}
54
55static void autofs_read_inode(struct inode *inode);
56
ee9b6d61 57static const struct super_operations autofs_sops = {
1da177e4 58 .read_inode = autofs_read_inode,
1da177e4
LT
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
d78e53c8
SB
74static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
75 pid_t *pgrp, int *minproto, int *maxproto)
1da177e4
LT
76{
77 char *p;
78 substring_t args[MAX_OPT_ARGS];
79 int option;
80
81 *uid = current->uid;
82 *gid = current->gid;
a47afb0f 83 *pgrp = task_pgrp_nr(current);
1da177e4
LT
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;
fa0334f1 144 pid_t pgid;
1da177e4 145
f8314dc6 146 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
d78e53c8 147 if (!sbi)
1da177e4 148 goto fail_unlock;
1da177e4
LT
149 DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
150
151 s->s_fs_info = sbi;
152 sbi->magic = AUTOFS_SBI_MAGIC;
ba8df43c
IK
153 sbi->pipe = NULL;
154 sbi->catatonic = 1;
1da177e4 155 sbi->exp_timeout = 0;
1da177e4
LT
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;
f76baf93 165 sbi->sb = s;
1da177e4
LT
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. */
d78e53c8 175 if (parse_options(data, &pipefd, &root_inode->i_uid,
fa0334f1 176 &root_inode->i_gid, &pgid, &minproto,
d78e53c8 177 &maxproto)) {
1da177e4
LT
178 printk("autofs: called with bogus options\n");
179 goto fail_dput;
180 }
181
182 /* Couldn't this be tested earlier? */
d78e53c8
SB
183 if (minproto > AUTOFS_PROTO_VERSION ||
184 maxproto < AUTOFS_PROTO_VERSION) {
1da177e4
LT
185 printk("autofs: kernel does not match daemon version\n");
186 goto fail_dput;
187 }
188
fa0334f1
SB
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
1da177e4
LT
197 pipe = fget(pipefd);
198
d78e53c8 199 if (!pipe) {
1da177e4 200 printk("autofs: could not open pipe file descriptor\n");
fa0334f1 201 goto fail_put_pid;
1da177e4 202 }
fa0334f1 203
d78e53c8 204 if (!pipe->f_op || !pipe->f_op->write)
1da177e4
LT
205 goto fail_fput;
206 sbi->pipe = pipe;
ba8df43c 207 sbi->catatonic = 0;
1da177e4
LT
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);
fa0334f1
SB
218fail_put_pid:
219 put_pid(sbi->oz_pgrp);
1da177e4
LT
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);
ba8df43c 228 s->s_fs_info = NULL;
1da177e4
LT
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;
1da177e4 247
d78e53c8 248 if (ino == AUTOFS_ROOT_INO) {
1da177e4
LT
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
d78e53c8 259 if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
1da177e4
LT
260 /* Symlink inode - should be in symlink list */
261 struct autofs_symlink *sl;
262
263 n = ino - AUTOFS_FIRST_SYMLINK;
d78e53c8 264 if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
1da177e4
LT
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];
8e18e294 271 inode->i_private = sl;
1da177e4
LT
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}