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