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 ino->last_used = jiffies;
52 atomic_set(&ino->count, 0);
56 if (reinit && ino->free)
59 memset(&ino->u, 0, sizeof(ino->u));
64 ino->free = ino_lnkfree;
69 void autofs4_free_ino(struct autofs_info *ino)
71 struct autofs_info *p_ino;
74 ino->dentry->d_fsdata = NULL;
75 if (ino->dentry->d_inode) {
76 struct dentry *parent = ino->dentry->d_parent;
77 if (atomic_dec_and_test(&ino->count)) {
78 p_ino = autofs4_dentry_ino(parent);
79 if (p_ino && parent != ino->dentry)
80 atomic_dec(&p_ino->count);
92 * Deal with the infamous "Busy inodes after umount ..." message.
94 * Clean up the dentry tree. This happens with autofs if the user
95 * space program goes away due to a SIGKILL, SIGSEGV etc.
97 static void autofs4_force_release(struct autofs_sb_info *sbi)
99 struct dentry *this_parent = sbi->sb->s_root;
100 struct list_head *next;
102 spin_lock(&dcache_lock);
104 next = this_parent->d_subdirs.next;
106 while (next != &this_parent->d_subdirs) {
107 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
109 /* Negative dentry - don`t care */
110 if (!simple_positive(dentry)) {
115 if (!list_empty(&dentry->d_subdirs)) {
116 this_parent = dentry;
121 spin_unlock(&dcache_lock);
123 DPRINTK("dentry %p %.*s",
124 dentry, (int)dentry->d_name.len, dentry->d_name.name);
127 spin_lock(&dcache_lock);
130 if (this_parent != sbi->sb->s_root) {
131 struct dentry *dentry = this_parent;
133 next = this_parent->d_u.d_child.next;
134 this_parent = this_parent->d_parent;
135 spin_unlock(&dcache_lock);
136 DPRINTK("parent dentry %p %.*s",
137 dentry, (int)dentry->d_name.len, dentry->d_name.name);
139 spin_lock(&dcache_lock);
142 spin_unlock(&dcache_lock);
145 void autofs4_kill_sb(struct super_block *sb)
147 struct autofs_sb_info *sbi = autofs4_sbi(sb);
149 sb->s_fs_info = NULL;
151 if ( !sbi->catatonic )
152 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
154 /* Clean up and release dangling references */
155 autofs4_force_release(sbi);
159 DPRINTK("shutting down");
163 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
165 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
170 seq_printf(m, ",fd=%d", sbi->pipefd);
171 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
172 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
173 seq_printf(m, ",minproto=%d", sbi->min_proto);
174 seq_printf(m, ",maxproto=%d", sbi->max_proto);
176 if (sbi->type & AUTOFS_TYPE_OFFSET)
177 seq_printf(m, ",offset");
178 else if (sbi->type & AUTOFS_TYPE_DIRECT)
179 seq_printf(m, ",direct");
181 seq_printf(m, ",indirect");
186 static struct super_operations autofs4_sops = {
187 .statfs = simple_statfs,
188 .show_options = autofs4_show_options,
191 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
192 Opt_indirect, Opt_direct, Opt_offset};
194 static match_table_t tokens = {
198 {Opt_pgrp, "pgrp=%u"},
199 {Opt_minproto, "minproto=%u"},
200 {Opt_maxproto, "maxproto=%u"},
201 {Opt_indirect, "indirect"},
202 {Opt_direct, "direct"},
203 {Opt_offset, "offset"},
207 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
208 pid_t *pgrp, unsigned int *type,
209 int *minproto, int *maxproto)
212 substring_t args[MAX_OPT_ARGS];
217 *pgrp = process_group(current);
219 *minproto = AUTOFS_MIN_PROTO_VERSION;
220 *maxproto = AUTOFS_MAX_PROTO_VERSION;
227 while ((p = strsep(&options, ",")) != NULL) {
232 token = match_token(p, tokens, args);
235 if (match_int(args, pipefd))
239 if (match_int(args, &option))
244 if (match_int(args, &option))
249 if (match_int(args, &option))
254 if (match_int(args, &option))
259 if (match_int(args, &option))
264 *type = AUTOFS_TYPE_INDIRECT;
267 *type = AUTOFS_TYPE_DIRECT;
270 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
276 return (*pipefd < 0);
279 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
281 struct autofs_info *ino;
283 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
290 static struct dentry_operations autofs4_sb_dentry_operations = {
291 .d_release = autofs4_dentry_release,
294 int autofs4_fill_super(struct super_block *s, void *data, int silent)
296 struct inode * root_inode;
297 struct dentry * root;
300 struct autofs_sb_info *sbi;
301 struct autofs_info *ino;
303 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
306 DPRINTK("starting up, sbi = %p",sbi);
308 memset(sbi, 0, sizeof(*sbi));
311 sbi->magic = AUTOFS_SBI_MAGIC;
314 sbi->exp_timeout = 0;
315 sbi->oz_pgrp = process_group(current);
318 sbi->sub_version = 0;
322 mutex_init(&sbi->wq_mutex);
323 spin_lock_init(&sbi->fs_lock);
325 s->s_blocksize = 1024;
326 s->s_blocksize_bits = 10;
327 s->s_magic = AUTOFS_SUPER_MAGIC;
328 s->s_op = &autofs4_sops;
332 * Get the root inode and dentry, but defer checking for errors.
334 ino = autofs4_mkroot(sbi);
337 root_inode = autofs4_get_inode(s, ino);
341 root = d_alloc_root(root_inode);
346 root->d_op = &autofs4_sb_dentry_operations;
347 root->d_fsdata = ino;
349 /* Can this call block? */
350 if (parse_options(data, &pipefd,
351 &root_inode->i_uid, &root_inode->i_gid,
352 &sbi->oz_pgrp, &sbi->type,
353 &sbi->min_proto, &sbi->max_proto)) {
354 printk("autofs: called with bogus options\n");
358 root_inode->i_fop = &autofs4_root_operations;
359 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
360 &autofs4_direct_root_inode_operations :
361 &autofs4_indirect_root_inode_operations;
363 /* Couldn't this be tested earlier? */
364 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
365 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
366 printk("autofs: kernel does not match daemon version "
367 "daemon (%d, %d) kernel (%d, %d)\n",
368 sbi->min_proto, sbi->max_proto,
369 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
373 /* Establish highest kernel protocol version */
374 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
375 sbi->version = AUTOFS_MAX_PROTO_VERSION;
377 sbi->version = sbi->max_proto;
378 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
380 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
384 printk("autofs: could not open pipe file descriptor\n");
387 if ( !pipe->f_op || !pipe->f_op->write )
390 sbi->pipefd = pipefd;
393 * Success! Install the root dentry now to indicate completion.
399 * Failure ... clean up.
402 printk("autofs: pipe file descriptor does not contain proper ops\n");
409 printk("autofs: get root dentry failed\n");
419 struct inode *autofs4_get_inode(struct super_block *sb,
420 struct autofs_info *inf)
422 struct inode *inode = new_inode(sb);
428 inode->i_mode = inf->mode;
430 inode->i_uid = sb->s_root->d_inode->i_uid;
431 inode->i_gid = sb->s_root->d_inode->i_gid;
437 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
439 if (S_ISDIR(inf->mode)) {
441 inode->i_op = &autofs4_dir_inode_operations;
442 inode->i_fop = &autofs4_dir_operations;
443 } else if (S_ISLNK(inf->mode)) {
444 inode->i_size = inf->size;
445 inode->i_op = &autofs4_symlink_inode_operations;