[PATCH] autofs4: change AUTOFS_TYP_* AUTOFS_TYPE_*
[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  *  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->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->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         dput(sbi->root);
144         sbi->root = NULL;
145         shrink_dcache_sb(sbi->sb);
146
147         return;
148 }
149
150 static void autofs4_put_super(struct super_block *sb)
151 {
152         struct autofs_sb_info *sbi = autofs4_sbi(sb);
153
154         sb->s_fs_info = NULL;
155
156         if ( !sbi->catatonic )
157                 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
158
159         /* Clean up and release dangling references */
160         if (sbi)
161                 autofs4_force_release(sbi);
162
163         kfree(sbi);
164
165         DPRINTK("shutting down");
166 }
167
168 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
169 {
170         struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
171
172         if (!sbi)
173                 return 0;
174
175         seq_printf(m, ",fd=%d", sbi->pipefd);
176         seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
177         seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
178         seq_printf(m, ",minproto=%d", sbi->min_proto);
179         seq_printf(m, ",maxproto=%d", sbi->max_proto);
180
181         if (sbi->type & AUTOFS_TYPE_OFFSET)
182                 seq_printf(m, ",offset");
183         else if (sbi->type & AUTOFS_TYPE_DIRECT)
184                 seq_printf(m, ",direct");
185         else
186                 seq_printf(m, ",indirect");
187
188         return 0;
189 }
190
191 static struct super_operations autofs4_sops = {
192         .put_super      = autofs4_put_super,
193         .statfs         = simple_statfs,
194         .show_options   = autofs4_show_options,
195 };
196
197 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
198         Opt_indirect, Opt_direct, Opt_offset};
199
200 static match_table_t tokens = {
201         {Opt_fd, "fd=%u"},
202         {Opt_uid, "uid=%u"},
203         {Opt_gid, "gid=%u"},
204         {Opt_pgrp, "pgrp=%u"},
205         {Opt_minproto, "minproto=%u"},
206         {Opt_maxproto, "maxproto=%u"},
207         {Opt_indirect, "indirect"},
208         {Opt_direct, "direct"},
209         {Opt_offset, "offset"},
210         {Opt_err, NULL}
211 };
212
213 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
214                          pid_t *pgrp, unsigned int *type,
215                          int *minproto, int *maxproto)
216 {
217         char *p;
218         substring_t args[MAX_OPT_ARGS];
219         int option;
220
221         *uid = current->uid;
222         *gid = current->gid;
223         *pgrp = process_group(current);
224
225         *minproto = AUTOFS_MIN_PROTO_VERSION;
226         *maxproto = AUTOFS_MAX_PROTO_VERSION;
227
228         *pipefd = -1;
229
230         if (!options)
231                 return 1;
232
233         while ((p = strsep(&options, ",")) != NULL) {
234                 int token;
235                 if (!*p)
236                         continue;
237
238                 token = match_token(p, tokens, args);
239                 switch (token) {
240                 case Opt_fd:
241                         if (match_int(args, pipefd))
242                                 return 1;
243                         break;
244                 case Opt_uid:
245                         if (match_int(args, &option))
246                                 return 1;
247                         *uid = option;
248                         break;
249                 case Opt_gid:
250                         if (match_int(args, &option))
251                                 return 1;
252                         *gid = option;
253                         break;
254                 case Opt_pgrp:
255                         if (match_int(args, &option))
256                                 return 1;
257                         *pgrp = option;
258                         break;
259                 case Opt_minproto:
260                         if (match_int(args, &option))
261                                 return 1;
262                         *minproto = option;
263                         break;
264                 case Opt_maxproto:
265                         if (match_int(args, &option))
266                                 return 1;
267                         *maxproto = option;
268                         break;
269                 case Opt_indirect:
270                         *type = AUTOFS_TYPE_INDIRECT;
271                         break;
272                 case Opt_direct:
273                         *type = AUTOFS_TYPE_DIRECT;
274                         break;
275                 case Opt_offset:
276                         *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
277                         break;
278                 default:
279                         return 1;
280                 }
281         }
282         return (*pipefd < 0);
283 }
284
285 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
286 {
287         struct autofs_info *ino;
288
289         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
290         if (!ino)
291                 return NULL;
292
293         return ino;
294 }
295
296 void autofs4_dentry_release(struct dentry *);
297 static struct dentry_operations autofs4_sb_dentry_operations = {
298         .d_release      = autofs4_dentry_release,
299 };
300
301 int autofs4_fill_super(struct super_block *s, void *data, int silent)
302 {
303         struct inode * root_inode;
304         struct dentry * root;
305         struct file * pipe;
306         int pipefd;
307         struct autofs_sb_info *sbi;
308         struct autofs_info *ino;
309
310         sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
311         if ( !sbi )
312                 goto fail_unlock;
313         DPRINTK("starting up, sbi = %p",sbi);
314
315         memset(sbi, 0, sizeof(*sbi));
316
317         s->s_fs_info = sbi;
318         sbi->magic = AUTOFS_SBI_MAGIC;
319         sbi->root = NULL;
320         sbi->pipefd = -1;
321         sbi->catatonic = 0;
322         sbi->exp_timeout = 0;
323         sbi->oz_pgrp = process_group(current);
324         sbi->sb = s;
325         sbi->version = 0;
326         sbi->sub_version = 0;
327         sbi->type = 0;
328         sbi->min_proto = 0;
329         sbi->max_proto = 0;
330         mutex_init(&sbi->wq_mutex);
331         spin_lock_init(&sbi->fs_lock);
332         sbi->queues = NULL;
333         s->s_blocksize = 1024;
334         s->s_blocksize_bits = 10;
335         s->s_magic = AUTOFS_SUPER_MAGIC;
336         s->s_op = &autofs4_sops;
337         s->s_time_gran = 1;
338
339         /*
340          * Get the root inode and dentry, but defer checking for errors.
341          */
342         ino = autofs4_mkroot(sbi);
343         if (!ino)
344                 goto fail_free;
345         root_inode = autofs4_get_inode(s, ino);
346         if (!root_inode)
347                 goto fail_ino;
348
349         root = d_alloc_root(root_inode);
350         if (!root)
351                 goto fail_iput;
352         pipe = NULL;
353
354         root->d_op = &autofs4_sb_dentry_operations;
355         root->d_fsdata = ino;
356
357         /* Can this call block? */
358         if (parse_options(data, &pipefd,
359                           &root_inode->i_uid, &root_inode->i_gid,
360                           &sbi->oz_pgrp, &sbi->type,
361                           &sbi->min_proto, &sbi->max_proto)) {
362                 printk("autofs: called with bogus options\n");
363                 goto fail_dput;
364         }
365
366         root_inode->i_fop = &autofs4_root_operations;
367         root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
368                         &autofs4_direct_root_inode_operations :
369                         &autofs4_indirect_root_inode_operations;
370
371         /* Couldn't this be tested earlier? */
372         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
373             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
374                 printk("autofs: kernel does not match daemon version "
375                        "daemon (%d, %d) kernel (%d, %d)\n",
376                         sbi->min_proto, sbi->max_proto,
377                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
378                 goto fail_dput;
379         }
380
381         /* Establish highest kernel protocol version */
382         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
383                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
384         else
385                 sbi->version = sbi->max_proto;
386         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
387
388         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
389         pipe = fget(pipefd);
390         
391         if ( !pipe ) {
392                 printk("autofs: could not open pipe file descriptor\n");
393                 goto fail_dput;
394         }
395         if ( !pipe->f_op || !pipe->f_op->write )
396                 goto fail_fput;
397         sbi->pipe = pipe;
398         sbi->pipefd = pipefd;
399
400         /*
401          * Take a reference to the root dentry so we get a chance to
402          * clean up the dentry tree on umount.
403          * See autofs4_force_release.
404          */
405         sbi->root = dget(root);
406
407         /*
408          * Success! Install the root dentry now to indicate completion.
409          */
410         s->s_root = root;
411         return 0;
412         
413         /*
414          * Failure ... clean up.
415          */
416 fail_fput:
417         printk("autofs: pipe file descriptor does not contain proper ops\n");
418         fput(pipe);
419         /* fall through */
420 fail_dput:
421         dput(root);
422         goto fail_free;
423 fail_iput:
424         printk("autofs: get root dentry failed\n");
425         iput(root_inode);
426 fail_ino:
427         kfree(ino);
428 fail_free:
429         kfree(sbi);
430 fail_unlock:
431         return -EINVAL;
432 }
433
434 struct inode *autofs4_get_inode(struct super_block *sb,
435                                 struct autofs_info *inf)
436 {
437         struct inode *inode = new_inode(sb);
438
439         if (inode == NULL)
440                 return NULL;
441
442         inf->inode = inode;
443         inode->i_mode = inf->mode;
444         if (sb->s_root) {
445                 inode->i_uid = sb->s_root->d_inode->i_uid;
446                 inode->i_gid = sb->s_root->d_inode->i_gid;
447         } else {
448                 inode->i_uid = 0;
449                 inode->i_gid = 0;
450         }
451         inode->i_blksize = PAGE_CACHE_SIZE;
452         inode->i_blocks = 0;
453         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
454
455         if (S_ISDIR(inf->mode)) {
456                 inode->i_nlink = 2;
457                 inode->i_op = &autofs4_dir_inode_operations;
458                 inode->i_fop = &autofs4_dir_operations;
459         } else if (S_ISLNK(inf->mode)) {
460                 inode->i_size = inf->size;
461                 inode->i_op = &autofs4_symlink_inode_operations;
462         }
463
464         return inode;
465 }