fix mismerge in ll_rw_blk.c
[linux-2.6] / fs / autofs4 / inode.c
1 /* -*- 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/slab.h>
15 #include <linux/file.h>
16 #include <linux/pagemap.h>
17 #include <linux/parser.h>
18 #include <linux/bitops.h>
19 #include <linux/smp_lock.h>
20 #include "autofs_i.h"
21 #include <linux/module.h>
22
23 static void ino_lnkfree(struct autofs_info *ino)
24 {
25         if (ino->u.symlink) {
26                 kfree(ino->u.symlink);
27                 ino->u.symlink = NULL;
28         }
29 }
30
31 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
32                                      struct autofs_sb_info *sbi, mode_t mode)
33 {
34         int reinit = 1;
35
36         if (ino == NULL) {
37                 reinit = 0;
38                 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
39         }
40
41         if (ino == NULL)
42                 return NULL;
43
44         ino->flags = 0;
45         ino->mode = mode;
46         ino->inode = NULL;
47         ino->dentry = NULL;
48         ino->size = 0;
49
50         ino->last_used = jiffies;
51
52         ino->sbi = sbi;
53
54         if (reinit && ino->free)
55                 (ino->free)(ino);
56
57         memset(&ino->u, 0, sizeof(ino->u));
58
59         ino->free = NULL;
60
61         if (S_ISLNK(mode))
62                 ino->free = ino_lnkfree;
63
64         return ino;
65 }
66
67 void autofs4_free_ino(struct autofs_info *ino)
68 {
69         if (ino->dentry) {
70                 ino->dentry->d_fsdata = NULL;
71                 if (ino->dentry->d_inode)
72                         dput(ino->dentry);
73                 ino->dentry = NULL;
74         }
75         if (ino->free)
76                 (ino->free)(ino);
77         kfree(ino);
78 }
79
80 /*
81  * Deal with the infamous "Busy inodes after umount ..." message.
82  *
83  * Clean up the dentry tree. This happens with autofs if the user
84  * space program goes away due to a SIGKILL, SIGSEGV etc.
85  */
86 static void autofs4_force_release(struct autofs_sb_info *sbi)
87 {
88         struct dentry *this_parent = sbi->root;
89         struct list_head *next;
90
91         spin_lock(&dcache_lock);
92 repeat:
93         next = this_parent->d_subdirs.next;
94 resume:
95         while (next != &this_parent->d_subdirs) {
96                 struct dentry *dentry = list_entry(next, struct dentry, d_child);
97
98                 /* Negative dentry - don`t care */
99                 if (!simple_positive(dentry)) {
100                         next = next->next;
101                         continue;
102                 }
103
104                 if (!list_empty(&dentry->d_subdirs)) {
105                         this_parent = dentry;
106                         goto repeat;
107                 }
108
109                 next = next->next;
110                 spin_unlock(&dcache_lock);
111
112                 DPRINTK("dentry %p %.*s",
113                         dentry, (int)dentry->d_name.len, dentry->d_name.name);
114
115                 dput(dentry);
116                 spin_lock(&dcache_lock);
117         }
118
119         if (this_parent != sbi->root) {
120                 struct dentry *dentry = this_parent;
121
122                 next = this_parent->d_child.next;
123                 this_parent = this_parent->d_parent;
124                 spin_unlock(&dcache_lock);
125                 DPRINTK("parent dentry %p %.*s",
126                         dentry, (int)dentry->d_name.len, dentry->d_name.name);
127                 dput(dentry);
128                 spin_lock(&dcache_lock);
129                 goto resume;
130         }
131         spin_unlock(&dcache_lock);
132
133         dput(sbi->root);
134         sbi->root = NULL;
135         shrink_dcache_sb(sbi->sb);
136
137         return;
138 }
139
140 static void autofs4_put_super(struct super_block *sb)
141 {
142         struct autofs_sb_info *sbi = autofs4_sbi(sb);
143
144         sb->s_fs_info = NULL;
145
146         if ( !sbi->catatonic )
147                 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
148
149         /* Clean up and release dangling references */
150         if (sbi)
151                 autofs4_force_release(sbi);
152
153         kfree(sbi);
154
155         DPRINTK("shutting down");
156 }
157
158 static struct super_operations autofs4_sops = {
159         .put_super      = autofs4_put_super,
160         .statfs         = simple_statfs,
161 };
162
163 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
164
165 static match_table_t tokens = {
166         {Opt_fd, "fd=%u"},
167         {Opt_uid, "uid=%u"},
168         {Opt_gid, "gid=%u"},
169         {Opt_pgrp, "pgrp=%u"},
170         {Opt_minproto, "minproto=%u"},
171         {Opt_maxproto, "maxproto=%u"},
172         {Opt_err, NULL}
173 };
174
175 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
176                          pid_t *pgrp, int *minproto, int *maxproto)
177 {
178         char *p;
179         substring_t args[MAX_OPT_ARGS];
180         int option;
181
182         *uid = current->uid;
183         *gid = current->gid;
184         *pgrp = process_group(current);
185
186         *minproto = AUTOFS_MIN_PROTO_VERSION;
187         *maxproto = AUTOFS_MAX_PROTO_VERSION;
188
189         *pipefd = -1;
190
191         if (!options)
192                 return 1;
193
194         while ((p = strsep(&options, ",")) != NULL) {
195                 int token;
196                 if (!*p)
197                         continue;
198
199                 token = match_token(p, tokens, args);
200                 switch (token) {
201                 case Opt_fd:
202                         if (match_int(args, pipefd))
203                                 return 1;
204                         break;
205                 case Opt_uid:
206                         if (match_int(args, &option))
207                                 return 1;
208                         *uid = option;
209                         break;
210                 case Opt_gid:
211                         if (match_int(args, &option))
212                                 return 1;
213                         *gid = option;
214                         break;
215                 case Opt_pgrp:
216                         if (match_int(args, &option))
217                                 return 1;
218                         *pgrp = option;
219                         break;
220                 case Opt_minproto:
221                         if (match_int(args, &option))
222                                 return 1;
223                         *minproto = option;
224                         break;
225                 case Opt_maxproto:
226                         if (match_int(args, &option))
227                                 return 1;
228                         *maxproto = option;
229                         break;
230                 default:
231                         return 1;
232                 }
233         }
234         return (*pipefd < 0);
235 }
236
237 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
238 {
239         struct autofs_info *ino;
240
241         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
242         if (!ino)
243                 return NULL;
244
245         return ino;
246 }
247
248 int autofs4_fill_super(struct super_block *s, void *data, int silent)
249 {
250         struct inode * root_inode;
251         struct dentry * root;
252         struct file * pipe;
253         int pipefd;
254         struct autofs_sb_info *sbi;
255         struct autofs_info *ino;
256         int minproto, maxproto;
257
258         sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
259         if ( !sbi )
260                 goto fail_unlock;
261         DPRINTK("starting up, sbi = %p",sbi);
262
263         memset(sbi, 0, sizeof(*sbi));
264
265         s->s_fs_info = sbi;
266         sbi->magic = AUTOFS_SBI_MAGIC;
267         sbi->root = NULL;
268         sbi->catatonic = 0;
269         sbi->exp_timeout = 0;
270         sbi->oz_pgrp = process_group(current);
271         sbi->sb = s;
272         sbi->version = 0;
273         sbi->sub_version = 0;
274         init_MUTEX(&sbi->wq_sem);
275         spin_lock_init(&sbi->fs_lock);
276         sbi->queues = NULL;
277         s->s_blocksize = 1024;
278         s->s_blocksize_bits = 10;
279         s->s_magic = AUTOFS_SUPER_MAGIC;
280         s->s_op = &autofs4_sops;
281         s->s_time_gran = 1;
282
283         /*
284          * Get the root inode and dentry, but defer checking for errors.
285          */
286         ino = autofs4_mkroot(sbi);
287         if (!ino)
288                 goto fail_free;
289         root_inode = autofs4_get_inode(s, ino);
290         kfree(ino);
291         if (!root_inode)
292                 goto fail_free;
293
294         root_inode->i_op = &autofs4_root_inode_operations;
295         root_inode->i_fop = &autofs4_root_operations;
296         root = d_alloc_root(root_inode);
297         pipe = NULL;
298
299         if (!root)
300                 goto fail_iput;
301
302         /* Can this call block? */
303         if (parse_options(data, &pipefd,
304                           &root_inode->i_uid, &root_inode->i_gid,
305                           &sbi->oz_pgrp,
306                           &minproto, &maxproto)) {
307                 printk("autofs: called with bogus options\n");
308                 goto fail_dput;
309         }
310
311         /* Couldn't this be tested earlier? */
312         if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
313             minproto > AUTOFS_MAX_PROTO_VERSION) {
314                 printk("autofs: kernel does not match daemon version "
315                        "daemon (%d, %d) kernel (%d, %d)\n",
316                         minproto, maxproto,
317                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
318                 goto fail_dput;
319         }
320
321         sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
322         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
323
324         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
325         pipe = fget(pipefd);
326         
327         if ( !pipe ) {
328                 printk("autofs: could not open pipe file descriptor\n");
329                 goto fail_dput;
330         }
331         if ( !pipe->f_op || !pipe->f_op->write )
332                 goto fail_fput;
333         sbi->pipe = pipe;
334
335         /*
336          * Take a reference to the root dentry so we get a chance to
337          * clean up the dentry tree on umount.
338          * See autofs4_force_release.
339          */
340         sbi->root = dget(root);
341
342         /*
343          * Success! Install the root dentry now to indicate completion.
344          */
345         s->s_root = root;
346         return 0;
347         
348         /*
349          * Failure ... clean up.
350          */
351 fail_fput:
352         printk("autofs: pipe file descriptor does not contain proper ops\n");
353         fput(pipe);
354         /* fall through */
355 fail_dput:
356         dput(root);
357         goto fail_free;
358 fail_iput:
359         printk("autofs: get root dentry failed\n");
360         iput(root_inode);
361 fail_free:
362         kfree(sbi);
363 fail_unlock:
364         return -EINVAL;
365 }
366
367 struct inode *autofs4_get_inode(struct super_block *sb,
368                                 struct autofs_info *inf)
369 {
370         struct inode *inode = new_inode(sb);
371
372         if (inode == NULL)
373                 return NULL;
374
375         inf->inode = inode;
376         inode->i_mode = inf->mode;
377         if (sb->s_root) {
378                 inode->i_uid = sb->s_root->d_inode->i_uid;
379                 inode->i_gid = sb->s_root->d_inode->i_gid;
380         } else {
381                 inode->i_uid = 0;
382                 inode->i_gid = 0;
383         }
384         inode->i_blksize = PAGE_CACHE_SIZE;
385         inode->i_blocks = 0;
386         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
387
388         if (S_ISDIR(inf->mode)) {
389                 inode->i_nlink = 2;
390                 inode->i_op = &autofs4_dir_inode_operations;
391                 inode->i_fop = &autofs4_dir_operations;
392         } else if (S_ISLNK(inf->mode)) {
393                 inode->i_size = inf->size;
394                 inode->i_op = &autofs4_symlink_inode_operations;
395         }
396
397         return inode;
398 }