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 ino
->active_count
= 0;
53 INIT_LIST_HEAD(&ino
->expiring
);
54 atomic_set(&ino
->count
, 0);
60 ino
->last_used
= jiffies
;
64 if (reinit
&& ino
->free
)
67 memset(&ino
->u
, 0, sizeof(ino
->u
));
72 ino
->free
= ino_lnkfree
;
77 void autofs4_free_ino(struct autofs_info
*ino
)
79 struct autofs_info
*p_ino
;
82 ino
->dentry
->d_fsdata
= NULL
;
83 if (ino
->dentry
->d_inode
) {
84 struct dentry
*parent
= ino
->dentry
->d_parent
;
85 if (atomic_dec_and_test(&ino
->count
)) {
86 p_ino
= autofs4_dentry_ino(parent
);
87 if (p_ino
&& parent
!= ino
->dentry
)
88 atomic_dec(&p_ino
->count
);
99 void autofs4_kill_sb(struct super_block
*sb
)
101 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
104 * In the event of a failure in get_sb_nodev the superblock
105 * info is not present so nothing else has been setup, so
106 * just call kill_anon_super when we are called from
112 /* Free wait queues, close pipe */
113 autofs4_catatonic_mode(sbi
);
115 sb
->s_fs_info
= NULL
;
119 DPRINTK("shutting down");
120 kill_litter_super(sb
);
123 static int autofs4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
125 struct autofs_sb_info
*sbi
= autofs4_sbi(mnt
->mnt_sb
);
126 struct inode
*root_inode
= mnt
->mnt_sb
->s_root
->d_inode
;
131 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
132 if (root_inode
->i_uid
!= 0)
133 seq_printf(m
, ",uid=%u", root_inode
->i_uid
);
134 if (root_inode
->i_gid
!= 0)
135 seq_printf(m
, ",gid=%u", root_inode
->i_gid
);
136 seq_printf(m
, ",pgrp=%d", sbi
->oz_pgrp
);
137 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
138 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
139 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
141 if (autofs_type_offset(sbi
->type
))
142 seq_printf(m
, ",offset");
143 else if (autofs_type_direct(sbi
->type
))
144 seq_printf(m
, ",direct");
146 seq_printf(m
, ",indirect");
151 static const struct super_operations autofs4_sops
= {
152 .statfs
= simple_statfs
,
153 .show_options
= autofs4_show_options
,
156 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
157 Opt_indirect
, Opt_direct
, Opt_offset
};
159 static const match_table_t tokens
= {
163 {Opt_pgrp
, "pgrp=%u"},
164 {Opt_minproto
, "minproto=%u"},
165 {Opt_maxproto
, "maxproto=%u"},
166 {Opt_indirect
, "indirect"},
167 {Opt_direct
, "direct"},
168 {Opt_offset
, "offset"},
172 static int parse_options(char *options
, int *pipefd
, uid_t
*uid
, gid_t
*gid
,
173 pid_t
*pgrp
, unsigned int *type
, int *minproto
, int *maxproto
)
176 substring_t args
[MAX_OPT_ARGS
];
179 *uid
= current_uid();
180 *gid
= current_gid();
181 *pgrp
= task_pgrp_nr(current
);
183 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
184 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
191 while ((p
= strsep(&options
, ",")) != NULL
) {
196 token
= match_token(p
, tokens
, args
);
199 if (match_int(args
, pipefd
))
203 if (match_int(args
, &option
))
208 if (match_int(args
, &option
))
213 if (match_int(args
, &option
))
218 if (match_int(args
, &option
))
223 if (match_int(args
, &option
))
228 set_autofs_type_indirect(type
);
231 set_autofs_type_direct(type
);
234 set_autofs_type_offset(type
);
240 return (*pipefd
< 0);
243 static struct autofs_info
*autofs4_mkroot(struct autofs_sb_info
*sbi
)
245 struct autofs_info
*ino
;
247 ino
= autofs4_init_ino(NULL
, sbi
, S_IFDIR
| 0755);
254 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
256 struct inode
* root_inode
;
257 struct dentry
* root
;
260 struct autofs_sb_info
*sbi
;
261 struct autofs_info
*ino
;
263 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
266 DPRINTK("starting up, sbi = %p",sbi
);
269 sbi
->magic
= AUTOFS_SBI_MAGIC
;
273 sbi
->exp_timeout
= 0;
274 sbi
->oz_pgrp
= task_pgrp_nr(current
);
277 sbi
->sub_version
= 0;
278 set_autofs_type_indirect(&sbi
->type
);
281 mutex_init(&sbi
->wq_mutex
);
282 spin_lock_init(&sbi
->fs_lock
);
284 spin_lock_init(&sbi
->lookup_lock
);
285 INIT_LIST_HEAD(&sbi
->active_list
);
286 INIT_LIST_HEAD(&sbi
->expiring_list
);
287 s
->s_blocksize
= 1024;
288 s
->s_blocksize_bits
= 10;
289 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
290 s
->s_op
= &autofs4_sops
;
294 * Get the root inode and dentry, but defer checking for errors.
296 ino
= autofs4_mkroot(sbi
);
299 root_inode
= autofs4_get_inode(s
, ino
);
303 root
= d_alloc_root(root_inode
);
308 d_set_d_op(root
, &autofs4_dentry_operations
);
309 root
->d_fsdata
= ino
;
311 /* Can this call block? */
312 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
313 &sbi
->oz_pgrp
, &sbi
->type
, &sbi
->min_proto
,
315 printk("autofs: called with bogus options\n");
319 if (autofs_type_trigger(sbi
->type
)) {
320 d_set_d_op(root
, &autofs4_mount_dentry_operations
);
321 __managed_dentry_set_managed(root
);
324 root_inode
->i_fop
= &autofs4_root_operations
;
325 root_inode
->i_op
= &autofs4_dir_inode_operations
;
327 /* Couldn't this be tested earlier? */
328 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
329 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
330 printk("autofs: kernel does not match daemon version "
331 "daemon (%d, %d) kernel (%d, %d)\n",
332 sbi
->min_proto
, sbi
->max_proto
,
333 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
337 /* Establish highest kernel protocol version */
338 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
339 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
341 sbi
->version
= sbi
->max_proto
;
342 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
344 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
348 printk("autofs: could not open pipe file descriptor\n");
351 if (!pipe
->f_op
|| !pipe
->f_op
->write
)
354 sbi
->pipefd
= pipefd
;
358 * Success! Install the root dentry now to indicate completion.
364 * Failure ... clean up.
367 printk("autofs: pipe file descriptor does not contain proper ops\n");
374 printk("autofs: get root dentry failed\n");
385 struct inode
*autofs4_get_inode(struct super_block
*sb
,
386 struct autofs_info
*inf
)
388 struct inode
*inode
= new_inode(sb
);
394 inode
->i_mode
= inf
->mode
;
396 inode
->i_uid
= sb
->s_root
->d_inode
->i_uid
;
397 inode
->i_gid
= sb
->s_root
->d_inode
->i_gid
;
399 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
400 inode
->i_ino
= get_next_ino();
402 if (S_ISDIR(inf
->mode
)) {
404 inode
->i_op
= &autofs4_dir_inode_operations
;
405 inode
->i_fop
= &autofs4_dir_operations
;
406 } else if (S_ISLNK(inf
->mode
)) {
407 inode
->i_size
= inf
->size
;
408 inode
->i_op
= &autofs4_symlink_inode_operations
;