[PATCH] autofs4: add a show mount options for proc filesystem
[deliverable/linux.git] / fs / autofs4 / inode.c
1 /* -*- 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/slab.h>
15 #include <linux/file.h>
16 #include <linux/seq_file.h>
17 #include <linux/pagemap.h>
18 #include <linux/parser.h>
19 #include <linux/bitops.h>
20 #include <linux/smp_lock.h>
21 #include "autofs_i.h"
22 #include <linux/module.h>
23
24 static void ino_lnkfree(struct autofs_info *ino)
25 {
26 kfree(ino->u.symlink);
27 ino->u.symlink = NULL;
28 }
29
30 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
31 struct autofs_sb_info *sbi, mode_t mode)
32 {
33 int reinit = 1;
34
35 if (ino == NULL) {
36 reinit = 0;
37 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
38 }
39
40 if (ino == NULL)
41 return NULL;
42
43 ino->flags = 0;
44 ino->mode = mode;
45 ino->inode = NULL;
46 ino->dentry = NULL;
47 ino->size = 0;
48
49 ino->last_used = jiffies;
50 atomic_set(&ino->count, 0);
51
52 ino->sbi = sbi;
53
54 if (reinit && ino->free)
55 (ino->free)(ino);
56
57 memset(&ino->u, 0, sizeof(ino->u));
58
59 ino->free = NULL;
60
61 if (S_ISLNK(mode))
62 ino->free = ino_lnkfree;
63
64 return ino;
65 }
66
67 void autofs4_free_ino(struct autofs_info *ino)
68 {
69 struct autofs_info *p_ino;
70
71 if (ino->dentry) {
72 ino->dentry->d_fsdata = NULL;
73 if (ino->dentry->d_inode) {
74 struct dentry *parent = ino->dentry->d_parent;
75 if (atomic_dec_and_test(&ino->count)) {
76 p_ino = autofs4_dentry_ino(parent);
77 if (p_ino && parent != ino->dentry)
78 atomic_dec(&p_ino->count);
79 }
80 dput(ino->dentry);
81 }
82 ino->dentry = NULL;
83 }
84 if (ino->free)
85 (ino->free)(ino);
86 kfree(ino);
87 }
88
89 /*
90 * Deal with the infamous "Busy inodes after umount ..." message.
91 *
92 * Clean up the dentry tree. This happens with autofs if the user
93 * space program goes away due to a SIGKILL, SIGSEGV etc.
94 */
95 static void autofs4_force_release(struct autofs_sb_info *sbi)
96 {
97 struct dentry *this_parent = sbi->root;
98 struct list_head *next;
99
100 spin_lock(&dcache_lock);
101 repeat:
102 next = this_parent->d_subdirs.next;
103 resume:
104 while (next != &this_parent->d_subdirs) {
105 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
106
107 /* Negative dentry - don`t care */
108 if (!simple_positive(dentry)) {
109 next = next->next;
110 continue;
111 }
112
113 if (!list_empty(&dentry->d_subdirs)) {
114 this_parent = dentry;
115 goto repeat;
116 }
117
118 next = next->next;
119 spin_unlock(&dcache_lock);
120
121 DPRINTK("dentry %p %.*s",
122 dentry, (int)dentry->d_name.len, dentry->d_name.name);
123
124 dput(dentry);
125 spin_lock(&dcache_lock);
126 }
127
128 if (this_parent != sbi->root) {
129 struct dentry *dentry = this_parent;
130
131 next = this_parent->d_u.d_child.next;
132 this_parent = this_parent->d_parent;
133 spin_unlock(&dcache_lock);
134 DPRINTK("parent dentry %p %.*s",
135 dentry, (int)dentry->d_name.len, dentry->d_name.name);
136 dput(dentry);
137 spin_lock(&dcache_lock);
138 goto resume;
139 }
140 spin_unlock(&dcache_lock);
141
142 dput(sbi->root);
143 sbi->root = NULL;
144 shrink_dcache_sb(sbi->sb);
145
146 return;
147 }
148
149 static void autofs4_put_super(struct super_block *sb)
150 {
151 struct autofs_sb_info *sbi = autofs4_sbi(sb);
152
153 sb->s_fs_info = NULL;
154
155 if ( !sbi->catatonic )
156 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
157
158 /* Clean up and release dangling references */
159 if (sbi)
160 autofs4_force_release(sbi);
161
162 kfree(sbi);
163
164 DPRINTK("shutting down");
165 }
166
167 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
168 {
169 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
170
171 if (!sbi)
172 return 0;
173
174 seq_printf(m, ",fd=%d", sbi->pipefd);
175 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
176 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
177 seq_printf(m, ",minproto=%d", sbi->min_proto);
178 seq_printf(m, ",maxproto=%d", sbi->max_proto);
179
180 return 0;
181 }
182
183 static struct super_operations autofs4_sops = {
184 .put_super = autofs4_put_super,
185 .statfs = simple_statfs,
186 .show_options = autofs4_show_options,
187 };
188
189 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
190
191 static match_table_t tokens = {
192 {Opt_fd, "fd=%u"},
193 {Opt_uid, "uid=%u"},
194 {Opt_gid, "gid=%u"},
195 {Opt_pgrp, "pgrp=%u"},
196 {Opt_minproto, "minproto=%u"},
197 {Opt_maxproto, "maxproto=%u"},
198 {Opt_err, NULL}
199 };
200
201 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
202 pid_t *pgrp, int *minproto, int *maxproto)
203 {
204 char *p;
205 substring_t args[MAX_OPT_ARGS];
206 int option;
207
208 *uid = current->uid;
209 *gid = current->gid;
210 *pgrp = process_group(current);
211
212 *minproto = AUTOFS_MIN_PROTO_VERSION;
213 *maxproto = AUTOFS_MAX_PROTO_VERSION;
214
215 *pipefd = -1;
216
217 if (!options)
218 return 1;
219
220 while ((p = strsep(&options, ",")) != NULL) {
221 int token;
222 if (!*p)
223 continue;
224
225 token = match_token(p, tokens, args);
226 switch (token) {
227 case Opt_fd:
228 if (match_int(args, pipefd))
229 return 1;
230 break;
231 case Opt_uid:
232 if (match_int(args, &option))
233 return 1;
234 *uid = option;
235 break;
236 case Opt_gid:
237 if (match_int(args, &option))
238 return 1;
239 *gid = option;
240 break;
241 case Opt_pgrp:
242 if (match_int(args, &option))
243 return 1;
244 *pgrp = option;
245 break;
246 case Opt_minproto:
247 if (match_int(args, &option))
248 return 1;
249 *minproto = option;
250 break;
251 case Opt_maxproto:
252 if (match_int(args, &option))
253 return 1;
254 *maxproto = option;
255 break;
256 default:
257 return 1;
258 }
259 }
260 return (*pipefd < 0);
261 }
262
263 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
264 {
265 struct autofs_info *ino;
266
267 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
268 if (!ino)
269 return NULL;
270
271 return ino;
272 }
273
274 int autofs4_fill_super(struct super_block *s, void *data, int silent)
275 {
276 struct inode * root_inode;
277 struct dentry * root;
278 struct file * pipe;
279 int pipefd;
280 struct autofs_sb_info *sbi;
281 struct autofs_info *ino;
282
283 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
284 if ( !sbi )
285 goto fail_unlock;
286 DPRINTK("starting up, sbi = %p",sbi);
287
288 memset(sbi, 0, sizeof(*sbi));
289
290 s->s_fs_info = sbi;
291 sbi->magic = AUTOFS_SBI_MAGIC;
292 sbi->root = NULL;
293 sbi->pipefd = -1;
294 sbi->catatonic = 0;
295 sbi->exp_timeout = 0;
296 sbi->oz_pgrp = process_group(current);
297 sbi->sb = s;
298 sbi->version = 0;
299 sbi->sub_version = 0;
300 sbi->min_proto = 0;
301 sbi->max_proto = 0;
302 mutex_init(&sbi->wq_mutex);
303 spin_lock_init(&sbi->fs_lock);
304 sbi->queues = NULL;
305 s->s_blocksize = 1024;
306 s->s_blocksize_bits = 10;
307 s->s_magic = AUTOFS_SUPER_MAGIC;
308 s->s_op = &autofs4_sops;
309 s->s_time_gran = 1;
310
311 /*
312 * Get the root inode and dentry, but defer checking for errors.
313 */
314 ino = autofs4_mkroot(sbi);
315 if (!ino)
316 goto fail_free;
317 root_inode = autofs4_get_inode(s, ino);
318 kfree(ino);
319 if (!root_inode)
320 goto fail_free;
321
322 root_inode->i_op = &autofs4_root_inode_operations;
323 root_inode->i_fop = &autofs4_root_operations;
324 root = d_alloc_root(root_inode);
325 pipe = NULL;
326
327 if (!root)
328 goto fail_iput;
329
330 /* Can this call block? */
331 if (parse_options(data, &pipefd,
332 &root_inode->i_uid, &root_inode->i_gid,
333 &sbi->oz_pgrp,
334 &sbi->min_proto, &sbi->max_proto)) {
335 printk("autofs: called with bogus options\n");
336 goto fail_dput;
337 }
338
339 /* Couldn't this be tested earlier? */
340 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
341 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
342 printk("autofs: kernel does not match daemon version "
343 "daemon (%d, %d) kernel (%d, %d)\n",
344 sbi->min_proto, sbi->max_proto,
345 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
346 goto fail_dput;
347 }
348
349 /* Establish highest kernel protocol version */
350 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
351 sbi->version = AUTOFS_MAX_PROTO_VERSION;
352 else
353 sbi->version = sbi->max_proto;
354 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
355
356 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
357 pipe = fget(pipefd);
358
359 if ( !pipe ) {
360 printk("autofs: could not open pipe file descriptor\n");
361 goto fail_dput;
362 }
363 if ( !pipe->f_op || !pipe->f_op->write )
364 goto fail_fput;
365 sbi->pipe = pipe;
366 sbi->pipefd = pipefd;
367
368 /*
369 * Take a reference to the root dentry so we get a chance to
370 * clean up the dentry tree on umount.
371 * See autofs4_force_release.
372 */
373 sbi->root = dget(root);
374
375 /*
376 * Success! Install the root dentry now to indicate completion.
377 */
378 s->s_root = root;
379 return 0;
380
381 /*
382 * Failure ... clean up.
383 */
384 fail_fput:
385 printk("autofs: pipe file descriptor does not contain proper ops\n");
386 fput(pipe);
387 /* fall through */
388 fail_dput:
389 dput(root);
390 goto fail_free;
391 fail_iput:
392 printk("autofs: get root dentry failed\n");
393 iput(root_inode);
394 fail_free:
395 kfree(sbi);
396 fail_unlock:
397 return -EINVAL;
398 }
399
400 struct inode *autofs4_get_inode(struct super_block *sb,
401 struct autofs_info *inf)
402 {
403 struct inode *inode = new_inode(sb);
404
405 if (inode == NULL)
406 return NULL;
407
408 inf->inode = inode;
409 inode->i_mode = inf->mode;
410 if (sb->s_root) {
411 inode->i_uid = sb->s_root->d_inode->i_uid;
412 inode->i_gid = sb->s_root->d_inode->i_gid;
413 } else {
414 inode->i_uid = 0;
415 inode->i_gid = 0;
416 }
417 inode->i_blksize = PAGE_CACHE_SIZE;
418 inode->i_blocks = 0;
419 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
420
421 if (S_ISDIR(inf->mode)) {
422 inode->i_nlink = 2;
423 inode->i_op = &autofs4_dir_inode_operations;
424 inode->i_fop = &autofs4_dir_operations;
425 } else if (S_ISLNK(inf->mode)) {
426 inode->i_size = inf->size;
427 inode->i_op = &autofs4_symlink_inode_operations;
428 }
429
430 return inode;
431 }
This page took 0.042445 seconds and 6 git commands to generate.