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/smp_lock.h>
22 #include <linux/magic.h>
24 #include <linux/module.h>
26 static void ino_lnkfree(struct autofs_info *ino)
28 kfree(ino->u.symlink);
29 ino->u.symlink = NULL;
32 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
33 struct autofs_sb_info *sbi, mode_t mode)
39 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
51 INIT_LIST_HEAD(&ino->rehash);
53 ino->last_used = jiffies;
54 atomic_set(&ino->count, 0);
58 if (reinit && ino->free)
61 memset(&ino->u, 0, sizeof(ino->u));
66 ino->free = ino_lnkfree;
71 void autofs4_free_ino(struct autofs_info *ino)
73 struct autofs_info *p_ino;
76 ino->dentry->d_fsdata = NULL;
77 if (ino->dentry->d_inode) {
78 struct dentry *parent = ino->dentry->d_parent;
79 if (atomic_dec_and_test(&ino->count)) {
80 p_ino = autofs4_dentry_ino(parent);
81 if (p_ino && parent != ino->dentry)
82 atomic_dec(&p_ino->count);
94 * Deal with the infamous "Busy inodes after umount ..." message.
96 * Clean up the dentry tree. This happens with autofs if the user
97 * space program goes away due to a SIGKILL, SIGSEGV etc.
99 static void autofs4_force_release(struct autofs_sb_info *sbi)
101 struct dentry *this_parent = sbi->sb->s_root;
102 struct list_head *next;
104 if (!sbi->sb->s_root)
107 spin_lock(&dcache_lock);
109 next = this_parent->d_subdirs.next;
111 while (next != &this_parent->d_subdirs) {
112 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
114 /* Negative dentry - don`t care */
115 if (!simple_positive(dentry)) {
120 if (!list_empty(&dentry->d_subdirs)) {
121 this_parent = dentry;
126 spin_unlock(&dcache_lock);
128 DPRINTK("dentry %p %.*s",
129 dentry, (int)dentry->d_name.len, dentry->d_name.name);
132 spin_lock(&dcache_lock);
135 if (this_parent != sbi->sb->s_root) {
136 struct dentry *dentry = this_parent;
138 next = this_parent->d_u.d_child.next;
139 this_parent = this_parent->d_parent;
140 spin_unlock(&dcache_lock);
141 DPRINTK("parent dentry %p %.*s",
142 dentry, (int)dentry->d_name.len, dentry->d_name.name);
144 spin_lock(&dcache_lock);
147 spin_unlock(&dcache_lock);
150 void autofs4_kill_sb(struct super_block *sb)
152 struct autofs_sb_info *sbi = autofs4_sbi(sb);
155 * In the event of a failure in get_sb_nodev the superblock
156 * info is not present so nothing else has been setup, so
157 * just call kill_anon_super when we are called from
164 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
166 /* Clean up and release dangling references */
167 autofs4_force_release(sbi);
169 sb->s_fs_info = NULL;
173 DPRINTK("shutting down");
177 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
179 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
184 seq_printf(m, ",fd=%d", sbi->pipefd);
185 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
186 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
187 seq_printf(m, ",minproto=%d", sbi->min_proto);
188 seq_printf(m, ",maxproto=%d", sbi->max_proto);
190 if (sbi->type & AUTOFS_TYPE_OFFSET)
191 seq_printf(m, ",offset");
192 else if (sbi->type & AUTOFS_TYPE_DIRECT)
193 seq_printf(m, ",direct");
195 seq_printf(m, ",indirect");
200 static const struct super_operations autofs4_sops = {
201 .statfs = simple_statfs,
202 .show_options = autofs4_show_options,
205 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
206 Opt_indirect, Opt_direct, Opt_offset};
208 static match_table_t tokens = {
212 {Opt_pgrp, "pgrp=%u"},
213 {Opt_minproto, "minproto=%u"},
214 {Opt_maxproto, "maxproto=%u"},
215 {Opt_indirect, "indirect"},
216 {Opt_direct, "direct"},
217 {Opt_offset, "offset"},
221 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
222 pid_t *pgrp, unsigned int *type,
223 int *minproto, int *maxproto)
226 substring_t args[MAX_OPT_ARGS];
231 *pgrp = process_group(current);
233 *minproto = AUTOFS_MIN_PROTO_VERSION;
234 *maxproto = AUTOFS_MAX_PROTO_VERSION;
241 while ((p = strsep(&options, ",")) != NULL) {
246 token = match_token(p, tokens, args);
249 if (match_int(args, pipefd))
253 if (match_int(args, &option))
258 if (match_int(args, &option))
263 if (match_int(args, &option))
268 if (match_int(args, &option))
273 if (match_int(args, &option))
278 *type = AUTOFS_TYPE_INDIRECT;
281 *type = AUTOFS_TYPE_DIRECT;
284 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
290 return (*pipefd < 0);
293 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
295 struct autofs_info *ino;
297 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
304 static struct dentry_operations autofs4_sb_dentry_operations = {
305 .d_release = autofs4_dentry_release,
308 int autofs4_fill_super(struct super_block *s, void *data, int silent)
310 struct inode * root_inode;
311 struct dentry * root;
314 struct autofs_sb_info *sbi;
315 struct autofs_info *ino;
317 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
320 DPRINTK("starting up, sbi = %p",sbi);
322 memset(sbi, 0, sizeof(*sbi));
325 sbi->magic = AUTOFS_SBI_MAGIC;
329 sbi->exp_timeout = 0;
330 sbi->oz_pgrp = process_group(current);
333 sbi->sub_version = 0;
337 mutex_init(&sbi->wq_mutex);
338 spin_lock_init(&sbi->fs_lock);
340 spin_lock_init(&sbi->rehash_lock);
341 INIT_LIST_HEAD(&sbi->rehash_list);
342 s->s_blocksize = 1024;
343 s->s_blocksize_bits = 10;
344 s->s_magic = AUTOFS_SUPER_MAGIC;
345 s->s_op = &autofs4_sops;
349 * Get the root inode and dentry, but defer checking for errors.
351 ino = autofs4_mkroot(sbi);
354 root_inode = autofs4_get_inode(s, ino);
358 root = d_alloc_root(root_inode);
363 root->d_op = &autofs4_sb_dentry_operations;
364 root->d_fsdata = ino;
366 /* Can this call block? */
367 if (parse_options(data, &pipefd,
368 &root_inode->i_uid, &root_inode->i_gid,
369 &sbi->oz_pgrp, &sbi->type,
370 &sbi->min_proto, &sbi->max_proto)) {
371 printk("autofs: called with bogus options\n");
375 root_inode->i_fop = &autofs4_root_operations;
376 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
377 &autofs4_direct_root_inode_operations :
378 &autofs4_indirect_root_inode_operations;
380 /* Couldn't this be tested earlier? */
381 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
382 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
383 printk("autofs: kernel does not match daemon version "
384 "daemon (%d, %d) kernel (%d, %d)\n",
385 sbi->min_proto, sbi->max_proto,
386 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
390 /* Establish highest kernel protocol version */
391 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
392 sbi->version = AUTOFS_MAX_PROTO_VERSION;
394 sbi->version = sbi->max_proto;
395 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
397 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
401 printk("autofs: could not open pipe file descriptor\n");
404 if ( !pipe->f_op || !pipe->f_op->write )
407 sbi->pipefd = pipefd;
411 * Success! Install the root dentry now to indicate completion.
417 * Failure ... clean up.
420 printk("autofs: pipe file descriptor does not contain proper ops\n");
427 printk("autofs: get root dentry failed\n");
438 struct inode *autofs4_get_inode(struct super_block *sb,
439 struct autofs_info *inf)
441 struct inode *inode = new_inode(sb);
447 inode->i_mode = inf->mode;
449 inode->i_uid = sb->s_root->d_inode->i_uid;
450 inode->i_gid = sb->s_root->d_inode->i_gid;
456 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
458 if (S_ISDIR(inf->mode)) {
460 inode->i_op = &autofs4_dir_inode_operations;
461 inode->i_fop = &autofs4_dir_operations;
462 } else if (S_ISLNK(inf->mode)) {
463 inode->i_size = inf->size;
464 inode->i_op = &autofs4_symlink_inode_operations;