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)
26 kfree(ino->u.symlink);
27 ino->u.symlink = NULL;
31 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
32 struct autofs_sb_info *sbi, mode_t mode)
38 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
50 ino->last_used = jiffies;
54 if (reinit && ino->free)
57 memset(&ino->u, 0, sizeof(ino->u));
62 ino->free = ino_lnkfree;
67 void autofs4_free_ino(struct autofs_info *ino)
70 ino->dentry->d_fsdata = NULL;
71 if (ino->dentry->d_inode)
81 * Deal with the infamous "Busy inodes after umount ..." message.
83 * Clean up the dentry tree. This happens with autofs if the user
84 * space program goes away due to a SIGKILL, SIGSEGV etc.
86 static void autofs4_force_release(struct autofs_sb_info *sbi)
88 struct dentry *this_parent = sbi->root;
89 struct list_head *next;
91 spin_lock(&dcache_lock);
93 next = this_parent->d_subdirs.next;
95 while (next != &this_parent->d_subdirs) {
96 struct dentry *dentry = list_entry(next, struct dentry, d_child);
98 /* Negative dentry - don`t care */
99 if (!simple_positive(dentry)) {
104 if (!list_empty(&dentry->d_subdirs)) {
105 this_parent = dentry;
110 spin_unlock(&dcache_lock);
112 DPRINTK("dentry %p %.*s",
113 dentry, (int)dentry->d_name.len, dentry->d_name.name);
116 spin_lock(&dcache_lock);
119 if (this_parent != sbi->root) {
120 struct dentry *dentry = this_parent;
122 next = this_parent->d_child.next;
123 this_parent = this_parent->d_parent;
124 spin_unlock(&dcache_lock);
125 DPRINTK("parent dentry %p %.*s",
126 dentry, (int)dentry->d_name.len, dentry->d_name.name);
128 spin_lock(&dcache_lock);
131 spin_unlock(&dcache_lock);
135 shrink_dcache_sb(sbi->sb);
140 static void autofs4_put_super(struct super_block *sb)
142 struct autofs_sb_info *sbi = autofs4_sbi(sb);
144 sb->s_fs_info = NULL;
146 if ( !sbi->catatonic )
147 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
149 /* Clean up and release dangling references */
151 autofs4_force_release(sbi);
155 DPRINTK("shutting down");
158 static struct super_operations autofs4_sops = {
159 .put_super = autofs4_put_super,
160 .statfs = simple_statfs,
163 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
165 static match_table_t tokens = {
169 {Opt_pgrp, "pgrp=%u"},
170 {Opt_minproto, "minproto=%u"},
171 {Opt_maxproto, "maxproto=%u"},
175 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
176 pid_t *pgrp, int *minproto, int *maxproto)
179 substring_t args[MAX_OPT_ARGS];
184 *pgrp = process_group(current);
186 *minproto = AUTOFS_MIN_PROTO_VERSION;
187 *maxproto = AUTOFS_MAX_PROTO_VERSION;
194 while ((p = strsep(&options, ",")) != NULL) {
199 token = match_token(p, tokens, args);
202 if (match_int(args, pipefd))
206 if (match_int(args, &option))
211 if (match_int(args, &option))
216 if (match_int(args, &option))
221 if (match_int(args, &option))
226 if (match_int(args, &option))
234 return (*pipefd < 0);
237 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
239 struct autofs_info *ino;
241 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
248 int autofs4_fill_super(struct super_block *s, void *data, int silent)
250 struct inode * root_inode;
251 struct dentry * root;
254 struct autofs_sb_info *sbi;
255 struct autofs_info *ino;
256 int minproto, maxproto;
258 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
261 DPRINTK("starting up, sbi = %p",sbi);
263 memset(sbi, 0, sizeof(*sbi));
266 sbi->magic = AUTOFS_SBI_MAGIC;
269 sbi->exp_timeout = 0;
270 sbi->oz_pgrp = process_group(current);
273 sbi->sub_version = 0;
274 init_MUTEX(&sbi->wq_sem);
275 spin_lock_init(&sbi->fs_lock);
277 s->s_blocksize = 1024;
278 s->s_blocksize_bits = 10;
279 s->s_magic = AUTOFS_SUPER_MAGIC;
280 s->s_op = &autofs4_sops;
284 * Get the root inode and dentry, but defer checking for errors.
286 ino = autofs4_mkroot(sbi);
289 root_inode = autofs4_get_inode(s, ino);
294 root_inode->i_op = &autofs4_root_inode_operations;
295 root_inode->i_fop = &autofs4_root_operations;
296 root = d_alloc_root(root_inode);
302 /* Can this call block? */
303 if (parse_options(data, &pipefd,
304 &root_inode->i_uid, &root_inode->i_gid,
306 &minproto, &maxproto)) {
307 printk("autofs: called with bogus options\n");
311 /* Couldn't this be tested earlier? */
312 if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
313 minproto > AUTOFS_MAX_PROTO_VERSION) {
314 printk("autofs: kernel does not match daemon version "
315 "daemon (%d, %d) kernel (%d, %d)\n",
317 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
321 sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
322 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
324 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
328 printk("autofs: could not open pipe file descriptor\n");
331 if ( !pipe->f_op || !pipe->f_op->write )
336 * Take a reference to the root dentry so we get a chance to
337 * clean up the dentry tree on umount.
338 * See autofs4_force_release.
340 sbi->root = dget(root);
343 * Success! Install the root dentry now to indicate completion.
349 * Failure ... clean up.
352 printk("autofs: pipe file descriptor does not contain proper ops\n");
359 printk("autofs: get root dentry failed\n");
367 struct inode *autofs4_get_inode(struct super_block *sb,
368 struct autofs_info *inf)
370 struct inode *inode = new_inode(sb);
376 inode->i_mode = inf->mode;
378 inode->i_uid = sb->s_root->d_inode->i_uid;
379 inode->i_gid = sb->s_root->d_inode->i_gid;
384 inode->i_blksize = PAGE_CACHE_SIZE;
386 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
388 if (S_ISDIR(inf->mode)) {
390 inode->i_op = &autofs4_dir_inode_operations;
391 inode->i_fop = &autofs4_dir_operations;
392 } else if (S_ISLNK(inf->mode)) {
393 inode->i_size = inf->size;
394 inode->i_op = &autofs4_symlink_inode_operations;