autofs4: split autofs4_init_ino()
[pandora-kernel.git] / fs / autofs4 / inode.c
1 /* -*- c -*- --------------------------------------------------------------- *
2  *
3  * linux/fs/autofs/inode.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *  Copyright 2005-2006 Ian Kent <raven@themaw.net>
7  *
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.
11  *
12  * ------------------------------------------------------------------------- */
13
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>
24
25 struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
26 {
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;
33         }
34         return ino;
35 }
36
37 void autofs4_clean_ino(struct autofs_info *ino)
38 {
39         ino->uid = 0;
40         ino->gid = 0;
41         ino->last_used = jiffies;
42 }
43
44 void autofs4_free_ino(struct autofs_info *ino)
45 {
46         if (ino->dentry) {
47                 ino->dentry->d_fsdata = NULL;
48                 ino->dentry = NULL;
49         }
50         kfree(ino);
51 }
52
53 void autofs4_kill_sb(struct super_block *sb)
54 {
55         struct autofs_sb_info *sbi = autofs4_sbi(sb);
56
57         /*
58          * In the event of a failure in get_sb_nodev the superblock
59          * info is not present so nothing else has been setup, so
60          * just call kill_anon_super when we are called from
61          * deactivate_super.
62          */
63         if (!sbi)
64                 goto out_kill_sb;
65
66         /* Free wait queues, close pipe */
67         autofs4_catatonic_mode(sbi);
68
69         sb->s_fs_info = NULL;
70         kfree(sbi);
71
72 out_kill_sb:
73         DPRINTK("shutting down");
74         kill_litter_super(sb);
75 }
76
77 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
78 {
79         struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
80         struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
81
82         if (!sbi)
83                 return 0;
84
85         seq_printf(m, ",fd=%d", sbi->pipefd);
86         if (root_inode->i_uid != 0)
87                 seq_printf(m, ",uid=%u", root_inode->i_uid);
88         if (root_inode->i_gid != 0)
89                 seq_printf(m, ",gid=%u", root_inode->i_gid);
90         seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
91         seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
92         seq_printf(m, ",minproto=%d", sbi->min_proto);
93         seq_printf(m, ",maxproto=%d", sbi->max_proto);
94
95         if (autofs_type_offset(sbi->type))
96                 seq_printf(m, ",offset");
97         else if (autofs_type_direct(sbi->type))
98                 seq_printf(m, ",direct");
99         else
100                 seq_printf(m, ",indirect");
101
102         return 0;
103 }
104
105 static void autofs4_evict_inode(struct inode *inode)
106 {
107         end_writeback(inode);
108         kfree(inode->i_private);
109 }
110
111 static const struct super_operations autofs4_sops = {
112         .statfs         = simple_statfs,
113         .show_options   = autofs4_show_options,
114         .evict_inode    = autofs4_evict_inode,
115 };
116
117 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
118         Opt_indirect, Opt_direct, Opt_offset};
119
120 static const match_table_t tokens = {
121         {Opt_fd, "fd=%u"},
122         {Opt_uid, "uid=%u"},
123         {Opt_gid, "gid=%u"},
124         {Opt_pgrp, "pgrp=%u"},
125         {Opt_minproto, "minproto=%u"},
126         {Opt_maxproto, "maxproto=%u"},
127         {Opt_indirect, "indirect"},
128         {Opt_direct, "direct"},
129         {Opt_offset, "offset"},
130         {Opt_err, NULL}
131 };
132
133 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
134                 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
135 {
136         char *p;
137         substring_t args[MAX_OPT_ARGS];
138         int option;
139
140         *uid = current_uid();
141         *gid = current_gid();
142         *pgrp = task_pgrp_nr(current);
143
144         *minproto = AUTOFS_MIN_PROTO_VERSION;
145         *maxproto = AUTOFS_MAX_PROTO_VERSION;
146
147         *pipefd = -1;
148
149         if (!options)
150                 return 1;
151
152         while ((p = strsep(&options, ",")) != NULL) {
153                 int token;
154                 if (!*p)
155                         continue;
156
157                 token = match_token(p, tokens, args);
158                 switch (token) {
159                 case Opt_fd:
160                         if (match_int(args, pipefd))
161                                 return 1;
162                         break;
163                 case Opt_uid:
164                         if (match_int(args, &option))
165                                 return 1;
166                         *uid = option;
167                         break;
168                 case Opt_gid:
169                         if (match_int(args, &option))
170                                 return 1;
171                         *gid = option;
172                         break;
173                 case Opt_pgrp:
174                         if (match_int(args, &option))
175                                 return 1;
176                         *pgrp = option;
177                         break;
178                 case Opt_minproto:
179                         if (match_int(args, &option))
180                                 return 1;
181                         *minproto = option;
182                         break;
183                 case Opt_maxproto:
184                         if (match_int(args, &option))
185                                 return 1;
186                         *maxproto = option;
187                         break;
188                 case Opt_indirect:
189                         set_autofs_type_indirect(type);
190                         break;
191                 case Opt_direct:
192                         set_autofs_type_direct(type);
193                         break;
194                 case Opt_offset:
195                         set_autofs_type_offset(type);
196                         break;
197                 default:
198                         return 1;
199                 }
200         }
201         return (*pipefd < 0);
202 }
203
204 int autofs4_fill_super(struct super_block *s, void *data, int silent)
205 {
206         struct inode * root_inode;
207         struct dentry * root;
208         struct file * pipe;
209         int pipefd;
210         struct autofs_sb_info *sbi;
211         struct autofs_info *ino;
212
213         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
214         if (!sbi)
215                 goto fail_unlock;
216         DPRINTK("starting up, sbi = %p",sbi);
217
218         s->s_fs_info = sbi;
219         sbi->magic = AUTOFS_SBI_MAGIC;
220         sbi->pipefd = -1;
221         sbi->pipe = NULL;
222         sbi->catatonic = 1;
223         sbi->exp_timeout = 0;
224         sbi->oz_pgrp = task_pgrp_nr(current);
225         sbi->sb = s;
226         sbi->version = 0;
227         sbi->sub_version = 0;
228         set_autofs_type_indirect(&sbi->type);
229         sbi->min_proto = 0;
230         sbi->max_proto = 0;
231         mutex_init(&sbi->wq_mutex);
232         spin_lock_init(&sbi->fs_lock);
233         sbi->queues = NULL;
234         spin_lock_init(&sbi->lookup_lock);
235         INIT_LIST_HEAD(&sbi->active_list);
236         INIT_LIST_HEAD(&sbi->expiring_list);
237         s->s_blocksize = 1024;
238         s->s_blocksize_bits = 10;
239         s->s_magic = AUTOFS_SUPER_MAGIC;
240         s->s_op = &autofs4_sops;
241         s->s_d_op = &autofs4_dentry_operations;
242         s->s_time_gran = 1;
243
244         /*
245          * Get the root inode and dentry, but defer checking for errors.
246          */
247         ino = autofs4_new_ino(sbi);
248         if (!ino)
249                 goto fail_free;
250         root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
251         if (!root_inode)
252                 goto fail_ino;
253
254         root = d_alloc_root(root_inode);
255         if (!root)
256                 goto fail_iput;
257         pipe = NULL;
258
259         root->d_fsdata = ino;
260
261         /* Can this call block? */
262         if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
263                                 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
264                                 &sbi->max_proto)) {
265                 printk("autofs: called with bogus options\n");
266                 goto fail_dput;
267         }
268
269         if (autofs_type_trigger(sbi->type))
270                 __managed_dentry_set_managed(root);
271
272         root_inode->i_fop = &autofs4_root_operations;
273         root_inode->i_op = &autofs4_dir_inode_operations;
274
275         /* Couldn't this be tested earlier? */
276         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
277             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
278                 printk("autofs: kernel does not match daemon version "
279                        "daemon (%d, %d) kernel (%d, %d)\n",
280                         sbi->min_proto, sbi->max_proto,
281                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
282                 goto fail_dput;
283         }
284
285         /* Establish highest kernel protocol version */
286         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
287                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
288         else
289                 sbi->version = sbi->max_proto;
290         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
291
292         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
293         pipe = fget(pipefd);
294         
295         if (!pipe) {
296                 printk("autofs: could not open pipe file descriptor\n");
297                 goto fail_dput;
298         }
299         if (!pipe->f_op || !pipe->f_op->write)
300                 goto fail_fput;
301         sbi->pipe = pipe;
302         sbi->pipefd = pipefd;
303         sbi->catatonic = 0;
304
305         /*
306          * Success! Install the root dentry now to indicate completion.
307          */
308         s->s_root = root;
309         return 0;
310         
311         /*
312          * Failure ... clean up.
313          */
314 fail_fput:
315         printk("autofs: pipe file descriptor does not contain proper ops\n");
316         fput(pipe);
317         /* fall through */
318 fail_dput:
319         dput(root);
320         goto fail_free;
321 fail_iput:
322         printk("autofs: get root dentry failed\n");
323         iput(root_inode);
324 fail_ino:
325         kfree(ino);
326 fail_free:
327         kfree(sbi);
328         s->s_fs_info = NULL;
329 fail_unlock:
330         return -EINVAL;
331 }
332
333 struct inode *autofs4_get_inode(struct super_block *sb, mode_t mode)
334 {
335         struct inode *inode = new_inode(sb);
336
337         if (inode == NULL)
338                 return NULL;
339
340         inode->i_mode = mode;
341         if (sb->s_root) {
342                 inode->i_uid = sb->s_root->d_inode->i_uid;
343                 inode->i_gid = sb->s_root->d_inode->i_gid;
344         }
345         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
346         inode->i_ino = get_next_ino();
347
348         if (S_ISDIR(mode)) {
349                 inode->i_nlink = 2;
350                 inode->i_op = &autofs4_dir_inode_operations;
351                 inode->i_fop = &autofs4_dir_operations;
352         } else if (S_ISLNK(mode)) {
353                 inode->i_op = &autofs4_symlink_inode_operations;
354         }
355
356         return inode;
357 }