Merge branch 'next' of git://git.monstr.eu/linux-2.6-microblaze
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / autofs4 / inode.c
blobe16980b00b8d7315dda8d7b92d93fdaf7062e843
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>
22 #include "autofs_i.h"
23 #include <linux/module.h>
25 struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
27 struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
28 if (ino) {
29 INIT_LIST_HEAD(&ino->active);
30 INIT_LIST_HEAD(&ino->expiring);
31 ino->last_used = jiffies;
32 ino->sbi = sbi;
34 return ino;
37 void autofs4_clean_ino(struct autofs_info *ino)
39 ino->uid = 0;
40 ino->gid = 0;
41 ino->last_used = jiffies;
44 void autofs4_free_ino(struct autofs_info *ino)
46 kfree(ino);
49 void autofs4_kill_sb(struct super_block *sb)
51 struct autofs_sb_info *sbi = autofs4_sbi(sb);
54 * In the event of a failure in get_sb_nodev the superblock
55 * info is not present so nothing else has been setup, so
56 * just call kill_anon_super when we are called from
57 * deactivate_super.
59 if (!sbi)
60 goto out_kill_sb;
62 /* Free wait queues, close pipe */
63 autofs4_catatonic_mode(sbi);
65 sb->s_fs_info = NULL;
66 kfree(sbi);
68 out_kill_sb:
69 DPRINTK("shutting down");
70 kill_litter_super(sb);
73 static int autofs4_show_options(struct seq_file *m, struct dentry *root)
75 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
76 struct inode *root_inode = root->d_sb->s_root->d_inode;
78 if (!sbi)
79 return 0;
81 seq_printf(m, ",fd=%d", sbi->pipefd);
82 if (root_inode->i_uid != 0)
83 seq_printf(m, ",uid=%u", root_inode->i_uid);
84 if (root_inode->i_gid != 0)
85 seq_printf(m, ",gid=%u", root_inode->i_gid);
86 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
87 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
88 seq_printf(m, ",minproto=%d", sbi->min_proto);
89 seq_printf(m, ",maxproto=%d", sbi->max_proto);
91 if (autofs_type_offset(sbi->type))
92 seq_printf(m, ",offset");
93 else if (autofs_type_direct(sbi->type))
94 seq_printf(m, ",direct");
95 else
96 seq_printf(m, ",indirect");
98 return 0;
101 static void autofs4_evict_inode(struct inode *inode)
103 end_writeback(inode);
104 kfree(inode->i_private);
107 static const struct super_operations autofs4_sops = {
108 .statfs = simple_statfs,
109 .show_options = autofs4_show_options,
110 .evict_inode = autofs4_evict_inode,
113 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
114 Opt_indirect, Opt_direct, Opt_offset};
116 static const match_table_t tokens = {
117 {Opt_fd, "fd=%u"},
118 {Opt_uid, "uid=%u"},
119 {Opt_gid, "gid=%u"},
120 {Opt_pgrp, "pgrp=%u"},
121 {Opt_minproto, "minproto=%u"},
122 {Opt_maxproto, "maxproto=%u"},
123 {Opt_indirect, "indirect"},
124 {Opt_direct, "direct"},
125 {Opt_offset, "offset"},
126 {Opt_err, NULL}
129 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
130 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
132 char *p;
133 substring_t args[MAX_OPT_ARGS];
134 int option;
136 *uid = current_uid();
137 *gid = current_gid();
138 *pgrp = task_pgrp_nr(current);
140 *minproto = AUTOFS_MIN_PROTO_VERSION;
141 *maxproto = AUTOFS_MAX_PROTO_VERSION;
143 *pipefd = -1;
145 if (!options)
146 return 1;
148 while ((p = strsep(&options, ",")) != NULL) {
149 int token;
150 if (!*p)
151 continue;
153 token = match_token(p, tokens, args);
154 switch (token) {
155 case Opt_fd:
156 if (match_int(args, pipefd))
157 return 1;
158 break;
159 case Opt_uid:
160 if (match_int(args, &option))
161 return 1;
162 *uid = option;
163 break;
164 case Opt_gid:
165 if (match_int(args, &option))
166 return 1;
167 *gid = option;
168 break;
169 case Opt_pgrp:
170 if (match_int(args, &option))
171 return 1;
172 *pgrp = option;
173 break;
174 case Opt_minproto:
175 if (match_int(args, &option))
176 return 1;
177 *minproto = option;
178 break;
179 case Opt_maxproto:
180 if (match_int(args, &option))
181 return 1;
182 *maxproto = option;
183 break;
184 case Opt_indirect:
185 set_autofs_type_indirect(type);
186 break;
187 case Opt_direct:
188 set_autofs_type_direct(type);
189 break;
190 case Opt_offset:
191 set_autofs_type_offset(type);
192 break;
193 default:
194 return 1;
197 return (*pipefd < 0);
200 int autofs4_fill_super(struct super_block *s, void *data, int silent)
202 struct inode * root_inode;
203 struct dentry * root;
204 struct file * pipe;
205 int pipefd;
206 struct autofs_sb_info *sbi;
207 struct autofs_info *ino;
209 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
210 if (!sbi)
211 goto fail_unlock;
212 DPRINTK("starting up, sbi = %p",sbi);
214 s->s_fs_info = sbi;
215 sbi->magic = AUTOFS_SBI_MAGIC;
216 sbi->pipefd = -1;
217 sbi->pipe = NULL;
218 sbi->catatonic = 1;
219 sbi->exp_timeout = 0;
220 sbi->oz_pgrp = task_pgrp_nr(current);
221 sbi->sb = s;
222 sbi->version = 0;
223 sbi->sub_version = 0;
224 set_autofs_type_indirect(&sbi->type);
225 sbi->min_proto = 0;
226 sbi->max_proto = 0;
227 mutex_init(&sbi->wq_mutex);
228 mutex_init(&sbi->pipe_mutex);
229 spin_lock_init(&sbi->fs_lock);
230 sbi->queues = NULL;
231 spin_lock_init(&sbi->lookup_lock);
232 INIT_LIST_HEAD(&sbi->active_list);
233 INIT_LIST_HEAD(&sbi->expiring_list);
234 s->s_blocksize = 1024;
235 s->s_blocksize_bits = 10;
236 s->s_magic = AUTOFS_SUPER_MAGIC;
237 s->s_op = &autofs4_sops;
238 s->s_d_op = &autofs4_dentry_operations;
239 s->s_time_gran = 1;
242 * Get the root inode and dentry, but defer checking for errors.
244 ino = autofs4_new_ino(sbi);
245 if (!ino)
246 goto fail_free;
247 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
248 if (!root_inode)
249 goto fail_ino;
251 root = d_alloc_root(root_inode);
252 if (!root)
253 goto fail_iput;
254 pipe = NULL;
256 root->d_fsdata = ino;
258 /* Can this call block? */
259 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
260 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
261 &sbi->max_proto)) {
262 printk("autofs: called with bogus options\n");
263 goto fail_dput;
266 if (autofs_type_trigger(sbi->type))
267 __managed_dentry_set_managed(root);
269 root_inode->i_fop = &autofs4_root_operations;
270 root_inode->i_op = &autofs4_dir_inode_operations;
272 /* Couldn't this be tested earlier? */
273 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
274 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
275 printk("autofs: kernel does not match daemon version "
276 "daemon (%d, %d) kernel (%d, %d)\n",
277 sbi->min_proto, sbi->max_proto,
278 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
279 goto fail_dput;
282 /* Establish highest kernel protocol version */
283 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
284 sbi->version = AUTOFS_MAX_PROTO_VERSION;
285 else
286 sbi->version = sbi->max_proto;
287 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
289 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
290 pipe = fget(pipefd);
292 if (!pipe) {
293 printk("autofs: could not open pipe file descriptor\n");
294 goto fail_dput;
296 if (!pipe->f_op || !pipe->f_op->write)
297 goto fail_fput;
298 sbi->pipe = pipe;
299 sbi->pipefd = pipefd;
300 sbi->catatonic = 0;
303 * Success! Install the root dentry now to indicate completion.
305 s->s_root = root;
306 return 0;
309 * Failure ... clean up.
311 fail_fput:
312 printk("autofs: pipe file descriptor does not contain proper ops\n");
313 fput(pipe);
314 /* fall through */
315 fail_dput:
316 dput(root);
317 goto fail_free;
318 fail_iput:
319 printk("autofs: get root dentry failed\n");
320 iput(root_inode);
321 fail_ino:
322 kfree(ino);
323 fail_free:
324 kfree(sbi);
325 s->s_fs_info = NULL;
326 fail_unlock:
327 return -EINVAL;
330 struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
332 struct inode *inode = new_inode(sb);
334 if (inode == NULL)
335 return NULL;
337 inode->i_mode = mode;
338 if (sb->s_root) {
339 inode->i_uid = sb->s_root->d_inode->i_uid;
340 inode->i_gid = sb->s_root->d_inode->i_gid;
342 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
343 inode->i_ino = get_next_ino();
345 if (S_ISDIR(mode)) {
346 set_nlink(inode, 2);
347 inode->i_op = &autofs4_dir_inode_operations;
348 inode->i_fop = &autofs4_dir_operations;
349 } else if (S_ISLNK(mode)) {
350 inode->i_op = &autofs4_symlink_inode_operations;
353 return inode;