2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
5 * This file is part of the Linux kernel and is made available under
6 * the terms of the GNU General Public License, version 2, or at your
7 * option, any later version, incorporated herein by reference.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/seq_file.h>
14 #include <linux/pagemap.h>
15 #include <linux/parser.h>
16 #include <linux/bitops.h>
17 #include <linux/magic.h>
19 #include <linux/module.h>
21 struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
23 struct autofs_info *ino;
25 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
27 INIT_LIST_HEAD(&ino->active);
28 INIT_LIST_HEAD(&ino->expiring);
29 ino->last_used = jiffies;
35 void autofs4_clean_ino(struct autofs_info *ino)
37 ino->uid = GLOBAL_ROOT_UID;
38 ino->gid = GLOBAL_ROOT_GID;
39 ino->last_used = jiffies;
42 void autofs4_free_ino(struct autofs_info *ino)
47 void autofs4_kill_sb(struct super_block *sb)
49 struct autofs_sb_info *sbi = autofs4_sbi(sb);
52 * In the event of a failure in get_sb_nodev the superblock
53 * info is not present so nothing else has been setup, so
54 * just call kill_anon_super when we are called from
58 /* Free wait queues, close pipe */
59 autofs4_catatonic_mode(sbi);
60 put_pid(sbi->oz_pgrp);
63 pr_debug("shutting down\n");
64 kill_litter_super(sb);
69 static int autofs4_show_options(struct seq_file *m, struct dentry *root)
71 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
72 struct inode *root_inode = d_inode(root->d_sb->s_root);
77 seq_printf(m, ",fd=%d", sbi->pipefd);
78 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
79 seq_printf(m, ",uid=%u",
80 from_kuid_munged(&init_user_ns, root_inode->i_uid));
81 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
82 seq_printf(m, ",gid=%u",
83 from_kgid_munged(&init_user_ns, root_inode->i_gid));
84 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
85 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
86 seq_printf(m, ",minproto=%d", sbi->min_proto);
87 seq_printf(m, ",maxproto=%d", sbi->max_proto);
89 if (autofs_type_offset(sbi->type))
90 seq_printf(m, ",offset");
91 else if (autofs_type_direct(sbi->type))
92 seq_printf(m, ",direct");
94 seq_printf(m, ",indirect");
95 #ifdef CONFIG_CHECKPOINT_RESTORE
97 seq_printf(m, ",pipe_ino=%ld", sbi->pipe->f_inode->i_ino);
99 seq_printf(m, ",pipe_ino=-1");
104 static void autofs4_evict_inode(struct inode *inode)
107 kfree(inode->i_private);
110 static const struct super_operations autofs4_sops = {
111 .statfs = simple_statfs,
112 .show_options = autofs4_show_options,
113 .evict_inode = autofs4_evict_inode,
116 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
117 Opt_indirect, Opt_direct, Opt_offset};
119 static const match_table_t tokens = {
123 {Opt_pgrp, "pgrp=%u"},
124 {Opt_minproto, "minproto=%u"},
125 {Opt_maxproto, "maxproto=%u"},
126 {Opt_indirect, "indirect"},
127 {Opt_direct, "direct"},
128 {Opt_offset, "offset"},
132 static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
133 int *pgrp, bool *pgrp_set, unsigned int *type,
134 int *minproto, int *maxproto)
137 substring_t args[MAX_OPT_ARGS];
140 *uid = current_uid();
141 *gid = current_gid();
143 *minproto = AUTOFS_MIN_PROTO_VERSION;
144 *maxproto = AUTOFS_MAX_PROTO_VERSION;
151 while ((p = strsep(&options, ",")) != NULL) {
157 token = match_token(p, tokens, args);
160 if (match_int(args, pipefd))
164 if (match_int(args, &option))
166 *uid = make_kuid(current_user_ns(), option);
167 if (!uid_valid(*uid))
171 if (match_int(args, &option))
173 *gid = make_kgid(current_user_ns(), option);
174 if (!gid_valid(*gid))
178 if (match_int(args, &option))
184 if (match_int(args, &option))
189 if (match_int(args, &option))
194 set_autofs_type_indirect(type);
197 set_autofs_type_direct(type);
200 set_autofs_type_offset(type);
206 return (*pipefd < 0);
209 int autofs4_fill_super(struct super_block *s, void *data, int silent)
211 struct inode *root_inode;
215 struct autofs_sb_info *sbi;
216 struct autofs_info *ino;
218 bool pgrp_set = false;
221 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
224 pr_debug("starting up, sbi = %p\n", sbi);
227 sbi->magic = AUTOFS_SBI_MAGIC;
231 sbi->exp_timeout = 0;
235 sbi->sub_version = 0;
236 set_autofs_type_indirect(&sbi->type);
239 mutex_init(&sbi->wq_mutex);
240 mutex_init(&sbi->pipe_mutex);
241 spin_lock_init(&sbi->fs_lock);
243 spin_lock_init(&sbi->lookup_lock);
244 INIT_LIST_HEAD(&sbi->active_list);
245 INIT_LIST_HEAD(&sbi->expiring_list);
246 s->s_blocksize = 1024;
247 s->s_blocksize_bits = 10;
248 s->s_magic = AUTOFS_SUPER_MAGIC;
249 s->s_op = &autofs4_sops;
250 s->s_d_op = &autofs4_dentry_operations;
254 * Get the root inode and dentry, but defer checking for errors.
256 ino = autofs4_new_ino(sbi);
261 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
262 root = d_make_root(root_inode);
267 root->d_fsdata = ino;
269 /* Can this call block? */
270 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
271 &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
273 pr_err("called with bogus options\n");
278 sbi->oz_pgrp = find_get_pid(pgrp);
280 pr_err("could not find process group %d\n",
285 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
288 if (autofs_type_trigger(sbi->type))
289 __managed_dentry_set_managed(root);
291 root_inode->i_fop = &autofs4_root_operations;
292 root_inode->i_op = &autofs4_dir_inode_operations;
294 /* Couldn't this be tested earlier? */
295 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
296 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
297 pr_err("kernel does not match daemon version "
298 "daemon (%d, %d) kernel (%d, %d)\n",
299 sbi->min_proto, sbi->max_proto,
300 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
304 /* Establish highest kernel protocol version */
305 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
306 sbi->version = AUTOFS_MAX_PROTO_VERSION;
308 sbi->version = sbi->max_proto;
309 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
311 pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp));
315 pr_err("could not open pipe file descriptor\n");
318 ret = autofs_prepare_pipe(pipe);
322 sbi->pipefd = pipefd;
326 * Success! Install the root dentry now to indicate completion.
332 * Failure ... clean up.
335 pr_err("pipe file descriptor does not contain proper ops\n");
344 put_pid(sbi->oz_pgrp);
350 struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
352 struct inode *inode = new_inode(sb);
357 inode->i_mode = mode;
359 inode->i_uid = d_inode(sb->s_root)->i_uid;
360 inode->i_gid = d_inode(sb->s_root)->i_gid;
362 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
363 inode->i_ino = get_next_ino();
367 inode->i_op = &autofs4_dir_inode_operations;
368 inode->i_fop = &autofs4_dir_operations;
369 } else if (S_ISLNK(mode)) {
370 inode->i_op = &autofs4_symlink_inode_operations;