Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[linux-2.6.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 static void ino_lnkfree(struct autofs_info *ino)
26 {
27         if (ino->u.symlink) {
28                 kfree(ino->u.symlink);
29                 ino->u.symlink = NULL;
30         }
31 }
32
33 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34                                      struct autofs_sb_info *sbi, mode_t mode)
35 {
36         int reinit = 1;
37
38         if (ino == NULL) {
39                 reinit = 0;
40                 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
41         }
42
43         if (ino == NULL)
44                 return NULL;
45
46         if (!reinit) {
47                 ino->flags = 0;
48                 ino->dentry = NULL;
49                 ino->size = 0;
50                 INIT_LIST_HEAD(&ino->active);
51                 ino->active_count = 0;
52                 INIT_LIST_HEAD(&ino->expiring);
53                 atomic_set(&ino->count, 0);
54         }
55
56         ino->uid = 0;
57         ino->gid = 0;
58         ino->mode = mode;
59         ino->last_used = jiffies;
60
61         ino->sbi = sbi;
62
63         if (reinit && ino->free)
64                 (ino->free)(ino);
65
66         memset(&ino->u, 0, sizeof(ino->u));
67
68         ino->free = NULL;
69
70         if (S_ISLNK(mode))
71                 ino->free = ino_lnkfree;
72
73         return ino;
74 }
75
76 void autofs4_free_ino(struct autofs_info *ino)
77 {
78         if (ino->dentry) {
79                 ino->dentry->d_fsdata = NULL;
80                 ino->dentry = NULL;
81         }
82         if (ino->free)
83                 (ino->free)(ino);
84         kfree(ino);
85 }
86
87 void autofs4_kill_sb(struct super_block *sb)
88 {
89         struct autofs_sb_info *sbi = autofs4_sbi(sb);
90
91         /*
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
95          * deactivate_super.
96          */
97         if (!sbi)
98                 goto out_kill_sb;
99
100         /* Free wait queues, close pipe */
101         autofs4_catatonic_mode(sbi);
102
103         sb->s_fs_info = NULL;
104         kfree(sbi);
105
106 out_kill_sb:
107         DPRINTK("shutting down");
108         kill_litter_super(sb);
109 }
110
111 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
112 {
113         struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
114         struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
115
116         if (!sbi)
117                 return 0;
118
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);
128
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");
133         else
134                 seq_printf(m, ",indirect");
135
136         return 0;
137 }
138
139 static const struct super_operations autofs4_sops = {
140         .statfs         = simple_statfs,
141         .show_options   = autofs4_show_options,
142 };
143
144 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
145         Opt_indirect, Opt_direct, Opt_offset};
146
147 static const match_table_t tokens = {
148         {Opt_fd, "fd=%u"},
149         {Opt_uid, "uid=%u"},
150         {Opt_gid, "gid=%u"},
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"},
157         {Opt_err, NULL}
158 };
159
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)
162 {
163         char *p;
164         substring_t args[MAX_OPT_ARGS];
165         int option;
166
167         *uid = current_uid();
168         *gid = current_gid();
169         *pgrp = task_pgrp_nr(current);
170
171         *minproto = AUTOFS_MIN_PROTO_VERSION;
172         *maxproto = AUTOFS_MAX_PROTO_VERSION;
173
174         *pipefd = -1;
175
176         if (!options)
177                 return 1;
178
179         while ((p = strsep(&options, ",")) != NULL) {
180                 int token;
181                 if (!*p)
182                         continue;
183
184                 token = match_token(p, tokens, args);
185                 switch (token) {
186                 case Opt_fd:
187                         if (match_int(args, pipefd))
188                                 return 1;
189                         break;
190                 case Opt_uid:
191                         if (match_int(args, &option))
192                                 return 1;
193                         *uid = option;
194                         break;
195                 case Opt_gid:
196                         if (match_int(args, &option))
197                                 return 1;
198                         *gid = option;
199                         break;
200                 case Opt_pgrp:
201                         if (match_int(args, &option))
202                                 return 1;
203                         *pgrp = option;
204                         break;
205                 case Opt_minproto:
206                         if (match_int(args, &option))
207                                 return 1;
208                         *minproto = option;
209                         break;
210                 case Opt_maxproto:
211                         if (match_int(args, &option))
212                                 return 1;
213                         *maxproto = option;
214                         break;
215                 case Opt_indirect:
216                         set_autofs_type_indirect(type);
217                         break;
218                 case Opt_direct:
219                         set_autofs_type_direct(type);
220                         break;
221                 case Opt_offset:
222                         set_autofs_type_offset(type);
223                         break;
224                 default:
225                         return 1;
226                 }
227         }
228         return (*pipefd < 0);
229 }
230
231 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
232 {
233         struct autofs_info *ino;
234
235         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
236         if (!ino)
237                 return NULL;
238
239         return ino;
240 }
241
242 int autofs4_fill_super(struct super_block *s, void *data, int silent)
243 {
244         struct inode * root_inode;
245         struct dentry * root;
246         struct file * pipe;
247         int pipefd;
248         struct autofs_sb_info *sbi;
249         struct autofs_info *ino;
250
251         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
252         if (!sbi)
253                 goto fail_unlock;
254         DPRINTK("starting up, sbi = %p",sbi);
255
256         s->s_fs_info = sbi;
257         sbi->magic = AUTOFS_SBI_MAGIC;
258         sbi->pipefd = -1;
259         sbi->pipe = NULL;
260         sbi->catatonic = 1;
261         sbi->exp_timeout = 0;
262         sbi->oz_pgrp = task_pgrp_nr(current);
263         sbi->sb = s;
264         sbi->version = 0;
265         sbi->sub_version = 0;
266         set_autofs_type_indirect(&sbi->type);
267         sbi->min_proto = 0;
268         sbi->max_proto = 0;
269         mutex_init(&sbi->wq_mutex);
270         spin_lock_init(&sbi->fs_lock);
271         sbi->queues = NULL;
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;
280         s->s_time_gran = 1;
281
282         /*
283          * Get the root inode and dentry, but defer checking for errors.
284          */
285         ino = autofs4_mkroot(sbi);
286         if (!ino)
287                 goto fail_free;
288         root_inode = autofs4_get_inode(s, ino);
289         if (!root_inode)
290                 goto fail_ino;
291
292         root = d_alloc_root(root_inode);
293         if (!root)
294                 goto fail_iput;
295         pipe = NULL;
296
297         root->d_fsdata = ino;
298
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,
302                                 &sbi->max_proto)) {
303                 printk("autofs: called with bogus options\n");
304                 goto fail_dput;
305         }
306
307         if (autofs_type_trigger(sbi->type))
308                 __managed_dentry_set_managed(root);
309
310         root_inode->i_fop = &autofs4_root_operations;
311         root_inode->i_op = &autofs4_dir_inode_operations;
312
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);
320                 goto fail_dput;
321         }
322
323         /* Establish highest kernel protocol version */
324         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
325                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
326         else
327                 sbi->version = sbi->max_proto;
328         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
329
330         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
331         pipe = fget(pipefd);
332         
333         if (!pipe) {
334                 printk("autofs: could not open pipe file descriptor\n");
335                 goto fail_dput;
336         }
337         if (!pipe->f_op || !pipe->f_op->write)
338                 goto fail_fput;
339         sbi->pipe = pipe;
340         sbi->pipefd = pipefd;
341         sbi->catatonic = 0;
342
343         /*
344          * Success! Install the root dentry now to indicate completion.
345          */
346         s->s_root = root;
347         return 0;
348         
349         /*
350          * Failure ... clean up.
351          */
352 fail_fput:
353         printk("autofs: pipe file descriptor does not contain proper ops\n");
354         fput(pipe);
355         /* fall through */
356 fail_dput:
357         dput(root);
358         goto fail_free;
359 fail_iput:
360         printk("autofs: get root dentry failed\n");
361         iput(root_inode);
362 fail_ino:
363         kfree(ino);
364 fail_free:
365         kfree(sbi);
366         s->s_fs_info = NULL;
367 fail_unlock:
368         return -EINVAL;
369 }
370
371 struct inode *autofs4_get_inode(struct super_block *sb,
372                                 struct autofs_info *inf)
373 {
374         struct inode *inode = new_inode(sb);
375
376         if (inode == NULL)
377                 return NULL;
378
379         inode->i_mode = inf->mode;
380         if (sb->s_root) {
381                 inode->i_uid = sb->s_root->d_inode->i_uid;
382                 inode->i_gid = sb->s_root->d_inode->i_gid;
383         }
384         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
385         inode->i_ino = get_next_ino();
386
387         if (S_ISDIR(inf->mode)) {
388                 inode->i_nlink = 2;
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;
394         }
395
396         return inode;
397 }