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 struct autofs_info
*autofs4_new_ino(struct autofs_sb_info
*sbi
)
27 struct autofs_info
*ino
= kzalloc(sizeof(*ino
), GFP_KERNEL
);
29 INIT_LIST_HEAD(&ino
->active
);
30 INIT_LIST_HEAD(&ino
->expiring
);
31 ino
->last_used
= jiffies
;
37 void autofs4_clean_ino(struct autofs_info
*ino
)
39 ino
->uid
= GLOBAL_ROOT_UID
;
40 ino
->gid
= GLOBAL_ROOT_GID
;
41 ino
->last_used
= jiffies
;
44 void autofs4_free_ino(struct autofs_info
*ino
)
49 void autofs4_kill_sb(struct super_block
*sb
)
51 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
54 * In the event of a failure in get_sb_nodev the superblock
55 * info is not present so nothing else has been setup, so
56 * just call kill_anon_super when we are called from
62 /* Free wait queues, close pipe */
63 autofs4_catatonic_mode(sbi
);
69 DPRINTK("shutting down");
70 kill_litter_super(sb
);
73 static int autofs4_show_options(struct seq_file
*m
, struct dentry
*root
)
75 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
76 struct inode
*root_inode
= root
->d_sb
->s_root
->d_inode
;
81 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
82 if (!uid_eq(root_inode
->i_uid
, GLOBAL_ROOT_UID
))
83 seq_printf(m
, ",uid=%u",
84 from_kuid_munged(&init_user_ns
, root_inode
->i_uid
));
85 if (!gid_eq(root_inode
->i_gid
, GLOBAL_ROOT_GID
))
86 seq_printf(m
, ",gid=%u",
87 from_kgid_munged(&init_user_ns
, root_inode
->i_gid
));
88 seq_printf(m
, ",pgrp=%d", sbi
->oz_pgrp
);
89 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
90 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
91 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
93 if (autofs_type_offset(sbi
->type
))
94 seq_printf(m
, ",offset");
95 else if (autofs_type_direct(sbi
->type
))
96 seq_printf(m
, ",direct");
98 seq_printf(m
, ",indirect");
103 static void autofs4_evict_inode(struct inode
*inode
)
106 kfree(inode
->i_private
);
109 static const struct super_operations autofs4_sops
= {
110 .statfs
= simple_statfs
,
111 .show_options
= autofs4_show_options
,
112 .evict_inode
= autofs4_evict_inode
,
115 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
116 Opt_indirect
, Opt_direct
, Opt_offset
};
118 static const match_table_t tokens
= {
122 {Opt_pgrp
, "pgrp=%u"},
123 {Opt_minproto
, "minproto=%u"},
124 {Opt_maxproto
, "maxproto=%u"},
125 {Opt_indirect
, "indirect"},
126 {Opt_direct
, "direct"},
127 {Opt_offset
, "offset"},
131 static int parse_options(char *options
, int *pipefd
, kuid_t
*uid
, kgid_t
*gid
,
132 pid_t
*pgrp
, unsigned int *type
, int *minproto
, int *maxproto
)
135 substring_t args
[MAX_OPT_ARGS
];
138 *uid
= current_uid();
139 *gid
= current_gid();
140 *pgrp
= task_pgrp_nr(current
);
142 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
143 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
150 while ((p
= strsep(&options
, ",")) != NULL
) {
155 token
= match_token(p
, tokens
, args
);
158 if (match_int(args
, pipefd
))
162 if (match_int(args
, &option
))
164 *uid
= make_kuid(current_user_ns(), option
);
165 if (!uid_valid(*uid
))
169 if (match_int(args
, &option
))
171 *gid
= make_kgid(current_user_ns(), option
);
172 if (!gid_valid(*gid
))
176 if (match_int(args
, &option
))
181 if (match_int(args
, &option
))
186 if (match_int(args
, &option
))
191 set_autofs_type_indirect(type
);
194 set_autofs_type_direct(type
);
197 set_autofs_type_offset(type
);
203 return (*pipefd
< 0);
206 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
208 struct inode
* root_inode
;
209 struct dentry
* root
;
212 struct autofs_sb_info
*sbi
;
213 struct autofs_info
*ino
;
215 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
218 DPRINTK("starting up, sbi = %p",sbi
);
221 sbi
->magic
= AUTOFS_SBI_MAGIC
;
225 sbi
->exp_timeout
= 0;
226 sbi
->oz_pgrp
= task_pgrp_nr(current
);
229 sbi
->sub_version
= 0;
230 set_autofs_type_indirect(&sbi
->type
);
233 mutex_init(&sbi
->wq_mutex
);
234 mutex_init(&sbi
->pipe_mutex
);
235 spin_lock_init(&sbi
->fs_lock
);
237 spin_lock_init(&sbi
->lookup_lock
);
238 INIT_LIST_HEAD(&sbi
->active_list
);
239 INIT_LIST_HEAD(&sbi
->expiring_list
);
240 s
->s_blocksize
= 1024;
241 s
->s_blocksize_bits
= 10;
242 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
243 s
->s_op
= &autofs4_sops
;
244 s
->s_d_op
= &autofs4_dentry_operations
;
248 * Get the root inode and dentry, but defer checking for errors.
250 ino
= autofs4_new_ino(sbi
);
253 root_inode
= autofs4_get_inode(s
, S_IFDIR
| 0755);
254 root
= d_make_root(root_inode
);
259 root
->d_fsdata
= ino
;
261 /* Can this call block? */
262 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
263 &sbi
->oz_pgrp
, &sbi
->type
, &sbi
->min_proto
,
265 printk("autofs: called with bogus options\n");
269 if (autofs_type_trigger(sbi
->type
))
270 __managed_dentry_set_managed(root
);
272 root_inode
->i_fop
= &autofs4_root_operations
;
273 root_inode
->i_op
= &autofs4_dir_inode_operations
;
275 /* Couldn't this be tested earlier? */
276 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
277 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
278 printk("autofs: kernel does not match daemon version "
279 "daemon (%d, %d) kernel (%d, %d)\n",
280 sbi
->min_proto
, sbi
->max_proto
,
281 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
285 /* Establish highest kernel protocol version */
286 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
287 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
289 sbi
->version
= sbi
->max_proto
;
290 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
292 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, sbi
->oz_pgrp
);
296 printk("autofs: could not open pipe file descriptor\n");
299 if (autofs_prepare_pipe(pipe
) < 0)
302 sbi
->pipefd
= pipefd
;
306 * Success! Install the root dentry now to indicate completion.
312 * Failure ... clean up.
315 printk("autofs: pipe file descriptor does not contain proper ops\n");
330 struct inode
*autofs4_get_inode(struct super_block
*sb
, umode_t mode
)
332 struct inode
*inode
= new_inode(sb
);
337 inode
->i_mode
= mode
;
339 inode
->i_uid
= sb
->s_root
->d_inode
->i_uid
;
340 inode
->i_gid
= sb
->s_root
->d_inode
->i_gid
;
342 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
343 inode
->i_ino
= get_next_ino();
347 inode
->i_op
= &autofs4_dir_inode_operations
;
348 inode
->i_fop
= &autofs4_dir_operations
;
349 } else if (S_ISLNK(mode
)) {
350 inode
->i_op
= &autofs4_symlink_inode_operations
;