1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/inode.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
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.
11 * ------------------------------------------------------------------------- */
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/file.h>
16 #include <linux/pagemap.h>
17 #include <linux/parser.h>
18 #include <linux/bitops.h>
19 #include <linux/smp_lock.h>
21 #include <linux/module.h>
23 static void ino_lnkfree(struct autofs_info *ino)
25 kfree(ino->u.symlink);
26 ino->u.symlink = NULL;
29 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
30 struct autofs_sb_info *sbi, mode_t mode)
36 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
48 ino->last_used = jiffies;
49 atomic_set(&ino->count, 0);
53 if (reinit && ino->free)
56 memset(&ino->u, 0, sizeof(ino->u));
61 ino->free = ino_lnkfree;
66 void autofs4_free_ino(struct autofs_info *ino)
68 struct autofs_info *p_ino;
71 ino->dentry->d_fsdata = NULL;
72 if (ino->dentry->d_inode) {
73 struct dentry *parent = ino->dentry->d_parent;
74 if (atomic_dec_and_test(&ino->count)) {
75 p_ino = autofs4_dentry_ino(parent);
76 if (p_ino && parent != ino->dentry)
77 atomic_dec(&p_ino->count);
89 * Deal with the infamous "Busy inodes after umount ..." message.
91 * Clean up the dentry tree. This happens with autofs if the user
92 * space program goes away due to a SIGKILL, SIGSEGV etc.
94 static void autofs4_force_release(struct autofs_sb_info *sbi)
96 struct dentry *this_parent = sbi->root;
97 struct list_head *next;
99 spin_lock(&dcache_lock);
101 next = this_parent->d_subdirs.next;
103 while (next != &this_parent->d_subdirs) {
104 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
106 /* Negative dentry - don`t care */
107 if (!simple_positive(dentry)) {
112 if (!list_empty(&dentry->d_subdirs)) {
113 this_parent = dentry;
118 spin_unlock(&dcache_lock);
120 DPRINTK("dentry %p %.*s",
121 dentry, (int)dentry->d_name.len, dentry->d_name.name);
124 spin_lock(&dcache_lock);
127 if (this_parent != sbi->root) {
128 struct dentry *dentry = this_parent;
130 next = this_parent->d_u.d_child.next;
131 this_parent = this_parent->d_parent;
132 spin_unlock(&dcache_lock);
133 DPRINTK("parent dentry %p %.*s",
134 dentry, (int)dentry->d_name.len, dentry->d_name.name);
136 spin_lock(&dcache_lock);
139 spin_unlock(&dcache_lock);
143 shrink_dcache_sb(sbi->sb);
148 static void autofs4_put_super(struct super_block *sb)
150 struct autofs_sb_info *sbi = autofs4_sbi(sb);
152 sb->s_fs_info = NULL;
154 if ( !sbi->catatonic )
155 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
157 /* Clean up and release dangling references */
159 autofs4_force_release(sbi);
163 DPRINTK("shutting down");
166 static struct super_operations autofs4_sops = {
167 .put_super = autofs4_put_super,
168 .statfs = simple_statfs,
171 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
173 static match_table_t tokens = {
177 {Opt_pgrp, "pgrp=%u"},
178 {Opt_minproto, "minproto=%u"},
179 {Opt_maxproto, "maxproto=%u"},
183 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
184 pid_t *pgrp, int *minproto, int *maxproto)
187 substring_t args[MAX_OPT_ARGS];
192 *pgrp = process_group(current);
194 *minproto = AUTOFS_MIN_PROTO_VERSION;
195 *maxproto = AUTOFS_MAX_PROTO_VERSION;
202 while ((p = strsep(&options, ",")) != NULL) {
207 token = match_token(p, tokens, args);
210 if (match_int(args, pipefd))
214 if (match_int(args, &option))
219 if (match_int(args, &option))
224 if (match_int(args, &option))
229 if (match_int(args, &option))
234 if (match_int(args, &option))
242 return (*pipefd < 0);
245 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
247 struct autofs_info *ino;
249 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
256 int autofs4_fill_super(struct super_block *s, void *data, int silent)
258 struct inode * root_inode;
259 struct dentry * root;
262 struct autofs_sb_info *sbi;
263 struct autofs_info *ino;
264 int minproto, maxproto;
266 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
269 DPRINTK("starting up, sbi = %p",sbi);
271 memset(sbi, 0, sizeof(*sbi));
274 sbi->magic = AUTOFS_SBI_MAGIC;
277 sbi->exp_timeout = 0;
278 sbi->oz_pgrp = process_group(current);
281 sbi->sub_version = 0;
282 mutex_init(&sbi->wq_mutex);
283 spin_lock_init(&sbi->fs_lock);
285 s->s_blocksize = 1024;
286 s->s_blocksize_bits = 10;
287 s->s_magic = AUTOFS_SUPER_MAGIC;
288 s->s_op = &autofs4_sops;
292 * Get the root inode and dentry, but defer checking for errors.
294 ino = autofs4_mkroot(sbi);
297 root_inode = autofs4_get_inode(s, ino);
302 root_inode->i_op = &autofs4_root_inode_operations;
303 root_inode->i_fop = &autofs4_root_operations;
304 root = d_alloc_root(root_inode);
310 /* Can this call block? */
311 if (parse_options(data, &pipefd,
312 &root_inode->i_uid, &root_inode->i_gid,
314 &minproto, &maxproto)) {
315 printk("autofs: called with bogus options\n");
319 /* Couldn't this be tested earlier? */
320 if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
321 minproto > AUTOFS_MAX_PROTO_VERSION) {
322 printk("autofs: kernel does not match daemon version "
323 "daemon (%d, %d) kernel (%d, %d)\n",
325 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
329 sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
330 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
332 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
336 printk("autofs: could not open pipe file descriptor\n");
339 if ( !pipe->f_op || !pipe->f_op->write )
344 * Take a reference to the root dentry so we get a chance to
345 * clean up the dentry tree on umount.
346 * See autofs4_force_release.
348 sbi->root = dget(root);
351 * Success! Install the root dentry now to indicate completion.
357 * Failure ... clean up.
360 printk("autofs: pipe file descriptor does not contain proper ops\n");
367 printk("autofs: get root dentry failed\n");
375 struct inode *autofs4_get_inode(struct super_block *sb,
376 struct autofs_info *inf)
378 struct inode *inode = new_inode(sb);
384 inode->i_mode = inf->mode;
386 inode->i_uid = sb->s_root->d_inode->i_uid;
387 inode->i_gid = sb->s_root->d_inode->i_gid;
392 inode->i_blksize = PAGE_CACHE_SIZE;
394 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
396 if (S_ISDIR(inf->mode)) {
398 inode->i_op = &autofs4_dir_inode_operations;
399 inode->i_fop = &autofs4_dir_operations;
400 } else if (S_ISLNK(inf->mode)) {
401 inode->i_size = inf->size;
402 inode->i_op = &autofs4_symlink_inode_operations;