1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/inode.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
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_init_ino(struct autofs_info *ino,
26 struct autofs_sb_info *sbi)
32 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
42 INIT_LIST_HEAD(&ino->active);
43 ino->active_count = 0;
44 INIT_LIST_HEAD(&ino->expiring);
45 atomic_set(&ino->count, 0);
50 ino->last_used = jiffies;
57 void autofs4_free_ino(struct autofs_info *ino)
60 ino->dentry->d_fsdata = NULL;
66 void autofs4_kill_sb(struct super_block *sb)
68 struct autofs_sb_info *sbi = autofs4_sbi(sb);
71 * In the event of a failure in get_sb_nodev the superblock
72 * info is not present so nothing else has been setup, so
73 * just call kill_anon_super when we are called from
79 /* Free wait queues, close pipe */
80 autofs4_catatonic_mode(sbi);
86 DPRINTK("shutting down");
87 kill_litter_super(sb);
90 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
92 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
93 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
98 seq_printf(m, ",fd=%d", sbi->pipefd);
99 if (root_inode->i_uid != 0)
100 seq_printf(m, ",uid=%u", root_inode->i_uid);
101 if (root_inode->i_gid != 0)
102 seq_printf(m, ",gid=%u", root_inode->i_gid);
103 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
104 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
105 seq_printf(m, ",minproto=%d", sbi->min_proto);
106 seq_printf(m, ",maxproto=%d", sbi->max_proto);
108 if (autofs_type_offset(sbi->type))
109 seq_printf(m, ",offset");
110 else if (autofs_type_direct(sbi->type))
111 seq_printf(m, ",direct");
113 seq_printf(m, ",indirect");
118 static void autofs4_evict_inode(struct inode *inode)
120 end_writeback(inode);
121 kfree(inode->i_private);
124 static const struct super_operations autofs4_sops = {
125 .statfs = simple_statfs,
126 .show_options = autofs4_show_options,
127 .evict_inode = autofs4_evict_inode,
130 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
131 Opt_indirect, Opt_direct, Opt_offset};
133 static const match_table_t tokens = {
137 {Opt_pgrp, "pgrp=%u"},
138 {Opt_minproto, "minproto=%u"},
139 {Opt_maxproto, "maxproto=%u"},
140 {Opt_indirect, "indirect"},
141 {Opt_direct, "direct"},
142 {Opt_offset, "offset"},
146 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
147 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
150 substring_t args[MAX_OPT_ARGS];
153 *uid = current_uid();
154 *gid = current_gid();
155 *pgrp = task_pgrp_nr(current);
157 *minproto = AUTOFS_MIN_PROTO_VERSION;
158 *maxproto = AUTOFS_MAX_PROTO_VERSION;
165 while ((p = strsep(&options, ",")) != NULL) {
170 token = match_token(p, tokens, args);
173 if (match_int(args, pipefd))
177 if (match_int(args, &option))
182 if (match_int(args, &option))
187 if (match_int(args, &option))
192 if (match_int(args, &option))
197 if (match_int(args, &option))
202 set_autofs_type_indirect(type);
205 set_autofs_type_direct(type);
208 set_autofs_type_offset(type);
214 return (*pipefd < 0);
217 int autofs4_fill_super(struct super_block *s, void *data, int silent)
219 struct inode * root_inode;
220 struct dentry * root;
223 struct autofs_sb_info *sbi;
224 struct autofs_info *ino;
226 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
229 DPRINTK("starting up, sbi = %p",sbi);
232 sbi->magic = AUTOFS_SBI_MAGIC;
236 sbi->exp_timeout = 0;
237 sbi->oz_pgrp = task_pgrp_nr(current);
240 sbi->sub_version = 0;
241 set_autofs_type_indirect(&sbi->type);
244 mutex_init(&sbi->wq_mutex);
245 spin_lock_init(&sbi->fs_lock);
247 spin_lock_init(&sbi->lookup_lock);
248 INIT_LIST_HEAD(&sbi->active_list);
249 INIT_LIST_HEAD(&sbi->expiring_list);
250 s->s_blocksize = 1024;
251 s->s_blocksize_bits = 10;
252 s->s_magic = AUTOFS_SUPER_MAGIC;
253 s->s_op = &autofs4_sops;
254 s->s_d_op = &autofs4_dentry_operations;
258 * Get the root inode and dentry, but defer checking for errors.
260 ino = autofs4_init_ino(NULL, sbi);
263 root_inode = autofs4_get_inode(s, ino, S_IFDIR | 0755);
267 root = d_alloc_root(root_inode);
272 root->d_fsdata = ino;
274 /* Can this call block? */
275 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
276 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
278 printk("autofs: called with bogus options\n");
282 if (autofs_type_trigger(sbi->type))
283 __managed_dentry_set_managed(root);
285 root_inode->i_fop = &autofs4_root_operations;
286 root_inode->i_op = &autofs4_dir_inode_operations;
288 /* Couldn't this be tested earlier? */
289 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
290 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
291 printk("autofs: kernel does not match daemon version "
292 "daemon (%d, %d) kernel (%d, %d)\n",
293 sbi->min_proto, sbi->max_proto,
294 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
298 /* Establish highest kernel protocol version */
299 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
300 sbi->version = AUTOFS_MAX_PROTO_VERSION;
302 sbi->version = sbi->max_proto;
303 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
305 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
309 printk("autofs: could not open pipe file descriptor\n");
312 if (!pipe->f_op || !pipe->f_op->write)
315 sbi->pipefd = pipefd;
319 * Success! Install the root dentry now to indicate completion.
325 * Failure ... clean up.
328 printk("autofs: pipe file descriptor does not contain proper ops\n");
335 printk("autofs: get root dentry failed\n");
346 struct inode *autofs4_get_inode(struct super_block *sb,
347 struct autofs_info *inf,
350 struct inode *inode = new_inode(sb);
355 inode->i_mode = mode;
357 inode->i_uid = sb->s_root->d_inode->i_uid;
358 inode->i_gid = sb->s_root->d_inode->i_gid;
360 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
361 inode->i_ino = get_next_ino();
365 inode->i_op = &autofs4_dir_inode_operations;
366 inode->i_fop = &autofs4_dir_operations;
367 } else if (S_ISLNK(mode)) {
368 inode->i_size = inf->size;
369 inode->i_op = &autofs4_symlink_inode_operations;