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