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