]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/autofs4/inode.c
cf8abc793d5051c4e8f0db5a3db2613c02b18377
[mv-sheeva.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 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
219 {
220         struct autofs_info *ino;
221
222         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
223         if (!ino)
224                 return NULL;
225
226         return ino;
227 }
228
229 int autofs4_fill_super(struct super_block *s, void *data, int silent)
230 {
231         struct inode * root_inode;
232         struct dentry * root;
233         struct file * pipe;
234         int pipefd;
235         struct autofs_sb_info *sbi;
236         struct autofs_info *ino;
237
238         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
239         if (!sbi)
240                 goto fail_unlock;
241         DPRINTK("starting up, sbi = %p",sbi);
242
243         s->s_fs_info = sbi;
244         sbi->magic = AUTOFS_SBI_MAGIC;
245         sbi->pipefd = -1;
246         sbi->pipe = NULL;
247         sbi->catatonic = 1;
248         sbi->exp_timeout = 0;
249         sbi->oz_pgrp = task_pgrp_nr(current);
250         sbi->sb = s;
251         sbi->version = 0;
252         sbi->sub_version = 0;
253         set_autofs_type_indirect(&sbi->type);
254         sbi->min_proto = 0;
255         sbi->max_proto = 0;
256         mutex_init(&sbi->wq_mutex);
257         spin_lock_init(&sbi->fs_lock);
258         sbi->queues = NULL;
259         spin_lock_init(&sbi->lookup_lock);
260         INIT_LIST_HEAD(&sbi->active_list);
261         INIT_LIST_HEAD(&sbi->expiring_list);
262         s->s_blocksize = 1024;
263         s->s_blocksize_bits = 10;
264         s->s_magic = AUTOFS_SUPER_MAGIC;
265         s->s_op = &autofs4_sops;
266         s->s_d_op = &autofs4_dentry_operations;
267         s->s_time_gran = 1;
268
269         /*
270          * Get the root inode and dentry, but defer checking for errors.
271          */
272         ino = autofs4_mkroot(sbi);
273         if (!ino)
274                 goto fail_free;
275         root_inode = autofs4_get_inode(s, ino);
276         if (!root_inode)
277                 goto fail_ino;
278
279         root = d_alloc_root(root_inode);
280         if (!root)
281                 goto fail_iput;
282         pipe = NULL;
283
284         root->d_fsdata = ino;
285
286         /* Can this call block? */
287         if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
288                                 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
289                                 &sbi->max_proto)) {
290                 printk("autofs: called with bogus options\n");
291                 goto fail_dput;
292         }
293
294         if (autofs_type_trigger(sbi->type))
295                 __managed_dentry_set_managed(root);
296
297         root_inode->i_fop = &autofs4_root_operations;
298         root_inode->i_op = &autofs4_dir_inode_operations;
299
300         /* Couldn't this be tested earlier? */
301         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
302             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
303                 printk("autofs: kernel does not match daemon version "
304                        "daemon (%d, %d) kernel (%d, %d)\n",
305                         sbi->min_proto, sbi->max_proto,
306                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
307                 goto fail_dput;
308         }
309
310         /* Establish highest kernel protocol version */
311         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
312                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
313         else
314                 sbi->version = sbi->max_proto;
315         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
316
317         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
318         pipe = fget(pipefd);
319         
320         if (!pipe) {
321                 printk("autofs: could not open pipe file descriptor\n");
322                 goto fail_dput;
323         }
324         if (!pipe->f_op || !pipe->f_op->write)
325                 goto fail_fput;
326         sbi->pipe = pipe;
327         sbi->pipefd = pipefd;
328         sbi->catatonic = 0;
329
330         /*
331          * Success! Install the root dentry now to indicate completion.
332          */
333         s->s_root = root;
334         return 0;
335         
336         /*
337          * Failure ... clean up.
338          */
339 fail_fput:
340         printk("autofs: pipe file descriptor does not contain proper ops\n");
341         fput(pipe);
342         /* fall through */
343 fail_dput:
344         dput(root);
345         goto fail_free;
346 fail_iput:
347         printk("autofs: get root dentry failed\n");
348         iput(root_inode);
349 fail_ino:
350         kfree(ino);
351 fail_free:
352         kfree(sbi);
353         s->s_fs_info = NULL;
354 fail_unlock:
355         return -EINVAL;
356 }
357
358 struct inode *autofs4_get_inode(struct super_block *sb,
359                                 struct autofs_info *inf)
360 {
361         struct inode *inode = new_inode(sb);
362
363         if (inode == NULL)
364                 return NULL;
365
366         inode->i_mode = inf->mode;
367         if (sb->s_root) {
368                 inode->i_uid = sb->s_root->d_inode->i_uid;
369                 inode->i_gid = sb->s_root->d_inode->i_gid;
370         }
371         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
372         inode->i_ino = get_next_ino();
373
374         if (S_ISDIR(inf->mode)) {
375                 inode->i_nlink = 2;
376                 inode->i_op = &autofs4_dir_inode_operations;
377                 inode->i_fop = &autofs4_dir_operations;
378         } else if (S_ISLNK(inf->mode)) {
379                 inode->i_size = inf->size;
380                 inode->i_op = &autofs4_symlink_inode_operations;
381         }
382
383         return inode;
384 }