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_init_ino(struct autofs_info *ino,
26 struct autofs_sb_info *sbi, mode_t mode)
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);
51 ino->last_used = jiffies;
58 void autofs4_free_ino(struct autofs_info *ino)
61 ino->dentry->d_fsdata = NULL;
67 void autofs4_kill_sb(struct super_block *sb)
69 struct autofs_sb_info *sbi = autofs4_sbi(sb);
72 * In the event of a failure in get_sb_nodev the superblock
73 * info is not present so nothing else has been setup, so
74 * just call kill_anon_super when we are called from
80 /* Free wait queues, close pipe */
81 autofs4_catatonic_mode(sbi);
87 DPRINTK("shutting down");
88 kill_litter_super(sb);
91 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
93 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
94 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
99 seq_printf(m, ",fd=%d", sbi->pipefd);
100 if (root_inode->i_uid != 0)
101 seq_printf(m, ",uid=%u", root_inode->i_uid);
102 if (root_inode->i_gid != 0)
103 seq_printf(m, ",gid=%u", root_inode->i_gid);
104 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
105 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
106 seq_printf(m, ",minproto=%d", sbi->min_proto);
107 seq_printf(m, ",maxproto=%d", sbi->max_proto);
109 if (autofs_type_offset(sbi->type))
110 seq_printf(m, ",offset");
111 else if (autofs_type_direct(sbi->type))
112 seq_printf(m, ",direct");
114 seq_printf(m, ",indirect");
119 static void autofs4_evict_inode(struct inode *inode)
121 end_writeback(inode);
122 kfree(inode->i_private);
125 static const struct super_operations autofs4_sops = {
126 .statfs = simple_statfs,
127 .show_options = autofs4_show_options,
128 .evict_inode = autofs4_evict_inode,
131 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
132 Opt_indirect, Opt_direct, Opt_offset};
134 static const match_table_t tokens = {
138 {Opt_pgrp, "pgrp=%u"},
139 {Opt_minproto, "minproto=%u"},
140 {Opt_maxproto, "maxproto=%u"},
141 {Opt_indirect, "indirect"},
142 {Opt_direct, "direct"},
143 {Opt_offset, "offset"},
147 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
148 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
151 substring_t args[MAX_OPT_ARGS];
154 *uid = current_uid();
155 *gid = current_gid();
156 *pgrp = task_pgrp_nr(current);
158 *minproto = AUTOFS_MIN_PROTO_VERSION;
159 *maxproto = AUTOFS_MAX_PROTO_VERSION;
166 while ((p = strsep(&options, ",")) != NULL) {
171 token = match_token(p, tokens, args);
174 if (match_int(args, pipefd))
178 if (match_int(args, &option))
183 if (match_int(args, &option))
188 if (match_int(args, &option))
193 if (match_int(args, &option))
198 if (match_int(args, &option))
203 set_autofs_type_indirect(type);
206 set_autofs_type_direct(type);
209 set_autofs_type_offset(type);
215 return (*pipefd < 0);
218 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
220 struct autofs_info *ino;
222 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
229 int autofs4_fill_super(struct super_block *s, void *data, int silent)
231 struct inode * root_inode;
232 struct dentry * root;
235 struct autofs_sb_info *sbi;
236 struct autofs_info *ino;
238 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
241 DPRINTK("starting up, sbi = %p",sbi);
244 sbi->magic = AUTOFS_SBI_MAGIC;
248 sbi->exp_timeout = 0;
249 sbi->oz_pgrp = task_pgrp_nr(current);
252 sbi->sub_version = 0;
253 set_autofs_type_indirect(&sbi->type);
256 mutex_init(&sbi->wq_mutex);
257 spin_lock_init(&sbi->fs_lock);
259 spin_lock_init(&sbi->lookup_lock);
260 INIT_LIST_HEAD(&sbi->active_list);
261 INIT_LIST_HEAD(&sbi->expiring_list);
262 s->s_blocksize = 1024;
263 s->s_blocksize_bits = 10;
264 s->s_magic = AUTOFS_SUPER_MAGIC;
265 s->s_op = &autofs4_sops;
266 s->s_d_op = &autofs4_dentry_operations;
270 * Get the root inode and dentry, but defer checking for errors.
272 ino = autofs4_mkroot(sbi);
275 root_inode = autofs4_get_inode(s, ino);
279 root = d_alloc_root(root_inode);
284 root->d_fsdata = ino;
286 /* Can this call block? */
287 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
288 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
290 printk("autofs: called with bogus options\n");
294 if (autofs_type_trigger(sbi->type))
295 __managed_dentry_set_managed(root);
297 root_inode->i_fop = &autofs4_root_operations;
298 root_inode->i_op = &autofs4_dir_inode_operations;
300 /* Couldn't this be tested earlier? */
301 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
302 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
303 printk("autofs: kernel does not match daemon version "
304 "daemon (%d, %d) kernel (%d, %d)\n",
305 sbi->min_proto, sbi->max_proto,
306 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
310 /* Establish highest kernel protocol version */
311 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
312 sbi->version = AUTOFS_MAX_PROTO_VERSION;
314 sbi->version = sbi->max_proto;
315 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
317 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
321 printk("autofs: could not open pipe file descriptor\n");
324 if (!pipe->f_op || !pipe->f_op->write)
327 sbi->pipefd = pipefd;
331 * Success! Install the root dentry now to indicate completion.
337 * Failure ... clean up.
340 printk("autofs: pipe file descriptor does not contain proper ops\n");
347 printk("autofs: get root dentry failed\n");
358 struct inode *autofs4_get_inode(struct super_block *sb,
359 struct autofs_info *inf)
361 struct inode *inode = new_inode(sb);
366 inode->i_mode = inf->mode;
368 inode->i_uid = sb->s_root->d_inode->i_uid;
369 inode->i_gid = sb->s_root->d_inode->i_gid;
371 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
372 inode->i_ino = get_next_ino();
374 if (S_ISDIR(inf->mode)) {
376 inode->i_op = &autofs4_dir_inode_operations;
377 inode->i_fop = &autofs4_dir_operations;
378 } else if (S_ISLNK(inf->mode)) {
379 inode->i_size = inf->size;
380 inode->i_op = &autofs4_symlink_inode_operations;