Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.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/smp_lock.h>
22 #include "autofs_i.h"
23 #include <linux/module.h>
24
25 static void ino_lnkfree(struct autofs_info *ino)
26 {
27         kfree(ino->u.symlink);
28         ino->u.symlink = NULL;
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         atomic_set(&ino->count, 0);
52
53         ino->sbi = sbi;
54
55         if (reinit && ino->free)
56                 (ino->free)(ino);
57
58         memset(&ino->u, 0, sizeof(ino->u));
59
60         ino->free = NULL;
61
62         if (S_ISLNK(mode))
63                 ino->free = ino_lnkfree;
64
65         return ino;
66 }
67
68 void autofs4_free_ino(struct autofs_info *ino)
69 {
70         struct autofs_info *p_ino;
71
72         if (ino->dentry) {
73                 ino->dentry->d_fsdata = NULL;
74                 if (ino->dentry->d_inode) {
75                         struct dentry *parent = ino->dentry->d_parent;
76                         if (atomic_dec_and_test(&ino->count)) {
77                                 p_ino = autofs4_dentry_ino(parent);
78                                 if (p_ino && parent != ino->dentry)
79                                         atomic_dec(&p_ino->count);
80                         }
81                         dput(ino->dentry);
82                 }
83                 ino->dentry = NULL;
84         }
85         if (ino->free)
86                 (ino->free)(ino);
87         kfree(ino);
88 }
89
90 /*
91  * Deal with the infamous "Busy inodes after umount ..." message.
92  *
93  * Clean up the dentry tree. This happens with autofs if the user
94  * space program goes away due to a SIGKILL, SIGSEGV etc.
95  */
96 static void autofs4_force_release(struct autofs_sb_info *sbi)
97 {
98         struct dentry *this_parent = sbi->sb->s_root;
99         struct list_head *next;
100
101         spin_lock(&dcache_lock);
102 repeat:
103         next = this_parent->d_subdirs.next;
104 resume:
105         while (next != &this_parent->d_subdirs) {
106                 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
107
108                 /* Negative dentry - don`t care */
109                 if (!simple_positive(dentry)) {
110                         next = next->next;
111                         continue;
112                 }
113
114                 if (!list_empty(&dentry->d_subdirs)) {
115                         this_parent = dentry;
116                         goto repeat;
117                 }
118
119                 next = next->next;
120                 spin_unlock(&dcache_lock);
121
122                 DPRINTK("dentry %p %.*s",
123                         dentry, (int)dentry->d_name.len, dentry->d_name.name);
124
125                 dput(dentry);
126                 spin_lock(&dcache_lock);
127         }
128
129         if (this_parent != sbi->sb->s_root) {
130                 struct dentry *dentry = this_parent;
131
132                 next = this_parent->d_u.d_child.next;
133                 this_parent = this_parent->d_parent;
134                 spin_unlock(&dcache_lock);
135                 DPRINTK("parent dentry %p %.*s",
136                         dentry, (int)dentry->d_name.len, dentry->d_name.name);
137                 dput(dentry);
138                 spin_lock(&dcache_lock);
139                 goto resume;
140         }
141         spin_unlock(&dcache_lock);
142 }
143
144 void autofs4_kill_sb(struct super_block *sb)
145 {
146         struct autofs_sb_info *sbi = autofs4_sbi(sb);
147
148         sb->s_fs_info = NULL;
149
150         if ( !sbi->catatonic )
151                 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
152
153         /* Clean up and release dangling references */
154         autofs4_force_release(sbi);
155
156         kfree(sbi);
157
158         DPRINTK("shutting down");
159         kill_anon_super(sb);
160 }
161
162 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
163 {
164         struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
165
166         if (!sbi)
167                 return 0;
168
169         seq_printf(m, ",fd=%d", sbi->pipefd);
170         seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
171         seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
172         seq_printf(m, ",minproto=%d", sbi->min_proto);
173         seq_printf(m, ",maxproto=%d", sbi->max_proto);
174
175         if (sbi->type & AUTOFS_TYPE_OFFSET)
176                 seq_printf(m, ",offset");
177         else if (sbi->type & AUTOFS_TYPE_DIRECT)
178                 seq_printf(m, ",direct");
179         else
180                 seq_printf(m, ",indirect");
181
182         return 0;
183 }
184
185 static struct super_operations autofs4_sops = {
186         .statfs         = simple_statfs,
187         .show_options   = autofs4_show_options,
188 };
189
190 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
191         Opt_indirect, Opt_direct, Opt_offset};
192
193 static match_table_t tokens = {
194         {Opt_fd, "fd=%u"},
195         {Opt_uid, "uid=%u"},
196         {Opt_gid, "gid=%u"},
197         {Opt_pgrp, "pgrp=%u"},
198         {Opt_minproto, "minproto=%u"},
199         {Opt_maxproto, "maxproto=%u"},
200         {Opt_indirect, "indirect"},
201         {Opt_direct, "direct"},
202         {Opt_offset, "offset"},
203         {Opt_err, NULL}
204 };
205
206 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
207                          pid_t *pgrp, unsigned int *type,
208                          int *minproto, int *maxproto)
209 {
210         char *p;
211         substring_t args[MAX_OPT_ARGS];
212         int option;
213
214         *uid = current->uid;
215         *gid = current->gid;
216         *pgrp = process_group(current);
217
218         *minproto = AUTOFS_MIN_PROTO_VERSION;
219         *maxproto = AUTOFS_MAX_PROTO_VERSION;
220
221         *pipefd = -1;
222
223         if (!options)
224                 return 1;
225
226         while ((p = strsep(&options, ",")) != NULL) {
227                 int token;
228                 if (!*p)
229                         continue;
230
231                 token = match_token(p, tokens, args);
232                 switch (token) {
233                 case Opt_fd:
234                         if (match_int(args, pipefd))
235                                 return 1;
236                         break;
237                 case Opt_uid:
238                         if (match_int(args, &option))
239                                 return 1;
240                         *uid = option;
241                         break;
242                 case Opt_gid:
243                         if (match_int(args, &option))
244                                 return 1;
245                         *gid = option;
246                         break;
247                 case Opt_pgrp:
248                         if (match_int(args, &option))
249                                 return 1;
250                         *pgrp = option;
251                         break;
252                 case Opt_minproto:
253                         if (match_int(args, &option))
254                                 return 1;
255                         *minproto = option;
256                         break;
257                 case Opt_maxproto:
258                         if (match_int(args, &option))
259                                 return 1;
260                         *maxproto = option;
261                         break;
262                 case Opt_indirect:
263                         *type = AUTOFS_TYPE_INDIRECT;
264                         break;
265                 case Opt_direct:
266                         *type = AUTOFS_TYPE_DIRECT;
267                         break;
268                 case Opt_offset:
269                         *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
270                         break;
271                 default:
272                         return 1;
273                 }
274         }
275         return (*pipefd < 0);
276 }
277
278 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
279 {
280         struct autofs_info *ino;
281
282         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
283         if (!ino)
284                 return NULL;
285
286         return ino;
287 }
288
289 static struct dentry_operations autofs4_sb_dentry_operations = {
290         .d_release      = autofs4_dentry_release,
291 };
292
293 int autofs4_fill_super(struct super_block *s, void *data, int silent)
294 {
295         struct inode * root_inode;
296         struct dentry * root;
297         struct file * pipe;
298         int pipefd;
299         struct autofs_sb_info *sbi;
300         struct autofs_info *ino;
301
302         sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
303         if ( !sbi )
304                 goto fail_unlock;
305         DPRINTK("starting up, sbi = %p",sbi);
306
307         memset(sbi, 0, sizeof(*sbi));
308
309         s->s_fs_info = sbi;
310         sbi->magic = AUTOFS_SBI_MAGIC;
311         sbi->pipefd = -1;
312         sbi->catatonic = 0;
313         sbi->exp_timeout = 0;
314         sbi->oz_pgrp = process_group(current);
315         sbi->sb = s;
316         sbi->version = 0;
317         sbi->sub_version = 0;
318         sbi->type = 0;
319         sbi->min_proto = 0;
320         sbi->max_proto = 0;
321         mutex_init(&sbi->wq_mutex);
322         spin_lock_init(&sbi->fs_lock);
323         sbi->queues = NULL;
324         s->s_blocksize = 1024;
325         s->s_blocksize_bits = 10;
326         s->s_magic = AUTOFS_SUPER_MAGIC;
327         s->s_op = &autofs4_sops;
328         s->s_time_gran = 1;
329
330         /*
331          * Get the root inode and dentry, but defer checking for errors.
332          */
333         ino = autofs4_mkroot(sbi);
334         if (!ino)
335                 goto fail_free;
336         root_inode = autofs4_get_inode(s, ino);
337         if (!root_inode)
338                 goto fail_ino;
339
340         root = d_alloc_root(root_inode);
341         if (!root)
342                 goto fail_iput;
343         pipe = NULL;
344
345         root->d_op = &autofs4_sb_dentry_operations;
346         root->d_fsdata = ino;
347
348         /* Can this call block? */
349         if (parse_options(data, &pipefd,
350                           &root_inode->i_uid, &root_inode->i_gid,
351                           &sbi->oz_pgrp, &sbi->type,
352                           &sbi->min_proto, &sbi->max_proto)) {
353                 printk("autofs: called with bogus options\n");
354                 goto fail_dput;
355         }
356
357         root_inode->i_fop = &autofs4_root_operations;
358         root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
359                         &autofs4_direct_root_inode_operations :
360                         &autofs4_indirect_root_inode_operations;
361
362         /* Couldn't this be tested earlier? */
363         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
364             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
365                 printk("autofs: kernel does not match daemon version "
366                        "daemon (%d, %d) kernel (%d, %d)\n",
367                         sbi->min_proto, sbi->max_proto,
368                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
369                 goto fail_dput;
370         }
371
372         /* Establish highest kernel protocol version */
373         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
374                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
375         else
376                 sbi->version = sbi->max_proto;
377         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
378
379         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
380         pipe = fget(pipefd);
381         
382         if ( !pipe ) {
383                 printk("autofs: could not open pipe file descriptor\n");
384                 goto fail_dput;
385         }
386         if ( !pipe->f_op || !pipe->f_op->write )
387                 goto fail_fput;
388         sbi->pipe = pipe;
389         sbi->pipefd = pipefd;
390
391         /*
392          * Success! Install the root dentry now to indicate completion.
393          */
394         s->s_root = root;
395         return 0;
396         
397         /*
398          * Failure ... clean up.
399          */
400 fail_fput:
401         printk("autofs: pipe file descriptor does not contain proper ops\n");
402         fput(pipe);
403         /* fall through */
404 fail_dput:
405         dput(root);
406         goto fail_free;
407 fail_iput:
408         printk("autofs: get root dentry failed\n");
409         iput(root_inode);
410 fail_ino:
411         kfree(ino);
412 fail_free:
413         kfree(sbi);
414 fail_unlock:
415         return -EINVAL;
416 }
417
418 struct inode *autofs4_get_inode(struct super_block *sb,
419                                 struct autofs_info *inf)
420 {
421         struct inode *inode = new_inode(sb);
422
423         if (inode == NULL)
424                 return NULL;
425
426         inf->inode = inode;
427         inode->i_mode = inf->mode;
428         if (sb->s_root) {
429                 inode->i_uid = sb->s_root->d_inode->i_uid;
430                 inode->i_gid = sb->s_root->d_inode->i_gid;
431         } else {
432                 inode->i_uid = 0;
433                 inode->i_gid = 0;
434         }
435         inode->i_blocks = 0;
436         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
437
438         if (S_ISDIR(inf->mode)) {
439                 inode->i_nlink = 2;
440                 inode->i_op = &autofs4_dir_inode_operations;
441                 inode->i_fop = &autofs4_dir_operations;
442         } else if (S_ISLNK(inf->mode)) {
443                 inode->i_size = inf->size;
444                 inode->i_op = &autofs4_symlink_inode_operations;
445         }
446
447         return inode;
448 }