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