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>
22 #include <linux/module.h>
24 struct autofs_info
*autofs4_new_ino(struct autofs_sb_info
*sbi
)
26 struct autofs_info
*ino
= kzalloc(sizeof(*ino
), GFP_KERNEL
);
28 INIT_LIST_HEAD(&ino
->active
);
29 INIT_LIST_HEAD(&ino
->expiring
);
30 ino
->last_used
= jiffies
;
36 void autofs4_clean_ino(struct autofs_info
*ino
)
38 ino
->uid
= GLOBAL_ROOT_UID
;
39 ino
->gid
= GLOBAL_ROOT_GID
;
40 ino
->last_used
= jiffies
;
43 void autofs4_free_ino(struct autofs_info
*ino
)
48 void autofs4_kill_sb(struct super_block
*sb
)
50 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
53 * In the event of a failure in get_sb_nodev the superblock
54 * info is not present so nothing else has been setup, so
55 * just call kill_anon_super when we are called from
59 /* Free wait queues, close pipe */
60 autofs4_catatonic_mode(sbi
);
61 put_pid(sbi
->oz_pgrp
);
64 DPRINTK("shutting down");
65 kill_litter_super(sb
);
70 static int autofs4_show_options(struct seq_file
*m
, struct dentry
*root
)
72 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
73 struct inode
*root_inode
= d_inode(root
->d_sb
->s_root
);
78 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
79 if (!uid_eq(root_inode
->i_uid
, GLOBAL_ROOT_UID
))
80 seq_printf(m
, ",uid=%u",
81 from_kuid_munged(&init_user_ns
, root_inode
->i_uid
));
82 if (!gid_eq(root_inode
->i_gid
, GLOBAL_ROOT_GID
))
83 seq_printf(m
, ",gid=%u",
84 from_kgid_munged(&init_user_ns
, root_inode
->i_gid
));
85 seq_printf(m
, ",pgrp=%d", pid_vnr(sbi
->oz_pgrp
));
86 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
87 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
88 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
90 if (autofs_type_offset(sbi
->type
))
91 seq_printf(m
, ",offset");
92 else if (autofs_type_direct(sbi
->type
))
93 seq_printf(m
, ",direct");
95 seq_printf(m
, ",indirect");
100 static void autofs4_evict_inode(struct inode
*inode
)
103 kfree(inode
->i_private
);
106 static const struct super_operations autofs4_sops
= {
107 .statfs
= simple_statfs
,
108 .show_options
= autofs4_show_options
,
109 .evict_inode
= autofs4_evict_inode
,
112 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
113 Opt_indirect
, Opt_direct
, Opt_offset
};
115 static const match_table_t tokens
= {
119 {Opt_pgrp
, "pgrp=%u"},
120 {Opt_minproto
, "minproto=%u"},
121 {Opt_maxproto
, "maxproto=%u"},
122 {Opt_indirect
, "indirect"},
123 {Opt_direct
, "direct"},
124 {Opt_offset
, "offset"},
128 static int parse_options(char *options
, int *pipefd
, kuid_t
*uid
, kgid_t
*gid
,
129 int *pgrp
, bool *pgrp_set
, unsigned int *type
,
130 int *minproto
, int *maxproto
)
133 substring_t args
[MAX_OPT_ARGS
];
136 *uid
= current_uid();
137 *gid
= current_gid();
139 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
140 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
147 while ((p
= strsep(&options
, ",")) != NULL
) {
152 token
= match_token(p
, tokens
, args
);
155 if (match_int(args
, pipefd
))
159 if (match_int(args
, &option
))
161 *uid
= make_kuid(current_user_ns(), option
);
162 if (!uid_valid(*uid
))
166 if (match_int(args
, &option
))
168 *gid
= make_kgid(current_user_ns(), option
);
169 if (!gid_valid(*gid
))
173 if (match_int(args
, &option
))
179 if (match_int(args
, &option
))
184 if (match_int(args
, &option
))
189 set_autofs_type_indirect(type
);
192 set_autofs_type_direct(type
);
195 set_autofs_type_offset(type
);
201 return (*pipefd
< 0);
204 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
206 struct inode
* root_inode
;
207 struct dentry
* root
;
210 struct autofs_sb_info
*sbi
;
211 struct autofs_info
*ino
;
213 bool pgrp_set
= false;
216 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
219 DPRINTK("starting up, sbi = %p",sbi
);
222 sbi
->magic
= AUTOFS_SBI_MAGIC
;
226 sbi
->exp_timeout
= 0;
230 sbi
->sub_version
= 0;
231 set_autofs_type_indirect(&sbi
->type
);
234 mutex_init(&sbi
->wq_mutex
);
235 mutex_init(&sbi
->pipe_mutex
);
236 spin_lock_init(&sbi
->fs_lock
);
238 spin_lock_init(&sbi
->lookup_lock
);
239 INIT_LIST_HEAD(&sbi
->active_list
);
240 INIT_LIST_HEAD(&sbi
->expiring_list
);
241 s
->s_blocksize
= 1024;
242 s
->s_blocksize_bits
= 10;
243 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
244 s
->s_op
= &autofs4_sops
;
245 s
->s_d_op
= &autofs4_dentry_operations
;
249 * Get the root inode and dentry, but defer checking for errors.
251 ino
= autofs4_new_ino(sbi
);
256 root_inode
= autofs4_get_inode(s
, S_IFDIR
| 0755);
257 root
= d_make_root(root_inode
);
264 root
->d_fsdata
= ino
;
266 /* Can this call block? */
267 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
268 &pgrp
, &pgrp_set
, &sbi
->type
, &sbi
->min_proto
,
270 printk("autofs: called with bogus options\n");
275 sbi
->oz_pgrp
= find_get_pid(pgrp
);
277 pr_warn("autofs: could not find process group %d\n",
282 sbi
->oz_pgrp
= get_task_pid(current
, PIDTYPE_PGID
);
285 if (autofs_type_trigger(sbi
->type
))
286 __managed_dentry_set_managed(root
);
288 root_inode
->i_fop
= &autofs4_root_operations
;
289 root_inode
->i_op
= &autofs4_dir_inode_operations
;
291 /* Couldn't this be tested earlier? */
292 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
293 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
294 printk("autofs: kernel does not match daemon version "
295 "daemon (%d, %d) kernel (%d, %d)\n",
296 sbi
->min_proto
, sbi
->max_proto
,
297 AUTOFS_MIN_PROTO_VERSION
, AUTOFS_MAX_PROTO_VERSION
);
301 /* Establish highest kernel protocol version */
302 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
303 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
305 sbi
->version
= sbi
->max_proto
;
306 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
308 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, pid_nr(sbi
->oz_pgrp
));
312 printk("autofs: could not open pipe file descriptor\n");
315 ret
= autofs_prepare_pipe(pipe
);
319 sbi
->pipefd
= pipefd
;
323 * Success! Install the root dentry now to indicate completion.
329 * Failure ... clean up.
332 printk("autofs: pipe file descriptor does not contain proper ops\n");
341 put_pid(sbi
->oz_pgrp
);
347 struct inode
*autofs4_get_inode(struct super_block
*sb
, umode_t mode
)
349 struct inode
*inode
= new_inode(sb
);
354 inode
->i_mode
= mode
;
356 inode
->i_uid
= d_inode(sb
->s_root
)->i_uid
;
357 inode
->i_gid
= d_inode(sb
->s_root
)->i_gid
;
359 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
360 inode
->i_ino
= get_next_ino();
364 inode
->i_op
= &autofs4_dir_inode_operations
;
365 inode
->i_fop
= &autofs4_dir_operations
;
366 } else if (S_ISLNK(mode
)) {
367 inode
->i_op
= &autofs4_symlink_inode_operations
;