Merge branch 'core-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-2.6-openrd.git] / fs / autofs4 / inode.c
blobd0a3de247458aae03f045bf6788b64e5045ad950
1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/inode.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * ------------------------------------------------------------------------- */
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/file.h>
17 #include <linux/seq_file.h>
18 #include <linux/pagemap.h>
19 #include <linux/parser.h>
20 #include <linux/bitops.h>
21 #include <linux/magic.h>
22 #include "autofs_i.h"
23 #include <linux/module.h>
25 static void ino_lnkfree(struct autofs_info *ino)
27 if (ino->u.symlink) {
28 kfree(ino->u.symlink);
29 ino->u.symlink = NULL;
33 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34 struct autofs_sb_info *sbi, mode_t mode)
36 int reinit = 1;
38 if (ino == NULL) {
39 reinit = 0;
40 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
43 if (ino == NULL)
44 return NULL;
46 if (!reinit) {
47 ino->flags = 0;
48 ino->inode = NULL;
49 ino->dentry = NULL;
50 ino->size = 0;
51 INIT_LIST_HEAD(&ino->active);
52 INIT_LIST_HEAD(&ino->rehash_list);
53 ino->active_count = 0;
54 INIT_LIST_HEAD(&ino->expiring);
55 atomic_set(&ino->count, 0);
58 ino->uid = 0;
59 ino->gid = 0;
60 ino->mode = mode;
61 ino->last_used = jiffies;
63 ino->sbi = sbi;
65 if (reinit && ino->free)
66 (ino->free)(ino);
68 memset(&ino->u, 0, sizeof(ino->u));
70 ino->free = NULL;
72 if (S_ISLNK(mode))
73 ino->free = ino_lnkfree;
75 return ino;
78 void autofs4_free_ino(struct autofs_info *ino)
80 struct autofs_info *p_ino;
82 if (ino->dentry) {
83 ino->dentry->d_fsdata = NULL;
84 if (ino->dentry->d_inode) {
85 struct dentry *parent = ino->dentry->d_parent;
86 if (atomic_dec_and_test(&ino->count)) {
87 p_ino = autofs4_dentry_ino(parent);
88 if (p_ino && parent != ino->dentry)
89 atomic_dec(&p_ino->count);
91 dput(ino->dentry);
93 ino->dentry = NULL;
95 if (ino->free)
96 (ino->free)(ino);
97 kfree(ino);
101 * Deal with the infamous "Busy inodes after umount ..." message.
103 * Clean up the dentry tree. This happens with autofs if the user
104 * space program goes away due to a SIGKILL, SIGSEGV etc.
106 static void autofs4_force_release(struct autofs_sb_info *sbi)
108 struct dentry *this_parent = sbi->sb->s_root;
109 struct list_head *next;
111 if (!sbi->sb->s_root)
112 return;
114 spin_lock(&dcache_lock);
115 repeat:
116 next = this_parent->d_subdirs.next;
117 resume:
118 while (next != &this_parent->d_subdirs) {
119 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
121 /* Negative dentry - don`t care */
122 if (!simple_positive(dentry)) {
123 next = next->next;
124 continue;
127 if (!list_empty(&dentry->d_subdirs)) {
128 this_parent = dentry;
129 goto repeat;
132 next = next->next;
133 spin_unlock(&dcache_lock);
135 DPRINTK("dentry %p %.*s",
136 dentry, (int)dentry->d_name.len, dentry->d_name.name);
138 dput(dentry);
139 spin_lock(&dcache_lock);
142 if (this_parent != sbi->sb->s_root) {
143 struct dentry *dentry = this_parent;
145 next = this_parent->d_u.d_child.next;
146 this_parent = this_parent->d_parent;
147 spin_unlock(&dcache_lock);
148 DPRINTK("parent dentry %p %.*s",
149 dentry, (int)dentry->d_name.len, dentry->d_name.name);
150 dput(dentry);
151 spin_lock(&dcache_lock);
152 goto resume;
154 spin_unlock(&dcache_lock);
157 void autofs4_kill_sb(struct super_block *sb)
159 struct autofs_sb_info *sbi = autofs4_sbi(sb);
162 * In the event of a failure in get_sb_nodev the superblock
163 * info is not present so nothing else has been setup, so
164 * just call kill_anon_super when we are called from
165 * deactivate_super.
167 if (!sbi)
168 goto out_kill_sb;
170 /* Free wait queues, close pipe */
171 autofs4_catatonic_mode(sbi);
173 /* Clean up and release dangling references */
174 autofs4_force_release(sbi);
176 sb->s_fs_info = NULL;
177 kfree(sbi);
179 out_kill_sb:
180 DPRINTK("shutting down");
181 kill_anon_super(sb);
184 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
186 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
187 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
189 if (!sbi)
190 return 0;
192 seq_printf(m, ",fd=%d", sbi->pipefd);
193 if (root_inode->i_uid != 0)
194 seq_printf(m, ",uid=%u", root_inode->i_uid);
195 if (root_inode->i_gid != 0)
196 seq_printf(m, ",gid=%u", root_inode->i_gid);
197 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
198 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
199 seq_printf(m, ",minproto=%d", sbi->min_proto);
200 seq_printf(m, ",maxproto=%d", sbi->max_proto);
202 if (autofs_type_offset(sbi->type))
203 seq_printf(m, ",offset");
204 else if (autofs_type_direct(sbi->type))
205 seq_printf(m, ",direct");
206 else
207 seq_printf(m, ",indirect");
209 return 0;
212 static const struct super_operations autofs4_sops = {
213 .statfs = simple_statfs,
214 .show_options = autofs4_show_options,
217 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
218 Opt_indirect, Opt_direct, Opt_offset};
220 static const match_table_t tokens = {
221 {Opt_fd, "fd=%u"},
222 {Opt_uid, "uid=%u"},
223 {Opt_gid, "gid=%u"},
224 {Opt_pgrp, "pgrp=%u"},
225 {Opt_minproto, "minproto=%u"},
226 {Opt_maxproto, "maxproto=%u"},
227 {Opt_indirect, "indirect"},
228 {Opt_direct, "direct"},
229 {Opt_offset, "offset"},
230 {Opt_err, NULL}
233 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
234 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
236 char *p;
237 substring_t args[MAX_OPT_ARGS];
238 int option;
240 *uid = current_uid();
241 *gid = current_gid();
242 *pgrp = task_pgrp_nr(current);
244 *minproto = AUTOFS_MIN_PROTO_VERSION;
245 *maxproto = AUTOFS_MAX_PROTO_VERSION;
247 *pipefd = -1;
249 if (!options)
250 return 1;
252 while ((p = strsep(&options, ",")) != NULL) {
253 int token;
254 if (!*p)
255 continue;
257 token = match_token(p, tokens, args);
258 switch (token) {
259 case Opt_fd:
260 if (match_int(args, pipefd))
261 return 1;
262 break;
263 case Opt_uid:
264 if (match_int(args, &option))
265 return 1;
266 *uid = option;
267 break;
268 case Opt_gid:
269 if (match_int(args, &option))
270 return 1;
271 *gid = option;
272 break;
273 case Opt_pgrp:
274 if (match_int(args, &option))
275 return 1;
276 *pgrp = option;
277 break;
278 case Opt_minproto:
279 if (match_int(args, &option))
280 return 1;
281 *minproto = option;
282 break;
283 case Opt_maxproto:
284 if (match_int(args, &option))
285 return 1;
286 *maxproto = option;
287 break;
288 case Opt_indirect:
289 set_autofs_type_indirect(type);
290 break;
291 case Opt_direct:
292 set_autofs_type_direct(type);
293 break;
294 case Opt_offset:
295 set_autofs_type_offset(type);
296 break;
297 default:
298 return 1;
301 return (*pipefd < 0);
304 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
306 struct autofs_info *ino;
308 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
309 if (!ino)
310 return NULL;
312 return ino;
315 static const struct dentry_operations autofs4_sb_dentry_operations = {
316 .d_release = autofs4_dentry_release,
319 int autofs4_fill_super(struct super_block *s, void *data, int silent)
321 struct inode * root_inode;
322 struct dentry * root;
323 struct file * pipe;
324 int pipefd;
325 struct autofs_sb_info *sbi;
326 struct autofs_info *ino;
328 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
329 if (!sbi)
330 goto fail_unlock;
331 DPRINTK("starting up, sbi = %p",sbi);
333 s->s_fs_info = sbi;
334 sbi->magic = AUTOFS_SBI_MAGIC;
335 sbi->pipefd = -1;
336 sbi->pipe = NULL;
337 sbi->catatonic = 1;
338 sbi->exp_timeout = 0;
339 sbi->oz_pgrp = task_pgrp_nr(current);
340 sbi->sb = s;
341 sbi->version = 0;
342 sbi->sub_version = 0;
343 set_autofs_type_indirect(&sbi->type);
344 sbi->min_proto = 0;
345 sbi->max_proto = 0;
346 mutex_init(&sbi->wq_mutex);
347 spin_lock_init(&sbi->fs_lock);
348 sbi->queues = NULL;
349 spin_lock_init(&sbi->lookup_lock);
350 INIT_LIST_HEAD(&sbi->active_list);
351 INIT_LIST_HEAD(&sbi->expiring_list);
352 s->s_blocksize = 1024;
353 s->s_blocksize_bits = 10;
354 s->s_magic = AUTOFS_SUPER_MAGIC;
355 s->s_op = &autofs4_sops;
356 s->s_time_gran = 1;
359 * Get the root inode and dentry, but defer checking for errors.
361 ino = autofs4_mkroot(sbi);
362 if (!ino)
363 goto fail_free;
364 root_inode = autofs4_get_inode(s, ino);
365 if (!root_inode)
366 goto fail_ino;
368 root = d_alloc_root(root_inode);
369 if (!root)
370 goto fail_iput;
371 pipe = NULL;
373 root->d_op = &autofs4_sb_dentry_operations;
374 root->d_fsdata = ino;
376 /* Can this call block? */
377 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
378 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
379 &sbi->max_proto)) {
380 printk("autofs: called with bogus options\n");
381 goto fail_dput;
384 root_inode->i_fop = &autofs4_root_operations;
385 root_inode->i_op = autofs_type_trigger(sbi->type) ?
386 &autofs4_direct_root_inode_operations :
387 &autofs4_indirect_root_inode_operations;
389 /* Couldn't this be tested earlier? */
390 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
391 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
392 printk("autofs: kernel does not match daemon version "
393 "daemon (%d, %d) kernel (%d, %d)\n",
394 sbi->min_proto, sbi->max_proto,
395 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
396 goto fail_dput;
399 /* Establish highest kernel protocol version */
400 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
401 sbi->version = AUTOFS_MAX_PROTO_VERSION;
402 else
403 sbi->version = sbi->max_proto;
404 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
406 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
407 pipe = fget(pipefd);
409 if (!pipe) {
410 printk("autofs: could not open pipe file descriptor\n");
411 goto fail_dput;
413 if (!pipe->f_op || !pipe->f_op->write)
414 goto fail_fput;
415 sbi->pipe = pipe;
416 sbi->pipefd = pipefd;
417 sbi->catatonic = 0;
420 * Success! Install the root dentry now to indicate completion.
422 s->s_root = root;
423 return 0;
426 * Failure ... clean up.
428 fail_fput:
429 printk("autofs: pipe file descriptor does not contain proper ops\n");
430 fput(pipe);
431 /* fall through */
432 fail_dput:
433 dput(root);
434 goto fail_free;
435 fail_iput:
436 printk("autofs: get root dentry failed\n");
437 iput(root_inode);
438 fail_ino:
439 kfree(ino);
440 fail_free:
441 kfree(sbi);
442 s->s_fs_info = NULL;
443 fail_unlock:
444 return -EINVAL;
447 struct inode *autofs4_get_inode(struct super_block *sb,
448 struct autofs_info *inf)
450 struct inode *inode = new_inode(sb);
452 if (inode == NULL)
453 return NULL;
455 inf->inode = inode;
456 inode->i_mode = inf->mode;
457 if (sb->s_root) {
458 inode->i_uid = sb->s_root->d_inode->i_uid;
459 inode->i_gid = sb->s_root->d_inode->i_gid;
461 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
463 if (S_ISDIR(inf->mode)) {
464 inode->i_nlink = 2;
465 inode->i_op = &autofs4_dir_inode_operations;
466 inode->i_fop = &autofs4_dir_operations;
467 } else if (S_ISLNK(inf->mode)) {
468 inode->i_size = inf->size;
469 inode->i_op = &autofs4_symlink_inode_operations;
472 return inode;