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 static void ino_lnkfree(struct autofs_info *ino)
28 kfree(ino->u.symlink);
29 ino->u.symlink = NULL;
33 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34 struct autofs_sb_info *sbi, mode_t mode)
40 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
50 INIT_LIST_HEAD(&ino->active);
51 ino->active_count = 0;
52 INIT_LIST_HEAD(&ino->expiring);
53 atomic_set(&ino->count, 0);
59 ino->last_used = jiffies;
63 if (reinit && ino->free)
66 memset(&ino->u, 0, sizeof(ino->u));
71 ino->free = ino_lnkfree;
76 void autofs4_free_ino(struct autofs_info *ino)
79 ino->dentry->d_fsdata = NULL;
87 void autofs4_kill_sb(struct super_block *sb)
89 struct autofs_sb_info *sbi = autofs4_sbi(sb);
92 * In the event of a failure in get_sb_nodev the superblock
93 * info is not present so nothing else has been setup, so
94 * just call kill_anon_super when we are called from
100 /* Free wait queues, close pipe */
101 autofs4_catatonic_mode(sbi);
103 sb->s_fs_info = NULL;
107 DPRINTK("shutting down");
108 kill_litter_super(sb);
111 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
113 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
114 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
119 seq_printf(m, ",fd=%d", sbi->pipefd);
120 if (root_inode->i_uid != 0)
121 seq_printf(m, ",uid=%u", root_inode->i_uid);
122 if (root_inode->i_gid != 0)
123 seq_printf(m, ",gid=%u", root_inode->i_gid);
124 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
125 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
126 seq_printf(m, ",minproto=%d", sbi->min_proto);
127 seq_printf(m, ",maxproto=%d", sbi->max_proto);
129 if (autofs_type_offset(sbi->type))
130 seq_printf(m, ",offset");
131 else if (autofs_type_direct(sbi->type))
132 seq_printf(m, ",direct");
134 seq_printf(m, ",indirect");
139 static const struct super_operations autofs4_sops = {
140 .statfs = simple_statfs,
141 .show_options = autofs4_show_options,
144 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
145 Opt_indirect, Opt_direct, Opt_offset};
147 static const match_table_t tokens = {
151 {Opt_pgrp, "pgrp=%u"},
152 {Opt_minproto, "minproto=%u"},
153 {Opt_maxproto, "maxproto=%u"},
154 {Opt_indirect, "indirect"},
155 {Opt_direct, "direct"},
156 {Opt_offset, "offset"},
160 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
161 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
164 substring_t args[MAX_OPT_ARGS];
167 *uid = current_uid();
168 *gid = current_gid();
169 *pgrp = task_pgrp_nr(current);
171 *minproto = AUTOFS_MIN_PROTO_VERSION;
172 *maxproto = AUTOFS_MAX_PROTO_VERSION;
179 while ((p = strsep(&options, ",")) != NULL) {
184 token = match_token(p, tokens, args);
187 if (match_int(args, pipefd))
191 if (match_int(args, &option))
196 if (match_int(args, &option))
201 if (match_int(args, &option))
206 if (match_int(args, &option))
211 if (match_int(args, &option))
216 set_autofs_type_indirect(type);
219 set_autofs_type_direct(type);
222 set_autofs_type_offset(type);
228 return (*pipefd < 0);
231 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
233 struct autofs_info *ino;
235 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
242 int autofs4_fill_super(struct super_block *s, void *data, int silent)
244 struct inode * root_inode;
245 struct dentry * root;
248 struct autofs_sb_info *sbi;
249 struct autofs_info *ino;
251 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
254 DPRINTK("starting up, sbi = %p",sbi);
257 sbi->magic = AUTOFS_SBI_MAGIC;
261 sbi->exp_timeout = 0;
262 sbi->oz_pgrp = task_pgrp_nr(current);
265 sbi->sub_version = 0;
266 set_autofs_type_indirect(&sbi->type);
269 mutex_init(&sbi->wq_mutex);
270 spin_lock_init(&sbi->fs_lock);
272 spin_lock_init(&sbi->lookup_lock);
273 INIT_LIST_HEAD(&sbi->active_list);
274 INIT_LIST_HEAD(&sbi->expiring_list);
275 s->s_blocksize = 1024;
276 s->s_blocksize_bits = 10;
277 s->s_magic = AUTOFS_SUPER_MAGIC;
278 s->s_op = &autofs4_sops;
279 s->s_d_op = &autofs4_dentry_operations;
283 * Get the root inode and dentry, but defer checking for errors.
285 ino = autofs4_mkroot(sbi);
288 root_inode = autofs4_get_inode(s, ino);
292 root = d_alloc_root(root_inode);
297 root->d_fsdata = ino;
299 /* Can this call block? */
300 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
301 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
303 printk("autofs: called with bogus options\n");
307 if (autofs_type_trigger(sbi->type))
308 __managed_dentry_set_managed(root);
310 root_inode->i_fop = &autofs4_root_operations;
311 root_inode->i_op = &autofs4_dir_inode_operations;
313 /* Couldn't this be tested earlier? */
314 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
315 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
316 printk("autofs: kernel does not match daemon version "
317 "daemon (%d, %d) kernel (%d, %d)\n",
318 sbi->min_proto, sbi->max_proto,
319 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
323 /* Establish highest kernel protocol version */
324 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
325 sbi->version = AUTOFS_MAX_PROTO_VERSION;
327 sbi->version = sbi->max_proto;
328 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
330 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
334 printk("autofs: could not open pipe file descriptor\n");
337 if (!pipe->f_op || !pipe->f_op->write)
340 sbi->pipefd = pipefd;
344 * Success! Install the root dentry now to indicate completion.
350 * Failure ... clean up.
353 printk("autofs: pipe file descriptor does not contain proper ops\n");
360 printk("autofs: get root dentry failed\n");
371 struct inode *autofs4_get_inode(struct super_block *sb,
372 struct autofs_info *inf)
374 struct inode *inode = new_inode(sb);
379 inode->i_mode = inf->mode;
381 inode->i_uid = sb->s_root->d_inode->i_uid;
382 inode->i_gid = sb->s_root->d_inode->i_gid;
384 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
385 inode->i_ino = get_next_ino();
387 if (S_ISDIR(inf->mode)) {
389 inode->i_op = &autofs4_dir_inode_operations;
390 inode->i_fop = &autofs4_dir_operations;
391 } else if (S_ISLNK(inf->mode)) {
392 inode->i_size = inf->size;
393 inode->i_op = &autofs4_symlink_inode_operations;