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/seq_file.h>
17 #include <linux/pagemap.h>
18 #include <linux/parser.h>
19 #include <linux/bitops.h>
20 #include <linux/smp_lock.h>
22 #include <linux/module.h>
24 static void ino_lnkfree(struct autofs_info
*ino
)
26 kfree(ino
->u
.symlink
);
27 ino
->u
.symlink
= NULL
;
30 struct autofs_info
*autofs4_init_ino(struct autofs_info
*ino
,
31 struct autofs_sb_info
*sbi
, mode_t mode
)
37 ino
= kmalloc(sizeof(*ino
), GFP_KERNEL
);
49 ino
->last_used
= jiffies
;
50 atomic_set(&ino
->count
, 0);
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
)
69 struct autofs_info
*p_ino
;
72 ino
->dentry
->d_fsdata
= NULL
;
73 if (ino
->dentry
->d_inode
) {
74 struct dentry
*parent
= ino
->dentry
->d_parent
;
75 if (atomic_dec_and_test(&ino
->count
)) {
76 p_ino
= autofs4_dentry_ino(parent
);
77 if (p_ino
&& parent
!= ino
->dentry
)
78 atomic_dec(&p_ino
->count
);
90 * Deal with the infamous "Busy inodes after umount ..." message.
92 * Clean up the dentry tree. This happens with autofs if the user
93 * space program goes away due to a SIGKILL, SIGSEGV etc.
95 static void autofs4_force_release(struct autofs_sb_info
*sbi
)
97 struct dentry
*this_parent
= sbi
->root
;
98 struct list_head
*next
;
100 spin_lock(&dcache_lock
);
102 next
= this_parent
->d_subdirs
.next
;
104 while (next
!= &this_parent
->d_subdirs
) {
105 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
107 /* Negative dentry - don`t care */
108 if (!simple_positive(dentry
)) {
113 if (!list_empty(&dentry
->d_subdirs
)) {
114 this_parent
= dentry
;
119 spin_unlock(&dcache_lock
);
121 DPRINTK("dentry %p %.*s",
122 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
125 spin_lock(&dcache_lock
);
128 if (this_parent
!= sbi
->root
) {
129 struct dentry
*dentry
= this_parent
;
131 next
= this_parent
->d_u
.d_child
.next
;
132 this_parent
= this_parent
->d_parent
;
133 spin_unlock(&dcache_lock
);
134 DPRINTK("parent dentry %p %.*s",
135 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
137 spin_lock(&dcache_lock
);
140 spin_unlock(&dcache_lock
);
144 shrink_dcache_sb(sbi
->sb
);
149 static void autofs4_put_super(struct super_block
*sb
)
151 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
153 sb
->s_fs_info
= NULL
;
155 if ( !sbi
->catatonic
)
156 autofs4_catatonic_mode(sbi
); /* Free wait queues, close pipe */
158 /* Clean up and release dangling references */
160 autofs4_force_release(sbi
);
164 DPRINTK("shutting down");
167 static int autofs4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
169 struct autofs_sb_info
*sbi
= autofs4_sbi(mnt
->mnt_sb
);
174 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
175 seq_printf(m
, ",pgrp=%d", sbi
->oz_pgrp
);
176 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
177 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
178 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
183 static struct super_operations autofs4_sops
= {
184 .put_super
= autofs4_put_super
,
185 .statfs
= simple_statfs
,
186 .show_options
= autofs4_show_options
,
189 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
};
191 static match_table_t tokens
= {
195 {Opt_pgrp
, "pgrp=%u"},
196 {Opt_minproto
, "minproto=%u"},
197 {Opt_maxproto
, "maxproto=%u"},
201 static int parse_options(char *options
, int *pipefd
, uid_t
*uid
, gid_t
*gid
,
202 pid_t
*pgrp
, int *minproto
, int *maxproto
)
205 substring_t args
[MAX_OPT_ARGS
];
210 *pgrp
= process_group(current
);
212 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
213 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
220 while ((p
= strsep(&options
, ",")) != NULL
) {
225 token
= match_token(p
, tokens
, args
);
228 if (match_int(args
, pipefd
))
232 if (match_int(args
, &option
))
237 if (match_int(args
, &option
))
242 if (match_int(args
, &option
))
247 if (match_int(args
, &option
))
252 if (match_int(args
, &option
))
260 return (*pipefd
< 0);
263 static struct autofs_info
*autofs4_mkroot(struct autofs_sb_info
*sbi
)
265 struct autofs_info
*ino
;
267 ino
= autofs4_init_ino(NULL
, sbi
, S_IFDIR
| 0755);
274 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
276 struct inode
* root_inode
;
277 struct dentry
* root
;
280 struct autofs_sb_info
*sbi
;
281 struct autofs_info
*ino
;
283 sbi
= (struct autofs_sb_info
*) kmalloc(sizeof(*sbi
), GFP_KERNEL
);
286 DPRINTK("starting up, sbi = %p",sbi
);
288 memset(sbi
, 0, sizeof(*sbi
));
291 sbi
->magic
= AUTOFS_SBI_MAGIC
;
295 sbi
->exp_timeout
= 0;
296 sbi
->oz_pgrp
= process_group(current
);
299 sbi
->sub_version
= 0;
302 mutex_init(&sbi
->wq_mutex
);
303 spin_lock_init(&sbi
->fs_lock
);
305 s
->s_blocksize
= 1024;
306 s
->s_blocksize_bits
= 10;
307 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
308 s
->s_op
= &autofs4_sops
;
312 * Get the root inode and dentry, but defer checking for errors.
314 ino
= autofs4_mkroot(sbi
);
317 root_inode
= autofs4_get_inode(s
, ino
);
322 root_inode
->i_op
= &autofs4_root_inode_operations
;
323 root_inode
->i_fop
= &autofs4_root_operations
;
324 root
= d_alloc_root(root_inode
);
330 /* Can this call block? */
331 if (parse_options(data
, &pipefd
,
332 &root_inode
->i_uid
, &root_inode
->i_gid
,
334 &sbi
->min_proto
, &sbi
->max_proto
)) {
335 printk("autofs: called with bogus options\n");
339 /* Couldn't this be tested earlier? */
340 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
341 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
342 printk("autofs: kernel does not match daemon version "
343 "daemon (%d, %d) kernel (%d, %d)\n",
344 sbi
->min_proto
, sbi
->max_proto
,
345 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
349 /* Establish highest kernel protocol version */
350 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
351 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
353 sbi
->version
= sbi
->max_proto
;
354 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
356 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
360 printk("autofs: could not open pipe file descriptor\n");
363 if ( !pipe
->f_op
|| !pipe
->f_op
->write
)
366 sbi
->pipefd
= pipefd
;
369 * Take a reference to the root dentry so we get a chance to
370 * clean up the dentry tree on umount.
371 * See autofs4_force_release.
373 sbi
->root
= dget(root
);
376 * Success! Install the root dentry now to indicate completion.
382 * Failure ... clean up.
385 printk("autofs: pipe file descriptor does not contain proper ops\n");
392 printk("autofs: get root dentry failed\n");
400 struct inode
*autofs4_get_inode(struct super_block
*sb
,
401 struct autofs_info
*inf
)
403 struct inode
*inode
= new_inode(sb
);
409 inode
->i_mode
= inf
->mode
;
411 inode
->i_uid
= sb
->s_root
->d_inode
->i_uid
;
412 inode
->i_gid
= sb
->s_root
->d_inode
->i_gid
;
417 inode
->i_blksize
= PAGE_CACHE_SIZE
;
419 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
421 if (S_ISDIR(inf
->mode
)) {
423 inode
->i_op
= &autofs4_dir_inode_operations
;
424 inode
->i_fop
= &autofs4_dir_operations
;
425 } else if (S_ISLNK(inf
->mode
)) {
426 inode
->i_size
= inf
->size
;
427 inode
->i_op
= &autofs4_symlink_inode_operations
;