]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/autofs/inode.c
[PATCH] autofs4: panic after mount fail
[linux-2.6-omap-h63xx.git] / fs / autofs / inode.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2  *
3  * linux/fs/autofs/inode.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *
7  * This file is part of the Linux kernel and is made available under
8  * the terms of the GNU General Public License, version 2, or at your
9  * option, any later version, incorporated herein by reference.
10  *
11  * ------------------------------------------------------------------------- */
12
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/slab.h>
16 #include <linux/file.h>
17 #include <linux/parser.h>
18 #include <linux/bitops.h>
19 #include <linux/magic.h>
20 #include "autofs_i.h"
21 #include <linux/module.h>
22
23 void autofs_kill_sb(struct super_block *sb)
24 {
25         struct autofs_sb_info *sbi = autofs_sbi(sb);
26         unsigned int n;
27
28         /*
29          * In the event of a failure in get_sb_nodev the superblock
30          * info is not present so nothing else has been setup, so
31          * just exit when we are called from deactivate_super.
32          */
33         if (!sbi)
34                 return;
35
36         if ( !sbi->catatonic )
37                 autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
38
39         autofs_hash_nuke(sbi);
40         for ( n = 0 ; n < AUTOFS_MAX_SYMLINKS ; n++ ) {
41                 if ( test_bit(n, sbi->symlink_bitmap) )
42                         kfree(sbi->symlink[n].data);
43         }
44
45         kfree(sb->s_fs_info);
46
47         DPRINTK(("autofs: shutting down\n"));
48         kill_anon_super(sb);
49 }
50
51 static void autofs_read_inode(struct inode *inode);
52
53 static struct super_operations autofs_sops = {
54         .read_inode     = autofs_read_inode,
55         .statfs         = simple_statfs,
56 };
57
58 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
59
60 static match_table_t autofs_tokens = {
61         {Opt_fd, "fd=%u"},
62         {Opt_uid, "uid=%u"},
63         {Opt_gid, "gid=%u"},
64         {Opt_pgrp, "pgrp=%u"},
65         {Opt_minproto, "minproto=%u"},
66         {Opt_maxproto, "maxproto=%u"},
67         {Opt_err, NULL}
68 };
69
70 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, pid_t *pgrp, int *minproto, int *maxproto)
71 {
72         char *p;
73         substring_t args[MAX_OPT_ARGS];
74         int option;
75
76         *uid = current->uid;
77         *gid = current->gid;
78         *pgrp = process_group(current);
79
80         *minproto = *maxproto = AUTOFS_PROTO_VERSION;
81
82         *pipefd = -1;
83
84         if (!options)
85                 return 1;
86
87         while ((p = strsep(&options, ",")) != NULL) {
88                 int token;
89                 if (!*p)
90                         continue;
91
92                 token = match_token(p, autofs_tokens, args);
93                 switch (token) {
94                 case Opt_fd:
95                         if (match_int(&args[0], &option))
96                                 return 1;
97                         *pipefd = option;
98                         break;
99                 case Opt_uid:
100                         if (match_int(&args[0], &option))
101                                 return 1;
102                         *uid = option;
103                         break;
104                 case Opt_gid:
105                         if (match_int(&args[0], &option))
106                                 return 1;
107                         *gid = option;
108                         break;
109                 case Opt_pgrp:
110                         if (match_int(&args[0], &option))
111                                 return 1;
112                         *pgrp = option;
113                         break;
114                 case Opt_minproto:
115                         if (match_int(&args[0], &option))
116                                 return 1;
117                         *minproto = option;
118                         break;
119                 case Opt_maxproto:
120                         if (match_int(&args[0], &option))
121                                 return 1;
122                         *maxproto = option;
123                         break;
124                 default:
125                         return 1;
126                 }
127         }
128         return (*pipefd < 0);
129 }
130
131 int autofs_fill_super(struct super_block *s, void *data, int silent)
132 {
133         struct inode * root_inode;
134         struct dentry * root;
135         struct file * pipe;
136         int pipefd;
137         struct autofs_sb_info *sbi;
138         int minproto, maxproto;
139
140         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
141         if ( !sbi )
142                 goto fail_unlock;
143         DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
144
145         s->s_fs_info = sbi;
146         sbi->magic = AUTOFS_SBI_MAGIC;
147         sbi->pipe = NULL;
148         sbi->catatonic = 1;
149         sbi->exp_timeout = 0;
150         sbi->oz_pgrp = process_group(current);
151         autofs_initialize_hash(&sbi->dirhash);
152         sbi->queues = NULL;
153         memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
154         sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
155         s->s_blocksize = 1024;
156         s->s_blocksize_bits = 10;
157         s->s_magic = AUTOFS_SUPER_MAGIC;
158         s->s_op = &autofs_sops;
159         s->s_time_gran = 1;
160         sbi->sb = s;
161
162         root_inode = iget(s, AUTOFS_ROOT_INO);
163         root = d_alloc_root(root_inode);
164         pipe = NULL;
165
166         if (!root)
167                 goto fail_iput;
168
169         /* Can this call block?  - WTF cares? s is locked. */
170         if ( parse_options(data,&pipefd,&root_inode->i_uid,&root_inode->i_gid,&sbi->oz_pgrp,&minproto,&maxproto) ) {
171                 printk("autofs: called with bogus options\n");
172                 goto fail_dput;
173         }
174
175         /* Couldn't this be tested earlier? */
176         if ( minproto > AUTOFS_PROTO_VERSION || 
177              maxproto < AUTOFS_PROTO_VERSION ) {
178                 printk("autofs: kernel does not match daemon version\n");
179                 goto fail_dput;
180         }
181
182         DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp));
183         pipe = fget(pipefd);
184         
185         if ( !pipe ) {
186                 printk("autofs: could not open pipe file descriptor\n");
187                 goto fail_dput;
188         }
189         if ( !pipe->f_op || !pipe->f_op->write )
190                 goto fail_fput;
191         sbi->pipe = pipe;
192         sbi->catatonic = 0;
193
194         /*
195          * Success! Install the root dentry now to indicate completion.
196          */
197         s->s_root = root;
198         return 0;
199
200 fail_fput:
201         printk("autofs: pipe file descriptor does not contain proper ops\n");
202         fput(pipe);
203 fail_dput:
204         dput(root);
205         goto fail_free;
206 fail_iput:
207         printk("autofs: get root dentry failed\n");
208         iput(root_inode);
209 fail_free:
210         kfree(sbi);
211         s->s_fs_info = NULL;
212         kill_anon_super(s);
213 fail_unlock:
214         return -EINVAL;
215 }
216
217 static void autofs_read_inode(struct inode *inode)
218 {
219         ino_t ino = inode->i_ino;
220         unsigned int n;
221         struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
222
223         /* Initialize to the default case (stub directory) */
224
225         inode->i_op = &simple_dir_inode_operations;
226         inode->i_fop = &simple_dir_operations;
227         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
228         inode->i_nlink = 2;
229         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
230         inode->i_blocks = 0;
231
232         if ( ino == AUTOFS_ROOT_INO ) {
233                 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
234                 inode->i_op = &autofs_root_inode_operations;
235                 inode->i_fop = &autofs_root_operations;
236                 inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
237                 return;
238         } 
239         
240         inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
241         inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
242         
243         if ( ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO ) {
244                 /* Symlink inode - should be in symlink list */
245                 struct autofs_symlink *sl;
246
247                 n = ino - AUTOFS_FIRST_SYMLINK;
248                 if ( n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
249                         printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
250                         return;
251                 }
252                 
253                 inode->i_op = &autofs_symlink_inode_operations;
254                 sl = &sbi->symlink[n];
255                 inode->i_private = sl;
256                 inode->i_mode = S_IFLNK | S_IRWXUGO;
257                 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
258                 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
259                 inode->i_size = sl->len;
260                 inode->i_nlink = 1;
261         }
262 }