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/magic.h>
23 #include <linux/module.h>
25 static void ino_lnkfree(struct autofs_info
*ino
)
28 kfree(ino
->u
.symlink
);
29 ino
->u
.symlink
= NULL
;
33 struct autofs_info
*autofs4_init_ino(struct autofs_info
*ino
,
34 struct autofs_sb_info
*sbi
, mode_t mode
)
40 ino
= kmalloc(sizeof(*ino
), GFP_KERNEL
);
51 INIT_LIST_HEAD(&ino
->active
);
52 INIT_LIST_HEAD(&ino
->expiring
);
53 atomic_set(&ino
->count
, 0);
59 ino
->last_used
= jiffies
;
63 if (reinit
&& ino
->free
)
66 memset(&ino
->u
, 0, sizeof(ino
->u
));
71 ino
->free
= ino_lnkfree
;
76 void autofs4_free_ino(struct autofs_info
*ino
)
78 struct autofs_info
*p_ino
;
81 ino
->dentry
->d_fsdata
= NULL
;
82 if (ino
->dentry
->d_inode
) {
83 struct dentry
*parent
= ino
->dentry
->d_parent
;
84 if (atomic_dec_and_test(&ino
->count
)) {
85 p_ino
= autofs4_dentry_ino(parent
);
86 if (p_ino
&& parent
!= ino
->dentry
)
87 atomic_dec(&p_ino
->count
);
99 * Deal with the infamous "Busy inodes after umount ..." message.
101 * Clean up the dentry tree. This happens with autofs if the user
102 * space program goes away due to a SIGKILL, SIGSEGV etc.
104 static void autofs4_force_release(struct autofs_sb_info
*sbi
)
106 struct dentry
*this_parent
= sbi
->sb
->s_root
;
107 struct list_head
*next
;
109 if (!sbi
->sb
->s_root
)
112 spin_lock(&dcache_lock
);
114 next
= this_parent
->d_subdirs
.next
;
116 while (next
!= &this_parent
->d_subdirs
) {
117 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
119 /* Negative dentry - don`t care */
120 if (!simple_positive(dentry
)) {
125 if (!list_empty(&dentry
->d_subdirs
)) {
126 this_parent
= dentry
;
131 spin_unlock(&dcache_lock
);
133 DPRINTK("dentry %p %.*s",
134 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
137 spin_lock(&dcache_lock
);
140 if (this_parent
!= sbi
->sb
->s_root
) {
141 struct dentry
*dentry
= this_parent
;
143 next
= this_parent
->d_u
.d_child
.next
;
144 this_parent
= this_parent
->d_parent
;
145 spin_unlock(&dcache_lock
);
146 DPRINTK("parent dentry %p %.*s",
147 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
149 spin_lock(&dcache_lock
);
152 spin_unlock(&dcache_lock
);
155 void autofs4_kill_sb(struct super_block
*sb
)
157 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
160 * In the event of a failure in get_sb_nodev the superblock
161 * info is not present so nothing else has been setup, so
162 * just call kill_anon_super when we are called from
168 /* Free wait queues, close pipe */
169 autofs4_catatonic_mode(sbi
);
171 /* Clean up and release dangling references */
172 autofs4_force_release(sbi
);
174 sb
->s_fs_info
= NULL
;
178 DPRINTK("shutting down");
182 static int autofs4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
184 struct autofs_sb_info
*sbi
= autofs4_sbi(mnt
->mnt_sb
);
185 struct inode
*root_inode
= mnt
->mnt_sb
->s_root
->d_inode
;
190 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
191 if (root_inode
->i_uid
!= 0)
192 seq_printf(m
, ",uid=%u", root_inode
->i_uid
);
193 if (root_inode
->i_gid
!= 0)
194 seq_printf(m
, ",gid=%u", root_inode
->i_gid
);
195 seq_printf(m
, ",pgrp=%d", sbi
->oz_pgrp
);
196 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
197 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
198 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
200 if (autofs_type_offset(sbi
->type
))
201 seq_printf(m
, ",offset");
202 else if (autofs_type_direct(sbi
->type
))
203 seq_printf(m
, ",direct");
205 seq_printf(m
, ",indirect");
210 static const struct super_operations autofs4_sops
= {
211 .statfs
= simple_statfs
,
212 .show_options
= autofs4_show_options
,
215 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
216 Opt_indirect
, Opt_direct
, Opt_offset
};
218 static const match_table_t tokens
= {
222 {Opt_pgrp
, "pgrp=%u"},
223 {Opt_minproto
, "minproto=%u"},
224 {Opt_maxproto
, "maxproto=%u"},
225 {Opt_indirect
, "indirect"},
226 {Opt_direct
, "direct"},
227 {Opt_offset
, "offset"},
231 static int parse_options(char *options
, int *pipefd
, uid_t
*uid
, gid_t
*gid
,
232 pid_t
*pgrp
, unsigned int *type
, int *minproto
, int *maxproto
)
235 substring_t args
[MAX_OPT_ARGS
];
238 *uid
= current_uid();
239 *gid
= current_gid();
240 *pgrp
= task_pgrp_nr(current
);
242 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
243 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
250 while ((p
= strsep(&options
, ",")) != NULL
) {
255 token
= match_token(p
, tokens
, args
);
258 if (match_int(args
, pipefd
))
262 if (match_int(args
, &option
))
267 if (match_int(args
, &option
))
272 if (match_int(args
, &option
))
277 if (match_int(args
, &option
))
282 if (match_int(args
, &option
))
287 set_autofs_type_indirect(type
);
290 set_autofs_type_direct(type
);
293 set_autofs_type_offset(type
);
299 return (*pipefd
< 0);
302 static struct autofs_info
*autofs4_mkroot(struct autofs_sb_info
*sbi
)
304 struct autofs_info
*ino
;
306 ino
= autofs4_init_ino(NULL
, sbi
, S_IFDIR
| 0755);
313 static const struct dentry_operations autofs4_sb_dentry_operations
= {
314 .d_release
= autofs4_dentry_release
,
317 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
319 struct inode
* root_inode
;
320 struct dentry
* root
;
323 struct autofs_sb_info
*sbi
;
324 struct autofs_info
*ino
;
326 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
329 DPRINTK("starting up, sbi = %p",sbi
);
332 sbi
->magic
= AUTOFS_SBI_MAGIC
;
336 sbi
->exp_timeout
= 0;
337 sbi
->oz_pgrp
= task_pgrp_nr(current
);
340 sbi
->sub_version
= 0;
341 set_autofs_type_indirect(&sbi
->type
);
344 mutex_init(&sbi
->wq_mutex
);
345 spin_lock_init(&sbi
->fs_lock
);
347 spin_lock_init(&sbi
->lookup_lock
);
348 INIT_LIST_HEAD(&sbi
->active_list
);
349 INIT_LIST_HEAD(&sbi
->expiring_list
);
350 s
->s_blocksize
= 1024;
351 s
->s_blocksize_bits
= 10;
352 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
353 s
->s_op
= &autofs4_sops
;
357 * Get the root inode and dentry, but defer checking for errors.
359 ino
= autofs4_mkroot(sbi
);
362 root_inode
= autofs4_get_inode(s
, ino
);
366 root
= d_alloc_root(root_inode
);
371 root
->d_op
= &autofs4_sb_dentry_operations
;
372 root
->d_fsdata
= ino
;
374 /* Can this call block? */
375 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
376 &sbi
->oz_pgrp
, &sbi
->type
, &sbi
->min_proto
,
378 printk("autofs: called with bogus options\n");
382 root_inode
->i_fop
= &autofs4_root_operations
;
383 root_inode
->i_op
= autofs_type_trigger(sbi
->type
) ?
384 &autofs4_direct_root_inode_operations
:
385 &autofs4_indirect_root_inode_operations
;
387 /* Couldn't this be tested earlier? */
388 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
389 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
390 printk("autofs: kernel does not match daemon version "
391 "daemon (%d, %d) kernel (%d, %d)\n",
392 sbi
->min_proto
, sbi
->max_proto
,
393 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
397 /* Establish highest kernel protocol version */
398 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
399 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
401 sbi
->version
= sbi
->max_proto
;
402 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
404 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
408 printk("autofs: could not open pipe file descriptor\n");
411 if (!pipe
->f_op
|| !pipe
->f_op
->write
)
414 sbi
->pipefd
= pipefd
;
418 * Success! Install the root dentry now to indicate completion.
424 * Failure ... clean up.
427 printk("autofs: pipe file descriptor does not contain proper ops\n");
434 printk("autofs: get root dentry failed\n");
445 struct inode
*autofs4_get_inode(struct super_block
*sb
,
446 struct autofs_info
*inf
)
448 struct inode
*inode
= new_inode(sb
);
454 inode
->i_mode
= inf
->mode
;
456 inode
->i_uid
= sb
->s_root
->d_inode
->i_uid
;
457 inode
->i_gid
= sb
->s_root
->d_inode
->i_gid
;
459 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
461 if (S_ISDIR(inf
->mode
)) {
463 inode
->i_op
= &autofs4_dir_inode_operations
;
464 inode
->i_fop
= &autofs4_dir_operations
;
465 } else if (S_ISLNK(inf
->mode
)) {
466 inode
->i_size
= inf
->size
;
467 inode
->i_op
= &autofs4_symlink_inode_operations
;