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>
23 #include <linux/module.h>
25 static void ino_lnkfree(struct autofs_info
*ino
)
27 kfree(ino
->u
.symlink
);
28 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
;
51 atomic_set(&ino
->count
, 0);
55 if (reinit
&& ino
->free
)
58 memset(&ino
->u
, 0, sizeof(ino
->u
));
63 ino
->free
= ino_lnkfree
;
68 void autofs4_free_ino(struct autofs_info
*ino
)
70 struct autofs_info
*p_ino
;
73 ino
->dentry
->d_fsdata
= NULL
;
74 if (ino
->dentry
->d_inode
) {
75 struct dentry
*parent
= ino
->dentry
->d_parent
;
76 if (atomic_dec_and_test(&ino
->count
)) {
77 p_ino
= autofs4_dentry_ino(parent
);
78 if (p_ino
&& parent
!= ino
->dentry
)
79 atomic_dec(&p_ino
->count
);
91 * Deal with the infamous "Busy inodes after umount ..." message.
93 * Clean up the dentry tree. This happens with autofs if the user
94 * space program goes away due to a SIGKILL, SIGSEGV etc.
96 static void autofs4_force_release(struct autofs_sb_info
*sbi
)
98 struct dentry
*this_parent
= sbi
->root
;
99 struct list_head
*next
;
101 spin_lock(&dcache_lock
);
103 next
= this_parent
->d_subdirs
.next
;
105 while (next
!= &this_parent
->d_subdirs
) {
106 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
108 /* Negative dentry - don`t care */
109 if (!simple_positive(dentry
)) {
114 if (!list_empty(&dentry
->d_subdirs
)) {
115 this_parent
= dentry
;
120 spin_unlock(&dcache_lock
);
122 DPRINTK("dentry %p %.*s",
123 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
126 spin_lock(&dcache_lock
);
129 if (this_parent
!= sbi
->root
) {
130 struct dentry
*dentry
= this_parent
;
132 next
= this_parent
->d_u
.d_child
.next
;
133 this_parent
= this_parent
->d_parent
;
134 spin_unlock(&dcache_lock
);
135 DPRINTK("parent dentry %p %.*s",
136 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
138 spin_lock(&dcache_lock
);
141 spin_unlock(&dcache_lock
);
145 shrink_dcache_sb(sbi
->sb
);
150 static void autofs4_put_super(struct super_block
*sb
)
152 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
154 sb
->s_fs_info
= NULL
;
156 if ( !sbi
->catatonic
)
157 autofs4_catatonic_mode(sbi
); /* Free wait queues, close pipe */
159 /* 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
);
180 if (sbi
->type
& AUTOFS_TYPE_OFFSET
)
181 seq_printf(m
, ",offset");
182 else if (sbi
->type
& AUTOFS_TYPE_DIRECT
)
183 seq_printf(m
, ",direct");
185 seq_printf(m
, ",indirect");
190 static struct super_operations autofs4_sops
= {
191 .put_super
= autofs4_put_super
,
192 .statfs
= simple_statfs
,
193 .show_options
= autofs4_show_options
,
196 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
197 Opt_indirect
, Opt_direct
, Opt_offset
};
199 static match_table_t tokens
= {
203 {Opt_pgrp
, "pgrp=%u"},
204 {Opt_minproto
, "minproto=%u"},
205 {Opt_maxproto
, "maxproto=%u"},
206 {Opt_indirect
, "indirect"},
207 {Opt_direct
, "direct"},
208 {Opt_offset
, "offset"},
212 static int parse_options(char *options
, int *pipefd
, uid_t
*uid
, gid_t
*gid
,
213 pid_t
*pgrp
, unsigned int *type
,
214 int *minproto
, int *maxproto
)
217 substring_t args
[MAX_OPT_ARGS
];
222 *pgrp
= process_group(current
);
224 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
225 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
232 while ((p
= strsep(&options
, ",")) != NULL
) {
237 token
= match_token(p
, tokens
, args
);
240 if (match_int(args
, pipefd
))
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 if (match_int(args
, &option
))
269 *type
= AUTOFS_TYPE_INDIRECT
;
272 *type
= AUTOFS_TYPE_DIRECT
;
275 *type
= AUTOFS_TYPE_DIRECT
| AUTOFS_TYPE_OFFSET
;
281 return (*pipefd
< 0);
284 static struct autofs_info
*autofs4_mkroot(struct autofs_sb_info
*sbi
)
286 struct autofs_info
*ino
;
288 ino
= autofs4_init_ino(NULL
, sbi
, S_IFDIR
| 0755);
295 void autofs4_dentry_release(struct dentry
*);
296 static struct dentry_operations autofs4_sb_dentry_operations
= {
297 .d_release
= autofs4_dentry_release
,
300 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
302 struct inode
* root_inode
;
303 struct dentry
* root
;
306 struct autofs_sb_info
*sbi
;
307 struct autofs_info
*ino
;
309 sbi
= (struct autofs_sb_info
*) kmalloc(sizeof(*sbi
), GFP_KERNEL
);
312 DPRINTK("starting up, sbi = %p",sbi
);
314 memset(sbi
, 0, sizeof(*sbi
));
317 sbi
->magic
= AUTOFS_SBI_MAGIC
;
321 sbi
->exp_timeout
= 0;
322 sbi
->oz_pgrp
= process_group(current
);
325 sbi
->sub_version
= 0;
329 mutex_init(&sbi
->wq_mutex
);
330 spin_lock_init(&sbi
->fs_lock
);
332 s
->s_blocksize
= 1024;
333 s
->s_blocksize_bits
= 10;
334 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
335 s
->s_op
= &autofs4_sops
;
339 * Get the root inode and dentry, but defer checking for errors.
341 ino
= autofs4_mkroot(sbi
);
344 root_inode
= autofs4_get_inode(s
, ino
);
348 root
= d_alloc_root(root_inode
);
353 root
->d_op
= &autofs4_sb_dentry_operations
;
354 root
->d_fsdata
= ino
;
356 /* Can this call block? */
357 if (parse_options(data
, &pipefd
,
358 &root_inode
->i_uid
, &root_inode
->i_gid
,
359 &sbi
->oz_pgrp
, &sbi
->type
,
360 &sbi
->min_proto
, &sbi
->max_proto
)) {
361 printk("autofs: called with bogus options\n");
365 root_inode
->i_fop
= &autofs4_root_operations
;
366 root_inode
->i_op
= sbi
->type
& AUTOFS_TYPE_DIRECT
?
367 &autofs4_direct_root_inode_operations
:
368 &autofs4_indirect_root_inode_operations
;
370 /* Couldn't this be tested earlier? */
371 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
372 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
373 printk("autofs: kernel does not match daemon version "
374 "daemon (%d, %d) kernel (%d, %d)\n",
375 sbi
->min_proto
, sbi
->max_proto
,
376 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
380 /* Establish highest kernel protocol version */
381 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
382 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
384 sbi
->version
= sbi
->max_proto
;
385 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
387 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
391 printk("autofs: could not open pipe file descriptor\n");
394 if ( !pipe
->f_op
|| !pipe
->f_op
->write
)
397 sbi
->pipefd
= pipefd
;
400 * Take a reference to the root dentry so we get a chance to
401 * clean up the dentry tree on umount.
402 * See autofs4_force_release.
404 sbi
->root
= dget(root
);
407 * Success! Install the root dentry now to indicate completion.
413 * Failure ... clean up.
416 printk("autofs: pipe file descriptor does not contain proper ops\n");
423 printk("autofs: get root dentry failed\n");
433 struct inode
*autofs4_get_inode(struct super_block
*sb
,
434 struct autofs_info
*inf
)
436 struct inode
*inode
= new_inode(sb
);
442 inode
->i_mode
= inf
->mode
;
444 inode
->i_uid
= sb
->s_root
->d_inode
->i_uid
;
445 inode
->i_gid
= sb
->s_root
->d_inode
->i_gid
;
450 inode
->i_blksize
= PAGE_CACHE_SIZE
;
452 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
454 if (S_ISDIR(inf
->mode
)) {
456 inode
->i_op
= &autofs4_dir_inode_operations
;
457 inode
->i_fop
= &autofs4_dir_operations
;
458 } else if (S_ISLNK(inf
->mode
)) {
459 inode
->i_size
= inf
->size
;
460 inode
->i_op
= &autofs4_symlink_inode_operations
;