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
)
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 INIT_LIST_HEAD(&ino
->rehash
);
52 ino
->last_used
= jiffies
;
53 atomic_set(&ino
->count
, 0);
57 if (reinit
&& ino
->free
)
60 memset(&ino
->u
, 0, sizeof(ino
->u
));
65 ino
->free
= ino_lnkfree
;
70 void autofs4_free_ino(struct autofs_info
*ino
)
72 struct autofs_info
*p_ino
;
75 ino
->dentry
->d_fsdata
= NULL
;
76 if (ino
->dentry
->d_inode
) {
77 struct dentry
*parent
= ino
->dentry
->d_parent
;
78 if (atomic_dec_and_test(&ino
->count
)) {
79 p_ino
= autofs4_dentry_ino(parent
);
80 if (p_ino
&& parent
!= ino
->dentry
)
81 atomic_dec(&p_ino
->count
);
93 * Deal with the infamous "Busy inodes after umount ..." message.
95 * Clean up the dentry tree. This happens with autofs if the user
96 * space program goes away due to a SIGKILL, SIGSEGV etc.
98 static void autofs4_force_release(struct autofs_sb_info
*sbi
)
100 struct dentry
*this_parent
= sbi
->sb
->s_root
;
101 struct list_head
*next
;
103 if (!sbi
->sb
->s_root
)
106 spin_lock(&dcache_lock
);
108 next
= this_parent
->d_subdirs
.next
;
110 while (next
!= &this_parent
->d_subdirs
) {
111 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
113 /* Negative dentry - don`t care */
114 if (!simple_positive(dentry
)) {
119 if (!list_empty(&dentry
->d_subdirs
)) {
120 this_parent
= dentry
;
125 spin_unlock(&dcache_lock
);
127 DPRINTK("dentry %p %.*s",
128 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
131 spin_lock(&dcache_lock
);
134 if (this_parent
!= sbi
->sb
->s_root
) {
135 struct dentry
*dentry
= this_parent
;
137 next
= this_parent
->d_u
.d_child
.next
;
138 this_parent
= this_parent
->d_parent
;
139 spin_unlock(&dcache_lock
);
140 DPRINTK("parent dentry %p %.*s",
141 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
143 spin_lock(&dcache_lock
);
146 spin_unlock(&dcache_lock
);
149 void autofs4_kill_sb(struct super_block
*sb
)
151 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
154 * In the event of a failure in get_sb_nodev the superblock
155 * info is not present so nothing else has been setup, so
156 * just call kill_anon_super when we are called from
163 autofs4_catatonic_mode(sbi
); /* Free wait queues, close pipe */
165 /* Clean up and release dangling references */
166 autofs4_force_release(sbi
);
168 sb
->s_fs_info
= NULL
;
172 DPRINTK("shutting down");
176 static int autofs4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
178 struct autofs_sb_info
*sbi
= autofs4_sbi(mnt
->mnt_sb
);
183 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
184 seq_printf(m
, ",pgrp=%d", sbi
->oz_pgrp
);
185 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
186 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
187 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
189 if (sbi
->type
& AUTOFS_TYPE_OFFSET
)
190 seq_printf(m
, ",offset");
191 else if (sbi
->type
& AUTOFS_TYPE_DIRECT
)
192 seq_printf(m
, ",direct");
194 seq_printf(m
, ",indirect");
199 static const struct super_operations autofs4_sops
= {
200 .statfs
= simple_statfs
,
201 .show_options
= autofs4_show_options
,
204 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
205 Opt_indirect
, Opt_direct
, Opt_offset
};
207 static match_table_t tokens
= {
211 {Opt_pgrp
, "pgrp=%u"},
212 {Opt_minproto
, "minproto=%u"},
213 {Opt_maxproto
, "maxproto=%u"},
214 {Opt_indirect
, "indirect"},
215 {Opt_direct
, "direct"},
216 {Opt_offset
, "offset"},
220 static int parse_options(char *options
, int *pipefd
, uid_t
*uid
, gid_t
*gid
,
221 pid_t
*pgrp
, unsigned int *type
, int *minproto
, int *maxproto
)
224 substring_t args
[MAX_OPT_ARGS
];
229 *pgrp
= task_pgrp_nr(current
);
231 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
232 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
239 while ((p
= strsep(&options
, ",")) != NULL
) {
244 token
= match_token(p
, tokens
, args
);
247 if (match_int(args
, pipefd
))
251 if (match_int(args
, &option
))
256 if (match_int(args
, &option
))
261 if (match_int(args
, &option
))
266 if (match_int(args
, &option
))
271 if (match_int(args
, &option
))
276 *type
= AUTOFS_TYPE_INDIRECT
;
279 *type
= AUTOFS_TYPE_DIRECT
;
282 *type
= AUTOFS_TYPE_DIRECT
| AUTOFS_TYPE_OFFSET
;
288 return (*pipefd
< 0);
291 static struct autofs_info
*autofs4_mkroot(struct autofs_sb_info
*sbi
)
293 struct autofs_info
*ino
;
295 ino
= autofs4_init_ino(NULL
, sbi
, S_IFDIR
| 0755);
302 static struct dentry_operations autofs4_sb_dentry_operations
= {
303 .d_release
= autofs4_dentry_release
,
306 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
308 struct inode
* root_inode
;
309 struct dentry
* root
;
312 struct autofs_sb_info
*sbi
;
313 struct autofs_info
*ino
;
315 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
318 DPRINTK("starting up, sbi = %p",sbi
);
321 sbi
->magic
= AUTOFS_SBI_MAGIC
;
325 sbi
->exp_timeout
= 0;
326 sbi
->oz_pgrp
= task_pgrp_nr(current
);
329 sbi
->sub_version
= 0;
333 mutex_init(&sbi
->wq_mutex
);
334 spin_lock_init(&sbi
->fs_lock
);
336 spin_lock_init(&sbi
->rehash_lock
);
337 INIT_LIST_HEAD(&sbi
->rehash_list
);
338 s
->s_blocksize
= 1024;
339 s
->s_blocksize_bits
= 10;
340 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
341 s
->s_op
= &autofs4_sops
;
345 * Get the root inode and dentry, but defer checking for errors.
347 ino
= autofs4_mkroot(sbi
);
350 root_inode
= autofs4_get_inode(s
, ino
);
354 root
= d_alloc_root(root_inode
);
359 root
->d_op
= &autofs4_sb_dentry_operations
;
360 root
->d_fsdata
= ino
;
362 /* Can this call block? */
363 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
364 &sbi
->oz_pgrp
, &sbi
->type
, &sbi
->min_proto
,
366 printk("autofs: called with bogus options\n");
370 root_inode
->i_fop
= &autofs4_root_operations
;
371 root_inode
->i_op
= sbi
->type
& AUTOFS_TYPE_DIRECT
?
372 &autofs4_direct_root_inode_operations
:
373 &autofs4_indirect_root_inode_operations
;
375 /* Couldn't this be tested earlier? */
376 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
377 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
378 printk("autofs: kernel does not match daemon version "
379 "daemon (%d, %d) kernel (%d, %d)\n",
380 sbi
->min_proto
, sbi
->max_proto
,
381 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
385 /* Establish highest kernel protocol version */
386 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
387 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
389 sbi
->version
= sbi
->max_proto
;
390 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
392 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
396 printk("autofs: could not open pipe file descriptor\n");
399 if (!pipe
->f_op
|| !pipe
->f_op
->write
)
402 sbi
->pipefd
= pipefd
;
406 * Success! Install the root dentry now to indicate completion.
412 * Failure ... clean up.
415 printk("autofs: pipe file descriptor does not contain proper ops\n");
422 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
;
451 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
453 if (S_ISDIR(inf
->mode
)) {
455 inode
->i_op
= &autofs4_dir_inode_operations
;
456 inode
->i_fop
= &autofs4_dir_operations
;
457 } else if (S_ISLNK(inf
->mode
)) {
458 inode
->i_size
= inf
->size
;
459 inode
->i_op
= &autofs4_symlink_inode_operations
;