Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[pandora-kernel.git] / fs / autofs4 / root.c
1 /* -*- c -*- --------------------------------------------------------------- *
2  *
3  * linux/fs/autofs/root.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *  Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7  *  Copyright 2001-2003 Ian Kent <raven@themaw.net>
8  *
9  * This file is part of the Linux kernel and is made available under
10  * the terms of the GNU General Public License, version 2, or at your
11  * option, any later version, incorporated herein by reference.
12  *
13  * ------------------------------------------------------------------------- */
14
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include <linux/smp_lock.h>
21 #include "autofs_i.h"
22
23 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24 static int autofs4_dir_unlink(struct inode *,struct dentry *);
25 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28 static int autofs4_dir_open(struct inode *inode, struct file *file);
29 static int autofs4_dir_close(struct inode *inode, struct file *file);
30 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
33 static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
34
35 struct file_operations autofs4_root_operations = {
36         .open           = dcache_dir_open,
37         .release        = dcache_dir_close,
38         .read           = generic_read_dir,
39         .readdir        = autofs4_root_readdir,
40         .ioctl          = autofs4_root_ioctl,
41 };
42
43 struct file_operations autofs4_dir_operations = {
44         .open           = autofs4_dir_open,
45         .release        = autofs4_dir_close,
46         .read           = generic_read_dir,
47         .readdir        = autofs4_dir_readdir,
48 };
49
50 struct inode_operations autofs4_root_inode_operations = {
51         .lookup         = autofs4_lookup,
52         .unlink         = autofs4_dir_unlink,
53         .symlink        = autofs4_dir_symlink,
54         .mkdir          = autofs4_dir_mkdir,
55         .rmdir          = autofs4_dir_rmdir,
56 };
57
58 struct inode_operations autofs4_dir_inode_operations = {
59         .lookup         = autofs4_lookup,
60         .unlink         = autofs4_dir_unlink,
61         .symlink        = autofs4_dir_symlink,
62         .mkdir          = autofs4_dir_mkdir,
63         .rmdir          = autofs4_dir_rmdir,
64 };
65
66 static int autofs4_root_readdir(struct file *file, void *dirent,
67                                 filldir_t filldir)
68 {
69         struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
70         int oz_mode = autofs4_oz_mode(sbi);
71
72         DPRINTK("called, filp->f_pos = %lld", file->f_pos);
73
74         /*
75          * Don't set reghost flag if:
76          * 1) f_pos is larger than zero -- we've already been here.
77          * 2) we haven't even enabled reghosting in the 1st place.
78          * 3) this is the daemon doing a readdir
79          */
80         if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
81                 sbi->needs_reghost = 1;
82
83         DPRINTK("needs_reghost = %d", sbi->needs_reghost);
84
85         return autofs4_dcache_readdir(file, dirent, filldir);
86 }
87
88 /* Update usage from here to top of tree, so that scan of
89    top-level directories will give a useful result */
90 static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry)
91 {
92         struct dentry *top = dentry->d_sb->s_root;
93
94         spin_lock(&dcache_lock);
95         for(; dentry != top; dentry = dentry->d_parent) {
96                 struct autofs_info *ino = autofs4_dentry_ino(dentry);
97
98                 if (ino) {
99                         touch_atime(mnt, dentry);
100                         ino->last_used = jiffies;
101                 }
102         }
103         spin_unlock(&dcache_lock);
104 }
105
106 /*
107  * From 2.4 kernel readdir.c
108  */
109 static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
110 {
111         int i;
112         struct dentry *dentry = filp->f_dentry;
113
114         i = filp->f_pos;
115         switch (i) {
116                 case 0:
117                         if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
118                                 break;
119                         i++;
120                         filp->f_pos++;
121                         /* fallthrough */
122                 case 1:
123                         if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
124                                 break;
125                         i++;
126                         filp->f_pos++;
127                         /* fallthrough */
128                 default: {
129                         struct list_head *list;
130                         int j = i-2;
131
132                         spin_lock(&dcache_lock);
133                         list = dentry->d_subdirs.next;
134
135                         for (;;) {
136                                 if (list == &dentry->d_subdirs) {
137                                         spin_unlock(&dcache_lock);
138                                         return 0;
139                                 }
140                                 if (!j)
141                                         break;
142                                 j--;
143                                 list = list->next;
144                         }
145
146                         while(1) {
147                                 struct dentry *de = list_entry(list,
148                                                 struct dentry, d_u.d_child);
149
150                                 if (!d_unhashed(de) && de->d_inode) {
151                                         spin_unlock(&dcache_lock);
152                                         if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
153                                                 break;
154                                         spin_lock(&dcache_lock);
155                                 }
156                                 filp->f_pos++;
157                                 list = list->next;
158                                 if (list != &dentry->d_subdirs)
159                                         continue;
160                                 spin_unlock(&dcache_lock);
161                                 break;
162                         }
163                 }
164         }
165         return 0;
166 }
167
168 static int autofs4_dir_open(struct inode *inode, struct file *file)
169 {
170         struct dentry *dentry = file->f_dentry;
171         struct vfsmount *mnt = file->f_vfsmnt;
172         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
173         int status;
174
175         DPRINTK("file=%p dentry=%p %.*s",
176                 file, dentry, dentry->d_name.len, dentry->d_name.name);
177
178         if (autofs4_oz_mode(sbi))
179                 goto out;
180
181         if (autofs4_ispending(dentry)) {
182                 DPRINTK("dentry busy");
183                 return -EBUSY;
184         }
185
186         if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
187                 struct nameidata nd;
188                 int empty;
189
190                 /* In case there are stale directory dentrys from a failed mount */
191                 spin_lock(&dcache_lock);
192                 empty = list_empty(&dentry->d_subdirs);
193                 spin_unlock(&dcache_lock);
194
195                 if (!empty)
196                         d_invalidate(dentry);
197
198                 nd.flags = LOOKUP_DIRECTORY;
199                 status = (dentry->d_op->d_revalidate)(dentry, &nd);
200
201                 if (!status)
202                         return -ENOENT;
203         }
204
205         if (d_mountpoint(dentry)) {
206                 struct file *fp = NULL;
207                 struct vfsmount *fp_mnt = mntget(mnt);
208                 struct dentry *fp_dentry = dget(dentry);
209
210                 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
211                         dput(fp_dentry);
212                         mntput(fp_mnt);
213                         return -ENOENT;
214                 }
215
216                 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
217                 status = PTR_ERR(fp);
218                 if (IS_ERR(fp)) {
219                         file->private_data = NULL;
220                         return status;
221                 }
222                 file->private_data = fp;
223         }
224 out:
225         return 0;
226 }
227
228 static int autofs4_dir_close(struct inode *inode, struct file *file)
229 {
230         struct dentry *dentry = file->f_dentry;
231         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
232
233         DPRINTK("file=%p dentry=%p %.*s",
234                 file, dentry, dentry->d_name.len, dentry->d_name.name);
235
236         if (autofs4_oz_mode(sbi))
237                 goto out;
238
239         if (autofs4_ispending(dentry)) {
240                 DPRINTK("dentry busy");
241                 return -EBUSY;
242         }
243
244         if (d_mountpoint(dentry)) {
245                 struct file *fp = file->private_data;
246
247                 if (!fp)
248                         return -ENOENT;
249
250                 filp_close(fp, current->files);
251                 file->private_data = NULL;
252         }
253 out:
254         return 0;
255 }
256
257 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
258 {
259         struct dentry *dentry = file->f_dentry;
260         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
261         int status;
262
263         DPRINTK("file=%p dentry=%p %.*s",
264                 file, dentry, dentry->d_name.len, dentry->d_name.name);
265
266         if (autofs4_oz_mode(sbi))
267                 goto out;
268
269         if (autofs4_ispending(dentry)) {
270                 DPRINTK("dentry busy");
271                 return -EBUSY;
272         }
273
274         if (d_mountpoint(dentry)) {
275                 struct file *fp = file->private_data;
276
277                 if (!fp)
278                         return -ENOENT;
279
280                 if (!fp->f_op || !fp->f_op->readdir)
281                         goto out;
282
283                 status = vfs_readdir(fp, filldir, dirent);
284                 file->f_pos = fp->f_pos;
285                 if (status)
286                         autofs4_copy_atime(file, fp);
287                 return status;
288         }
289 out:
290         return autofs4_dcache_readdir(file, dirent, filldir);
291 }
292
293 static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
294 {
295         struct super_block *sb = mnt->mnt_sb;
296         struct autofs_sb_info *sbi = autofs4_sbi(sb);
297         struct autofs_info *de_info = autofs4_dentry_ino(dentry);
298         int status = 0;
299
300         /* Block on any pending expiry here; invalidate the dentry
301            when expiration is done to trigger mount request with a new
302            dentry */
303         if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
304                 DPRINTK("waiting for expire %p name=%.*s",
305                          dentry, dentry->d_name.len, dentry->d_name.name);
306
307                 status = autofs4_wait(sbi, dentry, NFY_NONE);
308                 
309                 DPRINTK("expire done status=%d", status);
310                 
311                 /*
312                  * If the directory still exists the mount request must
313                  * continue otherwise it can't be followed at the right
314                  * time during the walk.
315                  */
316                 status = d_invalidate(dentry);
317                 if (status != -EBUSY)
318                         return 0;
319         }
320
321         DPRINTK("dentry=%p %.*s ino=%p",
322                  dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
323
324         /* Wait for a pending mount, triggering one if there isn't one already */
325         if (dentry->d_inode == NULL) {
326                 DPRINTK("waiting for mount name=%.*s",
327                          dentry->d_name.len, dentry->d_name.name);
328
329                 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
330                  
331                 DPRINTK("mount done status=%d", status);
332
333                 if (status && dentry->d_inode)
334                         return 0; /* Try to get the kernel to invalidate this dentry */
335                 
336                 /* Turn this into a real negative dentry? */
337                 if (status == -ENOENT) {
338                         dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
339                         spin_lock(&dentry->d_lock);
340                         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
341                         spin_unlock(&dentry->d_lock);
342                         return 1;
343                 } else if (status) {
344                         /* Return a negative dentry, but leave it "pending" */
345                         return 1;
346                 }
347         /* Trigger mount for path component or follow link */
348         } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
349                         current->link_count) {
350                 DPRINTK("waiting for mount name=%.*s",
351                         dentry->d_name.len, dentry->d_name.name);
352
353                 spin_lock(&dentry->d_lock);
354                 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
355                 spin_unlock(&dentry->d_lock);
356                 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
357
358                 DPRINTK("mount done status=%d", status);
359
360                 if (status) {
361                         spin_lock(&dentry->d_lock);
362                         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
363                         spin_unlock(&dentry->d_lock);
364                         return 0;
365                 }
366         }
367
368         /* We don't update the usages for the autofs daemon itself, this
369            is necessary for recursive autofs mounts */
370         if (!autofs4_oz_mode(sbi))
371                 autofs4_update_usage(mnt, dentry);
372
373         spin_lock(&dentry->d_lock);
374         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
375         spin_unlock(&dentry->d_lock);
376         return 1;
377 }
378
379 /*
380  * Revalidate is called on every cache lookup.  Some of those
381  * cache lookups may actually happen while the dentry is not
382  * yet completely filled in, and revalidate has to delay such
383  * lookups..
384  */
385 static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
386 {
387         struct inode * dir = dentry->d_parent->d_inode;
388         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
389         int oz_mode = autofs4_oz_mode(sbi);
390         int flags = nd ? nd->flags : 0;
391         int status = 1;
392
393         /* Pending dentry */
394         if (autofs4_ispending(dentry)) {
395                 if (!oz_mode)
396                         status = try_to_fill_dentry(nd->mnt, dentry, flags);
397                 return status;
398         }
399
400         /* Negative dentry.. invalidate if "old" */
401         if (dentry->d_inode == NULL)
402                 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
403
404         /* Check for a non-mountpoint directory with no contents */
405         spin_lock(&dcache_lock);
406         if (S_ISDIR(dentry->d_inode->i_mode) &&
407             !d_mountpoint(dentry) && 
408             list_empty(&dentry->d_subdirs)) {
409                 DPRINTK("dentry=%p %.*s, emptydir",
410                          dentry, dentry->d_name.len, dentry->d_name.name);
411                 spin_unlock(&dcache_lock);
412                 if (!oz_mode)
413                         status = try_to_fill_dentry(nd->mnt, dentry, flags);
414                 return status;
415         }
416         spin_unlock(&dcache_lock);
417
418         /* Update the usage list */
419         if (!oz_mode)
420                 autofs4_update_usage(nd->mnt, dentry);
421
422         return 1;
423 }
424
425 static void autofs4_dentry_release(struct dentry *de)
426 {
427         struct autofs_info *inf;
428
429         DPRINTK("releasing %p", de);
430
431         inf = autofs4_dentry_ino(de);
432         de->d_fsdata = NULL;
433
434         if (inf) {
435                 inf->dentry = NULL;
436                 inf->inode = NULL;
437
438                 autofs4_free_ino(inf);
439         }
440 }
441
442 /* For dentries of directories in the root dir */
443 static struct dentry_operations autofs4_root_dentry_operations = {
444         .d_revalidate   = autofs4_revalidate,
445         .d_release      = autofs4_dentry_release,
446 };
447
448 /* For other dentries */
449 static struct dentry_operations autofs4_dentry_operations = {
450         .d_revalidate   = autofs4_revalidate,
451         .d_release      = autofs4_dentry_release,
452 };
453
454 /* Lookups in the root directory */
455 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
456 {
457         struct autofs_sb_info *sbi;
458         int oz_mode;
459
460         DPRINTK("name = %.*s",
461                 dentry->d_name.len, dentry->d_name.name);
462
463         if (dentry->d_name.len > NAME_MAX)
464                 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
465
466         sbi = autofs4_sbi(dir->i_sb);
467
468         oz_mode = autofs4_oz_mode(sbi);
469         DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
470                  current->pid, process_group(current), sbi->catatonic, oz_mode);
471
472         /*
473          * Mark the dentry incomplete, but add it. This is needed so
474          * that the VFS layer knows about the dentry, and we can count
475          * on catching any lookups through the revalidate.
476          *
477          * Let all the hard work be done by the revalidate function that
478          * needs to be able to do this anyway..
479          *
480          * We need to do this before we release the directory semaphore.
481          */
482         dentry->d_op = &autofs4_root_dentry_operations;
483
484         if (!oz_mode) {
485                 spin_lock(&dentry->d_lock);
486                 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
487                 spin_unlock(&dentry->d_lock);
488         }
489         dentry->d_fsdata = NULL;
490         d_add(dentry, NULL);
491
492         if (dentry->d_op && dentry->d_op->d_revalidate) {
493                 mutex_unlock(&dir->i_mutex);
494                 (dentry->d_op->d_revalidate)(dentry, nd);
495                 mutex_lock(&dir->i_mutex);
496         }
497
498         /*
499          * If we are still pending, check if we had to handle
500          * a signal. If so we can force a restart..
501          */
502         if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
503                 /* See if we were interrupted */
504                 if (signal_pending(current)) {
505                         sigset_t *sigset = &current->pending.signal;
506                         if (sigismember (sigset, SIGKILL) ||
507                             sigismember (sigset, SIGQUIT) ||
508                             sigismember (sigset, SIGINT)) {
509                             return ERR_PTR(-ERESTARTNOINTR);
510                         }
511                 }
512         }
513
514         /*
515          * If this dentry is unhashed, then we shouldn't honour this
516          * lookup even if the dentry is positive.  Returning ENOENT here
517          * doesn't do the right thing for all system calls, but it should
518          * be OK for the operations we permit from an autofs.
519          */
520         if ( dentry->d_inode && d_unhashed(dentry) )
521                 return ERR_PTR(-ENOENT);
522
523         return NULL;
524 }
525
526 static int autofs4_dir_symlink(struct inode *dir, 
527                                struct dentry *dentry,
528                                const char *symname)
529 {
530         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
531         struct autofs_info *ino = autofs4_dentry_ino(dentry);
532         struct inode *inode;
533         char *cp;
534
535         DPRINTK("%s <- %.*s", symname,
536                 dentry->d_name.len, dentry->d_name.name);
537
538         if (!autofs4_oz_mode(sbi))
539                 return -EACCES;
540
541         ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
542         if (ino == NULL)
543                 return -ENOSPC;
544
545         ino->size = strlen(symname);
546         ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
547
548         if (cp == NULL) {
549                 kfree(ino);
550                 return -ENOSPC;
551         }
552
553         strcpy(cp, symname);
554
555         inode = autofs4_get_inode(dir->i_sb, ino);
556         d_instantiate(dentry, inode);
557
558         if (dir == dir->i_sb->s_root->d_inode)
559                 dentry->d_op = &autofs4_root_dentry_operations;
560         else
561                 dentry->d_op = &autofs4_dentry_operations;
562
563         dentry->d_fsdata = ino;
564         ino->dentry = dget(dentry);
565         ino->inode = inode;
566
567         dir->i_mtime = CURRENT_TIME;
568
569         return 0;
570 }
571
572 /*
573  * NOTE!
574  *
575  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
576  * that the file no longer exists. However, doing that means that the
577  * VFS layer can turn the dentry into a negative dentry.  We don't want
578  * this, because since the unlink is probably the result of an expire.
579  * We simply d_drop it, which allows the dentry lookup to remount it
580  * if necessary.
581  *
582  * If a process is blocked on the dentry waiting for the expire to finish,
583  * it will invalidate the dentry and try to mount with a new one.
584  *
585  * Also see autofs4_dir_rmdir()..
586  */
587 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
588 {
589         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
590         struct autofs_info *ino = autofs4_dentry_ino(dentry);
591         
592         /* This allows root to remove symlinks */
593         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
594                 return -EACCES;
595
596         dput(ino->dentry);
597
598         dentry->d_inode->i_size = 0;
599         dentry->d_inode->i_nlink = 0;
600
601         dir->i_mtime = CURRENT_TIME;
602
603         d_drop(dentry);
604
605         return 0;
606 }
607
608 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
609 {
610         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
611         struct autofs_info *ino = autofs4_dentry_ino(dentry);
612         
613         if (!autofs4_oz_mode(sbi))
614                 return -EACCES;
615
616         spin_lock(&dcache_lock);
617         if (!list_empty(&dentry->d_subdirs)) {
618                 spin_unlock(&dcache_lock);
619                 return -ENOTEMPTY;
620         }
621         spin_lock(&dentry->d_lock);
622         __d_drop(dentry);
623         spin_unlock(&dentry->d_lock);
624         spin_unlock(&dcache_lock);
625
626         dput(ino->dentry);
627
628         dentry->d_inode->i_size = 0;
629         dentry->d_inode->i_nlink = 0;
630
631         if (dir->i_nlink)
632                 dir->i_nlink--;
633
634         return 0;
635 }
636
637 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
638 {
639         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
640         struct autofs_info *ino = autofs4_dentry_ino(dentry);
641         struct inode *inode;
642
643         if ( !autofs4_oz_mode(sbi) )
644                 return -EACCES;
645
646         DPRINTK("dentry %p, creating %.*s",
647                 dentry, dentry->d_name.len, dentry->d_name.name);
648
649         ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
650         if (ino == NULL)
651                 return -ENOSPC;
652
653         inode = autofs4_get_inode(dir->i_sb, ino);
654         d_instantiate(dentry, inode);
655
656         if (dir == dir->i_sb->s_root->d_inode)
657                 dentry->d_op = &autofs4_root_dentry_operations;
658         else
659                 dentry->d_op = &autofs4_dentry_operations;
660
661         dentry->d_fsdata = ino;
662         ino->dentry = dget(dentry);
663         ino->inode = inode;
664         dir->i_nlink++;
665         dir->i_mtime = CURRENT_TIME;
666
667         return 0;
668 }
669
670 /* Get/set timeout ioctl() operation */
671 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
672                                          unsigned long __user *p)
673 {
674         int rv;
675         unsigned long ntimeout;
676
677         if ( (rv = get_user(ntimeout, p)) ||
678              (rv = put_user(sbi->exp_timeout/HZ, p)) )
679                 return rv;
680
681         if ( ntimeout > ULONG_MAX/HZ )
682                 sbi->exp_timeout = 0;
683         else
684                 sbi->exp_timeout = ntimeout * HZ;
685
686         return 0;
687 }
688
689 /* Return protocol version */
690 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
691 {
692         return put_user(sbi->version, p);
693 }
694
695 /* Return protocol sub version */
696 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
697 {
698         return put_user(sbi->sub_version, p);
699 }
700
701 /*
702  * Tells the daemon whether we need to reghost or not. Also, clears
703  * the reghost_needed flag.
704  */
705 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
706 {
707         int status;
708
709         DPRINTK("returning %d", sbi->needs_reghost);
710
711         status = put_user(sbi->needs_reghost, p);
712         if ( status )
713                 return status;
714
715         sbi->needs_reghost = 0;
716         return 0;
717 }
718
719 /*
720  * Enable / Disable reghosting ioctl() operation
721  */
722 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
723 {
724         int status;
725         int val;
726
727         status = get_user(val, p);
728
729         DPRINTK("reghost = %d", val);
730
731         if (status)
732                 return status;
733
734         /* turn on/off reghosting, with the val */
735         sbi->reghost_enabled = val;
736         return 0;
737 }
738
739 /*
740 * Tells the daemon whether it can umount the autofs mount.
741 */
742 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
743 {
744         int status = 0;
745
746         if (may_umount(mnt) == 0)
747                 status = 1;
748
749         DPRINTK("returning %d", status);
750
751         status = put_user(status, p);
752
753         return status;
754 }
755
756 /* Identify autofs4_dentries - this is so we can tell if there's
757    an extra dentry refcount or not.  We only hold a refcount on the
758    dentry if its non-negative (ie, d_inode != NULL)
759 */
760 int is_autofs4_dentry(struct dentry *dentry)
761 {
762         return dentry && dentry->d_inode &&
763                 (dentry->d_op == &autofs4_root_dentry_operations ||
764                  dentry->d_op == &autofs4_dentry_operations) &&
765                 dentry->d_fsdata != NULL;
766 }
767
768 /*
769  * ioctl()'s on the root directory is the chief method for the daemon to
770  * generate kernel reactions
771  */
772 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
773                              unsigned int cmd, unsigned long arg)
774 {
775         struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
776         void __user *p = (void __user *)arg;
777
778         DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
779                 cmd,arg,sbi,process_group(current));
780
781         if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
782              _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
783                 return -ENOTTY;
784         
785         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
786                 return -EPERM;
787         
788         switch(cmd) {
789         case AUTOFS_IOC_READY:  /* Wait queue: go ahead and retry */
790                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
791         case AUTOFS_IOC_FAIL:   /* Wait queue: fail with ENOENT */
792                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
793         case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
794                 autofs4_catatonic_mode(sbi);
795                 return 0;
796         case AUTOFS_IOC_PROTOVER: /* Get protocol version */
797                 return autofs4_get_protover(sbi, p);
798         case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
799                 return autofs4_get_protosubver(sbi, p);
800         case AUTOFS_IOC_SETTIMEOUT:
801                 return autofs4_get_set_timeout(sbi, p);
802
803         case AUTOFS_IOC_TOGGLEREGHOST:
804                 return autofs4_toggle_reghost(sbi, p);
805         case AUTOFS_IOC_ASKREGHOST:
806                 return autofs4_ask_reghost(sbi, p);
807
808         case AUTOFS_IOC_ASKUMOUNT:
809                 return autofs4_ask_umount(filp->f_vfsmnt, p);
810
811         /* return a single thing to expire */
812         case AUTOFS_IOC_EXPIRE:
813                 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
814         /* same as above, but can send multiple expires through pipe */
815         case AUTOFS_IOC_EXPIRE_MULTI:
816                 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
817
818         default:
819                 return -ENOSYS;
820         }
821 }